PostgreSQL Source Code  git master
pgstat.c File Reference
#include "postgres.h"
#include <unistd.h>
#include "access/transam.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 "storage/pg_shmem.h"
#include "storage/shmem.h"
#include "utils/guc.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)
 

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 const PgStat_KindInfo pgstat_kind_infos [PGSTAT_NUM_KINDS]
 

Macro Definition Documentation

◆ PGSTAT_IDLE_INTERVAL

#define PGSTAT_IDLE_INTERVAL   10000

Definition at line 123 of file pgstat.c.

◆ PGSTAT_MAX_INTERVAL

#define PGSTAT_MAX_INTERVAL   60000

Definition at line 121 of file pgstat.c.

◆ PGSTAT_MIN_INTERVAL

#define PGSTAT_MIN_INTERVAL   1000

Definition at line 119 of file pgstat.c.

◆ PGSTAT_SNAPSHOT_HASH_SIZE

#define PGSTAT_SNAPSHOT_HASH_SIZE   512

Definition at line 130 of file pgstat.c.

◆ read_chunk_s

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

Definition at line 1425 of file pgstat.c.

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 158 of file pgstat.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 157 of file pgstat.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   PgStat_SnapshotEntry

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

◆ SH_KEY

#define SH_KEY   key

Definition at line 151 of file pgstat.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   PgStat_HashKey

Definition at line 150 of file pgstat.c.

◆ SH_PREFIX

#define SH_PREFIX   pgstat_snapshot

Definition at line 148 of file pgstat.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 156 of file pgstat.c.

◆ write_chunk_s

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

Definition at line 1258 of file pgstat.c.

Typedef Documentation

◆ PgStat_SnapshotEntry

Function Documentation

◆ match_db_entries()

static bool match_db_entries ( PgStatShared_HashEntry entry,
Datum  match_data 
)
static

Definition at line 674 of file pgstat.c.

675 {
676  return entry->key.dboid == DatumGetObjectId(MyDatabaseId);
677 }
Oid MyDatabaseId
Definition: globals.c:89
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:590

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

449 {
450  Assert(pgStatLocal.shmem != NULL);
452 
453  /*
454  * Stats should only be reported after pgstat_initialize() and before
455  * pgstat_shutdown(). This is a convenient point to catch most violations
456  * of this rule.
457  */
458  Assert(pgstat_is_initialized && !pgstat_is_shutdown);
459 
460  /* flush out our own pending changes before writing out */
461  pgstat_report_stat(true);
462 
463  /*
464  * Only write out file during normal shutdown. Don't even signal that
465  * we've shutdown during irregular shutdowns, because the shutdown
466  * sequence isn't coordinated to ensure this backend shuts down last.
467  */
468  if (code == 0)
469  {
470  pgStatLocal.shmem->is_shutdown = true;
472  }
473 }
Assert(fmt[strlen(fmt) - 1] !='\n')
static void pgstat_write_statsfile(void)
Definition: pgstat.c:1265
long pgstat_report_stat(bool force)
Definition: pgstat.c:565
PgStat_LocalState pgStatLocal
Definition: pgstat.c:195
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 934 of file pgstat.c.

