Skip to content

Commit 28a7e31

Browse files
committed
Preserve RangeTblEntry.relid when expanding a view RTE.
When the rewriter converts an RTE_RELATION RTE for a view into an RTE_SUBQUERY RTE containing the view's defining query, leave the relid field alone instead of zeroing it. This allows the planner to tell that the subquery came from a view rather than having been written in-line, which is needed to support an upcoming planner bug fix. We cannot change the behavior of the outfuncs/readfuncs code in released branches, so the relid field will not survive in plans passed to parallel workers; therefore this info should not be relied on in the executor. This back-patches a portion of the data structure definitional changes made in v16 and up by commit 47bb9db. It's being committed separately for visibility in the commit log, but with luck it will not actually matter to anyone. While it's not inconceivable that this change will break code expecting relid to be zero in a subquery RTE, we can hope that such code has already been adjusted to cope with v16 and up. Reported-by: Duncan Sands <duncan.sands@deepbluecap.com> Diagnosed-by: Tender Wang <tndrwang@gmail.com> 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 f5b4a0b commit 28a7e31

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/backend/rewrite/rewriteHandler.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,8 +1825,12 @@ ApplyRetrieveRule(Query *parsetree,
18251825
rte->rtekind = RTE_SUBQUERY;
18261826
rte->subquery = rule_action;
18271827
rte->security_barrier = RelationIsSecurityView(relation);
1828-
/* Clear fields that should not be set in a subquery RTE */
1829-
rte->relid = InvalidOid;
1828+
1829+
/*
1830+
* 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.
1833+
*/
18301834
rte->relkind = 0;
18311835
rte->rellockmode = 0;
18321836
rte->tablesample = NULL;

src/include/nodes/parsenodes.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,12 @@ typedef struct RangeTblEntry
982982
/*
983983
* Fields valid for a plain relation RTE (else zero):
984984
*
985+
* As a special case, relid can also be set in RTE_SUBQUERY RTEs. This
986+
* happens when an RTE_RELATION RTE for a view is transformed to an
987+
* 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.)
990+
*
985991
* As a special case, RTE_NAMEDTUPLESTORE can also set relid to indicate
986992
* that the tuple format of the tuplestore is the same as the referenced
987993
* relation. This allows plans referencing AFTER trigger transition

0 commit comments

Comments
 (0)