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 "port/atomics.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 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_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 (int ikind)
 
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, Oid objoid)
 
void pgstat_reset_of_kind (PgStat_Kind kind)
 
void pgstat_clear_snapshot (void)
 
void * pgstat_fetch_entry (PgStat_Kind kind, Oid dboid, Oid objoid)
 
TimestampTz pgstat_get_stat_snapshot_timestamp (bool *have_snapshot)
 
bool pgstat_have_entry (PgStat_Kind kind, Oid dboid, Oid objoid)
 
void pgstat_snapshot_fixed (PgStat_Kind kind)
 
PgStat_EntryRefpgstat_prep_pending_entry (PgStat_Kind kind, Oid dboid, Oid objoid, bool *created_entry)
 
PgStat_EntryRefpgstat_fetch_pending_entry (PgStat_Kind kind, Oid dboid, Oid objoid)
 
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)
 
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
 
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_infos [PGSTAT_NUM_KINDS]
 

Macro Definition Documentation

◆ PGSTAT_IDLE_INTERVAL

#define PGSTAT_IDLE_INTERVAL   10000

Definition at line 121 of file pgstat.c.

◆ PGSTAT_MAX_INTERVAL

#define PGSTAT_MAX_INTERVAL   60000

Definition at line 119 of file pgstat.c.

◆ PGSTAT_MIN_INTERVAL

#define PGSTAT_MIN_INTERVAL   1000

Definition at line 117 of file pgstat.c.

◆ PGSTAT_SNAPSHOT_HASH_SIZE

#define PGSTAT_SNAPSHOT_HASH_SIZE   512

Definition at line 128 of file pgstat.c.

◆ read_chunk_s

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

Definition at line 1474 of file pgstat.c.

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 156 of file pgstat.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 155 of file pgstat.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   PgStat_SnapshotEntry

Definition at line 147 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 152 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 150 of file pgstat.c.

◆ SH_KEY

#define SH_KEY   key

Definition at line 149 of file pgstat.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   PgStat_HashKey

Definition at line 148 of file pgstat.c.

◆ SH_PREFIX

#define SH_PREFIX   pgstat_snapshot

Definition at line 146 of file pgstat.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 154 of file pgstat.c.

◆ write_chunk_s

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

Definition at line 1301 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 1707 of file pgstat.c.

1708 {
1709  /*
1710  * Changing this value in a transaction may cause snapshot state
1711  * inconsistencies, so force a clear of the current snapshot on the next
1712  * snapshot build attempt.
1713  */
1716 }
#define newval
int pgstat_fetch_consistency
Definition: pgstat.c:185
static bool force_stats_snapshot_clear
Definition: pgstat.c:231

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 703 of file pgstat.c.

704 {
705  return entry->key.dboid == DatumGetObjectId(MyDatabaseId);
706 }
Oid MyDatabaseId
Definition: globals.c:91
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:242

References DatumGetObjectId(), 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 462 of file pgstat.c.

463 {
464  Assert(pgStatLocal.shmem != NULL);
466 
467  /*
468  * Stats should only be reported after pgstat_initialize() and before
469  * pgstat_shutdown(). This is a convenient point to catch most violations
470  * of this rule.
471  */
472  Assert(pgstat_is_initialized && !pgstat_is_shutdown);
473 
474  /* flush out our own pending changes before writing out */
475  pgstat_report_stat(true);
476 
477  /*
478  * Only write out file during normal shutdown. Don't even signal that
479  * we've shutdown during irregular shutdowns, because the shutdown
480  * sequence isn't coordinated to ensure this backend shuts down last.
481  */
482  if (code == 0)
483  {
484  pgStatLocal.shmem->is_shutdown = true;
486  }
487 }
Assert(fmt[strlen(fmt) - 1] !='\n')
static void pgstat_write_statsfile(void)
Definition: pgstat.c:1308
long pgstat_report_stat(bool force)
Definition: pgstat.c:579
PgStat_LocalState pgStatLocal
Definition: pgstat.c:193
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 976 of file pgstat.c.

977 {
978  dshash_seq_status hstat;
980 
981  /* should only be called when we need a snapshot */
983 
984  /* snapshot already built */
986  return;
987 
989 
990  Assert(pgStatLocal.snapshot.stats->members == 0);
991 
993 
994  /*
995  * Snapshot all variable stats.
996  */
997  dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
998  while ((p = dshash_seq_next(&hstat)) != NULL)
999  {
1000  PgStat_Kind kind = p->key.kind;
1001  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1002  bool found;
1003  PgStat_SnapshotEntry *entry;
1004  PgStatShared_Common *stats_data;
1005 
1006  /*
1007  * Check if the stats object should be included in the snapshot.
1008  * Unless the stats kind can be accessed from all databases (e.g.,
1009  * database stats themselves), we only include stats for the current
1010  * database or objects not associated with a database (e.g. shared
1011  * relations).
1012  */
1013  if (p->key.dboid != MyDatabaseId &&
1014  p->key.dboid != InvalidOid &&
1015  !kind_info->accessed_across_databases)
1016  continue;
1017 
1018  if (p->dropped)
1019  continue;
1020 
1022 
1023  stats_data = dsa_get_address(pgStatLocal.dsa, p->body);
1024  Assert(stats_data);
1025 
1026  entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, p->key, &found);
1027  Assert(!found);
1028 
1030  kind_info->shared_size);
1031 
1032  /*
1033  * Acquire the LWLock directly instead of using
1034  * pg_stat_lock_entry_shared() which requires a reference.
1035  */
1036  LWLockAcquire(&stats_data->lock, LW_SHARED);
1037  memcpy(entry->data,
1038  pgstat_get_entry_data(kind, stats_data),
1039  kind_info->shared_size);
1040  LWLockRelease(&stats_data->lock);
1041  }
1042  dshash_seq_term(&hstat);
1043 
1044  /*
1045  * Build snapshot of all fixed-numbered stats.
1046  */
1047  for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1048  {
1049  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1050 
1051  if (!kind_info->fixed_amount)
1052  {
1053  Assert(kind_info->snapshot_cb == NULL);
1054  continue;
1055  }
1056 
1058  }
1059 
1061 }
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:234
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1654
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:955
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:1172
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1785
@ LW_SHARED
Definition: lwlock.h:117
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1168
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1263
static void pgstat_prep_snapshot(void)
Definition: pgstat.c:955
static void pgstat_build_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1064
PgStat_Kind
Definition: pgstat.h:36
@ PGSTAT_FETCH_CONSISTENCY_SNAPSHOT
Definition: pgstat.h:72
#define PGSTAT_KIND_LAST
Definition: pgstat.h:57
#define PGSTAT_KIND_FIRST_VALID
Definition: pgstat.h:56
static void * pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry)
#define InvalidOid
Definition: postgres_ext.h:36
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_kind_info(), PGSTAT_KIND_FIRST_VALID, PGSTAT_KIND_LAST, pgstat_prep_snapshot(), pgStatLocal, PgStatShared_HashEntry::refcount, PgStat_LocalState::shared_hash, PgStat_KindInfo::shared_size, 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 1064 of file pgstat.c.

