Skip to content

Commit b200180

Browse files
committed
Repair commits 317aba7 et al for -DWRITE_READ_PARSE_PLAN_TREES.
Letting the rewriter keep RangeTblEntry.relid when expanding a view RTE, without making the outfuncs/readfuncs changes that went along with that originally, is more problematic than I realized. It causes WRITE_READ_PARSE_PLAN_TREES testing to fail because outfuncs/readfuncs don't think relid need be saved in an RTE_SUBQUERY RTE. There doesn't seem to be any other good route to fixing the whole-row Var problem solved at f4e7756, so we just have to deal with the consequences. We can make the eventually-produced plan tree safe for WRITE_READ_PARSE_PLAN_TREES by clearing the relid field at the end of planning, as was already being done for the functions field. (The functions field is not problematic here because our abuse of it is strictly local to the planner.) However, there is no nice fix for the post-rewrite WRITE_READ_PARSE_PLAN_TREES test. The solution adopted here is to remove the post-rewrite test in the affected branches. That's surely less than ideal, but a couple of arguments can be made why it's not unacceptable. First, the behavior of outfuncs/readfuncs for parsetrees in these branches is frozen no matter what, because of catalog stability requirements. So we're not testing anything that is going to change. Second, testing WRITE_READ_PARSE_PLAN_TREES at this particular time doesn't correspond to any direct system functionality requirement, neither rule storage nor plan transmission. Reported-by: Andres Freund <andres@anarazel.de> Author: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Dean Rasheed <dean.a.rasheed@gmail.com> Discussion: https://postgr.es/m/3518c50a-ab18-482f-b916-a37263622501@deepbluecap.com Backpatch-through: 13-15
1 parent 39af32f commit b200180

File tree

4 files changed

+17
-43
lines changed

4 files changed

+17
-43
lines changed

src/backend/optimizer/plan/setrefs.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,15 @@ add_rte_to_flat_rtable(PlannerGlobal *glob, RangeTblEntry *rte)
438438
newrte->colcollations = NIL;
439439
newrte->securityQuals = NIL;
440440

441+
/*
442+
* Also, if it's a subquery RTE, lose the relid that may have been kept to
443+
* signal that it had been a view. We don't want that to escape the
444+
* planner, mainly because doing so breaks -DWRITE_READ_PARSE_PLAN_TREES
445+
* testing thanks to outfuncs/readfuncs not preserving it.
446+
*/
447+
if (newrte->rtekind == RTE_SUBQUERY)
448+
newrte->relid = InvalidOid;
449+
441450
glob->finalrtable = lappend(glob->finalrtable, newrte);
442451

443452
/*

src/backend/rewrite/rewriteHandler.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,8 +1828,8 @@ ApplyRetrieveRule(Query *parsetree,
18281828

18291829
/*
18301830
* Clear fields that should not be set in a subquery RTE. However, we
1831-
* retain the relid to support correct operation of makeWholeRowVar during
1832-
* planning.
1831+
* retain the relid for now, to support correct operation of
1832+
* makeWholeRowVar during planning.
18331833
*/
18341834
rte->relkind = 0;
18351835
rte->rellockmode = 0;

src/backend/tcop/postgres.c

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -803,45 +803,10 @@ pg_rewrite_query(Query *query)
803803
}
804804
#endif
805805

806-
#ifdef WRITE_READ_PARSE_PLAN_TREES
807-
/* Optional debugging check: pass querytree through outfuncs/readfuncs */
808-
{
809-
List *new_list = NIL;
810-
ListCell *lc;
811-
812-
/*
813-
* We currently lack outfuncs/readfuncs support for most utility
814-
* statement types, so only attempt to write/read non-utility queries.
815-
*/
816-
foreach(lc, querytree_list)
817-
{
818-
Query *query = castNode(Query, lfirst(lc));
819-
820-
if (query->commandType != CMD_UTILITY)
821-
{
822-
char *str = nodeToString(query);
823-
Query *new_query = stringToNodeWithLocations(str);
824-
825-
/*
826-
* queryId is not saved in stored rules, but we must preserve
827-
* it here to avoid breaking pg_stat_statements.
828-
*/
829-
new_query->queryId = query->queryId;
830-
831-
new_list = lappend(new_list, new_query);
832-
pfree(str);
833-
}
834-
else
835-
new_list = lappend(new_list, query);
836-
}
837-
838-
/* This checks both outfuncs/readfuncs and the equal() routines... */
839-
if (!equal(new_list, querytree_list))
840-
elog(WARNING, "outfuncs/readfuncs failed to produce equal parse tree");
841-
else
842-
querytree_list = new_list;
843-
}
844-
#endif
806+
/*
807+
* We don't apply WRITE_READ_PARSE_PLAN_TREES to rewritten query trees,
808+
* because it breaks the hack of preserving relid for rewritten views.
809+
*/
845810

846811
if (Debug_print_rewritten)
847812
elog_node_display(LOG, "rewritten parse tree", querytree_list,

src/include/nodes/parsenodes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -985,8 +985,8 @@ typedef struct RangeTblEntry
985985
* As a special case, relid can also be set in RTE_SUBQUERY RTEs. This
986986
* happens when an RTE_RELATION RTE for a view is transformed to an
987987
* RTE_SUBQUERY during rewriting. We keep the relid because it is useful
988-
* during planning, cf makeWholeRowVar. (It cannot be relied on during
989-
* execution, because it will not propagate to parallel workers.)
988+
* during planning, cf makeWholeRowVar. (It will not be passed on to the
989+
* executor, however.)
990990
*
991991
* As a special case, RTE_NAMEDTUPLESTORE can also set relid to indicate
992992
* that the tuple format of the tuplestore is the same as the referenced

0 commit comments

Comments
 (0)