Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

PostgreSQL Source Code git master
pgstat.c File Reference
#include "postgres.h"
#include <unistd.h>
#include "access/xact.h"
#include "lib/dshash.h"
#include "pgstat.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/lwlock.h"
#include "utils/guc_hooks.h"
#include "utils/memutils.h"
#include "utils/pgstat_internal.h"
#include "utils/timestamp.h"
#include "lib/simplehash.h"
Include dependency graph for pgstat.c:

Go to the source code of this file.

Data Structures

struct  PgStat_SnapshotEntry
 

Macros

#define PGSTAT_MIN_INTERVAL   1000
 
#define PGSTAT_MAX_INTERVAL   60000
 
#define PGSTAT_IDLE_INTERVAL   10000
 
#define PGSTAT_SNAPSHOT_HASH_SIZE   512
 
#define PGSTAT_FILE_ENTRY_END   'E' /* end of file */
 
#define PGSTAT_FILE_ENTRY_FIXED   'F' /* fixed-numbered stats entry */
 
#define PGSTAT_FILE_ENTRY_NAME   'N' /* stats entry identified by name */
 
#define PGSTAT_FILE_ENTRY_HASH
 
#define SH_PREFIX   pgstat_snapshot
 
#define SH_ELEMENT_TYPE   PgStat_SnapshotEntry
 
#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
 
#define write_chunk_s(fpout, ptr)   write_chunk(fpout, ptr, sizeof(*ptr))
 
#define read_chunk_s(fpin, ptr)   read_chunk(fpin, ptr, sizeof(*ptr))
 

Typedefs

typedef struct PgStat_SnapshotEntry PgStat_SnapshotEntry
 

Functions

static void pgstat_write_statsfile (void)
 
static void pgstat_read_statsfile (void)
 
static void pgstat_init_snapshot_fixed (void)
 
static void pgstat_reset_after_failure (void)
 
static bool pgstat_flush_pending_entries (bool nowait)
 
static void pgstat_prep_snapshot (void)
 
static void pgstat_build_snapshot (void)
 
static void pgstat_build_snapshot_fixed (PgStat_Kind kind)
 
static bool pgstat_is_kind_valid (PgStat_Kind kind)
 
void pgstat_restore_stats (void)
 
void pgstat_discard_stats (void)
 
void pgstat_before_server_shutdown (int code, Datum arg)
 
static void pgstat_shutdown_hook (int code, Datum arg)
 
void pgstat_initialize (void)
 
long pgstat_report_stat (bool force)
 
void pgstat_force_next_flush (void)
 
static bool match_db_entries (PgStatShared_HashEntry *entry, Datum match_data)
 
void pgstat_reset_counters (void)
 
void pgstat_reset (PgStat_Kind kind, Oid dboid, uint64 objid)
 
void pgstat_reset_of_kind (PgStat_Kind kind)
 
void pgstat_clear_snapshot (void)
 
void * pgstat_fetch_entry (PgStat_Kind kind, Oid dboid, uint64 objid)
 
TimestampTz pgstat_get_stat_snapshot_timestamp (bool *have_snapshot)
 
bool pgstat_have_entry (PgStat_Kind kind, Oid dboid, uint64 objid)
 
void pgstat_snapshot_fixed (PgStat_Kind kind)
 
PgStat_EntryRefpgstat_prep_pending_entry (PgStat_Kind kind, Oid dboid, uint64 objid, bool *created_entry)
 
PgStat_EntryRefpgstat_fetch_pending_entry (PgStat_Kind kind, Oid dboid, uint64 objid)
 
void pgstat_delete_pending_entry (PgStat_EntryRef *entry_ref)
 
PgStat_Kind pgstat_get_kind_from_str (char *kind_str)
 
const PgStat_KindInfopgstat_get_kind_info (PgStat_Kind kind)
 
void pgstat_register_kind (PgStat_Kind kind, const PgStat_KindInfo *kind_info)
 
static void write_chunk (FILE *fpout, void *ptr, size_t len)
 
static bool read_chunk (FILE *fpin, void *ptr, size_t len)
 
void assign_stats_fetch_consistency (int newval, void *extra)
 

Variables

bool pgstat_track_counts = false
 
int pgstat_fetch_consistency = PGSTAT_FETCH_CONSISTENCY_CACHE
 
PgStat_LocalState pgStatLocal
 
bool pgstat_report_fixed = false
 
static MemoryContext pgStatPendingContext = NULL
 
static dlist_head pgStatPending = DLIST_STATIC_INIT(pgStatPending)
 
static bool pgStatForceNextFlush = false
 
static bool force_stats_snapshot_clear = false
 
static const PgStat_KindInfo pgstat_kind_builtin_infos [PGSTAT_KIND_BUILTIN_SIZE]
 
static const PgStat_KindInfo ** pgstat_kind_custom_infos = NULL
 

Macro Definition Documentation

◆ PGSTAT_FILE_ENTRY_END

#define PGSTAT_FILE_ENTRY_END   'E' /* end of file */

Definition at line 143 of file pgstat.c.

◆ PGSTAT_FILE_ENTRY_FIXED

#define PGSTAT_FILE_ENTRY_FIXED   'F' /* fixed-numbered stats entry */

Definition at line 144 of file pgstat.c.

◆ PGSTAT_FILE_ENTRY_HASH

#define PGSTAT_FILE_ENTRY_HASH
Value:
'S' /* stats entry identified by
* PgStat_HashKey */

Definition at line 146 of file pgstat.c.

◆ PGSTAT_FILE_ENTRY_NAME

#define PGSTAT_FILE_ENTRY_NAME   'N' /* stats entry identified by name */

Definition at line 145 of file pgstat.c.

◆ PGSTAT_IDLE_INTERVAL

#define PGSTAT_IDLE_INTERVAL   10000

Definition at line 130 of file pgstat.c.

◆ PGSTAT_MAX_INTERVAL

#define PGSTAT_MAX_INTERVAL   60000

Definition at line 128 of file pgstat.c.

◆ PGSTAT_MIN_INTERVAL

#define PGSTAT_MIN_INTERVAL   1000

Definition at line 126 of file pgstat.c.

◆ PGSTAT_SNAPSHOT_HASH_SIZE

#define PGSTAT_SNAPSHOT_HASH_SIZE   512

Definition at line 137 of file pgstat.c.

◆ read_chunk_s

#define read_chunk_s (   fpin,
  ptr 
)    read_chunk(fpin, ptr, sizeof(*ptr))

Definition at line 1745 of file pgstat.c.

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 173 of file pgstat.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 172 of file pgstat.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   PgStat_SnapshotEntry

Definition at line 164 of file pgstat.c.

◆ SH_EQUAL

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

Definition at line 169 of file pgstat.c.

◆ SH_HASH_KEY

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

Definition at line 167 of file pgstat.c.

◆ SH_KEY

#define SH_KEY   key

Definition at line 166 of file pgstat.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   PgStat_HashKey

Definition at line 165 of file pgstat.c.

◆ SH_PREFIX

#define SH_PREFIX   pgstat_snapshot

Definition at line 163 of file pgstat.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 171 of file pgstat.c.

◆ write_chunk_s

#define write_chunk_s (   fpout,
  ptr 
)    write_chunk(fpout, ptr, sizeof(*ptr))

Definition at line 1563 of file pgstat.c.

Typedef Documentation

◆ PgStat_SnapshotEntry

Function Documentation

◆ assign_stats_fetch_consistency()

void assign_stats_fetch_consistency ( int  newval,
void *  extra 
)

Definition at line 2067 of file pgstat.c.

