PostgreSQL Source Code  git master
dynahash.c File Reference
#include "postgres.h"
#include <limits.h>
#include "access/xact.h"
#include "storage/shmem.h"
#include "storage/spin.h"
#include "utils/dynahash.h"
#include "utils/memutils.h"
Include dependency graph for dynahash.c:

Go to the source code of this file.

Data Structures

struct  FreeListData
 
struct  HASHHDR
 
struct  HTAB
 

Macros

#define DEF_SEGSIZE   256
 
#define DEF_SEGSIZE_SHIFT   8 /* must be log2(DEF_SEGSIZE) */
 
#define DEF_DIRSIZE   256
 
#define DEF_FFACTOR   1 /* default fill factor */
 
#define NUM_FREELISTS   32
 
#define IS_PARTITIONED(hctl)   ((hctl)->num_partitions != 0)
 
#define FREELIST_IDX(hctl, hashcode)   (IS_PARTITIONED(hctl) ? (hashcode) % NUM_FREELISTS : 0)
 
#define ELEMENTKEY(helem)   (((char *)(helem)) + MAXALIGN(sizeof(HASHELEMENT)))
 
#define ELEMENT_FROM_KEY(key)   ((HASHELEMENT *) (((char *) (key)) - MAXALIGN(sizeof(HASHELEMENT))))
 
#define MOD(x, y)   ((x) & ((y)-1))
 
#define MAX_SEQ_SCANS   100
 

Typedefs

typedef HASHELEMENTHASHBUCKET
 
typedef HASHBUCKETHASHSEGMENT
 

Functions

static void * DynaHashAlloc (Size size)
 
static HASHSEGMENT seg_alloc (HTAB *hashp)
 
static bool element_alloc (HTAB *hashp, int nelem, int freelist_idx)
 
static bool dir_realloc (HTAB *hashp)
 
static bool expand_table (HTAB *hashp)
 
static HASHBUCKET get_hash_entry (HTAB *hashp, int freelist_idx)
 
static void hdefault (HTAB *hashp)
 
static int choose_nelem_alloc (Size entrysize)
 
static bool init_htab (HTAB *hashp, long nelem)
 
static void hash_corrupted (HTAB *hashp)
 
static long next_pow2_long (long num)
 
static int next_pow2_int (long num)
 
static void register_seq_scan (HTAB *hashp)
 
static void deregister_seq_scan (HTAB *hashp)
 
static bool has_seq_scans (HTAB *hashp)
 
static int string_compare (const char *key1, const char *key2, Size keysize)
 
HTABhash_create (const char *tabname, long nelem, HASHCTL *info, int flags)
 
Size hash_estimate_size (long num_entries, Size entrysize)
 
long hash_select_dirsize (long num_entries)
 
Size hash_get_shared_size (HASHCTL *info, int flags)
 
void hash_destroy (HTAB *hashp)
 
void hash_stats (const char *where, HTAB *hashp)
 
uint32 get_hash_value (HTAB *hashp, const void *keyPtr)
 
static uint32 calc_bucket (HASHHDR *hctl, uint32 hash_val)
 
void * hash_search (HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
 
void * hash_search_with_hash_value (HTAB *hashp, const void *keyPtr, uint32 hashvalue, HASHACTION action, bool *foundPtr)
 
bool hash_update_hash_key (HTAB *hashp, void *existingEntry, const void *newKeyPtr)
 
long hash_get_num_entries (HTAB *hashp)
 
void hash_seq_init (HASH_SEQ_STATUS *status, HTAB *hashp)
 
void * hash_seq_search (HASH_SEQ_STATUS *status)
 
void hash_seq_term (HASH_SEQ_STATUS *status)
 
void hash_freeze (HTAB *hashp)
 
int my_log2 (long num)
 
void AtEOXact_HashTables (bool isCommit)
 
void AtEOSubXact_HashTables (bool isCommit, int nestDepth)
 

Variables

static MemoryContext CurrentDynaHashCxt = NULL
 
static HTABseq_scan_tables [MAX_SEQ_SCANS]
 
static int seq_scan_level [MAX_SEQ_SCANS]
 
static int num_seq_scans = 0
 

Macro Definition Documentation

◆ DEF_DIRSIZE

#define DEF_DIRSIZE   256

Definition at line 112 of file dynahash.c.

Referenced by hash_estimate_size(), hash_select_dirsize(), and hdefault().

◆ DEF_FFACTOR

#define DEF_FFACTOR   1 /* default fill factor */

Definition at line 113 of file dynahash.c.

Referenced by hash_estimate_size(), hash_select_dirsize(), and hdefault().

◆ DEF_SEGSIZE

#define DEF_SEGSIZE   256

Definition at line 110 of file dynahash.c.

Referenced by hash_estimate_size(), hash_select_dirsize(), and hdefault().

◆ DEF_SEGSIZE_SHIFT

#define DEF_SEGSIZE_SHIFT   8 /* must be log2(DEF_SEGSIZE) */

Definition at line 111 of file dynahash.c.

Referenced by hdefault().

◆ ELEMENT_FROM_KEY

#define ELEMENT_FROM_KEY (   key)    ((HASHELEMENT *) (((char *) (key)) - MAXALIGN(sizeof(HASHELEMENT))))

Definition at line 238 of file dynahash.c.

Referenced by hash_update_hash_key().

◆ ELEMENTKEY

#define ELEMENTKEY (   helem)    (((char *)(helem)) + MAXALIGN(sizeof(HASHELEMENT)))

Definition at line 233 of file dynahash.c.

Referenced by hash_search_with_hash_value(), hash_seq_search(), and hash_update_hash_key().

◆ FREELIST_IDX

#define FREELIST_IDX (   hctl,
  hashcode 
)    (IS_PARTITIONED(hctl) ? (hashcode) % NUM_FREELISTS : 0)

Definition at line 201 of file dynahash.c.

Referenced by hash_search_with_hash_value().

◆ IS_PARTITIONED

#define IS_PARTITIONED (   hctl)    ((hctl)->num_partitions != 0)

◆ MAX_SEQ_SCANS

#define MAX_SEQ_SCANS   100

Definition at line 1778 of file dynahash.c.

Referenced by register_seq_scan().

◆ MOD

#define MOD (   x,
 
)    ((x) & ((y)-1))

◆ NUM_FREELISTS

#define NUM_FREELISTS   32

Definition at line 116 of file dynahash.c.

Referenced by get_hash_entry(), hash_create(), hash_get_num_entries(), and init_htab().

Typedef Documentation

◆ HASHBUCKET

Definition at line 119 of file dynahash.c.

◆ HASHSEGMENT

Definition at line 122 of file dynahash.c.

Function Documentation

◆ AtEOSubXact_HashTables()

void AtEOSubXact_HashTables ( bool  isCommit,
int  nestDepth 
)

Definition at line 1860 of file dynahash.c.

References elog, i, num_seq_scans, seq_scan_level, and WARNING.

Referenced by AbortSubTransaction(), and CommitSubTransaction().

1861 {
1862  int i;
1863 
1864  /*
1865  * Search backward to make cleanup easy. Note we must check all entries,
1866  * not only those at the end of the array, because deletion technique
1867  * doesn't keep them in order.
1868  */
1869  for (i = num_seq_scans - 1; i >= 0; i--)
1870  {
1871  if (seq_scan_level[i] >= nestDepth)
1872  {
1873  if (isCommit)
1874  elog(WARNING, "leaked hash_seq_search scan for hash table %p",
1875  seq_scan_tables[i]);
1878  num_seq_scans--;
1879  }
1880  }
1881 }
static int seq_scan_level[MAX_SEQ_SCANS]
Definition: dynahash.c:1781
#define WARNING
Definition: elog.h:40
static int num_seq_scans
Definition: dynahash.c:1782
static HTAB * seq_scan_tables[MAX_SEQ_SCANS]
Definition: dynahash.c:1780
int i
#define elog
Definition: elog.h:219

◆ AtEOXact_HashTables()

void AtEOXact_HashTables ( bool  isCommit)

Definition at line 1834 of file dynahash.c.

References elog, i, num_seq_scans, and WARNING.

Referenced by AbortTransaction(), AutoVacLauncherMain(), BackgroundWriterMain(), CheckpointerMain(), CommitTransaction(), PrepareTransaction(), and WalWriterMain().

1835 {
1836  /*
1837  * During abort cleanup, open scans are expected; just silently clean 'em
1838  * out. An open scan at commit means someone forgot a hash_seq_term()
1839  * call, so complain.
1840  *
1841  * Note: it's tempting to try to print the tabname here, but refrain for
1842  * fear of touching deallocated memory. This isn't a user-facing message
1843  * anyway, so it needn't be pretty.
1844  */
1845  if (isCommit)
1846  {
1847  int i;
1848 
1849  for (i = 0; i < num_seq_scans; i++)
1850  {
1851  elog(WARNING, "leaked hash_seq_search scan for hash table %p",
1852  seq_scan_tables[i]);
1853  }
1854  }
1855  num_seq_scans = 0;
1856 }
#define WARNING
Definition: elog.h:40
static int num_seq_scans
Definition: dynahash.c:1782
static HTAB * seq_scan_tables[MAX_SEQ_SCANS]
Definition: dynahash.c:1780
int i
#define elog
Definition: elog.h:219

◆ calc_bucket()

static uint32 calc_bucket ( HASHHDR hctl,
uint32  hash_val 
)
inlinestatic

Definition at line 867 of file dynahash.c.

References HASHHDR::high_mask, HASHHDR::low_mask, and HASHHDR::max_bucket.

Referenced by expand_table(), hash_search_with_hash_value(), and hash_update_hash_key().

868 {
869  uint32 bucket;
870 
871  bucket = hash_val & hctl->high_mask;
872  if (bucket > hctl->max_bucket)
873  bucket = bucket & hctl->low_mask;
874 
875  return bucket;
876 }
uint32 max_bucket
Definition: dynahash.c:174
uint32 low_mask
Definition: dynahash.c:176
unsigned int uint32
Definition: c.h:325
uint32 high_mask
Definition: dynahash.c:175

◆ choose_nelem_alloc()

static int choose_nelem_alloc ( Size  entrysize)
static

Definition at line 603 of file dynahash.c.

References MAXALIGN, and HASHHDR::nelem_alloc.

Referenced by hash_estimate_size(), and init_htab().

604 {
605  int nelem_alloc;
606  Size elementSize;
607  Size allocSize;
608 
609  /* Each element has a HASHELEMENT header plus user data. */
610  /* NB: this had better match element_alloc() */
611  elementSize = MAXALIGN(sizeof(HASHELEMENT)) + MAXALIGN(entrysize);
612 
613  /*
614  * The idea here is to choose nelem_alloc at least 32, but round up so
615  * that the allocation request will be a power of 2 or just less. This
616  * makes little difference for hash tables in shared memory, but for hash
617  * tables managed by palloc, the allocation request will be rounded up to
618  * a power of 2 anyway. If we fail to take this into account, we'll waste
619  * as much as half the allocated space.
620  */
621  allocSize = 32 * 4; /* assume elementSize at least 8 */
622  do
623  {
624  allocSize <<= 1;
625  nelem_alloc = allocSize / elementSize;
626  } while (nelem_alloc < 32);
627 
628  return nelem_alloc;
629 }
size_t Size
Definition: c.h:433
#define MAXALIGN(LEN)
Definition: c.h:652

◆ deregister_seq_scan()

static void deregister_seq_scan ( HTAB hashp)
static

Definition at line 1799 of file dynahash.c.

References elog, ERROR, i, num_seq_scans, seq_scan_level, and HTAB::tabname.

Referenced by hash_seq_term().

1800 {
1801  int i;
1802 
1803  /* Search backward since it's most likely at the stack top */
1804  for (i = num_seq_scans - 1; i >= 0; i--)
1805  {
1806  if (seq_scan_tables[i] == hashp)
1807  {
1810  num_seq_scans--;
1811  return;
1812  }
1813  }
1814  elog(ERROR, "no hash_seq_search scan for hash table \"%s\"",
1815  hashp->tabname);
1816 }
static int seq_scan_level[MAX_SEQ_SCANS]
Definition: dynahash.c:1781
#define ERROR
Definition: elog.h:43
static int num_seq_scans
Definition: dynahash.c:1782
static HTAB * seq_scan_tables[MAX_SEQ_SCANS]
Definition: dynahash.c:1780
char * tabname
Definition: dynahash.c:217
int i
#define elog
Definition: elog.h:219

◆ dir_realloc()

static bool dir_realloc ( HTAB hashp)
static

Definition at line 1599 of file dynahash.c.