1065 {
1066  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1067 
1068  Assert(kind_info->fixed_amount);
1069  Assert(kind_info->snapshot_cb != NULL);
1070 
1072  {
1073  /* rebuild every time */
1074  pgStatLocal.snapshot.fixed_valid[kind] = false;
1075  }
1076  else if (pgStatLocal.snapshot.fixed_valid[kind])
1077  {
1078  /* in snapshot mode we shouldn't get called again */
1080  return;
1081  }
1082 
1084 
1085  kind_info->snapshot_cb();
1086 
1088  pgStatLocal.snapshot.fixed_valid[kind] = true;
1089 }
@ PGSTAT_FETCH_CONSISTENCY_NONE
Definition: pgstat.h:70
@ PGSTAT_FETCH_CONSISTENCY_CACHE
Definition: pgstat.h:71
bool fixed_valid[PGSTAT_NUM_KINDS]

References Assert(), PgStat_KindInfo::fixed_amount, PgStat_Snapshot::fixed_valid, pgstat_fetch_consistency, PGSTAT_FETCH_CONSISTENCY_CACHE, PGSTAT_FETCH_CONSISTENCY_NONE, pgstat_get_kind_info(), 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 782 of file pgstat.c.

783 {
785 
786  memset(&pgStatLocal.snapshot.fixed_valid, 0,
788  pgStatLocal.snapshot.stats = NULL;
790 
791  /* Release memory, if any was allocated */
793  {
795 
796  /* Reset variables */
798  }
799 
800  /*
801  * Historically the backend_status.c facilities lived in this file, and
802  * were reset with the same function. For now keep it that way, and
803  * forward the reset request.
804  */
806 
807  /* Reset this flag, as it may be possible that a cleanup was forced. */
809 }
void pgstat_clear_backend_activity_snapshot(void)
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:442
#define pgstat_assert_is_up()

References PgStat_Snapshot::context, 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 1156 of file pgstat.c.

1157 {
1158  PgStat_Kind kind = entry_ref->shared_entry->key.kind;
1159  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1160  void *pending_data = entry_ref->pending;
1161 
1162  Assert(pending_data != NULL);
1163  /* !fixed_amount stats should be handled explicitly */
1164  Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1165 
1166  if (kind_info->delete_pending_cb)
1167  kind_info->delete_pending_cb(entry_ref);
1168 
1169  pfree(pending_data);
1170  entry_ref->pending = NULL;
1171 
1172  dlist_delete(&entry_ref->pending_node);
1173 }
static void dlist_delete(dlist_node *node)
Definition: ilist.h:405
void pfree(void *pointer)
Definition: mcxt.c:1508
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(), 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 419 of file pgstat.c.

420 {
421  int ret;
422 
423  /* NB: this needs to be done even in single user mode */
424 
425  ret = unlink(PGSTAT_STAT_PERMANENT_FILENAME);
426  if (ret != 0)
427  {
428  if (errno == ENOENT)
429  elog(DEBUG2,
430  "didn't need to unlink permanent stats file \"%s\" - didn't exist",
432  else
433  ereport(LOG,
435  errmsg("could not unlink permanent statistics file \"%s\": %m",
437  }
438  else
439  {
440  ereport(DEBUG2,
442  errmsg_internal("unlinked permanent statistics file \"%s\"",
444  }
445 
446  /*
447  * Reset stats contents. This will set reset timestamps of fixed-numbered
448  * stats to the current time (no variable stats exist).
449  */
451 }
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1159
int errcode_for_file_access(void)
Definition: elog.c:882
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define LOG
Definition: elog.h:31
#define DEBUG2
Definition: elog.h:29
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
static void pgstat_reset_after_failure(void)
Definition: pgstat.c:1684
#define PGSTAT_STAT_PERMANENT_FILENAME
Definition: pgstat.h:28

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,
Oid  objoid 
)

Definition at line 812 of file pgstat.c.

813 {
815  PgStat_EntryRef *entry_ref;
816  void *stats_data;
817  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
818 
819  /* should be called from backends */
821  Assert(!kind_info->fixed_amount);
822 
824 
825  key.kind = kind;
826  key.dboid = dboid;
827  key.objoid = objoid;
828 
829  /* if we need to build a full snapshot, do so */
832 
833  /* if caching is desired, look up in cache */
835  {
836  PgStat_SnapshotEntry *entry = NULL;
837 
838  entry = pgstat_snapshot_lookup(pgStatLocal.snapshot.stats, key);
839 
840  if (entry)
841  return entry->data;
842 
843  /*
844  * If we built a full snapshot and the key is not in
845  * pgStatLocal.snapshot.stats, there are no matching stats.
846  */
848  return NULL;
849  }
850 
852 
853  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
854 
855  if (entry_ref == NULL || entry_ref->shared_entry->dropped)
856  {
857  /* create empty entry when using PGSTAT_FETCH_CONSISTENCY_CACHE */
859  {
860  PgStat_SnapshotEntry *entry = NULL;
861  bool found;
862 
863  entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
864  Assert(!found);
865  entry->data = NULL;
866  }
867  return NULL;
868  }
869 
870  /*
871  * Allocate in caller's context for PGSTAT_FETCH_CONSISTENCY_NONE,
872  * otherwise we could quickly end up with a fair bit of memory used due to
873  * repeated accesses.
874  */
876  stats_data = palloc(kind_info->shared_data_len);
877  else
879  kind_info->shared_data_len);
880 
881  pgstat_lock_entry_shared(entry_ref, false);
882  memcpy(stats_data,
883  pgstat_get_entry_data(kind, entry_ref->shared_stats),
884  kind_info->shared_data_len);
885  pgstat_unlock_entry(entry_ref);
886 
888  {
889  PgStat_SnapshotEntry *entry = NULL;
890  bool found;
891 
892  entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
893  entry->data = stats_data;
894  }
895 
896  return stats_data;
897 }
bool IsUnderPostmaster
Definition: globals.c:117
bool IsPostmasterEnvironment
Definition: globals.c:116
void * palloc(Size size)
Definition: mcxt.c:1304
static void pgstat_build_snapshot(void)
Definition: pgstat.c:976
bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:592
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:398
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:604
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_dbentry(), pgstat_fetch_stat_funcentry(), 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,
Oid  objoid 
)

Definition at line 1143 of file pgstat.c.

1144 {
1145  PgStat_EntryRef *entry_ref;
1146 
1147  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
1148 
1149  if (entry_ref == NULL || entry_ref->pending == NULL)
1150  return NULL;
1151 
1152  return entry_ref;
1153 }

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 1180 of file pgstat.c.

1181 {
1182  bool have_pending = false;
1183  dlist_node *cur = NULL;
1184 
1185  /*
1186  * Need to be a bit careful iterating over the list of pending entries.
1187  * Processing a pending entry may queue further pending entries to the end
1188  * of the list that we want to process, so a simple iteration won't do.
1189  * Further complicating matters is that we want to delete the current
1190  * entry in each iteration from the list if we flushed successfully.
1191  *
1192  * So we just keep track of the next pointer in each loop iteration.
1193  */
1196 
1197  while (cur)
1198  {
1199  PgStat_EntryRef *entry_ref =
1200  dlist_container(PgStat_EntryRef, pending_node, cur);
1201  PgStat_HashKey key = entry_ref->shared_entry->key;
1202  PgStat_Kind kind = key.kind;
1203  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1204  bool did_flush;
1205  dlist_node *next;
1206 
1207  Assert(!kind_info->fixed_amount);
1208  Assert(kind_info->flush_pending_cb != NULL);
1209 
1210  /* flush the stats, if possible */
1211  did_flush = kind_info->flush_pending_cb(entry_ref, nowait);
1212 
1213  Assert(did_flush || nowait);
1214 
1215  /* determine next entry, before deleting the pending entry */
1218  else
1219  next = NULL;
1220 
1221  /* if successfully flushed, remove entry */
1222  if (did_flush)
1223  pgstat_delete_pending_entry(entry_ref);
1224  else
1225  have_pending = true;
1226 
1227  cur = next;
1228  }
1229 
1230  Assert(dlist_is_empty(&pgStatPending) == !have_pending);
1231 
1232  return have_pending;
1233 }
static int32 next
Definition: blutils.c:221
struct cursor * cur
Definition: ecpg.c:28
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 bool dlist_is_empty(const dlist_head *head)
Definition: ilist.h:336
static dlist_node * dlist_head_node(dlist_head *head)
Definition: ilist.h:565
#define dlist_container(type, membername, ptr)
Definition: ilist.h:593
void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat.c:1156
static dlist_head pgStatPending
Definition: pgstat.c:218
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(), pgStatPending, and PgStat_EntryRef::shared_entry.

Referenced by pgstat_report_stat().

◆ pgstat_force_next_flush()

void pgstat_force_next_flush ( void  )

Definition at line 694 of file pgstat.c.

695 {
696  pgStatForceNextFlush = true;
697 }
static bool pgStatForceNextFlush
Definition: pgstat.c:225

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 1242 of file pgstat.c.

1243 {
1244  for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1245  {
1246  if (pg_strcasecmp(kind_str, pgstat_kind_infos[kind].name) == 0)
1247  return kind;
1248  }
1249 
1250  ereport(ERROR,
1251  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1252  errmsg("invalid statistics kind: \"%s\"", kind_str)));
1253  return PGSTAT_KIND_DATABASE; /* avoid compiler warnings */
1254 }
int errcode(int sqlerrcode)
Definition: elog.c:859
#define ERROR
Definition: elog.h:39
static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS]
Definition: pgstat.c:257
@ PGSTAT_KIND_DATABASE
Definition: pgstat.h:41
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
const char * name

References ereport, errcode(), errmsg(), ERROR, name, pg_strcasecmp(), PGSTAT_KIND_DATABASE, PGSTAT_KIND_FIRST_VALID, pgstat_kind_infos, and PGSTAT_KIND_LAST.

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 905 of file pgstat.c.

906 {
909 
911  {
912  *have_snapshot = true;
914  }
915 
916  *have_snapshot = false;
917 
918  return 0;
919 }
void pgstat_clear_snapshot(void)
Definition: pgstat.c:782

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,
Oid  objoid 
)

