From: Amit Kapila Date: Wed, 29 Jul 2026 04:16:53 +0000 (+0530) Subject: Avoid accumulating relation locks during sequence synchronization. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c12c101b0846b1e6488f2dc986a852fbc6bf2e3b;p=thirdparty%2Fpostgresql.git Avoid accumulating relation locks during sequence synchronization. While collecting the sequences to synchronize, the sequence sync worker opened each INIT sequence with RowExclusiveLock and held it until the transaction committed. With many such sequences, this could exhaust the shared lock table and fail with "out of shared memory". The worker only reads each sequence's identity (namespace and name) here and needs it to stay stable while read, for which AccessShareLock is enough, as it conflicts with the AccessExclusiveLock taken by DROP, RENAME, and SET SCHEMA. Take that lock instead and release it as soon as the identity is read. The later synchronization re-opens each sequence, so it does not rely on the lock being retained. Reported-by: Noah Misch Author: vignesh C Reviewed-by: Hayato Kuroda Reviewed-by: Amit Kapila Backpatch-through: 19, where it was introduced Discussion: https://postgr.es/m/20260710045217.f0.noahmisch@microsoft.com --- diff --git a/src/backend/replication/logical/sequencesync.c b/src/backend/replication/logical/sequencesync.c index fe506a98c20..0423745a428 100644 --- a/src/backend/replication/logical/sequencesync.c +++ b/src/backend/replication/logical/sequencesync.c @@ -752,7 +752,16 @@ LogicalRepSyncSequences(void) subrel = (Form_pg_subscription_rel) GETSTRUCT(tup); - sequence_rel = try_table_open(subrel->srrelid, RowExclusiveLock); + /* + * Lock the sequence so its identity (namespace and name) cannot + * change under us via a concurrent DROP, RENAME or SET SCHEMA. The + * lock is released immediately rathen than at the transaction end. + * The later synchronization does not depend on this captured identity + * remaining valid, as it re-opens the sequence and tolerates + * concurrent changes. Releasing early also avoids holding one lock + * per sequence, which could exhaust the lock table. + */ + sequence_rel = try_table_open(subrel->srrelid, AccessShareLock); /* Skip if sequence was dropped concurrently */ if (!sequence_rel) @@ -761,7 +770,7 @@ LogicalRepSyncSequences(void) /* Skip if the relation is not a sequence */ if (sequence_rel->rd_rel->relkind != RELKIND_SEQUENCE) { - table_close(sequence_rel, NoLock); + table_close(sequence_rel, AccessShareLock); continue; } @@ -779,7 +788,7 @@ LogicalRepSyncSequences(void) MemoryContextSwitchTo(oldctx); - table_close(sequence_rel, NoLock); + table_close(sequence_rel, AccessShareLock); } /* Cleanup */