935 {
936  dshash_seq_status hstat;
938 
939  /* should only be called when we need a snapshot */
941 
942  /* snapshot already built */
944  return;
945 
947 
948  Assert(pgStatLocal.snapshot.stats->members == 0);
949 
951 
952  /*
953  * Snapshot all variable stats.
954  */
955  dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
956  while ((p = dshash_seq_next(&hstat)) != NULL)
957  {
958  PgStat_Kind kind = p->key.kind;
959  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
960  bool found;
961  PgStat_SnapshotEntry *entry;
962  PgStatShared_Common *stats_data;
963 
964  /*
965  * Check if the stats object should be included in the snapshot.
966  * Unless the stats kind can be accessed from all databases (e.g.,
967  * database stats themselves), we only include stats for the current
968  * database or objects not associated with a database (e.g. shared
969  * relations).
970  */
971  if (p->key.dboid != MyDatabaseId &&
972  p->key.dboid != InvalidOid &&
973  !kind_info->accessed_across_databases)
974  continue;
975 
976  if (p->dropped)
977  continue;
978 
980 
981  stats_data = dsa_get_address(pgStatLocal.dsa, p->body);
982  Assert(stats_data);
983 
984  entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, p->key, &found);
985  Assert(!found);
986 
988  kind_info->shared_size);
989  /*
990  * Acquire the LWLock directly instead of using
991  * pg_stat_lock_entry_shared() which requires a reference.
992  */
993  LWLockAcquire(&stats_data->lock, LW_SHARED);
994  memcpy(entry->data,
995  pgstat_get_entry_data(kind, stats_data),
996  kind_info->shared_size);
997  LWLockRelease(&stats_data->lock);
998  }
999  dshash_seq_term(&hstat);
1000 
1001  /*
1002  * Build snapshot of all fixed-numbered stats.
1003  */
1004  for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1005  {
1006  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1007 
1008  if (!kind_info->fixed_amount)
1009  {
1010  Assert(kind_info->snapshot_cb == NULL);
1011  continue;
1012  }
1013 
1015  }
1016 
1018 }
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:236
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1585
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:944
void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table, bool exclusive)
Definition: dshash.c:595
void dshash_seq_term(dshash_seq_status *status)
Definition: dshash.c:704
void * dshash_seq_next(dshash_seq_status *status)
Definition: dshash.c:614
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1194
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1802
@ LW_SHARED
Definition: lwlock.h:113
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:994
int pgstat_fetch_consistency
Definition: pgstat.c:187
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1220
static void pgstat_prep_snapshot(void)
Definition: pgstat.c:916
static void pgstat_build_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1021
PgStat_Kind
Definition: pgstat.h:36
@ PGSTAT_FETCH_CONSISTENCY_SNAPSHOT
Definition: pgstat.h:71
#define PGSTAT_KIND_LAST
Definition: pgstat.h:56
#define PGSTAT_KIND_FIRST_VALID
Definition: pgstat.h:55
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 1021 of file pgstat.c.

1022 {
1023  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1024 
1025  Assert(kind_info->fixed_amount);
1026  Assert(kind_info->snapshot_cb != NULL);
1027 
1029  {
1030  /* rebuild every time */
1031  pgStatLocal.snapshot.fixed_valid[kind] = false;
1032  }
1033  else if (pgStatLocal.snapshot.fixed_valid[kind])
1034  {
1035  /* in snapshot mode we shouldn't get called again */
1037  return;
1038  }
1039 
1041 
1042  kind_info->snapshot_cb();
1043 
1045  pgStatLocal.snapshot.fixed_valid[kind] = true;
1046 }
@ PGSTAT_FETCH_CONSISTENCY_NONE
Definition: pgstat.h:69
@ PGSTAT_FETCH_CONSISTENCY_CACHE
Definition: pgstat.h:70
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 752 of file pgstat.c.

