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

◆ PGSTAT_MAX_INTERVAL

#define PGSTAT_MAX_INTERVAL   60000

Definition at line 122 of file pgstat.c.

◆ PGSTAT_MIN_INTERVAL

#define PGSTAT_MIN_INTERVAL   1000

Definition at line 120 of file pgstat.c.

◆ PGSTAT_SNAPSHOT_HASH_SIZE

#define PGSTAT_SNAPSHOT_HASH_SIZE   512

Definition at line 131 of file pgstat.c.

◆ read_chunk_s

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

Definition at line 1463 of file pgstat.c.

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 159 of file pgstat.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 158 of file pgstat.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   PgStat_SnapshotEntry

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

◆ SH_KEY

#define SH_KEY   key

Definition at line 152 of file pgstat.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   PgStat_HashKey

Definition at line 151 of file pgstat.c.

◆ SH_PREFIX

#define SH_PREFIX   pgstat_snapshot

Definition at line 149 of file pgstat.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 157 of file pgstat.c.

◆ write_chunk_s

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

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

1697 {
1698  /*
1699  * Changing this value in a transaction may cause snapshot state
1700  * inconsistencies, so force a clear of the current snapshot on the next
1701  * snapshot build attempt.
1702  */
1705 }
#define newval
int pgstat_fetch_consistency
Definition: pgstat.c:188
static bool force_stats_snapshot_clear
Definition: pgstat.c:234

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

696 {
697  return entry->key.dboid == DatumGetObjectId(MyDatabaseId);
698 }
Oid MyDatabaseId
Definition: globals.c:89
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 465 of file pgstat.c.

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

966 {
967  dshash_seq_status hstat;
969 
970  /* should only be called when we need a snapshot */
972 
973  /* snapshot already built */
975  return;
976 
978 
979  Assert(pgStatLocal.snapshot.stats->members == 0);
980 
982 
983  /*
984  * Snapshot all variable stats.
985  */
986  dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
987  while ((p = dshash_seq_next(&hstat)) != NULL)
988  {
989  PgStat_Kind kind = p->key.kind;
990  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
991  bool found;
992  PgStat_SnapshotEntry *entry;
993  PgStatShared_Common *stats_data;
994 
995  /*
996  * Check if the stats object should be included in the snapshot.
997  * Unless the stats kind can be accessed from all databases (e.g.,
998  * database stats themselves), we only include stats for the current
999  * database or objects not associated with a database (e.g. shared
1000  * relations).
1001  */
1002  if (p->key.dboid != MyDatabaseId &&
1003  p->key.dboid != InvalidOid &&
1004  !kind_info->accessed_across_databases)
1005  continue;
1006 
1007  if (p->dropped)
1008  continue;
1009 
1011 
1012  stats_data = dsa_get_address(pgStatLocal.dsa, p->body);
1013  Assert(stats_data);
1014 
1015  entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, p->key, &found);
1016  Assert(!found);
1017 
1019  kind_info->shared_size);
1020 
1021  /*
1022  * Acquire the LWLock directly instead of using
1023  * pg_stat_lock_entry_shared() which requires a reference.
1024  */
1025  LWLockAcquire(&stats_data->lock, LW_SHARED);
1026  memcpy(entry->data,
1027  pgstat_get_entry_data(kind, stats_data),
1028  kind_info->shared_size);
1029  LWLockRelease(&stats_data->lock);
1030  }
1031  dshash_seq_term(&hstat);
1032 
1033  /*
1034  * Build snapshot of all fixed-numbered stats.
1035  */
1036  for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1037  {
1038  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1039 
1040  if (!kind_info->fixed_amount)
1041  {
1042  Assert(kind_info->snapshot_cb == NULL);
1043  continue;
1044  }
1045 
1047  }
1048 
1050 }
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:236
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1583
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:1195
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
@ LW_SHARED
Definition: lwlock.h:117
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1021
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1252
static void pgstat_prep_snapshot(void)
Definition: pgstat.c:944
static void pgstat_build_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1053
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 1053 of file pgstat.c.

