126#define PGSTAT_MIN_INTERVAL 1000
128#define PGSTAT_MAX_INTERVAL 60000
130#define PGSTAT_IDLE_INTERVAL 10000
137#define PGSTAT_SNAPSHOT_HASH_SIZE 512
143#define PGSTAT_FILE_ENTRY_END 'E'
144#define PGSTAT_FILE_ENTRY_FIXED 'F'
145#define PGSTAT_FILE_ENTRY_NAME 'N'
146#define PGSTAT_FILE_ENTRY_HASH 'S'
164#define SH_PREFIX pgstat_snapshot
165#define SH_ELEMENT_TYPE PgStat_SnapshotEntry
166#define SH_KEY_TYPE PgStat_HashKey
168#define SH_HASH_KEY(tb, key) \
169 pgstat_hash_hash_key(&key, sizeof(PgStat_HashKey), NULL)
170#define SH_EQUAL(tb, a, b) \
171 pgstat_cmp_hash_key(&a, &b, sizeof(PgStat_HashKey), NULL) == 0
172#define SH_SCOPE static inline
258#ifdef USE_ASSERT_CHECKING
259static bool pgstat_is_initialized =
false;
260static bool pgstat_is_shutdown =
false;
284 .fixed_amount =
false,
285 .write_to_file =
true,
287 .accessed_across_databases =
true,
301 .fixed_amount =
false,
302 .write_to_file =
true,
316 .fixed_amount =
false,
317 .write_to_file =
true,
330 .fixed_amount =
false,
331 .write_to_file =
true,
333 .accessed_across_databases =
true,
345 .name =
"subscription",
347 .fixed_amount =
false,
348 .write_to_file =
true,
350 .accessed_across_databases =
true,
364 .fixed_amount =
false,
365 .write_to_file =
false,
367 .accessed_across_databases =
true,
383 .fixed_amount =
true,
384 .write_to_file =
true,
399 .fixed_amount =
true,
400 .write_to_file =
true,
413 .name =
"checkpointer",
415 .fixed_amount =
true,
416 .write_to_file =
true,
431 .fixed_amount =
true,
432 .write_to_file =
true,
449 .fixed_amount =
true,
450 .write_to_file =
true,
467 .fixed_amount =
true,
468 .write_to_file =
true,
528 "didn't need to unlink permanent stats file \"%s\" - didn't exist",
533 errmsg(
"could not unlink permanent statistics file \"%s\": %m",
570 Assert(pgstat_is_initialized && !pgstat_is_shutdown);
603 Assert(!pgstat_is_shutdown);
627#ifdef USE_ASSERT_CHECKING
628 pgstat_is_shutdown =
true;
641 Assert(!pgstat_is_initialized);
661#ifdef USE_ASSERT_CHECKING
662 pgstat_is_initialized =
true;
713 bool do_flush =
false;
734 Assert(pending_since == 0);
762 if (pending_since > 0 &&
768 else if (last_flush > 0 &&
772 if (pending_since == 0)
784 partial_flush =
false;
814 if (pending_since == 0)
1034 entry->
data = stats_data;
1053 *have_snapshot =
true;
1057 *have_snapshot =
false;
1255 else if (valid[
idx])
1300 true, created_entry);
1302 if (entry_ref->
pending == NULL)
1306 Assert(entrysize != (
size_t) -1);
1328 if (entry_ref == NULL || entry_ref->
pending == NULL)
1339 void *pending_data = entry_ref->
pending;
1341 Assert(pending_data != NULL);
1348 pfree(pending_data);
1360 bool have_pending =
false;
1391 Assert(did_flush || nowait);
1403 have_pending =
true;
1410 return have_pending;
1442 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1443 errmsg(
"invalid statistics kind: \"%s\"", kind_str)));
1486 if (kind_info->
name == NULL || strlen(kind_info->
name) == 0)
1488 (
errmsg(
"custom cumulative statistics name is invalid"),
1489 errhint(
"Provide a non-empty name for the custom cumulative statistics.")));
1493 errhint(
"Provide a custom cumulative statistics ID between %u and %u.",
1498 (
errmsg(
"failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->
name, kind),
1499 errdetail(
"Custom cumulative statistics must be registered while initializing modules in \"shared_preload_libraries\".")));
1508 (
errmsg(
"custom cumulative statistics property is invalid"),
1509 errhint(
"Custom cumulative statistics require a shared memory size for fixed-numbered objects.")));
1525 (
errmsg(
"failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->
name, kind),
1526 errdetail(
"Custom cumulative statistics \"%s\" already registered with the same ID.",
1538 (
errmsg(
"failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->
name, kind),
1539 errdetail(
"Existing cumulative statistics with ID %u has the same name.", existing_kind)));
1545 (
errmsg(
"registered custom cumulative statistics \"%s\" with ID %u",
1546 kind_info->
name, kind)));
1554#ifdef USE_ASSERT_CHECKING
1558 Assert(pgstat_is_initialized && !pgstat_is_shutdown);
1574 rc = fwrite(ptr,
len, 1, fpout);
1580#define write_chunk_s(fpout, ptr) write_chunk(fpout, ptr, sizeof(*ptr))
1604 elog(
DEBUG2,
"writing stats file \"%s\"", statfile);
1614 errmsg(
"could not open temporary statistics file \"%s\": %m",
1682 elog(
WARNING,
"found unknown stats entry %u/%u/%" PRIu64,
1683 ps->key.kind,
ps->key.dboid,
1735 errmsg(
"could not write temporary statistics file \"%s\": %m",
1744 errmsg(
"could not close temporary statistics file \"%s\": %m",
1759 return fread(ptr, 1,
len, fpin) ==
len;
1762#define read_chunk_s(fpin, ptr) read_chunk(fpin, ptr, sizeof(*ptr))
1782 elog(
DEBUG2,
"reading stats file \"%s\"", statfile);
1795 if (errno != ENOENT)
1798 errmsg(
"could not open statistics file \"%s\": %m",
1815 elog(
WARNING,
"found incorrect format ID %d (expected %d)",
1826 int t = fgetc(fpin);
1839 elog(
WARNING,
"could not read stats kind for entry of type %c", t);
1845 elog(
WARNING,
"invalid stats kind %u for entry of type %c",
1853 elog(
WARNING,
"could not find information of kind %u for entry of type %c",
1860 elog(
WARNING,
"invalid fixed_amount in stats kind %u for entry of type %c",
1879 elog(
WARNING,
"could not read data of stats kind %u for entry of type %c with size %u",
1900 elog(
WARNING,
"could not read key for entry of type %c", t);
1906 elog(
WARNING,
"invalid stats kind for entry %u/%u/%" PRIu64
" of type %c",
1921 elog(
WARNING,
"could not read stats kind for entry of type %c", t);
1926 elog(
WARNING,
"could not read name of stats kind %u for entry of type %c",
1932 elog(
WARNING,
"invalid stats kind %u for entry of type %c",
1940 elog(
WARNING,
"could not find information of kind %u for entry of type %c",
1947 elog(
WARNING,
"invalid from_serialized_name in stats kind %u for entry of type %c",
1957 elog(
WARNING,
"could not seek \"%s\" of stats kind %u for entry of type %c",
1979 elog(
WARNING,
"found duplicate stats entry %u/%u/%" PRIu64
" of type %c",
1992 elog(
WARNING,
"could not read data for entry %u/%u/%" PRIu64
" of type %c",
2006 if (fgetc(fpin) != EOF)
2015 elog(
WARNING,
"could not read entry of type %c", t);
2023 elog(
DEBUG2,
"removing permanent stats file \"%s\"", statfile);
2030 (
errmsg(
"corrupted statistics file \"%s\"", statfile)));
Datum idx(PG_FUNCTION_ARGS)
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)
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 errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
int durable_rename(const char *oldfile, const char *newfile, int elevel)
FILE * AllocateFile(const char *name, const char *mode)
bool IsPostmasterEnvironment
Assert(PointerIsAligned(start, uint64))
static void dlist_init(dlist_head *head)
static bool dlist_has_next(const dlist_head *head, const dlist_node *node)
static dlist_node * dlist_next_node(dlist_head *head, dlist_node *node)
static void dlist_delete(dlist_node *node)
static dlist_node * dlist_head_node(dlist_head *head)
static bool dlist_is_empty(const dlist_head *head)
static void dlist_push_tail(dlist_head *head, dlist_node *node)
#define DLIST_STATIC_INIT(name)
#define dlist_container(type, membername, ptr)
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
void LWLockRelease(LWLock *lock)
void * MemoryContextAlloc(MemoryContext context, Size size)
void * MemoryContextAllocZero(MemoryContext context, Size size)
void pfree(void *pointer)
MemoryContext TopMemoryContext
void MemoryContextDelete(MemoryContext context)
#define AllocSetContextCreate
#define ALLOCSET_SMALL_SIZES
#define CHECK_FOR_INTERRUPTS()
BackendType MyBackendType
bool process_shared_preload_libraries_in_progress
void pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
static bool pgStatForceNextFlush
int pgstat_fetch_consistency
#define PGSTAT_MIN_INTERVAL
void pgstat_snapshot_fixed(PgStat_Kind kind)
static void pgstat_prep_snapshot(void)
static const PgStat_KindInfo ** pgstat_kind_custom_infos
static void pgstat_build_snapshot_fixed(PgStat_Kind kind)
#define PGSTAT_SNAPSHOT_HASH_SIZE
#define PGSTAT_FILE_ENTRY_END
void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref)
void pgstat_reset_counters(void)
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *created_entry)
static void pgstat_build_snapshot(void)
void pgstat_initialize(void)
static void pgstat_write_statsfile(void)
long pgstat_report_stat(bool force)
static void pgstat_read_statsfile(void)
static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
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)
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
static void pgstat_reset_after_failure(void)
void assign_stats_fetch_consistency(int newval, void *extra)
void pgstat_force_next_flush(void)
static bool pgstat_is_kind_valid(PgStat_Kind kind)
void pgstat_clear_snapshot(void)
TimestampTz pgstat_get_stat_snapshot_timestamp(bool *have_snapshot)
bool pgstat_have_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
static MemoryContext pgStatPendingContext
static dlist_head pgStatPending
#define PGSTAT_FILE_ENTRY_HASH
static void pgstat_init_snapshot_fixed(void)
PgStat_EntryRef * pgstat_fetch_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
static void write_chunk(FILE *fpout, void *ptr, size_t len)
void pgstat_restore_stats(void)
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
#define write_chunk_s(fpout, ptr)
struct PgStat_SnapshotEntry PgStat_SnapshotEntry
#define PGSTAT_MAX_INTERVAL
#define PGSTAT_FILE_ENTRY_FIXED
static bool read_chunk(FILE *fpin, void *ptr, size_t len)
#define read_chunk_s(fpin, ptr)
#define PGSTAT_IDLE_INTERVAL
#define PGSTAT_FILE_ENTRY_NAME
void pgstat_register_kind(PgStat_Kind kind, const PgStat_KindInfo *kind_info)
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_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
void pgstat_archiver_init_shmem_cb(void *stats)
void pgstat_archiver_reset_all_cb(TimestampTz ts)
void pgstat_archiver_snapshot_cb(void)
bool pgstat_backend_have_pending_cb(void)
void pgstat_backend_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
bool pgstat_backend_flush_cb(bool nowait)
void pgstat_bgwriter_reset_all_cb(TimestampTz ts)
void pgstat_bgwriter_init_shmem_cb(void *stats)
void pgstat_bgwriter_snapshot_cb(void)
void pgstat_checkpointer_snapshot_cb(void)
void pgstat_checkpointer_init_shmem_cb(void *stats)
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_Backend PgStatShared_Backend
struct PgStatShared_ReplSlot PgStatShared_ReplSlot
bool pgstat_io_have_pending_cb(void)
bool pgstat_io_flush_cb(bool nowait)
void pgstat_io_reset_all_cb(TimestampTz ts)
void pgstat_io_snapshot_cb(void)
void pgstat_io_init_shmem_cb(void *stats)
#define PGSTAT_KIND_CUSTOM_MAX
#define PGSTAT_KIND_ARCHIVER
static bool pgstat_is_kind_builtin(PgStat_Kind kind)
static bool pgstat_is_kind_custom(PgStat_Kind kind)
#define PGSTAT_KIND_BGWRITER
#define PGSTAT_KIND_CUSTOM_MIN
#define PGSTAT_KIND_REPLSLOT
#define PGSTAT_KIND_FUNCTION
#define PGSTAT_KIND_BUILTIN_MAX
#define PGSTAT_KIND_CUSTOM_SIZE
#define PGSTAT_KIND_DATABASE
#define PGSTAT_KIND_INVALID
#define PGSTAT_KIND_RELATION
#define PGSTAT_KIND_CHECKPOINTER
#define PGSTAT_KIND_BUILTIN_SIZE
#define PGSTAT_KIND_SUBSCRIPTION
#define PGSTAT_KIND_BACKEND
#define PGSTAT_KIND_BUILTIN_MIN
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)
void pgstat_request_entry_refs_gc(void)
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)
bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
void pgstat_attach_shmem(void)
bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
void pgstat_reset_entry(PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts)
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)
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
void pgstat_detach_shmem(void)
void pgstat_slru_snapshot_cb(void)
bool pgstat_slru_flush_cb(bool nowait)
void pgstat_slru_reset_all_cb(TimestampTz ts)
void pgstat_slru_init_shmem_cb(void *stats)
bool pgstat_slru_have_pending_cb(void)
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)
void pgstat_wal_init_shmem_cb(void *stats)
void pgstat_wal_init_backend_cb(void)
bool pgstat_wal_flush_cb(bool nowait)
bool pgstat_wal_have_pending_cb(void)
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)
pg_atomic_uint32 refcount
PgStatShared_Common * shared_stats
PgStatShared_HashEntry * shared_entry
bool accessed_across_databases
bool(* flush_static_cb)(bool nowait)
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(* have_static_pending_cb)(void)
bool(* flush_pending_cb)(PgStat_EntryRef *sr, bool nowait)
void(* reset_all_cb)(TimestampTz ts)
void(* init_backend_cb)(void)
void(* delete_pending_cb)(PgStat_EntryRef *sr)
void(* snapshot_cb)(void)
dshash_table * shared_hash
PgStat_ShmemControl * shmem
void * custom_data[PGSTAT_KIND_CUSTOM_SIZE]
void * custom_data[PGSTAT_KIND_CUSTOM_SIZE]
TimestampTz snapshot_timestamp
bool custom_valid[PGSTAT_KIND_CUSTOM_SIZE]
PgStat_FetchConsistency mode
struct pgstat_snapshot_hash * stats
bool fixed_valid[PGSTAT_KIND_BUILTIN_SIZE]
bool IsTransactionOrTransactionBlock(void)
TimestampTz GetCurrentTransactionStopTimestamp(void)