PostgreSQL Source Code git master
pgstat_shmem.c File Reference
#include "postgres.h"
#include "pgstat.h"
#include "storage/shmem.h"
#include "utils/memutils.h"
#include "utils/pgstat_internal.h"
#include "lib/simplehash.h"
Include dependency graph for pgstat_shmem.c:

Go to the source code of this file.

Data Structures

struct  PgStat_EntryRefHashEntry
 

Macros

#define PGSTAT_ENTRY_REF_HASH_SIZE   128
 
#define SH_PREFIX   pgstat_entry_ref_hash
 
#define SH_ELEMENT_TYPE   PgStat_EntryRefHashEntry
 
#define SH_KEY_TYPE   PgStat_HashKey
 
#define SH_KEY   key
 
#define SH_HASH_KEY(tb, key)    pgstat_hash_hash_key(&key, sizeof(PgStat_HashKey), NULL)
 
#define SH_EQUAL(tb, a, b)    pgstat_cmp_hash_key(&a, &b, sizeof(PgStat_HashKey), NULL) == 0
 
#define SH_SCOPE   static inline
 
#define SH_DEFINE
 
#define SH_DECLARE
 

Typedefs

typedef struct PgStat_EntryRefHashEntry PgStat_EntryRefHashEntry
 
typedef bool(* ReleaseMatchCB) (PgStat_EntryRefHashEntry *, Datum data)
 

Functions

static void pgstat_drop_database_and_contents (Oid dboid)
 
static void pgstat_free_entry (PgStatShared_HashEntry *shent, dshash_seq_status *hstat)
 
static void pgstat_release_entry_ref (PgStat_HashKey key, PgStat_EntryRef *entry_ref, bool discard_pending)
 
static bool pgstat_need_entry_refs_gc (void)
 
static void pgstat_gc_entry_refs (void)
 
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)
 
static void pgstat_setup_memcxt (void)
 
static Size pgstat_dsa_init_size (void)
 
Size StatsShmemSize (void)
 
void StatsShmemInit (void)
 
void pgstat_attach_shmem (void)
 
void pgstat_detach_shmem (void)
 
PgStatShared_Commonpgstat_init_entry (PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
 
static PgStatShared_Commonpgstat_reinit_entry (PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
 
static void pgstat_setup_shared_refs (void)
 
static void pgstat_acquire_entry_ref (PgStat_EntryRef *entry_ref, PgStatShared_HashEntry *shhashent, PgStatShared_Common *shheader)
 
static bool pgstat_get_entry_ref_cached (PgStat_HashKey key, PgStat_EntryRef **entry_ref_p)
 
PgStat_EntryRefpgstat_get_entry_ref (PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)
 
bool pgstat_lock_entry (PgStat_EntryRef *entry_ref, bool nowait)
 
bool pgstat_lock_entry_shared (PgStat_EntryRef *entry_ref, bool nowait)
 
void pgstat_unlock_entry (PgStat_EntryRef *entry_ref)
 
PgStat_EntryRefpgstat_get_entry_ref_locked (PgStat_Kind kind, Oid dboid, uint64 objid, bool nowait)
 
void pgstat_request_entry_refs_gc (void)
 
static bool match_db (PgStat_EntryRefHashEntry *ent, Datum match_data)
 
static void pgstat_release_db_entry_refs (Oid dboid)
 
static bool pgstat_drop_entry_internal (PgStatShared_HashEntry *shent, dshash_seq_status *hstat)
 
bool pgstat_drop_entry (PgStat_Kind kind, Oid dboid, uint64 objid)
 
void pgstat_drop_all_entries (void)
 
static void shared_stat_reset_contents (PgStat_Kind kind, PgStatShared_Common *header, TimestampTz ts)
 
void pgstat_reset_entry (PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts)
 
void pgstat_reset_matching_entries (bool(*do_reset)(PgStatShared_HashEntry *, Datum), Datum match_data, TimestampTz ts)
 
static bool match_kind (PgStatShared_HashEntry *p, Datum match_data)
 
void pgstat_reset_entries_of_kind (PgStat_Kind kind, TimestampTz ts)
 

Variables

static const dshash_parameters dsh_params
 
static pgstat_entry_ref_hash_hash * pgStatEntryRefHash = NULL
 
static int pgStatSharedRefAge = 0
 
static MemoryContext pgStatSharedRefContext = NULL
 
static MemoryContext pgStatEntryRefHashContext = NULL
 

Macro Definition Documentation

◆ PGSTAT_ENTRY_REF_HASH_SIZE

#define PGSTAT_ENTRY_REF_HASH_SIZE   128

Definition at line 21 of file pgstat_shmem.c.

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 43 of file pgstat_shmem.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 42 of file pgstat_shmem.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   PgStat_EntryRefHashEntry

Definition at line 34 of file pgstat_shmem.c.

◆ SH_EQUAL

#define SH_EQUAL (   tb,
  a,
  b 
)     pgstat_cmp_hash_key(&a, &b, sizeof(PgStat_HashKey), NULL) == 0

Definition at line 39 of file pgstat_shmem.c.

◆ SH_HASH_KEY

#define SH_HASH_KEY (   tb,
  key 
)     pgstat_hash_hash_key(&key, sizeof(PgStat_HashKey), NULL)

Definition at line 37 of file pgstat_shmem.c.

◆ SH_KEY

#define SH_KEY   key

Definition at line 36 of file pgstat_shmem.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   PgStat_HashKey

Definition at line 35 of file pgstat_shmem.c.

◆ SH_PREFIX

#define SH_PREFIX   pgstat_entry_ref_hash

Definition at line 33 of file pgstat_shmem.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 41 of file pgstat_shmem.c.

Typedef Documentation

◆ PgStat_EntryRefHashEntry

◆ ReleaseMatchCB

typedef bool(* ReleaseMatchCB) (PgStat_EntryRefHashEntry *, Datum data)

Definition at line 55 of file pgstat_shmem.c.

Function Documentation

◆ match_db()

static bool match_db ( PgStat_EntryRefHashEntry ent,
Datum  match_data 
)
static

Definition at line 805 of file pgstat_shmem.c.

806{
807 Oid dboid = DatumGetObjectId(match_data);
808
809 return ent->key.dboid == dboid;
810}
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:247
unsigned int Oid
Definition: postgres_ext.h:32

References DatumGetObjectId(), PgStat_HashKey::dboid, and PgStat_EntryRefHashEntry::key.

Referenced by pgstat_release_db_entry_refs().

◆ match_kind()

static bool match_kind ( PgStatShared_HashEntry p,
Datum  match_data 
)
static

Definition at line 1085 of file pgstat_shmem.c.

1086{
1087 return p->key.kind == DatumGetInt32(match_data);
1088}
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:207
PgStat_Kind kind

References DatumGetInt32(), PgStatShared_HashEntry::key, and PgStat_HashKey::kind.

Referenced by pgstat_reset_entries_of_kind().