Definition at line 922 of file pgstat.c.

923 {
924  /* fixed-numbered stats always exist */
925  if (pgstat_get_kind_info(kind)->fixed_amount)
926  return true;
927 
928  return pgstat_get_entry_ref(kind, dboid, objoid, false, NULL) != NULL;
929 }

References pgstat_get_entry_ref(), and pgstat_get_kind_info().

Referenced by pg_stat_have_stats().

◆ pgstat_initialize()

void pgstat_initialize ( void  )

Definition at line 537 of file pgstat.c.

538 {
539  Assert(!pgstat_is_initialized);
540 
542 
543  pgstat_init_wal();
544 
545  /* Set up a process-exit hook to clean up */
547 
548 #ifdef USE_ASSERT_CHECKING
549  pgstat_is_initialized = true;
550 #endif
551 }
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:337
static void pgstat_shutdown_hook(int code, Datum arg)
Definition: pgstat.c:503
void pgstat_attach_shmem(void)
Definition: pgstat_shmem.c:218
void pgstat_init_wal(void)
Definition: pgstat_wal.c:141

References Assert(), before_shmem_exit(), pgstat_attach_shmem(), pgstat_init_wal(), and pgstat_shutdown_hook().

Referenced by BaseInit().

◆ pgstat_is_kind_valid()

