21#define PGSTAT_ENTRY_REF_HASH_SIZE 128
33#define SH_PREFIX pgstat_entry_ref_hash
34#define SH_ELEMENT_TYPE PgStat_EntryRefHashEntry
35#define SH_KEY_TYPE PgStat_HashKey
37#define SH_HASH_KEY(tb, key) \
38 pgstat_hash_hash_key(&key, sizeof(PgStat_HashKey), NULL)
39#define SH_EQUAL(tb, a, b) \
40 pgstat_cmp_hash_key(&a, &b, sizeof(PgStat_HashKey), NULL) == 0
41#define SH_SCOPE static inline
170 char *p = (
char *)
ctl;
182 ctl->raw_dsa_area = p;
237 ptr =
ctl->custom_data[
idx];
452 Assert(entry_ref->shared_stats->magic == 0xdeadbeef);
546 key.kind, key.dboid, key.objid)));
615 if (entry_ref && entry_ref->
pending)
620 elog(
ERROR,
"releasing ref with pending data");
649 elog(
ERROR,
"could not find just referenced shared stats entry");
676 elog(
ERROR,
"entry ref vanished before deletion");
854 return ent->key.dboid == dboid;
917 "trying to drop stats entry already dropped: kind=%s dboid=%u objid=%" PRIu64 " refcount=%u generation=%u",
923 shent->dropped =
true;
1105 kind_info->reset_timestamp_cb(header, ts);
1179 "PgStat Shared Ref",
1184 "PgStat Shared Ref Hash",
Datum idx(PG_FUNCTION_ARGS)
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
static uint32 pg_atomic_fetch_sub_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
static uint32 pg_atomic_fetch_add_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
static uint64 pg_atomic_fetch_add_u64(volatile pg_atomic_uint64 *ptr, int64 add_)
static uint64 pg_atomic_sub_fetch_u64(volatile pg_atomic_uint64 *ptr, int64 sub_)
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
#define PG_USED_FOR_ASSERTS_ONLY
#define Assert(condition)
#define OidIsValid(objectId)
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
dsa_pointer dsa_allocate_extended(dsa_area *area, size_t size, int flags)
void dsa_release_in_place(void *place)
void dsa_set_size_limit(dsa_area *area, size_t limit)
void dsa_pin_mapping(dsa_area *area)
void dsa_detach(dsa_area *area)
void dsa_free(dsa_area *area, dsa_pointer dp)
size_t dsa_minimum_size(void)
void dsa_pin(dsa_area *area)
#define dsa_create_in_place(place, size, tranche_id, segment)
#define InvalidDsaPointer
void dshash_memcpy(void *dest, const void *src, size_t size, void *arg)
void dshash_delete_entry(dshash_table *hash_table, void *entry)
void dshash_release_lock(dshash_table *hash_table, void *entry)
void dshash_detach(dshash_table *hash_table)
void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table, bool exclusive)
void * dshash_find(dshash_table *hash_table, const void *key, bool exclusive)
dshash_table_handle dshash_get_hash_table_handle(dshash_table *hash_table)
dshash_table * dshash_attach(dsa_area *area, const dshash_parameters *params, dshash_table_handle handle, void *arg)
void dshash_seq_term(dshash_seq_status *status)
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
void * dshash_seq_next(dshash_seq_status *status)
dshash_table * dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
void dshash_delete_current(dshash_seq_status *status)
int errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
void LWLockRelease(LWLock *lock)
void LWLockInitialize(LWLock *lock, int tranche_id)
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
void * MemoryContextAlloc(MemoryContext context, Size size)
void pfree(void *pointer)
MemoryContext TopMemoryContext
#define AllocSetContextCreate
#define ALLOCSET_SMALL_SIZES
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref)
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
PgStat_LocalState pgStatLocal
static uint32 pgstat_hash_hash_key(const void *d, size_t size, void *arg)
static int pgstat_cmp_hash_key(const void *a, const void *b, size_t size, void *arg)
static void * pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry)
#define pgstat_assert_is_up()
static size_t pgstat_get_entry_len(PgStat_Kind kind)
#define PGSTAT_KIND_CUSTOM_MAX
static bool pgstat_is_kind_builtin(PgStat_Kind kind)
#define PGSTAT_KIND_CUSTOM_MIN
#define PGSTAT_KIND_DATABASE
static bool match_db(PgStat_EntryRefHashEntry *ent, Datum match_data)
static void pgstat_setup_memcxt(void)
void pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts)
void pgstat_request_entry_refs_gc(void)
static void pgstat_release_db_entry_refs(Oid dboid)
static Size pgstat_dsa_init_size(void)
static bool match_kind(PgStatShared_HashEntry *p, Datum match_data)
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)
static MemoryContext pgStatEntryRefHashContext
void StatsShmemInit(void)
bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
void pgstat_attach_shmem(void)
static void pgstat_free_entry(PgStatShared_HashEntry *shent, dshash_seq_status *hstat)
static int pgStatSharedRefAge
#define PGSTAT_ENTRY_REF_HASH_SIZE
bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
void pgstat_reset_entry(PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts)
static void shared_stat_reset_contents(PgStat_Kind kind, PgStatShared_Common *header, TimestampTz ts)
static void pgstat_setup_shared_refs(void)
static void pgstat_release_entry_ref(PgStat_HashKey key, PgStat_EntryRef *entry_ref, bool discard_pending)
static PgStatShared_Common * pgstat_reinit_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
void pgstat_drop_matching_entries(bool(*do_drop)(PgStatShared_HashEntry *, Datum), Datum match_data)
static void pgstat_drop_database_and_contents(Oid dboid)
static pgstat_entry_ref_hash_hash * pgStatEntryRefHash
Size StatsShmemSize(void)
static void pgstat_acquire_entry_ref(PgStat_EntryRef *entry_ref, PgStatShared_HashEntry *shhashent, PgStatShared_Common *shheader)
static MemoryContext pgStatSharedRefContext
PgStatShared_Common * pgstat_init_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
void pgstat_reset_matching_entries(bool(*do_reset)(PgStatShared_HashEntry *, Datum), Datum match_data, TimestampTz ts)
void pgstat_drop_all_entries(void)
static const dshash_parameters dsh_params
static bool pgstat_get_entry_ref_cached(PgStat_HashKey key, PgStat_EntryRef **entry_ref_p)
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
static void pgstat_release_all_entry_refs(bool discard_pending)
static void pgstat_release_matching_entry_refs(bool discard_pending, ReleaseMatchCB match, Datum match_data)
bool(* ReleaseMatchCB)(PgStat_EntryRefHashEntry *, Datum data)
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid, bool nowait)
static void pgstat_gc_entry_refs(void)
static bool pgstat_need_entry_refs_gc(void)
static bool pgstat_drop_entry_internal(PgStatShared_HashEntry *shent, dshash_seq_status *hstat)
void pgstat_detach_shmem(void)
static Oid DatumGetObjectId(Datum X)
static Datum ObjectIdGetDatum(Oid X)
static Datum Int32GetDatum(int32 X)
static int32 DatumGetInt32(Datum X)
Size add_size(Size s1, Size s2)
void * ShmemAlloc(Size size)
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
pg_atomic_uint32 refcount
pg_atomic_uint32 generation
PgStat_EntryRef * entry_ref
PgStatShared_Common * shared_stats
PgStatShared_HashEntry * shared_entry
dshash_table * shared_hash
PgStat_ShmemControl * shmem
dshash_table_handle hash_handle
pg_atomic_uint64 gc_request_count
pg_atomic_uint64 entry_counts[PGSTAT_KIND_MAX]