References HTAB::alloc, Assert, HTAB::dir, HASHHDR::dsize, DynaHashAlloc(), HTAB::hctl, HTAB::hcxt, HASHHDR::max_dsize, MemSet, NO_MAX_DSIZE, and pfree().

Referenced by expand_table().

1600 {
1601  HASHSEGMENT *p;
1602  HASHSEGMENT *old_p;
1603  long new_dsize;
1604  long old_dirsize;
1605  long new_dirsize;
1606 
1607  if (hashp->hctl->max_dsize != NO_MAX_DSIZE)
1608  return false;
1609 
1610  /* Reallocate directory */
1611  new_dsize = hashp->hctl->dsize << 1;
1612  old_dirsize = hashp->hctl->dsize * sizeof(HASHSEGMENT);
1613  new_dirsize = new_dsize * sizeof(HASHSEGMENT);
1614 
1615  old_p = hashp->dir;
1616  CurrentDynaHashCxt = hashp->hcxt;
1617  p = (HASHSEGMENT *) hashp->alloc((Size) new_dirsize);
1618 
1619  if (p != NULL)
1620  {
1621  memcpy(p, old_p, old_dirsize);
1622  MemSet(((char *) p) + old_dirsize, 0, new_dirsize - old_dirsize);
1623  hashp->dir = p;
1624  hashp->hctl->dsize = new_dsize;
1625 
1626  /* XXX assume the allocator is palloc, so we know how to free */
1627  Assert(hashp->alloc == DynaHashAlloc);
1628  pfree(old_p);
1629 
1630  return true;
1631  }
1632 
1633  return false;
1634 }
long dsize
Definition: dynahash.c:172
#define NO_MAX_DSIZE
Definition: hsearch.h:100
HashAllocFunc alloc
Definition: dynahash.c:215
#define MemSet(start, val, len)
Definition: c.h:908
void pfree(void *pointer)
Definition: mcxt.c:1031
HASHBUCKET * HASHSEGMENT
Definition: dynahash.c:122
long max_dsize
Definition: dynahash.c:183
HASHHDR * hctl
Definition: dynahash.c:210
MemoryContext hcxt
Definition: dynahash.c:216
#define Assert(condition)
Definition: c.h:699
size_t Size
Definition: c.h:433
static void * DynaHashAlloc(Size size)
Definition: dynahash.c:278
static MemoryContext CurrentDynaHashCxt
Definition: dynahash.c:275
HASHSEGMENT * dir
Definition: dynahash.c:211

◆ DynaHashAlloc()

static void * DynaHashAlloc ( Size  size)
static

Definition at line 278 of file dynahash.c.

References Assert, MemoryContextAlloc(), and MemoryContextIsValid.

Referenced by dir_realloc(), hash_create(), hash_destroy(), and hash_search_with_hash_value().

279 {
282 }
#define MemoryContextIsValid(context)
Definition: memnodes.h:101
#define Assert(condition)
Definition: c.h:699
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:771
static MemoryContext CurrentDynaHashCxt
Definition: dynahash.c:275

◆ element_alloc()

static bool element_alloc ( HTAB hashp,
int  nelem,
int  freelist_idx 
)
static

Definition at line 1657 of file dynahash.c.

References HTAB::alloc, HASHHDR::entrysize, FreeListData::freeList, HASHHDR::freeList, HTAB::hctl, HTAB::hcxt, i, IS_PARTITIONED, HTAB::isfixed, HASHELEMENT::link, MAXALIGN, FreeListData::mutex, SpinLockAcquire, and SpinLockRelease.

Referenced by get_hash_entry(), and hash_create().

1658 {
1659  HASHHDR *hctl = hashp->hctl;
1660  Size elementSize;
1661  HASHELEMENT *firstElement;
1662  HASHELEMENT *tmpElement;
1663  HASHELEMENT *prevElement;
1664  int i;
1665 
1666  if (hashp->isfixed)
1667  return false;
1668 
1669  /* Each element has a HASHELEMENT header plus user data. */
1670  elementSize = MAXALIGN(sizeof(HASHELEMENT)) + MAXALIGN(hctl->entrysize);
1671 
1672  CurrentDynaHashCxt = hashp->hcxt;
1673  firstElement = (HASHELEMENT *) hashp->alloc(nelem * elementSize);
1674 
1675  if (!firstElement)
1676  return false;
1677 
1678  /* prepare to link all the new entries into the freelist */
1679  prevElement = NULL;
1680  tmpElement = firstElement;
1681  for (i = 0; i < nelem; i++)
1682  {
1683  tmpElement->link = prevElement;
1684  prevElement = tmpElement;
1685  tmpElement = (HASHELEMENT *) (((char *) tmpElement) + elementSize);
1686  }
1687 
1688  /* if partitioned, must lock to touch freeList */
1689  if (IS_PARTITIONED(hctl))
1690  SpinLockAcquire(&hctl->freeList[freelist_idx].mutex);
1691 
1692  /* freelist could be nonempty if two backends did this concurrently */
1693  firstElement->link = hctl->freeList[freelist_idx].freeList;
1694  hctl->freeList[freelist_idx].freeList = prevElement;
1695 
1696  if (IS_PARTITIONED(hctl))
1697  SpinLockRelease(&hctl->freeList[freelist_idx].mutex);
1698 
1699  return true;
1700 }
Size entrysize
Definition: dynahash.c:180
#define IS_PARTITIONED(hctl)
Definition: dynahash.c:199
HashAllocFunc alloc
Definition: dynahash.c:215
slock_t mutex
Definition: dynahash.c:143
#define SpinLockAcquire(lock)
Definition: spin.h:62
HASHHDR * hctl
Definition: dynahash.c:210
#define SpinLockRelease(lock)
Definition: spin.h:64
FreeListData freeList[NUM_FREELISTS]
Definition: dynahash.c:168
MemoryContext hcxt
Definition: dynahash.c:216
size_t Size
Definition: c.h:433
#define MAXALIGN(LEN)
Definition: c.h:652
bool isfixed
Definition: dynahash.c:219
struct HASHELEMENT * link
Definition: hsearch.h:53
static MemoryContext CurrentDynaHashCxt
Definition: dynahash.c:275
int i
HASHELEMENT * freeList
Definition: dynahash.c:145

◆ expand_table()

static bool expand_table ( HTAB hashp)
static

Definition at line 1502 of file dynahash.c.

References Assert, calc_bucket(), HTAB::dir, dir_realloc(), HASHHDR::dsize, HASHELEMENT::hashvalue, HTAB::hctl, HASHHDR::high_mask, IS_PARTITIONED, HASHELEMENT::link, HASHHDR::low_mask, HASHHDR::max_bucket, MOD, HASHHDR::nsegs, seg_alloc(), HTAB::sshift, and HTAB::ssize.

Referenced by hash_search_with_hash_value().

1503 {
1504  HASHHDR *hctl = hashp->hctl;
1505  HASHSEGMENT old_seg,
1506  new_seg;
1507  long old_bucket,
1508  new_bucket;
1509  long new_segnum,
1510  new_segndx;
1511  long old_segnum,
1512  old_segndx;
1513  HASHBUCKET *oldlink,
1514  *newlink;
1515  HASHBUCKET currElement,
1516  nextElement;
1517 
1518  Assert(!IS_PARTITIONED(hctl));
1519 
1520 #ifdef HASH_STATISTICS
1521  hash_expansions++;
1522 #endif
1523 
1524  new_bucket = hctl->max_bucket + 1;
1525  new_segnum = new_bucket >> hashp->sshift;
1526  new_segndx = MOD(new_bucket, hashp->ssize);
1527 
1528  if (new_segnum >= hctl->nsegs)
1529  {
1530  /* Allocate new segment if necessary -- could fail if dir full */
1531  if (new_segnum >= hctl->dsize)
1532  if (!dir_realloc(hashp))
1533  return false;
1534  if (!(hashp->dir[new_segnum] = seg_alloc(hashp)))
1535  return false;
1536  hctl->nsegs++;
1537  }
1538 
1539  /* OK, we created a new bucket */
1540  hctl->max_bucket++;
1541 
1542  /*
1543  * *Before* changing masks, find old bucket corresponding to same hash
1544  * values; values in that bucket may need to be relocated to new bucket.
1545  * Note that new_bucket is certainly larger than low_mask at this point,
1546  * so we can skip the first step of the regular hash mask calc.
1547  */
1548  old_bucket = (new_bucket & hctl->low_mask);
1549 
1550  /*
1551  * If we crossed a power of 2, readjust masks.
1552  */
1553  if ((uint32) new_bucket > hctl->high_mask)
1554  {
1555  hctl->low_mask = hctl->high_mask;
1556  hctl->high_mask = (uint32) new_bucket | hctl->low_mask;
1557  }
1558 
1559  /*
1560  * Relocate records to the new bucket. NOTE: because of the way the hash
1561  * masking is done in calc_bucket, only one old bucket can need to be
1562  * split at this point. With a different way of reducing the hash value,
1563  * that might not be true!
1564  */
1565  old_segnum = old_bucket >> hashp->sshift;
1566  old_segndx = MOD(old_bucket, hashp->ssize);
1567 
1568  old_seg = hashp->dir[old_segnum];
1569  new_seg = hashp->dir[new_segnum];
1570 
1571  oldlink = &old_seg[old_segndx];
1572  newlink = &new_seg[new_segndx];
1573 
1574  for (currElement = *oldlink;
1575  currElement != NULL;
1576  currElement = nextElement)
1577  {
1578  nextElement = currElement->link;
1579  if ((long) calc_bucket(hctl, currElement->hashvalue) == old_bucket)
1580  {
1581  *oldlink = currElement;
1582  oldlink = &currElement->link;
1583  }
1584  else
1585  {
1586  *newlink = currElement;
1587  newlink = &currElement->link;
1588  }
1589  }
1590  /* don't forget to terminate the rebuilt hash chains... */
1591  *oldlink = NULL;
1592  *newlink = NULL;
1593 
1594  return true;
1595 }
long dsize
Definition: dynahash.c:172
uint32 max_bucket
Definition: dynahash.c:174
static uint32 calc_bucket(HASHHDR *hctl, uint32 hash_val)
Definition: dynahash.c:867
#define IS_PARTITIONED(hctl)
Definition: dynahash.c:199
uint32 low_mask
Definition: dynahash.c:176
long ssize
Definition: dynahash.c:226
int sshift
Definition: dynahash.c:227
#define MOD(x, y)
Definition: dynahash.c:244
HASHHDR * hctl
Definition: dynahash.c:210
unsigned int uint32
Definition: c.h:325
uint32 high_mask
Definition: dynahash.c:175
#define Assert(condition)
Definition: c.h:699
static HASHSEGMENT seg_alloc(HTAB *hashp)
Definition: dynahash.c:1638
struct HASHELEMENT * link
Definition: hsearch.h:53
HASHSEGMENT * dir
Definition: dynahash.c:211
static bool dir_realloc(HTAB *hashp)
Definition: dynahash.c:1599
uint32 hashvalue
Definition: hsearch.h:54
long nsegs
Definition: dynahash.c:173

◆ get_hash_entry()

static HASHBUCKET get_hash_entry ( HTAB hashp,
int  freelist_idx 
)
static

Definition at line 1250 of file dynahash.c.

References element_alloc(), FreeListData::freeList, HASHHDR::freeList, HTAB::hctl, IS_PARTITIONED, HASHELEMENT::link, FreeListData::mutex, HASHHDR::nelem_alloc, FreeListData::nentries, NUM_FREELISTS, SpinLockAcquire, and SpinLockRelease.

Referenced by hash_search_with_hash_value().