static bool pgstat_is_kind_valid ( int  ikind)
inlinestatic

Definition at line 1257 of file pgstat.c.

1258 {
1259  return ikind >= PGSTAT_KIND_FIRST_VALID && ikind <= PGSTAT_KIND_LAST;
1260 }

References PGSTAT_KIND_FIRST_VALID, and PGSTAT_KIND_LAST.

Referenced by pgstat_get_kind_info(), pgstat_read_statsfile(), and pgstat_snapshot_fixed().

◆ pgstat_prep_pending_entry()

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

Definition at line 1105 of file pgstat.c.

1106 {
1107  PgStat_EntryRef *entry_ref;
1108 
1109  /* need to be able to flush out */
1110  Assert(pgstat_get_kind_info(kind)->flush_pending_cb != NULL);
1111 
1113  {
1116  "PgStat Pending",
1118  }
1119 
1120  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid,
1121  true, created_entry);
1122 
1123  if (entry_ref->pending == NULL)
1124  {
1125  size_t entrysize = pgstat_get_kind_info(kind)->pending_size;
1126 
1127  Assert(entrysize != (size_t) -1);
1128 
1129  entry_ref->pending = MemoryContextAllocZero(pgStatPendingContext, entrysize);
1131  }
1132 
1133  return entry_ref;
1134 }
#define unlikely(x)
Definition: c.h:298
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:364
MemoryContext TopMemoryContext
Definition: mcxt.c:137
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1202
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:163
static MemoryContext pgStatPendingContext
Definition: pgstat.c:210

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_init_function_usage(), pgstat_prep_database_pending(), pgstat_prep_relation_pending(), and pgstat_report_subscription_error().

◆ pgstat_prep_snapshot()

◆ pgstat_read_statsfile()

static void pgstat_read_statsfile ( void  )
static

Definition at line 1483 of file pgstat.c.

