1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-02 04:21:28 +03:00

- Modifies LOCKTAG to include a 'classId'. Relation receive a classId of

RelOid_pg_class, and transaction locks XactLockTableId. RelId is renamed
to objId.

- LockObject() and UnlockObject() functions created, and their use
sprinkled throughout the code to do descent locking for domains and
types. They accept lock modes AccessShare and AccessExclusive, as we
only really need a 'read' and 'write' lock at the moment.  Most locking
cases are held until the end of the transaction.

This fixes the cases Tom mentioned earlier in regards to locking with
Domains.  If the patch is good, I'll work on cleaning up issues with
other database objects that have this problem (most of them).

Rod Taylor
This commit is contained in:
Bruce Momjian
2003-02-19 04:02:54 +00:00
parent 81f6db4803
commit d0f3a7e9c4
10 changed files with 219 additions and 73 deletions

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lmgr.c,v 1.54 2002/08/01 05:18:33 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lmgr.c,v 1.55 2003/02/19 04:02:53 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -126,9 +126,10 @@ LockRelation(Relation relation, LOCKMODE lockmode)
LOCKTAG tag;
MemSet(&tag, 0, sizeof(tag));
tag.relId = relation->rd_lockInfo.lockRelId.relId;
tag.objId = relation->rd_lockInfo.lockRelId.relId;
tag.classId = RelOid_pg_class;
tag.dbId = relation->rd_lockInfo.lockRelId.dbId;
tag.objId.blkno = InvalidBlockNumber;
tag.objsubId.blkno = InvalidBlockNumber;
if (!LockAcquire(LockTableId, &tag, GetCurrentTransactionId(),
lockmode, false))
@@ -160,9 +161,10 @@ ConditionalLockRelation(Relation relation, LOCKMODE lockmode)
LOCKTAG tag;
MemSet(&tag, 0, sizeof(tag));
tag.relId = relation->rd_lockInfo.lockRelId.relId;
tag.objId = relation->rd_lockInfo.lockRelId.relId;
tag.classId = RelOid_pg_class;
tag.dbId = relation->rd_lockInfo.lockRelId.dbId;
tag.objId.blkno = InvalidBlockNumber;
tag.objsubId.blkno = InvalidBlockNumber;
if (!LockAcquire(LockTableId, &tag, GetCurrentTransactionId(),
lockmode, true))
@@ -190,9 +192,10 @@ UnlockRelation(Relation relation, LOCKMODE lockmode)
LOCKTAG tag;
MemSet(&tag, 0, sizeof(tag));
tag.relId = relation->rd_lockInfo.lockRelId.relId;
tag.objId = relation->rd_lockInfo.lockRelId.relId;
tag.classId = RelOid_pg_class;
tag.dbId = relation->rd_lockInfo.lockRelId.dbId;
tag.objId.blkno = InvalidBlockNumber;
tag.objsubId.blkno = InvalidBlockNumber;
LockRelease(LockTableId, &tag, GetCurrentTransactionId(), lockmode);
}
@@ -215,9 +218,10 @@ LockRelationForSession(LockRelId *relid, LOCKMODE lockmode)
LOCKTAG tag;
MemSet(&tag, 0, sizeof(tag));
tag.relId = relid->relId;
tag.objId = relid->relId;
tag.classId = RelOid_pg_class;
tag.dbId = relid->dbId;
tag.objId.blkno = InvalidBlockNumber;
tag.objsubId.blkno = InvalidBlockNumber;
if (!LockAcquire(LockTableId, &tag, InvalidTransactionId,
lockmode, false))
@@ -233,9 +237,10 @@ UnlockRelationForSession(LockRelId *relid, LOCKMODE lockmode)
LOCKTAG tag;
MemSet(&tag, 0, sizeof(tag));
tag.relId = relid->relId;
tag.objId = relid->relId;
tag.classId = RelOid_pg_class;
tag.dbId = relid->dbId;
tag.objId.blkno = InvalidBlockNumber;
tag.objsubId.blkno = InvalidBlockNumber;
LockRelease(LockTableId, &tag, InvalidTransactionId, lockmode);
}
@@ -253,9 +258,10 @@ LockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
LOCKTAG tag;
MemSet(&tag, 0, sizeof(tag));
tag.relId = relation->rd_lockInfo.lockRelId.relId;
tag.objId = relation->rd_lockInfo.lockRelId.relId;
tag.classId = RelOid_pg_class;
tag.dbId = relation->rd_lockInfo.lockRelId.dbId;
tag.objId.blkno = blkno;
tag.objsubId.blkno = blkno;
if (!LockAcquire(LockTableId, &tag, GetCurrentTransactionId(),
lockmode, false))
@@ -271,9 +277,10 @@ UnlockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
LOCKTAG tag;
MemSet(&tag, 0, sizeof(tag));
tag.relId = relation->rd_lockInfo.lockRelId.relId;
tag.objId = relation->rd_lockInfo.lockRelId.relId;
tag.classId = RelOid_pg_class;
tag.dbId = relation->rd_lockInfo.lockRelId.dbId;
tag.objId.blkno = blkno;
tag.objsubId.blkno = blkno;
LockRelease(LockTableId, &tag, GetCurrentTransactionId(), lockmode);
}
@@ -294,9 +301,10 @@ XactLockTableInsert(TransactionId xid)
LOCKTAG tag;
MemSet(&tag, 0, sizeof(tag));
tag.relId = XactLockTableId;
tag.objId = InvalidOid;
tag.classId = XactLockTableId;
tag.dbId = InvalidOid; /* xids are globally unique */
tag.objId.xid = xid;
tag.objsubId.xid = xid;
if (!LockAcquire(LockTableId, &tag, xid,
ExclusiveLock, false))
@@ -317,9 +325,10 @@ XactLockTableWait(TransactionId xid)
Assert(!TransactionIdEquals(xid, myxid));
MemSet(&tag, 0, sizeof(tag));
tag.relId = XactLockTableId;
tag.objId = InvalidOid;
tag.classId = XactLockTableId;
tag.dbId = InvalidOid;
tag.objId.xid = xid;
tag.objsubId.xid = xid;
if (!LockAcquire(LockTableId, &tag, myxid,
ShareLock, false))
@@ -334,3 +343,59 @@ XactLockTableWait(TransactionId xid)
if (!TransactionIdDidCommit(xid) && !TransactionIdDidAbort(xid))
TransactionIdAbort(xid);
}
/*
* LockObject
*
* Lock an arbitrary database object. A standard relation lock would lock the
* classId of RelOid_pg_class and objId of the relations OID within the pg_class
* table. LockObject allows classId to be specified by the caller, thus allowing
* locks on any row in any system table.
*
* If classId is NOT a system table (protected from removal), an additional lock
* should be held on the relation to prevent it from being dropped.
*/
void
LockObject(Oid objId, Oid classId, LOCKMODE lockmode)
{
LOCKTAG tag;
MemSet(&tag, 0, sizeof(tag));
tag.objId = objId;
tag.classId = classId;
tag.dbId = MyDatabaseId;
tag.objsubId.blkno = InvalidBlockNumber;
/* Only two reasonable lock types */
Assert(lockmode == AccessShareLock || lockmode == AccessExclusiveLock);
if (!LockAcquire(LockTableId, &tag, GetCurrentTransactionId(),
lockmode, false))
elog(ERROR, "LockObject: LockAcquire failed");
}
/*
* UnlockObject
*/
void
UnlockObject(Oid objId, Oid classId, LOCKMODE lockmode)
{
LOCKTAG tag;
/* NoLock is a no-op */
if (lockmode == NoLock)
return;
MemSet(&tag, 0, sizeof(tag));
tag.objId = objId;
tag.classId = classId;
tag.dbId = MyDatabaseId;
tag.objsubId.blkno = InvalidBlockNumber;
/* Only two reasonable lock types */
Assert(lockmode == AccessShareLock
|| lockmode == AccessExclusiveLock);
LockRelease(LockTableId, &tag, GetCurrentTransactionId(), lockmode);
}