mirror of
https://github.com/postgres/postgres.git
synced 2025-11-12 05:01:15 +03:00
Mega-commit to make heap_open/heap_openr/heap_close take an
additional argument specifying the kind of lock to acquire/release (or 'NoLock' to do no lock processing). Ensure that all relations are locked with some appropriate lock level before being examined --- this ensures that relevant shared-inval messages have been processed and should prevent problems caused by concurrent VACUUM. Fix several bugs having to do with mismatched increment/decrement of relation ref count and mismatched heap_open/close (which amounts to the same thing). A bogus ref count on a relation doesn't matter much *unless* a SI Inval message happens to arrive at the wrong time, which is probably why we got away with this sloppiness for so long. Repair missing grab of AccessExclusiveLock in DROP TABLE, ALTER/RENAME TABLE, etc, as noted by Hiroshi. Recommend 'make clean all' after pulling this update; I modified the Relation struct layout slightly. Will post further discussion to pghackers list shortly.
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v 1.61 1999/07/17 20:17:40 momjian Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v 1.62 1999/09/18 19:07:26 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -109,7 +109,7 @@ RelationGetBufferWithBuffer(Relation relation,
|
||||
{
|
||||
if (!BufferIsLocal(buffer))
|
||||
{
|
||||
LockRelId *lrelId = &(((LockInfo) (relation->lockInfo))->lockRelId);
|
||||
LockRelId *lrelId = & relation->rd_lockInfo.lockRelId;
|
||||
|
||||
bufHdr = &BufferDescriptors[buffer - 1];
|
||||
SpinAcquire(BufMgrLock);
|
||||
@@ -813,6 +813,7 @@ FlushBuffer(Buffer buffer, bool release)
|
||||
status = smgrflush(DEFAULT_SMGR, bufrel, bufHdr->tag.blockNum,
|
||||
(char *) MAKE_PTR(bufHdr->data));
|
||||
|
||||
/* drop relcache refcount incremented by RelationIdCacheGetRelation */
|
||||
RelationDecrementReferenceCount(bufrel);
|
||||
|
||||
if (status == SM_FAIL)
|
||||
@@ -993,6 +994,7 @@ BufferSync()
|
||||
elog(ERROR, "BufferSync: write error %u for %s",
|
||||
bufHdr->tag.blockNum, bufHdr->sb_relname);
|
||||
}
|
||||
/* drop refcount from RelationIdCacheGetRelation */
|
||||
if (reln != (Relation) NULL)
|
||||
RelationDecrementReferenceCount(reln);
|
||||
continue;
|
||||
@@ -1047,6 +1049,7 @@ BufferSync()
|
||||
*/
|
||||
if (!(bufHdr->flags & BM_JUST_DIRTIED))
|
||||
bufHdr->flags &= ~BM_DIRTY;
|
||||
/* drop refcount from RelationIdCacheGetRelation */
|
||||
if (reln != (Relation) NULL)
|
||||
RelationDecrementReferenceCount(reln);
|
||||
}
|
||||
@@ -1282,14 +1285,16 @@ BufferGetRelation(Buffer buffer)
|
||||
/* XXX should be a critical section */
|
||||
relid = BufferDescriptors[buffer - 1].tag.relId.relId;
|
||||
relation = RelationIdGetRelation(relid);
|
||||
Assert(relation);
|
||||
|
||||
/* drop relcache refcount incremented by RelationIdGetRelation */
|
||||
RelationDecrementReferenceCount(relation);
|
||||
|
||||
if (RelationHasReferenceCountZero(relation))
|
||||
{
|
||||
|
||||
/*
|
||||
* elog(NOTICE, "BufferGetRelation: 0->1");
|
||||
* XXX why??
|
||||
*/
|
||||
|
||||
RelationIncrementReferenceCount(relation);
|
||||
@@ -1342,7 +1347,6 @@ BufferReplace(BufferDesc *bufHdr, bool bufferLockHeld)
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
/* blind write always flushes */
|
||||
status = smgrblindwrt(DEFAULT_SMGR, bufHdr->sb_dbname,
|
||||
bufHdr->sb_relname, bufdb, bufrel,
|
||||
@@ -1350,6 +1354,7 @@ BufferReplace(BufferDesc *bufHdr, bool bufferLockHeld)
|
||||
(char *) MAKE_PTR(bufHdr->data));
|
||||
}
|
||||
|
||||
/* drop relcache refcount incremented by RelationIdCacheGetRelation */
|
||||
if (reln != (Relation) NULL)
|
||||
RelationDecrementReferenceCount(reln);
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/localbuf.c,v 1.26 1999/07/17 20:17:41 momjian Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/localbuf.c,v 1.27 1999/09/18 19:07:26 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -109,6 +109,8 @@ LocalBufferAlloc(Relation reln, BlockNumber blockNum, bool *foundPtr)
|
||||
smgrwrite(DEFAULT_SMGR, bufrel, bufHdr->tag.blockNum,
|
||||
(char *) MAKE_PTR(bufHdr->data));
|
||||
LocalBufferFlushCount++;
|
||||
|
||||
/* drop relcache refcount incremented by RelationIdCacheGetRelation */
|
||||
RelationDecrementReferenceCount(bufrel);
|
||||
}
|
||||
|
||||
@@ -187,6 +189,8 @@ FlushLocalBuffer(Buffer buffer, bool release)
|
||||
smgrflush(DEFAULT_SMGR, bufrel, bufHdr->tag.blockNum,
|
||||
(char *) MAKE_PTR(bufHdr->data));
|
||||
LocalBufferFlushCount++;
|
||||
|
||||
/* drop relcache refcount incremented by RelationIdCacheGetRelation */
|
||||
RelationDecrementReferenceCount(bufrel);
|
||||
|
||||
Assert(LocalRefCount[bufid] > 0);
|
||||
@@ -260,6 +264,8 @@ LocalBufferSync(void)
|
||||
smgrwrite(DEFAULT_SMGR, bufrel, buf->tag.blockNum,
|
||||
(char *) MAKE_PTR(buf->data));
|
||||
LocalBufferFlushCount++;
|
||||
|
||||
/* drop relcache refcount from RelationIdCacheGetRelation */
|
||||
RelationDecrementReferenceCount(bufrel);
|
||||
|
||||
buf->tag.relId.relId = InvalidOid;
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/large_object/inv_api.c,v 1.58 1999/07/19 07:07:23 momjian Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/large_object/inv_api.c,v 1.59 1999/09/18 19:07:32 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -139,13 +139,16 @@ inv_create(int flags)
|
||||
|
||||
/* make the relation visible in this transaction */
|
||||
CommandCounterIncrement();
|
||||
r = heap_openr(objname);
|
||||
|
||||
if (!RelationIsValid(r))
|
||||
{
|
||||
elog(ERROR, "cannot create large object on %s under inversion",
|
||||
smgrout(DEFAULT_SMGR));
|
||||
}
|
||||
/*--------------------
|
||||
* We hold AccessShareLock on any large object we have open
|
||||
* by inv_create or inv_open; it is released by inv_close.
|
||||
* Note this will not conflict with ExclusiveLock or ShareLock
|
||||
* that we acquire when actually reading/writing; it just prevents
|
||||
* deletion of the large object while we have it open.
|
||||
*--------------------
|
||||
*/
|
||||
r = heap_openr(objname, AccessShareLock);
|
||||
|
||||
/*
|
||||
* Now create a btree index on the relation's olastbyte attribute to
|
||||
@@ -205,10 +208,7 @@ inv_open(Oid lobjId, int flags)
|
||||
char *indname;
|
||||
Relation indrel;
|
||||
|
||||
r = heap_open(lobjId);
|
||||
|
||||
if (!RelationIsValid(r))
|
||||
return (LargeObjectDesc *) NULL;
|
||||
r = heap_open(lobjId, AccessShareLock);
|
||||
|
||||
indname = pstrdup((r->rd_rel->relname).data);
|
||||
|
||||
@@ -262,8 +262,8 @@ inv_close(LargeObjectDesc *obj_desc)
|
||||
obj_desc->iscan = NULL;
|
||||
}
|
||||
|
||||
heap_close(obj_desc->heap_r);
|
||||
index_close(obj_desc->index_r);
|
||||
heap_close(obj_desc->heap_r, AccessShareLock);
|
||||
|
||||
pfree(obj_desc);
|
||||
}
|
||||
@@ -279,7 +279,7 @@ inv_destroy(Oid lobjId)
|
||||
Relation r;
|
||||
|
||||
r = (Relation) RelationIdGetRelation(lobjId);
|
||||
if (!RelationIsValid(r) || r->rd_rel->relkind == RELKIND_INDEX)
|
||||
if (!RelationIsValid(r) || r->rd_rel->relkind != RELKIND_LOBJECT)
|
||||
return -1;
|
||||
|
||||
heap_destroy_with_catalog(r->rd_rel->relname.data);
|
||||
@@ -497,7 +497,7 @@ inv_write(LargeObjectDesc *obj_desc, char *buf, int nbytes)
|
||||
|
||||
if (!(obj_desc->flags & IFS_WRLOCK))
|
||||
{
|
||||
LockRelation(obj_desc->heap_r, ShareLock);
|
||||
LockRelation(obj_desc->heap_r, ExclusiveLock);
|
||||
obj_desc->flags |= (IFS_WRLOCK | IFS_RDLOCK);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lmgr.c,v 1.34 1999/09/04 18:42:14 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lmgr.c,v 1.35 1999/09/18 19:07:38 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -20,11 +20,12 @@
|
||||
|
||||
|
||||
#include "postgres.h"
|
||||
|
||||
#include "access/transam.h"
|
||||
#include "catalog/catalog.h"
|
||||
#include "miscadmin.h"
|
||||
#include "utils/inval.h"
|
||||
|
||||
extern Oid MyDatabaseId;
|
||||
|
||||
static LOCKMASK LockConflicts[] = {
|
||||
(int) NULL,
|
||||
@@ -106,37 +107,25 @@ InitLockTable()
|
||||
/*
|
||||
* RelationInitLockInfo
|
||||
* Initializes the lock information in a relation descriptor.
|
||||
*
|
||||
* relcache.c must call this during creation of any reldesc.
|
||||
*/
|
||||
void
|
||||
RelationInitLockInfo(Relation relation)
|
||||
{
|
||||
LockInfo info;
|
||||
char *relname;
|
||||
MemoryContext oldcxt;
|
||||
extern Oid MyDatabaseId; /* XXX use include */
|
||||
extern GlobalMemory CacheCxt;
|
||||
|
||||
Assert(RelationIsValid(relation));
|
||||
Assert(OidIsValid(RelationGetRelid(relation)));
|
||||
|
||||
info = (LockInfo) relation->lockInfo;
|
||||
|
||||
if (LockInfoIsValid(info))
|
||||
return;
|
||||
|
||||
relname = (char *) RelationGetRelationName(relation);
|
||||
|
||||
oldcxt = MemoryContextSwitchTo((MemoryContext) CacheCxt);
|
||||
info = (LockInfo) palloc(sizeof(LockInfoData));
|
||||
MemoryContextSwitchTo(oldcxt);
|
||||
relation->rd_lockInfo.lockRelId.relId = RelationGetRelid(relation);
|
||||
|
||||
info->lockRelId.relId = RelationGetRelid(relation);
|
||||
if (IsSharedSystemRelationName(relname))
|
||||
info->lockRelId.dbId = InvalidOid;
|
||||
relation->rd_lockInfo.lockRelId.dbId = InvalidOid;
|
||||
else
|
||||
info->lockRelId.dbId = MyDatabaseId;
|
||||
|
||||
relation->lockInfo = (Pointer) info;
|
||||
relation->rd_lockInfo.lockRelId.dbId = MyDatabaseId;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -145,20 +134,14 @@ RelationInitLockInfo(Relation relation)
|
||||
void
|
||||
LockRelation(Relation relation, LOCKMODE lockmode)
|
||||
{
|
||||
LockInfo lockinfo;
|
||||
LOCKTAG tag;
|
||||
|
||||
if (LockingDisabled())
|
||||
return;
|
||||
|
||||
if (!LockInfoIsValid(relation->lockInfo))
|
||||
RelationInitLockInfo(relation);
|
||||
|
||||
lockinfo = (LockInfo) relation->lockInfo;
|
||||
|
||||
MemSet(&tag, 0, sizeof(tag));
|
||||
tag.relId = lockinfo->lockRelId.relId;
|
||||
tag.dbId = lockinfo->lockRelId.dbId;
|
||||
tag.relId = relation->rd_lockInfo.lockRelId.relId;
|
||||
tag.dbId = relation->rd_lockInfo.lockRelId.dbId;
|
||||
tag.objId.blkno = InvalidBlockNumber;
|
||||
|
||||
LockAcquire(LockTableId, &tag, lockmode);
|
||||
@@ -180,28 +163,17 @@ LockRelation(Relation relation, LOCKMODE lockmode)
|
||||
void
|
||||
UnlockRelation(Relation relation, LOCKMODE lockmode)
|
||||
{
|
||||
LockInfo lockinfo;
|
||||
LOCKTAG tag;
|
||||
|
||||
if (LockingDisabled())
|
||||
return;
|
||||
|
||||
lockinfo = (LockInfo) relation->lockInfo;
|
||||
|
||||
if (!LockInfoIsValid(lockinfo))
|
||||
{
|
||||
elog(ERROR,
|
||||
"Releasing a lock on %s with invalid lock information",
|
||||
RelationGetRelationName(relation));
|
||||
}
|
||||
|
||||
MemSet(&tag, 0, sizeof(tag));
|
||||
tag.relId = lockinfo->lockRelId.relId;
|
||||
tag.dbId = lockinfo->lockRelId.dbId;
|
||||
tag.relId = relation->rd_lockInfo.lockRelId.relId;
|
||||
tag.dbId = relation->rd_lockInfo.lockRelId.dbId;
|
||||
tag.objId.blkno = InvalidBlockNumber;
|
||||
|
||||
LockRelease(LockTableId, &tag, lockmode);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -210,24 +182,17 @@ UnlockRelation(Relation relation, LOCKMODE lockmode)
|
||||
void
|
||||
LockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
|
||||
{
|
||||
LockInfo lockinfo;
|
||||
LOCKTAG tag;
|
||||
|
||||
if (LockingDisabled())
|
||||
return;
|
||||
|
||||
if (!LockInfoIsValid(relation->lockInfo))
|
||||
RelationInitLockInfo(relation);
|
||||
|
||||
lockinfo = (LockInfo) relation->lockInfo;
|
||||
|
||||
MemSet(&tag, 0, sizeof(tag));
|
||||
tag.relId = lockinfo->lockRelId.relId;
|
||||
tag.dbId = lockinfo->lockRelId.dbId;
|
||||
tag.relId = relation->rd_lockInfo.lockRelId.relId;
|
||||
tag.dbId = relation->rd_lockInfo.lockRelId.dbId;
|
||||
tag.objId.blkno = blkno;
|
||||
|
||||
LockAcquire(LockTableId, &tag, lockmode);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -236,28 +201,17 @@ LockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
|
||||
void
|
||||
UnlockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
|
||||
{
|
||||
LockInfo lockinfo;
|
||||
LOCKTAG tag;
|
||||
|
||||
if (LockingDisabled())
|
||||
return;
|
||||
|
||||
lockinfo = (LockInfo) relation->lockInfo;
|
||||
|
||||
if (!LockInfoIsValid(lockinfo))
|
||||
{
|
||||
elog(ERROR,
|
||||
"Releasing a lock on %s with invalid lock information",
|
||||
RelationGetRelationName(relation));
|
||||
}
|
||||
|
||||
MemSet(&tag, 0, sizeof(tag));
|
||||
tag.relId = lockinfo->lockRelId.relId;
|
||||
tag.dbId = lockinfo->lockRelId.dbId;
|
||||
tag.relId = relation->rd_lockInfo.lockRelId.relId;
|
||||
tag.dbId = relation->rd_lockInfo.lockRelId.dbId;
|
||||
tag.objId.blkno = blkno;
|
||||
|
||||
LockRelease(LockTableId, &tag, lockmode);
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
@@ -274,7 +228,6 @@ XactLockTableInsert(TransactionId xid)
|
||||
tag.objId.xid = xid;
|
||||
|
||||
LockAcquire(LockTableId, &tag, ExclusiveLock);
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
@@ -291,7 +244,6 @@ XactLockTableDelete(TransactionId xid)
|
||||
tag.objId.xid = xid;
|
||||
|
||||
LockRelease(LockTableId, &tag, ExclusiveLock);
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
@@ -316,6 +268,4 @@ XactLockTableWait(TransactionId xid)
|
||||
*/
|
||||
if (!TransactionIdDidCommit(xid) && !TransactionIdDidAbort(xid))
|
||||
TransactionIdAbort(xid);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lock.c,v 1.61 1999/07/17 20:17:46 momjian Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lock.c,v 1.62 1999/09/18 19:07:38 tgl Exp $
|
||||
*
|
||||
* NOTES
|
||||
* Outside modules can create a lock table and acquire/release
|
||||
@@ -596,7 +596,7 @@ LockAcquire(LOCKMETHOD lockmethod, LOCKTAG *locktag, LOCKMODE lockmode)
|
||||
if (!result)
|
||||
{
|
||||
elog(NOTICE, "LockAcquire: xid table corrupted");
|
||||
return STATUS_ERROR;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1117,7 +1117,7 @@ LockRelease(LOCKMETHOD lockmethod, LOCKTAG *locktag, LOCKMODE lockmode)
|
||||
TPRINTF(TRACE_USERLOCKS, "LockRelease: no lock with this tag");
|
||||
else
|
||||
#endif
|
||||
elog(NOTICE, "LockReplace: xid table corrupted");
|
||||
elog(NOTICE, "LockRelease: xid table corrupted");
|
||||
return FALSE;
|
||||
}
|
||||
XID_PRINT("LockRelease: found", result);
|
||||
|
||||
Reference in New Issue
Block a user