◆ pgstat_acquire_entry_ref()

static void pgstat_acquire_entry_ref ( PgStat_EntryRef entry_ref,
PgStatShared_HashEntry shhashent,
PgStatShared_Common shheader 
)
static

Definition at line 368 of file pgstat_shmem.c.

371{
372 Assert(shheader->magic == 0xdeadbeef);
373 Assert(pg_atomic_read_u32(&shhashent->refcount) > 0);
374
375 pg_atomic_fetch_add_u32(&shhashent->refcount, 1);
376
378
379 entry_ref->shared_stats = shheader;
380 entry_ref->shared_entry = shhashent;
381 entry_ref->generation = pg_atomic_read_u32(&shhashent->generation);
382}
static uint32 pg_atomic_fetch_add_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:366
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:239
#define Assert(condition)
Definition: c.h:815
void dshash_release_lock(dshash_table *hash_table, void *entry)
Definition: dshash.c:558
PgStat_LocalState pgStatLocal
Definition: pgstat.c:213
pg_atomic_uint32 refcount
pg_atomic_uint32 generation
PgStatShared_Common * shared_stats
PgStatShared_HashEntry * shared_entry
dshash_table * shared_hash

References Assert, dshash_release_lock(), PgStatShared_HashEntry::generation, PgStat_EntryRef::generation, PgStatShared_Common::magic, pg_atomic_fetch_add_u32(), pg_atomic_read_u32(), pgStatLocal, PgStatShared_HashEntry::refcount, PgStat_EntryRef::shared_entry, PgStat_LocalState::shared_hash, and PgStat_EntryRef::shared_stats.

Referenced by pgstat_get_entry_ref().

◆ pgstat_attach_shmem()

void pgstat_attach_shmem ( void  )

Definition at line 244 of file pgstat_shmem.c.

245{
246 MemoryContext oldcontext;
247
248 Assert(pgStatLocal.dsa == NULL);
249
250 /* stats shared memory persists for the backend lifetime */
252
254 NULL);
256
259
260 MemoryContextSwitchTo(oldcontext);
261}
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition: dsa.c:545
void dsa_pin_mapping(dsa_area *area)
Definition: dsa.c:635
dshash_table * dshash_attach(dsa_area *area, const dshash_parameters *params, dshash_table_handle handle, void *arg)
Definition: dshash.c:270
MemoryContext TopMemoryContext
Definition: mcxt.c:149
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
static const dshash_parameters dsh_params
Definition: pgstat_shmem.c:62
PgStat_ShmemControl * shmem
dshash_table_handle hash_handle

References Assert, PgStat_LocalState::dsa, dsa_attach_in_place(), dsa_pin_mapping(), dsh_params, dshash_attach(), PgStat_ShmemControl::hash_handle, MemoryContextSwitchTo(), pgStatLocal, PgStat_ShmemControl::raw_dsa_area, PgStat_LocalState::shared_hash, PgStat_LocalState::shmem, and TopMemoryContext.

Referenced by pgstat_initialize().

◆ pgstat_detach_shmem()

void pgstat_detach_shmem ( void  )

Definition at line 264 of file pgstat_shmem.c.

265{
267
268 /* we shouldn't leave references to shared stats */
270
273
275
276 /*
277 * dsa_detach() does not decrement the DSA reference count as no segment
278 * was provided to dsa_attach_in_place(), causing no cleanup callbacks to
279 * be registered. Hence, release it manually now.
280 */
282
283 pgStatLocal.dsa = NULL;
284}
void dsa_release_in_place(void *place)
Definition: dsa.c:605
void dsa_detach(dsa_area *area)
Definition: dsa.c:1952
void dshash_detach(dshash_table *hash_table)
Definition: dshash.c:307
static void pgstat_release_all_entry_refs(bool discard_pending)
Definition: pgstat_shmem.c:793

References Assert, PgStat_LocalState::dsa, dsa_detach(), dsa_release_in_place(), dshash_detach(), pgstat_release_all_entry_refs(), pgStatLocal, PgStat_ShmemControl::raw_dsa_area, PgStat_LocalState::shared_hash, and PgStat_LocalState::shmem.

Referenced by pgstat_shutdown_hook().

◆ pgstat_drop_all_entries()

void pgstat_drop_all_entries ( void  )

Definition at line 997 of file pgstat_shmem.c.

998{
999 dshash_seq_status hstat;
1001 uint64 not_freed_count = 0;
1002
1004 while ((ps = dshash_seq_next(&hstat)) != NULL)
1005 {
1006 if (ps->dropped)
1007 continue;
1008
1009 if (!pgstat_drop_entry_internal(ps, &hstat))
1010 not_freed_count++;
1011 }
1012 dshash_seq_term(&hstat);
1013
1014 if (not_freed_count > 0)
1016}
uint64_t uint64
Definition: c.h:489
void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table, bool exclusive)
Definition: dshash.c:638
void dshash_seq_term(dshash_seq_status *status)
Definition: dshash.c:747
void * dshash_seq_next(dshash_seq_status *status)
Definition: dshash.c:657
struct parser_state ps
void pgstat_request_entry_refs_gc(void)
Definition: pgstat_shmem.c:700
static bool pgstat_drop_entry_internal(PgStatShared_HashEntry *shent, dshash_seq_status *hstat)
Definition: pgstat_shmem.c:852

References dshash_seq_init(), dshash_seq_next(), dshash_seq_term(), pgstat_drop_entry_internal(), pgstat_request_entry_refs_gc(), pgStatLocal, ps, and PgStat_LocalState::shared_hash.

Referenced by pgstat_reset_after_failure().

◆ pgstat_drop_database_and_contents()

static void pgstat_drop_database_and_contents ( Oid  dboid)
static

Definition at line 892 of file pgstat_shmem.c.

893{
894 dshash_seq_status hstat;
896 uint64 not_freed_count = 0;
897
898 Assert(OidIsValid(dboid));
899
901
902 /*
903 * This backend might very well be the only backend holding a reference to
904 * about-to-be-dropped entries. Ensure that we're not preventing it from
905 * being cleaned up till later.
906 *
907 * Doing this separately from the dshash iteration below avoids having to
908 * do so while holding a partition lock on the shared hashtable.
909 */
911
912 /* some of the dshash entries are to be removed, take exclusive lock. */
914 while ((p = dshash_seq_next(&hstat)) != NULL)
915 {
916 if (p->dropped)
917 continue;
918
919 if (p->key.dboid != dboid)
920 continue;
921
922 if (!pgstat_drop_entry_internal(p, &hstat))
923 {
924 /*
925 * Even statistics for a dropped database might currently be
926 * accessed (consider e.g. database stats for pg_stat_database).
927 */
928 not_freed_count++;
929 }
930 }
931 dshash_seq_term(&hstat);
932
933 /*
934 * If some of the stats data could not be freed, signal the reference
935 * holders to run garbage collection of their cached pgStatLocal.shmem.
936 */
937 if (not_freed_count > 0)
939}
#define OidIsValid(objectId)
Definition: c.h:732
static void pgstat_release_db_entry_refs(Oid dboid)
Definition: pgstat_shmem.c:813

