/*-------------------------------------------------------------------------
|
*
|
* cfs_shmem_internal.h
|
* State and helpers shared between the files of the CFS shared-memory
|
* layer (cfs_shmem.c today; cfs_map_pool.c / cfs_relmeta.c after the
|
* split). NOT for inclusion outside the layer.
|
*
|
* Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
|
* Portions Copyright (c) 2026, Diasoft
|
*
|
* src/include/storage/cfs/cfs_shmem_internal.h
|
*
|
*-------------------------------------------------------------------------
|
*/
|
|
#ifndef CFS_SHMEM_INTERNAL_H
|
#define CFS_SHMEM_INTERNAL_H
|
|
#include "postgres.h"
|
|
#include "storage/cfs/cfs_shmem.h"
|
|
/*
|
* The partition count is a power of two; the low CFS_PART_BITS bits of a
|
* relation hash select a statistics / metadata partition (CFS_PART_MASK),
|
* and the remaining bits feed the per-partition hash chains. Shared by
|
* the relmeta and stats sides of the layer.
|
*/
|
#define CFS_PART_BITS 6
|
#define CFS_PART_MASK (CFS_MAP_CACHE_NUM_PARTITIONS - 1)
|
|
/*
|
* The mask must cover exactly the low CFS_PART_BITS bits of the hash.
|
* The two constants live in different headers, so changing the
|
* partition count without a matching CFS_PART_BITS update would
|
* silently break the "bucket does not correlate with the partition"
|
* contract (cfs_meta_bucket / cfs_stats_bucket shift the hash by
|
* CFS_PART_BITS), so pin the pair at compile time.
|
*/
|
StaticAssertDecl(CFS_MAP_CACHE_NUM_PARTITIONS == (1 << CFS_PART_BITS),
|
"CFS_PART_BITS must match CFS_MAP_CACHE_NUM_PARTITIONS");
|
|
/*
|
* Hash chains inside partitions (QDB-1516 Д2).
|
*
|
* Each partition of the metadata and statistics slot tables keeps one
|
* bucket-head array with as many buckets as the partition has slots; a
|
* key's bucket is derived from the hash bits that do not select the
|
* partition (see cfs_meta_bucket() / cfs_stats_bucket()). Invariants,
|
* shared by both domains:
|
*
|
* I1 every occupied slot (hash != 0) belongs to exactly one chain of
|
* its own partition; a free slot belongs to none, and its chain_next
|
* value is undefined;
|
* I2 chain mutations (link, unlink, head rebuild) happen only under
|
* LW_EXCLUSIVE of the partition; chain search runs under LW_SHARED
|
* or LW_EXCLUSIVE of that same partition;
|
* I3 a slot is unlinked from its chain BEFORE it is cleared (MemSet);
|
* the clear also resets chain_next, which is safe only after the
|
* unlink.
|
*/
|
|
/* The control block, owned by cfs_shmem.c. */
|
extern CfsShmemControl *cfs_control;
|
|
/* Compare two relation locators for equality (including backend). */
|
static inline bool
|
cfs_rlbe_eq(RelFileLocatorBackend a, RelFileLocatorBackend b)
|
{
|
return a.locator.spcOid == b.locator.spcOid &&
|
a.locator.dbOid == b.locator.dbOid &&
|
a.locator.relNumber == b.locator.relNumber &&
|
a.backend == b.backend;
|
}
|
|
/*
|
* Hash of the relation key. The result is guaranteed non-zero because a
|
* value of 0 is reserved to mark an empty slot.
|
*/
|
static inline uint32
|
cfs_cache_hash_rel(RelFileLocatorBackend rlocator, ForkNumber forknum)
|
{
|
uint32 h;
|
|
h = rlocator.locator.spcOid;
|
h = (h << 5) - h + rlocator.locator.dbOid;
|
h = (h << 5) - h + rlocator.locator.relNumber;
|
h = (h << 5) - h + (uint32) rlocator.backend;
|
h = (h << 5) - h + (uint32) forknum;
|
return (h == 0) ? 1 : h;
|
}
|
|
/*
|
* Per-partition LRU access clocks (QDB-1516 Д1). The map-domain array is
|
* updated only while holding cfs_map_locks[part] in LW_EXCLUSIVE mode
|
* (every map-domain touch point does), so a plain increment is enough.
|
* The stats-domain array is updated while holding cfs_stats_locks[part] in
|
* either mode — the shared-lock accounting path of the atomic counters
|
* ticks it — so its cells are atomic. Both arrays live in their own
|
* CACHELINEALIGN-ed shared-memory allocations, so a clock never shares a
|
* cache line with the LWLockPadded lock arrays or with the other domain's
|
* clocks.
|
*/
|
extern uint64 *cfs_map_clocks; /* [CFS_MAP_CACHE_NUM_PARTITIONS] */
|
extern pg_atomic_uint64 *cfs_stats_clocks; /* [CFS_MAP_CACHE_NUM_PARTITIONS] */
|
|
/*
|
* Advance and return the map-domain access clock of a partition. The
|
* caller must hold cfs_map_locks[part] in LW_EXCLUSIVE mode.
|
*/
|
extern uint64 cfs_map_clock_tick(int part);
|
|
/*
|
* Advance and return the stats-domain access clock of a partition. The
|
* caller must hold cfs_stats_locks[part] in either mode.
|
*/
|
extern uint64 cfs_stats_clock_tick(int part);
|
|
#endif /* CFS_SHMEM_INTERNAL_H */
|