From 64da545b26993706b8e04bab3ffeae658935900e Mon Sep 17 00:00:00 2001 From: ChangAo Chen Date: Sat, 13 Jun 2026 17:07:39 +0800 Subject: [PATCH v2] Make log_lock_failures support LOCK NOWAIT --- doc/src/sgml/config.sgml | 9 +++++---- src/backend/catalog/namespace.c | 3 ++- src/backend/commands/lockcmds.c | 7 +++++-- src/backend/commands/repack.c | 4 ++-- src/backend/commands/tablecmds.c | 2 +- src/backend/commands/vacuum.c | 2 +- src/backend/postmaster/autovacuum.c | 2 +- src/backend/storage/lmgr/lmgr.c | 4 ++-- src/include/storage/lmgr.h | 2 +- 9 files changed, 20 insertions(+), 15 deletions(-) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index fa566c9e553..5396363129e 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -8373,10 +8373,11 @@ log_line_prefix = '%m [%p] %q%u@%d/%a ' Controls whether a detailed log message is produced when a lock acquisition fails. This is useful for analyzing the causes of lock failures. Currently, only lock failures - due to SELECT NOWAIT is supported. - The default is off. Only superusers and - users with the appropriate SET privilege - can change this setting. + due to SELECT NOWAIT and + LOCK NOWAIT is supported. The default is + off. Only superusers and users with the + appropriate SET privilege can change this + setting. diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c index 56b87d878e8..7d5af5dcaea 100644 --- a/src/backend/catalog/namespace.c +++ b/src/backend/catalog/namespace.c @@ -591,7 +591,8 @@ RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, AcceptInvalidationMessages(); else if (!(flags & (RVR_NOWAIT | RVR_SKIP_LOCKED))) LockRelationOid(relId, lockmode); - else if (!ConditionalLockRelationOid(relId, lockmode)) + else if (!ConditionalLockRelationOid(relId, lockmode, + (flags & RVR_NOWAIT) && log_lock_failures)) { int elevel = (flags & RVR_SKIP_LOCKED) ? DEBUG1 : ERROR; diff --git a/src/backend/commands/lockcmds.c b/src/backend/commands/lockcmds.c index f66b8f17b9b..2c705d7535b 100644 --- a/src/backend/commands/lockcmds.c +++ b/src/backend/commands/lockcmds.c @@ -23,6 +23,7 @@ #include "nodes/nodeFuncs.h" #include "rewrite/rewriteHandler.h" #include "storage/lmgr.h" +#include "storage/lock.h" #include "utils/acl.h" #include "utils/lsyscache.h" #include "utils/syscache.h" @@ -131,7 +132,8 @@ LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait) if (!nowait) LockRelationOid(childreloid, lockmode); - else if (!ConditionalLockRelationOid(childreloid, lockmode)) + else if (!ConditionalLockRelationOid(childreloid, lockmode, + log_lock_failures)) { /* try to throw error by name; relation could be deleted... */ char *relname = get_rel_name(childreloid); @@ -217,7 +219,8 @@ LockViewRecurse_walker(Node *node, LockViewRecurse_context *context) /* We have enough rights to lock the relation; do so. */ if (!context->nowait) LockRelationOid(relid, context->lockmode); - else if (!ConditionalLockRelationOid(relid, context->lockmode)) + else if (!ConditionalLockRelationOid(relid, context->lockmode, + log_lock_failures)) ereport(ERROR, (errcode(ERRCODE_LOCK_NOT_AVAILABLE), errmsg("could not obtain lock on relation \"%s\"", diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c index ec100e3eef5..923c768b0c7 100644 --- a/src/backend/commands/repack.c +++ b/src/backend/commands/repack.c @@ -2165,7 +2165,7 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt) * disregard it. Be sure to release this if we ultimately decide * not to process the table! */ - if (!ConditionalLockRelationOid(index->indrelid, AccessShareLock)) + if (!ConditionalLockRelationOid(index->indrelid, AccessShareLock, false)) continue; /* Verify that the table still exists; skip if not */ @@ -2224,7 +2224,7 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt) * disregard the table. Be sure to release this if we ultimately * decide not to process the table! */ - if (!ConditionalLockRelationOid(class->oid, AccessShareLock)) + if (!ConditionalLockRelationOid(class->oid, AccessShareLock, false)) continue; /* Verify that the table still exists */ diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 38f9ffcd04f..a42a860885b 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -17357,7 +17357,7 @@ AlterTableMoveAll(AlterTableMoveAllStmt *stmt) NameStr(relForm->relname)); if (stmt->nowait && - !ConditionalLockRelationOid(relOid, AccessExclusiveLock)) + !ConditionalLockRelationOid(relOid, AccessExclusiveLock, false)) ereport(ERROR, (errcode(ERRCODE_OBJECT_IN_USE), errmsg("aborting because lock on relation \"%s.%s\" is not available", diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index a4abb29cf64..7bd13421093 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -789,7 +789,7 @@ vacuum_open_relation(Oid relid, RangeVar *relation, uint32 options, */ if (!(options & VACOPT_SKIP_LOCKED)) rel = try_relation_open(relid, lmode); - else if (ConditionalLockRelationOid(relid, lmode)) + else if (ConditionalLockRelationOid(relid, lmode, false)) rel = try_relation_open(relid, NoLock); else { diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c index a5a8db2ff88..550c38d3ed7 100644 --- a/src/backend/postmaster/autovacuum.c +++ b/src/backend/postmaster/autovacuum.c @@ -2220,7 +2220,7 @@ do_autovacuum(void) * somebody else is using (or dropping) the table, so it's not our * concern anymore. Having the lock prevents race conditions below. */ - if (!ConditionalLockRelationOid(relid, AccessExclusiveLock)) + if (!ConditionalLockRelationOid(relid, AccessExclusiveLock, false)) continue; /* diff --git a/src/backend/storage/lmgr/lmgr.c b/src/backend/storage/lmgr/lmgr.c index 2ccf7237fee..29871105416 100644 --- a/src/backend/storage/lmgr/lmgr.c +++ b/src/backend/storage/lmgr/lmgr.c @@ -148,7 +148,7 @@ LockRelationOid(Oid relid, LOCKMODE lockmode) * LockXXX routines in this file, but they could easily be added if needed. */ bool -ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode) +ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode, bool logLockFailure) { LOCKTAG tag; LOCALLOCK *locallock; @@ -157,7 +157,7 @@ ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode) SetLocktagRelationOid(&tag, relid); res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock, - false); + logLockFailure); if (res == LOCKACQUIRE_NOT_AVAIL) return false; diff --git a/src/include/storage/lmgr.h b/src/include/storage/lmgr.h index 2a985ce5e15..7a31a90e8c9 100644 --- a/src/include/storage/lmgr.h +++ b/src/include/storage/lmgr.h @@ -39,7 +39,7 @@ extern void RelationInitLockInfo(Relation relation); /* Lock a relation */ extern void LockRelationOid(Oid relid, LOCKMODE lockmode); extern void LockRelationId(LockRelId *relid, LOCKMODE lockmode); -extern bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode); +extern bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode, bool logLockFailure); extern void UnlockRelationId(LockRelId *relid, LOCKMODE lockmode); extern void UnlockRelationOid(Oid relid, LOCKMODE lockmode); -- 2.54.0