References Assert, PgStat_HashKey::dboid, PgStatShared_HashEntry::dropped, dshash_seq_init(), dshash_seq_next(), dshash_seq_term(), PgStatShared_HashEntry::key, OidIsValid, pgstat_drop_entry_internal(), pgstat_release_db_entry_refs(), pgstat_request_entry_refs_gc(), pgStatLocal, and PgStat_LocalState::shared_hash.

Referenced by pgstat_drop_entry().

◆ pgstat_drop_entry()

bool pgstat_drop_entry ( PgStat_Kind  kind,
Oid  dboid,
uint64  objid 
)

Definition at line 953 of file pgstat_shmem.c.

954{
957 bool freed = true;
958
959 /* clear padding */
960 memset(&key, 0, sizeof(struct PgStat_HashKey));
961
962 key.kind = kind;
963 key.dboid = dboid;
964 key.objid = objid;
965
966 /* delete local reference */
968 {
969 PgStat_EntryRefHashEntry *lohashent =
970 pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, key);
971
972 if (lohashent)
973 pgstat_release_entry_ref(lohashent->key, lohashent->entry_ref,
974 true);
975 }
976
977 /* mark entry in shared hashtable as deleted, drop if possible */
978 shent = dshash_find(pgStatLocal.shared_hash, &key, true);
979 if (shent)
980 {
981 freed = pgstat_drop_entry_internal(shent, NULL);
982
983 /*
984 * Database stats contain other stats. Drop those as well when
985 * dropping the database. XXX: Perhaps this should be done in a
986 * slightly more principled way? But not obvious what that'd look
987 * like, and so far this is the only case...
988 */
989 if (key.kind == PGSTAT_KIND_DATABASE)
991 }
992
993 return freed;
994}
void * dshash_find(dshash_table *hash_table, const void *key, bool exclusive)
Definition: dshash.c:390
#define PGSTAT_KIND_DATABASE
Definition: pgstat_kind.h:27
static void pgstat_release_entry_ref(PgStat_HashKey key, PgStat_EntryRef *entry_ref, bool discard_pending)
Definition: pgstat_shmem.c:576
static void pgstat_drop_database_and_contents(Oid dboid)
Definition: pgstat_shmem.c:892
static pgstat_entry_ref_hash_hash * pgStatEntryRefHash
Definition: pgstat_shmem.c:82
PgStat_EntryRef * entry_ref
Definition: pgstat_shmem.c:28

References dshash_find(), PgStat_EntryRefHashEntry::entry_ref, PgStat_EntryRefHashEntry::key, sort-test::key, pgstat_drop_database_and_contents(), pgstat_drop_entry_internal(), PGSTAT_KIND_DATABASE, pgstat_release_entry_ref(), pgStatEntryRefHash, pgStatLocal, and PgStat_LocalState::shared_hash.

Referenced by AtEOSubXact_PgStat_DroppedStats(), AtEOXact_PgStat_DroppedStats(), pgstat_drop_inj(), pgstat_drop_replslot(), pgstat_execute_transactional_drops(), pgstat_init_function_usage(), and pgstat_shutdown_hook().

◆ pgstat_drop_entry_internal()

static bool pgstat_drop_entry_internal ( PgStatShared_HashEntry shent,
dshash_seq_status hstat 
)
static

Definition at line 852 of file pgstat_shmem.c.

854{
855 Assert(shent->body != InvalidDsaPointer);
856
857 /* should already have released local reference */
859 Assert(!pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, shent->key));
860
861 /*
862 * Signal that the entry is dropped - this will eventually cause other
863 * backends to release their references.
864 */
865 if (shent->dropped)
866 elog(ERROR,
867 "trying to drop stats entry already dropped: kind=%s dboid=%u objid=%llu refcount=%u",
869 shent->key.dboid,
870 (unsigned long long) shent->key.objid,
872 shent->dropped = true;
873
874 /* release refcount marking entry as not dropped */
875 if (pg_atomic_sub_fetch_u32(&shent->refcount, 1) == 0)
876 {
877 pgstat_free_entry(shent, hstat);
878 return true;
879 }
880 else
881 {
882 if (!hstat)
884 return false;
885 }
886}
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:439
#define InvalidDsaPointer
Definition: dsa.h:78
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1464
static void pgstat_free_entry(PgStatShared_HashEntry *shent, dshash_seq_status *hstat)
Definition: pgstat_shmem.c:827
const char *const name

References Assert, PgStatShared_HashEntry::body, PgStat_HashKey::dboid, PgStatShared_HashEntry::dropped, dshash_release_lock(), elog, ERROR, InvalidDsaPointer, PgStatShared_HashEntry::key, PgStat_HashKey::kind, PgStat_KindInfo::name, PgStat_HashKey::objid, pg_atomic_read_u32(), pg_atomic_sub_fetch_u32(), pgstat_free_entry(), pgstat_get_kind_info(), pgStatEntryRefHash, pgStatLocal, PgStatShared_HashEntry::refcount, and PgStat_LocalState::shared_hash.

Referenced by pgstat_drop_all_entries(), pgstat_drop_database_and_contents(), and pgstat_drop_entry().

◆ pgstat_dsa_init_size()

static Size pgstat_dsa_init_size ( void  )
static

Definition at line 106 of file pgstat_shmem.c.

107{
108 Size sz;
109
110 /*
111 * The dshash header / initial buckets array needs to fit into "plain"
112 * shared memory, but it's beneficial to not need dsm segments
113 * immediately. A size of 256kB seems works well and is not
114 * disproportional compared to other constant sized shared memory
115 * allocations. NB: To avoid DSMs further, the user can configure
116 * min_dynamic_shared_memory.
117 */
118 sz = 256 * 1024;
119 Assert(dsa_minimum_size() <= sz);
120 return MAXALIGN(sz);
121}
#define MAXALIGN(LEN)
Definition: c.h:768
size_t Size
Definition: c.h:562
size_t dsa_minimum_size(void)
Definition: dsa.c:1196

References Assert, dsa_minimum_size(), and MAXALIGN.

Referenced by StatsShmemInit(), and StatsShmemSize().

◆ pgstat_free_entry()

static void pgstat_free_entry ( PgStatShared_HashEntry shent,
dshash_seq_status hstat 
)
static

Definition at line 827 of file pgstat_shmem.c.

828{
829 dsa_pointer pdsa;
830
831 /*
832 * Fetch dsa pointer before deleting entry - that way we can free the
833 * memory after releasing the lock.
834 */
835 pdsa = shent->body;
836
837 if (!hstat)
839 else
841
842 dsa_free(pgStatLocal.dsa, pdsa);
843}
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:826
uint64 dsa_pointer
Definition: dsa.h:62
void dshash_delete_entry(dshash_table *hash_table, void *entry)
Definition: dshash.c:541
void dshash_delete_current(dshash_seq_status *status)
Definition: dshash.c:757

