PostgreSQL Source Code  git master
dynahash.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * dynahash.c
4  * dynamic hash tables
5  *
6  * dynahash.c supports both local-to-a-backend hash tables and hash tables in
7  * shared memory. For shared hash tables, it is the caller's responsibility
8  * to provide appropriate access interlocking. The simplest convention is
9  * that a single LWLock protects the whole hash table. Searches (HASH_FIND or
10  * hash_seq_search) need only shared lock, but any update requires exclusive
11  * lock. For heavily-used shared tables, the single-lock approach creates a
12  * concurrency bottleneck, so we also support "partitioned" locking wherein
13  * there are multiple LWLocks guarding distinct subsets of the table. To use
14  * a hash table in partitioned mode, the HASH_PARTITION flag must be given
15  * to hash_create. This prevents any attempt to split buckets on-the-fly.
16  * Therefore, each hash bucket chain operates independently, and no fields
17  * of the hash header change after init except nentries and freeList.
18  * (A partitioned table uses multiple copies of those fields, guarded by
19  * spinlocks, for additional concurrency.)
20  * This lets any subset of the hash buckets be treated as a separately
21  * lockable partition. We expect callers to use the low-order bits of a
22  * lookup key's hash value as a partition number --- this will work because
23  * of the way calc_bucket() maps hash values to bucket numbers.
24  *
25  * For hash tables in shared memory, the memory allocator function should
26  * match malloc's semantics of returning NULL on failure. For hash tables
27  * in local memory, we typically use palloc() which will throw error on
28  * failure. The code in this file has to cope with both cases.
29  *
30  * dynahash.c provides support for these types of lookup keys:
31  *
32  * 1. Null-terminated C strings (truncated if necessary to fit in keysize),
33  * compared as though by strcmp(). This is the default behavior.
34  *
35  * 2. Arbitrary binary data of size keysize, compared as though by memcmp().
36  * (Caller must ensure there are no undefined padding bits in the keys!)
37  * This is selected by specifying HASH_BLOBS flag to hash_create.
38  *
39  * 3. More complex key behavior can be selected by specifying user-supplied
40  * hashing, comparison, and/or key-copying functions. At least a hashing
41  * function must be supplied; comparison defaults to memcmp() and key copying
42  * to memcpy() when a user-defined hashing function is selected.
43  *
44  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
45  * Portions Copyright (c) 1994, Regents of the University of California
46  *
47  *
48  * IDENTIFICATION
49  * src/backend/utils/hash/dynahash.c
50  *
51  *-------------------------------------------------------------------------
52  */
53 
54 /*
55  * Original comments:
56  *
57  * Dynamic hashing, after CACM April 1988 pp 446-457, by Per-Ake Larson.
58  * Coded into C, with minor code improvements, and with hsearch(3) interface,
59  * by ejp@ausmelb.oz, Jul 26, 1988: 13:16;
60  * also, hcreate/hdestroy routines added to simulate hsearch(3).
61  *
62  * These routines simulate hsearch(3) and family, with the important
63  * difference that the hash table is dynamic - can grow indefinitely
64  * beyond its original size (as supplied to hcreate()).
65  *
66  * Performance appears to be comparable to that of hsearch(3).
67  * The 'source-code' options referred to in hsearch(3)'s 'man' page
68  * are not implemented; otherwise functionality is identical.
69  *
70  * Compilation controls:
71  * HASH_DEBUG controls some informative traces, mainly for debugging.
72  * HASH_STATISTICS causes HashAccesses and HashCollisions to be maintained;
73  * when combined with HASH_DEBUG, these are displayed by hdestroy().
74  *
75  * Problems & fixes to ejp@ausmelb.oz. WARNING: relies on pre-processor
76  * concatenation property, in probably unnecessary code 'optimization'.
77  *
78  * Modified margo@postgres.berkeley.edu February 1990
79  * added multiple table interface
80  * Modified by sullivan@postgres.berkeley.edu April 1990
81  * changed ctl structure for shared memory
82  */
83 
84 #include "postgres.h"
85 
86 #include <limits.h>
87 
88 #include "access/xact.h"
89 #include "storage/shmem.h"
90 #include "storage/spin.h"
91 #include "utils/dynahash.h"
92 #include "utils/memutils.h"
93 
94 
95 /*
96  * Constants
97  *
98  * A hash table has a top-level "directory", each of whose entries points
99  * to a "segment" of ssize bucket headers. The maximum number of hash
100  * buckets is thus dsize * ssize (but dsize may be expansible). Of course,
101  * the number of records in the table can be larger, but we don't want a
102  * whole lot of records per bucket or performance goes down.
103  *
104  * In a hash table allocated in shared memory, the directory cannot be
105  * expanded because it must stay at a fixed address. The directory size
106  * should be selected using hash_select_dirsize (and you'd better have
107  * a good idea of the maximum number of entries!). For non-shared hash
108  * tables, the initial directory size can be left at the default.
109  */
110 #define DEF_SEGSIZE 256
111 #define DEF_SEGSIZE_SHIFT 8 /* must be log2(DEF_SEGSIZE) */
112 #define DEF_DIRSIZE 256
113 #define DEF_FFACTOR 1 /* default fill factor */
114 
115 /* Number of freelists to be used for a partitioned hash table. */
116 #define NUM_FREELISTS 32
117 
118 /* A hash bucket is a linked list of HASHELEMENTs */
120 
121 /* A hash segment is an array of bucket headers */
123 
124 /*
125  * Per-freelist data.
126  *
127  * In a partitioned hash table, each freelist is associated with a specific
128  * set of hashcodes, as determined by the FREELIST_IDX() macro below.
129  * nentries tracks the number of live hashtable entries having those hashcodes
130  * (NOT the number of entries in the freelist, as you might expect).
131  *
132  * The coverage of a freelist might be more or less than one partition, so it
133  * needs its own lock rather than relying on caller locking. Relying on that
134  * wouldn't work even if the coverage was the same, because of the occasional
135  * need to "borrow" entries from another freelist; see get_hash_entry().
136  *
137  * Using an array of FreeListData instead of separate arrays of mutexes,
138  * nentries and freeLists helps to reduce sharing of cache lines between
139  * different mutexes.
140  */
141 typedef struct
142 {
143  slock_t mutex; /* spinlock for this freelist */
144  long nentries; /* number of entries in associated buckets */
145  HASHELEMENT *freeList; /* chain of free elements */
146 } FreeListData;
147 
148 /*
149  * Header structure for a hash table --- contains all changeable info
150  *
151  * In a shared-memory hash table, the HASHHDR is in shared memory, while
152  * each backend has a local HTAB struct. For a non-shared table, there isn't
153  * any functional difference between HASHHDR and HTAB, but we separate them
154  * anyway to share code between shared and non-shared tables.
155  */
156 struct HASHHDR
157 {
158  /*
159  * The freelist can become a point of contention in high-concurrency hash
160  * tables, so we use an array of freelists, each with its own mutex and
161  * nentries count, instead of just a single one. Although the freelists
162  * normally operate independently, we will scavenge entries from freelists
163  * other than a hashcode's default freelist when necessary.
164  *
165  * If the hash table is not partitioned, only freeList[0] is used and its
166  * spinlock is not used at all; callers' locking is assumed sufficient.
167  */
169 
170  /* These fields can change, but not in a partitioned table */
171  /* Also, dsize can't change in a shared table, even if unpartitioned */
172  long dsize; /* directory size */
173  long nsegs; /* number of allocated segments (<= dsize) */
174  uint32 max_bucket; /* ID of maximum bucket in use */
175  uint32 high_mask; /* mask to modulo into entire table */
176  uint32 low_mask; /* mask to modulo into lower half of table */
177 
178  /* These fields are fixed at hashtable creation */
179  Size keysize; /* hash key length in bytes */
180  Size entrysize; /* total user element size in bytes */
181  long num_partitions; /* # partitions (must be power of 2), or 0 */
182  long ffactor; /* target fill factor */
183  long max_dsize; /* 'dsize' limit if directory is fixed size */
184  long ssize; /* segment size --- must be power of 2 */
185  int sshift; /* segment shift = log2(ssize) */
186  int nelem_alloc; /* number of entries to allocate at once */
187 
188 #ifdef HASH_STATISTICS
189 
190  /*
191  * Count statistics here. NB: stats code doesn't bother with mutex, so
192  * counts could be corrupted a bit in a partitioned table.
193  */
194  long accesses;
195  long collisions;
196 #endif
197 };
198 
199 #define IS_PARTITIONED(hctl) ((hctl)->num_partitions != 0)
200 
201 #define FREELIST_IDX(hctl, hashcode) \
202  (IS_PARTITIONED(hctl) ? (hashcode) % NUM_FREELISTS : 0)
203 
204 /*
205  * Top control structure for a hashtable --- in a shared table, each backend
206  * has its own copy (OK since no fields change at runtime)
207  */
208 struct HTAB
209 {
210  HASHHDR *hctl; /* => shared control information */
211  HASHSEGMENT *dir; /* directory of segment starts */
212  HashValueFunc hash; /* hash function */
213  HashCompareFunc match; /* key comparison function */
214  HashCopyFunc keycopy; /* key copying function */
215  HashAllocFunc alloc; /* memory allocator */
216  MemoryContext hcxt; /* memory context if default allocator used */
217  char *tabname; /* table name (for error messages) */
218  bool isshared; /* true if table is in shared memory */
219  bool isfixed; /* if true, don't enlarge */
220 
221  /* freezing a shared table isn't allowed, so we can keep state here */
222  bool frozen; /* true = no more inserts allowed */
223 
224  /* We keep local copies of these fixed values to reduce contention */
225  Size keysize; /* hash key length in bytes */
226  long ssize; /* segment size --- must be power of 2 */
227  int sshift; /* segment shift = log2(ssize) */
228 };
229 
230 /*
231  * Key (also entry) part of a HASHELEMENT
232  */
233 #define ELEMENTKEY(helem) (((char *)(helem)) + MAXALIGN(sizeof(HASHELEMENT)))
234 
235 /*
236  * Obtain element pointer given pointer to key
237  */
238 #define ELEMENT_FROM_KEY(key) \
239  ((HASHELEMENT *) (((char *) (key)) - MAXALIGN(sizeof(HASHELEMENT))))
240 
241 /*
242  * Fast MOD arithmetic, assuming that y is a power of 2 !
243  */
244 #define MOD(x,y) ((x) & ((y)-1))
245 
246 #if HASH_STATISTICS
247 static long hash_accesses,
248  hash_collisions,
249  hash_expansions;
250 #endif
251 
252 /*
253  * Private function prototypes
254  */
255 static void *DynaHashAlloc(Size size);
256 static HASHSEGMENT seg_alloc(HTAB *hashp);
257 static bool element_alloc(HTAB *hashp, int nelem, int freelist_idx);
258 static bool dir_realloc(HTAB *hashp);
259 static bool expand_table(HTAB *hashp);
260 static HASHBUCKET get_hash_entry(HTAB *hashp, int freelist_idx);
261 static void hdefault(HTAB *hashp);
262 static int choose_nelem_alloc(Size entrysize);
263 static bool init_htab(HTAB *hashp, long nelem);
264 static void hash_corrupted(HTAB *hashp);
265 static long next_pow2_long(long num);
266 static int next_pow2_int(long num);
267 static void register_seq_scan(HTAB *hashp);
268 static void deregister_seq_scan(HTAB *hashp);
269 static bool has_seq_scans(HTAB *hashp);
270 
271 
272 /*
273  * memory allocation support
274  */
276 
277 static void *
279 {
280  Assert(MemoryContextIsValid(CurrentDynaHashCxt));
281  return MemoryContextAlloc(CurrentDynaHashCxt, size);
282 }
283 
284 
285 /*
286  * HashCompareFunc for string keys
287  *
288  * Because we copy keys with strlcpy(), they will be truncated at keysize-1
289  * bytes, so we can only compare that many ... hence strncmp is almost but
290  * not quite the right thing.
291  */
292 static int
293 string_compare(const char *key1, const char *key2, Size keysize)
294 {
295  return strncmp(key1, key2, keysize - 1);
296 }
297 
298 
299 /************************** CREATE ROUTINES **********************/
300 
301 /*
302  * hash_create -- create a new dynamic hash table
303  *
304  * tabname: a name for the table (for debugging purposes)
305  * nelem: maximum number of elements expected
306  * *info: additional table parameters, as indicated by flags
307  * flags: bitmask indicating which parameters to take from *info
308  *
309  * Note: for a shared-memory hashtable, nelem needs to be a pretty good
310  * estimate, since we can't expand the table on the fly. But an unshared
311  * hashtable can be expanded on-the-fly, so it's better for nelem to be
312  * on the small side and let the table grow if it's exceeded. An overly
313  * large nelem will penalize hash_seq_search speed without buying much.
314  */
315 HTAB *
316 hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
317 {
318  HTAB *hashp;
319  HASHHDR *hctl;
320 
321  /*
322  * For shared hash tables, we have a local hash header (HTAB struct) that
323  * we allocate in TopMemoryContext; all else is in shared memory.
324  *
325  * For non-shared hash tables, everything including the hash header is in
326  * a memory context created specially for the hash table --- this makes
327  * hash_destroy very simple. The memory context is made a child of either
328  * a context specified by the caller, or TopMemoryContext if nothing is
329  * specified.
330  */
331  if (flags & HASH_SHARED_MEM)
332  {
333  /* Set up to allocate the hash header */
334  CurrentDynaHashCxt = TopMemoryContext;
335  }
336  else
337  {
338  /* Create the hash table's private memory context */
339  if (flags & HASH_CONTEXT)
340  CurrentDynaHashCxt = info->hcxt;
341  else
342  CurrentDynaHashCxt = TopMemoryContext;
343  CurrentDynaHashCxt =
344  AllocSetContextCreateExtended(CurrentDynaHashCxt,
345  tabname,
348  }
349 
350  /* Initialize the hash header, plus a copy of the table name */
351  hashp = (HTAB *) DynaHashAlloc(sizeof(HTAB) + strlen(tabname) + 1);
352  MemSet(hashp, 0, sizeof(HTAB));
353 
354  hashp->tabname = (char *) (hashp + 1);
355  strcpy(hashp->tabname, tabname);
356 
357  /*
358  * Select the appropriate hash function (see comments at head of file).
359  */
360  if (flags & HASH_FUNCTION)
361  hashp->hash = info->hash;
362  else if (flags & HASH_BLOBS)
363  {
364  /* We can optimize hashing for common key sizes */
365  Assert(flags & HASH_ELEM);
366  if (info->keysize == sizeof(uint32))
367  hashp->hash = uint32_hash;
368  else
369  hashp->hash = tag_hash;
370  }
371  else
372  hashp->hash = string_hash; /* default hash function */
373 
374  /*
375  * If you don't specify a match function, it defaults to string_compare if
376  * you used string_hash (either explicitly or by default) and to memcmp
377  * otherwise.
378  *
379  * Note: explicitly specifying string_hash is deprecated, because this
380  * might not work for callers in loadable modules on some platforms due to
381  * referencing a trampoline instead of the string_hash function proper.
382  * Just let it default, eh?
383  */
384  if (flags & HASH_COMPARE)
385  hashp->match = info->match;
386  else if (hashp->hash == string_hash)
388  else
389  hashp->match = memcmp;
390 
391  /*
392  * Similarly, the key-copying function defaults to strlcpy or memcpy.
393  */
394  if (flags & HASH_KEYCOPY)
395  hashp->keycopy = info->keycopy;
396  else if (hashp->hash == string_hash)
397  hashp->keycopy = (HashCopyFunc) strlcpy;
398  else
399  hashp->keycopy = memcpy;
400 
401  /* And select the entry allocation function, too. */
402  if (flags & HASH_ALLOC)
403  hashp->alloc = info->alloc;
404  else
405  hashp->alloc = DynaHashAlloc;
406 
407  if (flags & HASH_SHARED_MEM)
408  {
409  /*
410  * ctl structure and directory are preallocated for shared memory
411  * tables. Note that HASH_DIRSIZE and HASH_ALLOC had better be set as
412  * well.
413  */
414  hashp->hctl = info->hctl;
415  hashp->dir = (HASHSEGMENT *) (((char *) info->hctl) + sizeof(HASHHDR));
416  hashp->hcxt = NULL;
417  hashp->isshared = true;
418 
419  /* hash table already exists, we're just attaching to it */
420  if (flags & HASH_ATTACH)
421  {
422  /* make local copies of some heavily-used values */
423  hctl = hashp->hctl;
424  hashp->keysize = hctl->keysize;
425  hashp->ssize = hctl->ssize;
426  hashp->sshift = hctl->sshift;
427 
428  return hashp;
429  }
430  }
431  else
432  {
433  /* setup hash table defaults */
434  hashp->hctl = NULL;
435  hashp->dir = NULL;
436  hashp->hcxt = CurrentDynaHashCxt;
437  hashp->isshared = false;
438  }
439 
440  if (!hashp->hctl)
441  {
442  hashp->hctl = (HASHHDR *) hashp->alloc(sizeof(HASHHDR));
443  if (!hashp->hctl)
444  ereport(ERROR,
445  (errcode(ERRCODE_OUT_OF_MEMORY),
446  errmsg("out of memory")));
447  }
448 
449  hashp->frozen = false;
450 
451  hdefault(hashp);
452 
453  hctl = hashp->hctl;
454 
455  if (flags & HASH_PARTITION)
456  {
457  /* Doesn't make sense to partition a local hash table */
458  Assert(flags & HASH_SHARED_MEM);
459 
460  /*
461  * The number of partitions had better be a power of 2. Also, it must
462  * be less than INT_MAX (see init_htab()), so call the int version of
463  * next_pow2.
464  */
466 
467  hctl->num_partitions = info->num_partitions;
468  }
469 
470  if (flags & HASH_SEGMENT)
471  {
472  hctl->ssize = info->ssize;
473  hctl->sshift = my_log2(info->ssize);
474  /* ssize had better be a power of 2 */
475  Assert(hctl->ssize == (1L << hctl->sshift));
476  }
477  if (flags & HASH_FFACTOR)
478  hctl->ffactor = info->ffactor;
479 
480  /*
481  * SHM hash tables have fixed directory size passed by the caller.
482  */
483  if (flags & HASH_DIRSIZE)
484  {
485  hctl->max_dsize = info->max_dsize;
486  hctl->dsize = info->dsize;
487  }
488 
489  /*
490  * hash table now allocates space for key and data but you have to say how
491  * much space to allocate
492  */
493  if (flags & HASH_ELEM)
494  {
495  Assert(info->entrysize >= info->keysize);
496  hctl->keysize = info->keysize;
497  hctl->entrysize = info->entrysize;
498  }
499 
500  /* make local copies of heavily-used constant fields */
501  hashp->keysize = hctl->keysize;
502  hashp->ssize = hctl->ssize;
503  hashp->sshift = hctl->sshift;
504 
505  /* Build the hash directory structure */
506  if (!init_htab(hashp, nelem))
507  elog(ERROR, "failed to initialize hash table \"%s\"", hashp->tabname);
508 
509  /*
510  * For a shared hash table, preallocate the requested number of elements.
511  * This reduces problems with run-time out-of-shared-memory conditions.
512  *
513  * For a non-shared hash table, preallocate the requested number of
514  * elements if it's less than our chosen nelem_alloc. This avoids wasting
515  * space if the caller correctly estimates a small table size.
516  */
517  if ((flags & HASH_SHARED_MEM) ||
518  nelem < hctl->nelem_alloc)
519  {
520  int i,
521  freelist_partitions,
522  nelem_alloc,
523  nelem_alloc_first;
524 
525  /*
526  * If hash table is partitioned, give each freelist an equal share of
527  * the initial allocation. Otherwise only freeList[0] is used.
528  */
529  if (IS_PARTITIONED(hashp->hctl))
530  freelist_partitions = NUM_FREELISTS;
531  else
532  freelist_partitions = 1;
533 
534  nelem_alloc = nelem / freelist_partitions;
535  if (nelem_alloc <= 0)
536  nelem_alloc = 1;
537 
538  /*
539  * Make sure we'll allocate all the requested elements; freeList[0]
540  * gets the excess if the request isn't divisible by NUM_FREELISTS.
541  */
542  if (nelem_alloc * freelist_partitions < nelem)
543  nelem_alloc_first =
544  nelem - nelem_alloc * (freelist_partitions - 1);
545  else
546  nelem_alloc_first = nelem_alloc;
547 
548  for (i = 0; i < freelist_partitions; i++)
549  {
550  int temp = (i == 0) ? nelem_alloc_first : nelem_alloc;
551 
552  if (!element_alloc(hashp, temp, i))
553  ereport(ERROR,
554  (errcode(ERRCODE_OUT_OF_MEMORY),
555  errmsg("out of memory")));
556  }
557  }
558 
559  if (flags & HASH_FIXED_SIZE)
560  hashp->isfixed = true;
561  return hashp;
562 }
563 
564 /*
565  * Set default HASHHDR parameters.
566  */
567 static void
568 hdefault(HTAB *hashp)
569 {
570  HASHHDR *hctl = hashp->hctl;
571 
572  MemSet(hctl, 0, sizeof(HASHHDR));
573 
574  hctl->dsize = DEF_DIRSIZE;
575  hctl->nsegs = 0;
576 
577  /* rather pointless defaults for key & entry size */
578  hctl->keysize = sizeof(char *);
579  hctl->entrysize = 2 * sizeof(char *);
580 
581  hctl->num_partitions = 0; /* not partitioned */
582 
583  hctl->ffactor = DEF_FFACTOR;
584 
585  /* table has no fixed maximum size */
586  hctl->max_dsize = NO_MAX_DSIZE;
587 
588  hctl->ssize = DEF_SEGSIZE;
589  hctl->sshift = DEF_SEGSIZE_SHIFT;
590 
591 #ifdef HASH_STATISTICS
592  hctl->accesses = hctl->collisions = 0;
593 #endif
594 }
595 
596 /*
597  * Given the user-specified entry size, choose nelem_alloc, ie, how many
598  * elements to add to the hash table when we need more.
599  */
600 static int
602 {
603  int nelem_alloc;
604  Size elementSize;
605  Size allocSize;
606 
607  /* Each element has a HASHELEMENT header plus user data. */
608  /* NB: this had better match element_alloc() */
609  elementSize = MAXALIGN(sizeof(HASHELEMENT)) + MAXALIGN(entrysize);
610 
611  /*
612  * The idea here is to choose nelem_alloc at least 32, but round up so
613  * that the allocation request will be a power of 2 or just less. This
614  * makes little difference for hash tables in shared memory, but for hash
615  * tables managed by palloc, the allocation request will be rounded up to
616  * a power of 2 anyway. If we fail to take this into account, we'll waste
617  * as much as half the allocated space.
618  */
619  allocSize = 32 * 4; /* assume elementSize at least 8 */
620  do
621  {
622  allocSize <<= 1;
623  nelem_alloc = allocSize / elementSize;
624  } while (nelem_alloc < 32);
625 
626  return nelem_alloc;
627 }
628 
629 /*
630  * Compute derived fields of hctl and build the initial directory/segment
631  * arrays
632  */
633 static bool
634 init_htab(HTAB *hashp, long nelem)
635 {
636  HASHHDR *hctl = hashp->hctl;
637  HASHSEGMENT *segp;
638  int nbuckets;
639  int nsegs;
640  int i;
641 
642  /*
643  * initialize mutexes if it's a partitioned table
644  */
645  if (IS_PARTITIONED(hctl))
646  for (i = 0; i < NUM_FREELISTS; i++)
647  SpinLockInit(&(hctl->freeList[i].mutex));
648 
649  /*
650  * Divide number of elements by the fill factor to determine a desired
651  * number of buckets. Allocate space for the next greater power of two
652  * number of buckets
653  */
654  nbuckets = next_pow2_int((nelem - 1) / hctl->ffactor + 1);
655 
656  /*
657  * In a partitioned table, nbuckets must be at least equal to
658  * num_partitions; were it less, keys with apparently different partition
659  * numbers would map to the same bucket, breaking partition independence.
660  * (Normally nbuckets will be much bigger; this is just a safety check.)
661  */
662  while (nbuckets < hctl->num_partitions)
663  nbuckets <<= 1;
664 
665  hctl->max_bucket = hctl->low_mask = nbuckets - 1;
666  hctl->high_mask = (nbuckets << 1) - 1;
667 
668  /*
669  * Figure number of directory segments needed, round up to a power of 2
670  */
671  nsegs = (nbuckets - 1) / hctl->ssize + 1;
672  nsegs = next_pow2_int(nsegs);
673 
674  /*
675  * Make sure directory is big enough. If pre-allocated directory is too
676  * small, choke (caller screwed up).
677  */
678  if (nsegs > hctl->dsize)
679  {
680  if (!(hashp->dir))
681  hctl->dsize = nsegs;
682  else
683  return false;
684  }
685 
686  /* Allocate a directory */
687  if (!(hashp->dir))
688  {
689  CurrentDynaHashCxt = hashp->hcxt;
690  hashp->dir = (HASHSEGMENT *)
691  hashp->alloc(hctl->dsize * sizeof(HASHSEGMENT));
692  if (!hashp->dir)
693  return false;
694  }
695 
696  /* Allocate initial segments */
697  for (segp = hashp->dir; hctl->nsegs < nsegs; hctl->nsegs++, segp++)
698  {
699  *segp = seg_alloc(hashp);
700  if (*segp == NULL)
701  return false;
702  }
703 
704  /* Choose number of entries to allocate at a time */
706 
707 #if HASH_DEBUG
708  fprintf(stderr, "init_htab:\n%s%p\n%s%ld\n%s%ld\n%s%d\n%s%ld\n%s%u\n%s%x\n%s%x\n%s%ld\n",
709  "TABLE POINTER ", hashp,
710  "DIRECTORY SIZE ", hctl->dsize,
711  "SEGMENT SIZE ", hctl->ssize,
712  "SEGMENT SHIFT ", hctl->sshift,
713  "FILL FACTOR ", hctl->ffactor,
714  "MAX BUCKET ", hctl->max_bucket,
715  "HIGH MASK ", hctl->high_mask,
716  "LOW MASK ", hctl->low_mask,
717  "NSEGS ", hctl->nsegs);
718 #endif
719  return true;
720 }
721 
722 /*
723  * Estimate the space needed for a hashtable containing the given number
724  * of entries of given size.
725  * NOTE: this is used to estimate the footprint of hashtables in shared
726  * memory; therefore it does not count HTAB which is in local memory.
727  * NB: assumes that all hash structure parameters have default values!
728  */
729 Size
731 {
732  Size size;
733  long nBuckets,
734  nSegments,
735  nDirEntries,
736  nElementAllocs,
737  elementSize,
738  elementAllocCnt;
739 
740  /* estimate number of buckets wanted */
741  nBuckets = next_pow2_long((num_entries - 1) / DEF_FFACTOR + 1);
742  /* # of segments needed for nBuckets */
743  nSegments = next_pow2_long((nBuckets - 1) / DEF_SEGSIZE + 1);
744  /* directory entries */
745  nDirEntries = DEF_DIRSIZE;
746  while (nDirEntries < nSegments)
747  nDirEntries <<= 1; /* dir_alloc doubles dsize at each call */
748 
749  /* fixed control info */
750  size = MAXALIGN(sizeof(HASHHDR)); /* but not HTAB, per above */
751  /* directory */
752  size = add_size(size, mul_size(nDirEntries, sizeof(HASHSEGMENT)));
753  /* segments */
754  size = add_size(size, mul_size(nSegments,
755  MAXALIGN(DEF_SEGSIZE * sizeof(HASHBUCKET))));
756  /* elements --- allocated in groups of choose_nelem_alloc() entries */
757  elementAllocCnt = choose_nelem_alloc(entrysize);
758  nElementAllocs = (num_entries - 1) / elementAllocCnt + 1;
759  elementSize = MAXALIGN(sizeof(HASHELEMENT)) + MAXALIGN(entrysize);
760  size = add_size(size,
761  mul_size(nElementAllocs,
762  mul_size(elementAllocCnt, elementSize)));
763 
764  return size;
765 }
766 
767 /*
768  * Select an appropriate directory size for a hashtable with the given
769  * maximum number of entries.
770  * This is only needed for hashtables in shared memory, whose directories
771  * cannot be expanded dynamically.
772  * NB: assumes that all hash structure parameters have default values!
773  *
774  * XXX this had better agree with the behavior of init_htab()...
775  */
776 long
777 hash_select_dirsize(long num_entries)
778 {
779  long nBuckets,
780  nSegments,
781  nDirEntries;
782 
783  /* estimate number of buckets wanted */
784  nBuckets = next_pow2_long((num_entries - 1) / DEF_FFACTOR + 1);
785  /* # of segments needed for nBuckets */
786  nSegments = next_pow2_long((nBuckets - 1) / DEF_SEGSIZE + 1);
787  /* directory entries */
788  nDirEntries = DEF_DIRSIZE;
789  while (nDirEntries < nSegments)
790  nDirEntries <<= 1; /* dir_alloc doubles dsize at each call */
791 
792  return nDirEntries;
793 }
794 
795 /*
796  * Compute the required initial memory allocation for a shared-memory
797  * hashtable with the given parameters. We need space for the HASHHDR
798  * and for the (non expansible) directory.
799  */
800 Size
801 hash_get_shared_size(HASHCTL *info, int flags)
802 {
803  Assert(flags & HASH_DIRSIZE);
804  Assert(info->dsize == info->max_dsize);
805  return sizeof(HASHHDR) + info->dsize * sizeof(HASHSEGMENT);
806 }
807 
808 
809 /********************** DESTROY ROUTINES ************************/
810 
811 void
813 {
814  if (hashp != NULL)
815  {
816  /* allocation method must be one we know how to free, too */
817  Assert(hashp->alloc == DynaHashAlloc);
818  /* so this hashtable must have it's own context */
819  Assert(hashp->hcxt != NULL);
820 
821  hash_stats("destroy", hashp);
822 
823  /*
824  * Free everything by destroying the hash table's memory context.
825  */
826  MemoryContextDelete(hashp->hcxt);
827  }
828 }
829 
830 void
831 hash_stats(const char *where, HTAB *hashp)
832 {
833 #if HASH_STATISTICS
834  fprintf(stderr, "%s: this HTAB -- accesses %ld collisions %ld\n",
835  where, hashp->hctl->accesses, hashp->hctl->collisions);
836 
837  fprintf(stderr, "hash_stats: entries %ld keysize %ld maxp %u segmentcount %ld\n",
838  hash_get_num_entries(hashp), (long) hashp->hctl->keysize,
839  hashp->hctl->max_bucket, hashp->hctl->nsegs);
840  fprintf(stderr, "%s: total accesses %ld total collisions %ld\n",
841  where, hash_accesses, hash_collisions);
842  fprintf(stderr, "hash_stats: total expansions %ld\n",
843  hash_expansions);
844 #endif
845 }
846 
847 /*******************************SEARCH ROUTINES *****************************/
848 
849 
850 /*
851  * get_hash_value -- exported routine to calculate a key's hash value
852  *
853  * We export this because for partitioned tables, callers need to compute
854  * the partition number (from the low-order bits of the hash value) before
855  * searching.
856  */
857 uint32
858 get_hash_value(HTAB *hashp, const void *keyPtr)
859 {
860  return hashp->hash(keyPtr, hashp->keysize);
861 }
862 
863 /* Convert a hash value to a bucket number */
864 static inline uint32
865 calc_bucket(HASHHDR *hctl, uint32 hash_val)
866 {
867  uint32 bucket;
868 
869  bucket = hash_val & hctl->high_mask;
870  if (bucket > hctl->max_bucket)
871  bucket = bucket & hctl->low_mask;
872 
873  return bucket;
874 }
875 
876 /*
877  * hash_search -- look up key in table and perform action
878  * hash_search_with_hash_value -- same, with key's hash value already computed
879  *
880  * action is one of:
881  * HASH_FIND: look up key in table
882  * HASH_ENTER: look up key in table, creating entry if not present
883  * HASH_ENTER_NULL: same, but return NULL if out of memory
884  * HASH_REMOVE: look up key in table, remove entry if present
885  *
886  * Return value is a pointer to the element found/entered/removed if any,
887  * or NULL if no match was found. (NB: in the case of the REMOVE action,
888  * the result is a dangling pointer that shouldn't be dereferenced!)
889  *
890  * HASH_ENTER will normally ereport a generic "out of memory" error if
891  * it is unable to create a new entry. The HASH_ENTER_NULL operation is
892  * the same except it will return NULL if out of memory. Note that
893  * HASH_ENTER_NULL cannot be used with the default palloc-based allocator,
894  * since palloc internally ereports on out-of-memory.
895  *
896  * If foundPtr isn't NULL, then *foundPtr is set true if we found an
897  * existing entry in the table, false otherwise. This is needed in the
898  * HASH_ENTER case, but is redundant with the return value otherwise.
899  *
900  * For hash_search_with_hash_value, the hashvalue parameter must have been
901  * calculated with get_hash_value().
902  */
903 void *
905  const void *keyPtr,
907  bool *foundPtr)
908 {
909  return hash_search_with_hash_value(hashp,
910  keyPtr,
911  hashp->hash(keyPtr, hashp->keysize),
912  action,
913  foundPtr);
914 }
915 
916 void *
918  const void *keyPtr,
919  uint32 hashvalue,
921  bool *foundPtr)
922 {
923  HASHHDR *hctl = hashp->hctl;
924  int freelist_idx = FREELIST_IDX(hctl, hashvalue);
925  Size keysize;
926  uint32 bucket;
927  long segment_num;
928  long segment_ndx;
929  HASHSEGMENT segp;
930  HASHBUCKET currBucket;
931  HASHBUCKET *prevBucketPtr;
932  HashCompareFunc match;
933 
934 #if HASH_STATISTICS
935  hash_accesses++;
936  hctl->accesses++;
937 #endif
938 
939  /*
940  * If inserting, check if it is time to split a bucket.
941  *
942  * NOTE: failure to expand table is not a fatal error, it just means we
943  * have to run at higher fill factor than we wanted. However, if we're
944  * using the palloc allocator then it will throw error anyway on
945  * out-of-memory, so we must do this before modifying the table.
946  */
947  if (action == HASH_ENTER || action == HASH_ENTER_NULL)
948  {
949  /*
950  * Can't split if running in partitioned mode, nor if frozen, nor if
951  * table is the subject of any active hash_seq_search scans. Strange
952  * order of these tests is to try to check cheaper conditions first.
953  */
954  if (!IS_PARTITIONED(hctl) && !hashp->frozen &&
955  hctl->freeList[0].nentries / (long) (hctl->max_bucket + 1) >= hctl->ffactor &&
956  !has_seq_scans(hashp))
957  (void) expand_table(hashp);
958  }
959 
960  /*
961  * Do the initial lookup
962  */
963  bucket = calc_bucket(hctl, hashvalue);
964 
965  segment_num = bucket >> hashp->sshift;
966  segment_ndx = MOD(bucket, hashp->ssize);
967 
968  segp = hashp->dir[segment_num];
969 
970  if (segp == NULL)
971  hash_corrupted(hashp);
972 
973  prevBucketPtr = &segp[segment_ndx];
974  currBucket = *prevBucketPtr;
975 
976  /*
977  * Follow collision chain looking for matching key
978  */
979  match = hashp->match; /* save one fetch in inner loop */
980  keysize = hashp->keysize; /* ditto */
981 
982  while (currBucket != NULL)
983  {
984  if (currBucket->hashvalue == hashvalue &&
985  match(ELEMENTKEY(currBucket), keyPtr, keysize) == 0)
986  break;
987  prevBucketPtr = &(currBucket->link);
988  currBucket = *prevBucketPtr;
989 #if HASH_STATISTICS
990  hash_collisions++;
991  hctl->collisions++;
992 #endif
993  }
994 
995  if (foundPtr)
996  *foundPtr = (bool) (currBucket != NULL);
997 
998  /*
999  * OK, now what?
1000  */
1001  switch (action)
1002  {
1003  case HASH_FIND:
1004  if (currBucket != NULL)
1005  return (void *) ELEMENTKEY(currBucket);
1006  return NULL;
1007 
1008  case HASH_REMOVE:
1009  if (currBucket != NULL)
1010  {
1011  /* if partitioned, must lock to touch nentries and freeList */
1012  if (IS_PARTITIONED(hctl))
1013  SpinLockAcquire(&(hctl->freeList[freelist_idx].mutex));
1014 
1015  /* delete the record from the appropriate nentries counter. */
1016  Assert(hctl->freeList[freelist_idx].nentries > 0);
1017  hctl->freeList[freelist_idx].nentries--;
1018 
1019  /* remove record from hash bucket's chain. */
1020  *prevBucketPtr = currBucket->link;
1021 
1022  /* add the record to the appropriate freelist. */
1023  currBucket->link = hctl->freeList[freelist_idx].freeList;
1024  hctl->freeList[freelist_idx].freeList = currBucket;
1025 
1026  if (IS_PARTITIONED(hctl))
1027  SpinLockRelease(&hctl->freeList[freelist_idx].mutex);
1028 
1029  /*
1030  * better hope the caller is synchronizing access to this
1031  * element, because someone else is going to reuse it the next
1032  * time something is added to the table
1033  */
1034  return (void *) ELEMENTKEY(currBucket);
1035  }
1036  return NULL;
1037 
1038  case HASH_ENTER_NULL:
1039  /* ENTER_NULL does not work with palloc-based allocator */
1040  Assert(hashp->alloc != DynaHashAlloc);
1041  /* FALL THRU */
1042 
1043  case HASH_ENTER:
1044  /* Return existing element if found, else create one */
1045  if (currBucket != NULL)
1046  return (void *) ELEMENTKEY(currBucket);
1047 
1048  /* disallow inserts if frozen */
1049  if (hashp->frozen)
1050  elog(ERROR, "cannot insert into frozen hashtable \"%s\"",
1051  hashp->tabname);
1052 
1053  currBucket = get_hash_entry(hashp, freelist_idx);
1054  if (currBucket == NULL)
1055  {
1056  /* out of memory */
1057  if (action == HASH_ENTER_NULL)
1058  return NULL;
1059  /* report a generic message */
1060  if (hashp->isshared)
1061  ereport(ERROR,
1062  (errcode(ERRCODE_OUT_OF_MEMORY),
1063  errmsg("out of shared memory")));
1064  else
1065  ereport(ERROR,
1066  (errcode(ERRCODE_OUT_OF_MEMORY),
1067  errmsg("out of memory")));
1068  }
1069 
1070  /* link into hashbucket chain */
1071  *prevBucketPtr = currBucket;
1072  currBucket->link = NULL;
1073 
1074  /* copy key into record */
1075  currBucket->hashvalue = hashvalue;
1076  hashp->keycopy(ELEMENTKEY(currBucket), keyPtr, keysize);
1077 
1078  /*
1079  * Caller is expected to fill the data field on return. DO NOT
1080  * insert any code that could possibly throw error here, as doing
1081  * so would leave the table entry incomplete and hence corrupt the
1082  * caller's data structure.
1083  */
1084 
1085  return (void *) ELEMENTKEY(currBucket);
1086  }
1087 
1088  elog(ERROR, "unrecognized hash action code: %d", (int) action);
1089 
1090  return NULL; /* keep compiler quiet */
1091 }
1092 
1093 /*
1094  * hash_update_hash_key -- change the hash key of an existing table entry
1095  *
1096  * This is equivalent to removing the entry, making a new entry, and copying
1097  * over its data, except that the entry never goes to the table's freelist.
1098  * Therefore this cannot suffer an out-of-memory failure, even if there are
1099  * other processes operating in other partitions of the hashtable.
1100  *
1101  * Returns true if successful, false if the requested new hash key is already
1102  * present. Throws error if the specified entry pointer isn't actually a
1103  * table member.
1104  *
1105  * NB: currently, there is no special case for old and new hash keys being
1106  * identical, which means we'll report false for that situation. This is
1107  * preferable for existing uses.
1108  *
1109  * NB: for a partitioned hashtable, caller must hold lock on both relevant
1110  * partitions, if the new hash key would belong to a different partition.
1111  */
1112 bool
1114  void *existingEntry,
1115  const void *newKeyPtr)
1116 {
1117  HASHELEMENT *existingElement = ELEMENT_FROM_KEY(existingEntry);
1118  HASHHDR *hctl = hashp->hctl;
1119  uint32 newhashvalue;
1120  Size keysize;
1121  uint32 bucket;
1122  uint32 newbucket;
1123  long segment_num;
1124  long segment_ndx;
1125  HASHSEGMENT segp;
1126  HASHBUCKET currBucket;
1127  HASHBUCKET *prevBucketPtr;
1128  HASHBUCKET *oldPrevPtr;
1129  HashCompareFunc match;
1130 
1131 #if HASH_STATISTICS
1132  hash_accesses++;
1133  hctl->accesses++;
1134 #endif
1135 
1136  /* disallow updates if frozen */
1137  if (hashp->frozen)
1138  elog(ERROR, "cannot update in frozen hashtable \"%s\"",
1139  hashp->tabname);
1140 
1141  /*
1142  * Lookup the existing element using its saved hash value. We need to do
1143  * this to be able to unlink it from its hash chain, but as a side benefit
1144  * we can verify the validity of the passed existingEntry pointer.
1145  */
1146  bucket = calc_bucket(hctl, existingElement->hashvalue);
1147 
1148  segment_num = bucket >> hashp->sshift;
1149  segment_ndx = MOD(bucket, hashp->ssize);
1150 
1151  segp = hashp->dir[segment_num];
1152 
1153  if (segp == NULL)
1154  hash_corrupted(hashp);
1155 
1156  prevBucketPtr = &segp[segment_ndx];
1157  currBucket = *prevBucketPtr;
1158 
1159  while (currBucket != NULL)
1160  {
1161  if (currBucket == existingElement)
1162  break;
1163  prevBucketPtr = &(currBucket->link);
1164  currBucket = *prevBucketPtr;
1165  }
1166 
1167  if (currBucket == NULL)
1168  elog(ERROR, "hash_update_hash_key argument is not in hashtable \"%s\"",
1169  hashp->tabname);
1170 
1171  oldPrevPtr = prevBucketPtr;
1172 
1173  /*
1174  * Now perform the equivalent of a HASH_ENTER operation to locate the hash
1175  * chain we want to put the entry into.
1176  */
1177  newhashvalue = hashp->hash(newKeyPtr, hashp->keysize);
1178 
1179  newbucket = calc_bucket(hctl, newhashvalue);
1180 
1181  segment_num = newbucket >> hashp->sshift;
1182  segment_ndx = MOD(newbucket, hashp->ssize);
1183 
1184  segp = hashp->dir[segment_num];
1185 
1186  if (segp == NULL)
1187  hash_corrupted(hashp);
1188 
1189  prevBucketPtr = &segp[segment_ndx];
1190  currBucket = *prevBucketPtr;
1191 
1192  /*
1193  * Follow collision chain looking for matching key
1194  */
1195  match = hashp->match; /* save one fetch in inner loop */
1196  keysize = hashp->keysize; /* ditto */
1197 
1198  while (currBucket != NULL)
1199  {
1200  if (currBucket->hashvalue == newhashvalue &&
1201  match(ELEMENTKEY(currBucket), newKeyPtr, keysize) == 0)
1202  break;
1203  prevBucketPtr = &(currBucket->link);
1204  currBucket = *prevBucketPtr;
1205 #if HASH_STATISTICS
1206  hash_collisions++;
1207  hctl->collisions++;
1208 #endif
1209  }
1210 
1211  if (currBucket != NULL)
1212  return false; /* collision with an existing entry */
1213 
1214  currBucket = existingElement;
1215 
1216  /*
1217  * If old and new hash values belong to the same bucket, we need not
1218  * change any chain links, and indeed should not since this simplistic
1219  * update will corrupt the list if currBucket is the last element. (We
1220  * cannot fall out earlier, however, since we need to scan the bucket to
1221  * check for duplicate keys.)
1222  */
1223  if (bucket != newbucket)
1224  {
1225  /* OK to remove record from old hash bucket's chain. */
1226  *oldPrevPtr = currBucket->link;
1227 
1228  /* link into new hashbucket chain */
1229  *prevBucketPtr = currBucket;
1230  currBucket->link = NULL;
1231  }
1232 
1233  /* copy new key into record */
1234  currBucket->hashvalue = newhashvalue;
1235  hashp->keycopy(ELEMENTKEY(currBucket), newKeyPtr, keysize);
1236 
1237  /* rest of record is untouched */
1238 
1239  return true;
1240 }
1241 
1242 /*
1243  * Allocate a new hashtable entry if possible; return NULL if out of memory.
1244  * (Or, if the underlying space allocator throws error for out-of-memory,
1245  * we won't return at all.)
1246  */
1247 static HASHBUCKET
1248 get_hash_entry(HTAB *hashp, int freelist_idx)
1249 {
1250  HASHHDR *hctl = hashp->hctl;
1251  HASHBUCKET newElement;
1252 
1253  for (;;)
1254  {
1255  /* if partitioned, must lock to touch nentries and freeList */
1256  if (IS_PARTITIONED(hctl))
1257  SpinLockAcquire(&hctl->freeList[freelist_idx].mutex);
1258 
1259  /* try to get an entry from the freelist */
1260  newElement = hctl->freeList[freelist_idx].freeList;
1261 
1262  if (newElement != NULL)
1263  break;
1264 
1265  if (IS_PARTITIONED(hctl))
1266  SpinLockRelease(&hctl->freeList[freelist_idx].mutex);
1267 
1268  /*
1269  * No free elements in this freelist. In a partitioned table, there
1270  * might be entries in other freelists, but to reduce contention we
1271  * prefer to first try to get another chunk of buckets from the main
1272  * shmem allocator. If that fails, though, we *MUST* root through all
1273  * the other freelists before giving up. There are multiple callers
1274  * that assume that they can allocate every element in the initially
1275  * requested table size, or that deleting an element guarantees they
1276  * can insert a new element, even if shared memory is entirely full.
1277  * Failing because the needed element is in a different freelist is
1278  * not acceptable.
1279  */
1280  if (!element_alloc(hashp, hctl->nelem_alloc, freelist_idx))
1281  {
1282  int borrow_from_idx;
1283 
1284  if (!IS_PARTITIONED(hctl))
1285  return NULL; /* out of memory */
1286 
1287  /* try to borrow element from another freelist */
1288  borrow_from_idx = freelist_idx;
1289  for (;;)
1290  {
1291  borrow_from_idx = (borrow_from_idx + 1) % NUM_FREELISTS;
1292  if (borrow_from_idx == freelist_idx)
1293  break; /* examined all freelists, fail */
1294 
1295  SpinLockAcquire(&(hctl->freeList[borrow_from_idx].mutex));
1296  newElement = hctl->freeList[borrow_from_idx].freeList;
1297 
1298  if (newElement != NULL)
1299  {
1300  hctl->freeList[borrow_from_idx].freeList = newElement->link;
1301  SpinLockRelease(&(hctl->freeList[borrow_from_idx].mutex));
1302 
1303  /* careful: count the new element in its proper freelist */
1304  SpinLockAcquire(&hctl->freeList[freelist_idx].mutex);
1305  hctl->freeList[freelist_idx].nentries++;
1306  SpinLockRelease(&hctl->freeList[freelist_idx].mutex);
1307 
1308  return newElement;
1309  }
1310 
1311  SpinLockRelease(&(hctl->freeList[borrow_from_idx].mutex));
1312  }
1313 
1314  /* no elements available to borrow either, so out of memory */
1315  return NULL;
1316  }
1317  }
1318 
1319  /* remove entry from freelist, bump nentries */
1320  hctl->freeList[freelist_idx].freeList = newElement->link;
1321  hctl->freeList[freelist_idx].nentries++;
1322 
1323  if (IS_PARTITIONED(hctl))
1324  SpinLockRelease(&hctl->freeList[freelist_idx].mutex);
1325 
1326  return newElement;
1327 }
1328 
1329 /*
1330  * hash_get_num_entries -- get the number of entries in a hashtable
1331  */
1332 long
1334 {
1335  int i;
1336  long sum = hashp->hctl->freeList[0].nentries;
1337 
1338  /*
1339  * We currently don't bother with acquiring the mutexes; it's only
1340  * sensible to call this function if you've got lock on all partitions of
1341  * the table.
1342  */
1343  if (IS_PARTITIONED(hashp->hctl))
1344  {
1345  for (i = 1; i < NUM_FREELISTS; i++)
1346  sum += hashp->hctl->freeList[i].nentries;
1347  }
1348 
1349  return sum;
1350 }
1351 
1352 /*
1353  * hash_seq_init/_search/_term
1354  * Sequentially search through hash table and return
1355  * all the elements one by one, return NULL when no more.
1356  *
1357  * hash_seq_term should be called if and only if the scan is abandoned before
1358  * completion; if hash_seq_search returns NULL then it has already done the
1359  * end-of-scan cleanup.
1360  *
1361  * NOTE: caller may delete the returned element before continuing the scan.
1362  * However, deleting any other element while the scan is in progress is
1363  * UNDEFINED (it might be the one that curIndex is pointing at!). Also,
1364  * if elements are added to the table while the scan is in progress, it is
1365  * unspecified whether they will be visited by the scan or not.
1366  *
1367  * NOTE: it is possible to use hash_seq_init/hash_seq_search without any
1368  * worry about hash_seq_term cleanup, if the hashtable is first locked against
1369  * further insertions by calling hash_freeze.
1370  *
1371  * NOTE: to use this with a partitioned hashtable, caller had better hold
1372  * at least shared lock on all partitions of the table throughout the scan!
1373  * We can cope with insertions or deletions by our own backend, but *not*
1374  * with concurrent insertions or deletions by another.
1375  */
1376 void
1378 {
1379  status->hashp = hashp;
1380  status->curBucket = 0;
1381  status->curEntry = NULL;
1382  if (!hashp->frozen)
1383  register_seq_scan(hashp);
1384 }
1385 
1386 void *
1388 {
1389  HTAB *hashp;
1390  HASHHDR *hctl;
1392  long ssize;
1393  long segment_num;
1394  long segment_ndx;
1395  HASHSEGMENT segp;
1396  uint32 curBucket;
1397  HASHELEMENT *curElem;
1398 
1399  if ((curElem = status->curEntry) != NULL)
1400  {
1401  /* Continuing scan of curBucket... */
1402  status->curEntry = curElem->link;
1403  if (status->curEntry == NULL) /* end of this bucket */
1404  ++status->curBucket;
1405  return (void *) ELEMENTKEY(curElem);
1406  }
1407 
1408  /*
1409  * Search for next nonempty bucket starting at curBucket.
1410  */
1411  curBucket = status->curBucket;
1412  hashp = status->hashp;
1413  hctl = hashp->hctl;
1414  ssize = hashp->ssize;
1415  max_bucket = hctl->max_bucket;
1416 
1417  if (curBucket > max_bucket)
1418  {
1419  hash_seq_term(status);
1420  return NULL; /* search is done */
1421  }
1422 
1423  /*
1424  * first find the right segment in the table directory.
1425  */
1426  segment_num = curBucket >> hashp->sshift;
1427  segment_ndx = MOD(curBucket, ssize);
1428 
1429  segp = hashp->dir[segment_num];
1430 
1431  /*
1432  * Pick up the first item in this bucket's chain. If chain is not empty
1433  * we can begin searching it. Otherwise we have to advance to find the
1434  * next nonempty bucket. We try to optimize that case since searching a
1435  * near-empty hashtable has to iterate this loop a lot.
1436  */
1437  while ((curElem = segp[segment_ndx]) == NULL)
1438  {
1439  /* empty bucket, advance to next */
1440  if (++curBucket > max_bucket)
1441  {
1442  status->curBucket = curBucket;
1443  hash_seq_term(status);
1444  return NULL; /* search is done */
1445  }
1446  if (++segment_ndx >= ssize)
1447  {
1448  segment_num++;
1449  segment_ndx = 0;
1450  segp = hashp->dir[segment_num];
1451  }
1452  }
1453 
1454  /* Begin scan of curBucket... */
1455  status->curEntry = curElem->link;
1456  if (status->curEntry == NULL) /* end of this bucket */
1457  ++curBucket;
1458  status->curBucket = curBucket;
1459  return (void *) ELEMENTKEY(curElem);
1460 }
1461 
1462 void
1464 {
1465  if (!status->hashp->frozen)
1466  deregister_seq_scan(status->hashp);
1467 }
1468 
1469 /*
1470  * hash_freeze
1471  * Freeze a hashtable against future insertions (deletions are
1472  * still allowed)
1473  *
1474  * The reason for doing this is that by preventing any more bucket splits,
1475  * we no longer need to worry about registering hash_seq_search scans,
1476  * and thus caller need not be careful about ensuring hash_seq_term gets
1477  * called at the right times.
1478  *
1479  * Multiple calls to hash_freeze() are allowed, but you can't freeze a table
1480  * with active scans (since hash_seq_term would then do the wrong thing).
1481  */
1482 void
1484 {
1485  if (hashp->isshared)
1486  elog(ERROR, "cannot freeze shared hashtable \"%s\"", hashp->tabname);
1487  if (!hashp->frozen && has_seq_scans(hashp))
1488  elog(ERROR, "cannot freeze hashtable \"%s\" because it has active scans",
1489  hashp->tabname);
1490  hashp->frozen = true;
1491 }
1492 
1493 
1494 /********************************* UTILITIES ************************/
1495 
1496 /*
1497  * Expand the table by adding one more hash bucket.
1498  */
1499 static bool
1501 {
1502  HASHHDR *hctl = hashp->hctl;
1503  HASHSEGMENT old_seg,
1504  new_seg;
1505  long old_bucket,
1506  new_bucket;
1507  long new_segnum,
1508  new_segndx;
1509  long old_segnum,
1510  old_segndx;
1511  HASHBUCKET *oldlink,
1512  *newlink;
1513  HASHBUCKET currElement,
1514  nextElement;
1515 
1516  Assert(!IS_PARTITIONED(hctl));
1517 
1518 #ifdef HASH_STATISTICS
1519  hash_expansions++;
1520 #endif
1521 
1522  new_bucket = hctl->max_bucket + 1;
1523  new_segnum = new_bucket >> hashp->sshift;
1524  new_segndx = MOD(new_bucket, hashp->ssize);
1525 
1526  if (new_segnum >= hctl->nsegs)
1527  {
1528  /* Allocate new segment if necessary -- could fail if dir full */
1529  if (new_segnum >= hctl->dsize)
1530  if (!dir_realloc(hashp))
1531  return false;
1532  if (!(hashp->dir[new_segnum] = seg_alloc(hashp)))
1533  return false;
1534  hctl->nsegs++;
1535  }
1536 
1537  /* OK, we created a new bucket */
1538  hctl->max_bucket++;
1539 
1540  /*
1541  * *Before* changing masks, find old bucket corresponding to same hash
1542  * values; values in that bucket may need to be relocated to new bucket.
1543  * Note that new_bucket is certainly larger than low_mask at this point,
1544  * so we can skip the first step of the regular hash mask calc.
1545  */
1546  old_bucket = (new_bucket & hctl->low_mask);
1547 
1548  /*
1549  * If we crossed a power of 2, readjust masks.
1550  */
1551  if ((uint32) new_bucket > hctl->high_mask)
1552  {
1553  hctl->low_mask = hctl->high_mask;
1554  hctl->high_mask = (uint32) new_bucket | hctl->low_mask;
1555  }
1556 
1557  /*
1558  * Relocate records to the new bucket. NOTE: because of the way the hash
1559  * masking is done in calc_bucket, only one old bucket can need to be
1560  * split at this point. With a different way of reducing the hash value,
1561  * that might not be true!
1562  */
1563  old_segnum = old_bucket >> hashp->sshift;
1564  old_segndx = MOD(old_bucket, hashp->ssize);
1565 
1566  old_seg = hashp->dir[old_segnum];
1567  new_seg = hashp->dir[new_segnum];
1568 
1569  oldlink = &old_seg[old_segndx];
1570  newlink = &new_seg[new_segndx];
1571 
1572  for (currElement = *oldlink;
1573  currElement != NULL;
1574  currElement = nextElement)
1575  {
1576  nextElement = currElement->link;
1577  if ((long) calc_bucket(hctl, currElement->hashvalue) == old_bucket)
1578  {
1579  *oldlink = currElement;
1580  oldlink = &currElement->link;
1581  }
1582  else
1583  {
1584  *newlink = currElement;
1585  newlink = &currElement->link;
1586  }
1587  }
1588  /* don't forget to terminate the rebuilt hash chains... */
1589  *oldlink = NULL;
1590  *newlink = NULL;
1591 
1592  return true;
1593 }
1594 
1595 
1596 static bool
1598 {
1599  HASHSEGMENT *p;
1600  HASHSEGMENT *old_p;
1601  long new_dsize;
1602  long old_dirsize;
1603  long new_dirsize;
1604 
1605  if (hashp->hctl->max_dsize != NO_MAX_DSIZE)
1606  return false;
1607 
1608  /* Reallocate directory */
1609  new_dsize = hashp->hctl->dsize << 1;
1610  old_dirsize = hashp->hctl->dsize * sizeof(HASHSEGMENT);
1611  new_dirsize = new_dsize * sizeof(HASHSEGMENT);
1612 
1613  old_p = hashp->dir;
1614  CurrentDynaHashCxt = hashp->hcxt;
1615  p = (HASHSEGMENT *) hashp->alloc((Size) new_dirsize);
1616 
1617  if (p != NULL)
1618  {
1619  memcpy(p, old_p, old_dirsize);
1620  MemSet(((char *) p) + old_dirsize, 0, new_dirsize - old_dirsize);
1621  hashp->dir = p;
1622  hashp->hctl->dsize = new_dsize;
1623 
1624  /* XXX assume the allocator is palloc, so we know how to free */
1625  Assert(hashp->alloc == DynaHashAlloc);
1626  pfree(old_p);
1627 
1628  return true;
1629  }
1630 
1631  return false;
1632 }
1633 
1634 
1635 static HASHSEGMENT
1637 {
1638  HASHSEGMENT segp;
1639 
1640  CurrentDynaHashCxt = hashp->hcxt;
1641  segp = (HASHSEGMENT) hashp->alloc(sizeof(HASHBUCKET) * hashp->ssize);
1642 
1643  if (!segp)
1644  return NULL;
1645 
1646  MemSet(segp, 0, sizeof(HASHBUCKET) * hashp->ssize);
1647 
1648  return segp;
1649 }
1650 
1651 /*
1652  * allocate some new elements and link them into the indicated free list
1653  */
1654 static bool
1655 element_alloc(HTAB *hashp, int nelem, int freelist_idx)
1656 {
1657  HASHHDR *hctl = hashp->hctl;
1658  Size elementSize;
1659  HASHELEMENT *firstElement;
1660  HASHELEMENT *tmpElement;
1661  HASHELEMENT *prevElement;
1662  int i;
1663 
1664  if (hashp->isfixed)
1665  return false;
1666 
1667  /* Each element has a HASHELEMENT header plus user data. */
1668  elementSize = MAXALIGN(sizeof(HASHELEMENT)) + MAXALIGN(hctl->entrysize);
1669 
1670  CurrentDynaHashCxt = hashp->hcxt;
1671  firstElement = (HASHELEMENT *) hashp->alloc(nelem * elementSize);
1672 
1673  if (!firstElement)
1674  return false;
1675 
1676  /* prepare to link all the new entries into the freelist */
1677  prevElement = NULL;
1678  tmpElement = firstElement;
1679  for (i = 0; i < nelem; i++)
1680  {
1681  tmpElement->link = prevElement;
1682  prevElement = tmpElement;
1683  tmpElement = (HASHELEMENT *) (((char *) tmpElement) + elementSize);
1684  }
1685 
1686  /* if partitioned, must lock to touch freeList */
1687  if (IS_PARTITIONED(hctl))
1688  SpinLockAcquire(&hctl->freeList[freelist_idx].mutex);
1689 
1690  /* freelist could be nonempty if two backends did this concurrently */
1691  firstElement->link = hctl->freeList[freelist_idx].freeList;
1692  hctl->freeList[freelist_idx].freeList = prevElement;
1693 
1694  if (IS_PARTITIONED(hctl))
1695  SpinLockRelease(&hctl->freeList[freelist_idx].mutex);
1696 
1697  return true;
1698 }
1699 
1700 /* complain when we have detected a corrupted hashtable */
1701 static void
1703 {
1704  /*
1705  * If the corruption is in a shared hashtable, we'd better force a
1706  * systemwide restart. Otherwise, just shut down this one backend.
1707  */
1708  if (hashp->isshared)
1709  elog(PANIC, "hash table \"%s\" corrupted", hashp->tabname);
1710  else
1711  elog(FATAL, "hash table \"%s\" corrupted", hashp->tabname);
1712 }
1713 
1714 /* calculate ceil(log base 2) of num */
1715 int
1716 my_log2(long num)
1717 {
1718  int i;
1719  long limit;
1720 
1721  /* guard against too-large input, which would put us into infinite loop */
1722  if (num > LONG_MAX / 2)
1723  num = LONG_MAX / 2;
1724 
1725  for (i = 0, limit = 1; limit < num; i++, limit <<= 1)
1726  ;
1727  return i;
1728 }
1729 
1730 /* calculate first power of 2 >= num, bounded to what will fit in a long */
1731 static long
1733 {
1734  /* my_log2's internal range check is sufficient */
1735  return 1L << my_log2(num);
1736 }
1737 
1738 /* calculate first power of 2 >= num, bounded to what will fit in an int */
1739 static int
1740 next_pow2_int(long num)
1741 {
1742  if (num > INT_MAX / 2)
1743  num = INT_MAX / 2;
1744  return 1 << my_log2(num);
1745 }
1746 
1747 
1748 /************************* SEQ SCAN TRACKING ************************/
1749 
1750 /*
1751  * We track active hash_seq_search scans here. The need for this mechanism
1752  * comes from the fact that a scan will get confused if a bucket split occurs
1753  * while it's in progress: it might visit entries twice, or even miss some
1754  * entirely (if it's partway through the same bucket that splits). Hence
1755  * we want to inhibit bucket splits if there are any active scans on the
1756  * table being inserted into. This is a fairly rare case in current usage,
1757  * so just postponing the split until the next insertion seems sufficient.
1758  *
1759  * Given present usages of the function, only a few scans are likely to be
1760  * open concurrently; so a finite-size stack of open scans seems sufficient,
1761  * and we don't worry that linear search is too slow. Note that we do
1762  * allow multiple scans of the same hashtable to be open concurrently.
1763  *
1764  * This mechanism can support concurrent scan and insertion in a shared
1765  * hashtable if it's the same backend doing both. It would fail otherwise,
1766  * but locking reasons seem to preclude any such scenario anyway, so we don't
1767  * worry.
1768  *
1769  * This arrangement is reasonably robust if a transient hashtable is deleted
1770  * without notifying us. The absolute worst case is we might inhibit splits
1771  * in another table created later at exactly the same address. We will give
1772  * a warning at transaction end for reference leaks, so any bugs leading to
1773  * lack of notification should be easy to catch.
1774  */
1775 
1776 #define MAX_SEQ_SCANS 100
1777 
1778 static HTAB *seq_scan_tables[MAX_SEQ_SCANS]; /* tables being scanned */
1779 static int seq_scan_level[MAX_SEQ_SCANS]; /* subtransaction nest level */
1780 static int num_seq_scans = 0;
1781 
1782 
1783 /* Register a table as having an active hash_seq_search scan */
1784 static void
1786 {
1788  elog(ERROR, "too many active hash_seq_search scans, cannot start one on \"%s\"",
1789  hashp->tabname);
1790  seq_scan_tables[num_seq_scans] = hashp;
1792  num_seq_scans++;
1793 }
1794 
1795 /* Deregister an active scan */
1796 static void
1798 {
1799  int i;
1800 
1801  /* Search backward since it's most likely at the stack top */
1802  for (i = num_seq_scans - 1; i >= 0; i--)
1803  {
1804  if (seq_scan_tables[i] == hashp)
1805  {
1806  seq_scan_tables[i] = seq_scan_tables[num_seq_scans - 1];
1808  num_seq_scans--;
1809  return;
1810  }
1811  }
1812  elog(ERROR, "no hash_seq_search scan for hash table \"%s\"",
1813  hashp->tabname);
1814 }
1815 
1816 /* Check if a table has any active scan */
1817 static bool
1819 {
1820  int i;
1821 
1822  for (i = 0; i < num_seq_scans; i++)
1823  {
1824  if (seq_scan_tables[i] == hashp)
1825  return true;
1826  }
1827  return false;
1828 }
1829 
1830 /* Clean up any open scans at end of transaction */
1831 void
1832 AtEOXact_HashTables(bool isCommit)
1833 {
1834  /*
1835  * During abort cleanup, open scans are expected; just silently clean 'em
1836  * out. An open scan at commit means someone forgot a hash_seq_term()
1837  * call, so complain.
1838  *
1839  * Note: it's tempting to try to print the tabname here, but refrain for
1840  * fear of touching deallocated memory. This isn't a user-facing message
1841  * anyway, so it needn't be pretty.
1842  */
1843  if (isCommit)
1844  {
1845  int i;
1846 
1847  for (i = 0; i < num_seq_scans; i++)
1848  {
1849  elog(WARNING, "leaked hash_seq_search scan for hash table %p",
1850  seq_scan_tables[i]);
1851  }
1852  }
1853  num_seq_scans = 0;
1854 }
1855 
1856 /* Clean up any open scans at end of subtransaction */
1857 void
1858 AtEOSubXact_HashTables(bool isCommit, int nestDepth)
1859 {
1860  int i;
1861 
1862  /*
1863  * Search backward to make cleanup easy. Note we must check all entries,
1864  * not only those at the end of the array, because deletion technique
1865  * doesn't keep them in order.
1866  */
1867  for (i = num_seq_scans - 1; i >= 0; i--)
1868  {
1869  if (seq_scan_level[i] >= nestDepth)
1870  {
1871  if (isCommit)
1872  elog(WARNING, "leaked hash_seq_search scan for hash table %p",
1873  seq_scan_tables[i]);
1874  seq_scan_tables[i] = seq_scan_tables[num_seq_scans - 1];
1876  num_seq_scans--;
1877  }
1878  }
1879 }
void * hash_search_with_hash_value(HTAB *hashp, const void *keyPtr, uint32 hashvalue, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:917
static int seq_scan_level[MAX_SEQ_SCANS]
Definition: dynahash.c:1779
int(* HashCompareFunc)(const void *key1, const void *key2, Size keysize)
Definition: hsearch.h:29
Size keysize
Definition: dynahash.c:179
int slock_t
Definition: s_lock.h:912
Size keysize
Definition: dynahash.c:225
long dsize
Definition: dynahash.c:172
uint32(* HashValueFunc)(const void *key, Size keysize)
Definition: hsearch.h:21
uint32 curBucket
Definition: hsearch.h:115
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:812
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:198
#define HASH_CONTEXT
Definition: hsearch.h:93
#define HASH_ELEM
Definition: hsearch.h:87
uint32 max_bucket
Definition: dynahash.c:174
MemoryContext hcxt
Definition: hsearch.h:78
long ssize
Definition: hsearch.h:68
static long next_pow2_long(long num)
Definition: dynahash.c:1732
HashCopyFunc keycopy
Definition: hsearch.h:76
Size entrysize
Definition: dynahash.c:180
#define MEMCONTEXT_COPY_NAME
Definition: memutils.h:188
static uint32 calc_bucket(HASHHDR *hctl, uint32 hash_val)
Definition: dynahash.c:865
#define IS_PARTITIONED(hctl)
Definition: dynahash.c:199
uint32 string_hash(const void *key, Size keysize)
Definition: hashfn.c:34
#define SpinLockInit(lock)
Definition: spin.h:60
#define NO_MAX_DSIZE
Definition: hsearch.h:100
#define ELEMENTKEY(helem)
Definition: dynahash.c:233
long num_partitions
Definition: dynahash.c:181
void AtEOSubXact_HashTables(bool isCommit, int nestDepth)
Definition: dynahash.c:1858
Size entrysize
Definition: hsearch.h:73
#define NUM_FREELISTS
Definition: dynahash.c:116
void hash_stats(const char *where, HTAB *hashp)
Definition: dynahash.c:831
#define ELEMENT_FROM_KEY(key)
Definition: dynahash.c:238
int errcode(int sqlerrcode)
Definition: elog.c:575
HashAllocFunc alloc
Definition: dynahash.c:215
#define MemSet(start, val, len)
Definition: c.h:877
HASHELEMENT * curEntry
Definition: hsearch.h:116
long hash_get_num_entries(HTAB *hashp)
Definition: dynahash.c:1333
slock_t mutex
Definition: dynahash.c:143
uint32 low_mask
Definition: dynahash.c:176
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:904
#define HASH_SHARED_MEM
Definition: hsearch.h:94
static int string_compare(const char *key1, const char *key2, Size keysize)
Definition: dynahash.c:293
static HASHBUCKET get_hash_entry(HTAB *hashp, int freelist_idx)
Definition: dynahash.c:1248
long dsize
Definition: hsearch.h:69
#define PANIC
Definition: elog.h:53
uint32 uint32_hash(const void *key, Size keysize)
Definition: hashfn.c:64
#define HASH_PARTITION
Definition: hsearch.h:83
#define HASH_ATTACH
Definition: hsearch.h:95
char bool
Definition: c.h:257
long ffactor
Definition: dynahash.c:182
HashValueFunc hash
Definition: dynahash.c:212
#define SpinLockAcquire(lock)
Definition: spin.h:62
Definition: dynahash.c:208
uint32 get_hash_value(HTAB *hashp, const void *keyPtr)
Definition: dynahash.c:858
long max_dsize
Definition: hsearch.h:70
void pfree(void *pointer)
Definition: mcxt.c:936
HASHBUCKET * HASHSEGMENT
Definition: dynahash.c:122
long ssize
Definition: dynahash.c:226
#define ERROR
Definition: elog.h:43
long num_partitions
Definition: hsearch.h:67
MemoryContext AllocSetContextCreateExtended(MemoryContext parent, const char *name, int flags, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:394
#define FATAL
Definition: elog.h:52
int sshift
Definition: dynahash.c:227
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:197
#define DEF_SEGSIZE_SHIFT
Definition: dynahash.c:111
#define MOD(x, y)
Definition: dynahash.c:244
HashAllocFunc alloc
Definition: hsearch.h:77
#define HASH_KEYCOPY
Definition: hsearch.h:91
long max_dsize
Definition: dynahash.c:183
static void hdefault(HTAB *hashp)
Definition: dynahash.c:568
HASHHDR * hctl
Definition: dynahash.c:210
static void hash_corrupted(HTAB *hashp)
Definition: dynahash.c:1702
static bool has_seq_scans(HTAB *hashp)
Definition: dynahash.c:1818
struct HASHHDR HASHHDR
Definition: hsearch.h:58
unsigned int uint32
Definition: c.h:306
HASHELEMENT * HASHBUCKET
Definition: dynahash.c:119
HTAB * hashp
Definition: hsearch.h:114
static bool init_htab(HTAB *hashp, long nelem)
Definition: dynahash.c:634
uint32 high_mask
Definition: dynahash.c:175
long nentries
Definition: dynahash.c:144
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:43
int my_log2(long num)
Definition: dynahash.c:1716
#define WARNING
Definition: elog.h:40
HashCompareFunc match
Definition: dynahash.c:213
Size hash_estimate_size(long num_entries, Size entrysize)
Definition: dynahash.c:730
static int next_pow2_int(long num)
Definition: dynahash.c:1740
bool isshared
Definition: dynahash.c:218
#define SpinLockRelease(lock)
Definition: spin.h:64
#define MAX_SEQ_SCANS
Definition: dynahash.c:1776
#define HASH_BLOBS
Definition: hsearch.h:88
bool frozen
Definition: dynahash.c:222
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
static int num_seq_scans
Definition: dynahash.c:1780
static HTAB * seq_scan_tables[MAX_SEQ_SCANS]
Definition: dynahash.c:1778
uint32 tag_hash(const void *key, Size keysize)
Definition: hashfn.c:52
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
long ssize
Definition: dynahash.c:184
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
FreeListData freeList[NUM_FREELISTS]
Definition: dynahash.c:168
static void deregister_seq_scan(HTAB *hashp)
Definition: dynahash.c:1797
Size keysize
Definition: hsearch.h:72
void *(* HashCopyFunc)(void *dest, const void *src, Size keysize)
Definition: hsearch.h:37
HashCompareFunc match
Definition: hsearch.h:75
MemoryContext hcxt
Definition: dynahash.c:216
long hash_select_dirsize(long num_entries)
Definition: dynahash.c:777
HashCopyFunc keycopy
Definition: dynahash.c:214
#define HASH_SEGMENT
Definition: hsearch.h:84
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:754
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define MemoryContextIsValid(context)
Definition: memnodes.h:96
static void register_seq_scan(HTAB *hashp)
Definition: dynahash.c:1785
#define Assert(condition)
Definition: c.h:680
int nelem_alloc
Definition: dynahash.c:186
void *(* HashAllocFunc)(Size request)
Definition: hsearch.h:44
#define HASH_COMPARE
Definition: hsearch.h:90
size_t Size
Definition: c.h:414
void hash_freeze(HTAB *hashp)
Definition: dynahash.c:1483
static HASHSEGMENT seg_alloc(HTAB *hashp)
Definition: dynahash.c:1636
#define MAXALIGN(LEN)
Definition: c.h:633
Size hash_get_shared_size(HASHCTL *info, int flags)
Definition: dynahash.c:801
static bool expand_table(HTAB *hashp)
Definition: dynahash.c:1500
bool hash_update_hash_key(HTAB *hashp, void *existingEntry, const void *newKeyPtr)
Definition: dynahash.c:1113
HASHHDR * hctl
Definition: hsearch.h:79
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1387
long ffactor
Definition: hsearch.h:71
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1377
char * tabname
Definition: dynahash.c:217
#define HASH_FIXED_SIZE
Definition: hsearch.h:96
bool isfixed
Definition: dynahash.c:219
#define HASH_FFACTOR
Definition: hsearch.h:86
static void * DynaHashAlloc(Size size)
Definition: dynahash.c:278
#define HASH_DIRSIZE
Definition: hsearch.h:85
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:693
struct HASHELEMENT * link
Definition: hsearch.h:53
static int choose_nelem_alloc(Size entrysize)
Definition: dynahash.c:601
static MemoryContext CurrentDynaHashCxt
Definition: dynahash.c:275
int i
#define HASH_ALLOC
Definition: hsearch.h:92
HASHELEMENT * freeList
Definition: dynahash.c:145
HASHSEGMENT * dir
Definition: dynahash.c:211
#define elog
Definition: elog.h:219
static bool element_alloc(HTAB *hashp, int nelem, int freelist_idx)
Definition: dynahash.c:1655
#define FREELIST_IDX(hctl, hashcode)
Definition: dynahash.c:201
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1832
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
int sshift
Definition: dynahash.c:185
static bool dir_realloc(HTAB *hashp)
Definition: dynahash.c:1597
#define DEF_FFACTOR
Definition: dynahash.c:113
#define DEF_SEGSIZE
Definition: dynahash.c:110
void hash_seq_term(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1463
uint32 hashvalue
Definition: hsearch.h:54
HASHACTION
Definition: hsearch.h:103
HashValueFunc hash
Definition: hsearch.h:74
#define HASH_FUNCTION
Definition: hsearch.h:89
#define DEF_DIRSIZE
Definition: dynahash.c:112
long nsegs
Definition: dynahash.c:173