99 *
1010 *
1111 * IDENTIFICATION
12- * $PostgreSQL: pgsql/src/backend/storage/buffer/localbuf.c,v 1.65 2005/03/19 17:39:43 tgl Exp $
12+ * $PostgreSQL: pgsql/src/backend/storage/buffer/localbuf.c,v 1.66 2005/03/19 23:27:05 tgl Exp $
1313 *
1414 *-------------------------------------------------------------------------
1515 */
1818#include "storage/buf_internals.h"
1919#include "storage/bufmgr.h"
2020#include "storage/smgr.h"
21+ #include "utils/guc.h"
2122#include "utils/memutils.h"
2223#include "utils/resowner.h"
2324
@@ -46,6 +47,9 @@ static int nextFreeLocalBuf = 0;
4647static HTAB * LocalBufHash = NULL ;
4748
4849
50+ static void InitLocalBuffers (void );
51+
52+
4953/*
5054 * LocalBufferAlloc -
5155 * 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)
6670
6771 INIT_BUFFERTAG (newTag , reln , blockNum );
6872
73+ /* Initialize local buffers if first request in this session */
74+ if (LocalBufHash == NULL )
75+ InitLocalBuffers ();
76+
6977 /* See if the desired buffer already exists */
7078 hresult = (LocalBufferLookupEnt * )
7179 hash_search (LocalBufHash , (void * ) & newTag , HASH_FIND , NULL );
@@ -238,32 +246,18 @@ WriteLocalBuffer(Buffer buffer, bool release)
238246}
239247
240248/*
241- * InitLocalBuffer -
249+ * InitLocalBuffers -
242250 * init the local buffer cache. Since most queries (esp. multi-user ones)
243251 * don't involve local buffers, we delay allocating actual memory for the
244252 * buffers until we need them; just make the buffer headers here.
245253 */
246- void
247- InitLocalBuffer (void )
254+ static void
255+ InitLocalBuffers (void )
248256{
249- int nbufs = 64 ; /* should be from a GUC var */
257+ int nbufs = num_temp_buffers ;
250258 HASHCTL info ;
251259 int i ;
252260
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-
267261 /* Allocate and zero buffer headers and auxiliary arrays */
268262 LocalBufferDescriptors = (BufferDesc * )
269263 MemoryContextAllocZero (TopMemoryContext ,
@@ -291,6 +285,20 @@ InitLocalBuffer(void)
291285 buf -> buf_id = - i - 2 ;
292286 }
293287
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+
294302 /* Initialization done, mark buffers allocated */
295303 NLocBuffer = nbufs ;
296304}
0 commit comments