120 #define PGSTAT_MIN_INTERVAL 1000
122 #define PGSTAT_MAX_INTERVAL 60000
124 #define PGSTAT_IDLE_INTERVAL 10000
131 #define PGSTAT_SNAPSHOT_HASH_SIZE 512
149 #define SH_PREFIX pgstat_snapshot
150 #define SH_ELEMENT_TYPE PgStat_SnapshotEntry
151 #define SH_KEY_TYPE PgStat_HashKey
153 #define SH_HASH_KEY(tb, key) \
154 pgstat_hash_hash_key(&key, sizeof(PgStat_HashKey), NULL)
155 #define SH_EQUAL(tb, a, b) \
156 pgstat_cmp_hash_key(&a, &b, sizeof(PgStat_HashKey), NULL) == 0
157 #define SH_SCOPE static inline
241 #ifdef USE_ASSERT_CHECKING
242 static bool pgstat_is_initialized =
false;
243 static bool pgstat_is_shutdown =
false;
267 .fixed_amount =
false,
269 .accessed_across_databases =
true,
283 .fixed_amount =
false,
297 .fixed_amount =
false,
310 .fixed_amount =
false,
312 .accessed_across_databases =
true,
313 .named_on_disk =
true,
325 .name =
"subscription",
327 .fixed_amount =
false,
329 .accessed_across_databases =
true,
346 .fixed_amount =
true,
355 .fixed_amount =
true,
362 .name =
"checkpointer",
364 .fixed_amount =
true,
373 .fixed_amount =
true,
382 .fixed_amount =
true,
391 .fixed_amount =
true,
433 "didn't need to unlink permanent stats file \"%s\" - didn't exist",
438 errmsg(
"could not unlink permanent statistics file \"%s\": %m",
475 Assert(pgstat_is_initialized && !pgstat_is_shutdown);
508 Assert(!pgstat_is_shutdown);
528 #ifdef USE_ASSERT_CHECKING
529 pgstat_is_shutdown =
true;
542 Assert(!pgstat_is_initialized);
551 #ifdef USE_ASSERT_CHECKING
552 pgstat_is_initialized =
true;
606 Assert(pending_since == 0);
633 if (pending_since > 0 &&
639 else if (last_flush > 0 &&
643 if (pending_since == 0)
655 partial_flush =
false;
681 if (pending_since == 0)
896 entry->
data = stats_data;
915 *have_snapshot =
true;
919 *have_snapshot =
false;
1121 true, created_entry);
1123 if (entry_ref->
pending == NULL)
1127 Assert(entrysize != (
size_t) -1);
1149 if (entry_ref == NULL || entry_ref->
pending == NULL)
1160 void *pending_data = entry_ref->
pending;
1162 Assert(pending_data != NULL);
1169 pfree(pending_data);
1182 bool have_pending =
false;
1213 Assert(did_flush || nowait);
1225 have_pending =
true;
1232 return have_pending;
1251 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1252 errmsg(
"invalid statistics kind: \"%s\"", kind_str)));
1275 #ifdef USE_ASSERT_CHECKING
1279 Assert(pgstat_is_initialized && !pgstat_is_shutdown);
1295 rc = fwrite(ptr,
len, 1, fpout);
1301 #define write_chunk_s(fpout, ptr) write_chunk(fpout, ptr, sizeof(*ptr))
1322 elog(
DEBUG2,
"writing stats file \"%s\"", statfile);
1332 errmsg(
"could not open temporary statistics file \"%s\": %m",
1444 errmsg(
"could not write temporary statistics file \"%s\": %m",
1453 errmsg(
"could not close temporary statistics file \"%s\": %m",
1457 else if (rename(tmpfile, statfile) < 0)
1461 errmsg(
"could not rename temporary statistics file \"%s\" to \"%s\": %m",
1462 tmpfile, statfile)));
1471 return fread(ptr, 1,
len, fpin) ==
len;
1474 #define read_chunk_s(fpin, ptr) read_chunk(fpin, ptr, sizeof(*ptr))
1494 elog(
DEBUG2,
"reading stats file \"%s\"", statfile);
1507 if (errno != ENOENT)
1510 errmsg(
"could not open statistics file \"%s\": %m",
1571 int t = fgetc(fpin);
1635 elog(
WARNING,
"found duplicate stats entry %d/%u/%u",
1652 if (fgetc(fpin) != EOF)
1665 elog(
DEBUG2,
"removing permanent stats file \"%s\"", statfile);
1672 (
errmsg(
"corrupted statistics file \"%s\"", statfile)));
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
TimestampTz GetCurrentTimestamp(void)
Datum now(PG_FUNCTION_ARGS)
void pgstat_clear_backend_activity_snapshot(void)
#define OidIsValid(objectId)
elog(ERROR, "%s: %s", p2, msg)
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
void dshash_release_lock(dshash_table *hash_table, void *entry)
void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table, bool exclusive)
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)
int errmsg_internal(const char *fmt,...)
int errcode_for_file_access(void)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
FILE * AllocateFile(const char *name, const char *mode)
bool IsPostmasterEnvironment
static void dlist_init(dlist_head *head)
static bool dlist_has_next(const dlist_head *head, const dlist_node *node)
static void dlist_delete(dlist_node *node)
static dlist_node * dlist_next_node(dlist_head *head, dlist_node *node)
static bool dlist_is_empty(const dlist_head *head)
static void dlist_push_tail(dlist_head *head, dlist_node *node)
static dlist_node * dlist_head_node(dlist_head *head)
#define DLIST_STATIC_INIT(name)
#define dlist_container(type, membername, ptr)
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
void LWLockRelease(LWLock *lock)
void pfree(void *pointer)
MemoryContext TopMemoryContext
void * MemoryContextAllocZero(MemoryContext context, Size size)
void * MemoryContextAlloc(MemoryContext context, Size size)
void MemoryContextDelete(MemoryContext context)
#define AllocSetContextCreate
#define ALLOCSET_SMALL_SIZES
#define CHECK_FOR_INTERRUPTS()
void pgstat_reset(PgStat_Kind kind, Oid dboid, Oid objoid)
static bool pgStatForceNextFlush
int pgstat_fetch_consistency
#define PGSTAT_MIN_INTERVAL
void pgstat_snapshot_fixed(PgStat_Kind kind)
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
static void pgstat_prep_snapshot(void)
static void pgstat_build_snapshot_fixed(PgStat_Kind kind)
#define PGSTAT_SNAPSHOT_HASH_SIZE
static bool pgstat_is_kind_valid(int ikind)
void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref)
void pgstat_reset_counters(void)
static void pgstat_build_snapshot(void)
void pgstat_initialize(void)
bool pgstat_have_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
static void pgstat_write_statsfile(void)
long pgstat_report_stat(bool force)
static void pgstat_read_statsfile(void)
void pgstat_reset_of_kind(PgStat_Kind kind)
static bool pgstat_flush_pending_entries(bool nowait)
void pgstat_before_server_shutdown(int code, Datum arg)
static void pgstat_reset_after_failure(void)
void assign_stats_fetch_consistency(int newval, void *extra)
void pgstat_force_next_flush(void)
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
void pgstat_clear_snapshot(void)
TimestampTz pgstat_get_stat_snapshot_timestamp(bool *have_snapshot)
static MemoryContext pgStatPendingContext
static dlist_head pgStatPending
static void write_chunk(FILE *fpout, void *ptr, size_t len)
void pgstat_restore_stats(void)
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid, bool *created_entry)
#define write_chunk_s(fpout, ptr)
struct PgStat_SnapshotEntry PgStat_SnapshotEntry
PgStat_EntryRef * pgstat_fetch_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
#define PGSTAT_MAX_INTERVAL
static bool read_chunk(FILE *fpin, void *ptr, size_t len)
#define read_chunk_s(fpin, ptr)
static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS]
#define PGSTAT_IDLE_INTERVAL
PgStat_LocalState pgStatLocal
void pgstat_discard_stats(void)
static bool match_db_entries(PgStatShared_HashEntry *entry, Datum match_data)
static bool force_stats_snapshot_clear
PgStat_Kind pgstat_get_kind_from_str(char *kind_str)
static void pgstat_shutdown_hook(int code, Datum arg)
@ PGSTAT_KIND_SUBSCRIPTION
@ PGSTAT_KIND_CHECKPOINTER
@ PGSTAT_FETCH_CONSISTENCY_NONE
@ PGSTAT_FETCH_CONSISTENCY_CACHE
@ PGSTAT_FETCH_CONSISTENCY_SNAPSHOT
#define PGSTAT_STAT_PERMANENT_FILENAME
#define PGSTAT_STAT_PERMANENT_TMPFILE
#define PGSTAT_FILE_FORMAT_ID
#define PGSTAT_KIND_FIRST_VALID
void pgstat_archiver_reset_all_cb(TimestampTz ts)
void pgstat_archiver_snapshot_cb(void)
void pgstat_bgwriter_reset_all_cb(TimestampTz ts)
void pgstat_bgwriter_snapshot_cb(void)
void pgstat_checkpointer_snapshot_cb(void)
void pgstat_checkpointer_reset_all_cb(TimestampTz ts)
void pgstat_database_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_update_dbstats(TimestampTz ts)
void pgstat_reset_database_timestamp(Oid dboid, TimestampTz ts)
void pgstat_report_disconnect(Oid dboid)
bool pgstat_database_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
bool pgstat_function_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
struct PgStatShared_Function PgStatShared_Function
static void * pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry)
struct PgStatShared_Subscription PgStatShared_Subscription
#define pgstat_assert_is_up()
struct PgStatShared_Relation PgStatShared_Relation
struct PgStatShared_Database PgStatShared_Database
static size_t pgstat_get_entry_len(PgStat_Kind kind)
struct PgStatShared_ReplSlot PgStatShared_ReplSlot
bool pgstat_flush_io(bool nowait)
void pgstat_io_reset_all_cb(TimestampTz ts)
void pgstat_io_snapshot_cb(void)
bool pgstat_relation_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
void pgstat_relation_delete_pending_cb(PgStat_EntryRef *entry_ref)
bool pgstat_replslot_from_serialized_name_cb(const NameData *name, PgStat_HashKey *key)
void pgstat_replslot_to_serialized_name_cb(const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name)
void pgstat_replslot_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts)
PgStatShared_Common * pgstat_init_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
void pgstat_attach_shmem(void)
void pgstat_reset_entry(PgStat_Kind kind, Oid dboid, Oid objoid, TimestampTz ts)
void pgstat_reset_matching_entries(bool(*do_reset)(PgStatShared_HashEntry *, Datum), Datum match_data, TimestampTz ts)
void pgstat_drop_all_entries(void)
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create, bool *created_entry)
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
void pgstat_detach_shmem(void)
void pgstat_slru_snapshot_cb(void)
bool pgstat_slru_flush(bool nowait)
void pgstat_slru_reset_all_cb(TimestampTz ts)
bool pgstat_subscription_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
void pgstat_subscription_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_wal_reset_all_cb(TimestampTz ts)
bool pgstat_have_pending_wal(void)
void pgstat_init_wal(void)
bool pgstat_flush_wal(bool nowait)
void pgstat_wal_snapshot_cb(void)
int pg_strcasecmp(const char *s1, const char *s2)
static Oid DatumGetObjectId(Datum X)
static Datum ObjectIdGetDatum(Oid X)
PgStat_ArchiverStats stats
PgStat_BgWriterStats stats
PgStat_CheckpointerStats stats
pg_atomic_uint32 refcount
PgStat_SLRUStats stats[SLRU_NUM_ELEMENTS]
PgStatShared_Common * shared_stats
PgStatShared_HashEntry * shared_entry
bool accessed_across_databases
void(* to_serialized_name)(const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name)
bool(* from_serialized_name)(const NameData *name, PgStat_HashKey *key)
bool(* flush_pending_cb)(PgStat_EntryRef *sr, bool nowait)
void(* reset_all_cb)(TimestampTz ts)
void(* delete_pending_cb)(PgStat_EntryRef *sr)
void(* snapshot_cb)(void)
dshash_table * shared_hash
PgStat_ShmemControl * shmem
PgStatShared_Archiver archiver
PgStatShared_Checkpointer checkpointer
PgStatShared_BgWriter bgwriter
PgStat_CheckpointerStats checkpointer
TimestampTz snapshot_timestamp
bool fixed_valid[PGSTAT_NUM_KINDS]
PgStat_ArchiverStats archiver
PgStat_FetchConsistency mode
PgStat_SLRUStats slru[SLRU_NUM_ELEMENTS]
struct pgstat_snapshot_hash * stats
PgStat_BgWriterStats bgwriter
bool IsTransactionOrTransactionBlock(void)
TimestampTz GetCurrentTransactionStopTimestamp(void)