118 #define PGSTAT_MIN_INTERVAL 1000
120 #define PGSTAT_MAX_INTERVAL 60000
122 #define PGSTAT_IDLE_INTERVAL 10000
129 #define PGSTAT_SNAPSHOT_HASH_SIZE 512
147 #define SH_PREFIX pgstat_snapshot
148 #define SH_ELEMENT_TYPE PgStat_SnapshotEntry
149 #define SH_KEY_TYPE PgStat_HashKey
151 #define SH_HASH_KEY(tb, key) \
152 pgstat_hash_hash_key(&key, sizeof(PgStat_HashKey), NULL)
153 #define SH_EQUAL(tb, a, b) \
154 pgstat_cmp_hash_key(&a, &b, sizeof(PgStat_HashKey), NULL) == 0
155 #define SH_SCOPE static inline
232 #ifdef USE_ASSERT_CHECKING
233 static bool pgstat_is_initialized =
false;
234 static bool pgstat_is_shutdown =
false;
258 .fixed_amount =
false,
260 .accessed_across_databases =
true,
274 .fixed_amount =
false,
288 .fixed_amount =
false,
301 .fixed_amount =
false,
303 .accessed_across_databases =
true,
304 .named_on_disk =
true,
316 .name =
"subscription",
318 .fixed_amount =
false,
320 .accessed_across_databases =
true,
337 .fixed_amount =
true,
346 .fixed_amount =
true,
353 .name =
"checkpointer",
355 .fixed_amount =
true,
364 .fixed_amount =
true,
373 .fixed_amount =
true,
415 "didn't need to unlink permanent stats file \"%s\" - didn't exist",
420 errmsg(
"could not unlink permanent statistics file \"%s\": %m",
427 errmsg(
"unlinked permanent statistics file \"%s\"",
457 Assert(pgstat_is_initialized && !pgstat_is_shutdown);
490 Assert(!pgstat_is_shutdown);
510 #ifdef USE_ASSERT_CHECKING
511 pgstat_is_shutdown =
true;
524 Assert(!pgstat_is_initialized);
533 #ifdef USE_ASSERT_CHECKING
534 pgstat_is_initialized =
true;
587 Assert(pending_since == 0);
603 if (pending_since > 0 &&
609 else if (last_flush > 0 &&
613 if (pending_since == 0)
625 partial_flush =
false;
648 if (pending_since == 0)
856 entry->
data = stats_data;
872 *have_snapshot =
true;
876 *have_snapshot =
false;
1068 true, created_entry);
1070 if (entry_ref->
pending == NULL)
1074 Assert(entrysize != (
size_t) -1);
1096 if (entry_ref == NULL || entry_ref->
pending == NULL)
1107 void *pending_data = entry_ref->
pending;
1109 Assert(pending_data != NULL);
1116 pfree(pending_data);
1129 bool have_pending =
false;
1160 Assert(did_flush || nowait);
1172 have_pending =
true;
1179 return have_pending;
1198 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1199 errmsg(
"invalid statistics kind: \"%s\"", kind_str)));
1222 #ifdef USE_ASSERT_CHECKING
1226 Assert(pgstat_is_initialized && !pgstat_is_shutdown);
1242 rc = fwrite(ptr,
len, 1, fpout);
1248 #define write_chunk_s(fpout, ptr) write_chunk(fpout, ptr, sizeof(*ptr))
1269 elog(
DEBUG2,
"writing stats file \"%s\"", statfile);
1279 errmsg(
"could not open temporary statistics file \"%s\": %m",
1385 errmsg(
"could not write temporary statistics file \"%s\": %m",
1394 errmsg(
"could not close temporary statistics file \"%s\": %m",
1398 else if (rename(tmpfile, statfile) < 0)
1402 errmsg(
"could not rename temporary statistics file \"%s\" to \"%s\": %m",
1403 tmpfile, statfile)));
1412 return fread(ptr, 1,
len, fpin) ==
len;
1415 #define read_chunk_s(fpin, ptr) read_chunk(fpin, ptr, sizeof(*ptr))
1435 elog(
DEBUG2,
"reading stats file \"%s\"", statfile);
1448 if (errno != ENOENT)
1451 errmsg(
"could not open statistics file \"%s\": %m",
1506 int t = fgetc(fpin);
1570 elog(
WARNING,
"found duplicate stats entry %d/%u/%u",
1587 if (fgetc(fpin) != EOF)
1600 elog(
DEBUG2,
"removing permanent stats file \"%s\"", statfile);
1607 (
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 offsetof(type, field)
#define OidIsValid(objectId)
#define AssertArg(condition)
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 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_is_empty(dlist_head *head)
static void dlist_delete(dlist_node *node)
static dlist_node * dlist_next_node(dlist_head *head, dlist_node *node)
static bool dlist_has_next(dlist_head *head, dlist_node *node)
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')
void pfree(void *pointer)
MemoryContext TopMemoryContext
void * MemoryContextAllocZero(MemoryContext context, Size size)
void * MemoryContextAlloc(MemoryContext context, Size size)
MemoryContext CacheMemoryContext
void MemoryContextDelete(MemoryContext context)
#define AllocSetContextCreate
#define ALLOCSET_SMALL_SIZES
#define CHECK_FOR_INTERRUPTS()
static void header(const char *fmt,...) pg_attribute_printf(1
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 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)
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_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 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)
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_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)
#define DatumGetObjectId(X)
#define ObjectIdGetDatum(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 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 IsTransactionBlock(void)
TimestampTz GetCurrentTransactionStopTimestamp(void)