diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c index 1a73a63d61d..0a1019d6abf 100644 --- a/src/backend/commands/sequence.c +++ b/src/backend/commands/sequence.c @@ -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. */