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,
307 return strncmp(key1, key2, keysize - 1);
394 hashp->
tabname = (
char *) (hashp + 1);
395 strcpy(hashp->
tabname, tabname);
448 hashp->
match = memcmp;
513 (
errcode(ERRCODE_OUT_OF_MEMORY),
514 errmsg(
"out of memory")));
577 nelem < hctl->nelem_alloc)
591 freelist_partitions = 1;
593 nelem_alloc = nelem / freelist_partitions;
594 if (nelem_alloc <= 0)
601 if (nelem_alloc * freelist_partitions < nelem)
603 nelem - nelem_alloc * (freelist_partitions - 1);
605 nelem_alloc_first = nelem_alloc;
607 for (
i = 0;
i < freelist_partitions;
i++)
609 int temp = (
i == 0) ? nelem_alloc_first : nelem_alloc;
613 (
errcode(ERRCODE_OUT_OF_MEMORY),
614 errmsg(
"out of memory")));
644 #ifdef HASH_STATISTICS
645 hctl->accesses = hctl->collisions = 0;
676 nelem_alloc = allocSize / elementSize;
677 }
while (nelem_alloc < 32);
714 while (nbuckets < hctl->num_partitions)
723 nsegs = (nbuckets - 1) / hctl->
ssize + 1;
730 if (nsegs > hctl->
dsize)
749 for (segp = hashp->
dir; hctl->
nsegs < nsegs; hctl->
nsegs++, segp++)
760 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",
761 "TABLE POINTER ", hashp,
762 "DIRECTORY SIZE ", hctl->
dsize,
763 "SEGMENT SIZE ", hctl->
ssize,
764 "SEGMENT SHIFT ", hctl->
sshift,
768 "NSEGS ", hctl->
nsegs);
797 while (nDirEntries < nSegments)
809 nElementAllocs = (num_entries - 1) / elementAllocCnt + 1;
813 mul_size(elementAllocCnt, elementSize)));
840 while (nDirEntries < nSegments)
884 #ifdef HASH_STATISTICS
885 fprintf(stderr,
"%s: this HTAB -- accesses %ld collisions %ld\n",
886 where, hashp->
hctl->accesses, hashp->
hctl->collisions);
888 fprintf(stderr,
"hash_stats: entries %ld keysize %ld maxp %u segmentcount %ld\n",
891 fprintf(stderr,
"%s: total accesses %ld total collisions %ld\n",
892 where, hash_accesses, hash_collisions);
893 fprintf(stderr,
"hash_stats: total expansions %ld\n",
983 #ifdef HASH_STATISTICS
1013 segment_num = bucket >> hashp->
sshift;
1014 segment_ndx =
MOD(bucket, hashp->
ssize);
1016 segp = hashp->
dir[segment_num];
1021 prevBucketPtr = &segp[segment_ndx];
1022 currBucket = *prevBucketPtr;
1027 match = hashp->
match;
1030 while (currBucket != NULL)
1032 if (currBucket->
hashvalue == hashvalue &&
1033 match(
ELEMENTKEY(currBucket), keyPtr, keysize) == 0)
1035 prevBucketPtr = &(currBucket->
link);
1036 currBucket = *prevBucketPtr;
1037 #ifdef HASH_STATISTICS
1044 *foundPtr = (
bool) (currBucket != NULL);
1052 if (currBucket != NULL)
1057 if (currBucket != NULL)
1068 *prevBucketPtr = currBucket->
link;
1089 if (currBucket != NULL)
1094 elog(
ERROR,
"cannot insert into frozen hashtable \"%s\"",
1098 if (currBucket == NULL)
1106 (
errcode(ERRCODE_OUT_OF_MEMORY),
1107 errmsg(
"out of shared memory")));
1110 (
errcode(ERRCODE_OUT_OF_MEMORY),
1111 errmsg(
"out of memory")));
1115 *prevBucketPtr = currBucket;
1116 currBucket->
link = NULL;
1158 void *existingEntry,
1159 const void *newKeyPtr)
1175 #ifdef HASH_STATISTICS
1182 elog(
ERROR,
"cannot update in frozen hashtable \"%s\"",
1192 segment_num = bucket >> hashp->
sshift;
1193 segment_ndx =
MOD(bucket, hashp->
ssize);
1195 segp = hashp->
dir[segment_num];
1200 prevBucketPtr = &segp[segment_ndx];
1201 currBucket = *prevBucketPtr;
1203 while (currBucket != NULL)
1205 if (currBucket == existingElement)
1207 prevBucketPtr = &(currBucket->
link);
1208 currBucket = *prevBucketPtr;
1211 if (currBucket == NULL)
1212 elog(
ERROR,
"hash_update_hash_key argument is not in hashtable \"%s\"",
1215 oldPrevPtr = prevBucketPtr;
1221 newhashvalue = hashp->
hash(newKeyPtr, hashp->
keysize);
1225 segment_num = newbucket >> hashp->
sshift;
1226 segment_ndx =
MOD(newbucket, hashp->
ssize);
1228 segp = hashp->
dir[segment_num];
1233 prevBucketPtr = &segp[segment_ndx];
1234 currBucket = *prevBucketPtr;
1239 match = hashp->
match;
1242 while (currBucket != NULL)
1244 if (currBucket->
hashvalue == newhashvalue &&
1245 match(
ELEMENTKEY(currBucket), newKeyPtr, keysize) == 0)
1247 prevBucketPtr = &(currBucket->
link);
1248 currBucket = *prevBucketPtr;
1249 #ifdef HASH_STATISTICS
1255 if (currBucket != NULL)
1258 currBucket = existingElement;
1267 if (bucket != newbucket)
1270 *oldPrevPtr = currBucket->
link;
1273 *prevBucketPtr = currBucket;
1274 currBucket->
link = NULL;
1306 if (newElement != NULL)
1326 int borrow_from_idx;
1332 borrow_from_idx = freelist_idx;
1336 if (borrow_from_idx == freelist_idx)
1342 if (newElement != NULL)
1423 status->
hashp = hashp;
1443 if ((curElem = status->
curEntry) != NULL)
1456 hashp = status->
hashp;
1458 ssize = hashp->
ssize;
1461 if (curBucket > max_bucket)
1470 segment_num = curBucket >> hashp->
sshift;
1471 segment_ndx =
MOD(curBucket, ssize);
1473 segp = hashp->
dir[segment_num];
1481 while ((curElem = segp[segment_ndx]) == NULL)
1484 if (++curBucket > max_bucket)
1490 if (++segment_ndx >= ssize)
1494 segp = hashp->
dir[segment_num];
1532 elog(
ERROR,
"cannot freeze hashtable \"%s\" because it has active scans",
1562 #ifdef HASH_STATISTICS
1567 new_segnum = new_bucket >> hashp->
sshift;
1568 new_segndx =
MOD(new_bucket, hashp->
ssize);
1570 if (new_segnum >= hctl->
nsegs)
1573 if (new_segnum >= hctl->
dsize)
1590 old_bucket = (new_bucket & hctl->
low_mask);
1607 old_segnum = old_bucket >> hashp->
sshift;
1608 old_segndx =
MOD(old_bucket, hashp->
ssize);
1610 old_seg = hashp->
dir[old_segnum];
1611 new_seg = hashp->
dir[new_segnum];
1613 oldlink = &old_seg[old_segndx];
1614 newlink = &new_seg[new_segndx];
1616 for (currElement = *oldlink;
1617 currElement != NULL;
1618 currElement = nextElement)
1620 nextElement = currElement->
link;
1623 *oldlink = currElement;
1624 oldlink = &currElement->
link;
1628 *newlink = currElement;
1629 newlink = &currElement->
link;
1663 memcpy(p, old_p, old_dirsize);
1664 MemSet(((
char *) p) + old_dirsize, 0, new_dirsize - old_dirsize);
1722 tmpElement = firstElement;
1723 for (
i = 0;
i < nelem;
i++)
1725 tmpElement->
link = prevElement;
1726 prevElement = tmpElement;
1727 tmpElement = (
HASHELEMENT *) (((
char *) tmpElement) + elementSize);
1766 if (num > LONG_MAX / 2)
1788 if (num > INT_MAX / 2)
1822 #define MAX_SEQ_SCANS 100
1834 elog(
ERROR,
"too many active hash_seq_search scans, cannot start one on \"%s\"",
1858 elog(
ERROR,
"no hash_seq_search scan for hash table \"%s\"",
1895 elog(
WARNING,
"leaked hash_seq_search scan for hash table %p",
1918 elog(
WARNING,
"leaked hash_seq_search scan for hash table %p",
#define MemSet(start, val, len)
void(* pg_funcptr_t)(void)
elog(ERROR, "%s: %s", p2, msg)
static HTAB * seq_scan_tables[MAX_SEQ_SCANS]
static int seq_scan_level[MAX_SEQ_SCANS]
#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)
static void hash_corrupted(HTAB *hashp)
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)
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)
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)
Assert(fmt[strlen(fmt) - 1] !='\n')
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)