753 {
755 
756  memset(&pgStatLocal.snapshot.fixed_valid, 0,
758  pgStatLocal.snapshot.stats = NULL;
760 
761  /* Release memory, if any was allocated */
763  {
765 
766  /* Reset variables */
768  }
769 
770  /*
771  * Historically the backend_status.c facilities lived in this file, and
772  * were reset with the same function. For now keep it that way, and
773  * forward the reset request.
774  */
776 }
void pgstat_clear_backend_activity_snapshot(void)
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:376
#define pgstat_assert_is_up()

References PgStat_Snapshot::context, PgStat_Snapshot::fixed_valid, 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(), and PostPrepare_PgStat().

◆ pgstat_delete_pending_entry()

void pgstat_delete_pending_entry ( PgStat_EntryRef entry_ref)

Definition at line 1113 of file pgstat.c.

1114 {
1115  PgStat_Kind kind = entry_ref->shared_entry->key.kind;
1116  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1117  void *pending_data = entry_ref->pending;
1118 
1119  Assert(pending_data != NULL);
1120  /* !fixed_amount stats should be handled explicitly */
1121  Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1122 
1123  if (kind_info->delete_pending_cb)
1124  kind_info->delete_pending_cb(entry_ref);
1125 
1126  pfree(pending_data);
1127  entry_ref->pending = NULL;
1128 
1129  dlist_delete(&entry_ref->pending_node);
1130 }
static void dlist_delete(dlist_node *node)
Definition: ilist.h:394
void pfree(void *pointer)
Definition: mcxt.c:1306
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 405 of file pgstat.c.

406 {
407  int ret;
408 
409  /* NB: this needs to be done even in single user mode */
410 
411  ret = unlink(PGSTAT_STAT_PERMANENT_FILENAME);
412  if (ret != 0)
413  {
414  if (errno == ENOENT)
415  elog(DEBUG2,
416  "didn't need to unlink permanent stats file \"%s\" - didn't exist",
418  else
419  ereport(LOG,
421  errmsg("could not unlink permanent statistics file \"%s\": %m",
423  }
424  else
425  {
426  ereport(DEBUG2,
428  errmsg_internal("unlinked permanent statistics file \"%s\"",
430  }
431 
432  /*
433  * Reset stats contents. This will set reset timestamps of fixed-numbered
434  * stats to the current time (no variable stats exist).
435  */
437 }
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1156
int errcode_for_file_access(void)
Definition: elog.c:881
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define LOG
Definition: elog.h:31
#define DEBUG2
Definition: elog.h:29
#define ereport(elevel,...)
Definition: elog.h:149
static void pgstat_reset_after_failure(void)
Definition: pgstat.c:1629
#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 779 of file pgstat.c.

780 {
782  PgStat_EntryRef *entry_ref;
783  void *stats_data;
784  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
785 
786  /* should be called from backends */
788  Assert(!kind_info->fixed_amount);
789 
791 
792  key.kind = kind;
793  key.dboid = dboid;
794  key.objoid = objoid;
795 
796  /* if we need to build a full snapshot, do so */
799 
800  /* if caching is desired, look up in cache */
802  {
803  PgStat_SnapshotEntry *entry = NULL;
804 
805  entry = pgstat_snapshot_lookup(pgStatLocal.snapshot.stats, key);
806 
807  if (entry)
808  return entry->data;
809 
810  /*
811  * If we built a full snapshot and the key is not in
812  * pgStatLocal.snapshot.stats, there are no matching stats.
813  */
815  return NULL;
816  }
817 
819 
820  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
821 
822  if (entry_ref == NULL || entry_ref->shared_entry->dropped)
823  {
824  /* create empty entry when using PGSTAT_FETCH_CONSISTENCY_CACHE */
826  {
827  PgStat_SnapshotEntry *entry = NULL;
828  bool found;
829 
830  entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
831  Assert(!found);
832  entry->data = NULL;
833  }
834  return NULL;
835  }
836 
837  /*
838  * Allocate in caller's context for PGSTAT_FETCH_CONSISTENCY_NONE,
839  * otherwise we could quickly end up with a fair bit of memory used due to
840  * repeated accesses.
841  */
843  stats_data = palloc(kind_info->shared_data_len);
844  else
846  kind_info->shared_data_len);
847 
848  pgstat_lock_entry_shared(entry_ref, false);
849  memcpy(stats_data,
850  pgstat_get_entry_data(kind, entry_ref->shared_stats),
851  kind_info->shared_data_len);
852  pgstat_unlock_entry(entry_ref);
853 
855  {
856  PgStat_SnapshotEntry *entry = NULL;
857  bool found;
858 
859  entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
860  entry->data = stats_data;
861  }
862 
863  return stats_data;
864 }
bool IsUnderPostmaster
Definition: globals.c:113
bool IsPostmasterEnvironment
Definition: globals.c:112
void * palloc(Size size)
Definition: mcxt.c:1199
static void pgstat_build_snapshot(void)
Definition: pgstat.c:934
bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:587
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:393
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:599
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 1100 of file pgstat.c.

1101 {
1102  PgStat_EntryRef *entry_ref;
1103 
1104  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
1105 
1106  if (entry_ref == NULL || entry_ref->pending == NULL)
1107  return NULL;
1108 
1109  return entry_ref;
1110 }

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

1138 {
1139  bool have_pending = false;
1140  dlist_node *cur = NULL;
1141 
1142  /*
1143  * Need to be a bit careful iterating over the list of pending entries.
1144  * Processing a pending entry may queue further pending entries to the end
1145  * of the list that we want to process, so a simple iteration won't do.
1146  * Further complicating matters is that we want to delete the current
1147  * entry in each iteration from the list if we flushed successfully.
1148  *
1149  * So we just keep track of the next pointer in each loop iteration.
1150  */
1153 
1154  while (cur)
1155  {
1156  PgStat_EntryRef *entry_ref =
1157  dlist_container(PgStat_EntryRef, pending_node, cur);
1158  PgStat_HashKey key = entry_ref->shared_entry->key;
1159  PgStat_Kind kind = key.kind;
1160  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1161  bool did_flush;
1162  dlist_node *next;
1163 
1164  Assert(!kind_info->fixed_amount);
1165  Assert(kind_info->flush_pending_cb != NULL);
1166 
1167  /* flush the stats, if possible */
1168  did_flush = kind_info->flush_pending_cb(entry_ref, nowait);
1169 
1170  Assert(did_flush || nowait);
1171 
1172  /* determine next entry, before deleting the pending entry */
1175  else
1176  next = NULL;
1177 
1178  /* if successfully flushed, remove entry */
1179  if (did_flush)
1180  pgstat_delete_pending_entry(entry_ref);
1181  else
1182  have_pending = true;
1183 
1184  cur = next;
1185  }
1186 
1187  Assert(dlist_is_empty(&pgStatPending) == !have_pending);
1188 
1189  return have_pending;
1190 }
static int32 next
Definition: blutils.c:219
struct cursor * cur
Definition: ecpg.c:28
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:325
static dlist_node * dlist_next_node(dlist_head *head, dlist_node *node)
Definition: ilist.h:487
static bool dlist_has_next(dlist_head *head, dlist_node *node)
Definition: ilist.h:468
static dlist_node * dlist_head_node(dlist_head *head)
Definition: ilist.h:515
#define dlist_container(type, membername, ptr)
Definition: ilist.h:543
void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat.c:1113
static dlist_head pgStatPending
Definition: pgstat.c:220
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 665 of file pgstat.c.

666 {
667  pgStatForceNextFlush = true;
668 }
static bool pgStatForceNextFlush
Definition: pgstat.c:227

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

1200 {
1201  for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1202  {
1203  if (pg_strcasecmp(kind_str, pgstat_kind_infos[kind].name) == 0)
1204  return kind;
1205  }
1206 
1207  ereport(ERROR,
1208  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1209  errmsg("invalid statistics kind: \"%s\"", kind_str)));
1210  return PGSTAT_KIND_DATABASE; /* avoid compiler warnings */
1211 }
int errcode(int sqlerrcode)
Definition: elog.c:858
#define ERROR
Definition: elog.h:39
const char * name
Definition: encode.c:561
static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS]
Definition: pgstat.c:252
@ PGSTAT_KIND_DATABASE
Definition: pgstat.h:41
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36

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

873 {
875  {
876  *have_snapshot = true;
878  }
879 
880  *have_snapshot = false;
881 
882  return 0;
883 }

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

887 {
888  /* fixed-numbered stats always exist */
889  if (pgstat_get_kind_info(kind)->fixed_amount)
890  return true;
891 
892  return pgstat_get_entry_ref(kind, dboid, objoid, false, NULL) != NULL;
893 }

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

524 {
525  Assert(!pgstat_is_initialized);
526 
528 
529  pgstat_init_wal();
530 
531  /* Set up a process-exit hook to clean up */
533 
534 #ifdef USE_ASSERT_CHECKING
535  pgstat_is_initialized = true;
536 #endif
537 }
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:333
static void pgstat_shutdown_hook(int code, Datum arg)
Definition: pgstat.c:489
void pgstat_attach_shmem(void)
Definition: pgstat_shmem.c:213
void pgstat_init_wal(void)
Definition: pgstat_wal.c:126

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

1215 {
1216  return ikind >= PGSTAT_KIND_FIRST_VALID && ikind <= PGSTAT_KIND_LAST;
1217 }

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

1063 {
1064  PgStat_EntryRef *entry_ref;
1065 
1066  /* need to be able to flush out */
1067  Assert(pgstat_get_kind_info(kind)->flush_pending_cb != NULL);
1068 
1070  {
1073  "PgStat Pending",
1075  }
1076 
1077  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid,
1078  true, created_entry);
1079 
1080  if (entry_ref->pending == NULL)
1081  {
1082  size_t entrysize = pgstat_get_kind_info(kind)->pending_size;
1083 
1084  Assert(entrysize != (size_t) -1);
1085 
1086  entry_ref->pending = MemoryContextAllocZero(pgStatPendingContext, entrysize);
1088  }
1089 
1090  return entry_ref;
1091 }
#define unlikely(x)
Definition: c.h:295
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:353
MemoryContext TopMemoryContext
Definition: mcxt.c:130
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1037
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:163
static MemoryContext pgStatPendingContext
Definition: pgstat.c:212

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()

static void pgstat_prep_snapshot ( void  )
static

◆ pgstat_read_statsfile()

static void pgstat_read_statsfile ( void  )
static

Definition at line 1434 of file pgstat.c.

1435 {
1436  FILE *fpin;
1437  int32 format_id;
1438  bool found;
1439  const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
1441 
1442  /* shouldn't be called from postmaster */
1444 
1445  elog(DEBUG2, "reading stats file \"%s\"", statfile);
1446 
1447  /*
1448  * Try to open the stats file. If it doesn't exist, the backends simply
1449  * returns zero for anything and statistics simply starts from scratch
1450  * with empty counters.
1451  *
1452  * ENOENT is a possibility if stats collection was previously disabled or
1453  * has not yet written the stats file for the first time. Any other
1454  * failure condition is suspicious.
1455  */
1456  if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
1457  {
1458  if (errno != ENOENT)
1459  ereport(LOG,
1461  errmsg("could not open statistics file \"%s\": %m",
1462  statfile)));
1464  return;
1465  }
1466 
1467  /*
1468  * Verify it's of the expected format.
1469  */
1470  if (!read_chunk_s(fpin, &format_id) ||
1471  format_id != PGSTAT_FILE_FORMAT_ID)
1472  goto error;
1473 
1474  /*
1475  * XXX: The following could now be generalized to just iterate over
1476  * pgstat_kind_infos instead of knowing about the different kinds of
1477  * stats.
1478  */
1479 
1480  /*
1481  * Read archiver stats struct
1482  */
1483  if (!read_chunk_s(fpin, &shmem->archiver.stats))
1484  goto error;
1485 
1486  /*
1487  * Read bgwriter stats struct
1488  */
1489  if (!read_chunk_s(fpin, &shmem->bgwriter.stats))
1490  goto error;
1491 
1492  /*
1493  * Read checkpointer stats struct
1494  */
1495  if (!read_chunk_s(fpin, &shmem->checkpointer.stats))
1496  goto error;
1497 
1498  /*
1499  * Read SLRU stats struct
1500  */
1501  if (!read_chunk_s(fpin, &shmem->slru.stats))
1502  goto error;
1503 
1504  /*
1505  * Read WAL stats struct
1506  */
1507  if (!read_chunk_s(fpin, &shmem->wal.stats))
1508  goto error;
1509 
1510  /*
1511  * We found an existing statistics file. Read it and put all the hash
1512  * table entries into place.
1513  */
1514  for (;;)
1515  {
1516  int t = fgetc(fpin);
1517 
1518  switch (t)
1519  {
1520  case 'S':
1521  case 'N':
1522  {
1526 
1528 
1529  if (t == 'S')
1530  {
1531  /* normal stats entry, identified by PgStat_HashKey */
1532  if (!read_chunk_s(fpin, &key))
1533  goto error;
1534 
1535  if (!pgstat_is_kind_valid(key.kind))
1536  goto error;
1537  }
1538  else
1539  {
1540  /* stats entry identified by name on disk (e.g. slots) */
1541  const PgStat_KindInfo *kind_info = NULL;
1542  PgStat_Kind kind;
1543  NameData name;
1544 
1545  if (!read_chunk_s(fpin, &kind))
1546  goto error;
1547  if (!read_chunk_s(fpin, &name))
1548  goto error;
1549  if (!pgstat_is_kind_valid(kind))
1550  goto error;
1551 
1552  kind_info = pgstat_get_kind_info(kind);
1553 
1554  if (!kind_info->from_serialized_name)
1555  goto error;
1556 
1557  if (!kind_info->from_serialized_name(&name, &key))
1558  {
1559  /* skip over data for entry we don't care about */
1560  if (fseek(fpin, pgstat_get_entry_len(kind), SEEK_CUR) != 0)
1561  goto error;
1562 
1563  continue;
1564  }
1565 
1566  Assert(key.kind == kind);
1567  }
1568 
1569  /*
1570  * This intentionally doesn't use pgstat_get_entry_ref() -
1571  * putting all stats into checkpointer's
1572  * pgStatEntryRefHash would be wasted effort and memory.
1573  */
1575 
1576  /* don't allow duplicate entries */
1577  if (found)
1578  {
1580  elog(WARNING, "found duplicate stats entry %d/%u/%u",
1581  key.kind, key.dboid, key.objoid);
1582  goto error;
1583  }
1584 
1585  header = pgstat_init_entry(key.kind, p);
1587 
1588  if (!read_chunk(fpin,
1590  pgstat_get_entry_len(key.kind)))
1591  goto error;
1592 
1593  break;
1594  }
1595  case 'E':
1596  /* check that 'E' actually signals end of file */
1597  if (fgetc(fpin) != EOF)
1598  goto error;
1599 
1600  goto done;
1601 
1602  default:
1603  goto error;
1604  }
1605  }
1606 
1607 done:
1608  FreeFile(fpin);
1609 
1610  elog(DEBUG2, "removing permanent stats file \"%s\"", statfile);
1611  unlink(statfile);
1612 
1613  return;
1614 
1615 error:
1616  ereport(LOG,
1617  (errmsg("corrupted statistics file \"%s\"", statfile)));
1618 
1620 
1621  goto done;
1622 }
#define PG_BINARY_R
Definition: c.h:1211
signed int int32
Definition: c.h:430
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:2383
int FreeFile(FILE *file)
Definition: fd.c:2581
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:208
static bool read_chunk(FILE *fpin, void *ptr, size_t len)
Definition: pgstat.c:1420
#define read_chunk_s(fpin, ptr)
Definition: pgstat.c:1425
#define PGSTAT_FILE_FORMAT_ID
Definition: pgstat.h:245
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:254
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_Wal wal
PgStatShared_Archiver archiver
PgStatShared_Checkpointer checkpointer
PgStatShared_BgWriter bgwriter
Definition: c.h:677

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, header(), 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_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 565 of file pgstat.c.

566 {
567  static TimestampTz pending_since = 0;
568  static TimestampTz last_flush = 0;
569  bool partial_flush;
571  bool nowait;
572 
575 
576  /* "absorb" the forced flush even if there's nothing to flush */
578  {
579  force = true;
580  pgStatForceNextFlush = false;
581  }
582 
583  /* Don't expend a clock check if nothing to do */
585  !have_slrustats &&
587  {
588  Assert(pending_since == 0);
589  return 0;
590  }
591 
592  /*
593  * There should never be stats to report once stats are shut down. Can't
594  * assert that before the checks above, as there is an unconditional
595  * pgstat_report_stat() call in pgstat_shutdown_hook() - which at least
596  * the process that ran pgstat_before_server_shutdown() will still call.
597  */
599 
601 
602  if (!force)
603  {
604  if (pending_since > 0 &&
606  {
607  /* don't keep pending updates longer than PGSTAT_MAX_INTERVAL */
608  force = true;
609  }
610  else if (last_flush > 0 &&
612  {
613  /* don't flush too frequently */
614  if (pending_since == 0)
615  pending_since = now;
616 
617  return PGSTAT_IDLE_INTERVAL;
618  }
619  }
620 
622 
623  /* don't wait for lock acquisition when !force */
624  nowait = !force;
625 
626  partial_flush = false;
627 
628  /* flush database / relation / function / ... stats */
629  partial_flush |= pgstat_flush_pending_entries(nowait);
630 
631  /* flush wal stats */
632  partial_flush |= pgstat_flush_wal(nowait);
633 
634  /* flush SLRU stats */
635  partial_flush |= pgstat_slru_flush(nowait);
636 
637  last_flush = now;
638 
639  /*
640  * If some of the pending stats could not be flushed due to lock
641  * contention, let the caller know when to retry.
642  */
643  if (partial_flush)
644  {
645  /* force should have prevented us from getting here */
646  Assert(!force);
647 
648  /* remember since when stats have been pending */
649  if (pending_since == 0)
650  pending_since = now;
651 
652  return PGSTAT_IDLE_INTERVAL;
653  }
654 
655  pending_since = 0;
656 
657  return 0;
658 }
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1731
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1549
int64 TimestampTz
Definition: timestamp.h:39
#define PGSTAT_MIN_INTERVAL
Definition: pgstat.c:119
static bool pgstat_flush_pending_entries(bool nowait)
Definition: pgstat.c:1137
#define PGSTAT_MAX_INTERVAL
Definition: pgstat.c:121
#define PGSTAT_IDLE_INTERVAL
Definition: pgstat.c:123
void pgstat_update_dbstats(TimestampTz ts)
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:144
bool pgstat_flush_wal(bool nowait)
Definition: pgstat_wal.c:68
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4814
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition: xact.c:875

References Assert(), dlist_is_empty(), GetCurrentTransactionStopTimestamp(), have_slrustats, PgStat_ShmemControl::is_shutdown, IsTransactionOrTransactionBlock(), now(), pgstat_assert_is_up, 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(), and ProcessInterrupts().

◆ pgstat_reset()

void pgstat_reset ( PgStat_Kind  kind,
Oid  dboid,
Oid  objoid 
)

Definition at line 705 of file pgstat.c.

706 {
707  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
709 
710  /* not needed atm, and doesn't make sense with the current signature */
711  Assert(!pgstat_get_kind_info(kind)->fixed_amount);
712 
713  /* reset the "single counter" */
714  pgstat_reset_entry(kind, dboid, objoid, ts);
715 
716  if (!kind_info->accessed_across_databases)
718 }
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:929

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

1630 {
1632 
1633  /* reset fixed-numbered stats */
1634  for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1635  {
1636  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1637 
1638  if (!kind_info->fixed_amount)
1639  continue;
1640 
1641  kind_info->reset_all_cb(ts);
1642  }
1643 
1644  /* and drop variable-numbered ones */
1646 }
void pgstat_drop_all_entries(void)
Definition: pgstat_shmem.c:891
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 686 of file pgstat.c.

687 {
689 
692  ts);
693 }
static bool match_db_entries(PgStatShared_HashEntry *entry, Datum match_data)
Definition: pgstat.c:674
void pgstat_reset_matching_entries(bool(*do_reset)(PgStatShared_HashEntry *, Datum), Datum match_data, TimestampTz ts)
Definition: pgstat_shmem.c:949
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:600

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

728 {
729  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
731 
732  if (kind_info->fixed_amount)
733  kind_info->reset_all_cb(ts);
734  else
736 }
void pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts)
Definition: pgstat_shmem.c:985

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

394 {
396 }
static void pgstat_read_statsfile(void)
Definition: pgstat.c:1434

References pgstat_read_statsfile().

Referenced by StartupXLOG().

◆ pgstat_shutdown_hook()

static void pgstat_shutdown_hook ( int  code,
Datum  arg 
)
static

Definition at line 489 of file pgstat.c.

490 {
491  Assert(!pgstat_is_shutdown);
493 
494  /*
495  * If we got as far as discovering our own database ID, we can flush out
496  * what we did so far. Otherwise, we'd be reporting an invalid database
497  * ID, so forget it. (This means that accesses to pg_database during
498  * failed backend starts might never get counted.)
499  */
502 
503  pgstat_report_stat(true);
504 
505  /* there shouldn't be any pending changes left */
508 
510 
511 #ifdef USE_ASSERT_CHECKING
512  pgstat_is_shutdown = true;
513 #endif
514 }
#define OidIsValid(objectId)
Definition: c.h:711
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:233

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

1266 {
1267  FILE *fpout;
1268  int32 format_id;
1269  const char *tmpfile = PGSTAT_STAT_PERMANENT_TMPFILE;
1270  const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
1271  dshash_seq_status hstat;
1273 
1275 
1276  /* we're shutting down, so it's ok to just override this */
1278 
1279  elog(DEBUG2, "writing stats file \"%s\"", statfile);
1280 
1281  /*
1282  * Open the statistics temp file to write out the current values.
1283  */
1284  fpout = AllocateFile(tmpfile, PG_BINARY_W);
1285  if (fpout == NULL)
1286  {
1287  ereport(LOG,
1289  errmsg("could not open temporary statistics file \"%s\": %m",
1290  tmpfile)));
1291  return;
1292  }
1293 
1294  /*
1295  * Write the file header --- currently just a format ID.
1296  */
1297  format_id = PGSTAT_FILE_FORMAT_ID;
1298  write_chunk_s(fpout, &format_id);
1299 
1300  /*
1301  * XXX: The following could now be generalized to just iterate over
1302  * pgstat_kind_infos instead of knowing about the different kinds of
1303  * stats.
1304  */
1305 
1306  /*
1307  * Write archiver stats struct
1308  */
1311 
1312  /*
1313  * Write bgwriter stats struct
1314  */
1317 
1318  /*
1319  * Write checkpointer stats struct
1320  */
1323 
1324  /*
1325  * Write SLRU stats struct
1326  */
1329 
1330  /*
1331  * Write WAL stats struct
1332  */
1335 
1336  /*
1337  * Walk through the stats entries
1338  */
1339  dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
1340  while ((ps = dshash_seq_next(&hstat)) != NULL)
1341  {
1342  PgStatShared_Common *shstats;
1343  const PgStat_KindInfo *kind_info = NULL;
1344 
1346 
1347  /* we may have some "dropped" entries not yet removed, skip them */
1348  Assert(!ps->dropped);
1349  if (ps->dropped)
1350  continue;
1351 
1353 
1354  kind_info = pgstat_get_kind_info(ps->key.kind);
1355 
1356  /* if not dropped the valid-entry refcount should exist */
1357  Assert(pg_atomic_read_u32(&ps->refcount) > 0);
1358 
1359  if (!kind_info->to_serialized_name)
1360  {
1361  /* normal stats entry, identified by PgStat_HashKey */
1362  fputc('S', fpout);
1363  write_chunk_s(fpout, &ps->key);
1364  }
1365  else
1366  {
1367  /* stats entry identified by name on disk (e.g. slots) */
1368  NameData name;
1369 
1370  kind_info->to_serialized_name(&ps->key, shstats, &name);
1371 
1372  fputc('N', fpout);
1373  write_chunk_s(fpout, &ps->key.kind);
1374  write_chunk_s(fpout, &name);
1375  }
1376 
1377  /* Write except the header part of the entry */
1378  write_chunk(fpout,
1379  pgstat_get_entry_data(ps->key.kind, shstats),
1381  }
1382  dshash_seq_term(&hstat);
1383 
1384  /*
1385  * No more output to be done. Close the temp file and replace the old
1386  * pgstat.stat with it. The ferror() check replaces testing for error
1387  * after each individual fputc or fwrite (in write_chunk()) above.
1388  */
1389  fputc('E', fpout);
1390 
1391  if (ferror(fpout))
1392  {
1393  ereport(LOG,
1395  errmsg("could not write temporary statistics file \"%s\": %m",
1396  tmpfile)));
1397  FreeFile(fpout);
1398  unlink(tmpfile);
1399  }
1400  else if (FreeFile(fpout) < 0)
1401  {
1402  ereport(LOG,
1404  errmsg("could not close temporary statistics file \"%s\": %m",
1405  tmpfile)));
1406  unlink(tmpfile);
1407  }
1408  else if (rename(tmpfile, statfile) < 0)
1409  {
1410  ereport(LOG,
1412  errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
1413  tmpfile, statfile)));
1414  unlink(tmpfile);
1415  }
1416 }
#define PG_BINARY_W
Definition: c.h:1212
static void write_chunk(FILE *fpout, void *ptr, size_t len)
Definition: pgstat.c:1248
#define write_chunk_s(fpout, ptr)
Definition: pgstat.c:1258
@ PGSTAT_KIND_ARCHIVER
Definition: pgstat.h:48
@ PGSTAT_KIND_SLRU
Definition: pgstat.h:51
@ PGSTAT_KIND_BGWRITER
Definition: pgstat.h:49
@ PGSTAT_KIND_CHECKPOINTER
Definition: pgstat.h:50
@ PGSTAT_KIND_WAL
Definition: pgstat.h:52
#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, PgStatShared_HashEntry::body, CHECK_FOR_INTERRUPTS, PgStat_Snapshot::checkpointer, DEBUG2, PgStatShared_HashEntry::dropped, PgStat_LocalState::dsa, dsa_get_address(), dshash_seq_init(), dshash_seq_next(), dshash_seq_term(), elog(), ereport, errcode_for_file_access(), errmsg(), FreeFile(), PgStatShared_HashEntry::key, PgStat_HashKey::kind, 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_SLRU, PGSTAT_KIND_WAL, PGSTAT_STAT_PERMANENT_FILENAME, PGSTAT_STAT_PERMANENT_TMPFILE, pgStatLocal, PgStatShared_HashEntry::refcount, 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 1420 of file pgstat.c.

1421 {
1422  return fread(ptr, 1, len, fpin) == len;
1423 }
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 1248 of file pgstat.c.

1249 {
1250  int rc;
1251 
1252  rc = fwrite(ptr, len, 1, fpout);
1253 
1254  /* we'll check for errors with ferror once at the end */
1255  (void) rc;
1256 }

References len.

Referenced by pgstat_write_statsfile().

Variable Documentation

◆ pgstat_fetch_consistency

◆ pgstat_kind_infos

const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS]
static

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

Referenced by pgstat_force_next_flush(), and pgstat_report_stat().

◆ pgStatLocal

◆ pgStatPending

dlist_head pgStatPending = DLIST_STATIC_INIT(pgStatPending)
static

◆ pgStatPendingContext

MemoryContext pgStatPendingContext = NULL
static

Definition at line 212 of file pgstat.c.

Referenced by pgstat_prep_pending_entry().