]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Preserve RangeTblEntry.relid when expanding a view RTE.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 12 Mar 2025 15:28:04 +0000 (11:28 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 12 Mar 2025 15:28:04 +0000 (11:28 -0400)
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 47bb9db75.  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

src/backend/rewrite/rewriteHandler.c
src/include/nodes/parsenodes.h

index d069a9157a38385226719ab384e0f42e207fdb31..f288fab28edd92aa7f171a705ea3e6226bbb3e0b 100644 (file)
@@ -1855,8 +1855,12 @@ ApplyRetrieveRule(Query *parsetree,
        rte->rtekind = RTE_SUBQUERY;
        rte->subquery = rule_action;
        rte->security_barrier = RelationIsSecurityView(relation);
-       /* Clear fields that should not be set in a subquery RTE */
-       rte->relid = InvalidOid;
+
+       /*
+        * Clear fields that should not be set in a subquery RTE.  However, we
+        * retain the relid to support correct operation of makeWholeRowVar during
+        * planning.
+        */
        rte->relkind = 0;
        rte->rellockmode = 0;
        rte->tablesample = NULL;
index b8bf80258ccdacd59577db106ab9026f11c1bc04..155abdef25675d61e0369d4b856083d49d5cbd90 100644 (file)
@@ -996,6 +996,12 @@ typedef struct RangeTblEntry
        /*
         * Fields valid for a plain relation RTE (else zero):
         *
+        * As a special case, relid can also be set in RTE_SUBQUERY RTEs.  This
+        * happens when an RTE_RELATION RTE for a view is transformed to an
+        * RTE_SUBQUERY during rewriting.  We keep the relid because it is useful
+        * during planning, cf makeWholeRowVar.  (It cannot be relied on during
+        * execution, because it will not propagate to parallel workers.)
+        *
         * As a special case, RTE_NAMEDTUPLESTORE can also set relid to indicate
         * that the tuple format of the tuplestore is the same as the referenced
         * relation.  This allows plans referencing AFTER trigger transition