References PgStatShared_HashEntry::body, PgStat_LocalState::dsa, dsa_free(), dshash_delete_current(), dshash_delete_entry(), pgStatLocal, and PgStat_LocalState::shared_hash.

Referenced by pgstat_drop_entry_internal(), and pgstat_release_entry_ref().

◆ pgstat_gc_entry_refs()

static void pgstat_gc_entry_refs ( void  )
static

Definition at line 722 of file pgstat_shmem.c.

723{
724 pgstat_entry_ref_hash_iterator i;
726 uint64 curage;
727
729 Assert(curage != 0);
730
731 /*
732 * Some entries have been dropped or reinitialized. Invalidate cache
733 * pointer to them.
734 */
735 pgstat_entry_ref_hash_start_iterate(pgStatEntryRefHash, &i);
736 while ((ent = pgstat_entry_ref_hash_iterate(pgStatEntryRefHash, &i)) != NULL)
737 {
738 PgStat_EntryRef *entry_ref = ent->entry_ref;
739
740 Assert(!entry_ref->shared_stats ||
741 entry_ref->shared_stats->magic == 0xdeadbeef);
742
743 /*
744 * "generation" checks for the case of entries being reinitialized,
745 * and "dropped" for the case where these are.. dropped.
746 */
747 if (!entry_ref->shared_entry->dropped &&
749 entry_ref->generation)
750 continue;
751
752 /* cannot gc shared ref that has pending data */
753 if (entry_ref->pending != NULL)
754 continue;
755
756 pgstat_release_entry_ref(ent->key, entry_ref, false);
757 }
758
759 pgStatSharedRefAge = curage;
760}
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:467
int i
Definition: isn.c:72
static int pgStatSharedRefAge
Definition: pgstat_shmem.c:83
pg_atomic_uint64 gc_request_count

References Assert, PgStatShared_HashEntry::dropped, PgStat_EntryRefHashEntry::entry_ref, PgStat_ShmemControl::gc_request_count, PgStatShared_HashEntry::generation, PgStat_EntryRef::generation, i, PgStat_EntryRefHashEntry::key, PgStatShared_Common::magic, PgStat_EntryRef::pending, pg_atomic_read_u32(), pg_atomic_read_u64(), pgstat_release_entry_ref(), pgStatEntryRefHash, pgStatLocal, pgStatSharedRefAge, PgStat_EntryRef::shared_entry, PgStat_EntryRef::shared_stats, and PgStat_LocalState::shmem.

Referenced by pgstat_get_entry_ref().

◆ pgstat_get_entry_ref()

PgStat_EntryRef * pgstat_get_entry_ref ( PgStat_Kind  kind,
Oid  dboid,
uint64  objid,
bool  create,
bool *  created_entry 
)

Definition at line 444 of file pgstat_shmem.c.

446{
448 PgStatShared_HashEntry *shhashent;
449 PgStatShared_Common *shheader = NULL;
450 PgStat_EntryRef *entry_ref;
451
452 /* clear padding */
453 memset(&key, 0, sizeof(struct PgStat_HashKey));
454
455 key.kind = kind;
456 key.dboid = dboid;
457 key.objid = objid;
458
459 /*
460 * passing in created_entry only makes sense if we possibly could create
461 * entry.
462 */
463 Assert(create || created_entry == NULL);
467
470
471 if (created_entry != NULL)
472 *created_entry = false;
473
474 /*
475 * Check if other backends dropped stats that could not be deleted because
476 * somebody held references to it. If so, check this backend's references.
477 * This is not expected to happen often. The location of the check is a
478 * bit random, but this is a relatively frequently called path, so better
479 * than most.
480 */
483
484 /*
485 * First check the lookup cache hashtable in local memory. If we find a
486 * match here we can avoid taking locks / causing contention.
487 */
488 if (pgstat_get_entry_ref_cached(key, &entry_ref))
489 return entry_ref;
490
491 Assert(entry_ref != NULL);
492
493 /*
494 * Do a lookup in the hash table first - it's quite likely that the entry
495 * already exists, and that way we only need a shared lock.
496 */
497 shhashent = dshash_find(pgStatLocal.shared_hash, &key, false);
498
499 if (create && !shhashent)
500 {
501 bool shfound;
502
503 /*
504 * It's possible that somebody created the entry since the above
505 * lookup. If so, fall through to the same path as if we'd have if it
506 * already had been created before the dshash_find() calls.
507 */
508 shhashent = dshash_find_or_insert(pgStatLocal.shared_hash, &key, &shfound);
509 if (!shfound)
510 {
511 shheader = pgstat_init_entry(kind, shhashent);
512 pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
513
514 if (created_entry != NULL)
515 *created_entry = true;
516
517 return entry_ref;
518 }
519 }
520
521 if (!shhashent)
522 {
523 /*
524 * If we're not creating, delete the reference again. In all
525 * likelihood it's just a stats lookup - no point wasting memory for a
526 * shared ref to nothing...
527 */
528 pgstat_release_entry_ref(key, entry_ref, false);
529
530 return NULL;
531 }
532 else
533 {
534 /*
535 * Can get here either because dshash_find() found a match, or if
536 * dshash_find_or_insert() found a concurrently inserted entry.
537 */
538
539 if (shhashent->dropped && create)
540 {
541 /*
542 * There are legitimate cases where the old stats entry might not
543 * yet have been dropped by the time it's reused. The most obvious
544 * case are replication slot stats, where a new slot can be
545 * created with the same index just after dropping. But oid
546 * wraparound can lead to other cases as well. We just reset the
547 * stats to their plain state, while incrementing its "generation"
548 * in the shared entry for any remaining local references.
549 */
550 shheader = pgstat_reinit_entry(kind, shhashent);
551 pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
552
553 if (created_entry != NULL)
554 *created_entry = true;
555
556 return entry_ref;
557 }
558 else if (shhashent->dropped)
559 {
561 pgstat_release_entry_ref(key, entry_ref, false);
562
563 return NULL;
564 }
565 else
566 {
567 shheader = dsa_get_address(pgStatLocal.dsa, shhashent->body);
568 pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
569
570 return entry_ref;
571 }
572 }
573}
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:942
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
Definition: dshash.c:433
#define pgstat_assert_is_up()
static void pgstat_setup_memcxt(void)
static void pgstat_setup_shared_refs(void)
Definition: pgstat_shmem.c:352
static PgStatShared_Common * pgstat_reinit_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
Definition: pgstat_shmem.c:327
static void pgstat_acquire_entry_ref(PgStat_EntryRef *entry_ref, PgStatShared_HashEntry *shhashent, PgStatShared_Common *shheader)
Definition: pgstat_shmem.c:368
PgStatShared_Common * pgstat_init_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
Definition: pgstat_shmem.c:293
static bool pgstat_get_entry_ref_cached(PgStat_HashKey key, PgStat_EntryRef **entry_ref_p)
Definition: pgstat_shmem.c:388
static void pgstat_gc_entry_refs(void)
Definition: pgstat_shmem.c:722
static bool pgstat_need_entry_refs_gc(void)
Definition: pgstat_shmem.c:706

