summaryrefslogtreecommitdiff
path: root/src/backend/storage
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2000-12-18 00:44:50 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2000-12-18 00:44:50 +0000
commita626b78c8957f50ae6345015b249e996d9aab55d (patch)
tree352a378cead2660c6c4a1704007cfb0d96f96183 /src/backend/storage
parent8c8ed4f456f0b343d5df332e0ff31c6bb889429f (diff)
downloadpostgresql-a626b78c8957f50ae6345015b249e996d9aab55d.tar.gz
Clean up backend-exit-time cleanup behavior. Use on_shmem_exit callbacks
to ensure that we have released buffer refcounts and so forth, rather than putting ad-hoc operations before (some of the calls to) proc_exit. Add commentary to discourage future hackers from repeating that mistake.
Diffstat (limited to 'src/backend/storage')
-rw-r--r--src/backend/storage/buffer/buf_init.c56
-rw-r--r--src/backend/storage/lmgr/proc.c13
2 files changed, 56 insertions, 13 deletions
diff --git a/src/backend/storage/buffer/buf_init.c b/src/backend/storage/buffer/buf_init.c
index 19b71933d7..54b9d02e16 100644
--- a/src/backend/storage/buffer/buf_init.c
+++ b/src/backend/storage/buffer/buf_init.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/storage/buffer/buf_init.c,v 1.39 2000/11/30 01:39:07 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/storage/buffer/buf_init.c,v 1.40 2000/12/18 00:44:46 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -36,6 +36,9 @@
#include "utils/hsearch.h"
#include "utils/memutils.h"
+
+static void ShutdownBufferPoolAccess(void);
+
/*
* if BMTRACE is defined, we trace the last 200 buffer allocations and
* deallocations in a circular buffer in shared memory.
@@ -73,7 +76,7 @@ bool *BufferDirtiedByMe; /* T if buf has been dirtied in cur xact */
* Two important notes. First, the buffer has to be
* available for lookup BEFORE an IO begins. Otherwise
* a second process trying to read the buffer will
- * allocate its own copy and the buffeer pool will
+ * allocate its own copy and the buffer pool will
* become inconsistent.
*
* Buffer Replacement:
@@ -126,10 +129,10 @@ long int LocalBufferFlushCount;
/*
- * Initialize module: called once during shared-memory initialization
+ * Initialize shared buffer pool
*
- * should calculate size of pool dynamically based on the
- * amount of available memory.
+ * This is called once during shared-memory initialization (either in the
+ * postmaster, or in a standalone backend).
*/
void
InitBufferPool(void)
@@ -144,6 +147,10 @@ InitBufferPool(void)
Lookup_List_Descriptor = Data_Descriptors + 1;
Num_Descriptors = Data_Descriptors + 1;
+ /*
+ * It's probably not really necessary to grab the lock --- if there's
+ * anyone else attached to the shmem at this point, we've got problems.
+ */
SpinAcquire(BufMgrLock);
#ifdef BMTRACE
@@ -203,12 +210,28 @@ InitBufferPool(void)
BufferDescriptors[Data_Descriptors - 1].freeNext = 0;
}
- /* Init the rest of the module */
+ /* Init other shared buffer-management stuff */
InitBufTable();
InitFreeList(!foundDescs);
SpinRelease(BufMgrLock);
+}
+
+/*
+ * Initialize access to shared buffer pool
+ *
+ * This is called during backend startup (whether standalone or under the
+ * postmaster). It sets up for this backend's access to the already-existing
+ * buffer pool.
+ */
+void
+InitBufferPoolAccess(void)
+{
+ int i;
+ /*
+ * Allocate and zero local arrays of per-buffer info.
+ */
BufferBlockPointers = (Block *) calloc(NBuffers, sizeof(Block));
PrivateRefCount = (long *) calloc(NBuffers, sizeof(long));
BufferLocks = (bits8 *) calloc(NBuffers, sizeof(bits8));
@@ -224,6 +247,27 @@ InitBufferPool(void)
{
BufferBlockPointers[i] = (Block) MAKE_PTR(BufferDescriptors[i].data);
}
+
+ /*
+ * Now that buffer access is initialized, set up a callback to shut it
+ * down again at backend exit.
+ */
+ on_shmem_exit(ShutdownBufferPoolAccess, 0);
+}
+
+/*
+ * Shut down buffer manager at backend exit.
+ *
+ * This is needed mainly to ensure that we don't leave any buffer reference
+ * counts set during an error exit.
+ */
+static void
+ShutdownBufferPoolAccess(void)
+{
+ /* Release any buffer context locks we are holding */
+ UnlockBuffers();
+ /* Release any buffer reference counts we are holding */
+ ResetBufferPool(false);
}
/* -----------------------------------------------------
diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c
index 29502c3016..a95432e0f8 100644
--- a/src/backend/storage/lmgr/proc.c
+++ b/src/backend/storage/lmgr/proc.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.86 2000/12/11 16:35:59 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.87 2000/12/18 00:44:47 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -29,7 +29,8 @@
*
* Interface (b):
*
- * ProcReleaseLocks -- frees the locks associated with this process,
+ * ProcReleaseLocks -- frees the locks associated with current transaction
+ *
* ProcKill -- destroys the shared memory state (and locks)
* associated with the process.
*
@@ -47,7 +48,7 @@
* This is so that we can support more backends. (system-wide semaphore
* sets run out pretty fast.) -ay 4/95
*
- * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.86 2000/12/11 16:35:59 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.87 2000/12/18 00:44:47 tgl Exp $
*/
#include "postgres.h"
@@ -332,7 +333,7 @@ GetOffWaitqueue(PROC *proc)
}
/*
- * ProcReleaseLocks() -- release all locks associated with this process
+ * ProcReleaseLocks() -- release all locks associated with current transaction
*
*/
void
@@ -340,7 +341,7 @@ ProcReleaseLocks()
{
if (!MyProc)
return;
- LockReleaseAll(1, &MyProc->lockQueue);
+ LockReleaseAll(DEFAULT_LOCKMETHOD, &MyProc->lockQueue);
GetOffWaitqueue(MyProc);
}
@@ -423,8 +424,6 @@ ProcKill(int exitStatus, Datum pid)
* ----------------
*/
GetOffWaitqueue(proc);
-
- return;
}
/*