aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/sequence.c
diff options
context:
space:
mode:
authorNoah Misch <noah@leadboat.com>2024-06-27 19:21:05 -0700
committerNoah Misch <noah@leadboat.com>2024-06-27 19:21:05 -0700
commitf88cdb36c457f673a1966a22883ce47e565a37db (patch)
tree0680dfdf37c9b8193acdf91b7004496f10db4efb /src/backend/commands/sequence.c
parentd5f788b41dc2cbdde6e7694c70dda54d829a5ed5 (diff)
downloadpostgresql-f88cdb36c457f673a1966a22883ce47e565a37db.tar.gz
postgresql-f88cdb36c457f673a1966a22883ce47e565a37db.zip
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 (in an earlier version) by Robert Haas. Discussion: https://postgr.es/m/20240611024525.9f.nmisch@google.com
Diffstat (limited to 'src/backend/commands/sequence.c')
-rw-r--r--src/backend/commands/sequence.c7
1 files changed, 7 insertions, 0 deletions
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index 28f85222647..b4ad19c0539 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. */