diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c index 595760b19f..0a3fe07049 100644 --- a/src/backend/optimizer/plan/setrefs.c +++ b/src/backend/optimizer/plan/setrefs.c @@ -15,7 +15,9 @@ */ #include "postgres.h" +#include "access/table.h" #include "access/transam.h" +#include "catalog/pg_class.h" #include "catalog/pg_type.h" #include "nodes/makefuncs.h" #include "nodes/nodeFuncs.h" @@ -24,6 +26,8 @@ #include "optimizer/planmain.h" #include "optimizer/planner.h" #include "optimizer/tlist.h" +#include "parser/parsetree.h" +#include "partitioning/partdesc.h" #include "tcop/utility.h" #include "utils/lsyscache.h" #include "utils/syscache.h" @@ -108,6 +112,8 @@ static void add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing); static void flatten_unplanned_rtes(PlannerGlobal *glob, RangeTblEntry *rte); static bool flatten_rtes_walker(Node *node, PlannerGlobal *glob); static void add_rte_to_flat_rtable(PlannerGlobal *glob, RangeTblEntry *rte); +static void add_target_partition_oids_recurse(Oid relid, int lockmode, + PlannerGlobal *glob); static Plan *set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset); static Plan *set_indexonlyscan_references(PlannerInfo *root, IndexOnlyScan *plan, @@ -261,6 +267,21 @@ set_plan_references(PlannerInfo *root, Plan *plan) */ add_rtes_to_flat_rtable(root, false); + /* + * If inserting into a partitioned table, add its partitions too to + * glob->relationOids to register them as plan dependencies. + */ + if (root->parse->commandType == CMD_INSERT && glob->parallelModeNeeded) + { + RangeTblEntry *result_rte = rt_fetch(root->parse->resultRelation, + root->parse->rtable); + + if (result_rte->relkind == RELKIND_PARTITIONED_TABLE) + add_target_partition_oids_recurse(result_rte->relid, + result_rte->rellockmode, + glob); + } + /* * Adjust RT indexes of PlanRowMarks and add to final rowmarks list */ @@ -495,6 +516,39 @@ add_rte_to_flat_rtable(PlannerGlobal *glob, RangeTblEntry *rte) glob->relationOids = lappend_oid(glob->relationOids, newrte->relid); } +/* + * Recursively adds a table's partitions' OIDs to glob->relationOids. + */ +static void +add_target_partition_oids_recurse(Oid relid, int lockmode, + PlannerGlobal *glob) +{ + Relation rel; + PartitionDesc pdesc; + int i; + + if (glob->partition_directory == NULL) + glob->partition_directory = + CreatePartitionDirectory(CurrentMemoryContext); + + rel = table_open(relid, lockmode); + pdesc = PartitionDirectoryLookup(glob->partition_directory, rel); + + for (i = 0; i < pdesc->nparts; i++) + { + glob->relationOids = lappend_oid(glob->relationOids, pdesc->oids[i]); + + /* + * If the partition may have its own partitions, recurse to add them + * too. + */ + if (!pdesc->is_leaf[i]) + add_target_partition_oids_recurse(pdesc->oids[i], lockmode, glob); + } + + table_close(rel, NoLock); +} + /* * set_plan_refs: recurse through the Plan nodes of a single subquery level */ diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index de16a28639..2d681a6940 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -1120,10 +1120,6 @@ target_rel_max_parallel_hazard_recurse(Relation rel, context); table_close(part_rel, AccessShareLock); - /* Register the partition as a plan dependency. */ - glob->relationOids = - lappend_oid(glob->relationOids, pdesc->oids[i]); - if (max_hazard_found) { return true;