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;
231 ptr =
ctl->custom_data[
idx];
311 shheader->
magic = 0xdeadbeef;
412 Assert(entry_ref->shared_entry != NULL);
413 Assert(entry_ref->shared_stats != NULL);
415 Assert(entry_ref->shared_stats->magic == 0xdeadbeef);
444 Assert(create || created_entry == NULL);
452 if (created_entry != NULL)
453 *created_entry =
false;
472 Assert(entry_ref != NULL);
480 if (create && !shhashent)
495 if (created_entry != NULL)
496 *created_entry =
true;
520 if (shhashent->
dropped && create)
533 if (created_entry != NULL)
534 *created_entry =
true;
557 bool discard_pending)
559 if (entry_ref && entry_ref->
pending)
564 elog(
ERROR,
"releasing ref with pending data");
593 elog(
ERROR,
"could not find just referenced shared stats entry");
603 elog(
ERROR,
"entry ref vanished before deletion");
687 pgstat_entry_ref_hash_iterator
i;
709 if (entry_ref->
pending != NULL)
722 pgstat_entry_ref_hash_iterator
i;
735 if (match && !match(ent, match_data))
823 "trying to drop stats entry already dropped: kind=%s dboid=%u objid=%llu refcount=%u",
826 (
unsigned long long) shent->
key.
objid,
852 uint64 not_freed_count = 0;
893 if (not_freed_count > 0)
950 uint64 not_freed_count = 0;
963 if (not_freed_count > 0)
1019 if (!do_reset(p, match_data))
1051 "PgStat Shared Ref",
1056 "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 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)
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
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)
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
void dsa_free(dsa_area *area, dsa_pointer dp)
size_t dsa_minimum_size(void)
void dsa_pin(dsa_area *area)
#define dsa_allocate0(area, size)
#define dsa_create_in_place(place, size, tranch_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)
void dshash_seq_term(dshash_seq_status *status)
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
dshash_table * dshash_attach(dsa_area *area, const dshash_parameters *params, dshash_table_handle handle, void *arg)
dshash_table * dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
void * dshash_seq_next(dshash_seq_status *status)
void dshash_delete_current(dshash_seq_status *status)
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
void LWLockRelease(LWLock *lock)
void LWLockInitialize(LWLock *lock, int tranche_id)
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
void pfree(void *pointer)
MemoryContext TopMemoryContext
void * MemoryContextAlloc(MemoryContext context, Size size)
#define AllocSetContextCreate
#define ALLOCSET_SMALL_SIZES
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref)
PgStat_LocalState pgStatLocal
#define PGSTAT_KIND_CUSTOM_MAX
static bool pgstat_is_kind_builtin(PgStat_Kind kind)
#define PGSTAT_KIND_CUSTOM_MIN
#define PGSTAT_KIND_DATABASE
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()
struct PgStat_HashKey PgStat_HashKey
static size_t pgstat_get_entry_len(PgStat_Kind kind)
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)
PgStatShared_Common * pgstat_init_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
static Size pgstat_dsa_init_size(void)
static bool match_kind(PgStatShared_HashEntry *p, Datum match_data)
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 void pgstat_drop_database_and_contents(Oid dboid)
static PgStatShared_Common * pgstat_reinit_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)
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
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)
struct PgStat_EntryRefHashEntry PgStat_EntryRefHashEntry
static void pgstat_release_matching_entry_refs(bool discard_pending, ReleaseMatchCB match, Datum match_data)
bool(* ReleaseMatchCB)(PgStat_EntryRefHashEntry *, Datum data)
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid, bool nowait)
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, 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)
MemoryContextSwitchTo(old_ctx)
void * ShmemAlloc(Size size)
Size add_size(Size s1, Size s2)
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
pg_atomic_uint32 refcount
PgStat_EntryRef * entry_ref
PgStatShared_Common * shared_stats
PgStatShared_HashEntry * shared_entry
void(* reset_timestamp_cb)(PgStatShared_Common *header, TimestampTz ts)
void(* init_shmem_cb)(void *stats)
dshash_table * shared_hash
PgStat_ShmemControl * shmem
dshash_table_handle hash_handle
pg_atomic_uint64 gc_request_count