From 3b28c098a89100e18c55832378d39e4ecd5a03ba Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Thu, 20 Dec 2018 12:59:21 -0500 Subject: [PATCH 3/3] Lower the lock level for ALTER TABLE .. ATTACH/DETACH PARTITION. --- src/backend/commands/tablecmds.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index ce0c7b3153..42862d9ef5 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -3627,7 +3627,29 @@ AlterTableGetLockLevel(List *cmds) case AT_AttachPartition: case AT_DetachPartition: - cmd_lockmode = AccessExclusiveLock; + /* + * We can attach or detach a partition with only + * ShareUpdateExclusiveLock on the partitioned table, but at + * least in the case of an ATTACH PARTITION operation, we need + * a stronger lock on the partition itself and on any default + * partition of the partitioned table. If we didn't do this, + * we could be in the middle of routing a tuple to a table and + * at the same time its partition constraint could be changing + * under us, which would possibly result in inserting a tuple + * that does not satisfy the partition constraint. Or, we + * could decide to prune the table from the query while the + * partition constraint is changing in such a way that the + * table should no longer be pruned. + * + * Note that attaching or detaching a partition becomes visible + * to other sessions as soon as the transaction which performed + * the operation commits. We can't use the query snapshot, + * which might be older, to determine which partitions are + * visible to a particular query, because the tables that were + * visible at that time might no longer exist, might no longer + * have a matching tuple descriptor, etc. + */ + cmd_lockmode = ShareUpdateExclusiveLock; break; default: /* oops */ -- 2.14.3 (Apple Git-98)