PREPARE TRANSACTION that fails with "out of shared memory" while moving fast-path locks permanently leaks the fast-path strong-lock counts of its strong relation locks, so weak locks on relations hashing to those partitions can never use the fast path again until restart =============================================================================================================================================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a234a5-730e-7ab4-9440-707d5a76b45d Filed: 2026-12-02 02:46:27 UTC Severity: 25/100 Blamed commit: 3cba8999b343648c4c528432ab3d51400194e93b ## Summary AtPrepare_Locks() walks the local lock table and, for every lock, clears `locallock->holdsStrongLockCount` so that the strong-lock count in `FastPathStrongRelationLocks[]` survives into the prepared transaction. In the same loop it moves fast-path locks into the main lock table with FastPathGetRelationLockEntry(), which raises "out of shared memory" when the LOCK/PROCLOCK tables are full. If that error is raised after one or more strong locks (SHARE and stronger on a relation) have already been visited, PREPARE fails and the transaction aborts, but those locallocks no longer claim their strong-lock count. LockReleaseAll() releases the locks and RemoveLocalLock() skips the decrement, so the per-partition counters stay above zero for the life of the postmaster. From then on every backend that takes a weak relation lock (e.g. AccessShareLock for a plain SELECT) on any relation whose lock tag hashes to an affected partition (1/1024 of all relations per leaked count) is forced into the main lock table: more partition LWLock traffic and more main lock table entries consumed, which in turn makes further "out of shared memory" failures more likely. Each failed PREPARE can leak more counts. Only a restart clears them. ## Reproduction Settings (a fresh cluster): max_connections = 20 max_prepared_transactions = 5 max_locks_per_transaction = 64 Setup: SELECT format('CREATE TABLE b_s%s (a int)', i) FROM generate_series(1,20) i \gexec SELECT format('CREATE TABLE b_f%s (a int)', i) FROM generate_series(1,40) i \gexec SELECT format('CREATE TABLE a_%s (a int)', i) FROM generate_series(1,6000) i \gexec Session 1: 20 strong locks plus 40 fast-path weak locks. BEGIN; SELECT format('LOCK TABLE b_s%s IN SHARE MODE', i) FROM generate_series(1,20) i \gexec SELECT format('LOCK TABLE b_f%s IN ACCESS SHARE MODE', i) FROM generate_series(1,40) i \gexec SELECT count(*) FILTER (WHERE fastpath), count(*) FILTER (WHERE NOT fastpath) FROM pg_locks WHERE pid = pg_backend_pid() AND locktype = 'relation'; -- 40 | 21 Session 2: fill the shared lock table, leaving room for about 30 locks (fewer than the 40 that session 1 must move): BEGIN; DO $$ DECLARE c int := 0; BEGIN BEGIN LOOP c := c + 1; BEGIN EXECUTE format('LOCK TABLE a_%s IN ACCESS SHARE MODE', c); EXCEPTION WHEN out_of_memory THEN EXIT; END; END LOOP; RAISE EXCEPTION 'measured'; EXCEPTION WHEN raise_exception THEN NULL; END; FOR i IN 1 .. c - 30 LOOP EXECUTE format('LOCK TABLE a_%s IN ACCESS SHARE MODE', i); END LOOP; RAISE NOTICE 'capacity % locks; holding %', c - 1, c - 30; END $$; -- NOTICE: capacity 3946 locks; holding 3917 Session 1: PREPARE TRANSACTION 'p1'; -- ERROR: out of shared memory -- HINT: You might need to increase "max_locks_per_transaction". Session 2: COMMIT; Now nothing holds any lock (pg_locks is empty apart from the querying session, pg_prepared_xacts is empty). In a new session, check whether a weak lock on each b_s table uses the fast path: SELECT format($q$BEGIN; LOCK TABLE b_s%1$s IN ACCESS SHARE MODE; SELECT 'b_s%1$s', fastpath FROM pg_locks WHERE pid = pg_backend_pid() AND relation = 'b_s%1$s'::regclass; COMMIT;$q$, i) FROM generate_series(1,20) i \gexec Expected: fastpath = t for all 20 tables (no strong lock is held by anyone). Actual: fastpath = f for 17 of the 20 tables (b_s1..b_s5, b_s7..b_s9, b_s11..b_s13, b_s15..b_s20 in this run). The result is the same minutes later and in new sessions; it is cleared only by a restart. The exact number depends on the local lock hash order: every strong lock visited before the first failing fast-path transfer leaks its count. Control: after a restart, the same steps with session 1 ending in an ordinary error and ROLLBACK instead of PREPARE TRANSACTION leave all 20 tables at fastpath = t. ## Analysis In src/backend/storage/lmgr/lock.c, AtPrepare_Locks(): if (locallock->proclock == NULL) { locallock->proclock = FastPathGetRelationLockEntry(locallock); ... } locallock->holdsStrongLockCount = false; `holdsStrongLockCount = false` is applied to each entry as the loop goes, before it is known that the loop (and the rest of PrepareTransaction() up to the EndPrepare() critical section) will succeed. FastPathGetRelationLockEntry() can ereport(ERROR, "out of shared memory") for a later entry. On the resulting abort, LockReleaseAll() -> RemoveLocalLock() only decrements `FastPathStrongRelationLocks[fasthashcode]` when `holdsStrongLockCount` is still true, so the counts of the already-visited strong locks are never decremented. LockAcquire's fast path refuses any partition whose count is non-zero, so weak locks on all relations hashing there fall back to the main lock table permanently. The same window covers any other ERROR raised between AtPrepare_Locks() and PostPrepare_Locks() (the later AtPrepare_* callbacks and the pre-critical-section checks in EndPrepare()), but the lock-table-full case above is the practical trigger. The ordering was introduced with the fast-path lock mechanism in 3cba8999b34 ("Create a 'fast path' for acquiring weak relation locks."), which added both the in-loop transfer and the in-loop clearing of the strong-lock flag in AtPrepare_Locks(). Since e1ad034809a the lock hash tables can no longer grow into spare shared memory, which makes the "out of shared memory" condition easier to reach, but that commit is not the cause. ## Notes - Tested on master dc168a83874fbf5cee4afb76a623e1349a539724 with assertions enabled; no assertion fires because the counter only stays too high (it never underflows). - The effect is a lost optimization plus extra shared lock table consumption, not wrong locking: a non-zero count only makes weak lockers take the safe slow path. - Under a randomized stress mix (PREPARE TRANSACTION of transactions holding a SHARE lock plus many partition locks, concurrent with sessions filling the lock table) the same leak showed up as up to ~130 relations that permanently lost fast-path eligibility. ## Branches Blame 3cba8999b34 is in every branch, and the same in-loop clearing in AtPrepare_Locks() is present on all supported branches (REL_14_STABLE through master). Reproduced on master only (dc168a83874fbf5cee4afb76a623e1349a539724); back-branch reproductions welcome. ## Related None found. Nearest unrelated lock/2PC reports: 01a1a853-f84b, 01a1e72f-c5b1.