From 1e3e2996bb42cc6b3b7c49b2494b54251497535b Mon Sep 17 00:00:00 2001 From: Dilip Kumar Date: Fri, 24 Sep 2021 18:29:17 +0530 Subject: [PATCH v15 4/6] New interface to lock relation id Currently, we have LockRelationOid which provide a mechanism to lock the relation oid but we must be connected to the database from which this relation belong. As part of this patch we are providing a new interface which can lock the relation even if we are not connected to the containing database. --- src/backend/storage/lmgr/lmgr.c | 28 ++++++++++++++++++++++++++++ src/include/storage/lmgr.h | 1 + 2 files changed, 29 insertions(+) diff --git a/src/backend/storage/lmgr/lmgr.c b/src/backend/storage/lmgr/lmgr.c index 5ae52dd..1543da6 100644 --- a/src/backend/storage/lmgr/lmgr.c +++ b/src/backend/storage/lmgr/lmgr.c @@ -176,6 +176,34 @@ ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode) } /* + * LockRelationId + * + * Lock, given a LockRelId. Same as LockRelationOid but take LockRelId as an + * input. + */ +void +LockRelationId(LockRelId *relid, LOCKMODE lockmode) +{ + LOCKTAG tag; + LOCALLOCK *locallock; + LockAcquireResult res; + + SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId); + + res = LockAcquireExtended(&tag, lockmode, false, false, true, &locallock); + + /* + * Now that we have the lock, check for invalidation messages; see notes + * in LockRelationOid. + */ + if (res != LOCKACQUIRE_ALREADY_CLEAR) + { + AcceptInvalidationMessages(); + MarkLockClear(locallock); + } +} + +/* * UnlockRelationId * * Unlock, given a LockRelId. This is preferred over UnlockRelationOid diff --git a/src/include/storage/lmgr.h b/src/include/storage/lmgr.h index 49edbcc..be1d2c9 100644 --- a/src/include/storage/lmgr.h +++ b/src/include/storage/lmgr.h @@ -38,6 +38,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 void UnlockRelationId(LockRelId *relid, LOCKMODE lockmode); extern void UnlockRelationOid(Oid relid, LOCKMODE lockmode); -- 1.8.3.1