1484 {
1485  FILE *fpin;
1486  int32 format_id;
1487  bool found;
1488  const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
1490 
1491  /* shouldn't be called from postmaster */
1493 
1494  elog(DEBUG2, "reading stats file \"%s\"", statfile);
1495 
1496  /*
1497  * Try to open the stats file. If it doesn't exist, the backends simply
1498  * returns zero for anything and statistics simply starts from scratch
1499  * with empty counters.
1500  *
1501  * ENOENT is a possibility if stats collection was previously disabled or
1502  * has not yet written the stats file for the first time. Any other
1503  * failure condition is suspicious.
1504  */
1505  if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
1506  {
1507  if (errno != ENOENT)
1508  ereport(LOG,
1510  errmsg("could not open statistics file \"%s\": %m",
1511  statfile)));
1513  return;
1514  }
1515 
1516  /*
1517  * Verify it's of the expected format.
1518  */
1519  if (!read_chunk_s(fpin, &format_id) ||
1520  format_id != PGSTAT_FILE_FORMAT_ID)
1521  goto error;
1522 
1523  /*
1524  * XXX: The following could now be generalized to just iterate over
1525  * pgstat_kind_infos instead of knowing about the different kinds of
1526  * stats.
1527  */
1528 
1529  /*
1530  * Read archiver stats struct
1531  */
1532  if (!read_chunk_s(fpin, &shmem->archiver.stats))
1533  goto error;
1534 
1535  /*
1536  * Read bgwriter stats struct
1537  */
1538  if (!read_chunk_s(fpin, &shmem->bgwriter.stats))
1539  goto error;
1540 
1541  /*
1542  * Read checkpointer stats struct
1543  */
1544  if (!read_chunk_s(fpin, &shmem->checkpointer.stats))
1545  goto error;
1546 
1547  /*
1548  * Read IO stats struct
1549  */
1550  if (!read_chunk_s(fpin, &shmem->io.stats))
1551  goto error;
1552 
1553  /*
1554  * Read SLRU stats struct
1555  */
1556  if (!read_chunk_s(fpin, &shmem->slru.stats))
1557  goto error;
1558 
1559  /*
1560  * Read WAL stats struct
1561  */
1562  if (!read_chunk_s(fpin, &shmem->wal.stats))
1563  goto error;
1564 
1565  /*
1566  * We found an existing statistics file. Read it and put all the hash
1567  * table entries into place.
1568  */
1569  for (;;)
1570  {
1571  int t = fgetc(fpin);
1572 
1573  switch (t)
1574  {
1575  case 'S':
1576  case 'N':
1577  {
1580  PgStatShared_Common *header;
1581 
1583 
1584  if (t == 'S')
1585  {
1586  /* normal stats entry, identified by PgStat_HashKey */
1587  if (!read_chunk_s(fpin, &key))
1588  goto error;
1589 
1590  if (!pgstat_is_kind_valid(key.kind))
1591  goto error;
1592  }
1593  else
1594  {
1595  /* stats entry identified by name on disk (e.g. slots) */
1596  const PgStat_KindInfo *kind_info = NULL;
1597  PgStat_Kind kind;
1598  NameData name;
1599 
1600  if (!read_chunk_s(fpin, &kind))
1601  goto error;
1602  if (!read_chunk_s(fpin, &name))
1603  goto error;
1604  if (!pgstat_is_kind_valid(kind))
1605  goto error;
1606 
1607  kind_info = pgstat_get_kind_info(kind);
1608 
1609  if (!kind_info->from_serialized_name)
1610  goto error;
1611 
1612  if (!kind_info->from_serialized_name(&name, &key))
1613  {
1614  /* skip over data for entry we don't care about */
1615  if (fseek(fpin, pgstat_get_entry_len(kind), SEEK_CUR) != 0)
1616  goto error;
1617 
1618  continue;
1619  }
1620 
1621  Assert(key.kind == kind);
1622  }
1623 
1624  /*
1625  * This intentionally doesn't use pgstat_get_entry_ref() -
1626  * putting all stats into checkpointer's
1627  * pgStatEntryRefHash would be wasted effort and memory.
1628  */
1630 
1631  /* don't allow duplicate entries */
1632  if (found)
1633  {
1635  elog(WARNING, "found duplicate stats entry %d/%u/%u",
1636  key.kind, key.dboid, key.objoid);
1637  goto error;
1638  }
1639 
1640  header = pgstat_init_entry(key.kind, p);
1642 
1643  if (!read_chunk(fpin,
1644  pgstat_get_entry_data(key.kind, header),
1645  pgstat_get_entry_len(key.kind)))
1646  goto error;
1647 
1648  break;
1649  }
1650  case 'E':
1651  /* check that 'E' actually signals end of file */
1652  if (fgetc(fpin) != EOF)
1653  goto error;
1654 
1655  goto done;
1656 
1657  default:
1658  goto error;
1659  }
1660  }
1661 
1662 done:
1663  FreeFile(fpin);
1664 
1665  elog(DEBUG2, "removing permanent stats file \"%s\"", statfile);
1666  unlink(statfile);
1667 
1668  return;
1669 
1670 error:
1671  ereport(LOG,
1672  (errmsg("corrupted statistics file \"%s\"", statfile)));
1673 
1675 
1676  goto done;
1677 }
#define PG_BINARY_R
Definition: c.h:1262
signed int int32
Definition: c.h:481
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
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2583
int FreeFile(FILE *file)
Definition: fd.c:2781
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
static bool read_chunk(FILE *fpin, void *ptr, size_t len)
Definition: pgstat.c:1469
#define read_chunk_s(fpin, ptr)
Definition: pgstat.c:1474
#define PGSTAT_FILE_FORMAT_ID
Definition: pgstat.h:238
static size_t pgstat_get_entry_len(PgStat_Kind kind)
PgStatShared_Common * pgstat_init_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
Definition: pgstat_shmem.c:259
static void error(void)
Definition: sql-dyntest.c:147
PgStat_ArchiverStats stats
PgStat_BgWriterStats stats
PgStat_CheckpointerStats stats
PgStat_SLRUStats stats[SLRU_NUM_ELEMENTS]
PgStat_WalStats stats
bool(* from_serialized_name)(const NameData *name, PgStat_HashKey *key)
PgStatShared_SLRU slru
PgStatShared_IO io
PgStatShared_Wal wal
PgStatShared_Archiver archiver
PgStatShared_Checkpointer checkpointer
PgStatShared_BgWriter bgwriter
Definition: c.h:728

References AllocateFile(), PgStat_ShmemControl::archiver, Assert(), PgStat_ShmemControl::bgwriter, CHECK_FOR_INTERRUPTS, PgStat_ShmemControl::checkpointer, DEBUG2, dshash_find_or_insert(), dshash_release_lock(), elog, ereport, errcode_for_file_access(), errmsg(), error(), FreeFile(), PgStat_KindInfo::from_serialized_name, PgStat_ShmemControl::io, IsPostmasterEnvironment, IsUnderPostmaster, sort-test::key, LOG, name, PG_BINARY_R, PGSTAT_FILE_FORMAT_ID, pgstat_get_entry_data(), pgstat_get_entry_len(), pgstat_get_kind_info(), pgstat_init_entry(), pgstat_is_kind_valid(), pgstat_reset_after_failure(), PGSTAT_STAT_PERMANENT_FILENAME, pgStatLocal, read_chunk(), read_chunk_s, PgStat_LocalState::shared_hash, PgStat_LocalState::shmem, PgStat_ShmemControl::slru, PgStatShared_Archiver::stats, PgStatShared_BgWriter::stats, PgStatShared_Checkpointer::stats, PgStatShared_IO::stats, PgStatShared_SLRU::stats, PgStatShared_Wal::stats, PgStat_ShmemControl::wal, and WARNING.