References Assert, PgStatShared_HashEntry::body, PgStatShared_HashEntry::dropped, PgStat_LocalState::dsa, dsa_get_address(), dshash_find(), dshash_find_or_insert(), dshash_release_lock(), PgStat_ShmemControl::is_shutdown, sort-test::key, pgstat_acquire_entry_ref(), pgstat_assert_is_up, pgstat_gc_entry_refs(), pgstat_get_entry_ref_cached(), pgstat_init_entry(), pgstat_need_entry_refs_gc(), pgstat_reinit_entry(), pgstat_release_entry_ref(), pgstat_setup_memcxt(), pgstat_setup_shared_refs(), pgStatLocal, PgStat_LocalState::shared_hash, and PgStat_LocalState::shmem.

Referenced by pgstat_acquire_replslot(), pgstat_create_subscription(), pgstat_create_transactional(), pgstat_fetch_entry(), pgstat_fetch_pending_entry(), pgstat_flush_backend(), pgstat_get_entry_ref_locked(), pgstat_have_entry(), pgstat_prep_pending_entry(), and pgstat_reset_entry().

◆ pgstat_get_entry_ref_cached()

static bool pgstat_get_entry_ref_cached ( PgStat_HashKey  key,
PgStat_EntryRef **  entry_ref_p 
)
static

Definition at line 388 of file pgstat_shmem.c.

389{
390 bool found;
391 PgStat_EntryRefHashEntry *cache_entry;
392
393 /*
394 * We immediately insert a cache entry, because it avoids 1) multiple
395 * hashtable lookups in case of a cache miss 2) having to deal with
396 * out-of-memory errors after incrementing PgStatShared_Common->refcount.
397 */
398
399 cache_entry = pgstat_entry_ref_hash_insert(pgStatEntryRefHash, key, &found);
400
401 if (!found || !cache_entry->entry_ref)
402 {
403 PgStat_EntryRef *entry_ref;
404
405 cache_entry->entry_ref = entry_ref =
407 sizeof(PgStat_EntryRef));
408 entry_ref->shared_stats = NULL;
409 entry_ref->shared_entry = NULL;
410 entry_ref->pending = NULL;
411
412 found = false;
413 }
414 else if (cache_entry->entry_ref->shared_stats == NULL)
415 {
416 Assert(cache_entry->entry_ref->pending == NULL);
417 found = false;
418 }
419 else
420 {
422
423 entry_ref = cache_entry->entry_ref;
424 Assert(entry_ref->shared_entry != NULL);
425 Assert(entry_ref->shared_stats != NULL);
426
427 Assert(entry_ref->shared_stats->magic == 0xdeadbeef);
428 /* should have at least our reference */
430 }
431
432 *entry_ref_p = cache_entry->entry_ref;
433 return found;
434}
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:204
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181
static MemoryContext pgStatSharedRefContext
Definition: pgstat_shmem.c:90

References Assert, PgStat_EntryRefHashEntry::entry_ref, sort-test::key, MemoryContextAlloc(), PgStat_EntryRef::pending, pg_atomic_read_u32(), PG_USED_FOR_ASSERTS_ONLY, pgStatEntryRefHash, pgStatSharedRefContext, PgStat_EntryRef::shared_entry, and PgStat_EntryRef::shared_stats.

Referenced by pgstat_get_entry_ref().

◆ pgstat_get_entry_ref_locked()

PgStat_EntryRef * pgstat_get_entry_ref_locked ( PgStat_Kind  kind,
Oid  dboid,
uint64  objid,
bool  nowait 
)

Definition at line 684 of file pgstat_shmem.c.

686{
687 PgStat_EntryRef *entry_ref;
688
689 /* find shared table stats entry corresponding to the local entry */
690 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, true, NULL);
691
692 /* lock the shared entry to protect the content, skip if failed */
693 if (!pgstat_lock_entry(entry_ref, nowait))
694 return NULL;
695
696 return entry_ref;
697}
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:444
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:647

References pgstat_get_entry_ref(), and pgstat_lock_entry().

Referenced by pgstat_copy_relation_stats(), pgstat_create_replslot(), pgstat_report_analyze(), pgstat_report_autovac(), pgstat_report_checksum_failures_in_db(), pgstat_report_replslot(), pgstat_report_vacuum(), and pgstat_reset_database_timestamp().

◆ pgstat_init_entry()

PgStatShared_Common * pgstat_init_entry ( PgStat_Kind  kind,
PgStatShared_HashEntry shhashent 
)

Definition at line 293 of file pgstat_shmem.c.

295{
296 /* Create new stats entry. */
298 PgStatShared_Common *shheader;
299
300 /*
301 * Initialize refcount to 1, marking it as valid / not dropped. The entry
302 * can't be freed before the initialization because it can't be found as
303 * long as we hold the dshash partition lock. Caller needs to increase
304 * further if a longer lived reference is needed.
305 */
306 pg_atomic_init_u32(&shhashent->refcount, 1);
307
308 /*
309 * Initialize "generation" to 0, as freshly created.
310 */
311 pg_atomic_init_u32(&shhashent->generation, 0);
312 shhashent->dropped = false;
313
316 shheader->magic = 0xdeadbeef;
317
318 /* Link the new entry from the hash entry. */
319 shhashent->body = chunk;
320
322
323 return shheader;
324}
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:221
#define dsa_allocate0(area, size)
Definition: dsa.h:113
uint64 chunk
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:707
@ LWTRANCHE_PGSTATS_DATA
Definition: lwlock.h:205

References PgStatShared_HashEntry::body, chunk, PgStatShared_HashEntry::dropped, PgStat_LocalState::dsa, dsa_allocate0, dsa_get_address(), PgStatShared_HashEntry::generation, PgStatShared_Common::lock, LWLockInitialize(), LWTRANCHE_PGSTATS_DATA, PgStatShared_Common::magic, pg_atomic_init_u32(), pgstat_get_kind_info(), pgStatLocal, and PgStatShared_HashEntry::refcount.

Referenced by pgstat_get_entry_ref(), and pgstat_read_statsfile().

◆ pgstat_lock_entry()

bool pgstat_lock_entry ( PgStat_EntryRef entry_ref,
bool  nowait 
)

Definition at line 647 of file pgstat_shmem.c.

648{
649 LWLock *lock = &entry_ref->shared_stats->lock;
650
651 if (nowait)
653
655 return true;
656}
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1339
@ LW_EXCLUSIVE
Definition: lwlock.h:114
Definition: lwlock.h:42

References PgStatShared_Common::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockConditionalAcquire(), and PgStat_EntryRef::shared_stats.

