pgsql: Improve GrantLockLocal() handling on out-of-memory errors

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Improve GrantLockLocal() handling on out-of-memory errors
Date: 2026-08-12 00:13:46
Message-ID: E1wtwbI-00000000BDx-42Zw@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Improve GrantLockLocal() handling on out-of-memory errors

On the first LockAcquire for a lock tag, LockAcquireExtended() creates
a LOCALLOCK entry in the hash table LockMethodLocalHash, with lockOwners
set to NULL. lockOwners is then allocated in TopMemoryContext.

If the allocation fails, the hash entry would persist with lockOwners
still NULL and maxLockOwners already set to 8. A follow-up
LockAcquire() on the same entry would crash on a NULL pointer
dereference.

This commit adds an extra layer of safety for such a case, by
re-attempting MemoryContextAlloc() if a LOCALLOCK entry exists but its
lockOwners is still NULL due to a previous in-flight allocation failure.

This problem is unlikely going to show up in practice, so no backpatch
is done.

Author: Bryan Green <dbryan(dot)green(at)gmail(dot)com>
Author: Mark Dilger <mark(dot)dilger(at)enterprisedb(dot)com>
Discussion: https://postgr.es/m/c9a5eeb1-4d31-46e1-bb9e-33448a23a5d3@gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/afd63e0ad23ed826aa953977a5306a3703503afa

Modified Files
--------------
src/backend/storage/lmgr/lock.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2026-08-12 00:56:44 pgsql: Fix unlink() error report in KeepFileRestoredFromArchive()
Previous Message Tom Lane 2026-08-11 23:52:51 Re: pgsql: Change wal_compression=on to the first of zstd, lz4, pglz