mirror of
https://github.com/postgres/postgres.git
synced 2025-11-10 17:42:29 +03:00
Give the ResourceOwner mechanism full responsibility for releasing buffer
pins at end of transaction, and reduce AtEOXact_Buffers to an Assert cross-check that this was done correctly. When not USE_ASSERT_CHECKING, AtEOXact_Buffers is a complete no-op. This gets rid of an O(NBuffers) bottleneck during transaction commit/abort, which recent testing has shown becomes significant above a few tens of thousands of shared buffers.
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/storage/buffer/bufmgr.c,v 1.179 2004/10/16 18:05:06 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/storage/buffer/bufmgr.c,v 1.180 2004/10/16 18:57:23 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -851,35 +851,45 @@ ResetBufferUsage(void)
|
||||
/*
|
||||
* AtEOXact_Buffers - clean up at end of transaction.
|
||||
*
|
||||
* During abort, we need to release any buffer pins we're holding
|
||||
* (this cleans up in case ereport interrupted a routine that pins a
|
||||
* buffer). During commit, we shouldn't need to do that, but check
|
||||
* anyway to see if anyone leaked a buffer reference count.
|
||||
* As of PostgreSQL 8.0, buffer pins should get released by the
|
||||
* ResourceOwner mechanism. This routine is just a debugging
|
||||
* cross-check that no pins remain.
|
||||
*/
|
||||
void
|
||||
AtEOXact_Buffers(bool isCommit)
|
||||
{
|
||||
#ifdef USE_ASSERT_CHECKING
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NBuffers; i++)
|
||||
{
|
||||
Assert(PrivateRefCount[i] == 0);
|
||||
}
|
||||
|
||||
AtEOXact_LocalBuffers(isCommit);
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* Ensure we have released all shared-buffer locks and pins during backend exit
|
||||
*/
|
||||
void
|
||||
AtProcExit_Buffers(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
AbortBufferIO();
|
||||
UnlockBuffers();
|
||||
|
||||
for (i = 0; i < NBuffers; i++)
|
||||
{
|
||||
if (PrivateRefCount[i] != 0)
|
||||
{
|
||||
BufferDesc *buf = &(BufferDescriptors[i]);
|
||||
|
||||
if (isCommit)
|
||||
elog(WARNING,
|
||||
"buffer refcount leak: [%03d] "
|
||||
"(rel=%u/%u/%u, blockNum=%u, flags=0x%x, refcount=%u %d)",
|
||||
i,
|
||||
buf->tag.rnode.spcNode, buf->tag.rnode.dbNode,
|
||||
buf->tag.rnode.relNode,
|
||||
buf->tag.blockNum, buf->flags,
|
||||
buf->refcount, PrivateRefCount[i]);
|
||||
|
||||
/*
|
||||
* We don't worry about updating the ResourceOwner structures;
|
||||
* resowner.c will clear them for itself.
|
||||
* We don't worry about updating ResourceOwner; if we even got
|
||||
* here, it suggests that ResourceOwners are messed up.
|
||||
*/
|
||||
PrivateRefCount[i] = 1; /* make sure we release shared pin */
|
||||
LWLockAcquire(BufMgrLock, LW_EXCLUSIVE);
|
||||
@@ -888,8 +898,37 @@ AtEOXact_Buffers(bool isCommit)
|
||||
Assert(PrivateRefCount[i] == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AtEOXact_LocalBuffers(isCommit);
|
||||
/*
|
||||
* Helper routine to issue warnings when a buffer is unexpectedly pinned
|
||||
*/
|
||||
void
|
||||
PrintBufferLeakWarning(Buffer buffer)
|
||||
{
|
||||
BufferDesc *buf;
|
||||
int32 loccount;
|
||||
|
||||
Assert(BufferIsValid(buffer));
|
||||
if (BufferIsLocal(buffer))
|
||||
{
|
||||
buf = &LocalBufferDescriptors[-buffer - 1];
|
||||
loccount = LocalRefCount[-buffer - 1];
|
||||
}
|
||||
else
|
||||
{
|
||||
buf = &BufferDescriptors[buffer - 1];
|
||||
loccount = PrivateRefCount[buffer - 1];
|
||||
}
|
||||
|
||||
elog(WARNING,
|
||||
"buffer refcount leak: [%03d] "
|
||||
"(rel=%u/%u/%u, blockNum=%u, flags=0x%x, refcount=%u %d)",
|
||||
buffer,
|
||||
buf->tag.rnode.spcNode, buf->tag.rnode.dbNode,
|
||||
buf->tag.rnode.relNode,
|
||||
buf->tag.blockNum, buf->flags,
|
||||
buf->refcount, loccount);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/storage/buffer/localbuf.c,v 1.59 2004/08/29 05:06:47 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/storage/buffer/localbuf.c,v 1.60 2004/10/16 18:57:24 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -232,23 +232,12 @@ InitLocalBuffer(void)
|
||||
void
|
||||
AtEOXact_LocalBuffers(bool isCommit)
|
||||
{
|
||||
#ifdef USE_ASSERT_CHECKING
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NLocBuffer; i++)
|
||||
{
|
||||
if (LocalRefCount[i] != 0)
|
||||
{
|
||||
BufferDesc *buf = &(LocalBufferDescriptors[i]);
|
||||
|
||||
if (isCommit)
|
||||
elog(WARNING,
|
||||
"local buffer leak: [%03d] (rel=%u/%u/%u, blockNum=%u, flags=0x%x, refcount=%u %d)",
|
||||
i,
|
||||
buf->tag.rnode.spcNode, buf->tag.rnode.dbNode,
|
||||
buf->tag.rnode.relNode, buf->tag.blockNum, buf->flags,
|
||||
buf->refcount, LocalRefCount[i]);
|
||||
|
||||
LocalRefCount[i] = 0;
|
||||
}
|
||||
Assert(LocalRefCount[i] == 0);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/storage/lmgr/proc.c,v 1.154 2004/09/29 15:15:55 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/storage/lmgr/proc.c,v 1.155 2004/10/16 18:57:24 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -461,9 +461,7 @@ ProcKill(int code, Datum arg)
|
||||
* shutdown callback registered by the bufmgr ... but we must do this
|
||||
* *after* LWLockReleaseAll and *before* zapping MyProc.
|
||||
*/
|
||||
AbortBufferIO();
|
||||
UnlockBuffers();
|
||||
AtEOXact_Buffers(false);
|
||||
AtProcExit_Buffers();
|
||||
|
||||
/* Get off any wait queue I might be on */
|
||||
LockWaitCancel();
|
||||
@@ -509,9 +507,7 @@ DummyProcKill(int code, Datum arg)
|
||||
LWLockReleaseAll();
|
||||
|
||||
/* Release buffer locks and pins, too */
|
||||
AbortBufferIO();
|
||||
UnlockBuffers();
|
||||
AtEOXact_Buffers(false);
|
||||
AtProcExit_Buffers();
|
||||
|
||||
/* I can't be on regular lock queues, so needn't check */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user