]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Make it cheap to check if a relation is modified by a query
authorMelanie Plageman <melanieplageman@gmail.com>
Mon, 30 Mar 2026 13:38:03 +0000 (09:38 -0400)
committerMelanie Plageman <melanieplageman@gmail.com>
Mon, 30 Mar 2026 13:38:03 +0000 (09:38 -0400)
Save the range table indexes of result relations and row mark relations
in separate bitmapsets in the PlannedStmt. Precomputing them allows
cheap membership checks during execution. Together, these two groups
approximate all relations that will be modified by a query. This
includes relations targeted by INSERT, UPDATE, DELETE, and MERGE as well
as relations with any row mark (like SELECT FOR UPDATE).

Future work will use information on whether or not a relation is
modified by a query in a heuristic.

PlannedStmt->resultRelations is only used in a membership check, so it
will be removed in a separate commit.

Author: Melanie Plageman <melanieplageman@gmail.com>
Reviewed-by: Andres Freund <andres@anarazel.de>
Reviewed-by: David Rowley <dgrowleyml@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/F5CDD1B5-628C-44A1-9F85-3958C626F6A9%40gmail.com

src/backend/executor/execParallel.c
src/backend/optimizer/plan/planner.c
src/include/nodes/plannodes.h

index ac84af294c9f636ab4121e0ad36c068075bdad5b..f203ed85f50d529c1f9144d9eff017d02e8cfc6c 100644 (file)
@@ -212,6 +212,13 @@ ExecSerializePlan(Plan *plan, EState *estate)
 
        pstmt->rewindPlanIDs = NULL;
        pstmt->rowMarks = NIL;
+
+       /*
+        * Pass the row mark and result relation relids to parallel workers. They
+        * may need to check them to inform heuristics.
+        */
+       pstmt->rowMarkRelids = estate->es_plannedstmt->rowMarkRelids;
+       pstmt->resultRelationRelids = estate->es_plannedstmt->resultRelationRelids;
        pstmt->relationOids = NIL;
        pstmt->invalItems = NIL;        /* workers can't replan anyway... */
        pstmt->paramExecTypes = estate->es_plannedstmt->paramExecTypes;
index d19800ad6a5218217b387cfeefa22a61b10d58bc..c81eb76ad7517b2ec2b27d179ee320e962927261 100644 (file)
@@ -341,7 +341,8 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions,
        Path       *best_path;
        Plan       *top_plan;
        ListCell   *lp,
-                          *lr;
+                          *lr,
+                          *lc;
 
        /*
         * Set up global state for this planner invocation.  This data is needed
@@ -661,6 +662,18 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions,
        result->subplans = glob->subplans;
        result->rewindPlanIDs = glob->rewindPlanIDs;
        result->rowMarks = glob->finalrowmarks;
+
+       /*
+        * Compute resultRelationRelids and rowMarkRelids from resultRelations and
+        * rowMarks. These can be used for cheap membership checks.
+        */
+       foreach(lc, glob->resultRelations)
+               result->resultRelationRelids = bms_add_member(result->resultRelationRelids,
+                                                                                                         lfirst_int(lc));
+       foreach(lc, glob->finalrowmarks)
+               result->rowMarkRelids = bms_add_member(result->rowMarkRelids,
+                                                                                          ((PlanRowMark *) lfirst(lc))->rti);
+
        result->relationOids = glob->relationOids;
        result->invalItems = glob->invalItems;
        result->paramExecTypes = glob->paramExecTypes;
index b6185825fcb388125b32b091ec2fbd40686b5c7c..ca61ecfc1328b253fa8ced03835cb8150a727433 100644 (file)
@@ -121,6 +121,9 @@ typedef struct PlannedStmt
        /* integer list of RT indexes, or NIL */
        List       *resultRelations;
 
+       /* RT indexes of relations targeted by INSERT/UPDATE/DELETE/MERGE */
+       Bitmapset  *resultRelationRelids;
+
        /* list of AppendRelInfo nodes */
        List       *appendRelations;
 
@@ -138,6 +141,9 @@ typedef struct PlannedStmt
        /* a list of PlanRowMark's */
        List       *rowMarks;
 
+       /* RT indexes of relations with row marks */
+       Bitmapset  *rowMarkRelids;
+
        /* OIDs of relations the plan depends on */
        List       *relationOids;