Referenced by injection_stats_flush_cb(), pgstat_database_flush_cb(), pgstat_flush_backend_entry(), pgstat_function_flush_cb(), pgstat_get_entry_ref_locked(), pgstat_relation_flush_cb(), pgstat_reset_entry(), and pgstat_subscription_flush_cb().

◆ pgstat_lock_entry_shared()

bool pgstat_lock_entry_shared ( PgStat_EntryRef entry_ref,
bool  nowait 
)

Definition at line 663 of file pgstat_shmem.c.

664{
665 LWLock *lock = &entry_ref->shared_stats->lock;
666
667 if (nowait)
669
671 return true;
672}
@ LW_SHARED
Definition: lwlock.h:115

References PgStatShared_Common::lock, LW_SHARED, LWLockAcquire(), LWLockConditionalAcquire(), and PgStat_EntryRef::shared_stats.

Referenced by pgstat_fetch_entry().

◆ pgstat_need_entry_refs_gc()

static bool pgstat_need_entry_refs_gc ( void  )
static

Definition at line 706 of file pgstat_shmem.c.

707{
708 uint64 curage;
709
711 return false;
712
713 /* should have been initialized when creating pgStatEntryRefHash */
715
717
718 return pgStatSharedRefAge != curage;
719}

References Assert, PgStat_ShmemControl::gc_request_count, pg_atomic_read_u64(), pgStatEntryRefHash, pgStatLocal, pgStatSharedRefAge, and PgStat_LocalState::shmem.

Referenced by pgstat_get_entry_ref().

◆ pgstat_reinit_entry()

static PgStatShared_Common * pgstat_reinit_entry ( PgStat_Kind  kind,
PgStatShared_HashEntry shhashent 
)
static

Definition at line 327 of file pgstat_shmem.c.

328{
329 PgStatShared_Common *shheader;
330
331 shheader = dsa_get_address(pgStatLocal.dsa, shhashent->body);
332
333 /* mark as not dropped anymore */
334 pg_atomic_fetch_add_u32(&shhashent->refcount, 1);
335
336 /*
337 * Increment "generation", to let any backend with local references know
338 * that what they point to is outdated.
339 */
340 pg_atomic_fetch_add_u32(&shhashent->generation, 1);
341 shhashent->dropped = false;
342
343 /* reinitialize content */
344 Assert(shheader->magic == 0xdeadbeef);
345 memset(pgstat_get_entry_data(kind, shheader), 0,
347
348 return shheader;
349}
static void * pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry)
static size_t pgstat_get_entry_len(PgStat_Kind kind)

References Assert, PgStatShared_HashEntry::body, PgStatShared_HashEntry::dropped, PgStat_LocalState::dsa, dsa_get_address(), PgStatShared_HashEntry::generation, PgStatShared_Common::magic, pg_atomic_fetch_add_u32(), pgstat_get_entry_data(), pgstat_get_entry_len(), pgStatLocal, and PgStatShared_HashEntry::refcount.

Referenced by pgstat_get_entry_ref().

◆ pgstat_release_all_entry_refs()

static void pgstat_release_all_entry_refs ( bool  discard_pending)
static

Definition at line 793 of file pgstat_shmem.c.

794{
795 if (pgStatEntryRefHash == NULL)
796 return;
797
798 pgstat_release_matching_entry_refs(discard_pending, NULL, 0);
799 Assert(pgStatEntryRefHash->members == 0);
800 pgstat_entry_ref_hash_destroy(pgStatEntryRefHash);
801 pgStatEntryRefHash = NULL;
802}
static void pgstat_release_matching_entry_refs(bool discard_pending, ReleaseMatchCB match, Datum match_data)
Definition: pgstat_shmem.c:763

References Assert, pgstat_release_matching_entry_refs(), and pgStatEntryRefHash.

Referenced by pgstat_detach_shmem().

◆ pgstat_release_db_entry_refs()

static void pgstat_release_db_entry_refs ( Oid  dboid)
static

Definition at line 813 of file pgstat_shmem.c.

814{
815 pgstat_release_matching_entry_refs( /* discard pending = */ true,
816 match_db,
817 ObjectIdGetDatum(dboid));
818}
static bool match_db(PgStat_EntryRefHashEntry *ent, Datum match_data)
Definition: pgstat_shmem.c:805
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257

References match_db(), ObjectIdGetDatum(), and pgstat_release_matching_entry_refs().

Referenced by pgstat_drop_database_and_contents().

◆ pgstat_release_entry_ref()

static void pgstat_release_entry_ref ( PgStat_HashKey  key,
PgStat_EntryRef entry_ref,
bool  discard_pending 
)
static

Definition at line 576 of file pgstat_shmem.c.

578{
579 if (entry_ref && entry_ref->pending)
580 {
581 if (discard_pending)
583 else
584 elog(ERROR, "releasing ref with pending data");
585 }
586
587 if (entry_ref && entry_ref->shared_stats)
588 {
589 Assert(entry_ref->shared_stats->magic == 0xdeadbeef);
590 Assert(entry_ref->pending == NULL);
591
592 /*
593 * This can't race with another backend looking up the stats entry and
594 * increasing the refcount because it is not "legal" to create
595 * additional references to dropped entries.
596 */
597 if (pg_atomic_fetch_sub_u32(&entry_ref->shared_entry->refcount, 1) == 1)
598 {
600
601 /*
602 * We're the last referrer to this entry, try to drop the shared
603 * entry.
604 */
605
606 /* only dropped entries can reach a 0 refcount */
607 Assert(entry_ref->shared_entry->dropped);
608
610 &entry_ref->shared_entry->key,
611 true);
612 if (!shent)
613 elog(ERROR, "could not find just referenced shared stats entry");
614
615 /*
616 * This entry may have been reinitialized while trying to release
617 * it, so double-check that it has not been reused while holding a
618 * lock on its shared entry.
619 */
620 if (pg_atomic_read_u32(&entry_ref->shared_entry->generation) ==
621 entry_ref->generation)
622 {
623 /* Same "generation", so we're OK with the removal */
625 Assert(entry_ref->shared_entry == shent);
626 pgstat_free_entry(shent, NULL);
627 }
628 else
629 {
630 /*
631 * Shared stats entry has been reinitialized, so do not drop
632 * its shared entry, only release its lock.
633 */
635 }
636 }
637 }
638
639 if (!pgstat_entry_ref_hash_delete(pgStatEntryRefHash, key))
640 elog(ERROR, "entry ref vanished before deletion");
641
642 if (entry_ref)
643 pfree(entry_ref);
644}
static uint32 pg_atomic_fetch_sub_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:381
void pfree(void *pointer)
Definition: mcxt.c:1521
void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat.c:1345

