9
9
*
10
10
*
11
11
* IDENTIFICATION
12
- * $PostgreSQL: pgsql/src/backend/storage/buffer/localbuf.c,v 1.65 2005/03/1917:39:43 tgl Exp $
12
+ * $PostgreSQL: pgsql/src/backend/storage/buffer/localbuf.c,v 1.66 2005/03/1923:27:05 tgl Exp $
13
13
*
14
14
*-------------------------------------------------------------------------
15
15
*/
18
18
#include "storage/buf_internals.h"
19
19
#include "storage/bufmgr.h"
20
20
#include "storage/smgr.h"
21
+ #include "utils/guc.h"
21
22
#include "utils/memutils.h"
22
23
#include "utils/resowner.h"
23
24
@@ -46,6 +47,9 @@ static intnextFreeLocalBuf = 0;
46
47
static HTAB * LocalBufHash = NULL ;
47
48
48
49
50
+ static void InitLocalBuffers (void );
51
+
52
+
49
53
/*
50
54
* LocalBufferAlloc -
51
55
* Find or create a local buffer for the given page of the given relation.
@@ -66,6 +70,10 @@ LocalBufferAlloc(Relation reln, BlockNumber blockNum, bool *foundPtr)
66
70
67
71
INIT_BUFFERTAG (newTag ,reln ,blockNum );
68
72
73
+ /* Initialize local buffers if first request in this session */
74
+ if (LocalBufHash == NULL )
75
+ InitLocalBuffers ();
76
+
69
77
/* See if the desired buffer already exists */
70
78
hresult = (LocalBufferLookupEnt * )
71
79
hash_search (LocalBufHash , (void * )& newTag ,HASH_FIND ,NULL );
@@ -238,32 +246,18 @@ WriteLocalBuffer(Buffer buffer, bool release)
238
246
}
239
247
240
248
/*
241
- *InitLocalBuffer -
249
+ *InitLocalBuffers -
242
250
* init the local buffer cache. Since most queries (esp. multi-user ones)
243
251
* don't involve local buffers, we delay allocating actual memory for the
244
252
* buffers until we need them; just make the buffer headers here.
245
253
*/
246
- void
247
- InitLocalBuffer (void )
254
+ static void
255
+ InitLocalBuffers (void )
248
256
{
249
- int nbufs = 64 ; /* should be from a GUC var */
257
+ int nbufs = num_temp_buffers ;
250
258
HASHCTL info ;
251
259
int i ;
252
260
253
- /* Create the lookup hash table */
254
- MemSet (& info ,0 ,sizeof (info ));
255
- info .keysize = sizeof (BufferTag );
256
- info .entrysize = sizeof (LocalBufferLookupEnt );
257
- info .hash = tag_hash ;
258
-
259
- LocalBufHash = hash_create ("Local Buffer Lookup Table" ,
260
- nbufs ,
261
- & info ,
262
- HASH_ELEM |HASH_FUNCTION );
263
-
264
- if (!LocalBufHash )
265
- elog (ERROR ,"could not initialize local buffer hash table" );
266
-
267
261
/* Allocate and zero buffer headers and auxiliary arrays */
268
262
LocalBufferDescriptors = (BufferDesc * )
269
263
MemoryContextAllocZero (TopMemoryContext ,
@@ -291,6 +285,20 @@ InitLocalBuffer(void)
291
285
buf -> buf_id = - i - 2 ;
292
286
}
293
287
288
+ /* Create the lookup hash table */
289
+ MemSet (& info ,0 ,sizeof (info ));
290
+ info .keysize = sizeof (BufferTag );
291
+ info .entrysize = sizeof (LocalBufferLookupEnt );
292
+ info .hash = tag_hash ;
293
+
294
+ LocalBufHash = hash_create ("Local Buffer Lookup Table" ,
295
+ nbufs ,
296
+ & info ,
297
+ HASH_ELEM |HASH_FUNCTION );
298
+
299
+ if (!LocalBufHash )
300
+ elog (ERROR ,"could not initialize local buffer hash table" );
301
+
294
302
/* Initialization done, mark buffers allocated */
295
303
NLocBuffer = nbufs ;
296
304
}