1251 {
1252  HASHHDR *hctl = hashp->hctl;
1253  HASHBUCKET newElement;
1254 
1255  for (;;)
1256  {
1257  /* if partitioned, must lock to touch nentries and freeList */
1258  if (IS_PARTITIONED(hctl))
1259  SpinLockAcquire(&hctl->freeList[freelist_idx].mutex);
1260 
1261  /* try to get an entry from the freelist */
1262  newElement = hctl->freeList[freelist_idx].freeList;
1263 
1264  if (newElement != NULL)
1265  break;
1266 
1267  if (IS_PARTITIONED(hctl))
1268  SpinLockRelease(&hctl->freeList[freelist_idx].mutex);
1269 
1270  /*
1271  * No free elements in this freelist. In a partitioned table, there
1272  * might be entries in other freelists, but to reduce contention we
1273  * prefer to first try to get another chunk of buckets from the main
1274  * shmem allocator. If that fails, though, we *MUST* root through all
1275  * the other freelists before giving up. There are multiple callers
1276  * that assume that they can allocate every element in the initially
1277  * requested table size, or that deleting an element guarantees they
1278  * can insert a new element, even if shared memory is entirely full.
1279  * Failing because the needed element is in a different freelist is
1280  * not acceptable.
1281  */
1282  if (!element_alloc(hashp, hctl->nelem_alloc, freelist_idx))
1283  {
1284  int borrow_from_idx;
1285 
1286  if (!IS_PARTITIONED(hctl))
1287  return NULL; /* out of memory */
1288 
1289  /* try to borrow element from another freelist */
1290  borrow_from_idx = freelist_idx;
1291  for (;;)
1292  {
1293  borrow_from_idx = (borrow_from_idx + 1) % NUM_FREELISTS;
1294  if (borrow_from_idx == freelist_idx)
1295  break; /* examined all freelists, fail */
1296 
1297  SpinLockAcquire(&(hctl->freeList[borrow_from_idx].mutex));
1298  newElement = hctl->freeList[borrow_from_idx].freeList;
1299 
1300  if (newElement != NULL)
1301  {
1302  hctl->freeList[borrow_from_idx].freeList = newElement->link;
1303  SpinLockRelease(&(hctl->freeList[borrow_from_idx].mutex));
1304 
1305  /* careful: count the new element in its proper freelist */
1306  SpinLockAcquire(&hctl->freeList[freelist_idx].mutex);
1307  hctl->freeList[freelist_idx].nentries++;
1308  SpinLockRelease(&hctl->freeList[freelist_idx].mutex);
1309 
1310  return newElement;
1311  }
1312 
1313  SpinLockRelease(&(hctl->freeList[borrow_from_idx].mutex));
1314  }
1315 
1316  /* no elements available to borrow either, so out of memory */
1317  return NULL;
1318  }
1319  }
1320 
1321  /* remove entry from freelist, bump nentries */
1322  hctl->freeList[freelist_idx].freeList = newElement->link;
1323  hctl->freeList[freelist_idx].nentries++;
1324 
1325  if (IS_PARTITIONED(hctl))
1326  SpinLockRelease(&hctl->freeList[freelist_idx].mutex);
1327 
1328  return newElement;
1329 }
#define IS_PARTITIONED(hctl)
Definition: dynahash.c:199
#define NUM_FREELISTS
Definition: dynahash.c:116
slock_t mutex
Definition: dynahash.c:143
#define SpinLockAcquire(lock)
Definition: spin.h:62
HASHHDR * hctl
Definition: dynahash.c:210
long nentries
Definition: dynahash.c:144
#define SpinLockRelease(lock)
Definition: spin.h:64
FreeListData freeList[NUM_FREELISTS]
Definition: dynahash.c:168
int nelem_alloc
Definition: dynahash.c:186
struct HASHELEMENT * link
Definition: hsearch.h:53
HASHELEMENT * freeList
Definition: dynahash.c:145
static bool element_alloc(HTAB *hashp, int nelem, int freelist_idx)
Definition: dynahash.c:1657

◆ get_hash_value()

uint32 get_hash_value ( HTAB hashp,
const void *  keyPtr 
)

Definition at line 860 of file dynahash.c.

References HTAB::hash, and HTAB::keysize.

Referenced by BufTableHashCode(), and LockTagHashCode().

861 {
862  return hashp->hash(keyPtr, hashp->keysize);
863 }
Size keysize
Definition: dynahash.c:225
HashValueFunc hash
Definition: dynahash.c:212

◆ has_seq_scans()

static bool has_seq_scans ( HTAB hashp)
static

Definition at line 1820 of file dynahash.c.

References i, and num_seq_scans.

Referenced by hash_freeze(), and hash_search_with_hash_value().

1821 {
1822  int i;
1823 
1824  for (i = 0; i < num_seq_scans; i++)
1825  {
1826  if (seq_scan_tables[i] == hashp)
1827  return true;
1828  }
1829  return false;
1830 }
static int num_seq_scans
Definition: dynahash.c:1782
static HTAB * seq_scan_tables[MAX_SEQ_SCANS]
Definition: dynahash.c:1780
int i

◆ hash_corrupted()

static void hash_corrupted ( HTAB hashp)
static

Definition at line 1704 of file dynahash.c.

References elog, FATAL, HTAB::isshared, PANIC, and HTAB::tabname.

Referenced by hash_search_with_hash_value(), and hash_update_hash_key().

1705 {
1706  /*
1707  * If the corruption is in a shared hashtable, we'd better force a
1708  * systemwide restart. Otherwise, just shut down this one backend.
1709  */
1710  if (hashp->isshared)
1711  elog(PANIC, "hash table \"%s\" corrupted", hashp->tabname);
1712  else
1713  elog(FATAL, "hash table \"%s\" corrupted", hashp->tabname);
1714 }
#define PANIC
Definition: elog.h:53
#define FATAL
Definition: elog.h:52
bool isshared
Definition: dynahash.c:218
char * tabname
Definition: dynahash.c:217
#define elog
Definition: elog.h:219

◆ hash_create()

HTAB* hash_create ( const char *  tabname,
long  nelem,
HASHCTL info,
int  flags 
)

Definition at line 316 of file dynahash.c.

References HASHCTL::alloc, HTAB::alloc, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, CurrentDynaHashCxt, HTAB::dir, HASHCTL::dsize, HASHHDR::dsize, DynaHashAlloc(), element_alloc(), elog, HASHCTL::entrysize, HASHHDR::entrysize, ereport, errcode(), errmsg(), ERROR, HASHCTL::ffactor, HASHHDR::ffactor, HTAB::frozen, HASHCTL::hash, HTAB::hash, HASH_ALLOC, HASH_ATTACH, HASH_BLOBS, HASH_COMPARE, HASH_CONTEXT, HASH_DIRSIZE, HASH_ELEM, HASH_FFACTOR, HASH_FIXED_SIZE, HASH_FUNCTION, HASH_KEYCOPY, HASH_PARTITION, HASH_SEGMENT, HASH_SHARED_MEM, HASHCTL::hctl, HTAB::hctl, HASHCTL::hcxt, HTAB::hcxt, hdefault(), i, init_htab(), IS_PARTITIONED, HTAB::isfixed, HTAB::isshared, HASHCTL::keycopy, HTAB::keycopy, HASHCTL::keysize, HASHHDR::keysize, HTAB::keysize, HASHCTL::match, HTAB::match, HASHCTL::max_dsize, HASHHDR::max_dsize, MemoryContextSetIdentifier(), MemSet, my_log2(), HASHHDR::nelem_alloc, next_pow2_int(), NUM_FREELISTS, HASHCTL::num_partitions, HASHHDR::num_partitions, HASHHDR::sshift, HTAB::sshift, HASHCTL::ssize, HASHHDR::ssize, HTAB::ssize, string_compare(), string_hash(), strlcpy(), HTAB::tabname, tag_hash(), TopMemoryContext, and uint32_hash().

Referenced by _hash_finish_split(), _PG_init(), assign_record_type_typmod(), begin_heap_rewrite(), build_join_rel_hash(), BuildEventTriggerCache(), CompactCheckpointerRequestQueue(), compute_array_stats(), compute_tsvector_stats(), create_seq_hashtable(), createConnHash(), do_autovacuum(), EnablePortalManager(), find_all_inheritors(), find_oper_cache_entry(), find_rendezvous_variable(), get_json_object_as_hash(), get_tabstat_entry(), GetComboCommandId(), GetConnection(), GetSerializableTransactionSnapshotInt(), gistInitBuildBuffers(), gistInitParentMap(), init_procedure_caches(), init_rel_sync_cache(), init_timezone_hashtable(), init_ts_config_cache(), InitBufferPoolAccess(), InitializeAttoptCache(), InitializeRelfilenodeMap(), InitializeShippableCache(), InitializeTableSpaceCache(), InitLocalBuffers(), InitLocks(), InitQueryHashTable(), load_categories_hash(), log_invalid_page(), logical_begin_heap_rewrite(), logicalrep_relmap_init(), lookup_collation_cache(), lookup_proof_cache(), lookup_ts_dictionary_cache(), lookup_ts_parser_cache(), lookup_type_cache(), LookupOpclassInfo(), mdinit(), pgstat_collect_oids(), pgstat_init_function_usage(), pgstat_read_statsfiles(), plpgsql_estate_setup(), plpgsql_HashTableInit(), PLy_add_exceptions(), populate_recordset_object_start(), process_syncing_tables_for_apply(), rebuild_database_list(), record_C_func(), RegisterExtensibleNodeEntry(), RelationCacheInitialize(), ReorderBufferAllocate(), ReorderBufferBuildTupleCidHash(), ReorderBufferToastInitHash(), reset_dbentry_counters(), ResetUnloggedRelationsInDbspaceDir(), ri_InitHashTables(), select_perl_context(), set_rtable_names(), ShmemInitHash(), smgropen(), and transformGraph().