2069{
2070 /*
2071 * Changing this value in a transaction may cause snapshot state
2072 * inconsistencies, so force a clear of the current snapshot on the next
2073 * snapshot build attempt.
2074 */
#define newval
int pgstat_fetch_consistency
Definition: pgstat.c:204
static bool force_stats_snapshot_clear
Definition: pgstat.c:255

References force_stats_snapshot_clear, newval, and pgstat_fetch_consistency.

◆ match_db_entries()

static bool match_db_entries ( PgStatShared_HashEntry entry,
Datum  match_data 
)
static

Definition at line 821 of file pgstat.c.

823{
824 return entry->key.dboid == MyDatabaseId;
Oid MyDatabaseId
Definition: globals.c:94

References PgStat_HashKey::dboid, PgStatShared_HashEntry::key, and MyDatabaseId.

Referenced by pgstat_reset_counters().

◆ pgstat_before_server_shutdown()

void pgstat_before_server_shutdown ( int  code,
Datum  arg 
)

Definition at line 560 of file pgstat.c.

562{
563 Assert(pgStatLocal.shmem != NULL);
565
566 /*
567 * Stats should only be reported after pgstat_initialize() and before
568 * pgstat_shutdown(). This is a convenient point to catch most violations
569 * of this rule.
570 */
571 Assert(pgstat_is_initialized && !pgstat_is_shutdown);
572
573 /* flush out our own pending changes before writing out */
574 pgstat_report_stat(true);
575
576 /*
577 * Only write out file during normal shutdown. Don't even signal that
578 * we've shutdown during irregular shutdowns, because the shutdown
579 * sequence isn't coordinated to ensure this backend shuts down last.
580 */
581 if (code == 0)
582 {
585 }
Assert(PointerIsAligned(start, uint64))
static void pgstat_write_statsfile(void)
Definition: pgstat.c:1570
long pgstat_report_stat(bool force)
Definition: pgstat.c:692
PgStat_LocalState pgStatLocal
Definition: pgstat.c:212
PgStat_ShmemControl * shmem

References Assert(), PgStat_ShmemControl::is_shutdown, pgstat_report_stat(), pgstat_write_statsfile(), pgStatLocal, and PgStat_LocalState::shmem.

Referenced by CheckpointerMain(), and InitPostgres().

◆ pgstat_build_snapshot()

static void pgstat_build_snapshot ( void  )
static

Definition at line 1118 of file pgstat.c.

1120{
1121 dshash_seq_status hstat;
1123
1124 /* should only be called when we need a snapshot */
1126
1127 /* snapshot already built */
1129 return;
1130
1132
1133 Assert(pgStatLocal.snapshot.stats->members == 0);
1134
1136
1137 /*
1138 * Snapshot all variable stats.
1139 */
1140 dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
1141 while ((p = dshash_seq_next(&hstat)) != NULL)
1142 {
1143 PgStat_Kind kind = p->key.kind;
1144 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1145 bool found;
1146 PgStat_SnapshotEntry *entry;
1147 PgStatShared_Common *stats_data;
1148
1149 /*
1150 * Check if the stats object should be included in the snapshot.
1151 * Unless the stats kind can be accessed from all databases (e.g.,
1152 * database stats themselves), we only include stats for the current
1153 * database or objects not associated with a database (e.g. shared
1154 * relations).
1155 */
1156 if (p->key.dboid != MyDatabaseId &&
1157 p->key.dboid != InvalidOid &&
1158 !kind_info->accessed_across_databases)
1159 continue;
1160
1161 if (p->dropped)
1162 continue;
1163
1165
1166 stats_data = dsa_get_address(pgStatLocal.dsa, p->body);
1167 Assert(stats_data);
1168
1169 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, p->key, &found);
1170 Assert(!found);
1171
1173 pgstat_get_entry_len(kind));
1174
1175 /*
1176 * Acquire the LWLock directly instead of using
1177 * pg_stat_lock_entry_shared() which requires a reference.
1178 */
1179 LWLockAcquire(&stats_data->lock, LW_SHARED);
1180 memcpy(entry->data,
1181 pgstat_get_entry_data(kind, stats_data),
1182 pgstat_get_entry_len(kind));
1183 LWLockRelease(&stats_data->lock);
1184 }
1185 dshash_seq_term(&hstat);
1186
1187 /*
1188 * Build snapshot of all fixed-numbered stats.
1189 */
1190 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
1191 {
1192 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1193
1194 if (!kind_info)
1195 continue;
1196 if (!kind_info->fixed_amount)
1197 {
1198 Assert(kind_info->snapshot_cb == NULL);
1199 continue;
1200 }
1201
1203 }
1204
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:237
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1645
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:957
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
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1174
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1894
@ LW_SHARED
Definition: lwlock.h:113
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1229
static void pgstat_prep_snapshot(void)
Definition: pgstat.c:1097
static void pgstat_build_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1208
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1433
@ PGSTAT_FETCH_CONSISTENCY_SNAPSHOT
Definition: pgstat.h:49
static void * pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry)
static size_t pgstat_get_entry_len(PgStat_Kind kind)
#define PgStat_Kind
Definition: pgstat_kind.h:17
#define PGSTAT_KIND_MAX
Definition: pgstat_kind.h:21
#define PGSTAT_KIND_MIN
Definition: pgstat_kind.h:20
#define InvalidOid
Definition: postgres_ext.h:37
pg_atomic_uint32 refcount
PgStat_Kind kind
bool accessed_across_databases
void(* snapshot_cb)(void)
PgStat_Snapshot snapshot
dshash_table * shared_hash
TimestampTz snapshot_timestamp
MemoryContext context
PgStat_FetchConsistency mode
struct pgstat_snapshot_hash * stats

References PgStat_KindInfo::accessed_across_databases, Assert(), PgStatShared_HashEntry::body, PgStat_Snapshot::context, PgStat_SnapshotEntry::data, PgStat_HashKey::dboid, PgStatShared_HashEntry::dropped, PgStat_LocalState::dsa, dsa_get_address(), dshash_seq_init(), dshash_seq_next(), dshash_seq_term(), PgStat_KindInfo::fixed_amount, GetCurrentTimestamp(), InvalidOid, PgStatShared_HashEntry::key, PgStat_HashKey::kind, PgStatShared_Common::lock, LW_SHARED, LWLockAcquire(), LWLockRelease(), MemoryContextAlloc(), PgStat_Snapshot::mode, MyDatabaseId, pg_atomic_read_u32(), pgstat_build_snapshot_fixed(), pgstat_fetch_consistency, PGSTAT_FETCH_CONSISTENCY_SNAPSHOT, pgstat_get_entry_data(), pgstat_get_entry_len(), pgstat_get_kind_info(), PgStat_Kind, PGSTAT_KIND_MAX, PGSTAT_KIND_MIN, pgstat_prep_snapshot(), pgStatLocal, PgStatShared_HashEntry::refcount, PgStat_LocalState::shared_hash, PgStat_LocalState::snapshot, PgStat_KindInfo::snapshot_cb, PgStat_Snapshot::snapshot_timestamp, and PgStat_Snapshot::stats.

Referenced by pgstat_fetch_entry(), and pgstat_snapshot_fixed().

◆ pgstat_build_snapshot_fixed()

static void pgstat_build_snapshot_fixed ( PgStat_Kind  kind)
static

Definition at line 1208 of file pgstat.c.

1210{
1211 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1212 int idx;
1213 bool *valid;
1214
1215 /* Position in fixed_valid or custom_valid */
1216 if (pgstat_is_kind_builtin(kind))
1217 {
1218 idx = kind;
1220 }
1221 else
1222 {
1223 idx = kind - PGSTAT_KIND_CUSTOM_MIN;
1225 }
1226
1227 Assert(kind_info->fixed_amount);
1228 Assert(kind_info->snapshot_cb != NULL);
1229
1231 {
1232 /* rebuild every time */
1233 valid[idx] = false;
1234 }
1235 else if (valid[idx])
1236 {
1237 /* in snapshot mode we shouldn't get called again */
1239 return;
1240 }
1241
1242 Assert(!valid[idx]);
1243
1244 kind_info->snapshot_cb();
1245
1246 Assert(!valid[idx]);
1247 valid[idx] = true;
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:262
@ PGSTAT_FETCH_CONSISTENCY_NONE
Definition: pgstat.h:47
@ PGSTAT_FETCH_CONSISTENCY_CACHE
Definition: pgstat.h:48
static bool pgstat_is_kind_builtin(PgStat_Kind kind)
Definition: pgstat_kind.h:61
#define PGSTAT_KIND_CUSTOM_MIN
Definition: pgstat_kind.h:49
bool custom_valid[PGSTAT_KIND_CUSTOM_SIZE]
bool fixed_valid[PGSTAT_KIND_BUILTIN_SIZE]

References Assert(), PgStat_Snapshot::custom_valid, PgStat_KindInfo::fixed_amount, PgStat_Snapshot::fixed_valid, idx(), pgstat_fetch_consistency, PGSTAT_FETCH_CONSISTENCY_CACHE, PGSTAT_FETCH_CONSISTENCY_NONE, pgstat_get_kind_info(), pgstat_is_kind_builtin(), PGSTAT_KIND_CUSTOM_MIN, pgStatLocal, PgStat_LocalState::snapshot, and PgStat_KindInfo::snapshot_cb.

Referenced by pgstat_build_snapshot(), pgstat_snapshot_fixed(), and pgstat_write_statsfile().

◆ pgstat_clear_snapshot()

void pgstat_clear_snapshot ( void  )

Definition at line 900 of file pgstat.c.

902{
904
911
912 /* Release memory, if any was allocated */
914 {
916
917 /* Reset variables */
919 }
920
921 /*
922 * Historically the backend_status.c facilities lived in this file, and
923 * were reset with the same function. For now keep it that way, and
924 * forward the reset request.
925 */
927
928 /* Reset this flag, as it may be possible that a cleanup was forced. */
void pgstat_clear_backend_activity_snapshot(void)
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:469
#define pgstat_assert_is_up()

References PgStat_Snapshot::context, PgStat_Snapshot::custom_valid, PgStat_Snapshot::fixed_valid, force_stats_snapshot_clear, MemoryContextDelete(), PgStat_Snapshot::mode, pgstat_assert_is_up, pgstat_clear_backend_activity_snapshot(), PGSTAT_FETCH_CONSISTENCY_NONE, pgStatLocal, PgStat_LocalState::snapshot, and PgStat_Snapshot::stats.

Referenced by AtEOXact_PgStat(), pg_stat_clear_snapshot(), pgstat_get_stat_snapshot_timestamp(), pgstat_prep_snapshot(), pgstat_snapshot_fixed(), and PostPrepare_PgStat().

◆ pgstat_delete_pending_entry()

void pgstat_delete_pending_entry ( PgStat_EntryRef entry_ref)

Definition at line 1314 of file pgstat.c.

1316{
1317 PgStat_Kind kind = entry_ref->shared_entry->key.kind;
1318 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1319 void *pending_data = entry_ref->pending;
1320
1321 Assert(pending_data != NULL);
1322 /* !fixed_amount stats should be handled explicitly */
1323 Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1324
1325 if (kind_info->delete_pending_cb)
1326 kind_info->delete_pending_cb(entry_ref);
1327
1328 pfree(pending_data);
1329 entry_ref->pending = NULL;
1330
1331 dlist_delete(&entry_ref->pending_node);
static void dlist_delete(dlist_node *node)
Definition: ilist.h:405
void pfree(void *pointer)
Definition: mcxt.c:1594
PgStatShared_HashEntry * shared_entry
dlist_node pending_node
void(* delete_pending_cb)(PgStat_EntryRef *sr)

References Assert(), PgStat_KindInfo::delete_pending_cb, dlist_delete(), PgStatShared_HashEntry::key, PgStat_HashKey::kind, PgStat_EntryRef::pending, PgStat_EntryRef::pending_node, pfree(), pgstat_get_kind_info(), PgStat_Kind, and PgStat_EntryRef::shared_entry.

Referenced by pgstat_flush_pending_entries(), and pgstat_release_entry_ref().

◆ pgstat_discard_stats()

void pgstat_discard_stats ( void  )

Definition at line 517 of file pgstat.c.

519{
520 int ret;
521
522 /* NB: this needs to be done even in single user mode */
523
524 ret = unlink(PGSTAT_STAT_PERMANENT_FILENAME);
525 if (ret != 0)
526 {
527 if (errno == ENOENT)
528 elog(DEBUG2,
529 "didn't need to unlink permanent stats file \"%s\" - didn't exist",
531 else
532 ereport(LOG,
534 errmsg("could not unlink permanent statistics file \"%s\": %m",
536 }
537 else
538 {
541 errmsg_internal("unlinked permanent statistics file \"%s\"",
543 }
544
545 /*
546 * Reset stats contents. This will set reset timestamps of fixed-numbered
547 * stats to the current time (no variable stats exist).
548 */
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1161
int errcode_for_file_access(void)
Definition: elog.c:877
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define LOG
Definition: elog.h:31
#define DEBUG2
Definition: elog.h:29
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:150
static void pgstat_reset_after_failure(void)
Definition: pgstat.c:2044
#define PGSTAT_STAT_PERMANENT_FILENAME
Definition: pgstat.h:31

References DEBUG2, elog, ereport, errcode_for_file_access(), errmsg(), errmsg_internal(), LOG, pgstat_reset_after_failure(), and PGSTAT_STAT_PERMANENT_FILENAME.

Referenced by StartupXLOG().

◆ pgstat_fetch_entry()

void * pgstat_fetch_entry ( PgStat_Kind  kind,
Oid  dboid,
uint64  objid 
)

Definition at line 932 of file pgstat.c.

934{
935 PgStat_HashKey key = {0};
936 PgStat_EntryRef *entry_ref;
937 void *stats_data;
938 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
939
940 /* should be called from backends */
942 Assert(!kind_info->fixed_amount);
943
945
946 key.kind = kind;
947 key.dboid = dboid;
948 key.objid = objid;
949
950 /* if we need to build a full snapshot, do so */
953
954 /* if caching is desired, look up in cache */
956 {
957 PgStat_SnapshotEntry *entry = NULL;
958
959 entry = pgstat_snapshot_lookup(pgStatLocal.snapshot.stats, key);
960
961 if (entry)
962 return entry->data;
963
964 /*
965 * If we built a full snapshot and the key is not in
966 * pgStatLocal.snapshot.stats, there are no matching stats.
967 */
969 return NULL;
970 }
971
973
974 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
975
976 if (entry_ref == NULL || entry_ref->shared_entry->dropped)
977 {
978 /* create empty entry when using PGSTAT_FETCH_CONSISTENCY_CACHE */
980 {
981 PgStat_SnapshotEntry *entry = NULL;
982 bool found;
983
984 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
985 Assert(!found);
986 entry->data = NULL;
987 }
988 return NULL;
989 }
990
991 /*
992 * Allocate in caller's context for PGSTAT_FETCH_CONSISTENCY_NONE,
993 * otherwise we could quickly end up with a fair bit of memory used due to
994 * repeated accesses.
995 */
997 stats_data = palloc(kind_info->shared_data_len);
998 else
1000 kind_info->shared_data_len);
1001
1002 (void) pgstat_lock_entry_shared(entry_ref, false);
1003 memcpy(stats_data,
1004 pgstat_get_entry_data(kind, entry_ref->shared_stats),
1005 kind_info->shared_data_len);
1006 pgstat_unlock_entry(entry_ref);
1007
1009 {
1010 PgStat_SnapshotEntry *entry = NULL;
1011 bool found;
1012
1013 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
1014 entry->data = stats_data;
1015 }
1016
1017 return stats_data;
bool IsUnderPostmaster
Definition: globals.c:120
bool IsPostmasterEnvironment
Definition: globals.c:119
void * palloc(Size size)
Definition: mcxt.c:1365
static void pgstat_build_snapshot(void)
Definition: pgstat.c:1118
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:469
bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:708
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:720
PgStatShared_Common * shared_stats

References Assert(), PgStat_Snapshot::context, PgStat_SnapshotEntry::data, PgStatShared_HashEntry::dropped, PgStat_KindInfo::fixed_amount, IsPostmasterEnvironment, IsUnderPostmaster, sort-test::key, MemoryContextAlloc(), PgStat_Snapshot::mode, palloc(), pgstat_build_snapshot(), pgstat_fetch_consistency, PGSTAT_FETCH_CONSISTENCY_CACHE, PGSTAT_FETCH_CONSISTENCY_NONE, PGSTAT_FETCH_CONSISTENCY_SNAPSHOT, pgstat_get_entry_data(), pgstat_get_entry_ref(), pgstat_get_kind_info(), pgstat_lock_entry_shared(), pgstat_prep_snapshot(), pgstat_unlock_entry(), pgStatLocal, PgStat_KindInfo::shared_data_len, PgStat_EntryRef::shared_entry, PgStat_EntryRef::shared_stats, PgStat_LocalState::snapshot, and PgStat_Snapshot::stats.

Referenced by pgstat_fetch_replslot(), pgstat_fetch_stat_backend(), pgstat_fetch_stat_dbentry(), pgstat_fetch_stat_funcentry(), pgstat_fetch_stat_injentry(), pgstat_fetch_stat_subscription(), and pgstat_fetch_stat_tabentry_ext().

◆ pgstat_fetch_pending_entry()

PgStat_EntryRef * pgstat_fetch_pending_entry ( PgStat_Kind  kind,
Oid  dboid,
uint64  objid 
)

Definition at line 1301 of file pgstat.c.

1303{
1304 PgStat_EntryRef *entry_ref;
1305
1306 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
1307
1308 if (entry_ref == NULL || entry_ref->pending == NULL)
1309 return NULL;
1310
1311 return entry_ref;

References PgStat_EntryRef::pending, and pgstat_get_entry_ref().

Referenced by find_funcstat_entry(), and find_tabstat_entry().

◆ pgstat_flush_pending_entries()

static bool pgstat_flush_pending_entries ( bool  nowait)
static

Definition at line 1337 of file pgstat.c.

1339{
1340 bool have_pending = false;
1341 dlist_node *cur = NULL;
1342
1343 /*
1344 * Need to be a bit careful iterating over the list of pending entries.
1345 * Processing a pending entry may queue further pending entries to the end
1346 * of the list that we want to process, so a simple iteration won't do.
1347 * Further complicating matters is that we want to delete the current
1348 * entry in each iteration from the list if we flushed successfully.
1349 *
1350 * So we just keep track of the next pointer in each loop iteration.
1351 */
1354
1355 while (cur)
1356 {
1357 PgStat_EntryRef *entry_ref =
1358 dlist_container(PgStat_EntryRef, pending_node, cur);
1359 PgStat_HashKey key = entry_ref->shared_entry->key;
1360 PgStat_Kind kind = key.kind;
1361 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1362 bool did_flush;
1364
1365 Assert(!kind_info->fixed_amount);
1366 Assert(kind_info->flush_pending_cb != NULL);
1367
1368 /* flush the stats, if possible */
1369 did_flush = kind_info->flush_pending_cb(entry_ref, nowait);
1370
1371 Assert(did_flush || nowait);
1372
1373 /* determine next entry, before deleting the pending entry */
1376 else
1377 next = NULL;
1378
1379 /* if successfully flushed, remove entry */
1380 if (did_flush)
1381 pgstat_delete_pending_entry(entry_ref);
1382 else
1383 have_pending = true;
1384
1385 cur = next;
1386 }
1387
1388 Assert(dlist_is_empty(&pgStatPending) == !have_pending);
1389
1390 return have_pending;
static int32 next
Definition: blutils.c:224
struct cursor * cur
Definition: ecpg.c:29
static bool dlist_has_next(const dlist_head *head, const dlist_node *node)
Definition: ilist.h:503
static dlist_node * dlist_next_node(dlist_head *head, dlist_node *node)
Definition: ilist.h:537
static dlist_node * dlist_head_node(dlist_head *head)
Definition: ilist.h:565
static bool dlist_is_empty(const dlist_head *head)
Definition: ilist.h:336
#define dlist_container(type, membername, ptr)
Definition: ilist.h:593
void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat.c:1314
static dlist_head pgStatPending
Definition: pgstat.c:242
bool(* flush_pending_cb)(PgStat_EntryRef *sr, bool nowait)

References Assert(), cur, dlist_container, dlist_has_next(), dlist_head_node(), dlist_is_empty(), dlist_next_node(), PgStat_KindInfo::fixed_amount, PgStat_KindInfo::flush_pending_cb, PgStatShared_HashEntry::key, sort-test::key, next, pgstat_delete_pending_entry(), pgstat_get_kind_info(), PgStat_Kind, pgStatPending, and PgStat_EntryRef::shared_entry.

Referenced by pgstat_report_stat().

◆ pgstat_force_next_flush()

void pgstat_force_next_flush ( void  )

Definition at line 812 of file pgstat.c.

814{
static bool pgStatForceNextFlush
Definition: pgstat.c:249

References pgStatForceNextFlush.

Referenced by pg_stat_force_next_flush().

◆ pgstat_get_kind_from_str()

PgStat_Kind pgstat_get_kind_from_str ( char *  kind_str)

Definition at line 1399 of file pgstat.c.

1401{
1402 for (PgStat_Kind kind = PGSTAT_KIND_BUILTIN_MIN; kind <= PGSTAT_KIND_BUILTIN_MAX; kind++)
1403 {
1404 if (pg_strcasecmp(kind_str, pgstat_kind_builtin_infos[kind].name) == 0)
1405 return kind;
1406 }
1407
1408 /* Check the custom set of cumulative stats */
1410 {
1411 for (PgStat_Kind kind = PGSTAT_KIND_CUSTOM_MIN; kind <= PGSTAT_KIND_CUSTOM_MAX; kind++)
1412 {
1414
1417 return kind;
1418 }
1419 }
1420
1421 ereport(ERROR,
1422 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1423 errmsg("invalid statistics kind: \"%s\"", kind_str)));
1424 return PGSTAT_KIND_INVALID; /* avoid compiler warnings */
uint32_t uint32
Definition: c.h:539
int errcode(int sqlerrcode)
Definition: elog.c:854
#define ERROR
Definition: elog.h:39
static const PgStat_KindInfo ** pgstat_kind_custom_infos
Definition: pgstat.c:492
static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
Definition: pgstat.c:281
#define PGSTAT_KIND_CUSTOM_MAX
Definition: pgstat_kind.h:50
#define PGSTAT_KIND_BUILTIN_MAX
Definition: pgstat_kind.h:43
#define PGSTAT_KIND_INVALID
Definition: pgstat_kind.h:24
#define PGSTAT_KIND_BUILTIN_MIN
Definition: pgstat_kind.h:42
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
const char * name

References ereport, errcode(), errmsg(), ERROR, idx(), name, pg_strcasecmp(), PgStat_Kind, pgstat_kind_builtin_infos, PGSTAT_KIND_BUILTIN_MAX, PGSTAT_KIND_BUILTIN_MIN, pgstat_kind_custom_infos, PGSTAT_KIND_CUSTOM_MAX, PGSTAT_KIND_CUSTOM_MIN, and PGSTAT_KIND_INVALID.

Referenced by pg_stat_have_stats().

◆ pgstat_get_kind_info()

◆ pgstat_get_stat_snapshot_timestamp()

TimestampTz pgstat_get_stat_snapshot_timestamp ( bool *  have_snapshot)

Definition at line 1025 of file pgstat.c.

1027{
1030
1032 {
1033 *have_snapshot = true;
1035 }
1036
1037 *have_snapshot = false;
1038
1039 return 0;
void pgstat_clear_snapshot(void)
Definition: pgstat.c:900

References force_stats_snapshot_clear, PgStat_Snapshot::mode, pgstat_clear_snapshot(), PGSTAT_FETCH_CONSISTENCY_SNAPSHOT, pgStatLocal, PgStat_LocalState::snapshot, and PgStat_Snapshot::snapshot_timestamp.

Referenced by pg_stat_get_snapshot_timestamp().

◆ pgstat_have_entry()

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

Definition at line 1042 of file pgstat.c.

1044{
1045 /* fixed-numbered stats always exist */
1046 if (pgstat_get_kind_info(kind)->fixed_amount)
1047 return true;
1048
1049 return pgstat_get_entry_ref(kind, dboid, objid, false, NULL) != NULL;

References pgstat_get_entry_ref(), and pgstat_get_kind_info().

Referenced by pg_stat_have_stats().

◆ pgstat_init_snapshot_fixed()

static void pgstat_init_snapshot_fixed ( void  )
static

Definition at line 1078 of file pgstat.c.

1080{
1081 /*
1082 * Initialize fixed-numbered statistics data in snapshots, only for custom
1083 * stats kinds.
1084 */
1085 for (PgStat_Kind kind = PGSTAT_KIND_CUSTOM_MIN; kind <= PGSTAT_KIND_CUSTOM_MAX; kind++)
1086 {
1087 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1088
1089 if (!kind_info || !kind_info->fixed_amount)
1090 continue;
1091
1094 }
MemoryContext TopMemoryContext
Definition: mcxt.c:166
void * custom_data[PGSTAT_KIND_CUSTOM_SIZE]

References PgStat_Snapshot::custom_data, PgStat_KindInfo::fixed_amount, MemoryContextAlloc(), pgstat_get_kind_info(), PgStat_Kind, PGSTAT_KIND_CUSTOM_MAX, PGSTAT_KIND_CUSTOM_MIN, pgStatLocal, PgStat_KindInfo::shared_data_len, PgStat_LocalState::snapshot, and TopMemoryContext.

Referenced by pgstat_initialize().

◆ pgstat_initialize()

void pgstat_initialize ( void  )

Definition at line 639 of file pgstat.c.

641{
642 Assert(!pgstat_is_initialized);
643
645
647
648 /* Backend initialization callbacks */
649 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
650 {
651 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
652
653 if (kind_info == NULL || kind_info->init_backend_cb == NULL)
654 continue;
655
656 kind_info->init_backend_cb();
657 }
658
659 /* Set up a process-exit hook to clean up */
661
662#ifdef USE_ASSERT_CHECKING
663 pgstat_is_initialized = true;
664#endif
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:337
static void pgstat_init_snapshot_fixed(void)
Definition: pgstat.c:1078
static void pgstat_shutdown_hook(int code, Datum arg)
Definition: pgstat.c:601
void pgstat_attach_shmem(void)
Definition: pgstat_shmem.c:251
void(* init_backend_cb)(void)

References Assert(), before_shmem_exit(), PgStat_KindInfo::init_backend_cb, pgstat_attach_shmem(), pgstat_get_kind_info(), pgstat_init_snapshot_fixed(), PgStat_Kind, PGSTAT_KIND_MAX, PGSTAT_KIND_MIN, and pgstat_shutdown_hook().

Referenced by BaseInit().

◆ pgstat_is_kind_valid()

static bool pgstat_is_kind_valid ( PgStat_Kind  kind)
inlinestatic

◆ pgstat_prep_pending_entry()

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

Definition at line 1263 of file pgstat.c.

1265{
1266 PgStat_EntryRef *entry_ref;
1267
1268 /* need to be able to flush out */
1269 Assert(pgstat_get_kind_info(kind)->flush_pending_cb != NULL);
1270
1272 {
1275 "PgStat Pending",
1277 }
1278
1279 entry_ref = pgstat_get_entry_ref(kind, dboid, objid,
1280 true, created_entry);
1281
1282 if (entry_ref->pending == NULL)
1283 {
1284 size_t entrysize = pgstat_get_kind_info(kind)->pending_size;
1285
1286 Assert(entrysize != (size_t) -1);
1287
1288 entry_ref->pending = MemoryContextAllocZero(pgStatPendingContext, entrysize);
1290 }
1291
1292 return entry_ref;
#define unlikely(x)
Definition: c.h:403
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:364
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1263
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:170
static MemoryContext pgStatPendingContext
Definition: pgstat.c:234

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, Assert(), dlist_push_tail(), MemoryContextAllocZero(), PgStat_EntryRef::pending, PgStat_EntryRef::pending_node, PgStat_KindInfo::pending_size, pgstat_get_entry_ref(), pgstat_get_kind_info(), pgStatPending, pgStatPendingContext, TopMemoryContext, and unlikely.

Referenced by pgstat_create_inj(), pgstat_init_function_usage(), pgstat_prep_database_pending(), pgstat_prep_relation_pending(), pgstat_report_inj(), pgstat_report_subscription_conflict(), and pgstat_report_subscription_error().

◆ pgstat_prep_snapshot()

◆ pgstat_read_statsfile()

static void pgstat_read_statsfile ( void  )
static

Definition at line 1754 of file pgstat.c.

1756{
1757 FILE *fpin;
1758 int32 format_id;
1759 bool found;
1760 const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
1762
1763 /* shouldn't be called from postmaster */
1765
1766 elog(DEBUG2, "reading stats file \"%s\"", statfile);
1767
1768 /*
1769 * Try to open the stats file. If it doesn't exist, the backends simply
1770 * returns zero for anything and statistics simply starts from scratch
1771 * with empty counters.
1772 *
1773 * ENOENT is a possibility if stats collection was previously disabled or
1774 * has not yet written the stats file for the first time. Any other
1775 * failure condition is suspicious.
1776 */
1777 if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
1778 {
1779 if (errno != ENOENT)
1780 ereport(LOG,
1782 errmsg("could not open statistics file \"%s\": %m",
1783 statfile)));
1785 return;
1786 }
1787
1788 /*
1789 * Verify it's of the expected format.
1790 */
1791 if (!read_chunk_s(fpin, &format_id))
1792 {
1793 elog(WARNING, "could not read format ID");
1794 goto error;
1795 }
1796
1797 if (format_id != PGSTAT_FILE_FORMAT_ID)
1798 {
1799 elog(WARNING, "found incorrect format ID %d (expected %d)",
1800 format_id, PGSTAT_FILE_FORMAT_ID);
1801 goto error;
1802 }
1803
1804 /*
1805 * We found an existing statistics file. Read it and put all the stats
1806 * data into place.
1807 */
1808 for (;;)
1809 {
1810 int t = fgetc(fpin);
1811
1812 switch (t)
1813 {
1815 {
1816 PgStat_Kind kind;
1817 const PgStat_KindInfo *info;
1818 char *ptr;
1819
1820 /* entry for fixed-numbered stats */
1821 if (!read_chunk_s(fpin, &kind))
1822 {
1823 elog(WARNING, "could not read stats kind for entry of type %c", t);
1824 goto error;
1825 }
1826
1827 if (!pgstat_is_kind_valid(kind))
1828 {
1829 elog(WARNING, "invalid stats kind %u for entry of type %c",
1830 kind, t);
1831 goto error;
1832 }
1833
1834 info = pgstat_get_kind_info(kind);
1835 if (!info)
1836 {
1837 elog(WARNING, "could not find information of kind %u for entry of type %c",
1838 kind, t);
1839 goto error;
1840 }
1841
1842 if (!info->fixed_amount)
1843 {
1844 elog(WARNING, "invalid fixed_amount in stats kind %u for entry of type %c",
1845 kind, t);
1846 goto error;
1847 }
1848
1849 /* Load back stats into shared memory */
1850 if (pgstat_is_kind_builtin(kind))
1851 ptr = ((char *) shmem) + info->shared_ctl_off +
1852 info->shared_data_off;
1853 else
1854 {
1855 int idx = kind - PGSTAT_KIND_CUSTOM_MIN;
1856
1857 ptr = ((char *) shmem->custom_data[idx]) +
1858 info->shared_data_off;
1859 }
1860
1861 if (!read_chunk(fpin, ptr, info->shared_data_len))
1862 {
1863 elog(WARNING, "could not read data of stats kind %u for entry of type %c with size %u",
1864 kind, t, info->shared_data_len);
1865 goto error;
1866 }
1867
1868 break;
1869 }
1872 {
1875 PgStatShared_Common *header;
1876
1878
1879 if (t == PGSTAT_FILE_ENTRY_HASH)
1880 {
1881 /* normal stats entry, identified by PgStat_HashKey */
1882 if (!read_chunk_s(fpin, &key))
1883 {
1884 elog(WARNING, "could not read key for entry of type %c", t);
1885 goto error;
1886 }
1887
1888 if (!pgstat_is_kind_valid(key.kind))
1889 {
1890 elog(WARNING, "invalid stats kind for entry %u/%u/%" PRIu64 " of type %c",
1891 key.kind, key.dboid,
1892 key.objid, t);
1893 goto error;
1894 }
1895
1896 if (!pgstat_get_kind_info(key.kind))
1897 {
1898 elog(WARNING, "could not find information of kind for entry %u/%u/%" PRIu64 " of type %c",
1899 key.kind, key.dboid,
1900 key.objid, t);
1901 goto error;
1902 }
1903 }
1904 else
1905 {
1906 /* stats entry identified by name on disk (e.g. slots) */
1907 const PgStat_KindInfo *kind_info = NULL;
1908 PgStat_Kind kind;
1909 NameData name;
1910
1911 if (!read_chunk_s(fpin, &kind))
1912 {
1913 elog(WARNING, "could not read stats kind for entry of type %c", t);
1914 goto error;
1915 }
1916 if (!read_chunk_s(fpin, &name))
1917 {
1918 elog(WARNING, "could not read name of stats kind %u for entry of type %c",
1919 kind, t);
1920 goto error;
1921 }
1922 if (!pgstat_is_kind_valid(kind))
1923 {
1924 elog(WARNING, "invalid stats kind %u for entry of type %c",
1925 kind, t);
1926 goto error;
1927 }
1928
1929 kind_info = pgstat_get_kind_info(kind);
1930 if (!kind_info)
1931 {
1932 elog(WARNING, "could not find information of kind %u for entry of type %c",
1933 kind, t);
1934 goto error;
1935 }
1936
1937 if (!kind_info->from_serialized_name)
1938 {
1939 elog(WARNING, "invalid from_serialized_name in stats kind %u for entry of type %c",
1940 kind, t);
1941 goto error;
1942 }
1943
1944 if (!kind_info->from_serialized_name(&name, &key))
1945 {
1946 /* skip over data for entry we don't care about */
1947 if (fseek(fpin, pgstat_get_entry_len(kind), SEEK_CUR) != 0)
1948 {
1949 elog(WARNING, "could not seek \"%s\" of stats kind %u for entry of type %c",
1950 NameStr(name), kind, t);
1951 goto error;
1952 }
1953
1954 continue;
1955 }
1956
1957 Assert(key.kind == kind);
1958 }
1959
1960 /*
1961 * This intentionally doesn't use pgstat_get_entry_ref() -
1962 * putting all stats into checkpointer's
1963 * pgStatEntryRefHash would be wasted effort and memory.
1964 */
1966
1967 /* don't allow duplicate entries */
1968 if (found)
1969 {
1971 elog(WARNING, "found duplicate stats entry %u/%u/%" PRIu64 " of type %c",
1972 key.kind, key.dboid,
1973 key.objid, t);
1974 goto error;
1975 }
1976
1977 header = pgstat_init_entry(key.kind, p);
1979 if (header == NULL)
1980 {
1981 /*
1982 * It would be tempting to switch this ERROR to a
1983 * WARNING, but it would mean that all the statistics
1984 * are discarded when the environment fails on OOM.
1985 */
1986 elog(ERROR, "could not allocate entry %u/%u/%" PRIu64 " of type %c",
1987 key.kind, key.dboid,
1988 key.objid, t);
1989 }
1990
1991 if (!read_chunk(fpin,
1992 pgstat_get_entry_data(key.kind, header),
1994 {
1995 elog(WARNING, "could not read data for entry %u/%u/%" PRIu64 " of type %c",
1996 key.kind, key.dboid,
1997 key.objid, t);
1998 goto error;
1999 }
2000
2001 break;
2002 }
2004
2005 /*
2006 * check that PGSTAT_FILE_ENTRY_END actually signals end of
2007 * file
2008 */
2009 if (fgetc(fpin) != EOF)
2010 {
2011 elog(WARNING, "could not read end-of-file");
2012 goto error;
2013 }
2014
2015 goto done;
2016
2017 default:
2018 elog(WARNING, "could not read entry of type %c", t);
2019 goto error;
2020 }
2021 }
2022
2023done:
2024 FreeFile(fpin);
2025
2026 elog(DEBUG2, "removing permanent stats file \"%s\"", statfile);
2027 unlink(statfile);
2028
2029 return;
2030
2031error:
2032 ereport(LOG,
2033 (errmsg("corrupted statistics file \"%s\"", statfile)));
2034
2036
2037 goto done;
#define NameStr(name)
Definition: c.h:752
#define PG_BINARY_R
Definition: c.h:1275
int32_t int32
Definition: c.h:535
void dshash_release_lock(dshash_table *hash_table, void *entry)
Definition: dshash.c:558
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
Definition: dshash.c:433
#define WARNING
Definition: elog.h:36
int FreeFile(FILE *file)
Definition: fd.c:2840
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2641
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define PGSTAT_FILE_ENTRY_END
Definition: pgstat.c:143
static bool pgstat_is_kind_valid(PgStat_Kind kind)
Definition: pgstat.c:1427
#define PGSTAT_FILE_ENTRY_HASH
Definition: pgstat.c:146
#define PGSTAT_FILE_ENTRY_FIXED
Definition: pgstat.c:144
static bool read_chunk(FILE *fpin, void *ptr, size_t len)
Definition: pgstat.c:1740
#define read_chunk_s(fpin, ptr)
Definition: pgstat.c:1745
#define PGSTAT_FILE_ENTRY_NAME
Definition: pgstat.c:145
#define PGSTAT_FILE_FORMAT_ID
Definition: pgstat.h:215
PgStatShared_Common * pgstat_init_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
Definition: pgstat_shmem.c:308
static void error(void)
Definition: sql-dyntest.c:147
bool(* from_serialized_name)(const NameData *name, PgStat_HashKey *key)
void * custom_data[PGSTAT_KIND_CUSTOM_SIZE]
Definition: c.h:747

References AllocateFile(), Assert(), CHECK_FOR_INTERRUPTS, PgStat_ShmemControl::custom_data, DEBUG2, dshash_find_or_insert(), dshash_release_lock(), elog, ereport, errcode_for_file_access(), errmsg(), ERROR, error(), PgStat_KindInfo::fixed_amount, FreeFile(), PgStat_KindInfo::from_serialized_name, idx(), IsPostmasterEnvironment, IsUnderPostmaster, sort-test::key, LOG, name, NameStr, PG_BINARY_R, PGSTAT_FILE_ENTRY_END, PGSTAT_FILE_ENTRY_FIXED, PGSTAT_FILE_ENTRY_HASH, PGSTAT_FILE_ENTRY_NAME, PGSTAT_FILE_FORMAT_ID, pgstat_get_entry_data(), pgstat_get_entry_len(), pgstat_get_kind_info(), pgstat_init_entry(), pgstat_is_kind_builtin(), pgstat_is_kind_valid(), PgStat_Kind, PGSTAT_KIND_CUSTOM_MIN, pgstat_reset_after_failure(), PGSTAT_STAT_PERMANENT_FILENAME, pgStatLocal, read_chunk(), read_chunk_s, PgStat_KindInfo::shared_ctl_off, PgStat_KindInfo::shared_data_len, PgStat_KindInfo::shared_data_off, PgStat_LocalState::shared_hash, PgStat_LocalState::shmem, and WARNING.

Referenced by pgstat_restore_stats().

◆ pgstat_register_kind()

void pgstat_register_kind ( PgStat_Kind  kind,
const PgStat_KindInfo kind_info 
)

Definition at line 1461 of file pgstat.c.

1463{
1465
1466 if (kind_info->name == NULL || strlen(kind_info->name) == 0)
1467 ereport(ERROR,
1468 (errmsg("custom cumulative statistics name is invalid"),
1469 errhint("Provide a non-empty name for the custom cumulative statistics.")));
1470
1471 if (!pgstat_is_kind_custom(kind))
1472 ereport(ERROR, (errmsg("custom cumulative statistics ID %u is out of range", kind),
1473 errhint("Provide a custom cumulative statistics ID between %u and %u.",
1475
1477 ereport(ERROR,
1478 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1479 errdetail("Custom cumulative statistics must be registered while initializing modules in \"shared_preload_libraries\".")));
1480
1481 /*
1482 * Check some data for fixed-numbered stats.
1483 */
1484 if (kind_info->fixed_amount)
1485 {
1486 if (kind_info->shared_size == 0)
1487 ereport(ERROR,
1488 (errmsg("custom cumulative statistics property is invalid"),
1489 errhint("Custom cumulative statistics require a shared memory size for fixed-numbered objects.")));
1490 if (kind_info->track_entry_count)
1491 ereport(ERROR,
1492 (errmsg("custom cumulative statistics property is invalid"),
1493 errhint("Custom cumulative statistics cannot use entry count tracking for fixed-numbered objects.")));
1494 }
1495
1496 /*
1497 * If pgstat_kind_custom_infos is not available yet, allocate it.
1498 */
1499 if (pgstat_kind_custom_infos == NULL)
1500 {
1504 }
1505
1506 if (pgstat_kind_custom_infos[idx] != NULL &&
1508 ereport(ERROR,
1509 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1510 errdetail("Custom cumulative statistics \"%s\" already registered with the same ID.",
1512
1513 /* check for existing custom stats with the same name */
1514 for (PgStat_Kind existing_kind = PGSTAT_KIND_CUSTOM_MIN; existing_kind <= PGSTAT_KIND_CUSTOM_MAX; existing_kind++)
1515 {
1516 uint32 existing_idx = existing_kind - PGSTAT_KIND_CUSTOM_MIN;
1517
1518 if (pgstat_kind_custom_infos[existing_idx] == NULL)
1519 continue;
1520 if (!pg_strcasecmp(pgstat_kind_custom_infos[existing_idx]->name, kind_info->name))
1521 ereport(ERROR,
1522 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1523 errdetail("Existing cumulative statistics with ID %u has the same name.", existing_kind)));
1524 }
1525
1526 /* Register it */
1527 pgstat_kind_custom_infos[idx] = kind_info;
1528 ereport(LOG,
1529 (errmsg("registered custom cumulative statistics \"%s\" with ID %u",
1530 kind_info->name, kind)));
int errdetail(const char *fmt,...)
Definition: elog.c:1207
int errhint(const char *fmt,...)
Definition: elog.c:1321
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1786
#define PGSTAT_KIND_CUSTOM_SIZE
Definition: pgstat_kind.h:51
const char *const name

References ereport, errdetail(), errhint(), errmsg(), ERROR, PgStat_KindInfo::fixed_amount, idx(), LOG, MemoryContextAllocZero(), name, PgStat_KindInfo::name, pg_strcasecmp(), pgstat_is_kind_custom(), PgStat_Kind, pgstat_kind_custom_infos, PGSTAT_KIND_CUSTOM_MAX, PGSTAT_KIND_CUSTOM_MIN, PGSTAT_KIND_CUSTOM_SIZE, process_shared_preload_libraries_in_progress, PgStat_KindInfo::shared_size, TopMemoryContext, and PgStat_KindInfo::track_entry_count.

Referenced by pgstat_register_inj(), and pgstat_register_inj_fixed().

◆ pgstat_report_stat()

long pgstat_report_stat ( bool  force)

Definition at line 692 of file pgstat.c.

694{
695 static TimestampTz pending_since = 0;
696 static TimestampTz last_flush = 0;
697 bool partial_flush;
699 bool nowait;
700
703
704 /* "absorb" the forced flush even if there's nothing to flush */
706 {
707 force = true;
708 pgStatForceNextFlush = false;
709 }
710
711 /* Don't expend a clock check if nothing to do */
714 {
715 return 0;
716 }
717
718 /*
719 * There should never be stats to report once stats are shut down. Can't
720 * assert that before the checks above, as there is an unconditional
721 * pgstat_report_stat() call in pgstat_shutdown_hook() - which at least
722 * the process that ran pgstat_before_server_shutdown() will still call.
723 */
725
726 if (force)
727 {
728 /*
729 * Stats reports are forced either when it's been too long since stats
730 * have been reported or in processes that force stats reporting to
731 * happen at specific points (including shutdown). In the former case
732 * the transaction stop time might be quite old, in the latter it
733 * would never get cleared.
734 */
736 }
737 else
738 {
740
741 if (pending_since > 0 &&
743 {
744 /* don't keep pending updates longer than PGSTAT_MAX_INTERVAL */
745 force = true;
746 }
747 else if (last_flush > 0 &&
749 {
750 /* don't flush too frequently */
751 if (pending_since == 0)
752 pending_since = now;
753
755 }
756 }
757
759
760 /* don't wait for lock acquisition when !force */
761 nowait = !force;
762
763 partial_flush = false;
764
765 /* flush of variable-numbered stats tracked in pending entries list */
766 partial_flush |= pgstat_flush_pending_entries(nowait);
767
768 /* flush of other stats kinds */
770 {
771 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
772 {
773 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
774
775 if (!kind_info)
776 continue;
777 if (!kind_info->flush_static_cb)
778 continue;
779
780 partial_flush |= kind_info->flush_static_cb(nowait);
781 }
782 }
783
784 last_flush = now;
785
786 /*
787 * If some of the pending stats could not be flushed due to lock
788 * contention, let the caller know when to retry.
789 */
790 if (partial_flush)
791 {
792 /* force should have prevented us from getting here */
793 Assert(!force);
794
795 /* remember since when stats have been pending */
796 if (pending_since == 0)
797 pending_since = now;
798
800 }
801
802 pending_since = 0;
803 pgstat_report_fixed = false;
804
805 return 0;
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1781
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1609
int64 TimestampTz
Definition: timestamp.h:39
#define PGSTAT_MIN_INTERVAL
Definition: pgstat.c:126
static bool pgstat_flush_pending_entries(bool nowait)
Definition: pgstat.c:1337
bool pgstat_report_fixed
Definition: pgstat.c:218
#define PGSTAT_MAX_INTERVAL
Definition: pgstat.c:128
#define PGSTAT_IDLE_INTERVAL
Definition: pgstat.c:130
void pgstat_update_dbstats(TimestampTz ts)
bool(* flush_static_cb)(bool nowait)
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:5001
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition: xact.c:891

References Assert(), dlist_is_empty(), PgStat_KindInfo::flush_static_cb, GetCurrentTimestamp(), GetCurrentTransactionStopTimestamp(), PgStat_ShmemControl::is_shutdown, IsTransactionOrTransactionBlock(), now(), pgstat_assert_is_up, pgstat_flush_pending_entries(), pgstat_get_kind_info(), PGSTAT_IDLE_INTERVAL, PgStat_Kind, PGSTAT_KIND_MAX, PGSTAT_KIND_MIN, PGSTAT_MAX_INTERVAL, PGSTAT_MIN_INTERVAL, pgstat_report_fixed, pgstat_update_dbstats(), pgStatForceNextFlush, pgStatLocal, pgStatPending, PgStat_LocalState::shmem, and TimestampDifferenceExceeds().

Referenced by AllTablesyncsReady(), apply_handle_commit_internal(), apply_handle_commit_prepared(), apply_handle_prepare(), apply_handle_rollback_prepared(), apply_handle_stream_prepare(), finish_sync_worker(), HasSubscriptionRelationsCached(), LogicalRepApplyLoop(), LogicalRepSyncTableStart(), pgstat_before_server_shutdown(), pgstat_shutdown_hook(), PostgresMain(), process_syncing_tables_for_apply(), process_syncing_tables_for_sync(), ProcessInterrupts(), standby_redo(), and worker_spi_main().

◆ pgstat_reset()

void pgstat_reset ( PgStat_Kind  kind,
Oid  dboid,
uint64  objid 
)

Definition at line 852 of file pgstat.c.

854{
855 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
857
858 /* not needed atm, and doesn't make sense with the current signature */
859 Assert(!pgstat_get_kind_info(kind)->fixed_amount);
860
861 /* reset the "single counter" */
862 pgstat_reset_entry(kind, dboid, objid, ts);
863
864 if (!kind_info->accessed_across_databases)
void pgstat_reset_database_timestamp(Oid dboid, TimestampTz ts)
void pgstat_reset_entry(PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts)

References PgStat_KindInfo::accessed_across_databases, Assert(), GetCurrentTimestamp(), pgstat_get_kind_info(), pgstat_reset_database_timestamp(), and pgstat_reset_entry().

Referenced by pg_stat_reset_backend_stats(), pg_stat_reset_single_function_counters(), pg_stat_reset_single_table_counters(), pg_stat_reset_subscription_stats(), pgstat_create_transactional(), and pgstat_reset_replslot().

◆ pgstat_reset_after_failure()

static void pgstat_reset_after_failure ( void  )
static

Definition at line 2044 of file pgstat.c.

2046{
2048
2049 /* reset fixed-numbered stats */
2050 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
2051 {
2052 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
2053
2054 if (!kind_info || !kind_info->fixed_amount)
2055 continue;
2056
2057 kind_info->reset_all_cb(ts);
2058 }
2059
2060 /* and drop variable-numbered ones */
void pgstat_drop_all_entries(void)
void(* reset_all_cb)(TimestampTz ts)

References PgStat_KindInfo::fixed_amount, GetCurrentTimestamp(), pgstat_drop_all_entries(), pgstat_get_kind_info(), PgStat_Kind, PGSTAT_KIND_MAX, PGSTAT_KIND_MIN, and PgStat_KindInfo::reset_all_cb.

Referenced by pgstat_discard_stats(), and pgstat_read_statsfile().

◆ pgstat_reset_counters()

void pgstat_reset_counters ( void  )

Definition at line 833 of file pgstat.c.

835{
837
840 ts);
static bool match_db_entries(PgStatShared_HashEntry *entry, Datum match_data)
Definition: pgstat.c:821
void pgstat_reset_matching_entries(bool(*do_reset)(PgStatShared_HashEntry *, Datum), Datum match_data, TimestampTz ts)
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262

References GetCurrentTimestamp(), match_db_entries(), MyDatabaseId, ObjectIdGetDatum(), and pgstat_reset_matching_entries().

Referenced by pg_stat_reset().

◆ pgstat_reset_of_kind()

void pgstat_reset_of_kind ( PgStat_Kind  kind)

Definition at line 874 of file pgstat.c.

876{
877 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
879
880 if (kind_info->fixed_amount)
881 kind_info->reset_all_cb(ts);
882 else
void pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts)

References PgStat_KindInfo::fixed_amount, GetCurrentTimestamp(), pgstat_get_kind_info(), pgstat_reset_entries_of_kind(), and PgStat_KindInfo::reset_all_cb.

Referenced by pg_stat_reset_replication_slot(), pg_stat_reset_shared(), pg_stat_reset_slru(), and pg_stat_reset_subscription_stats().

◆ pgstat_restore_stats()

void pgstat_restore_stats ( void  )

Definition at line 505 of file pgstat.c.

507{
static void pgstat_read_statsfile(void)
Definition: pgstat.c:1754

References pgstat_read_statsfile().

Referenced by StartupXLOG().

◆ pgstat_shutdown_hook()

static void pgstat_shutdown_hook ( int  code,
Datum  arg 
)
static

Definition at line 601 of file pgstat.c.

603{
604 Assert(!pgstat_is_shutdown);
606
607 /*
608 * If we got as far as discovering our own database ID, we can flush out
609 * what we did so far. Otherwise, we'd be reporting an invalid database
610 * ID, so forget it. (This means that accesses to pg_database during
611 * failed backend starts might never get counted.)
612 */
615
616 pgstat_report_stat(true);
617
618 /* there shouldn't be any pending changes left */
621
622 /* drop the backend stats entry */
625
627
628#ifdef USE_ASSERT_CHECKING
629 pgstat_is_shutdown = true;
630#endif
#define OidIsValid(objectId)
Definition: c.h:775
ProcNumber MyProcNumber
Definition: globals.c:90
static void dlist_init(dlist_head *head)
Definition: ilist.h:314
void pgstat_report_disconnect(Oid dboid)
#define PGSTAT_KIND_BACKEND
Definition: pgstat_kind.h:32
void pgstat_request_entry_refs_gc(void)
Definition: pgstat_shmem.c:745
bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
void pgstat_detach_shmem(void)
Definition: pgstat_shmem.c:272

References Assert(), dlist_init(), dlist_is_empty(), InvalidOid, IsPostmasterEnvironment, IsUnderPostmaster, MyDatabaseId, MyProcNumber, OidIsValid, pgstat_detach_shmem(), pgstat_drop_entry(), PGSTAT_KIND_BACKEND, pgstat_report_disconnect(), pgstat_report_stat(), pgstat_request_entry_refs_gc(), and pgStatPending.

Referenced by pgstat_initialize().

◆ pgstat_snapshot_fixed()

◆ pgstat_write_statsfile()

static void pgstat_write_statsfile ( void  )
static

Definition at line 1570 of file pgstat.c.

1572{
1573 FILE *fpout;
1574 int32 format_id;
1575 const char *tmpfile = PGSTAT_STAT_PERMANENT_TMPFILE;
1576 const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
1577 dshash_seq_status hstat;
1579
1581
1582 /* should be called only by the checkpointer or single user mode */
1584
1585 /* we're shutting down, so it's ok to just override this */
1587
1588 elog(DEBUG2, "writing stats file \"%s\"", statfile);
1589
1590 /*
1591 * Open the statistics temp file to write out the current values.
1592 */
1593 fpout = AllocateFile(tmpfile, PG_BINARY_W);
1594 if (fpout == NULL)
1595 {
1596 ereport(LOG,
1598 errmsg("could not open temporary statistics file \"%s\": %m",
1599 tmpfile)));
1600 return;
1601 }
1602
1603 /*
1604 * Write the file header --- currently just a format ID.
1605 */
1606 format_id = PGSTAT_FILE_FORMAT_ID;
1607 write_chunk_s(fpout, &format_id);
1608
1609 /* Write various stats structs for fixed number of objects */
1610 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
1611 {
1612 char *ptr;
1613 const PgStat_KindInfo *info = pgstat_get_kind_info(kind);
1614
1615 if (!info || !info->fixed_amount)
1616 continue;
1617
1618 if (pgstat_is_kind_builtin(kind))
1619 Assert(info->snapshot_ctl_off != 0);
1620
1621 /* skip if no need to write to file */
1622 if (!info->write_to_file)
1623 continue;
1624
1626 if (pgstat_is_kind_builtin(kind))
1627 ptr = ((char *) &pgStatLocal.snapshot) + info->snapshot_ctl_off;
1628 else
1630
1631 fputc(PGSTAT_FILE_ENTRY_FIXED, fpout);
1632 write_chunk_s(fpout, &kind);
1633 write_chunk(fpout, ptr, info->shared_data_len);
1634 }
1635
1636 /*
1637 * Walk through the stats entries
1638 */
1639 dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
1640 while ((ps = dshash_seq_next(&hstat)) != NULL)
1641 {
1642 PgStatShared_Common *shstats;
1643 const PgStat_KindInfo *kind_info = NULL;
1644
1646
1647 /*
1648 * We should not see any "dropped" entries when writing the stats
1649 * file, as all backends and auxiliary processes should have cleaned
1650 * up their references before they terminated.
1651 *
1652 * However, since we are already shutting down, it is not worth
1653 * crashing the server over any potential cleanup issues, so we simply
1654 * skip such entries if encountered.
1655 */
1656 Assert(!ps->dropped);
1657 if (ps->dropped)
1658 continue;
1659
1660 /*
1661 * This discards data related to custom stats kinds that are unknown
1662 * to this process.
1663 */
1664 if (!pgstat_is_kind_valid(ps->key.kind))
1665 {
1666 elog(WARNING, "found unknown stats entry %u/%u/%" PRIu64,
1667 ps->key.kind, ps->key.dboid,
1668 ps->key.objid);
1669 continue;
1670 }
1671
1673
1674 kind_info = pgstat_get_kind_info(ps->key.kind);
1675
1676 /* if not dropped the valid-entry refcount should exist */
1677 Assert(pg_atomic_read_u32(&ps->refcount) > 0);
1678
1679 /* skip if no need to write to file */
1680 if (!kind_info->write_to_file)
1681 continue;
1682
1683 if (!kind_info->to_serialized_name)
1684 {
1685 /* normal stats entry, identified by PgStat_HashKey */
1686 fputc(PGSTAT_FILE_ENTRY_HASH, fpout);
1687 write_chunk_s(fpout, &ps->key);
1688 }
1689 else
1690 {
1691 /* stats entry identified by name on disk (e.g. slots) */
1692 NameData name;
1693
1694 kind_info->to_serialized_name(&ps->key, shstats, &name);
1695
1696 fputc(PGSTAT_FILE_ENTRY_NAME, fpout);
1697 write_chunk_s(fpout, &ps->key.kind);
1698 write_chunk_s(fpout, &name);
1699 }
1700
1701 /* Write except the header part of the entry */
1702 write_chunk(fpout,
1703 pgstat_get_entry_data(ps->key.kind, shstats),
1704 pgstat_get_entry_len(ps->key.kind));
1705 }
1706 dshash_seq_term(&hstat);
1707
1708 /*
1709 * No more output to be done. Close the temp file and replace the old
1710 * pgstat.stat with it. The ferror() check replaces testing for error
1711 * after each individual fputc or fwrite (in write_chunk()) above.
1712 */
1713 fputc(PGSTAT_FILE_ENTRY_END, fpout);
1714
1715 if (ferror(fpout))
1716 {
1717 ereport(LOG,
1719 errmsg("could not write temporary statistics file \"%s\": %m",
1720 tmpfile)));
1721 FreeFile(fpout);
1722 unlink(tmpfile);
1723 }
1724 else if (FreeFile(fpout) < 0)
1725 {
1726 ereport(LOG,
1728 errmsg("could not close temporary statistics file \"%s\": %m",
1729 tmpfile)));
1730 unlink(tmpfile);
1731 }
1732 else if (durable_rename(tmpfile, statfile, LOG) < 0)
1733 {
1734 /* durable_rename already emitted log message */
1735 unlink(tmpfile);
1736 }
#define PG_BINARY_W
Definition: c.h:1276
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:779
struct parser_state ps
@ B_CHECKPOINTER
Definition: miscadmin.h:362
BackendType MyBackendType
Definition: miscinit.c:64
static void write_chunk(FILE *fpout, void *ptr, size_t len)
Definition: pgstat.c:1553
#define write_chunk_s(fpout, ptr)
Definition: pgstat.c:1563
#define PGSTAT_STAT_PERMANENT_TMPFILE
Definition: pgstat.h:32
void(* to_serialized_name)(const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name)

References AllocateFile(), Assert(), B_CHECKPOINTER, CHECK_FOR_INTERRUPTS, PgStat_Snapshot::custom_data, DEBUG2, PgStat_LocalState::dsa, dsa_get_address(), dshash_seq_init(), dshash_seq_next(), dshash_seq_term(), durable_rename(), elog, ereport, errcode_for_file_access(), errmsg(), PgStat_KindInfo::fixed_amount, FreeFile(), IsUnderPostmaster, LOG, MyBackendType, name, pg_atomic_read_u32(), PG_BINARY_W, pgstat_assert_is_up, pgstat_build_snapshot_fixed(), pgstat_fetch_consistency, PGSTAT_FETCH_CONSISTENCY_NONE, PGSTAT_FILE_ENTRY_END, PGSTAT_FILE_ENTRY_FIXED, PGSTAT_FILE_ENTRY_HASH, PGSTAT_FILE_ENTRY_NAME, PGSTAT_FILE_FORMAT_ID, pgstat_get_entry_data(), pgstat_get_entry_len(), pgstat_get_kind_info(), pgstat_is_kind_builtin(), pgstat_is_kind_valid(), PgStat_Kind, PGSTAT_KIND_CUSTOM_MIN, PGSTAT_KIND_MAX, PGSTAT_KIND_MIN, PGSTAT_STAT_PERMANENT_FILENAME, PGSTAT_STAT_PERMANENT_TMPFILE, pgStatLocal, ps, PgStat_KindInfo::shared_data_len, PgStat_LocalState::shared_hash, PgStat_LocalState::snapshot, PgStat_KindInfo::snapshot_ctl_off, PgStat_KindInfo::to_serialized_name, WARNING, write_chunk(), write_chunk_s, and PgStat_KindInfo::write_to_file.

Referenced by pgstat_before_server_shutdown().

◆ read_chunk()

static bool read_chunk ( FILE *  fpin,
void *  ptr,
size_t  len 
)
static

Definition at line 1740 of file pgstat.c.

1742{
1743 return fread(ptr, 1, len, fpin) == len;
const void size_t len

References len.

Referenced by pgstat_read_statsfile().

◆ write_chunk()

static void write_chunk ( FILE *  fpout,
void *  ptr,
size_t  len 
)
static

Definition at line 1553 of file pgstat.c.

1555{
1556 int rc;
1557
1558 rc = fwrite(ptr, len, 1, fpout);
1559
1560 /* we'll check for errors with ferror once at the end */
1561 (void) rc;

References len.

Referenced by pgstat_write_statsfile().

Variable Documentation

◆ force_stats_snapshot_clear

◆ pgstat_fetch_consistency

◆ pgstat_kind_builtin_infos

const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
static

Definition at line 281 of file pgstat.c.

Referenced by pgstat_get_kind_from_str(), and pgstat_get_kind_info().

◆ pgstat_kind_custom_infos

const PgStat_KindInfo** pgstat_kind_custom_infos = NULL
static

Definition at line 492 of file pgstat.c.

Referenced by pgstat_get_kind_from_str(), pgstat_get_kind_info(), and pgstat_register_kind().

◆ pgstat_report_fixed

◆ pgstat_track_counts

◆ pgStatForceNextFlush

bool pgStatForceNextFlush = false
static

Definition at line 249 of file pgstat.c.

Referenced by pgstat_force_next_flush(), and pgstat_report_stat().

◆ pgStatLocal

PgStat_LocalState pgStatLocal

Definition at line 212 of file pgstat.c.

Referenced by pgstat_acquire_entry_ref(), pgstat_archiver_reset_all_cb(), pgstat_archiver_snapshot_cb(), pgstat_attach_shmem(), pgstat_before_server_shutdown(), pgstat_bgwriter_reset_all_cb(), pgstat_bgwriter_snapshot_cb(), pgstat_build_snapshot(), pgstat_build_snapshot_fixed(), pgstat_checkpointer_reset_all_cb(), pgstat_checkpointer_snapshot_cb(), pgstat_clear_snapshot(), pgstat_detach_shmem(), pgstat_drop_database_and_contents(), pgstat_drop_entry(), pgstat_drop_entry_internal(), pgstat_drop_matching_entries(), pgstat_fetch_entry(), pgstat_fetch_slru(), pgstat_fetch_stat_archiver(), pgstat_fetch_stat_bgwriter(), pgstat_fetch_stat_checkpointer(), pgstat_fetch_stat_io(), pgstat_fetch_stat_wal(), pgstat_free_entry(), pgstat_gc_entry_refs(), pgstat_get_custom_shmem_data(), pgstat_get_custom_snapshot_data(), pgstat_get_entry_count(), pgstat_get_entry_ref(), pgstat_get_stat_snapshot_timestamp(), pgstat_init_entry(), pgstat_init_snapshot_fixed(), pgstat_io_flush_cb(), pgstat_io_reset_all_cb(), pgstat_io_snapshot_cb(), pgstat_need_entry_refs_gc(), pgstat_prep_snapshot(), pgstat_read_statsfile(), pgstat_reinit_entry(), pgstat_release_entry_ref(), pgstat_report_archiver(), pgstat_report_bgwriter(), pgstat_report_checkpointer(), pgstat_report_stat(), pgstat_request_entry_refs_gc(), pgstat_reset_matching_entries(), pgstat_reset_slru_counter_internal(), pgstat_setup_shared_refs(), pgstat_slru_flush_cb(), pgstat_slru_snapshot_cb(), pgstat_snapshot_fixed(), pgstat_wal_flush_cb(), pgstat_wal_reset_all_cb(), pgstat_wal_snapshot_cb(), pgstat_write_statsfile(), and StatsShmemInit().

◆ pgStatPending

dlist_head pgStatPending = DLIST_STATIC_INIT(pgStatPending)
static

◆ pgStatPendingContext

MemoryContext pgStatPendingContext = NULL
static

Definition at line 234 of file pgstat.c.

Referenced by pgstat_prep_pending_entry().