Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbcfdc9d

Browse files
committed
Repair some pretty serious problems in dynahash.c and
shared memory space allocation. It's a wonder we have not seen bugreports traceable to this area ... it's quite clear that the routinedir_realloc() has never worked correctly, for example.
1 parentceb233e commitbcfdc9d

File tree

7 files changed

+178
-173
lines changed

7 files changed

+178
-173
lines changed

‎src/backend/storage/buffer/buf_init.c

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/buf_init.c,v 1.21 1999/02/13 23:17:54 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/buf_init.c,v 1.22 1999/02/22 06:16:50 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -33,7 +33,6 @@
3333
#include"storage/lmgr.h"
3434
#include"miscadmin.h"
3535
#include"utils/builtins.h"
36-
#include"utils/dynahash.h"
3736
#include"utils/hsearch.h"
3837
#include"utils/memutils.h"
3938
#include"executor/execdebug.h"/* for NDirectFileRead */
@@ -261,21 +260,11 @@ int
261260
BufferShmemSize()
262261
{
263262
intsize=0;
264-
intnbuckets;
265-
intnsegs;
266-
inttmp;
267-
268-
nbuckets=1 << (int)my_log2((NBuffers-1) /DEF_FFACTOR+1);
269-
nsegs=1 << (int)my_log2((nbuckets-1) /DEF_SEGSIZE+1);
270-
271-
/* size of shmem index table */
272-
size+=MAXALIGN(my_log2(SHMEM_INDEX_SIZE)*sizeof(void*));/* HTAB->dir */
273-
size+=MAXALIGN(sizeof(HHDR));/* HTAB->hctl */
274-
size+=MAXALIGN(DEF_SEGSIZE*sizeof(SEGMENT));
275-
size+=BUCKET_ALLOC_INCR*
276-
(MAXALIGN(sizeof(BUCKET_INDEX))+
277-
MAXALIGN(SHMEM_INDEX_KEYSIZE)+
278-
MAXALIGN(SHMEM_INDEX_DATASIZE));
263+
264+
/* size of shmem index hash table */
265+
size+=hash_estimate_size(SHMEM_INDEX_SIZE,
266+
SHMEM_INDEX_KEYSIZE,
267+
SHMEM_INDEX_DATASIZE);
279268

280269
/* size of buffer descriptors */
281270
size+=MAXALIGN((NBuffers+1)*sizeof(BufferDesc));
@@ -284,17 +273,13 @@ BufferShmemSize()
284273
size+=NBuffers*MAXALIGN(BLCKSZ);
285274

286275
/* size of buffer hash table */
287-
size+=MAXALIGN(my_log2(NBuffers)*sizeof(void*));/* HTAB->dir */
288-
size+=MAXALIGN(sizeof(HHDR));/* HTAB->hctl */
289-
size+=nsegs*MAXALIGN(DEF_SEGSIZE*sizeof(SEGMENT));
290-
tmp= (int)ceil((double)NBuffers /BUCKET_ALLOC_INCR);
291-
size+=tmp*BUCKET_ALLOC_INCR*
292-
(MAXALIGN(sizeof(BUCKET_INDEX))+
293-
MAXALIGN(sizeof(BufferTag))+
294-
MAXALIGN(sizeof(Buffer)));
276+
size+=hash_estimate_size(NBuffers,
277+
sizeof(BufferTag),
278+
sizeof(Buffer));
295279

296280
#ifdefBMTRACE
297281
size+= (BMT_LIMIT*sizeof(bmtrace))+sizeof(long);
298282
#endif
283+
299284
returnsize;
300285
}

‎src/backend/storage/ipc/ipci.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/ipci.c,v 1.21 1999/02/21 01:41:44 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/ipci.c,v 1.22 1999/02/22 06:16:49 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -72,11 +72,19 @@ CreateSharedMemoryAndSemaphores(IPCKey key, int maxBackends)
7272
* ----------------
7373
*/
7474
CreateSpinlocks(IPCKeyGetSpinLockSemaphoreKey(key));
75-
size=BufferShmemSize()+LockShmemSize(maxBackends);
7675

76+
/*
77+
* Size of the primary shared-memory block is estimated via
78+
* moderately-accurate estimates for the big hogs, plus 100K for
79+
* the stuff that's too small to bother with estimating.
80+
* Then we add 10% for a safety margin.
81+
*/
82+
size=BufferShmemSize()+LockShmemSize(maxBackends);
7783
#ifdefSTABLE_MEMORY_STORAGE
7884
size+=MMShmemSize();
7985
#endif
86+
size+=100000;
87+
size+=size /10;
8088