Referenced by pgstat_restore_stats().

◆ pgstat_report_stat()

long pgstat_report_stat ( bool  force)

Definition at line 579 of file pgstat.c.

580 {
581  static TimestampTz pending_since = 0;
582  static TimestampTz last_flush = 0;
583  bool partial_flush;
585  bool nowait;
586 
589 
590  /* "absorb" the forced flush even if there's nothing to flush */
592  {
593  force = true;
594  pgStatForceNextFlush = false;
595  }
596 
597  /* Don't expend a clock check if nothing to do */
599  !have_iostats &&
600  !have_slrustats &&
602  {
603  Assert(pending_since == 0);
604  return 0;
605  }
606 
607  /*
608  * There should never be stats to report once stats are shut down. Can't
609  * assert that before the checks above, as there is an unconditional
610  * pgstat_report_stat() call in pgstat_shutdown_hook() - which at least
611  * the process that ran pgstat_before_server_shutdown() will still call.
612  */
614 
615  if (force)
616  {
617  /*
618  * Stats reports are forced either when it's been too long since stats
619  * have been reported or in processes that force stats reporting to
620  * happen at specific points (including shutdown). In the former case
621  * the transaction stop time might be quite old, in the latter it
622  * would never get cleared.
623  */
625  }
626  else
627  {
629 
630  if (pending_since > 0 &&
632  {
633  /* don't keep pending updates longer than PGSTAT_MAX_INTERVAL */
634  force = true;
635  }
636  else if (last_flush > 0 &&
638  {
639  /* don't flush too frequently */
640  if (pending_since == 0)
641  pending_since = now;
642 
643  return PGSTAT_IDLE_INTERVAL;
644  }
645  }
646 
648 
649  /* don't wait for lock acquisition when !force */
650  nowait = !force;
651 
652  partial_flush = false;
653 
654  /* flush database / relation / function / ... stats */
655  partial_flush |= pgstat_flush_pending_entries(nowait);
656 
657  /* flush IO stats */
658  partial_flush |= pgstat_flush_io(nowait);
659 
660  /* flush wal stats */
661  partial_flush |= pgstat_flush_wal(nowait);
662 
663  /* flush SLRU stats */
664  partial_flush |= pgstat_slru_flush(nowait);
665 
666  last_flush = now;
667 
668  /*
669  * If some of the pending stats could not be flushed due to lock
670  * contention, let the caller know when to retry.
671  */
672  if (partial_flush)
673  {
674  /* force should have prevented us from getting here */
675  Assert(!force);
676 
677  /* remember since when stats have been pending */
678  if (pending_since == 0)
679  pending_since = now;
680 
681  return PGSTAT_IDLE_INTERVAL;
682  }
683 
684  pending_since = 0;
685 
686  return 0;
687 }
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1790
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1618
int64 TimestampTz
Definition: timestamp.h:39
#define PGSTAT_MIN_INTERVAL
Definition: pgstat.c:117
static bool pgstat_flush_pending_entries(bool nowait)
Definition: pgstat.c:1180
#define PGSTAT_MAX_INTERVAL
Definition: pgstat.c:119
#define PGSTAT_IDLE_INTERVAL
Definition: pgstat.c:121
void pgstat_update_dbstats(TimestampTz ts)
bool pgstat_flush_io(bool nowait)
Definition: pgstat_io.c:173
bool have_iostats
Definition: pgstat_io.c:32
bool have_slrustats
Definition: pgstat_slru.c:35
bool pgstat_slru_flush(bool nowait)
Definition: pgstat_slru.c:156
bool pgstat_have_pending_wal(void)
Definition: pgstat_wal.c:159
bool pgstat_flush_wal(bool nowait)
Definition: pgstat_wal.c:82
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4910
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition: xact.c:877

References Assert(), dlist_is_empty(), GetCurrentTimestamp(), GetCurrentTransactionStopTimestamp(), have_iostats, have_slrustats, PgStat_ShmemControl::is_shutdown, IsTransactionOrTransactionBlock(), now(), pgstat_assert_is_up, pgstat_flush_io(), pgstat_flush_pending_entries(), pgstat_flush_wal(), pgstat_have_pending_wal(), PGSTAT_IDLE_INTERVAL, PGSTAT_MAX_INTERVAL, PGSTAT_MIN_INTERVAL, pgstat_slru_flush(), 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(), LogicalRepApplyLoop(), LogicalRepSyncTableStart(), pg_attribute_noreturn(), 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,
Oid  objoid 
)

Definition at line 734 of file pgstat.c.

735 {
736  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
738 
739  /* not needed atm, and doesn't make sense with the current signature */
740  Assert(!pgstat_get_kind_info(kind)->fixed_amount);
741 
742  /* reset the "single counter" */
743  pgstat_reset_entry(kind, dboid, objoid, ts);
744 
745  if (!kind_info->accessed_across_databases)
747 }
void pgstat_reset_database_timestamp(Oid dboid, TimestampTz ts)
void pgstat_reset_entry(PgStat_Kind kind, Oid dboid, Oid objoid, TimestampTz ts)
Definition: pgstat_shmem.c:934

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_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 1684 of file pgstat.c.

1685 {
1687 
1688  /* reset fixed-numbered stats */
1689  for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1690  {
1691  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1692 
1693  if (!kind_info->fixed_amount)
1694  continue;
1695 
1696  kind_info->reset_all_cb(ts);
1697  }
1698 
1699  /* and drop variable-numbered ones */
1701 }
void pgstat_drop_all_entries(void)
Definition: pgstat_shmem.c:896
void(* reset_all_cb)(TimestampTz ts)

References PgStat_KindInfo::fixed_amount, GetCurrentTimestamp(), pgstat_drop_all_entries(), pgstat_get_kind_info(), PGSTAT_KIND_FIRST_VALID, PGSTAT_KIND_LAST, 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 715 of file pgstat.c.

716 {
718 
721  ts);
722 }
static bool match_db_entries(PgStatShared_HashEntry *entry, Datum match_data)
Definition: pgstat.c:703
void pgstat_reset_matching_entries(bool(*do_reset)(PgStatShared_HashEntry *, Datum), Datum match_data, TimestampTz ts)
Definition: pgstat_shmem.c:954
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252

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 756 of file pgstat.c.

