inplace065-lock-SequenceChangePersistence-v2.patch
text/plain
Filename: inplace065-lock-SequenceChangePersistence-v2.patch
Type: text/plain
Part: 5
Message:
Re: race condition in pg_class
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: unified
Series: patch v2
| File | + | − |
|---|---|---|
| src/backend/commands/sequence.c | 7 | 0 |
Author: Noah Misch <noah@leadboat.com>
Commit: Noah Misch <noah@leadboat.com>
Lock owned sequences during ALTER TABLE SET { LOGGED | UNLOGGED }.
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 by FIXME.
Discussion: https://postgr.es/m/20240512232923.aa.nmisch@google.com
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index 4610356..d020867 100644
--- a/src/backend/commands/sequence.c
+++ b/src/backend/commands/sequence.c
@@ -545,6 +545,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. */