1054 {
1055  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1056 
1057  Assert(kind_info->fixed_amount);
1058  Assert(kind_info->snapshot_cb != NULL);
1059 
1061  {
1062  /* rebuild every time */
1063  pgStatLocal.snapshot.fixed_valid[kind] = false;
1064  }
1065  else if (pgStatLocal.snapshot.fixed_valid[kind])
1066  {
1067  /* in snapshot mode we shouldn't get called again */
1069  return;
1070  }
1071 
1073 
1074  kind_info->snapshot_cb();
1075 
1077  pgStatLocal.snapshot.fixed_valid[kind] = true;
1078 }
@ 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 774 of file pgstat.c.

775 {
777 
778  memset(&pgStatLocal.snapshot.fixed_valid, 0,
780  pgStatLocal.snapshot.stats = NULL;
782 
783  /* Release memory, if any was allocated */
785  {
787 
788  /* Reset variables */
790  }
791 
792  /*
793  * Historically the backend_status.c facilities lived in this file, and
794  * were reset with the same function. For now keep it that way, and
795  * forward the reset request.
796  */
798 
799  /* Reset this flag, as it may be possible that a cleanup was forced. */
801 }
void pgstat_clear_backend_activity_snapshot(void)
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:403
#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(), and PostPrepare_PgStat().

◆ pgstat_delete_pending_entry()

void pgstat_delete_pending_entry ( PgStat_EntryRef entry_ref)

Definition at line 1145 of file pgstat.c.

1146 {
1147  PgStat_Kind kind = entry_ref->shared_entry->key.kind;
1148  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1149  void *pending_data = entry_ref->pending;
1150 
1151  Assert(pending_data != NULL);
1152  /* !fixed_amount stats should be handled explicitly */
1153  Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1154 
1155  if (kind_info->delete_pending_cb)
1156  kind_info->delete_pending_cb(entry_ref);
1157 
1158  pfree(pending_data);
1159  entry_ref->pending = NULL;
1160 
1161  dlist_delete(&entry_ref->pending_node);
1162 }
static void dlist_delete(dlist_node *node)
Definition: ilist.h:405
void pfree(void *pointer)
Definition: mcxt.c:1456
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 422 of file pgstat.c.

423 {
424  int ret;
425 
426  /* NB: this needs to be done even in single user mode */
427 
428  ret = unlink(PGSTAT_STAT_PERMANENT_FILENAME);
429  if (ret != 0)
430  {
431  if (errno == ENOENT)
432  elog(DEBUG2,
433  "didn't need to unlink permanent stats file \"%s\" - didn't exist",
435  else
436  ereport(LOG,
438  errmsg("could not unlink permanent statistics file \"%s\": %m",
440  }
441  else
442  {
443  ereport(DEBUG2,
445  errmsg_internal("unlinked permanent statistics file \"%s\"",
447  }
448 
449  /*
450  * Reset stats contents. This will set reset timestamps of fixed-numbered
451  * stats to the current time (no variable stats exist).
452  */
454 }
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:1673
#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 804 of file pgstat.c.

805 {
807  PgStat_EntryRef *entry_ref;
808  void *stats_data;
809  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
810 
811  /* should be called from backends */
813  Assert(!kind_info->fixed_amount);
814 
816 
817  key.kind = kind;
818  key.dboid = dboid;
819  key.objoid = objoid;
820 
821  /* if we need to build a full snapshot, do so */
824 
825  /* if caching is desired, look up in cache */
827  {
828  PgStat_SnapshotEntry *entry = NULL;
829 
830  entry = pgstat_snapshot_lookup(pgStatLocal.snapshot.stats, key);
831 
832  if (entry)
833  return entry->data;
834 
835  /*
836  * If we built a full snapshot and the key is not in
837  * pgStatLocal.snapshot.stats, there are no matching stats.
838  */
840  return NULL;
841  }
842 
844 
845  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
846 
847  if (entry_ref == NULL || entry_ref->shared_entry->dropped)
848  {
849  /* create empty entry when using PGSTAT_FETCH_CONSISTENCY_CACHE */
851  {
852  PgStat_SnapshotEntry *entry = NULL;
853  bool found;
854 
855  entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
856  Assert(!found);
857  entry->data = NULL;
858  }
859  return NULL;
860  }
861 
862  /*
863  * Allocate in caller's context for PGSTAT_FETCH_CONSISTENCY_NONE,
864  * otherwise we could quickly end up with a fair bit of memory used due to
865  * repeated accesses.
866  */
868  stats_data = palloc(kind_info->shared_data_len);
869  else
871  kind_info->shared_data_len);
872 
873  pgstat_lock_entry_shared(entry_ref, false);
874  memcpy(stats_data,
875  pgstat_get_entry_data(kind, entry_ref->shared_stats),
876  kind_info->shared_data_len);
877  pgstat_unlock_entry(entry_ref);
878 
880  {
881  PgStat_SnapshotEntry *entry = NULL;
882  bool found;
883 
884  entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
885  entry->data = stats_data;
886  }
887 
888  return stats_data;
889 }
bool IsUnderPostmaster
Definition: globals.c:113
bool IsPostmasterEnvironment
Definition: globals.c:112
void * palloc(Size size)
Definition: mcxt.c:1226
static void pgstat_build_snapshot(void)
Definition: pgstat.c:965
bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:591
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:397
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:603
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 1132 of file pgstat.c.

1133 {
1134  PgStat_EntryRef *entry_ref;
1135 
1136  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
1137 
1138  if (entry_ref == NULL || entry_ref->pending == NULL)
1139  return NULL;
1140 
1141  return entry_ref;
1142 }

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

1170 {
1171  bool have_pending = false;
1172  dlist_node *cur = NULL;
1173 
1174  /*
1175  * Need to be a bit careful iterating over the list of pending entries.
1176  * Processing a pending entry may queue further pending entries to the end
1177  * of the list that we want to process, so a simple iteration won't do.
1178  * Further complicating matters is that we want to delete the current
1179  * entry in each iteration from the list if we flushed successfully.
1180  *
1181  * So we just keep track of the next pointer in each loop iteration.
1182  */
1185 
1186  while (cur)
1187  {
1188  PgStat_EntryRef *entry_ref =
1189  dlist_container(PgStat_EntryRef, pending_node, cur);
1190  PgStat_HashKey key = entry_ref->shared_entry->key;
1191  PgStat_Kind kind = key.kind;
1192  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1193  bool did_flush;
1194  dlist_node *next;
1195 
1196  Assert(!kind_info->fixed_amount);
1197  Assert(kind_info->flush_pending_cb != NULL);
1198 
1199  /* flush the stats, if possible */
1200  did_flush = kind_info->flush_pending_cb(entry_ref, nowait);
1201 
1202  Assert(did_flush || nowait);
1203 
1204  /* determine next entry, before deleting the pending entry */
1207  else
1208  next = NULL;
1209 
1210  /* if successfully flushed, remove entry */
1211  if (did_flush)
1212  pgstat_delete_pending_entry(entry_ref);
1213  else
1214  have_pending = true;
1215 
1216  cur = next;
1217  }
1218 
1219  Assert(dlist_is_empty(&pgStatPending) == !have_pending);
1220 
1221  return have_pending;
1222 }
static int32 next
Definition: blutils.c:219
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:1145
static dlist_head pgStatPending
Definition: pgstat.c:221
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 686 of file pgstat.c.

687 {
688  pgStatForceNextFlush = true;
689 }
static bool pgStatForceNextFlush
Definition: pgstat.c:228

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

1232 {
1233  for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1234  {
1235  if (pg_strcasecmp(kind_str, pgstat_kind_infos[kind].name) == 0)
1236  return kind;
1237  }
1238 
1239  ereport(ERROR,
1240  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1241  errmsg("invalid statistics kind: \"%s\"", kind_str)));
1242  return PGSTAT_KIND_DATABASE; /* avoid compiler warnings */
1243 }
int errcode(int sqlerrcode)
Definition: elog.c:858
#define ERROR
Definition: elog.h:39
const char * name
Definition: encode.c:571
static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS]
Definition: pgstat.c:260
@ 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 897 of file pgstat.c.

898 {
901 
903  {
904  *have_snapshot = true;
906  }
907 
908  *have_snapshot = false;
909 
910  return 0;
911 }
void pgstat_clear_snapshot(void)
Definition: pgstat.c:774

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

915 {
916  /* fixed-numbered stats always exist */
917  if (pgstat_get_kind_info(kind)->fixed_amount)
918  return true;
919 
920  return pgstat_get_entry_ref(kind, dboid, objoid, false, NULL) != NULL;
921 }

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

541 {
542  Assert(!pgstat_is_initialized);
543 
545 
546  pgstat_init_wal();
547 
548  /* Set up a process-exit hook to clean up */
550 
551 #ifdef USE_ASSERT_CHECKING
552  pgstat_is_initialized = true;
553 #endif
554 }
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:506
void pgstat_attach_shmem(void)
Definition: pgstat_shmem.c:217
void pgstat_init_wal(void)
Definition: pgstat_wal.c:129

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

1247 {
1248  return ikind >= PGSTAT_KIND_FIRST_VALID && ikind <= PGSTAT_KIND_LAST;
1249 }

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

1095 {
1096  PgStat_EntryRef *entry_ref;
1097 
1098  /* need to be able to flush out */
1099  Assert(pgstat_get_kind_info(kind)->flush_pending_cb != NULL);
1100 
1102  {
1105  "PgStat Pending",
1107  }
1108 
1109  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid,
1110  true, created_entry);
1111 
1112  if (entry_ref->pending == NULL)
1113  {
1114  size_t entrysize = pgstat_get_kind_info(kind)->pending_size;
1115 
1116  Assert(entrysize != (size_t) -1);
1117 
1118  entry_ref->pending = MemoryContextAllocZero(pgStatPendingContext, entrysize);
1120  }
1121 
1122  return entry_ref;
1123 }
#define unlikely(x)
Definition: c.h:295
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:364
MemoryContext TopMemoryContext
Definition: mcxt.c:141
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1064
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:163
static MemoryContext pgStatPendingContext
Definition: pgstat.c:213

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

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

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

583 {
584  static TimestampTz pending_since = 0;
585  static TimestampTz last_flush = 0;
586  bool partial_flush;
588  bool nowait;
589 
592 
593  /* "absorb" the forced flush even if there's nothing to flush */
595  {
596  force = true;
597  pgStatForceNextFlush = false;
598  }
599 
600  /* Don't expend a clock check if nothing to do */
602  !have_iostats &&
603  !have_slrustats &&
605  {
606  Assert(pending_since == 0);
607  return 0;
608  }
609 
610  /*
611  * There should never be stats to report once stats are shut down. Can't
612  * assert that before the checks above, as there is an unconditional
613  * pgstat_report_stat() call in pgstat_shutdown_hook() - which at least
614  * the process that ran pgstat_before_server_shutdown() will still call.
615  */
617 
619 
620  if (!force)
621  {
622  if (pending_since > 0 &&
624  {
625  /* don't keep pending updates longer than PGSTAT_MAX_INTERVAL */
626  force = true;
627  }
628  else if (last_flush > 0 &&
630  {
631  /* don't flush too frequently */
632  if (pending_since == 0)
633  pending_since = now;
634 
635  return PGSTAT_IDLE_INTERVAL;
636  }
637  }
638 
640 
641  /* don't wait for lock acquisition when !force */
642  nowait = !force;
643 
644  partial_flush = false;
645 
646  /* flush database / relation / function / ... stats */
647  partial_flush |= pgstat_flush_pending_entries(nowait);
648 
649  /* flush IO stats */
650  partial_flush |= pgstat_flush_io(nowait);
651 
652  /* flush wal stats */
653  partial_flush |= pgstat_flush_wal(nowait);
654 
655  /* flush SLRU stats */
656  partial_flush |= pgstat_slru_flush(nowait);
657 
658  last_flush = now;
659 
660  /*
661  * If some of the pending stats could not be flushed due to lock
662  * contention, let the caller know when to retry.
663  */
664  if (partial_flush)
665  {
666  /* force should have prevented us from getting here */
667  Assert(!force);
668 
669  /* remember since when stats have been pending */
670  if (pending_since == 0)
671  pending_since = now;
672 
673  return PGSTAT_IDLE_INTERVAL;
674  }
675 
676  pending_since = 0;
677 
678  return 0;
679 }
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1719
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1547
int64 TimestampTz
Definition: timestamp.h:39
#define PGSTAT_MIN_INTERVAL
Definition: pgstat.c:120
static bool pgstat_flush_pending_entries(bool nowait)
Definition: pgstat.c:1169
#define PGSTAT_MAX_INTERVAL
Definition: pgstat.c:122
#define PGSTAT_IDLE_INTERVAL
Definition: pgstat.c:124
void pgstat_update_dbstats(TimestampTz ts)
bool pgstat_flush_io(bool nowait)
Definition: pgstat_io.c:159
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:147
bool pgstat_flush_wal(bool nowait)
Definition: pgstat_wal.c:70
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4833
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition: xact.c:876

References Assert(), dlist_is_empty(), 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(), and worker_spi_main().

◆ pgstat_reset()

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

Definition at line 726 of file pgstat.c.

727 {
728  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
730 
731  /* not needed atm, and doesn't make sense with the current signature */
732  Assert(!pgstat_get_kind_info(kind)->fixed_amount);
733 
734  /* reset the "single counter" */
735  pgstat_reset_entry(kind, dboid, objoid, ts);
736 
737  if (!kind_info->accessed_across_databases)
739 }
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:933

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

1674 {
1676 
1677  /* reset fixed-numbered stats */
1678  for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1679  {
1680  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1681 
1682  if (!kind_info->fixed_amount)
1683  continue;
1684 
1685  kind_info->reset_all_cb(ts);
1686  }
1687 
1688  /* and drop variable-numbered ones */
1690 }
void pgstat_drop_all_entries(void)
Definition: pgstat_shmem.c:895
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 707 of file pgstat.c.

708 {
710 
713  ts);
714 }
static bool match_db_entries(PgStatShared_HashEntry *entry, Datum match_data)
Definition: pgstat.c:695
void pgstat_reset_matching_entries(bool(*do_reset)(PgStatShared_HashEntry *, Datum), Datum match_data, TimestampTz ts)
Definition: pgstat_shmem.c:953
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 748 of file pgstat.c.

749 {
750  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
752 
753  if (kind_info->fixed_amount)
754  kind_info->reset_all_cb(ts);
755  else
757 }
void pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts)
Definition: pgstat_shmem.c:989

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

411 {
413 }
static void pgstat_read_statsfile(void)
Definition: pgstat.c:1472

References pgstat_read_statsfile().

Referenced by StartupXLOG().

◆ pgstat_shutdown_hook()

static void pgstat_shutdown_hook ( int  code,
Datum  arg 
)
static

Definition at line 506 of file pgstat.c.

507 {
508  Assert(!pgstat_is_shutdown);
510 
511  /*
512  * If we got as far as discovering our own database ID, we can flush out
513  * what we did so far. Otherwise, we'd be reporting an invalid database
514  * ID, so forget it. (This means that accesses to pg_database during
515  * failed backend starts might never get counted.)
516  */
519 
520  pgstat_report_stat(true);
521 
522  /* there shouldn't be any pending changes left */
525 
527 
528 #ifdef USE_ASSERT_CHECKING
529  pgstat_is_shutdown = true;
530 #endif
531 }
#define OidIsValid(objectId)
Definition: c.h:759
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:237

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

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

1459 {
1460  return fread(ptr, 1, len, fpin) == len;
1461 }
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 1280 of file pgstat.c.

1281 {
1282  int rc;
1283 
1284  rc = fwrite(ptr, len, 1, fpout);
1285 
1286  /* we'll check for errors with ferror once at the end */
1287  (void) rc;
1288 }

References len.

Referenced by pgstat_write_statsfile().

Variable Documentation

◆ force_stats_snapshot_clear

bool force_stats_snapshot_clear = false
static

◆ pgstat_fetch_consistency

◆ pgstat_kind_infos

const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS]
static

Definition at line 260 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 228 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 213 of file pgstat.c.

Referenced by pgstat_prep_pending_entry().