317 {
318  HTAB *hashp;
319  HASHHDR *hctl;
320 
321  /*
322  * For shared hash tables, we have a local hash header (HTAB struct) that
323  * we allocate in TopMemoryContext; all else is in shared memory.
324  *
325  * For non-shared hash tables, everything including the hash header is in
326  * a memory context created specially for the hash table --- this makes
327  * hash_destroy very simple. The memory context is made a child of either
328  * a context specified by the caller, or TopMemoryContext if nothing is
329  * specified.
330  */
331  if (flags & HASH_SHARED_MEM)
332  {
333  /* Set up to allocate the hash header */
335  }
336  else
337  {
338  /* Create the hash table's private memory context */
339  if (flags & HASH_CONTEXT)
340  CurrentDynaHashCxt = info->hcxt;
341  else
344  "dynahash",
346  }
347 
348  /* Initialize the hash header, plus a copy of the table name */
349  hashp = (HTAB *) DynaHashAlloc(sizeof(HTAB) + strlen(tabname) + 1);
350  MemSet(hashp, 0, sizeof(HTAB));
351 
352  hashp->tabname = (char *) (hashp + 1);
353  strcpy(hashp->tabname, tabname);
354 
355  /* If we have a private context, label it with hashtable's name */
356  if (!(flags & HASH_SHARED_MEM))
358 
359  /*
360  * Select the appropriate hash function (see comments at head of file).
361  */
362  if (flags & HASH_FUNCTION)
363  hashp->hash = info->hash;
364  else if (flags & HASH_BLOBS)
365  {
366  /* We can optimize hashing for common key sizes */
367  Assert(flags & HASH_ELEM);
368  if (info->keysize == sizeof(uint32))
369  hashp->hash = uint32_hash;
370  else
371  hashp->hash = tag_hash;
372  }
373  else
374  hashp->hash = string_hash; /* default hash function */
375 
376  /*
377  * If you don't specify a match function, it defaults to string_compare if
378  * you used string_hash (either explicitly or by default) and to memcmp
379  * otherwise.
380  *
381  * Note: explicitly specifying string_hash is deprecated, because this
382  * might not work for callers in loadable modules on some platforms due to
383  * referencing a trampoline instead of the string_hash function proper.
384  * Just let it default, eh?
385  */
386  if (flags & HASH_COMPARE)
387  hashp->match = info->match;
388  else if (hashp->hash == string_hash)
390  else
391  hashp->match = memcmp;
392 
393  /*
394  * Similarly, the key-copying function defaults to strlcpy or memcpy.
395  */
396  if (flags & HASH_KEYCOPY)
397  hashp->keycopy = info->keycopy;
398  else if (hashp->hash == string_hash)
399  hashp->keycopy = (HashCopyFunc) strlcpy;
400  else
401  hashp->keycopy = memcpy;
402 
403  /* And select the entry allocation function, too. */
404  if (flags & HASH_ALLOC)
405  hashp->alloc = info->alloc;
406  else
407  hashp->alloc = DynaHashAlloc;
408 
409  if (flags & HASH_SHARED_MEM)
410  {
411  /*
412  * ctl structure and directory are preallocated for shared memory
413  * tables. Note that HASH_DIRSIZE and HASH_ALLOC had better be set as
414  * well.
415  */
416  hashp->hctl = info->hctl;
417  hashp->dir = (HASHSEGMENT *) (((char *) info->hctl) + sizeof(HASHHDR));
418  hashp->hcxt = NULL;
419  hashp->isshared = true;
420 
421  /* hash table already exists, we're just attaching to it */
422  if (flags & HASH_ATTACH)
423  {
424  /* make local copies of some heavily-used values */
425  hctl = hashp->hctl;
426  hashp->keysize = hctl->keysize;
427  hashp->ssize = hctl->ssize;
428  hashp->sshift = hctl->sshift;
429 
430  return hashp;
431  }
432  }
433  else
434  {
435  /* setup hash table defaults */
436  hashp->hctl = NULL;
437  hashp->dir = NULL;
438  hashp->hcxt = CurrentDynaHashCxt;
439  hashp->isshared = false;
440  }
441 
442  if (!hashp->hctl)
443  {
444  hashp->hctl = (HASHHDR *) hashp->alloc(sizeof(HASHHDR));
445  if (!hashp->hctl)
446  ereport(ERROR,
447  (errcode(ERRCODE_OUT_OF_MEMORY),
448  errmsg("out of memory")));
449  }
450 
451  hashp->frozen = false;
452 
453  hdefault(hashp);
454 
455  hctl = hashp->hctl;
456 
457  if (flags & HASH_PARTITION)
458  {
459  /* Doesn't make sense to partition a local hash table */
460  Assert(flags & HASH_SHARED_MEM);
461 
462  /*
463  * The number of partitions had better be a power of 2. Also, it must
464  * be less than INT_MAX (see init_htab()), so call the int version of
465  * next_pow2.
466  */
468 
469  hctl->num_partitions = info->num_partitions;
470  }
471 
472  if (flags & HASH_SEGMENT)
473  {
474  hctl->ssize = info->ssize;
475  hctl->sshift = my_log2(info->ssize);
476  /* ssize had better be a power of 2 */
477  Assert(hctl->ssize == (1L << hctl->sshift));
478  }
479  if (flags & HASH_FFACTOR)
480  hctl->ffactor = info->ffactor;
481 
482  /*
483  * SHM hash tables have fixed directory size passed by the caller.
484  */
485  if (flags & HASH_DIRSIZE)
486  {
487  hctl->max_dsize = info->max_dsize;
488  hctl->dsize = info->dsize;
489  }
490 
491  /*
492  * hash table now allocates space for key and data but you have to say how
493  * much space to allocate
494  */
495  if (flags & HASH_ELEM)
496  {
497  Assert(info->entrysize >= info->keysize);
498  hctl->keysize = info->keysize;
499  hctl->entrysize = info->entrysize;
500  }
501 
502  /* make local copies of heavily-used constant fields */
503  hashp->keysize = hctl->keysize;
504  hashp->ssize = hctl->ssize;
505  hashp->sshift = hctl->sshift;
506 
507  /* Build the hash directory structure */
508  if (!init_htab(hashp, nelem))
509  elog(ERROR, "failed to initialize hash table \"%s\"", hashp->tabname);
510 
511  /*
512  * For a shared hash table, preallocate the requested number of elements.
513  * This reduces problems with run-time out-of-shared-memory conditions.
514  *
515  * For a non-shared hash table, preallocate the requested number of
516  * elements if it's less than our chosen nelem_alloc. This avoids wasting
517  * space if the caller correctly estimates a small table size.
518  */
519  if ((flags & HASH_SHARED_MEM) ||
520  nelem < hctl->nelem_alloc)
521  {
522  int i,
523  freelist_partitions,
524  nelem_alloc,
525  nelem_alloc_first;
526 
527  /*
528  * If hash table is partitioned, give each freelist an equal share of
529  * the initial allocation. Otherwise only freeList[0] is used.
530  */
531  if (IS_PARTITIONED(hashp->hctl))
532  freelist_partitions = NUM_FREELISTS;
533  else
534  freelist_partitions = 1;
535 
536  nelem_alloc = nelem / freelist_partitions;
537  if (nelem_alloc <= 0)
538  nelem_alloc = 1;
539 
540  /*
541  * Make sure we'll allocate all the requested elements; freeList[0]
542  * gets the excess if the request isn't divisible by NUM_FREELISTS.
543  */
544  if (nelem_alloc * freelist_partitions < nelem)
545  nelem_alloc_first =
546  nelem - nelem_alloc * (freelist_partitions - 1);
547  else
548  nelem_alloc_first = nelem_alloc;
549 
550  for (i = 0; i < freelist_partitions; i++)
551  {
552  int temp = (i == 0) ? nelem_alloc_first : nelem_alloc;
553 
554  if (!element_alloc(hashp, temp, i))
555  ereport(ERROR,
556  (errcode(ERRCODE_OUT_OF_MEMORY),
557  errmsg("out of memory")));
558  }
559  }
560 
561  if (flags & HASH_FIXED_SIZE)
562  hashp->isfixed = true;
563  return hashp;
564 }
int(* HashCompareFunc)(const void *key1, const void *key2, Size keysize)
Definition: hsearch.h:29
Size keysize
Definition: dynahash.c:179
Size keysize
Definition: dynahash.c:225
long dsize
Definition: dynahash.c:172
#define HASH_CONTEXT
Definition: hsearch.h:93
#define HASH_ELEM
Definition: hsearch.h:87
MemoryContext hcxt
Definition: hsearch.h:78
long ssize
Definition: hsearch.h:68
HashCopyFunc keycopy
Definition: hsearch.h:76
Size entrysize
Definition: dynahash.c:180
#define IS_PARTITIONED(hctl)
Definition: dynahash.c:199
uint32 string_hash(const void *key, Size keysize)
Definition: hashfn.c:34
long num_partitions
Definition: dynahash.c:181
Size entrysize
Definition: hsearch.h:73
#define NUM_FREELISTS
Definition: dynahash.c:116
int errcode(int sqlerrcode)
Definition: elog.c:575
HashAllocFunc alloc
Definition: dynahash.c:215
#define MemSet(start, val, len)
Definition: c.h:908
#define HASH_SHARED_MEM
Definition: hsearch.h:94
static int string_compare(const char *key1, const char *key2, Size keysize)
Definition: dynahash.c:293
long dsize
Definition: hsearch.h:69
uint32 uint32_hash(const void *key, Size keysize)
Definition: hashfn.c:64
#define HASH_PARTITION
Definition: hsearch.h:83
#define HASH_ATTACH
Definition: hsearch.h:95
long ffactor
Definition: dynahash.c:182
HashValueFunc hash
Definition: dynahash.c:212
Definition: dynahash.c:208
long max_dsize
Definition: hsearch.h:70
long ssize
Definition: dynahash.c:226
#define ERROR
Definition: elog.h:43
long num_partitions
Definition: hsearch.h:67
int sshift
Definition: dynahash.c:227
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
HashAllocFunc alloc
Definition: hsearch.h:77
#define HASH_KEYCOPY
Definition: hsearch.h:91
long max_dsize
Definition: dynahash.c:183
static void hdefault(HTAB *hashp)
Definition: dynahash.c:570
HASHHDR * hctl
Definition: dynahash.c:210
unsigned int uint32
Definition: c.h:325
static bool init_htab(HTAB *hashp, long nelem)
Definition: dynahash.c:636
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:44
int my_log2(long num)
Definition: dynahash.c:1718
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
HashCompareFunc match
Definition: dynahash.c:213
static int next_pow2_int(long num)
Definition: dynahash.c:1742
bool isshared
Definition: dynahash.c:218
#define HASH_BLOBS
Definition: hsearch.h:88
bool frozen
Definition: dynahash.c:222
uint32 tag_hash(const void *key, Size keysize)
Definition: hashfn.c:52
long ssize
Definition: dynahash.c:184
Size keysize
Definition: hsearch.h:72
void *(* HashCopyFunc)(void *dest, const void *src, Size keysize)
Definition: hsearch.h:37
HashCompareFunc match
Definition: hsearch.h:75
MemoryContext hcxt
Definition: dynahash.c:216
HashCopyFunc keycopy
Definition: dynahash.c:214
#define HASH_SEGMENT
Definition: hsearch.h:84
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define Assert(condition)
Definition: c.h:699
#define HASH_COMPARE
Definition: hsearch.h:90
void MemoryContextSetIdentifier(MemoryContext context, const char *id)
Definition: mcxt.c:329
HASHHDR * hctl
Definition: hsearch.h:79
long ffactor
Definition: hsearch.h:71
char * tabname
Definition: dynahash.c:217
#define HASH_FIXED_SIZE
Definition: hsearch.h:96
bool isfixed
Definition: dynahash.c:219
#define HASH_FFACTOR
Definition: hsearch.h:86
static void * DynaHashAlloc(Size size)
Definition: dynahash.c:278
#define HASH_DIRSIZE
Definition: hsearch.h:85
int errmsg(const char *fmt,...)
Definition: elog.c:797
static MemoryContext CurrentDynaHashCxt
Definition: dynahash.c:275
int i
#define HASH_ALLOC
Definition: hsearch.h:92
HASHSEGMENT * dir
Definition: dynahash.c:211
#define elog
Definition: elog.h:219
static bool element_alloc(HTAB *hashp, int nelem, int freelist_idx)
Definition: dynahash.c:1657
int sshift
Definition: dynahash.c:185
HashValueFunc hash
Definition: hsearch.h:74
#define HASH_FUNCTION
Definition: hsearch.h:89

◆ hash_destroy()

void hash_destroy ( HTAB hashp)

Definition at line 814 of file dynahash.c.

References HTAB::alloc, Assert, DynaHashAlloc(), hash_stats(), HTAB::hcxt, and MemoryContextDelete().

Referenced by _hash_finish_split(), clear_external_function_hash(), CompactCheckpointerRequestQueue(), find_all_inheritors(), InitLocks(), pgoutput_shutdown(), pgstat_recv_dropdb(), pgstat_recv_resetcounter(), pgstat_report_stat(), pgstat_vacuum_stat(), populate_recordset_object_end(), PostPrepare_PredicateLocks(), process_syncing_tables_for_apply(), ReleasePredicateLocks(), ReorderBufferReturnTXN(), ReorderBufferToastReset(), ResetSequenceCaches(), ResetUnloggedRelationsInDbspaceDir(), set_rtable_names(), SetForwardFsyncRequests(), and XLogCheckInvalidPages().

815 {
816  if (hashp != NULL)
817  {
818  /* allocation method must be one we know how to free, too */
819  Assert(hashp->alloc == DynaHashAlloc);
820  /* so this hashtable must have it's own context */
821  Assert(hashp->hcxt != NULL);
822 
823  hash_stats("destroy", hashp);
824 
825  /*
826  * Free everything by destroying the hash table's memory context.
827  */
828  MemoryContextDelete(hashp->hcxt);
829  }
830 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
void hash_stats(const char *where, HTAB *hashp)
Definition: dynahash.c:833
HashAllocFunc alloc
Definition: dynahash.c:215
MemoryContext hcxt
Definition: dynahash.c:216
#define Assert(condition)
Definition: c.h:699
static void * DynaHashAlloc(Size size)
Definition: dynahash.c:278

◆ hash_estimate_size()

Size hash_estimate_size ( long  num_entries,
Size  entrysize 
)

Definition at line 732 of file dynahash.c.

References add_size(), choose_nelem_alloc(), DEF_DIRSIZE, DEF_FFACTOR, DEF_SEGSIZE, MAXALIGN, mul_size(), and next_pow2_long().

Referenced by BufTableShmemSize(), CreateSharedMemoryAndSemaphores(), LockShmemSize(), pgss_memsize(), and PredicateLockShmemSize().

733 {
734  Size size;
735  long nBuckets,
736  nSegments,
737  nDirEntries,
738  nElementAllocs,
739  elementSize,
740  elementAllocCnt;
741 
742  /* estimate number of buckets wanted */
743  nBuckets = next_pow2_long((num_entries - 1) / DEF_FFACTOR + 1);
744  /* # of segments needed for nBuckets */
745  nSegments = next_pow2_long((nBuckets - 1) / DEF_SEGSIZE + 1);
746  /* directory entries */
747  nDirEntries = DEF_DIRSIZE;
748  while (nDirEntries < nSegments)
749  nDirEntries <<= 1; /* dir_alloc doubles dsize at each call */
750 
751  /* fixed control info */
752  size = MAXALIGN(sizeof(HASHHDR)); /* but not HTAB, per above */
753  /* directory */
754  size = add_size(size, mul_size(nDirEntries, sizeof(HASHSEGMENT)));
755  /* segments */
756  size = add_size(size, mul_size(nSegments,
757  MAXALIGN(DEF_SEGSIZE * sizeof(HASHBUCKET))));
758  /* elements --- allocated in groups of choose_nelem_alloc() entries */
759  elementAllocCnt = choose_nelem_alloc(entrysize);
760  nElementAllocs = (num_entries - 1) / elementAllocCnt + 1;
761  elementSize = MAXALIGN(sizeof(HASHELEMENT)) + MAXALIGN(entrysize);
762  size = add_size(size,
763  mul_size(nElementAllocs,
764  mul_size(elementAllocCnt, elementSize)));
765 
766  return size;
767 }
static long next_pow2_long(long num)
Definition: dynahash.c:1734
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
size_t Size
Definition: c.h:433
#define MAXALIGN(LEN)
Definition: c.h:652
static int choose_nelem_alloc(Size entrysize)
Definition: dynahash.c:603
#define DEF_FFACTOR
Definition: dynahash.c:113
#define DEF_SEGSIZE
Definition: dynahash.c:110
#define DEF_DIRSIZE
Definition: dynahash.c:112

◆ hash_freeze()

void hash_freeze ( HTAB hashp)

Definition at line 1485 of file dynahash.c.

References elog, ERROR, HTAB::frozen, has_seq_scans(), HTAB::isshared, and HTAB::tabname.

1486 {
1487  if (hashp->isshared)
1488  elog(ERROR, "cannot freeze shared hashtable \"%s\"", hashp->tabname);
1489  if (!hashp->frozen && has_seq_scans(hashp))
1490  elog(ERROR, "cannot freeze hashtable \"%s\" because it has active scans",
1491  hashp->tabname);
1492  hashp->frozen = true;
1493 }
#define ERROR
Definition: elog.h:43
static bool has_seq_scans(HTAB *hashp)
Definition: dynahash.c:1820
bool isshared
Definition: dynahash.c:218
bool frozen
Definition: dynahash.c:222
char * tabname
Definition: dynahash.c:217
#define elog
Definition: elog.h:219

◆ hash_get_num_entries()

long hash_get_num_entries ( HTAB hashp)

Definition at line 1335 of file dynahash.c.

References HASHHDR::freeList, HTAB::hctl, i, IS_PARTITIONED, FreeListData::nentries, and NUM_FREELISTS.

Referenced by compute_array_stats(), compute_tsvector_stats(), entry_alloc(), entry_dealloc(), get_crosstab_tuplestore(), GetLockStatusData(), GetPredicateLockStatusData(), GetRunningTransactionLocks(), hash_stats(), pgss_shmem_shutdown(), pgstat_vacuum_stat(), ResetUnloggedRelationsInDbspaceDir(), transformGraph(), and XLogHaveInvalidPages().

1336 {
1337  int i;
1338  long sum = hashp->hctl->freeList[0].nentries;
1339 
1340  /*
1341  * We currently don't bother with acquiring the mutexes; it's only
1342  * sensible to call this function if you've got lock on all partitions of
1343  * the table.
1344  */
1345  if (IS_PARTITIONED(hashp->hctl))
1346  {
1347  for (i = 1; i < NUM_FREELISTS; i++)
1348  sum += hashp->hctl->freeList[i].nentries;
1349  }
1350 
1351  return sum;
1352 }
#define IS_PARTITIONED(hctl)
Definition: dynahash.c:199
#define NUM_FREELISTS
Definition: dynahash.c:116
HASHHDR * hctl
Definition: dynahash.c:210
long nentries
Definition: dynahash.c:144
FreeListData freeList[NUM_FREELISTS]
Definition: dynahash.c:168
int i

◆ hash_get_shared_size()

Size hash_get_shared_size ( HASHCTL info,
int  flags 
)

Definition at line 803 of file dynahash.c.

References Assert, HASHCTL::dsize, HASH_DIRSIZE, and HASHCTL::max_dsize.

Referenced by ShmemInitHash().

804 {
805  Assert(flags & HASH_DIRSIZE);
806  Assert(info->dsize == info->max_dsize);
807  return sizeof(HASHHDR) + info->dsize * sizeof(HASHSEGMENT);
808 }
long dsize
Definition: hsearch.h:69
long max_dsize
Definition: hsearch.h:70
struct HASHHDR HASHHDR
Definition: hsearch.h:58
#define Assert(condition)
Definition: c.h:699
#define HASH_DIRSIZE
Definition: hsearch.h:85

◆ hash_search()

void* hash_search ( HTAB hashp,
const void *  keyPtr,
HASHACTION  action,
bool foundPtr 
)

Definition at line 906 of file dynahash.c.

References generate_unaccent_rules::action, HTAB::hash, hash_search_with_hash_value(), and HTAB::keysize.

Referenced by _hash_finish_split(), _hash_splitbucket(), add_join_rel(), ApplyLogicalMappingFile(), assign_record_type_typmod(), AtEOSubXact_RelationCache(), AtEOXact_RelationCache(), build_join_rel_hash(), BuildEventTriggerCache(), CheckAndPromotePredicateLockRequest(), CheckForSerializableConflictOut(), CompactCheckpointerRequestQueue(), compile_plperl_function(), compile_pltcl_function(), compute_array_stats(), compute_tsvector_stats(), createNewConnection(), deleteConnection(), do_autovacuum(), DropAllPredicateLocksFromTable(), DropAllPreparedStatements(), DropPreparedStatement(), DropRelFileNodeAllLocalBuffers(), DropRelFileNodeLocalBuffers(), entry_alloc(), entry_dealloc(), entry_reset(), EventCacheLookup(), FetchPreparedStatement(), find_all_inheritors(), find_funcstat_entry(), find_join_rel(), find_oper_cache_entry(), find_rendezvous_variable(), find_tabstat_entry(), forget_invalid_pages(), forget_invalid_pages_db(), ForgetPrivateRefCountEntry(), get_attribute_options(), get_cast_hashentry(), get_pgstat_tabentry_relid(), get_rel_sync_entry(), get_tablespace(), get_tabstat_entry(), GetComboCommandId(), GetConnection(), getConnectionByName(), GetExtensibleNodeEntry(), GetPrivateRefCountEntry(), getState(), gistGetNodeBuffer(), gistGetParent(), gistMemorizeParent(), gistRelocateBuildBuffersOnSplit(), hash_object_field_end(), init_sequence(), InitPredicateLocks(), InvalidateAttoptCacheCallback(), InvalidateOprCacheCallBack(), InvalidateShippableCacheCallback(), InvalidateTableSpaceCacheCallback(), is_shippable(), JsObjectGetField(), LocalBufferAlloc(), LocalPrefetchBuffer(), LockAcquireExtended(), LockHasWaiters(), LockRelease(), log_invalid_page(), logical_rewrite_log_mapping(), logicalrep_rel_open(), logicalrep_relmap_update(), logicalrep_typmap_gettypname(), logicalrep_typmap_update(), lookup_C_func(), lookup_collation_cache(), lookup_proof_cache(), lookup_ts_config_cache(), lookup_ts_dictionary_cache(), lookup_ts_parser_cache(), lookup_type_cache(), LookupOpclassInfo(), make_oper_cache_entry(), mdsync(), pg_tzset(), pgss_store(), pgstat_collect_oids(), pgstat_fetch_stat_dbentry(), pgstat_fetch_stat_funcentry(), pgstat_fetch_stat_tabentry(), pgstat_get_db_entry(), pgstat_get_tab_entry(), pgstat_init_function_usage(), pgstat_read_db_statsfile(), pgstat_read_statsfiles(), pgstat_recv_dropdb(), pgstat_recv_funcpurge(), pgstat_recv_funcstat(), pgstat_recv_resetsinglecounter(), pgstat_recv_tabpurge(), pgstat_recv_tabstat(), pgstat_vacuum_stat(), plperl_spi_exec_prepared(), plperl_spi_freeplan(), plperl_spi_prepare(), plperl_spi_query_prepared(), plpgsql_HashTableDelete(), plpgsql_HashTableInsert(), plpgsql_HashTableLookup(), pltcl_fetch_interp(), PLy_generate_spi_exceptions(), PLy_procedure_get(), PLy_spi_subtransaction_abort(), populate_recordset_object_field_end(), predicatelock_twophase_recover(), PredicateLockExists(), PredicateLockTwoPhaseFinish(), process_syncing_tables_for_apply(), prune_element_hashtable(), prune_lexemes_hashtable(), rebuild_database_list(), record_C_func(), RegisterExtensibleNodeEntry(), RegisterPredicateLockingXid(), rel_sync_cache_relation_cb(), ReleaseOneSerializableXact(), RelfilenodeMapInvalidateCallback(), RelidByRelfilenode(), RememberFsyncRequest(), RemoveLocalLock(), ReorderBufferBuildTupleCidHash(), ReorderBufferCleanupTXN(), ReorderBufferToastAppendChunk(), ReorderBufferToastReplace(), ReorderBufferTXNByXid(), ReservePrivateRefCountEntry(), ResetUnloggedRelationsInDbspaceDir(), ResolveCminCmaxDuringDecoding(), rewrite_heap_dead_tuple(), rewrite_heap_tuple(), ri_FetchPreparedPlan(), ri_HashCompareOp(), ri_HashPreparedPlan(), ri_LoadConstraintInfo(), select_perl_context(), set_rtable_names(), ShmemInitStruct(), smgrclose(), smgrclosenode(), smgropen(), StorePreparedStatement(), and table_recheck_autovac().

910 {
911  return hash_search_with_hash_value(hashp,
912  keyPtr,
913  hashp->hash(keyPtr, hashp->keysize),
914  action,
915  foundPtr);
916 }
void * hash_search_with_hash_value(HTAB *hashp, const void *keyPtr, uint32 hashvalue, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:919
Size keysize
Definition: dynahash.c:225
HashValueFunc hash
Definition: dynahash.c:212

◆ hash_search_with_hash_value()

void* hash_search_with_hash_value ( HTAB hashp,
const void *  keyPtr,
uint32  hashvalue,
HASHACTION  action,
bool foundPtr 
)

Definition at line 919 of file dynahash.c.

References HTAB::alloc, Assert, calc_bucket(), HTAB::dir, DynaHashAlloc(), ELEMENTKEY, elog, ereport, errcode(), errmsg(), ERROR, expand_table(), HASHHDR::ffactor, FreeListData::freeList, HASHHDR::freeList, FREELIST_IDX, HTAB::frozen, get_hash_entry(), has_seq_scans(), hash_corrupted(), HASH_ENTER, HASH_ENTER_NULL, HASH_FIND, HASH_REMOVE, HASHELEMENT::hashvalue, HTAB::hctl, IS_PARTITIONED, HTAB::isshared, HTAB::keycopy, HASHHDR::keysize, HTAB::keysize, HASHELEMENT::link, HTAB::match, HASHHDR::max_bucket, MOD, FreeListData::mutex, FreeListData::nentries, SpinLockAcquire, SpinLockRelease, HTAB::sshift, HTAB::ssize, and HTAB::tabname.

Referenced by BufTableDelete(), BufTableInsert(), BufTableLookup(), CheckTargetForConflictsIn(), CleanUpLock(), ClearOldPredicateLocks(), CreatePredicateLock(), DecrementParentLocks(), DeleteChildTargetLocks(), DeleteLockTarget(), DropAllPredicateLocksFromTable(), FastPathGetRelationLockEntry(), GetLockConflicts(), hash_search(), lock_twophase_recover(), LockAcquireExtended(), LockRefindAndRelease(), LockRelease(), LockWaiterCount(), PageIsPredicateLocked(), PredicateLockAcquire(), ReleaseOneSerializableXact(), RemoveScratchTarget(), RemoveTargetIfNoLongerUsed(), RestoreScratchTarget(), SetupLockInTable(), and TransferPredicateLocksToNewTarget().

924 {
925  HASHHDR *hctl = hashp->hctl;
926  int freelist_idx = FREELIST_IDX(hctl, hashvalue);
927  Size keysize;
928  uint32 bucket;
929  long segment_num;
930  long segment_ndx;
931  HASHSEGMENT segp;
932  HASHBUCKET currBucket;
933  HASHBUCKET *prevBucketPtr;
934  HashCompareFunc match;
935 
936 #if HASH_STATISTICS
937  hash_accesses++;
938  hctl->accesses++;
939 #endif
940 
941  /*
942  * If inserting, check if it is time to split a bucket.
943  *
944  * NOTE: failure to expand table is not a fatal error, it just means we
945  * have to run at higher fill factor than we wanted. However, if we're
946  * using the palloc allocator then it will throw error anyway on
947  * out-of-memory, so we must do this before modifying the table.
948  */
950  {
951  /*
952  * Can't split if running in partitioned mode, nor if frozen, nor if
953  * table is the subject of any active hash_seq_search scans. Strange
954  * order of these tests is to try to check cheaper conditions first.
955  */
956  if (!IS_PARTITIONED(hctl) && !hashp->frozen &&
957  hctl->freeList[0].nentries / (long) (hctl->max_bucket + 1) >= hctl->ffactor &&
958  !has_seq_scans(hashp))
959  (void) expand_table(hashp);
960  }
961 
962  /*
963  * Do the initial lookup
964  */
965  bucket = calc_bucket(hctl, hashvalue);
966 
967  segment_num = bucket >> hashp->sshift;
968  segment_ndx = MOD(bucket, hashp->ssize);
969 
970  segp = hashp->dir[segment_num];
971 
972  if (segp == NULL)
973  hash_corrupted(hashp);
974 
975  prevBucketPtr = &segp[segment_ndx];
976  currBucket = *prevBucketPtr;
977 
978  /*
979  * Follow collision chain looking for matching key
980  */
981  match = hashp->match; /* save one fetch in inner loop */
982  keysize = hashp->keysize; /* ditto */
983 
984  while (currBucket != NULL)
985  {
986  if (currBucket->hashvalue == hashvalue &&
987  match(ELEMENTKEY(currBucket), keyPtr, keysize) == 0)
988  break;
989  prevBucketPtr = &(currBucket->link);
990  currBucket = *prevBucketPtr;
991 #if HASH_STATISTICS
992  hash_collisions++;
993  hctl->collisions++;
994 #endif
995  }
996 
997  if (foundPtr)
998  *foundPtr = (bool) (currBucket != NULL);
999 
1000  /*
1001  * OK, now what?
1002  */
1003  switch (action)
1004  {
1005  case HASH_FIND:
1006  if (currBucket != NULL)
1007  return (void *) ELEMENTKEY(currBucket);
1008  return NULL;
1009 
1010  case HASH_REMOVE:
1011  if (currBucket != NULL)
1012  {
1013  /* if partitioned, must lock to touch nentries and freeList */
1014  if (IS_PARTITIONED(hctl))
1015  SpinLockAcquire(&(hctl->freeList[freelist_idx].mutex));
1016 
1017  /* delete the record from the appropriate nentries counter. */
1018  Assert(hctl->freeList[freelist_idx].nentries > 0);
1019  hctl->freeList[freelist_idx].nentries--;
1020 
1021  /* remove record from hash bucket's chain. */
1022  *prevBucketPtr = currBucket->link;
1023 
1024  /* add the record to the appropriate freelist. */
1025  currBucket->link = hctl->freeList[freelist_idx].freeList;
1026  hctl->freeList[freelist_idx].freeList = currBucket;
1027 
1028  if (IS_PARTITIONED(hctl))
1029  SpinLockRelease(&hctl->freeList[freelist_idx].mutex);
1030 
1031  /*
1032  * better hope the caller is synchronizing access to this
1033  * element, because someone else is going to reuse it the next
1034  * time something is added to the table
1035  */
1036  return (void *) ELEMENTKEY(currBucket);
1037  }
1038  return NULL;
1039 
1040  case HASH_ENTER_NULL:
1041  /* ENTER_NULL does not work with palloc-based allocator */
1042  Assert(hashp->alloc != DynaHashAlloc);
1043  /* FALL THRU */
1044 
1045  case HASH_ENTER:
1046  /* Return existing element if found, else create one */
1047  if (currBucket != NULL)
1048  return (void *) ELEMENTKEY(currBucket);
1049 
1050  /* disallow inserts if frozen */
1051  if (hashp->frozen)
1052  elog(ERROR, "cannot insert into frozen hashtable \"%s\"",
1053  hashp->tabname);
1054 
1055  currBucket = get_hash_entry(hashp, freelist_idx);
1056  if (currBucket == NULL)
1057  {
1058  /* out of memory */
1059  if (action == HASH_ENTER_NULL)
1060  return NULL;
1061  /* report a generic message */
1062  if (hashp->isshared)
1063  ereport(ERROR,
1064  (errcode(ERRCODE_OUT_OF_MEMORY),
1065  errmsg("out of shared memory")));
1066  else
1067  ereport(ERROR,
1068  (errcode(ERRCODE_OUT_OF_MEMORY),
1069  errmsg("out of memory")));
1070  }
1071 
1072  /* link into hashbucket chain */
1073  *prevBucketPtr = currBucket;
1074  currBucket->link = NULL;
1075 
1076  /* copy key into record */
1077  currBucket->hashvalue = hashvalue;
1078  hashp->keycopy(ELEMENTKEY(currBucket), keyPtr, keysize);
1079 
1080  /*
1081  * Caller is expected to fill the data field on return. DO NOT
1082  * insert any code that could possibly throw error here, as doing
1083  * so would leave the table entry incomplete and hence corrupt the
1084  * caller's data structure.
1085  */
1086 
1087  return (void *) ELEMENTKEY(currBucket);
1088  }
1089 
1090  elog(ERROR, "unrecognized hash action code: %d", (int) action);
1091 
1092  return NULL; /* keep compiler quiet */
1093 }
int(* HashCompareFunc)(const void *key1, const void *key2, Size keysize)
Definition: hsearch.h:29
Size keysize
Definition: dynahash.c:225
uint32 max_bucket
Definition: dynahash.c:174
static uint32 calc_bucket(HASHHDR *hctl, uint32 hash_val)
Definition: dynahash.c:867
#define IS_PARTITIONED(hctl)
Definition: dynahash.c:199
#define ELEMENTKEY(helem)
Definition: dynahash.c:233
int errcode(int sqlerrcode)
Definition: elog.c:575
HashAllocFunc alloc
Definition: dynahash.c:215
slock_t mutex
Definition: dynahash.c:143
static HASHBUCKET get_hash_entry(HTAB *hashp, int freelist_idx)
Definition: dynahash.c:1250
char bool
Definition: c.h:275
long ffactor
Definition: dynahash.c:182
#define SpinLockAcquire(lock)
Definition: spin.h:62
long ssize
Definition: dynahash.c:226
#define ERROR
Definition: elog.h:43
int sshift
Definition: dynahash.c:227
#define MOD(x, y)
Definition: dynahash.c:244
HASHHDR * hctl
Definition: dynahash.c:210
static void hash_corrupted(HTAB *hashp)
Definition: dynahash.c:1704
static bool has_seq_scans(HTAB *hashp)
Definition: dynahash.c:1820
unsigned int uint32
Definition: c.h:325
long nentries
Definition: dynahash.c:144
#define ereport(elevel, rest)
Definition: elog.h:122
HashCompareFunc match
Definition: dynahash.c:213
bool isshared
Definition: dynahash.c:218
#define SpinLockRelease(lock)
Definition: spin.h:64
bool frozen
Definition: dynahash.c:222
FreeListData freeList[NUM_FREELISTS]
Definition: dynahash.c:168
HashCopyFunc keycopy
Definition: dynahash.c:214
#define Assert(condition)
Definition: c.h:699
size_t Size
Definition: c.h:433
static bool expand_table(HTAB *hashp)
Definition: dynahash.c:1502
char * tabname
Definition: dynahash.c:217
static void * DynaHashAlloc(Size size)
Definition: dynahash.c:278
int errmsg(const char *fmt,...)
Definition: elog.c:797
struct HASHELEMENT * link
Definition: hsearch.h:53
HASHELEMENT * freeList
Definition: dynahash.c:145
HASHSEGMENT * dir
Definition: dynahash.c:211
#define elog
Definition: elog.h:219
#define FREELIST_IDX(hctl, hashcode)
Definition: dynahash.c:201
uint32 hashvalue
Definition: hsearch.h:54

◆ hash_select_dirsize()

long hash_select_dirsize ( long  num_entries)

Definition at line 779 of file dynahash.c.

References DEF_DIRSIZE, DEF_FFACTOR, DEF_SEGSIZE, and next_pow2_long().

Referenced by ShmemInitHash().

780 {
781  long nBuckets,
782  nSegments,
783  nDirEntries;
784 
785  /* estimate number of buckets wanted */
786  nBuckets = next_pow2_long((num_entries - 1) / DEF_FFACTOR + 1);
787  /* # of segments needed for nBuckets */
788  nSegments = next_pow2_long((nBuckets - 1) / DEF_SEGSIZE + 1);
789  /* directory entries */
790  nDirEntries = DEF_DIRSIZE;
791  while (nDirEntries < nSegments)
792  nDirEntries <<= 1; /* dir_alloc doubles dsize at each call */
793 
794  return nDirEntries;
795 }
static long next_pow2_long(long num)
Definition: dynahash.c:1734
#define DEF_FFACTOR
Definition: dynahash.c:113
#define DEF_SEGSIZE
Definition: dynahash.c:110
#define DEF_DIRSIZE
Definition: dynahash.c:112

◆ hash_seq_init()

void hash_seq_init ( HASH_SEQ_STATUS status,
HTAB hashp 
)

Definition at line 1379 of file dynahash.c.

References HASH_SEQ_STATUS::curBucket, HASH_SEQ_STATUS::curEntry, HTAB::frozen, HASH_SEQ_STATUS::hashp, and register_seq_scan().

Referenced by AtAbort_Portals(), AtCleanup_Portals(), AtEOSubXact_RelationCache(), AtEOXact_RelationCache(), AtPrepare_Locks(), AtSubAbort_Portals(), AtSubCleanup_Portals(), AtSubCommit_Portals(), CheckForBufferLeaks(), CheckTableForSerializableConflictIn(), compute_array_stats(), compute_tsvector_stats(), dblink_get_connections(), DropAllPredicateLocksFromTable(), DropAllPreparedStatements(), end_heap_rewrite(), entry_dealloc(), entry_reset(), forget_invalid_pages(), forget_invalid_pages_db(), gc_qtexts(), GetLockmodeName(), GetLockStatusData(), GetPredicateLockStatusData(), GetRunningTransactionLocks(), HoldPinnedPortals(), InvalidateAttoptCacheCallback(), InvalidateOprCacheCallBack(), InvalidateOprProofCacheCallBack(), InvalidateShippableCacheCallback(), InvalidateTableSpaceCacheCallback(), InvalidateTSCacheCallBack(), LockReassignCurrentOwner(), LockReleaseAll(), LockReleaseCurrentOwner(), LockReleaseSession(), logical_end_heap_rewrite(), logical_heap_rewrite_flush_mappings(), logicalrep_relmap_invalidate_cb(), mdsync(), packArcInfoCmp(), packGraph(), pg_cursor(), pg_prepared_statement(), pg_stat_statements_internal(), pgfdw_inval_callback(), pgfdw_subxact_callback(), pgfdw_xact_callback(), pgss_shmem_shutdown(), pgstat_send_funcstats(), pgstat_vacuum_stat(), pgstat_write_db_statsfile(), pgstat_write_statsfiles(), plperl_fini(), PortalErrorCleanup(), PortalHashTableDeleteAll(), PostPrepare_Locks(), PreCommit_Portals(), prune_element_hashtable(), prune_lexemes_hashtable(), rebuild_database_list(), rel_sync_cache_publication_cb(), RelationCacheInitializePhase3(), RelationCacheInvalidate(), RelfilenodeMapInvalidateCallback(), RememberFsyncRequest(), ReorderBufferToastReset(), selectColorTrigrams(), smgrcloseall(), ThereAreNoReadyPortals(), TypeCacheOpcCallback(), TypeCacheRelCallback(), write_relcache_init_file(), and XLogCheckInvalidPages().

1380 {
1381  status->hashp = hashp;
1382  status->curBucket = 0;
1383  status->curEntry = NULL;
1384  if (!hashp->frozen)
1385  register_seq_scan(hashp);
1386 }
uint32 curBucket
Definition: hsearch.h:115
HASHELEMENT * curEntry
Definition: hsearch.h:116
HTAB * hashp
Definition: hsearch.h:114
bool frozen
Definition: dynahash.c:222
static void register_seq_scan(HTAB *hashp)
Definition: dynahash.c:1787

◆ hash_seq_search()

void* hash_seq_search ( HASH_SEQ_STATUS status)

Definition at line 1389 of file dynahash.c.

References HASH_SEQ_STATUS::curBucket, HASH_SEQ_STATUS::curEntry, HTAB::dir, ELEMENTKEY, hash_seq_term(), HASH_SEQ_STATUS::hashp, HTAB::hctl, HASHELEMENT::link, HASHHDR::max_bucket, MOD, HTAB::sshift, HASHHDR::ssize, and HTAB::ssize.

Referenced by AtAbort_Portals(), AtCleanup_Portals(), AtEOSubXact_RelationCache(), AtEOXact_RelationCache(), AtPrepare_Locks(), AtSubAbort_Portals(), AtSubCleanup_Portals(), AtSubCommit_Portals(), CheckForBufferLeaks(), CheckTableForSerializableConflictIn(), compute_array_stats(), compute_tsvector_stats(), dblink_get_connections(), DropAllPredicateLocksFromTable(), DropAllPreparedStatements(), end_heap_rewrite(), entry_dealloc(), entry_reset(), forget_invalid_pages(), forget_invalid_pages_db(), gc_qtexts(), GetLockmodeName(), GetLockStatusData(), GetPredicateLockStatusData(), GetRunningTransactionLocks(), HoldPinnedPortals(), InvalidateAttoptCacheCallback(), InvalidateOprCacheCallBack(), InvalidateOprProofCacheCallBack(), InvalidateShippableCacheCallback(), InvalidateTableSpaceCacheCallback(), InvalidateTSCacheCallBack(), LockReassignCurrentOwner(), LockReleaseAll(), LockReleaseCurrentOwner(), LockReleaseSession(), logical_end_heap_rewrite(), logical_heap_rewrite_flush_mappings(), logicalrep_relmap_invalidate_cb(), mdsync(), packArcInfoCmp(), packGraph(), pg_cursor(), pg_prepared_statement(), pg_stat_statements_internal(), pgfdw_inval_callback(), pgfdw_subxact_callback(), pgfdw_xact_callback(), pgss_shmem_shutdown(), pgstat_send_funcstats(), pgstat_vacuum_stat(), pgstat_write_db_statsfile(), pgstat_write_statsfiles(), plperl_fini(), PortalErrorCleanup(), PortalHashTableDeleteAll(), PostPrepare_Locks(), PreCommit_Portals(), prune_element_hashtable(), prune_lexemes_hashtable(), rebuild_database_list(), rel_sync_cache_publication_cb(), RelationCacheInitializePhase3(), RelationCacheInvalidate(), RelfilenodeMapInvalidateCallback(), RememberFsyncRequest(), ReorderBufferToastReset(), selectColorTrigrams(), smgrcloseall(), ThereAreNoReadyPortals(), TypeCacheOpcCallback(), TypeCacheRelCallback(), write_relcache_init_file(), and XLogCheckInvalidPages().

1390 {
1391  HTAB *hashp;
1392  HASHHDR *hctl;
1393  uint32 max_bucket;
1394  long ssize;
1395  long segment_num;
1396  long segment_ndx;
1397  HASHSEGMENT segp;
1398  uint32 curBucket;
1399  HASHELEMENT *curElem;
1400 
1401  if ((curElem = status->curEntry) != NULL)
1402  {
1403  /* Continuing scan of curBucket... */
1404  status->curEntry = curElem->link;
1405  if (status->curEntry == NULL) /* end of this bucket */
1406  ++status->curBucket;
1407  return (void *) ELEMENTKEY(curElem);
1408  }
1409 
1410  /*
1411  * Search for next nonempty bucket starting at curBucket.
1412  */
1413  curBucket = status->curBucket;
1414  hashp = status->hashp;
1415  hctl = hashp->hctl;
1416  ssize = hashp->ssize;
1417  max_bucket = hctl->max_bucket;
1418 
1419  if (curBucket > max_bucket)
1420  {
1421  hash_seq_term(status);
1422  return NULL; /* search is done */
1423  }
1424 
1425  /*
1426  * first find the right segment in the table directory.
1427  */
1428  segment_num = curBucket >> hashp->sshift;
1429  segment_ndx = MOD(curBucket, ssize);
1430 
1431  segp = hashp->dir[segment_num];
1432 
1433  /*
1434  * Pick up the first item in this bucket's chain. If chain is not empty
1435  * we can begin searching it. Otherwise we have to advance to find the
1436  * next nonempty bucket. We try to optimize that case since searching a
1437  * near-empty hashtable has to iterate this loop a lot.
1438  */
1439  while ((curElem = segp[segment_ndx]) == NULL)
1440  {
1441  /* empty bucket, advance to next */
1442  if (++curBucket > max_bucket)
1443  {
1444  status->curBucket = curBucket;
1445  hash_seq_term(status);
1446  return NULL; /* search is done */
1447  }
1448  if (++segment_ndx >= ssize)
1449  {
1450  segment_num++;
1451  segment_ndx = 0;
1452  segp = hashp->dir[segment_num];
1453  }
1454  }
1455 
1456  /* Begin scan of curBucket... */
1457  status->curEntry = curElem->link;
1458  if (status->curEntry == NULL) /* end of this bucket */
1459  ++curBucket;
1460  status->curBucket = curBucket;
1461  return (void *) ELEMENTKEY(curElem);
1462 }
uint32 curBucket
Definition: hsearch.h:115
uint32 max_bucket
Definition: dynahash.c:174
#define ELEMENTKEY(helem)
Definition: dynahash.c:233
HASHELEMENT * curEntry
Definition: hsearch.h:116
Definition: dynahash.c:208
long ssize
Definition: dynahash.c:226
int sshift
Definition: dynahash.c:227
#define MOD(x, y)
Definition: dynahash.c:244
HASHHDR * hctl
Definition: dynahash.c:210
unsigned int uint32
Definition: c.h:325
HTAB * hashp
Definition: hsearch.h:114
struct HASHELEMENT * link
Definition: hsearch.h:53
HASHSEGMENT * dir
Definition: dynahash.c:211
void hash_seq_term(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1465

◆ hash_seq_term()

void hash_seq_term ( HASH_SEQ_STATUS status)

Definition at line 1465 of file dynahash.c.

References deregister_seq_scan(), HTAB::frozen, and HASH_SEQ_STATUS::hashp.

Referenced by gc_qtexts(), hash_seq_search(), logicalrep_relmap_invalidate_cb(), pgss_shmem_shutdown(), PortalHashTableDeleteAll(), PreCommit_Portals(), and RelationCacheInitializePhase3().

1466 {
1467  if (!status->hashp->frozen)
1468  deregister_seq_scan(status->hashp);
1469 }
HTAB * hashp
Definition: hsearch.h:114
bool frozen
Definition: dynahash.c:222
static void deregister_seq_scan(HTAB *hashp)
Definition: dynahash.c:1799

◆ hash_stats()

void hash_stats ( const char *  where,
HTAB hashp 
)

Definition at line 833 of file dynahash.c.

References hash_get_num_entries(), HTAB::hctl, HASHHDR::keysize, HASHHDR::max_bucket, and HASHHDR::nsegs.

Referenced by hash_destroy().

834 {
835 #if HASH_STATISTICS
836  fprintf(stderr, "%s: this HTAB -- accesses %ld collisions %ld\n",
837  where, hashp->hctl->accesses, hashp->hctl->collisions);
838 
839  fprintf(stderr, "hash_stats: entries %ld keysize %ld maxp %u segmentcount %ld\n",
840  hash_get_num_entries(hashp), (long) hashp->hctl->keysize,
841  hashp->hctl->max_bucket, hashp->hctl->nsegs);
842  fprintf(stderr, "%s: total accesses %ld total collisions %ld\n",
843  where, hash_accesses, hash_collisions);
844  fprintf(stderr, "hash_stats: total expansions %ld\n",
845  hash_expansions);
846 #endif
847 }
Size keysize
Definition: dynahash.c:179
uint32 max_bucket
Definition: dynahash.c:174
long hash_get_num_entries(HTAB *hashp)
Definition: dynahash.c:1335
HASHHDR * hctl
Definition: dynahash.c:210
long nsegs
Definition: dynahash.c:173

◆ hash_update_hash_key()

bool hash_update_hash_key ( HTAB hashp,
void *  existingEntry,
const void *  newKeyPtr 
)

Definition at line 1115 of file dynahash.c.

References calc_bucket(), HTAB::dir, ELEMENT_FROM_KEY, ELEMENTKEY, elog, ERROR, HTAB::frozen, HTAB::hash, hash_corrupted(), HASHELEMENT::hashvalue, HTAB::hctl, HTAB::keycopy, HASHHDR::keysize, HTAB::keysize, HASHELEMENT::link, HTAB::match, MOD, HTAB::sshift, HTAB::ssize, and HTAB::tabname.

Referenced by PostPrepare_Locks().

1118 {
1119  HASHELEMENT *existingElement = ELEMENT_FROM_KEY(existingEntry);
1120  HASHHDR *hctl = hashp->hctl;
1121  uint32 newhashvalue;
1122  Size keysize;
1123  uint32 bucket;
1124  uint32 newbucket;
1125  long segment_num;
1126  long segment_ndx;
1127  HASHSEGMENT segp;
1128  HASHBUCKET currBucket;
1129  HASHBUCKET *prevBucketPtr;
1130  HASHBUCKET *oldPrevPtr;
1131  HashCompareFunc match;
1132 
1133 #if HASH_STATISTICS
1134  hash_accesses++;
1135  hctl->accesses++;
1136 #endif
1137 
1138  /* disallow updates if frozen */
1139  if (hashp->frozen)
1140  elog(ERROR, "cannot update in frozen hashtable \"%s\"",
1141  hashp->tabname);
1142 
1143  /*
1144  * Lookup the existing element using its saved hash value. We need to do
1145  * this to be able to unlink it from its hash chain, but as a side benefit
1146  * we can verify the validity of the passed existingEntry pointer.
1147  */
1148  bucket = calc_bucket(hctl, existingElement->hashvalue);
1149 
1150  segment_num = bucket >> hashp->sshift;
1151  segment_ndx = MOD(bucket, hashp->ssize);
1152 
1153  segp = hashp->dir[segment_num];
1154 
1155  if (segp == NULL)
1156  hash_corrupted(hashp);
1157 
1158  prevBucketPtr = &segp[segment_ndx];
1159  currBucket = *prevBucketPtr;
1160 
1161  while (currBucket != NULL)
1162  {
1163  if (currBucket == existingElement)
1164  break;
1165  prevBucketPtr = &(currBucket->link);
1166  currBucket = *prevBucketPtr;
1167  }
1168 
1169  if (currBucket == NULL)
1170  elog(ERROR, "hash_update_hash_key argument is not in hashtable \"%s\"",
1171  hashp->tabname);
1172 
1173  oldPrevPtr = prevBucketPtr;
1174 
1175  /*
1176  * Now perform the equivalent of a HASH_ENTER operation to locate the hash
1177  * chain we want to put the entry into.
1178  */
1179  newhashvalue = hashp->hash(newKeyPtr, hashp->keysize);
1180 
1181  newbucket = calc_bucket(hctl, newhashvalue);
1182 
1183  segment_num = newbucket >> hashp->sshift;
1184  segment_ndx = MOD(newbucket, hashp->ssize);
1185 
1186  segp = hashp->dir[segment_num];
1187 
1188  if (segp == NULL)
1189  hash_corrupted(hashp);
1190 
1191  prevBucketPtr = &segp[segment_ndx];
1192  currBucket = *prevBucketPtr;
1193 
1194  /*
1195  * Follow collision chain looking for matching key
1196  */
1197  match = hashp->match; /* save one fetch in inner loop */
1198  keysize = hashp->keysize; /* ditto */
1199 
1200  while (currBucket != NULL)
1201  {
1202  if (currBucket->hashvalue == newhashvalue &&
1203  match(ELEMENTKEY(currBucket), newKeyPtr, keysize) == 0)
1204  break;
1205  prevBucketPtr = &(currBucket->link);
1206  currBucket = *prevBucketPtr;
1207 #if HASH_STATISTICS
1208  hash_collisions++;
1209  hctl->collisions++;
1210 #endif
1211  }
1212 
1213  if (currBucket != NULL)
1214  return false; /* collision with an existing entry */
1215 
1216  currBucket = existingElement;
1217 
1218  /*
1219  * If old and new hash values belong to the same bucket, we need not
1220  * change any chain links, and indeed should not since this simplistic
1221  * update will corrupt the list if currBucket is the last element. (We
1222  * cannot fall out earlier, however, since we need to scan the bucket to
1223  * check for duplicate keys.)
1224  */
1225  if (bucket != newbucket)
1226  {
1227  /* OK to remove record from old hash bucket's chain. */
1228  *oldPrevPtr = currBucket->link;
1229 
1230  /* link into new hashbucket chain */
1231  *prevBucketPtr = currBucket;
1232  currBucket->link = NULL;
1233  }
1234 
1235  /* copy new key into record */
1236  currBucket->hashvalue = newhashvalue;
1237  hashp->keycopy(ELEMENTKEY(currBucket), newKeyPtr, keysize);
1238 
1239  /* rest of record is untouched */
1240 
1241  return true;
1242 }
int(* HashCompareFunc)(const void *key1, const void *key2, Size keysize)
Definition: hsearch.h:29
Size keysize
Definition: dynahash.c:225
static uint32 calc_bucket(HASHHDR *hctl, uint32 hash_val)
Definition: dynahash.c:867
#define ELEMENTKEY(helem)
Definition: dynahash.c:233
#define ELEMENT_FROM_KEY(key)
Definition: dynahash.c:238
HashValueFunc hash
Definition: dynahash.c:212
long ssize
Definition: dynahash.c:226
#define ERROR
Definition: elog.h:43
int sshift
Definition: dynahash.c:227
#define MOD(x, y)
Definition: dynahash.c:244
HASHHDR * hctl
Definition: dynahash.c:210
static void hash_corrupted(HTAB *hashp)
Definition: dynahash.c:1704
unsigned int uint32
Definition: c.h:325
HashCompareFunc match
Definition: dynahash.c:213
bool frozen
Definition: dynahash.c:222
HashCopyFunc keycopy
Definition: dynahash.c:214
size_t Size
Definition: c.h:433
char * tabname
Definition: dynahash.c:217
struct HASHELEMENT * link
Definition: hsearch.h:53
HASHSEGMENT * dir
Definition: dynahash.c:211
#define elog
Definition: elog.h:219
uint32 hashvalue
Definition: hsearch.h:54

◆ hdefault()

static void hdefault ( HTAB hashp)
static

Definition at line 570 of file dynahash.c.

References DEF_DIRSIZE, DEF_FFACTOR, DEF_SEGSIZE, DEF_SEGSIZE_SHIFT, HASHHDR::dsize, HASHHDR::entrysize, HASHHDR::ffactor, HTAB::hctl, HASHHDR::keysize, HASHHDR::max_dsize, MemSet, NO_MAX_DSIZE, HASHHDR::nsegs, HASHHDR::num_partitions, HASHHDR::sshift, and HASHHDR::ssize.

Referenced by hash_create().

571 {
572  HASHHDR *hctl = hashp->hctl;
573 
574  MemSet(hctl, 0, sizeof(HASHHDR));
575 
576  hctl->dsize = DEF_DIRSIZE;
577  hctl->nsegs = 0;
578 
579  /* rather pointless defaults for key & entry size */
580  hctl->keysize = sizeof(char *);
581  hctl->entrysize = 2 * sizeof(char *);
582 
583  hctl->num_partitions = 0; /* not partitioned */
584 
585  hctl->ffactor = DEF_FFACTOR;
586 
587  /* table has no fixed maximum size */
588  hctl->max_dsize = NO_MAX_DSIZE;
589 
590  hctl->ssize = DEF_SEGSIZE;
591  hctl->sshift = DEF_SEGSIZE_SHIFT;
592 
593 #ifdef HASH_STATISTICS
594  hctl->accesses = hctl->collisions = 0;
595 #endif
596 }
Size keysize
Definition: dynahash.c:179
long dsize
Definition: dynahash.c:172
Size entrysize
Definition: dynahash.c:180
#define NO_MAX_DSIZE
Definition: hsearch.h:100
long num_partitions
Definition: dynahash.c:181
#define MemSet(start, val, len)
Definition: c.h:908
long ffactor
Definition: dynahash.c:182
#define DEF_SEGSIZE_SHIFT
Definition: dynahash.c:111
long max_dsize
Definition: dynahash.c:183
HASHHDR * hctl
Definition: dynahash.c:210
long ssize
Definition: dynahash.c:184
int sshift
Definition: dynahash.c:185
#define DEF_FFACTOR
Definition: dynahash.c:113
#define DEF_SEGSIZE
Definition: dynahash.c:110
#define DEF_DIRSIZE
Definition: dynahash.c:112
long nsegs
Definition: dynahash.c:173

◆ init_htab()

static bool init_htab ( HTAB hashp,
long  nelem 
)
static

Definition at line 636 of file dynahash.c.

References HTAB::alloc, choose_nelem_alloc(), HTAB::dir, HASHHDR::dsize, HASHHDR::entrysize, HASHHDR::ffactor, HASHHDR::freeList, HTAB::hctl, HTAB::hcxt, HASHHDR::high_mask, i, IS_PARTITIONED, HASHHDR::low_mask, HASHHDR::max_bucket, FreeListData::mutex, HASHHDR::nelem_alloc, next_pow2_int(), HASHHDR::nsegs, NUM_FREELISTS, HASHHDR::num_partitions, seg_alloc(), SpinLockInit, HASHHDR::sshift, and HASHHDR::ssize.

Referenced by hash_create().

637 {
638  HASHHDR *hctl = hashp->hctl;
639  HASHSEGMENT *segp;
640  int nbuckets;
641  int nsegs;
642  int i;
643 
644  /*
645  * initialize mutexes if it's a partitioned table
646  */
647  if (IS_PARTITIONED(hctl))
648  for (i = 0; i < NUM_FREELISTS; i++)
649  SpinLockInit(&(hctl->freeList[i].mutex));
650 
651  /*
652  * Divide number of elements by the fill factor to determine a desired
653  * number of buckets. Allocate space for the next greater power of two
654  * number of buckets
655  */
656  nbuckets = next_pow2_int((nelem - 1) / hctl->ffactor + 1);
657 
658  /*
659  * In a partitioned table, nbuckets must be at least equal to
660  * num_partitions; were it less, keys with apparently different partition
661  * numbers would map to the same bucket, breaking partition independence.
662  * (Normally nbuckets will be much bigger; this is just a safety check.)
663  */
664  while (nbuckets < hctl->num_partitions)
665  nbuckets <<= 1;
666 
667  hctl->max_bucket = hctl->low_mask = nbuckets - 1;
668  hctl->high_mask = (nbuckets << 1) - 1;
669 
670  /*
671  * Figure number of directory segments needed, round up to a power of 2
672  */
673  nsegs = (nbuckets - 1) / hctl->ssize + 1;
674  nsegs = next_pow2_int(nsegs);
675 
676  /*
677  * Make sure directory is big enough. If pre-allocated directory is too
678  * small, choke (caller screwed up).
679  */
680  if (nsegs > hctl->dsize)
681  {
682  if (!(hashp->dir))
683  hctl->dsize = nsegs;
684  else
685  return false;
686  }
687 
688  /* Allocate a directory */
689  if (!(hashp->dir))
690  {
691  CurrentDynaHashCxt = hashp->hcxt;
692  hashp->dir = (HASHSEGMENT *)
693  hashp->alloc(hctl->dsize * sizeof(HASHSEGMENT));
694  if (!hashp->dir)
695  return false;
696  }
697 
698  /* Allocate initial segments */
699  for (segp = hashp->dir; hctl->nsegs < nsegs; hctl->nsegs++, segp++)
700  {
701  *segp = seg_alloc(hashp);
702  if (*segp == NULL)
703  return false;
704  }
705 
706  /* Choose number of entries to allocate at a time */
708 
709 #if HASH_DEBUG
710  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",
711  "TABLE POINTER ", hashp,
712  "DIRECTORY SIZE ", hctl->dsize,
713  "SEGMENT SIZE ", hctl->ssize,
714  "SEGMENT SHIFT ", hctl->sshift,
715  "FILL FACTOR ", hctl->ffactor,
716  "MAX BUCKET ", hctl->max_bucket,
717  "HIGH MASK ", hctl->high_mask,
718  "LOW MASK ", hctl->low_mask,
719  "NSEGS ", hctl->nsegs);
720 #endif
721  return true;
722 }
long dsize
Definition: dynahash.c:172
uint32 max_bucket
Definition: dynahash.c:174
Size entrysize
Definition: dynahash.c:180
#define IS_PARTITIONED(hctl)
Definition: dynahash.c:199
#define SpinLockInit(lock)
Definition: spin.h:60
#define NUM_FREELISTS
Definition: dynahash.c:116
HashAllocFunc alloc
Definition: dynahash.c:215
slock_t mutex
Definition: dynahash.c:143
uint32 low_mask
Definition: dynahash.c:176
long ffactor
Definition: dynahash.c:182
HASHHDR * hctl
Definition: dynahash.c:210
uint32 high_mask
Definition: dynahash.c:175
static int next_pow2_int(long num)
Definition: dynahash.c:1742
long ssize
Definition: dynahash.c:184
FreeListData freeList[NUM_FREELISTS]
Definition: dynahash.c:168
MemoryContext hcxt
Definition: dynahash.c:216
int nelem_alloc
Definition: dynahash.c:186
static HASHSEGMENT seg_alloc(HTAB *hashp)
Definition: dynahash.c:1638
static int choose_nelem_alloc(Size entrysize)
Definition: dynahash.c:603
static MemoryContext CurrentDynaHashCxt
Definition: dynahash.c:275
int i
HASHSEGMENT * dir
Definition: dynahash.c:211
int sshift
Definition: dynahash.c:185
long nsegs
Definition: dynahash.c:173

◆ my_log2()

int my_log2 ( long  num)

Definition at line 1718 of file dynahash.c.

References i.

Referenced by ExecChooseHashTableSize(), ExecHashTableCreate(), ExecParallelHashIncreaseNumBatches(), ExecParallelHashTableSetCurrentBatch(), hash_create(), MultiExecParallelHash(), next_pow2_int(), and next_pow2_long().

1719 {
1720  int i;
1721  long limit;
1722 
1723  /* guard against too-large input, which would put us into infinite loop */
1724  if (num > LONG_MAX / 2)
1725  num = LONG_MAX / 2;
1726 
1727  for (i = 0, limit = 1; limit < num; i++, limit <<= 1)
1728  ;
1729  return i;
1730 }
int i

◆ next_pow2_int()

static int next_pow2_int ( long  num)
static

Definition at line 1742 of file dynahash.c.

References my_log2().

Referenced by hash_create(), and init_htab().

1743 {
1744  if (num > INT_MAX / 2)
1745  num = INT_MAX / 2;
1746  return 1 << my_log2(num);
1747 }
int my_log2(long num)
Definition: dynahash.c:1718

◆ next_pow2_long()

static long next_pow2_long ( long  num)
static

Definition at line 1734 of file dynahash.c.

References my_log2().

Referenced by hash_estimate_size(), and hash_select_dirsize().

1735 {
1736  /* my_log2's internal range check is sufficient */
1737  return 1L << my_log2(num);
1738 }
int my_log2(long num)
Definition: dynahash.c:1718

◆ register_seq_scan()

static void register_seq_scan ( HTAB hashp)
static

Definition at line 1787 of file dynahash.c.

References elog, ERROR, GetCurrentTransactionNestLevel(), MAX_SEQ_SCANS, num_seq_scans, seq_scan_level, and HTAB::tabname.

Referenced by hash_seq_init().

1788 {
1790  elog(ERROR, "too many active hash_seq_search scans, cannot start one on \"%s\"",
1791  hashp->tabname);
1792  seq_scan_tables[num_seq_scans] = hashp;
1794  num_seq_scans++;
1795 }
static int seq_scan_level[MAX_SEQ_SCANS]
Definition: dynahash.c:1781
#define ERROR
Definition: elog.h:43
#define MAX_SEQ_SCANS
Definition: dynahash.c:1778
static int num_seq_scans
Definition: dynahash.c:1782
static HTAB * seq_scan_tables[MAX_SEQ_SCANS]
Definition: dynahash.c:1780
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:753
char * tabname
Definition: dynahash.c:217
#define elog
Definition: elog.h:219

◆ seg_alloc()

static HASHSEGMENT seg_alloc ( HTAB hashp)
static

Definition at line 1638 of file dynahash.c.

References HTAB::alloc, HTAB::hcxt, MemSet, and HTAB::ssize.

Referenced by expand_table(), and init_htab().

1639 {
1640  HASHSEGMENT segp;
1641 
1642  CurrentDynaHashCxt = hashp->hcxt;
1643  segp = (HASHSEGMENT) hashp->alloc(sizeof(HASHBUCKET) * hashp->ssize);
1644 
1645  if (!segp)
1646  return NULL;
1647 
1648  MemSet(segp, 0, sizeof(HASHBUCKET) * hashp->ssize);
1649 
1650  return segp;
1651 }
HashAllocFunc alloc
Definition: dynahash.c:215
#define MemSet(start, val, len)
Definition: c.h:908
HASHBUCKET * HASHSEGMENT
Definition: dynahash.c:122
long ssize
Definition: dynahash.c:226
MemoryContext hcxt
Definition: dynahash.c:216
static MemoryContext CurrentDynaHashCxt
Definition: dynahash.c:275

◆ string_compare()

static int string_compare ( const char *  key1,
const char *  key2,
Size  keysize 
)
static

Definition at line 293 of file dynahash.c.

Referenced by hash_create().

294 {
295  return strncmp(key1, key2, keysize - 1);
296 }

Variable Documentation

◆ CurrentDynaHashCxt

MemoryContext CurrentDynaHashCxt = NULL
static

Definition at line 275 of file dynahash.c.

Referenced by hash_create().

◆ num_seq_scans

int num_seq_scans = 0
static

◆ seq_scan_level

int seq_scan_level[MAX_SEQ_SCANS]
static

Definition at line 1781 of file dynahash.c.

Referenced by AtEOSubXact_HashTables(), deregister_seq_scan(), and register_seq_scan().

◆ seq_scan_tables

HTAB* seq_scan_tables[MAX_SEQ_SCANS]
static

Definition at line 1780 of file dynahash.c.