757 {
758  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
760 
761  if (kind_info->fixed_amount)
762  kind_info->reset_all_cb(ts);
763  else
765 }
void pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts)
Definition: pgstat_shmem.c:990

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 407 of file pgstat.c.

408 {
410 }
static void pgstat_read_statsfile(void)
Definition: pgstat.c:1483

References pgstat_read_statsfile().

Referenced by StartupXLOG().

◆ pgstat_shutdown_hook()

static void pgstat_shutdown_hook ( int  code,
Datum  arg 
)
static

Definition at line 503 of file pgstat.c.

504 {
505  Assert(!pgstat_is_shutdown);
507 
508  /*
509  * If we got as far as discovering our own database ID, we can flush out
510  * what we did so far. Otherwise, we'd be reporting an invalid database
511  * ID, so forget it. (This means that accesses to pg_database during
512  * failed backend starts might never get counted.)
513  */
516 
517  pgstat_report_stat(true);
518 
519  /* there shouldn't be any pending changes left */
522 
524 
525 #ifdef USE_ASSERT_CHECKING
526  pgstat_is_shutdown = true;
527 #endif
528 }
#define OidIsValid(objectId)
Definition: c.h:762
static void dlist_init(dlist_head *head)
Definition: ilist.h:314
void pgstat_report_disconnect(Oid dboid)
void pgstat_detach_shmem(void)
Definition: pgstat_shmem.c:238

References Assert(), dlist_init(), dlist_is_empty(), IsPostmasterEnvironment, IsUnderPostmaster, MyDatabaseId, OidIsValid, pgstat_detach_shmem(), pgstat_report_disconnect(), pgstat_report_stat(), and pgStatPending.

Referenced by pgstat_initialize().

◆ pgstat_snapshot_fixed()

◆ pgstat_write_statsfile()

static void pgstat_write_statsfile ( void  )
static

Definition at line 1308 of file pgstat.c.

