127#define PGSTAT_MIN_INTERVAL 1000
129#define PGSTAT_MAX_INTERVAL 60000
131#define PGSTAT_IDLE_INTERVAL 10000
138#define PGSTAT_SNAPSHOT_HASH_SIZE 512
144#define PGSTAT_FILE_ENTRY_END 'E'
145#define PGSTAT_FILE_ENTRY_FIXED 'F'
146#define PGSTAT_FILE_ENTRY_NAME 'N'
147#define PGSTAT_FILE_ENTRY_HASH 'S'
165#define SH_PREFIX pgstat_snapshot
166#define SH_ELEMENT_TYPE PgStat_SnapshotEntry
167#define SH_KEY_TYPE PgStat_HashKey
169#define SH_HASH_KEY(tb, key) \
170 pgstat_hash_hash_key(&key, sizeof(PgStat_HashKey), NULL)
171#define SH_EQUAL(tb, a, b) \
172 pgstat_cmp_hash_key(&a, &b, sizeof(PgStat_HashKey), NULL) == 0
173#define SH_SCOPE static inline
259#ifdef USE_ASSERT_CHECKING
260static bool pgstat_is_initialized =
false;
261static bool pgstat_is_shutdown =
false;
285 .fixed_amount =
false,
286 .write_to_file =
true,
288 .accessed_across_databases =
true,
302 .fixed_amount =
false,
303 .write_to_file =
true,
317 .fixed_amount =
false,
318 .write_to_file =
true,
331 .fixed_amount =
false,
332 .write_to_file =
true,
334 .accessed_across_databases =
true,
346 .name =
"subscription",
348 .fixed_amount =
false,
349 .write_to_file =
true,
351 .accessed_across_databases =
true,
365 .fixed_amount =
false,
366 .write_to_file =
false,
368 .accessed_across_databases =
true,
384 .fixed_amount =
true,
385 .write_to_file =
true,
400 .fixed_amount =
true,
401 .write_to_file =
true,
414 .name =
"checkpointer",
416 .fixed_amount =
true,
417 .write_to_file =
true,
432 .fixed_amount =
true,
433 .write_to_file =
true,
450 .fixed_amount =
true,
451 .write_to_file =
true,
468 .fixed_amount =
true,
469 .write_to_file =
true,
529 "didn't need to unlink permanent stats file \"%s\" - didn't exist",
534 errmsg(
"could not unlink permanent statistics file \"%s\": %m",
571 Assert(pgstat_is_initialized && !pgstat_is_shutdown);
604 Assert(!pgstat_is_shutdown);
628#ifdef USE_ASSERT_CHECKING
629 pgstat_is_shutdown =
true;
642 Assert(!pgstat_is_initialized);
662#ifdef USE_ASSERT_CHECKING
663 pgstat_is_initialized =
true;
714 bool do_flush =
false;
735 Assert(pending_since == 0);
763 if (pending_since > 0 &&
769 else if (last_flush > 0 &&
773 if (pending_since == 0)
785 partial_flush =
false;
815 if (pending_since == 0)
1035 entry->
data = stats_data;
1054 *have_snapshot =
true;
1058 *have_snapshot =
false;
1256 else if (valid[
idx])
1301 true, created_entry);
1303 if (entry_ref->
pending == NULL)
1307 Assert(entrysize != (
size_t) -1);
1329 if (entry_ref == NULL || entry_ref->
pending == NULL)
1340 void *pending_data = entry_ref->
pending;
1342 Assert(pending_data != NULL);
1349 pfree(pending_data);
1361 bool have_pending =
false;
1392 Assert(did_flush || nowait);
1404 have_pending =
true;
1411 return have_pending;
1443 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1444 errmsg(
"invalid statistics kind: \"%s\"", kind_str)));
1487 if (kind_info->
name == NULL || strlen(kind_info->
name) == 0)
1489 (
errmsg(
"custom cumulative statistics name is invalid"),
1490 errhint(
"Provide a non-empty name for the custom cumulative statistics.")));
1494 errhint(
"Provide a custom cumulative statistics ID between %u and %u.",
1499 (
errmsg(
"failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->
name, kind),
1500 errdetail(
"Custom cumulative statistics must be registered while initializing modules in \"shared_preload_libraries\".")));
1509 (
errmsg(
"custom cumulative statistics property is invalid"),
1510 errhint(
"Custom cumulative statistics require a shared memory size for fixed-numbered objects.")));
1526 (
errmsg(
"failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->
name, kind),
1527 errdetail(
"Custom cumulative statistics \"%s\" already registered with the same ID.",
1539 (
errmsg(
"failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->
name, kind),
1540 errdetail(
"Existing cumulative statistics with ID %u has the same name.", existing_kind)));
1546 (
errmsg(
"registered custom cumulative statistics \"%s\" with ID %u",
1547 kind_info->
name, kind)));
1555#ifdef USE_ASSERT_CHECKING
1559 Assert(pgstat_is_initialized && !pgstat_is_shutdown);
1575 rc = fwrite(ptr,
len, 1, fpout);
1581#define write_chunk_s(fpout, ptr) write_chunk(fpout, ptr, sizeof(*ptr))
1605 elog(
DEBUG2,
"writing stats file \"%s\" with redo %X/%X", statfile,
1616 errmsg(
"could not open temporary statistics file \"%s\": %m",
1687 elog(
WARNING,
"found unknown stats entry %u/%u/%llu",
1688 ps->key.kind,
ps->key.dboid,
1689 (
unsigned long long)
ps->key.objid);
1740 errmsg(
"could not write temporary statistics file \"%s\": %m",
1749 errmsg(
"could not close temporary statistics file \"%s\": %m",
1764 return fread(ptr, 1,
len, fpin) ==
len;
1767#define read_chunk_s(fpin, ptr) read_chunk(fpin, ptr, sizeof(*ptr))
1788 elog(
DEBUG2,
"reading stats file \"%s\" with redo %X/%X", statfile,
1802 if (errno != ENOENT)
1805 errmsg(
"could not open statistics file \"%s\": %m",
1822 elog(
WARNING,
"found incorrect format ID %d (expected %d)",
1836 if (file_redo != redo)
1838 elog(
WARNING,
"found incorrect redo LSN %X/%X (expected %X/%X)",
1849 int t = fgetc(fpin);
1862 elog(
WARNING,
"could not read stats kind for entry of type %c", t);
1868 elog(
WARNING,
"invalid stats kind %u for entry of type %c",
1876 elog(
WARNING,
"could not find information of kind %u for entry of type %c",
1883 elog(
WARNING,
"invalid fixed_amount in stats kind %u for entry of type %c",
1902 elog(
WARNING,
"could not read data of stats kind %u for entry of type %c with size %u",
1923 elog(
WARNING,
"could not read key for entry of type %c", t);
1929 elog(
WARNING,
"invalid stats kind for entry %u/%u/%llu of type %c",
1931 (
unsigned long long)
key.objid, t);
1944 elog(
WARNING,
"could not read stats kind for entry of type %c", t);
1949 elog(
WARNING,
"could not read name of stats kind %u for entry of type %c",
1955 elog(
WARNING,
"invalid stats kind %u for entry of type %c",
1963 elog(
WARNING,
"could not find information of kind %u for entry of type %c",
1970 elog(
WARNING,
"invalid from_serialized_name in stats kind %u for entry of type %c",
1980 elog(
WARNING,
"could not seek \"%s\" of stats kind %u for entry of type %c",
2002 elog(
WARNING,
"found duplicate stats entry %u/%u/%llu of type %c",
2004 (
unsigned long long)
key.objid, t);
2015 elog(
WARNING,
"could not read data for entry %u/%u/%llu of type %c",
2017 (
unsigned long long)
key.objid, t);
2029 if (fgetc(fpin) != EOF)
2038 elog(
WARNING,
"could not read entry of type %c", t);
2046 elog(
DEBUG2,
"removing permanent stats file \"%s\"", statfile);
2053 (
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 Assert(condition)
#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
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)
long pgstat_report_stat(bool force)
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_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
#define write_chunk_s(fpout, ptr)
void pgstat_restore_stats(XLogRecPtr redo)
struct PgStat_SnapshotEntry PgStat_SnapshotEntry
static void pgstat_read_statsfile(XLogRecPtr redo)
#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
static void pgstat_write_statsfile(XLogRecPtr redo)
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)
XLogRecPtr GetRedoRecPtr(void)
#define LSN_FORMAT_ARGS(lsn)