aboutsummaryrefslogtreecommitdiff
path: root/src/backend/storage/buffer/buf_init.c
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/buffer/buf_init.c
parent8c8ed4f456f0b343d5df332e0ff31c6bb889429f (diff)
downloadpostgresql-a626b78c8957f50ae6345015b249e996d9aab55d.tar.gz
postgresql-a626b78c8957f50ae6345015b249e996d9aab55d.zip
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/buffer/buf_init.c')
-rw-r--r--src/backend/storage/buffer/buf_init.c56
1 files changed, 50 insertions, 6 deletions
diff --git a/src/backend/storage/buffer/buf_init.c b/src/backend/storage/buffer/buf_init.c
index 19b71933d76..54b9d02e160 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);
}
/* -----------------------------------------------------