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