References Assert, PgStatShared_HashEntry::dropped, dshash_find(), dshash_release_lock(), elog, ERROR, PgStatShared_HashEntry::generation, PgStat_EntryRef::generation, PgStatShared_HashEntry::key, sort-test::key, PgStatShared_Common::magic, PgStat_EntryRef::pending, pfree(), pg_atomic_fetch_sub_u32(), pg_atomic_read_u32(), pgstat_delete_pending_entry(), pgstat_free_entry(), pgStatEntryRefHash, pgStatLocal, PgStatShared_HashEntry::refcount, PgStat_EntryRef::shared_entry, PgStat_LocalState::shared_hash, and PgStat_EntryRef::shared_stats.

Referenced by pgstat_drop_entry(), pgstat_gc_entry_refs(), pgstat_get_entry_ref(), and pgstat_release_matching_entry_refs().

◆ pgstat_release_matching_entry_refs()

static void pgstat_release_matching_entry_refs ( bool  discard_pending,
ReleaseMatchCB  match,
Datum  match_data 
)
static

Definition at line 763 of file pgstat_shmem.c.

765{
766 pgstat_entry_ref_hash_iterator i;
768
769 if (pgStatEntryRefHash == NULL)
770 return;
771
772 pgstat_entry_ref_hash_start_iterate(pgStatEntryRefHash, &i);
773
774 while ((ent = pgstat_entry_ref_hash_iterate(pgStatEntryRefHash, &i))
775 != NULL)
776 {
777 Assert(ent->entry_ref != NULL);
778
779 if (match && !match(ent, match_data))
780 continue;
781
782 pgstat_release_entry_ref(ent->key, ent->entry_ref, discard_pending);
783 }
784}

References Assert, PgStat_EntryRefHashEntry::entry_ref, i, PgStat_EntryRefHashEntry::key, pgstat_release_entry_ref(), and pgStatEntryRefHash.

Referenced by pgstat_release_all_entry_refs(), and pgstat_release_db_entry_refs().

◆ pgstat_request_entry_refs_gc()

◆ pgstat_reset_entries_of_kind()

void pgstat_reset_entries_of_kind ( PgStat_Kind  kind,
TimestampTz  ts 
)

Definition at line 1091 of file pgstat_shmem.c.

1092{
1094}
static bool match_kind(PgStatShared_HashEntry *p, Datum match_data)
void pgstat_reset_matching_entries(bool(*do_reset)(PgStatShared_HashEntry *, Datum), Datum match_data, TimestampTz ts)
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217

References Int32GetDatum(), match_kind(), and pgstat_reset_matching_entries().

Referenced by pgstat_reset_of_kind().

◆ pgstat_reset_entry()

void pgstat_reset_entry ( PgStat_Kind  kind,
Oid  dboid,
uint64  objid,
TimestampTz  ts 
)

Definition at line 1035 of file pgstat_shmem.c.

1036{
1037 PgStat_EntryRef *entry_ref;
1038
1039 Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1040
1041 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
1042 if (!entry_ref || entry_ref->shared_entry->dropped)
1043 return;
1044
1045 (void) pgstat_lock_entry(entry_ref, false);
1046 shared_stat_reset_contents(kind, entry_ref->shared_stats, ts);
1047 pgstat_unlock_entry(entry_ref);
1048}
static void shared_stat_reset_contents(PgStat_Kind kind, PgStatShared_Common *header, TimestampTz ts)
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:675

References Assert, PgStatShared_HashEntry::dropped, pgstat_get_entry_ref(), pgstat_get_kind_info(), pgstat_lock_entry(), pgstat_unlock_entry(), PgStat_EntryRef::shared_entry, shared_stat_reset_contents(), and PgStat_EntryRef::shared_stats.

Referenced by pgstat_create_subscription(), and pgstat_reset().

◆ pgstat_reset_matching_entries()

void pgstat_reset_matching_entries ( bool(*)(PgStatShared_HashEntry *, Datum do_reset,
Datum  match_data,
TimestampTz  ts 
)

Definition at line 1055 of file pgstat_shmem.c.

1057{
1058 dshash_seq_status hstat;
1060
1061 /* dshash entry is not modified, take shared lock */
1062 dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
1063 while ((p = dshash_seq_next(&hstat)) != NULL)
1064 {
1065 PgStatShared_Common *header;
1066
1067 if (p->dropped)
1068 continue;
1069
1070 if (!do_reset(p, match_data))
1071 continue;
1072
1073 header = dsa_get_address(pgStatLocal.dsa, p->body);
1074
1075 LWLockAcquire(&header->lock, LW_EXCLUSIVE);
1076
1077 shared_stat_reset_contents(p->key.kind, header, ts);
1078
1079 LWLockRelease(&header->lock);
1080 }
1081 dshash_seq_term(&hstat);
1082}
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781

References PgStatShared_HashEntry::body, PgStatShared_HashEntry::dropped, PgStat_LocalState::dsa, dsa_get_address(), dshash_seq_init(), dshash_seq_next(), dshash_seq_term(), PgStatShared_HashEntry::key, PgStat_HashKey::kind, PgStatShared_Common::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), pgStatLocal, PgStat_LocalState::shared_hash, and shared_stat_reset_contents().

Referenced by pgstat_reset_counters(), and pgstat_reset_entries_of_kind().

◆ pgstat_setup_memcxt()

static void pgstat_setup_memcxt ( void  )
static

Definition at line 1097 of file pgstat_shmem.c.

1098{
1102 "PgStat Shared Ref",
1107 "PgStat Shared Ref Hash",
1109}
#define unlikely(x)
Definition: c.h:333
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:170
static MemoryContext pgStatEntryRefHashContext
Definition: pgstat_shmem.c:91

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, pgStatEntryRefHashContext, pgStatSharedRefContext, TopMemoryContext, and unlikely.

Referenced by pgstat_get_entry_ref().

◆ pgstat_setup_shared_refs()

static void pgstat_setup_shared_refs ( void  )
static

◆ pgstat_unlock_entry()

◆ shared_stat_reset_contents()

static void shared_stat_reset_contents ( PgStat_Kind  kind,
PgStatShared_Common header,
TimestampTz  ts 
)
static

Definition at line 1019 of file pgstat_shmem.c.

1021{
1022 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1023
1024 memset(pgstat_get_entry_data(kind, header), 0,
1025 pgstat_get_entry_len(kind));
1026
1027 if (kind_info->reset_timestamp_cb)
1028 kind_info->reset_timestamp_cb(header, ts);
1029}
void(* reset_timestamp_cb)(PgStatShared_Common *header, TimestampTz ts)

References pgstat_get_entry_data(), pgstat_get_entry_len(), pgstat_get_kind_info(), and PgStat_KindInfo::reset_timestamp_cb.

Referenced by pgstat_reset_entry(), and pgstat_reset_matching_entries().

◆ StatsShmemInit()

void StatsShmemInit ( void  )

Definition at line 156 of file pgstat_shmem.c.

