8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $Header: /cvsroot/pgsql/src/backend/storage/buffer/buf_init.c,v 1.39 2000/11/30 01:39:07 tgl Exp $
11
+ * $Header: /cvsroot/pgsql/src/backend/storage/buffer/buf_init.c,v 1.40 2000/12/18 00:44:46 tgl Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
36
36
#include "utils/hsearch.h"
37
37
#include "utils/memutils.h"
38
38
39
+
40
+ static void ShutdownBufferPoolAccess (void );
41
+
39
42
/*
40
43
* if BMTRACE is defined, we trace the last 200 buffer allocations and
41
44
* deallocations in a circular buffer in shared memory.
@@ -73,7 +76,7 @@ bool *BufferDirtiedByMe; /* T if buf has been dirtied in cur xact */
73
76
* Two important notes. First, the buffer has to be
74
77
* available for lookup BEFORE an IO begins. Otherwise
75
78
* a second process trying to read the buffer will
76
- * allocate its own copy and the buffeer pool will
79
+ * allocate its own copy and the buffer pool will
77
80
* become inconsistent.
78
81
*
79
82
* Buffer Replacement:
@@ -126,10 +129,10 @@ long int LocalBufferFlushCount;
126
129
127
130
128
131
/*
129
- * Initialize module: called once during shared-memory initialization
132
+ * Initialize shared buffer pool
130
133
*
131
- * should calculate size of pool dynamically based on the
132
- * amount of available memory .
134
+ * This is called once during shared-memory initialization (either in the
135
+ * postmaster, or in a standalone backend) .
133
136
*/
134
137
void
135
138
InitBufferPool (void )
@@ -144,6 +147,10 @@ InitBufferPool(void)
144
147
Lookup_List_Descriptor = Data_Descriptors + 1 ;
145
148
Num_Descriptors = Data_Descriptors + 1 ;
146
149
150
+ /*
151
+ * It's probably not really necessary to grab the lock --- if there's
152
+ * anyone else attached to the shmem at this point, we've got problems.
153
+ */
147
154
SpinAcquire (BufMgrLock );
148
155
149
156
#ifdef BMTRACE
@@ -203,12 +210,28 @@ InitBufferPool(void)
203
210
BufferDescriptors [Data_Descriptors - 1 ].freeNext = 0 ;
204
211
}
205
212
206
- /* Init the rest of the module */
213
+ /* Init other shared buffer-management stuff */
207
214
InitBufTable ();
208
215
InitFreeList (!foundDescs );
209
216
210
217
SpinRelease (BufMgrLock );
218
+ }
219
+
220
+ /*
221
+ * Initialize access to shared buffer pool
222
+ *
223
+ * This is called during backend startup (whether standalone or under the
224
+ * postmaster). It sets up for this backend's access to the already-existing
225
+ * buffer pool.
226
+ */
227
+ void
228
+ InitBufferPoolAccess (void )
229
+ {
230
+ int i ;
211
231
232
+ /*
233
+ * Allocate and zero local arrays of per-buffer info.
234
+ */
212
235
BufferBlockPointers = (Block * ) calloc (NBuffers , sizeof (Block ));
213
236
PrivateRefCount = (long * ) calloc (NBuffers , sizeof (long ));
214
237
BufferLocks = (bits8 * ) calloc (NBuffers , sizeof (bits8 ));
@@ -224,6 +247,27 @@ InitBufferPool(void)
224
247
{
225
248
BufferBlockPointers [i ] = (Block ) MAKE_PTR (BufferDescriptors [i ].data );
226
249
}
250
+
251
+ /*
252
+ * Now that buffer access is initialized, set up a callback to shut it
253
+ * down again at backend exit.
254
+ */
255
+ on_shmem_exit (ShutdownBufferPoolAccess , 0 );
256
+ }
257
+
258
+ /*
259
+ * Shut down buffer manager at backend exit.
260
+ *
261
+ * This is needed mainly to ensure that we don't leave any buffer reference
262
+ * counts set during an error exit.
263
+ */
264
+ static void
265
+ ShutdownBufferPoolAccess (void )
266
+ {
267
+ /* Release any buffer context locks we are holding */
268
+ UnlockBuffers ();
269
+ /* Release any buffer reference counts we are holding */
270
+ ResetBufferPool (false);
227
271
}
228
272
229
273
/* -----------------------------------------------------
0 commit comments