diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c index 65bbc18ecb..d666e860da 100644 --- a/src/backend/nodes/copyfuncs.c +++ b/src/backend/nodes/copyfuncs.c @@ -96,6 +96,7 @@ _copyPlannedStmt(const PlannedStmt *from) COPY_BITMAPSET_FIELD(rewindPlanIDs); COPY_NODE_FIELD(rowMarks); COPY_NODE_FIELD(relationOids); + COPY_NODE_FIELD(partitionOids); COPY_NODE_FIELD(invalItems); COPY_NODE_FIELD(paramExecTypes); COPY_NODE_FIELD(utilityStmt); diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index f5dcedf6e8..5c2cdc697b 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -314,6 +314,7 @@ _outPlannedStmt(StringInfo str, const PlannedStmt *node) WRITE_BITMAPSET_FIELD(rewindPlanIDs); WRITE_NODE_FIELD(rowMarks); WRITE_NODE_FIELD(relationOids); + WRITE_NODE_FIELD(partitionOids); WRITE_NODE_FIELD(invalItems); WRITE_NODE_FIELD(paramExecTypes); WRITE_NODE_FIELD(utilityStmt); @@ -2211,6 +2212,7 @@ _outPlannerGlobal(StringInfo str, const PlannerGlobal *node) WRITE_NODE_FIELD(resultRelations); WRITE_NODE_FIELD(appendRelations); WRITE_NODE_FIELD(relationOids); + WRITE_NODE_FIELD(partitionOids); WRITE_NODE_FIELD(invalItems); WRITE_NODE_FIELD(paramExecTypes); WRITE_UINT_FIELD(lastPHId); diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c index 4388aae71d..8962b37e2d 100644 --- a/src/backend/nodes/readfuncs.c +++ b/src/backend/nodes/readfuncs.c @@ -1590,6 +1590,7 @@ _readPlannedStmt(void) READ_BITMAPSET_FIELD(rewindPlanIDs); READ_NODE_FIELD(rowMarks); READ_NODE_FIELD(relationOids); + READ_NODE_FIELD(partitionOids); READ_NODE_FIELD(invalItems); READ_NODE_FIELD(paramExecTypes); READ_NODE_FIELD(utilityStmt); diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 18610c2345..85659acd75 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -527,6 +527,9 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions, result->rewindPlanIDs = glob->rewindPlanIDs; result->rowMarks = glob->finalrowmarks; result->relationOids = glob->relationOids; + /* For AcquireExecutorLocks(). */ + if (IsModifySupportedInParallelMode(parse->commandType)) + result->partitionOids = glob->partitionOids; result->invalItems = glob->invalItems; result->paramExecTypes = glob->paramExecTypes; /* utilityStmt should be null, but we might as well copy it */ diff --git a/src/backend/utils/cache/plancache.c b/src/backend/utils/cache/plancache.c index 1a0950489d..75d1088901 100644 --- a/src/backend/utils/cache/plancache.c +++ b/src/backend/utils/cache/plancache.c @@ -1735,6 +1735,23 @@ QueryListGetPrimaryStmt(List *stmts) return NULL; } +static void +AcquireExecutorLocksOnPartitions(List *partitionOids, int lockmode, + bool acquire) +{ + ListCell *lc; + + foreach(lc, partitionOids) + { + Oid partOid = lfirst_oid(lc); + + if (acquire) + LockRelationOid(partOid, lockmode); + else + UnlockRelationOid(partOid, lockmode); + } +} + /* * AcquireExecutorLocks: acquire locks needed for execution of a cached plan; * or release them if acquire is false. @@ -1748,6 +1765,8 @@ AcquireExecutorLocks(List *stmt_list, bool acquire) { PlannedStmt *plannedstmt = lfirst_node(PlannedStmt, lc1); ListCell *lc2; + Index rti, + resultRelation = 0; if (plannedstmt->commandType == CMD_UTILITY) { @@ -1765,6 +1784,9 @@ AcquireExecutorLocks(List *stmt_list, bool acquire) continue; } + rti = 1; + if (plannedstmt->resultRelations) + resultRelation = linitial_int(plannedstmt->resultRelations); foreach(lc2, plannedstmt->rtable) { RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc2); @@ -1782,6 +1804,15 @@ AcquireExecutorLocks(List *stmt_list, bool acquire) LockRelationOid(rte->relid, rte->rellockmode); else UnlockRelationOid(rte->relid, rte->rellockmode); + + /* Lock partitions ahead of modifying them in parallel mode. */ + if (rti == resultRelation && + plannedstmt->partitionOids != NIL && + plannedstmt->parallelModeNeeded) + AcquireExecutorLocksOnPartitions(plannedstmt->partitionOids, + rte->rellockmode, acquire); + + rti++; } } } diff --git a/src/include/nodes/plannodes.h b/src/include/nodes/plannodes.h index 43160439f0..8b3923c77f 100644 --- a/src/include/nodes/plannodes.h +++ b/src/include/nodes/plannodes.h @@ -79,6 +79,8 @@ typedef struct PlannedStmt List *relationOids; /* OIDs of relations the plan depends on */ + List *partitionOids; /* OIDs of partitions the plan depends on */ + List *invalItems; /* other dependencies, as PlanInvalItems */ List *paramExecTypes; /* type OIDs for PARAM_EXEC Params */