123#define DEF_SEGSIZE 256
124#define DEF_SEGSIZE_SHIFT 8
125#define DEF_DIRSIZE 256
128#define NUM_FREELISTS 32
199#ifdef HASH_STATISTICS
210#define IS_PARTITIONED(hctl) ((hctl)->num_partitions != 0)
212#define FREELIST_IDX(hctl, hashcode) \
213 (IS_PARTITIONED(hctl) ? (hashcode) % NUM_FREELISTS : 0)
244#define ELEMENTKEY(helem) (((char *)(helem)) + MAXALIGN(sizeof(HASHELEMENT)))
249#define ELEMENT_FROM_KEY(key) \
250 ((HASHELEMENT *) (((char *) (key)) - MAXALIGN(sizeof(HASHELEMENT))))
255#define MOD(x,y) ((x) & ((y)-1))
257#ifdef HASH_STATISTICS
258static long hash_accesses,
309 return strncmp(key1, key2, keysize - 1);
394 sizeof(
HTAB) + strlen(tabname) + 1);
397 hashp->
tabname = (
char *) (hashp + 1);
398 strcpy(hashp->
tabname, tabname);
451 hashp->
match = memcmp;
516 (
errcode(ERRCODE_OUT_OF_MEMORY),
517 errmsg(
"out of memory")));
580 nelem < hctl->nelem_alloc)
594 freelist_partitions = 1;
596 nelem_alloc = nelem / freelist_partitions;
597 if (nelem_alloc <= 0)
604 if (nelem_alloc * freelist_partitions < nelem)
606 nelem - nelem_alloc * (freelist_partitions - 1);
608 nelem_alloc_first = nelem_alloc;
610 for (
i = 0;
i < freelist_partitions;
i++)
612 int temp = (
i == 0) ? nelem_alloc_first : nelem_alloc;
616 (
errcode(ERRCODE_OUT_OF_MEMORY),
617 errmsg(
"out of memory")));
647#ifdef HASH_STATISTICS
648 hctl->accesses = hctl->collisions = 0;
679 nelem_alloc = allocSize / elementSize;
680 }
while (nelem_alloc < 32);
717 while (nbuckets < hctl->num_partitions)
726 nsegs = (nbuckets - 1) / hctl->
ssize + 1;
733 if (nsegs > hctl->
dsize)
752 for (segp = hashp->
dir; hctl->
nsegs < nsegs; hctl->
nsegs++, segp++)
763 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",
764 "TABLE POINTER ", hashp,
765 "DIRECTORY SIZE ", hctl->
dsize,
766 "SEGMENT SIZE ", hctl->
ssize,
767 "SEGMENT SHIFT ", hctl->
sshift,
771 "NSEGS ", hctl->
nsegs);
800 while (nDirEntries < nSegments)
812 nElementAllocs = (num_entries - 1) / elementAllocCnt + 1;
816 mul_size(elementAllocCnt, elementSize)));
843 while (nDirEntries < nSegments)
887#ifdef HASH_STATISTICS
888 fprintf(stderr,
"%s: this HTAB -- accesses %ld collisions %ld\n",
889 where, hashp->
hctl->accesses, hashp->
hctl->collisions);
891 fprintf(stderr,
"hash_stats: entries %ld keysize %ld maxp %u segmentcount %ld\n",
894 fprintf(stderr,
"%s: total accesses %ld total collisions %ld\n",
895 where, hash_accesses, hash_collisions);
896 fprintf(stderr,
"hash_stats: total expansions %ld\n",
982#ifdef HASH_STATISTICS
1011 currBucket = *prevBucketPtr;
1016 match = hashp->
match;
1019 while (currBucket != NULL)
1021 if (currBucket->
hashvalue == hashvalue &&
1022 match(
ELEMENTKEY(currBucket), keyPtr, keysize) == 0)
1024 prevBucketPtr = &(currBucket->
link);
1025 currBucket = *prevBucketPtr;
1026#ifdef HASH_STATISTICS
1033 *foundPtr = (bool) (currBucket != NULL);
1041 if (currBucket != NULL)
1046 if (currBucket != NULL)
1057 *prevBucketPtr = currBucket->
link;
1078 if (currBucket != NULL)
1083 elog(
ERROR,
"cannot insert into frozen hashtable \"%s\"",
1087 if (currBucket == NULL)
1095 (
errcode(ERRCODE_OUT_OF_MEMORY),
1096 errmsg(
"out of shared memory")));
1099 (
errcode(ERRCODE_OUT_OF_MEMORY),
1100 errmsg(
"out of memory")));
1104 *prevBucketPtr = currBucket;
1105 currBucket->
link = NULL;
1147 void *existingEntry,
1148 const void *newKeyPtr)
1160#ifdef HASH_STATISTICS
1167 elog(
ERROR,
"cannot update in frozen hashtable \"%s\"",
1177 currBucket = *prevBucketPtr;
1179 while (currBucket != NULL)
1181 if (currBucket == existingElement)
1183 prevBucketPtr = &(currBucket->
link);
1184 currBucket = *prevBucketPtr;
1187 if (currBucket == NULL)
1188 elog(
ERROR,
"hash_update_hash_key argument is not in hashtable \"%s\"",
1191 oldPrevPtr = prevBucketPtr;
1197 newhashvalue = hashp->
hash(newKeyPtr, hashp->
keysize);
1199 currBucket = *prevBucketPtr;
1204 match = hashp->
match;
1207 while (currBucket != NULL)
1209 if (currBucket->
hashvalue == newhashvalue &&
1210 match(
ELEMENTKEY(currBucket), newKeyPtr, keysize) == 0)
1212 prevBucketPtr = &(currBucket->
link);
1213 currBucket = *prevBucketPtr;
1214#ifdef HASH_STATISTICS
1220 if (currBucket != NULL)
1223 currBucket = existingElement;
1232 if (bucket != newbucket)
1235 *oldPrevPtr = currBucket->
link;
1238 *prevBucketPtr = currBucket;
1239 currBucket->
link = NULL;
1271 if (newElement != NULL)
1291 int borrow_from_idx;
1297 borrow_from_idx = freelist_idx;
1301 if (borrow_from_idx == freelist_idx)
1307 if (newElement != NULL)
1388 status->
hashp = hashp;
1439 while ((curElem = status->
curEntry) != NULL)
1451 if ((curElem = status->
curEntry) != NULL)
1464 hashp = status->
hashp;
1466 ssize = hashp->
ssize;
1469 if (curBucket > max_bucket)
1478 segment_num = curBucket >> hashp->
sshift;
1479 segment_ndx =
MOD(curBucket, ssize);
1481 segp = hashp->
dir[segment_num];
1489 while ((curElem = segp[segment_ndx]) == NULL)
1492 if (++curBucket > max_bucket)
1498 if (++segment_ndx >= ssize)
1502 segp = hashp->
dir[segment_num];
1540 elog(
ERROR,
"cannot freeze hashtable \"%s\" because it has active scans",
1570#ifdef HASH_STATISTICS
1575 new_segnum = new_bucket >> hashp->
sshift;
1576 new_segndx =
MOD(new_bucket, hashp->
ssize);
1578 if (new_segnum >= hctl->
nsegs)
1581 if (new_segnum >= hctl->
dsize)
1598 old_bucket = (new_bucket & hctl->
low_mask);
1615 old_segnum = old_bucket >> hashp->
sshift;
1616 old_segndx =
MOD(old_bucket, hashp->
ssize);
1618 old_seg = hashp->
dir[old_segnum];
1619 new_seg = hashp->
dir[new_segnum];
1621 oldlink = &old_seg[old_segndx];
1622 newlink = &new_seg[new_segndx];
1624 for (currElement = *oldlink;
1625 currElement != NULL;
1626 currElement = nextElement)
1628 nextElement = currElement->
link;
1631 *oldlink = currElement;
1632 oldlink = &currElement->
link;
1636 *newlink = currElement;
1637 newlink = &currElement->
link;
1671 memcpy(p, old_p, old_dirsize);
1672 MemSet(((
char *) p) + old_dirsize, 0, new_dirsize - old_dirsize);
1730 tmpElement = firstElement;
1731 for (
i = 0;
i < nelem;
i++)
1733 tmpElement->
link = prevElement;
1734 prevElement = tmpElement;
1735 tmpElement = (
HASHELEMENT *) (((
char *) tmpElement) + elementSize);
1767 segment_num = bucket >> hashp->
sshift;
1768 segment_ndx =
MOD(bucket, hashp->
ssize);
1770 segp = hashp->
dir[segment_num];
1775 *bucketptr = &segp[segment_ndx];
1801 if (num > LONG_MAX / 2)
1823 if (num > INT_MAX / 2)
1857#define MAX_SEQ_SCANS 100
1869 elog(
ERROR,
"too many active hash_seq_search scans, cannot start one on \"%s\"",
1893 elog(
ERROR,
"no hash_seq_search scan for hash table \"%s\"",
1930 elog(
WARNING,
"leaked hash_seq_search scan for hash table %p",
1953 elog(
WARNING,
"leaked hash_seq_search scan for hash table %p",
#define MemSet(start, val, len)
void(* pg_funcptr_t)(void)
#define fprintf(file, fmt, msg)
static HTAB * seq_scan_tables[MAX_SEQ_SCANS]
static int seq_scan_level[MAX_SEQ_SCANS]
void hash_seq_init_with_hash_value(HASH_SEQ_STATUS *status, HTAB *hashp, uint32 hashvalue)
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
#define ELEMENT_FROM_KEY(key)
static void * DynaHashAlloc(Size size)
static bool element_alloc(HTAB *hashp, int nelem, int freelist_idx)
void AtEOXact_HashTables(bool isCommit)
static bool init_htab(HTAB *hashp, long nelem)
static HASHSEGMENT seg_alloc(HTAB *hashp)
static MemoryContext CurrentDynaHashCxt
static int choose_nelem_alloc(Size entrysize)
static int next_pow2_int(long num)
Size hash_get_shared_size(HASHCTL *info, int flags)
static void register_seq_scan(HTAB *hashp)
#define IS_PARTITIONED(hctl)
#define DEF_SEGSIZE_SHIFT
void AtEOSubXact_HashTables(bool isCommit, int nestDepth)
static HASHBUCKET get_hash_entry(HTAB *hashp, int freelist_idx)
void hash_destroy(HTAB *hashp)
static int string_compare(const char *key1, const char *key2, Size keysize)
void * hash_search_with_hash_value(HTAB *hashp, const void *keyPtr, uint32 hashvalue, HASHACTION action, bool *foundPtr)
void * hash_seq_search(HASH_SEQ_STATUS *status)
static bool expand_table(HTAB *hashp)
static void hdefault(HTAB *hashp)
static void deregister_seq_scan(HTAB *hashp)
#define ELEMENTKEY(helem)
void hash_seq_term(HASH_SEQ_STATUS *status)
#define FREELIST_IDX(hctl, hashcode)
long hash_get_num_entries(HTAB *hashp)
long hash_select_dirsize(long num_entries)
static pg_noreturn void hash_corrupted(HTAB *hashp)
Size hash_estimate_size(long num_entries, Size entrysize)
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
void hash_stats(const char *where, HTAB *hashp)
void hash_freeze(HTAB *hashp)
static bool dir_realloc(HTAB *hashp)
bool hash_update_hash_key(HTAB *hashp, void *existingEntry, const void *newKeyPtr)
static uint32 hash_initial_lookup(HTAB *hashp, uint32 hashvalue, HASHBUCKET **bucketptr)
uint32 get_hash_value(HTAB *hashp, const void *keyPtr)
static uint32 calc_bucket(HASHHDR *hctl, uint32 hash_val)
static bool has_seq_scans(HTAB *hashp)
static long next_pow2_long(long num)
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define MCXT_ALLOC_NO_OOM
uint32 tag_hash(const void *key, Size keysize)
uint32 uint32_hash(const void *key, Size keysize)
uint32 string_hash(const void *key, Size keysize)
Assert(PointerIsAligned(start, uint64))
void *(* HashAllocFunc)(Size request)
int(* HashCompareFunc)(const void *key1, const void *key2, Size keysize)
uint32(* HashValueFunc)(const void *key, Size keysize)
void *(* HashCopyFunc)(void *dest, const void *src, Size keysize)
void * MemoryContextAlloc(MemoryContext context, Size size)
void pfree(void *pointer)
MemoryContext TopMemoryContext
void * MemoryContextAllocExtended(MemoryContext context, Size size, int flags)
void MemoryContextDelete(MemoryContext context)
void MemoryContextSetIdentifier(MemoryContext context, const char *id)
#define MemoryContextIsValid(context)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
static uint64 pg_ceil_log2_64(uint64 num)
static uint32 pg_ceil_log2_32(uint32 num)
size_t strlcpy(char *dst, const char *src, size_t siz)
Size add_size(Size s1, Size s2)
Size mul_size(Size s1, Size s2)
#define SpinLockInit(lock)
#define SpinLockRelease(lock)
#define SpinLockAcquire(lock)
struct HASHELEMENT * link
FreeListData freeList[NUM_FREELISTS]
int GetCurrentTransactionNestLevel(void)