157{
158 bool found;
159 Size sz;
160
161 sz = StatsShmemSize();
163 ShmemInitStruct("Shared Memory Stats", sz, &found);
164
166 {
167 dsa_area *dsa;
168 dshash_table *dsh;
170 char *p = (char *) ctl;
171
172 Assert(!found);
173
174 /* the allocation of pgStatLocal.shmem itself */
175 p += MAXALIGN(sizeof(PgStat_ShmemControl));
176
177 /*
178 * Create a small dsa allocation in plain shared memory. This is
179 * required because postmaster cannot use dsm segments. It also
180 * provides a small efficiency win.
181 */
182 ctl->raw_dsa_area = p;
184 dsa = dsa_create_in_place(ctl->raw_dsa_area,
187 dsa_pin(dsa);
188
189 /*
190 * To ensure dshash is created in "plain" shared memory, temporarily
191 * limit size of dsa to the initial size of the dsa.
192 */
194
195 /*
196 * With the limit in place, create the dshash table. XXX: It'd be nice
197 * if there were dshash_create_in_place().
198 */
199 dsh = dshash_create(dsa, &dsh_params, NULL);
200 ctl->hash_handle = dshash_get_hash_table_handle(dsh);
201
202 /* lift limit set above */
203 dsa_set_size_limit(dsa, -1);
204
205 /*
206 * Postmaster will never access these again, thus free the local
207 * dsa/dshash references.
208 */
209 dshash_detach(dsh);
210 dsa_detach(dsa);
211
212 pg_atomic_init_u64(&ctl->gc_request_count, 1);
213
214 /* initialize fixed-numbered stats */
215 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
216 {
217 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
218 char *ptr;
219
220 if (!kind_info || !kind_info->fixed_amount)
221 continue;
222
223 if (pgstat_is_kind_builtin(kind))
224 ptr = ((char *) ctl) + kind_info->shared_ctl_off;
225 else
226 {
227 int idx = kind - PGSTAT_KIND_CUSTOM_MIN;
228
229 Assert(kind_info->shared_size != 0);
230 ctl->custom_data[idx] = ShmemAlloc(kind_info->shared_size);
231 ptr = ctl->custom_data[idx];
232 }
233
234 kind_info->init_shmem_cb(ptr);
235 }
236 }
237 else
238 {
239 Assert(found);
240 }
241}
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:453
void dsa_set_size_limit(dsa_area *area, size_t limit)
Definition: dsa.c:1018
void dsa_pin(dsa_area *area)
Definition: dsa.c:975
#define dsa_create_in_place(place, size, tranch_id, segment)
Definition: dsa.h:122
dshash_table_handle dshash_get_hash_table_handle(dshash_table *hash_table)
Definition: dshash.c:367
dshash_table * dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
Definition: dshash.c:206
bool IsUnderPostmaster
Definition: globals.c:119
@ LWTRANCHE_PGSTATS_DSA
Definition: lwlock.h:203
static bool pgstat_is_kind_builtin(PgStat_Kind kind)
Definition: pgstat_kind.h:61
#define PgStat_Kind
Definition: pgstat_kind.h:17
#define PGSTAT_KIND_MAX
Definition: pgstat_kind.h:21
#define PGSTAT_KIND_CUSTOM_MIN
Definition: pgstat_kind.h:49
#define PGSTAT_KIND_MIN
Definition: pgstat_kind.h:20
static Size pgstat_dsa_init_size(void)
Definition: pgstat_shmem.c:106
Size StatsShmemSize(void)
Definition: pgstat_shmem.c:127
tree ctl
Definition: radixtree.h:1838
void * ShmemAlloc(Size size)
Definition: shmem.c:147
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:382
void(* init_shmem_cb)(void *stats)
Definition: dsa.c:348

References Assert, ctl, dsa_create_in_place, dsa_detach(), dsa_pin(), dsa_set_size_limit(), dsh_params, dshash_create(), dshash_detach(), dshash_get_hash_table_handle(), PgStat_KindInfo::fixed_amount, idx(), PgStat_KindInfo::init_shmem_cb, IsUnderPostmaster, LWTRANCHE_PGSTATS_DSA, MAXALIGN, pg_atomic_init_u64(), pgstat_dsa_init_size(), pgstat_get_kind_info(), pgstat_is_kind_builtin(), PgStat_Kind, PGSTAT_KIND_CUSTOM_MIN, PGSTAT_KIND_MAX, PGSTAT_KIND_MIN, pgStatLocal, PgStat_KindInfo::shared_ctl_off, PgStat_KindInfo::shared_size, PgStat_LocalState::shmem, ShmemAlloc(), ShmemInitStruct(), and StatsShmemSize().

Referenced by CreateOrAttachShmemStructs().

◆ StatsShmemSize()

Size StatsShmemSize ( void  )

Definition at line 127 of file pgstat_shmem.c.

128{
129 Size sz;
130
131 sz = MAXALIGN(sizeof(PgStat_ShmemControl));
132 sz = add_size(sz, pgstat_dsa_init_size());
133
134 /* Add shared memory for all the custom fixed-numbered statistics */
135 for (PgStat_Kind kind = PGSTAT_KIND_CUSTOM_MIN; kind <= PGSTAT_KIND_CUSTOM_MAX; kind++)
136 {
137 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
138
139 if (!kind_info)
140 continue;
141 if (!kind_info->fixed_amount)
142 continue;
143
144 Assert(kind_info->shared_size != 0);
145
146 sz += MAXALIGN(kind_info->shared_size);
147 }
148
149 return sz;
150}
#define PGSTAT_KIND_CUSTOM_MAX
Definition: pgstat_kind.h:50
Size add_size(Size s1, Size s2)
Definition: shmem.c:488

References add_size(), Assert, PgStat_KindInfo::fixed_amount, MAXALIGN, pgstat_dsa_init_size(), pgstat_get_kind_info(), PgStat_Kind, PGSTAT_KIND_CUSTOM_MAX, PGSTAT_KIND_CUSTOM_MIN, and PgStat_KindInfo::shared_size.

Referenced by CalculateShmemSize(), and StatsShmemInit().

Variable Documentation

◆ dsh_params

const dshash_parameters dsh_params
static
Initial value:
= {
sizeof(PgStat_HashKey),
}
void dshash_memcpy(void *dest, const void *src, size_t size, void *arg)
Definition: dshash.c:590
@ LWTRANCHE_PGSTATS_HASH
Definition: lwlock.h:204
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)
struct PgStat_HashKey PgStat_HashKey

Definition at line 62 of file pgstat_shmem.c.

Referenced by pgstat_attach_shmem(), and StatsShmemInit().

◆ pgStatEntryRefHash

◆ pgStatEntryRefHashContext

MemoryContext pgStatEntryRefHashContext = NULL
static

Definition at line 91 of file pgstat_shmem.c.

Referenced by pgstat_setup_memcxt(), and pgstat_setup_shared_refs().

◆ pgStatSharedRefAge

int pgStatSharedRefAge = 0
static

◆ pgStatSharedRefContext

MemoryContext pgStatSharedRefContext = NULL
static

Definition at line 90 of file pgstat_shmem.c.

Referenced by pgstat_get_entry_ref_cached(), and pgstat_setup_memcxt().