Repair commits 317aba70e et al for -DWRITE_READ_PARSE_PLAN_TREES.
authorTom Lane <[email protected]>
Thu, 13 Mar 2025 16:13:07 +0000 (12:13 -0400)
committerTom Lane <[email protected]>
Thu, 13 Mar 2025 16:13:07 +0000 (12:13 -0400)
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 f4e7756ef, 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 <[email protected]>
Author: Tom Lane <[email protected]>
Reviewed-by: Dean Rasheed <[email protected]>
Discussion: https://postgr.es/m/3518c50a-ab18-482f-b916-a37263622501@deepbluecap.com
Backpatch-through: 13-15

src/backend/optimizer/plan/setrefs.c
src/backend/rewrite/rewriteHandler.c
src/backend/tcop/postgres.c
src/include/nodes/parsenodes.h

index 4d05d59c5bd6fdc9d9011ab4e628adbc58c94c82..59387c21b510b0c81a104f24418d2894751b1b90 100644 (file)
@@ -512,6 +512,15 @@ add_rte_to_flat_rtable(PlannerGlobal *glob, RangeTblEntry *rte)
    newrte->colcollations = NIL;
    newrte->securityQuals = NIL;
 
+   /*
+    * Also, if it's a subquery RTE, lose the relid that may have been kept to
+    * signal that it had been a view.  We don't want that to escape the
+    * planner, mainly because doing so breaks -DWRITE_READ_PARSE_PLAN_TREES
+    * testing thanks to outfuncs/readfuncs not preserving it.
+    */
+   if (newrte->rtekind == RTE_SUBQUERY)
+       newrte->relid = InvalidOid;
+
    glob->finalrtable = lappend(glob->finalrtable, newrte);
 
    /*
index 30ae22e43df03c16fe4ca706255f0e3b8d052d20..aa0a3bfe89b0c866426a266b8450701236301130 100644 (file)
@@ -1862,8 +1862,8 @@ ApplyRetrieveRule(Query *parsetree,
 
    /*
     * Clear fields that should not be set in a subquery RTE.  However, we
-    * retain the relid to support correct operation of makeWholeRowVar during
-    * planning.
+    * retain the relid for now, to support correct operation of
+    * makeWholeRowVar during planning.
     */
    rte->relkind = 0;
    rte->rellockmode = 0;
index 8a464fcf150068627f7f74bc8a9a92b57d63958b..a6a13dc71baa8848eed01ce7b88b84cf577a025c 100644 (file)
@@ -813,45 +813,10 @@ pg_rewrite_query(Query *query)
    }
 #endif
 
-#ifdef WRITE_READ_PARSE_PLAN_TREES
-   /* Optional debugging check: pass querytree through outfuncs/readfuncs */
-   {
-       List       *new_list = NIL;
-       ListCell   *lc;
-
-       /*
-        * We currently lack outfuncs/readfuncs support for most utility
-        * statement types, so only attempt to write/read non-utility queries.
-        */
-       foreach(lc, querytree_list)
-       {
-           Query      *query = lfirst_node(Query, lc);
-
-           if (query->commandType != CMD_UTILITY)
-           {
-               char       *str = nodeToString(query);
-               Query      *new_query = stringToNodeWithLocations(str);
-
-               /*
-                * queryId is not saved in stored rules, but we must preserve
-                * it here to avoid breaking pg_stat_statements.
-                */
-               new_query->queryId = query->queryId;
-
-               new_list = lappend(new_list, new_query);
-               pfree(str);
-           }
-           else
-               new_list = lappend(new_list, query);
-       }
-
-       /* This checks both outfuncs/readfuncs and the equal() routines... */
-       if (!equal(new_list, querytree_list))
-           elog(WARNING, "outfuncs/readfuncs failed to produce equal parse tree");
-       else
-           querytree_list = new_list;
-   }
-#endif
+   /*
+    * We don't apply WRITE_READ_PARSE_PLAN_TREES to rewritten query trees,
+    * because it breaks the hack of preserving relid for rewritten views.
+    */
 
    if (Debug_print_rewritten)
        elog_node_display(LOG, "rewritten parse tree", querytree_list,
index abc87c59e6ff8c95803b891b3127c5f5d6f40f1e..a77b5ba1b1464da8d9b00fb0cb1aff9a00ed3bd6 100644 (file)
@@ -1020,8 +1020,8 @@ typedef struct RangeTblEntry
     * 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.)
+    * during planning, cf makeWholeRowVar.  (It will not be passed on to the
+    * executor, however.)
     *
     * 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