123#define DEF_SEGSIZE 256
124#define DEF_SEGSIZE_SHIFT 8
125#define DEF_DIRSIZE 256
128#define NUM_FREELISTS 32
200#ifdef HASH_STATISTICS
212#define IS_PARTITIONED(hctl) ((hctl)->num_partitions != 0)
214#define FREELIST_IDX(hctl, hashcode) \
215 (IS_PARTITIONED(hctl) ? (hashcode) % NUM_FREELISTS : 0)
255#define ELEMENTKEY(helem) (((char *)(helem)) + MAXALIGN(sizeof(HASHELEMENT)))
260#define ELEMENT_FROM_KEY(key) \
261 ((HASHELEMENT *) (((char *) (key)) - MAXALIGN(sizeof(HASHELEMENT))))
266#define MOD(x,y) ((x) & ((y)-1))
315 return strncmp(key1, key2, keysize - 1);
400 sizeof(
HTAB) + strlen(tabname) + 1);
403 hashp->
tabname = (
char *) (hashp + 1);
404 strcpy(hashp->
tabname, tabname);
457 hashp->
match = memcmp;
522 (
errcode(ERRCODE_OUT_OF_MEMORY),
523 errmsg(
"out of memory")));
586 nelem < hctl->nelem_alloc)
600 freelist_partitions = 1;
602 nelem_alloc = nelem / freelist_partitions;
603 if (nelem_alloc <= 0)
610 if (nelem_alloc * freelist_partitions < nelem)
612 nelem - nelem_alloc * (freelist_partitions - 1);
614 nelem_alloc_first = nelem_alloc;
616 for (
i = 0;
i < freelist_partitions;
i++)
618 int temp = (
i == 0) ? nelem_alloc_first : nelem_alloc;
622 (
errcode(ERRCODE_OUT_OF_MEMORY),
623 errmsg(
"out of memory")));
657#ifdef HASH_STATISTICS
658 hctl->accesses = hctl->collisions = hctl->expansions = 0;
689 nelem_alloc = allocSize / elementSize;
690 }
while (nelem_alloc < 32);
727 while (nbuckets < hctl->num_partitions)
736 nsegs = (nbuckets - 1) / hctl->
ssize + 1;
743 if (nsegs > hctl->
dsize)
762 for (segp = hashp->
dir; hctl->
nsegs < nsegs; hctl->
nsegs++, segp++)
799 while (nDirEntries < nSegments)
811 nElementAllocs = (num_entries - 1) / elementAllocCnt + 1;
815 mul_size(elementAllocCnt, elementSize)));
842 while (nDirEntries < nSegments)
886#ifdef HASH_STATISTICS
891 caller != NULL ? caller :
"(unknown)", hashp->
tabname, hctl->accesses,
978#ifdef HASH_STATISTICS
1006 currBucket = *prevBucketPtr;
1011 match = hashp->
match;
1014 while (currBucket != NULL)
1016 if (currBucket->
hashvalue == hashvalue &&
1017 match(
ELEMENTKEY(currBucket), keyPtr, keysize) == 0)
1019 prevBucketPtr = &(currBucket->
link);
1020 currBucket = *prevBucketPtr;
1021#ifdef HASH_STATISTICS
1027 *foundPtr = (bool) (currBucket != NULL);
1035 if (currBucket != NULL)
1040 if (currBucket != NULL)
1051 *prevBucketPtr = currBucket->
link;
1072 if (currBucket != NULL)
1077 elog(
ERROR,
"cannot insert into frozen hashtable \"%s\"",
1081 if (currBucket == NULL)
1089 (
errcode(ERRCODE_OUT_OF_MEMORY),
1090 errmsg(
"out of shared memory")));
1093 (
errcode(ERRCODE_OUT_OF_MEMORY),
1094 errmsg(
"out of memory")));
1098 *prevBucketPtr = currBucket;
1099 currBucket->
link = NULL;
1141 void *existingEntry,
1142 const void *newKeyPtr)
1154#ifdef HASH_STATISTICS
1162 elog(
ERROR,
"cannot update in frozen hashtable \"%s\"",
1172 currBucket = *prevBucketPtr;
1174 while (currBucket != NULL)
1176 if (currBucket == existingElement)
1178 prevBucketPtr = &(currBucket->
link);
1179 currBucket = *prevBucketPtr;
1182 if (currBucket == NULL)
1183 elog(
ERROR,
"hash_update_hash_key argument is not in hashtable \"%s\"",
1186 oldPrevPtr = prevBucketPtr;
1192 newhashvalue = hashp->
hash(newKeyPtr, hashp->
keysize);
1194 currBucket = *prevBucketPtr;
1199 match = hashp->
match;
1202 while (currBucket != NULL)
1204 if (currBucket->
hashvalue == newhashvalue &&
1205 match(
ELEMENTKEY(currBucket), newKeyPtr, keysize) == 0)
1207 prevBucketPtr = &(currBucket->
link);
1208 currBucket = *prevBucketPtr;
1209#ifdef HASH_STATISTICS
1214 if (currBucket != NULL)
1217 currBucket = existingElement;
1226 if (bucket != newbucket)
1229 *oldPrevPtr = currBucket->
link;
1232 *prevBucketPtr = currBucket;
1233 currBucket->
link = NULL;
1265 if (newElement != NULL)
1285 int borrow_from_idx;
1291 borrow_from_idx = freelist_idx;
1295 if (borrow_from_idx == freelist_idx)
1301 if (newElement != NULL)
1382 status->
hashp = hashp;
1433 while ((curElem = status->
curEntry) != NULL)
1445 if ((curElem = status->
curEntry) != NULL)
1458 hashp = status->
hashp;
1460 ssize = hashp->
ssize;
1463 if (curBucket > max_bucket)
1472 segment_num = curBucket >> hashp->
sshift;
1473 segment_ndx =
MOD(curBucket, ssize);
1475 segp = hashp->
dir[segment_num];
1483 while ((curElem = segp[segment_ndx]) == NULL)
1486 if (++curBucket > max_bucket)
1492 if (++segment_ndx >= ssize)
1496 segp = hashp->
dir[segment_num];
1534 elog(
ERROR,
"cannot freeze hashtable \"%s\" because it has active scans",
1564#ifdef HASH_STATISTICS
1569 new_segnum = new_bucket >> hashp->
sshift;
1570 new_segndx =
MOD(new_bucket, hashp->
ssize);
1572 if (new_segnum >= hctl->
nsegs)
1575 if (new_segnum >= hctl->
dsize)
1592 old_bucket = (new_bucket & hctl->
low_mask);
1609 old_segnum = old_bucket >> hashp->
sshift;
1610 old_segndx =
MOD(old_bucket, hashp->
ssize);
1612 old_seg = hashp->
dir[old_segnum];
1613 new_seg = hashp->
dir[new_segnum];
1615 oldlink = &old_seg[old_segndx];
1616 newlink = &new_seg[new_segndx];
1618 for (currElement = *oldlink;
1619 currElement != NULL;
1620 currElement = nextElement)
1622 nextElement = currElement->
link;
1625 *oldlink = currElement;
1626 oldlink = &currElement->
link;
1630 *newlink = currElement;
1631 newlink = &currElement->
link;
1665 memcpy(p, old_p, old_dirsize);
1666 MemSet(((
char *) p) + old_dirsize, 0, new_dirsize - old_dirsize);
1718 requestSize = nelem * elementSize;
1728 allocedBlock = hashp->
alloc(requestSize);
1752 tmpElement = firstElement;
1753 for (
i = 0;
i < nelem;
i++)
1755 tmpElement->
link = prevElement;
1756 prevElement = tmpElement;
1757 tmpElement = (
HASHELEMENT *) (((
char *) tmpElement) + elementSize);
1789 segment_num = bucket >> hashp->
sshift;
1790 segment_ndx =
MOD(bucket, hashp->
ssize);
1792 segp = hashp->
dir[segment_num];
1797 *bucketptr = &segp[segment_ndx];
1841 if (num > INT_MAX / 2)
1875#define MAX_SEQ_SCANS 100
1887 elog(
ERROR,
"too many active hash_seq_search scans, cannot start one on \"%s\"",
1911 elog(
ERROR,
"no hash_seq_search scan for hash table \"%s\"",
1948 elog(
WARNING,
"leaked hash_seq_search scan for hash table %p",
1971 elog(
WARNING,
"leaked hash_seq_search scan for hash table %p",
#define MemSet(start, val, len)
void(* pg_funcptr_t)(void)
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)
Size hash_estimate_size(int64 num_entries, Size entrysize)
static HASHSEGMENT seg_alloc(HTAB *hashp)
static MemoryContext CurrentDynaHashCxt
static int next_pow2_int(int64 num)
static int choose_nelem_alloc(Size entrysize)
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
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)
int64 hash_select_dirsize(int64 num_entries)
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)
static int my_log2(int64 num)
#define ELEMENTKEY(helem)
void hash_seq_term(HASH_SEQ_STATUS *status)
int64 hash_get_num_entries(HTAB *hashp)
static int64 next_pow2_int64(int64 num)
#define FREELIST_IDX(hctl, hashcode)
void hash_stats(const char *caller, HTAB *hashp)
static bool init_htab(HTAB *hashp, int64 nelem)
static pg_noreturn void hash_corrupted(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)
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)
static void slist_push_head(slist_head *head, slist_node *node)
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)
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)