1309 {
1310  FILE *fpout;
1311  int32 format_id;
1312  const char *tmpfile = PGSTAT_STAT_PERMANENT_TMPFILE;
1313  const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
1314  dshash_seq_status hstat;
1316 
1318 
1319  /* we're shutting down, so it's ok to just override this */
1321 
1322  elog(DEBUG2, "writing stats file \"%s\"", statfile);
1323 
1324  /*
1325  * Open the statistics temp file to write out the current values.
1326  */
1327  fpout = AllocateFile(tmpfile, PG_BINARY_W);
1328  if (fpout == NULL)
1329  {
1330  ereport(LOG,
1332  errmsg("could not open temporary statistics file \"%s\": %m",
1333  tmpfile)));
1334  return;
1335  }
1336 
1337  /*
1338  * Write the file header --- currently just a format ID.
1339  */
1340  format_id = PGSTAT_FILE_FORMAT_ID;
1341  write_chunk_s(fpout, &format_id);
1342 
1343  /*
1344  * XXX: The following could now be generalized to just iterate over
1345  * pgstat_kind_infos instead of knowing about the different kinds of
1346  * stats.
1347  */
1348 
1349  /*
1350  * Write archiver stats struct
1351  */
1354 
1355  /*
1356  * Write bgwriter stats struct
1357  */
1360 
1361  /*
1362  * Write checkpointer stats struct
1363  */
1366 
1367  /*
1368  * Write IO stats struct
1369  */
1372 
1373  /*
1374  * Write SLRU stats struct
1375  */
1378 
1379  /*
1380  * Write WAL stats struct
1381  */
1384 
1385  /*
1386  * Walk through the stats entries
1387  */
1388  dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
1389  while ((ps = dshash_seq_next(&hstat)) != NULL)
1390  {
1391  PgStatShared_Common *shstats;
1392  const PgStat_KindInfo *kind_info = NULL;
1393 
1395 
1396  /* we may have some "dropped" entries not yet removed, skip them */
1397  Assert(!ps->dropped);
1398  if (ps->dropped)
1399  continue;
1400 
1401  shstats = (PgStatShared_Common *) dsa_get_address(pgStatLocal.dsa, ps->body);
1402 
1403  kind_info = pgstat_get_kind_info(ps->key.kind);
1404 
1405  /* if not dropped the valid-entry refcount should exist */
1406  Assert(pg_atomic_read_u32(&ps->refcount) > 0);
1407 
1408  if (!kind_info->to_serialized_name)
1409  {
1410  /* normal stats entry, identified by PgStat_HashKey */
1411  fputc('S', fpout);
1412  write_chunk_s(fpout, &ps->key);
1413  }
1414  else
1415  {
1416  /* stats entry identified by name on disk (e.g. slots) */
1417  NameData name;
1418 
1419  kind_info->to_serialized_name(&ps->key, shstats, &name);
1420 
1421  fputc('N', fpout);
1422  write_chunk_s(fpout, &ps->key.kind);
1423  write_chunk_s(fpout, &name);
1424  }
1425 
1426  /* Write except the header part of the entry */
1427  write_chunk(fpout,
1428  pgstat_get_entry_data(ps->key.kind, shstats),
1429  pgstat_get_entry_len(ps->key.kind));
1430  }
1431  dshash_seq_term(&hstat);
1432 
1433  /*
1434  * No more output to be done. Close the temp file and replace the old
1435  * pgstat.stat with it. The ferror() check replaces testing for error
1436  * after each individual fputc or fwrite (in write_chunk()) above.
1437  */
1438  fputc('E', fpout);
1439 
1440  if (ferror(fpout))
1441  {
1442  ereport(LOG,
1444  errmsg("could not write temporary statistics file \"%s\": %m",
1445  tmpfile)));
1446  FreeFile(fpout);
1447  unlink(tmpfile);
1448  }
1449  else if (FreeFile(fpout) < 0)
1450  {
1451  ereport(LOG,
1453  errmsg("could not close temporary statistics file \"%s\": %m",
1454  tmpfile)));
1455  unlink(tmpfile);
1456  }
1457  else if (rename(tmpfile, statfile) < 0)
1458  {
1459  ereport(LOG,
1461  errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
1462  tmpfile, statfile)));
1463  unlink(tmpfile);
1464  }
1465 }
#define PG_BINARY_W
Definition: c.h:1263
struct parser_state ps
static void write_chunk(FILE *fpout, void *ptr, size_t len)
Definition: pgstat.c:1291
#define write_chunk_s(fpout, ptr)
Definition: pgstat.c:1301
@ PGSTAT_KIND_ARCHIVER
Definition: pgstat.h:48
@ PGSTAT_KIND_SLRU
Definition: pgstat.h:52
@ PGSTAT_KIND_IO
Definition: pgstat.h:51
@ PGSTAT_KIND_BGWRITER
Definition: pgstat.h:49
@ PGSTAT_KIND_CHECKPOINTER
Definition: pgstat.h:50
@ PGSTAT_KIND_WAL
Definition: pgstat.h:53
#define PGSTAT_STAT_PERMANENT_TMPFILE
Definition: pgstat.h:29
void(* to_serialized_name)(const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name)
PgStat_CheckpointerStats checkpointer
PgStat_WalStats wal
PgStat_ArchiverStats archiver
PgStat_SLRUStats slru[SLRU_NUM_ELEMENTS]
PgStat_BgWriterStats bgwriter

References AllocateFile(), PgStat_Snapshot::archiver, Assert(), PgStat_Snapshot::bgwriter, CHECK_FOR_INTERRUPTS, PgStat_Snapshot::checkpointer, DEBUG2, PgStat_LocalState::dsa, dsa_get_address(), dshash_seq_init(), dshash_seq_next(), dshash_seq_term(), elog, ereport, errcode_for_file_access(), errmsg(), FreeFile(), PgStat_Snapshot::io, LOG, 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_FORMAT_ID, pgstat_get_entry_data(), pgstat_get_entry_len(), pgstat_get_kind_info(), PGSTAT_KIND_ARCHIVER, PGSTAT_KIND_BGWRITER, PGSTAT_KIND_CHECKPOINTER, PGSTAT_KIND_IO, PGSTAT_KIND_SLRU, PGSTAT_KIND_WAL, PGSTAT_STAT_PERMANENT_FILENAME, PGSTAT_STAT_PERMANENT_TMPFILE, pgStatLocal, ps, PgStat_LocalState::shared_hash, PgStat_Snapshot::slru, PgStat_LocalState::snapshot, PgStat_KindInfo::to_serialized_name, PgStat_Snapshot::wal, write_chunk(), and write_chunk_s.

Referenced by pgstat_before_server_shutdown().

◆ read_chunk()

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

Definition at line 1469 of file pgstat.c.

1470 {
1471  return fread(ptr, 1, len, fpin) == len;
1472 }
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 1291 of file pgstat.c.

1292 {
1293  int rc;
1294 
1295  rc = fwrite(ptr, len, 1, fpout);
1296 
1297  /* we'll check for errors with ferror once at the end */
1298  (void) rc;
1299 }

References len.

Referenced by pgstat_write_statsfile().

Variable Documentation

◆ force_stats_snapshot_clear

◆ pgstat_fetch_consistency

◆ pgstat_kind_infos

const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS]
static

Definition at line 257 of file pgstat.c.

Referenced by pgstat_get_kind_from_str(), and pgstat_get_kind_info().

◆ pgstat_track_counts

◆ pgStatForceNextFlush

bool pgStatForceNextFlush = false
static

Definition at line 225 of file pgstat.c.

Referenced by pgstat_force_next_flush(), and pgstat_report_stat().

◆ pgStatLocal

PgStat_LocalState pgStatLocal

◆ pgStatPending

dlist_head pgStatPending = DLIST_STATIC_INIT(pgStatPending)
static

◆ pgStatPendingContext

MemoryContext pgStatPendingContext = NULL
static

Definition at line 210 of file pgstat.c.

Referenced by pgstat_prep_pending_entry().