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