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
258 static long hash_accesses,
309 return strncmp(key1, key2, keysize - 1);
396 hashp->
tabname = (
char *) (hashp + 1);
397 strcpy(hashp->
tabname, tabname);
450 hashp->
match = memcmp;
515 (
errcode(ERRCODE_OUT_OF_MEMORY),
516 errmsg(
"out of memory")));
579 nelem < hctl->nelem_alloc)
593 freelist_partitions = 1;
595 nelem_alloc = nelem / freelist_partitions;
596 if (nelem_alloc <= 0)
603 if (nelem_alloc * freelist_partitions < nelem)
605 nelem - nelem_alloc * (freelist_partitions - 1);
607 nelem_alloc_first = nelem_alloc;
609 for (
i = 0;
i < freelist_partitions;
i++)
611 int temp = (
i == 0) ? nelem_alloc_first : nelem_alloc;
615 (
errcode(ERRCODE_OUT_OF_MEMORY),
616 errmsg(
"out of memory")));
646 #ifdef HASH_STATISTICS
647 hctl->accesses = hctl->collisions = 0;
678 nelem_alloc = allocSize / elementSize;
679 }
while (nelem_alloc < 32);
716 while (nbuckets < hctl->num_partitions)
725 nsegs = (nbuckets - 1) / hctl->
ssize + 1;
732 if (nsegs > hctl->
dsize)
751 for (segp = hashp->
dir; hctl->
nsegs < nsegs; hctl->
nsegs++, segp++)
762 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",
763 "TABLE POINTER ", hashp,
764 "DIRECTORY SIZE ", hctl->
dsize,
765 "SEGMENT SIZE ", hctl->
ssize,
766 "SEGMENT SHIFT ", hctl->
sshift,
770 "NSEGS ", hctl->
nsegs);
799 while (nDirEntries < nSegments)
811 nElementAllocs = (num_entries - 1) / elementAllocCnt + 1;
815 mul_size(elementAllocCnt, elementSize)));
842 while (nDirEntries < nSegments)
886 #ifdef HASH_STATISTICS
887 fprintf(stderr,
"%s: this HTAB -- accesses %ld collisions %ld\n",
888 where, hashp->
hctl->accesses, hashp->
hctl->collisions);
890 fprintf(stderr,
"hash_stats: entries %ld keysize %ld maxp %u segmentcount %ld\n",
893 fprintf(stderr,
"%s: total accesses %ld total collisions %ld\n",
894 where, hash_accesses, hash_collisions);
895 fprintf(stderr,
"hash_stats: total expansions %ld\n",
981 #ifdef HASH_STATISTICS
1010 currBucket = *prevBucketPtr;
1015 match = hashp->
match;
1018 while (currBucket != NULL)
1020 if (currBucket->
hashvalue == hashvalue &&
1021 match(
ELEMENTKEY(currBucket), keyPtr, keysize) == 0)
1023 prevBucketPtr = &(currBucket->
link);
1024 currBucket = *prevBucketPtr;
1025 #ifdef HASH_STATISTICS
1032 *foundPtr = (bool) (currBucket != NULL);
1040 if (currBucket != NULL)
1045 if (currBucket != NULL)
1056 *prevBucketPtr = currBucket->
link;
1077 if (currBucket != NULL)
1082 elog(
ERROR,
"cannot insert into frozen hashtable \"%s\"",
1086 if (currBucket == NULL)
1094 (
errcode(ERRCODE_OUT_OF_MEMORY),
1095 errmsg(
"out of shared memory")));
1098 (
errcode(ERRCODE_OUT_OF_MEMORY),
1099 errmsg(
"out of memory")));
1103 *prevBucketPtr = currBucket;
1104 currBucket->
link = NULL;
1146 void *existingEntry,
1147 const void *newKeyPtr)
1159 #ifdef HASH_STATISTICS
1166 elog(
ERROR,
"cannot update in frozen hashtable \"%s\"",
1176 currBucket = *prevBucketPtr;
1178 while (currBucket != NULL)
1180 if (currBucket == existingElement)
1182 prevBucketPtr = &(currBucket->
link);
1183 currBucket = *prevBucketPtr;
1186 if (currBucket == NULL)
1187 elog(
ERROR,
"hash_update_hash_key argument is not in hashtable \"%s\"",
1190 oldPrevPtr = prevBucketPtr;
1196 newhashvalue = hashp->
hash(newKeyPtr, hashp->
keysize);
1198 currBucket = *prevBucketPtr;
1203 match = hashp->
match;
1206 while (currBucket != NULL)
1208 if (currBucket->
hashvalue == newhashvalue &&
1209 match(
ELEMENTKEY(currBucket), newKeyPtr, keysize) == 0)
1211 prevBucketPtr = &(currBucket->
link);
1212 currBucket = *prevBucketPtr;
1213 #ifdef HASH_STATISTICS
1219 if (currBucket != NULL)
1222 currBucket = existingElement;
1231 if (bucket != newbucket)
1234 *oldPrevPtr = currBucket->
link;
1237 *prevBucketPtr = currBucket;
1238 currBucket->
link = NULL;
1270 if (newElement != NULL)
1290 int borrow_from_idx;
1296 borrow_from_idx = freelist_idx;
1300 if (borrow_from_idx == freelist_idx)
1306 if (newElement != NULL)
1387 status->
hashp = hashp;
1438 while ((curElem = status->
curEntry) != NULL)
1450 if ((curElem = status->
curEntry) != NULL)
1463 hashp = status->
hashp;
1465 ssize = hashp->
ssize;
1468 if (curBucket > max_bucket)
1477 segment_num = curBucket >> hashp->
sshift;
1478 segment_ndx =
MOD(curBucket, ssize);
1480 segp = hashp->
dir[segment_num];
1488 while ((curElem = segp[segment_ndx]) == NULL)
1491 if (++curBucket > max_bucket)
1497 if (++segment_ndx >= ssize)
1501 segp = hashp->
dir[segment_num];
1539 elog(
ERROR,
"cannot freeze hashtable \"%s\" because it has active scans",
1569 #ifdef HASH_STATISTICS
1574 new_segnum = new_bucket >> hashp->
sshift;
1575 new_segndx =
MOD(new_bucket, hashp->
ssize);
1577 if (new_segnum >= hctl->
nsegs)
1580 if (new_segnum >= hctl->
dsize)
1597 old_bucket = (new_bucket & hctl->
low_mask);
1614 old_segnum = old_bucket >> hashp->
sshift;
1615 old_segndx =
MOD(old_bucket, hashp->
ssize);
1617 old_seg = hashp->
dir[old_segnum];
1618 new_seg = hashp->
dir[new_segnum];
1620 oldlink = &old_seg[old_segndx];
1621 newlink = &new_seg[new_segndx];
1623 for (currElement = *oldlink;
1624 currElement != NULL;
1625 currElement = nextElement)
1627 nextElement = currElement->
link;
1630 *oldlink = currElement;
1631 oldlink = &currElement->
link;
1635 *newlink = currElement;
1636 newlink = &currElement->
link;
1670 memcpy(p, old_p, old_dirsize);
1671 MemSet(((
char *) p) + old_dirsize, 0, new_dirsize - old_dirsize);
1729 tmpElement = firstElement;
1730 for (
i = 0;
i < nelem;
i++)
1732 tmpElement->
link = prevElement;
1733 prevElement = tmpElement;
1734 tmpElement = (
HASHELEMENT *) (((
char *) tmpElement) + elementSize);
1766 segment_num = bucket >> hashp->
sshift;
1767 segment_ndx =
MOD(bucket, hashp->
ssize);
1769 segp = hashp->
dir[segment_num];
1774 *bucketptr = &segp[segment_ndx];
1800 if (num > LONG_MAX / 2)
1822 if (num > INT_MAX / 2)
1856 #define MAX_SEQ_SCANS 100
1868 elog(
ERROR,
"too many active hash_seq_search scans, cannot start one on \"%s\"",
1892 elog(
ERROR,
"no hash_seq_search scan for hash table \"%s\"",
1929 elog(
WARNING,
"leaked hash_seq_search scan for hash table %p",
1952 elog(
WARNING,
"leaked hash_seq_search scan for hash table %p",
#define Assert(condition)
#define pg_attribute_noreturn()
#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)
#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(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
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)
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
#define FREELIST_IDX(hctl, hashcode)
long hash_get_num_entries(HTAB *hashp)
long hash_select_dirsize(long num_entries)
Size hash_estimate_size(long num_entries, Size entrysize)
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_search_with_hash_value(HTAB *hashp, const void *keyPtr, uint32 hashvalue, HASHACTION action, bool *foundPtr)
void * hash_seq_search(HASH_SEQ_STATUS *status)
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
static void hash_corrupted(HTAB *hashp) pg_attribute_noreturn()
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)
int(* HashCompareFunc)(const void *key1, const void *key2, Size keysize)
uint32(* HashValueFunc)(const void *key, Size keysize)
void *(* HashAllocFunc)(Size request)
void *(* HashCopyFunc)(void *dest, const void *src, Size keysize)
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)
static pg_noinline void Size size
#define SpinLockInit(lock)
#define SpinLockRelease(lock)
#define SpinLockAcquire(lock)
struct HASHELEMENT * link
FreeListData freeList[NUM_FREELISTS]
int GetCurrentTransactionNestLevel(void)