Skip to content

Commit 584f17a

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 5144e1f commit 584f17a

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
@@ -1855,8 +1855,12 @@ ApplyRetrieveRule(Query *parsetree,
18551855
rte->rtekind = RTE_SUBQUERY;
18561856
rte->subquery = rule_action;
18571857
rte->security_barrier = RelationIsSecurityView(relation);
1858-
/* Clear fields that should not be set in a subquery RTE */
1859-
rte->relid = InvalidOid;
1858+
1859+
/*
1860+
* Clear fields that should not be set in a subquery RTE. However, we
1861+
* retain the relid to support correct operation of makeWholeRowVar during
1862+
* planning.
1863+
*/
18601864
rte->relkind = 0;
18611865
rte->rellockmode = 0;
18621866
rte->tablesample = NULL;

src/include/nodes/parsenodes.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,12 @@ typedef struct RangeTblEntry
996996
/*
997997
* Fields valid for a plain relation RTE (else zero):
998998
*
999+
* As a special case, relid can also be set in RTE_SUBQUERY RTEs. This
1000+
* happens when an RTE_RELATION RTE for a view is transformed to an
1001+
* RTE_SUBQUERY during rewriting. We keep the relid because it is useful
1002+
* during planning, cf makeWholeRowVar. (It cannot be relied on during
1003+
* execution, because it will not propagate to parallel workers.)
1004+
*
9991005
* As a special case, RTE_NAMEDTUPLESTORE can also set relid to indicate
10001006
* that the tuple format of the tuplestore is the same as the referenced
10011007
* relation. This allows plans referencing AFTER trigger transition

0 commit comments

Comments
 (0)