Skip to content

Commit b8ca984

Browse files
committed
Revert lowering of lock level for ATTACH PARTITION
I lowered the lock level for partitions being scanned from AccessExclusive to ShareLock in the course of 72cf7f3, but that was bogus, as pointed out by Robert Haas. Revert that bit. Doing this is possible, but requires more work. Discussion: https://postgr.es/m/CA+TgmobV7Nfmqv+TZXcdSsb9Bjc-OL-Anv6BNmCbfJVZLYPE4Q@mail.gmail.com
1 parent 181ccbb commit b8ca984

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/backend/commands/tablecmds.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13997,10 +13997,9 @@ QueuePartitionConstraintValidation(List **wqueue, Relation scanrel,
1399713997
List *thisPartConstraint;
1399813998

1399913999
/*
14000-
* This is the minimum lock we need to prevent concurrent data
14001-
* additions.
14000+
* This is the minimum lock we need to prevent deadlocks.
1400214001
*/
14003-
part_rel = heap_open(partdesc->oids[i], ShareLock);
14002+
part_rel = heap_open(partdesc->oids[i], AccessExclusiveLock);
1400414003

1400514004
/*
1400614005
* Adjust the constraint for scanrel so that it matches this
@@ -14113,17 +14112,17 @@ ATExecAttachPartition(List **wqueue, Relation rel, PartitionCmd *cmd)
1411314112
*
1411414113
* We do that by checking if rel is a member of the list of attachrel's
1411514114
* partitions provided the latter is partitioned at all. We want to avoid
14116-
* having to construct this list again, so we request a lock on all
14117-
* partitions. We need ShareLock, preventing data changes, because we
14118-
* may decide to scan them if we find out that the table being attached (or
14119-
* its leaf partitions) may contain rows that violate the partition
14120-
* constraint. If the table has a constraint that would prevent such rows,
14121-
* which by definition is present in all the partitions, we need not scan
14122-
* the table, nor its partitions. But we cannot risk a deadlock by taking
14123-
* a weaker lock now and the stronger one only when needed.
14115+
* having to construct this list again, so we request the strongest lock
14116+
* on all partitions. We need the strongest lock, because we may decide
14117+
* to scan them if we find out that the table being attached (or its leaf
14118+
* partitions) may contain rows that violate the partition constraint. If
14119+
* the table has a constraint that would prevent such rows, which by
14120+
* definition is present in all the partitions, we need not scan the
14121+
* table, nor its partitions. But we cannot risk a deadlock by taking a
14122+
* weaker lock now and the stronger one only when needed.
1412414123
*/
1412514124
attachrel_children = find_all_inheritors(RelationGetRelid(attachrel),
14126-
ShareLock, NULL);
14125+
AccessExclusiveLock, NULL);
1412714126
if (list_member_oid(attachrel_children, RelationGetRelid(rel)))
1412814127
ereport(ERROR,
1412914128
(errcode(ERRCODE_DUPLICATE_TABLE),

0 commit comments

Comments
 (0)