[go: up one dir, main page]

Lock owned sequences during ALTER TABLE SET { LOGGED | UNLOGGED }.
authorNoah Misch <noah@leadboat.com>
Fri, 28 Jun 2024 02:21:05 +0000 (19:21 -0700)
committerNoah Misch <noah@leadboat.com>
Fri, 28 Jun 2024 02:21:10 +0000 (19:21 -0700)
These commands already make the persistence of owned sequences follow
owned table persistence changes.  They didn't lock those sequences.
They lost the effect of nextval() calls that other sessions make after
the ALTER TABLE command, before the ALTER TABLE transaction commits.
Fix by acquiring the same lock that ALTER SEQUENCE SET { LOGGED |
UNLOGGED } acquires.  This might cause more deadlocks.  Back-patch to
v15, where commit 344d62fb9a978a72cf8347f0369b9ee643fd0b31 introduced
unlogged sequences.

Reviewed (in an earlier version) by Robert Haas.

Discussion: https://postgr.es/m/20240611024525.9f.nmisch@google.com

src/backend/commands/sequence.c

index 1a73a63d61d4245080d1b187049edb42cb25fcef..0a1019d6abff3a9600e6c80ac1ae7cdb0bb60033 100644 (file)
@@ -558,6 +558,13 @@ SequenceChangePersistence(Oid relid, char newrelpersistence)
    Buffer      buf;
    HeapTupleData seqdatatuple;
 
+   /*
+    * ALTER SEQUENCE acquires this lock earlier.  If we're processing an
+    * owned sequence for ALTER TABLE, lock now.  Without the lock, we'd
+    * discard increments from nextval() calls (in other sessions) between
+    * this function's buffer unlock and this transaction's commit.
+    */
+   LockRelationOid(relid, AccessExclusiveLock);
    init_sequence(relid, &elm, &seqrel);
 
    /* check the comment above nextval_internal()'s equivalent call. */