8189
if (DebugLvl>1)
8290
{

‎src/backend/storage/ipc/shmem.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmem.c,v 1.36 1999/02/13 23:18:13 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmem.c,v 1.37 1999/02/22 06:16:48 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -65,7 +65,6 @@
6565
#include"storage/shmem.h"
6666
#include"storage/spin.h"
6767
#include"storage/proc.h"
68-
#include"utils/dynahash.h"
6968
#include"utils/hsearch.h"
7069
#include"utils/memutils.h"
7170
#include"access/xact.h"
@@ -215,7 +214,7 @@ InitShmem(unsigned int key, unsigned int size)
215214
/* create OR attach to the shared memory shmem index */
216215
info.keysize=SHMEM_INDEX_KEYSIZE;
217216
info.datasize=SHMEM_INDEX_DATASIZE;
218-
hash_flags=(HASH_ELEM);
217+
hash_flags=HASH_ELEM;
219218

220219
/* This will acquire the shmem index lock, but not release it. */
221220
ShmemIndex=ShmemInitHash("ShmemIndex",
@@ -340,26 +339,29 @@ ShmemIsValid(unsigned long addr)
340339
*/
341340
HTAB*
342341
ShmemInitHash(char*name,/* table string name for shmem index */
343-
longinit_size,/* initial size */
344-
longmax_size,/* max size of the table */
342+
longinit_size,/* initialtablesize */
343+
longmax_size,/* max size of the table(NOT USED)*/
345344
HASHCTL*infoP,/* info about key and bucket size */
346345
inthash_flags)/* info about infoP */
347346
{
348347
boolfound;
349348
long*location;
350349

351350
/*
352-
* shared memory hash tables have a fixed max size so that the control
353-
* structures don't try to grow. The segbase is for calculating
354-
* pointer values.The shared memory allocator must be specified.
351+
* Hash tables allocated in shared memory have a fixed directory;
352+
* it can't grow or other backends wouldn't be able to find it.
353+
* The segbase is for calculating pointer values.
354+
* The shared memory allocator must be specified too.
355355
*/
356+
infoP->dsize=infoP->max_dsize=DEF_DIRSIZE;
356357
infoP->segbase= (long*)ShmemBase;
357358
infoP->alloc=ShmemAlloc;
358-
infoP->max_size=max_size;
359-
hash_flags |=HASH_SHARED_MEM;
359+
hash_flags |=HASH_SHARED_MEM |HASH_DIRSIZE;
360360

361361
/* look it up in the shmem index */
362-
location=ShmemInitStruct(name,my_log2(max_size)+sizeof(HHDR),&found);
362+
location=ShmemInitStruct(name,
363+
sizeof(HHDR)+DEF_DIRSIZE*sizeof(SEG_OFFSET),
364+
&found);
363365

364366
/*
365367
* shmem index is corrupted.Let someone else give the error
@@ -375,13 +377,11 @@ ShmemInitHash(char *name,/* table string name for shmem index */
375377
if (found)
376378
hash_flags |=HASH_ATTACH;
377379

378-
/* these structures were allocated or bound in ShmemInitStruct */
379-
/* control information and parameters */
380+
/* Now provide the header and directory pointers */
380381
infoP->hctl= (long*)location;
381-
/* directory for hash lookup */
382-
infoP->dir= (long*) (location+sizeof(HHDR));
382+
infoP->dir= (long*) (((char*)location)+sizeof(HHDR));
383383

384-
returnhash_create(init_size,infoP,hash_flags);;
384+
returnhash_create(init_size,infoP,hash_flags);
385385
}
386386

387387
/*

‎src/backend/storage/lmgr/lock.c

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lock.c,v 1.44 1999/02/21 03:49:22 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lock.c,v 1.45 1999/02/22 06:16:52 tgl Exp $
1111
*
1212
* NOTES
1313
* Outside modules can create a lock table and acquire/release
@@ -42,7 +42,6 @@
4242
#include"storage/spin.h"
4343
#include"storage/proc.h"
4444
#include"storage/lock.h"
45-
#include"utils/dynahash.h"
4645
#include"utils/hsearch.h"
4746
#include"utils/memutils.h"
4847
#include"utils/palloc.h"
@@ -1481,36 +1480,21 @@ int
14811480
LockShmemSize(intmaxBackends)
14821481
{
14831482
intsize=0;
1484-
intnLockEnts=NLOCKENTS(maxBackends);
1485-
intnLockBuckets,
1486-
nLockSegs;
1487-
intnXidBuckets,
1488-
nXidSegs;
1489-
1490-
nLockBuckets=1 << (int)my_log2((nLockEnts-1) /DEF_FFACTOR+1);
1491-
nLockSegs=1 << (int)my_log2((nLockBuckets-1) /DEF_SEGSIZE+1);
1492-
1493-
nXidBuckets=1 << (int)my_log2((NLOCKS_PER_XACT-1) /DEF_FFACTOR+1);
1494-
nXidSegs=1 << (int)my_log2((nLockBuckets-1) /DEF_SEGSIZE+1);
14951483

1484+
size+=MAXALIGN(sizeof(PROC_HDR));/* ProcGlobal */
14961485
size+=MAXALIGN(maxBackends*sizeof(PROC));/* each MyProc */
14971486
size+=MAXALIGN(maxBackends*sizeof(LOCKMETHODCTL));/* each
14981487
* lockMethodTable->ctl */
1499-
size+=MAXALIGN(sizeof(PROC_HDR));/* ProcGlobal */
15001488

1501-
size+=MAXALIGN(my_log2(nLockEnts)*sizeof(void*));
1502-
size+=MAXALIGN(sizeof(HHDR));
1503-
size+=nLockSegs*MAXALIGN(DEF_SEGSIZE*sizeof(SEGMENT));
1504-
size+=nLockEnts*/* XXX not multiple of BUCKET_ALLOC_INCR? */
1505-
(MAXALIGN(sizeof(BUCKET_INDEX))+
1506-
MAXALIGN(sizeof(LOCK)));/* contains hash key */
1507-
1508-
size+=MAXALIGN(my_log2(maxBackends)*sizeof(void*));
1509-
size+=MAXALIGN(sizeof(HHDR));
1510-
size+=nXidSegs*MAXALIGN(DEF_SEGSIZE*sizeof(SEGMENT));
1511-
size+=maxBackends*/* XXX not multiple of BUCKET_ALLOC_INCR? */
1512-
(MAXALIGN(sizeof(BUCKET_INDEX))+
1513-
MAXALIGN(sizeof(XIDLookupEnt)));/* contains hash key */
1489+
/* lockHash table */
1490+
size+=hash_estimate_size(NLOCKENTS(maxBackends),
1491+
sizeof(LOCKTAG),
1492+
sizeof(LOCK));
1493+
1494+
/* xidHash table */
1495+
size+=hash_estimate_size(maxBackends,
1496+
XID_TAGSIZE,
1497+
sizeof(XIDLookupEnt));
15141498

15151499
returnsize;
15161500
}

‎src/backend/storage/smgr/mm.c

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/storage/smgr/Attic/mm.c,v 1.13 1999/02/13 23:18:36 momjian Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/storage/smgr/Attic/mm.c,v 1.14 1999/02/22 06:16:57 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -25,7 +25,6 @@
2525
#include"storage/shmem.h"
2626
#include"storage/spin.h"
2727

28-
#include"utils/dynahash.h"
2928
#include"utils/hsearch.h"
3029
#include"utils/rel.h"
3130
#include"utils/memutils.h"
@@ -565,36 +564,20 @@ int
565564
MMShmemSize()
566565
{
567566
intsize=0;
568-
intnbuckets;
569-
intnsegs;
570-
inttmp;
571567

572568
/*
573569
* first compute space occupied by the (dbid,relid,blkno) hash table
574570
*/
575-
576-
nbuckets=1 << (int)my_log2((MMNBUFFERS-1) /DEF_FFACTOR+1);
577-
nsegs=1 << (int)my_log2((nbuckets-1) /DEF_SEGSIZE+1);
578-
579-
size+=MAXALIGN(my_log2(MMNBUFFERS)*sizeof(void*));
580-
size+=MAXALIGN(sizeof(HHDR));
581-
size+=nsegs*MAXALIGN(DEF_SEGSIZE*sizeof(SEGMENT));
582-
tmp= (int)ceil((double)MMNBUFFERS /BUCKET_ALLOC_INCR);
583-
size+=tmp*BUCKET_ALLOC_INCR*
584-
(MAXALIGN(sizeof(BUCKET_INDEX))+
585-
MAXALIGN(sizeof(MMHashEntry)));/* contains hash key */
571+
size+=hash_estimate_size(MMNBUFFERS,
572+
0,/* MMHashEntry includes key */
573+
sizeof(MMHashEntry));
586574

587575
/*
588576
* now do the same for the rel hash table
589577
*/
590-
591-
size+=MAXALIGN(my_log2(MMNRELATIONS)*sizeof(void*));
592-
size+=MAXALIGN(sizeof(HHDR));
593-
size+=nsegs*MAXALIGN(DEF_SEGSIZE*sizeof(SEGMENT));
594-
tmp= (int)ceil((double)MMNRELATIONS /BUCKET_ALLOC_INCR);
595-
size+=tmp*BUCKET_ALLOC_INCR*
596-
(MAXALIGN(sizeof(BUCKET_INDEX))+
597-
MAXALIGN(sizeof(MMRelHashEntry)));/* contains hash key */
578+
size+=hash_estimate_size(MMNRELATIONS,
579+
0,/* MMRelHashEntry includes key */
580+
sizeof(MMRelHashEntry));
598581

599582
/*
600583
* finally, add in the memory block we use directly

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp