PostgreSQL Source Code interpretation (247)-HTAB dynamic extension Diagram # 1
This section provides a brief introduction to the dynamic extension of HTAB in PostgreSQL, which is part 1.
Data structure / * * Top control structure for a hashtable-in a shared table, each backend * has its own copy (OK since no fields change at runtime) * hash table top-level control structure. * in this shared hash table, each background process has its own copy * (there is no problem because no fields will change during the run time after fork comes out) * / struct HTAB {/ / points to the shared control information HASHHDR * hctl; / * = > shared control information * / / segment directory HASHSEGMENT * dir / * directory of segment starts * / / Hash function HashValueFunc hash; / * hash function * / / Hash key comparison function HashCompareFunc match; / * key comparison function * / / Hash key copy function HashCopyFunc keycopy; / * key copying function * / / memory allocator HashAllocFunc alloc; / * memory allocator * / memory context MemoryContext hcxt / * memory context if default allocator used * / / Table name (for error messages) char * tabname; / * table name (for error messages) * / / if in shared memory, T bool isshared; / * true if table is in shared memory * / / if T, fixed size cannot extend bool isfixed / * if true, don't enlarge * / * freezing a shared table isn't allowed, so we can keep state here * / / do not allow freezing of shared tables, so the relevant status bool frozen will be saved here / * true = no more inserts allowed * / / * We keep local copies of these fixed values to reduce contention * / / keep local copies of these fixed values to reduce conflict / / hash key length (in bytes) Size keysize; / * hash key length in bytes * / / segment size, which must be a power long ssize of 2 / * segment size-must be power of 2 * / / segment offset, logarithmic int sshift; / * segment shift of ssize = log2 (ssize) * /}; / * Header structure for a hash table-contains all changeable info * the header structure of the hash table-stores all variable information * * In a shared-memory hash table, the HASHHDR is in shared memory, while * each backend has a local HTAB struct. For a non-shared table, there isn't * any functional difference between HASHHDR and HTAB, but we separate them * anyway to share code between shared and non-shared tables. * in the shared memory hash table, HASHHDR is located in shared memory, and each background process has a local HTAB structure. There is no functional difference between HASHHDR and HTAB for non-shared hash tables, * but we distinguish them into shared and non-shared tables anyway. * / struct HASHHDR {/ * The freelist can become a point of contention in high-concurrency hash * tables, so we use an array of freelists, each with its own mutex and * nentries count, instead of just a single one. Although the freelists * normally operate independently, we will scavenge entries from freelists * other than a hashcode's default freelist when necessary. * in highly concurrent hash tables, free linked lists will become a hot spot of competition, so we use free linked list arrays. * each element in the array has its own mutex and entry statistics instead of using one. * * If the hash table is not partitioned, only freeList [0] is used and its * spinlock is not used at all; callers' locking is assumed sufficient. * if the hash table is not partitioned, then only the freelist [0] element is useful, and the spin lock is of no use at all; * the caller lock is considered sufficient for OK. * / * Number of freelists to be used for a partitioned hash table * / / # define NUM_FREELISTS 32 FreeListData freelist [num _ FREELISTS]; / * These fields can change, but not in a partitioned table * / / these field fields can be changed, but not applicable to partitioned tables / * Also, dsize can't change in a shared table, even if unpartitioned * / / at the same time, even for non-partitioned tables, the dsize of shared tables cannot change / / directory size long dsize / * directory size * / / allocated segment size (