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

Go to the source code of this file.

Data Structures

struct  PgStat_SnapshotEntry
 

Macros

#define PGSTAT_MIN_INTERVAL   1000
 
#define PGSTAT_MAX_INTERVAL   60000
 
#define PGSTAT_IDLE_INTERVAL   10000
 
#define PGSTAT_SNAPSHOT_HASH_SIZE   512
 
#define PGSTAT_FILE_ENTRY_END   'E' /* end of file */
 
#define PGSTAT_FILE_ENTRY_FIXED   'F' /* fixed-numbered stats entry */
 
#define PGSTAT_FILE_ENTRY_NAME   'N' /* stats entry identified by name */
 
#define PGSTAT_FILE_ENTRY_HASH
 
#define SH_PREFIX   pgstat_snapshot
 
#define SH_ELEMENT_TYPE   PgStat_SnapshotEntry
 
#define SH_KEY_TYPE   PgStat_HashKey
 
#define SH_KEY   key
 
#define SH_HASH_KEY(tb, key)    pgstat_hash_hash_key(&key, sizeof(PgStat_HashKey), NULL)
 
#define SH_EQUAL(tb, a, b)    pgstat_cmp_hash_key(&a, &b, sizeof(PgStat_HashKey), NULL) == 0
 
#define SH_SCOPE   static inline
 
#define SH_DEFINE
 
#define SH_DECLARE
 
#define write_chunk_s(fpout, ptr)   write_chunk(fpout, ptr, sizeof(*ptr))
 
#define read_chunk_s(fpin, ptr)   read_chunk(fpin, ptr, sizeof(*ptr))
 

Typedefs

typedef struct PgStat_SnapshotEntry PgStat_SnapshotEntry
 

Functions

static void pgstat_write_statsfile (void)
 
static void pgstat_read_statsfile (void)
 
static void pgstat_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_FILE_ENTRY_END

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

Definition at line 134 of file pgstat.c.

◆ PGSTAT_FILE_ENTRY_FIXED

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

Definition at line 135 of file pgstat.c.

◆ PGSTAT_FILE_ENTRY_HASH

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

Definition at line 137 of file pgstat.c.

◆ PGSTAT_FILE_ENTRY_NAME

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

Definition at line 136 of file pgstat.c.

◆ PGSTAT_IDLE_INTERVAL

#define PGSTAT_IDLE_INTERVAL   10000

Definition at line 121 of file pgstat.c.

◆ PGSTAT_MAX_INTERVAL

#define PGSTAT_MAX_INTERVAL   60000

Definition at line 119 of file pgstat.c.

◆ PGSTAT_MIN_INTERVAL

#define PGSTAT_MIN_INTERVAL   1000

Definition at line 117 of file pgstat.c.

◆ PGSTAT_SNAPSHOT_HASH_SIZE

#define PGSTAT_SNAPSHOT_HASH_SIZE   512

Definition at line 128 of file pgstat.c.

◆ read_chunk_s

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

Definition at line 1494 of file pgstat.c.

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 164 of file pgstat.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 163 of file pgstat.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   PgStat_SnapshotEntry

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

◆ SH_KEY

#define SH_KEY   key

Definition at line 157 of file pgstat.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   PgStat_HashKey

Definition at line 156 of file pgstat.c.

◆ SH_PREFIX

#define SH_PREFIX   pgstat_snapshot

Definition at line 154 of file pgstat.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 162 of file pgstat.c.

◆ write_chunk_s

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

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

1718 {
1719  /*
1720  * Changing this value in a transaction may cause snapshot state
1721  * inconsistencies, so force a clear of the current snapshot on the next
1722  * snapshot build attempt.
1723  */
#define newval
int pgstat_fetch_consistency
Definition: pgstat.c:193
static bool force_stats_snapshot_clear
Definition: pgstat.c:239

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

748 {
749  return entry->key.dboid == DatumGetObjectId(MyDatabaseId);
Oid MyDatabaseId
Definition: globals.c:92
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 505 of file pgstat.c.

507 {
508  Assert(pgStatLocal.shmem != NULL);
510 
511  /*
512  * Stats should only be reported after pgstat_initialize() and before
513  * pgstat_shutdown(). This is a convenient point to catch most violations
514  * of this rule.
515  */
516  Assert(pgstat_is_initialized && !pgstat_is_shutdown);
517 
518  /* flush out our own pending changes before writing out */
519  pgstat_report_stat(true);
520 
521  /*
522  * Only write out file during normal shutdown. Don't even signal that
523  * we've shutdown during irregular shutdowns, because the shutdown
524  * sequence isn't coordinated to ensure this backend shuts down last.
525  */
526  if (code == 0)
527  {
528  pgStatLocal.shmem->is_shutdown = true;
530  }
#define Assert(condition)
Definition: c.h:858
static void pgstat_write_statsfile(void)
Definition: pgstat.c:1351
long pgstat_report_stat(bool force)
Definition: pgstat.c:622
PgStat_LocalState pgStatLocal
Definition: pgstat.c:201
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 1019 of file pgstat.c.

1021 {
1022  dshash_seq_status hstat;
1024 
1025  /* should only be called when we need a snapshot */
1027 
1028  /* snapshot already built */
1030  return;
1031 
1033 
1034  Assert(pgStatLocal.snapshot.stats->members == 0);
1035 
1037 
1038  /*
1039  * Snapshot all variable stats.
1040  */
1041  dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
1042  while ((p = dshash_seq_next(&hstat)) != NULL)
1043  {
1044  PgStat_Kind kind = p->key.kind;
1045  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1046  bool found;
1047  PgStat_SnapshotEntry *entry;
1048  PgStatShared_Common *stats_data;
1049 
1050  /*
1051  * Check if the stats object should be included in the snapshot.
1052  * Unless the stats kind can be accessed from all databases (e.g.,
1053  * database stats themselves), we only include stats for the current
1054  * database or objects not associated with a database (e.g. shared
1055  * relations).
1056  */
1057  if (p->key.dboid != MyDatabaseId &&
1058  p->key.dboid != InvalidOid &&
1059  !kind_info->accessed_across_databases)
1060  continue;
1061 
1062  if (p->dropped)
1063  continue;
1064 
1066 
1067  stats_data = dsa_get_address(pgStatLocal.dsa, p->body);
1068  Assert(stats_data);
1069 
1070  entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, p->key, &found);
1071  Assert(!found);
1072 
1074  kind_info->shared_size);
1075 
1076  /*
1077  * Acquire the LWLock directly instead of using
1078  * pg_stat_lock_entry_shared() which requires a reference.
1079  */
1080  LWLockAcquire(&stats_data->lock, LW_SHARED);
1081  memcpy(entry->data,
1082  pgstat_get_entry_data(kind, stats_data),
1083  kind_info->shared_size);
1084  LWLockRelease(&stats_data->lock);
1085  }
1086  dshash_seq_term(&hstat);
1087 
1088  /*
1089  * Build snapshot of all fixed-numbered stats.
1090  */
1091  for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1092  {
1093  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1094 
1095  if (!kind_info->fixed_amount)
1096  {
1097  Assert(kind_info->snapshot_cb == NULL);
1098  continue;
1099  }
1100 
1102  }
1103 
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:232
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1655
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:942
void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table, bool exclusive)
Definition: dshash.c:638
void dshash_seq_term(dshash_seq_status *status)
Definition: dshash.c:747
void * dshash_seq_next(dshash_seq_status *status)
Definition: dshash.c:657
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
@ LW_SHARED
Definition: lwlock.h:115
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1306
static void pgstat_prep_snapshot(void)
Definition: pgstat.c:998
static void pgstat_build_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1107
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 1107 of file pgstat.c.

1109 {
1110  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1111 
1112  Assert(kind_info->fixed_amount);
1113  Assert(kind_info->snapshot_cb != NULL);
1114 
1116  {
1117  /* rebuild every time */
1118  pgStatLocal.snapshot.fixed_valid[kind] = false;
1119  }
1120  else if (pgStatLocal.snapshot.fixed_valid[kind])
1121  {
1122  /* in snapshot mode we shouldn't get called again */
1124  return;
1125  }
1126 
1128 
1129  kind_info->snapshot_cb();
1130 
1132  pgStatLocal.snapshot.fixed_valid[kind] = true;
@ 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 825 of file pgstat.c.

827 {
829 
830  memset(&pgStatLocal.snapshot.fixed_valid, 0,
832  pgStatLocal.snapshot.stats = NULL;
834 
835  /* Release memory, if any was allocated */
837  {
839 
840  /* Reset variables */
842  }
843 
844  /*
845  * Historically the backend_status.c facilities lived in this file, and
846  * were reset with the same function. For now keep it that way, and
847  * forward the reset request.
848  */
850 
851  /* Reset this flag, as it may be possible that a cleanup was forced. */
void pgstat_clear_backend_activity_snapshot(void)
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
#define pgstat_assert_is_up()

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

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

◆ pgstat_delete_pending_entry()

void pgstat_delete_pending_entry ( PgStat_EntryRef entry_ref)

Definition at line 1199 of file pgstat.c.

1201 {
1202  PgStat_Kind kind = entry_ref->shared_entry->key.kind;
1203  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1204  void *pending_data = entry_ref->pending;
1205 
1206  Assert(pending_data != NULL);
1207  /* !fixed_amount stats should be handled explicitly */
1208  Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1209 
1210  if (kind_info->delete_pending_cb)
1211  kind_info->delete_pending_cb(entry_ref);
1212 
1213  pfree(pending_data);
1214  entry_ref->pending = NULL;
1215 
1216  dlist_delete(&entry_ref->pending_node);
static void dlist_delete(dlist_node *node)
Definition: ilist.h:405
void pfree(void *pointer)
Definition: mcxt.c:1521
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 462 of file pgstat.c.

464 {
465  int ret;
466 
467  /* NB: this needs to be done even in single user mode */
468 
469  ret = unlink(PGSTAT_STAT_PERMANENT_FILENAME);
470  if (ret != 0)
471  {
472  if (errno == ENOENT)
473  elog(DEBUG2,
474  "didn't need to unlink permanent stats file \"%s\" - didn't exist",
476  else
477  ereport(LOG,
479  errmsg("could not unlink permanent statistics file \"%s\": %m",
481  }
482  else
483  {
484  ereport(DEBUG2,
486  errmsg_internal("unlinked permanent statistics file \"%s\"",
488  }
489 
490  /*
491  * Reset stats contents. This will set reset timestamps of fixed-numbered
492  * stats to the current time (no variable stats exist).
493  */
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
int errcode_for_file_access(void)
Definition: elog.c:876
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define LOG
Definition: elog.h:31
#define DEBUG2
Definition: elog.h:29
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
static void pgstat_reset_after_failure(void)
Definition: pgstat.c:1693
#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 855 of file pgstat.c.

857 {
859  PgStat_EntryRef *entry_ref;
860  void *stats_data;
861  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
862 
863  /* should be called from backends */
865  Assert(!kind_info->fixed_amount);
866 
868 
869  key.kind = kind;
870  key.dboid = dboid;
871  key.objoid = objoid;
872 
873  /* if we need to build a full snapshot, do so */
876 
877  /* if caching is desired, look up in cache */
879  {
880  PgStat_SnapshotEntry *entry = NULL;
881 
882  entry = pgstat_snapshot_lookup(pgStatLocal.snapshot.stats, key);
883 
884  if (entry)
885  return entry->data;
886 
887  /*
888  * If we built a full snapshot and the key is not in
889  * pgStatLocal.snapshot.stats, there are no matching stats.
890  */
892  return NULL;
893  }
894 
896 
897  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
898 
899  if (entry_ref == NULL || entry_ref->shared_entry->dropped)
900  {
901  /* create empty entry when using PGSTAT_FETCH_CONSISTENCY_CACHE */
903  {
904  PgStat_SnapshotEntry *entry = NULL;
905  bool found;
906 
907  entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
908  Assert(!found);
909  entry->data = NULL;
910  }
911  return NULL;
912  }
913 
914  /*
915  * Allocate in caller's context for PGSTAT_FETCH_CONSISTENCY_NONE,
916  * otherwise we could quickly end up with a fair bit of memory used due to
917  * repeated accesses.
918  */
920  stats_data = palloc(kind_info->shared_data_len);
921  else
923  kind_info->shared_data_len);
924 
925  pgstat_lock_entry_shared(entry_ref, false);
926  memcpy(stats_data,
927  pgstat_get_entry_data(kind, entry_ref->shared_stats),
928  kind_info->shared_data_len);
929  pgstat_unlock_entry(entry_ref);
930 
932  {
933  PgStat_SnapshotEntry *entry = NULL;
934  bool found;
935 
936  entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
937  entry->data = stats_data;
938  }
939 
940  return stats_data;
bool IsUnderPostmaster
Definition: globals.c:118
bool IsPostmasterEnvironment
Definition: globals.c:117
void * palloc(Size size)
Definition: mcxt.c:1317
static void pgstat_build_snapshot(void)
Definition: pgstat.c:1019
bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:601
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:407
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:613
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 1186 of file pgstat.c.

1188 {
1189  PgStat_EntryRef *entry_ref;
1190 
1191  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
1192 
1193  if (entry_ref == NULL || entry_ref->pending == NULL)
1194  return NULL;
1195 
1196  return entry_ref;

References PgStat_EntryRef::pending, and pgstat_get_entry_ref().

Referenced by find_funcstat_entry(), and find_tabstat_entry().

◆ pgstat_flush_pending_entries()

static bool pgstat_flush_pending_entries ( bool  nowait)
static

Definition at line 1223 of file pgstat.c.

1225 {
1226  bool have_pending = false;
1227  dlist_node *cur = NULL;
1228 
1229  /*
1230  * Need to be a bit careful iterating over the list of pending entries.
1231  * Processing a pending entry may queue further pending entries to the end
1232  * of the list that we want to process, so a simple iteration won't do.
1233  * Further complicating matters is that we want to delete the current
1234  * entry in each iteration from the list if we flushed successfully.
1235  *
1236  * So we just keep track of the next pointer in each loop iteration.
1237  */
1240 
1241  while (cur)
1242  {
1243  PgStat_EntryRef *entry_ref =
1244  dlist_container(PgStat_EntryRef, pending_node, cur);
1245  PgStat_HashKey key = entry_ref->shared_entry->key;
1246  PgStat_Kind kind = key.kind;
1247  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1248  bool did_flush;
1249  dlist_node *next;
1250 
1251  Assert(!kind_info->fixed_amount);
1252  Assert(kind_info->flush_pending_cb != NULL);
1253 
1254  /* flush the stats, if possible */
1255  did_flush = kind_info->flush_pending_cb(entry_ref, nowait);
1256 
1257  Assert(did_flush || nowait);
1258 
1259  /* determine next entry, before deleting the pending entry */
1262  else
1263  next = NULL;
1264 
1265  /* if successfully flushed, remove entry */
1266  if (did_flush)
1267  pgstat_delete_pending_entry(entry_ref);
1268  else
1269  have_pending = true;
1270 
1271  cur = next;
1272  }
1273 
1274  Assert(dlist_is_empty(&pgStatPending) == !have_pending);
1275 
1276  return have_pending;
static int32 next
Definition: blutils.c:221
struct cursor * cur
Definition: ecpg.c:28
static bool dlist_has_next(const dlist_head *head, const dlist_node *node)
Definition: ilist.h:503
static dlist_node * dlist_next_node(dlist_head *head, dlist_node *node)
Definition: ilist.h:537
static bool dlist_is_empty(const dlist_head *head)
Definition: ilist.h:336
static dlist_node * dlist_head_node(dlist_head *head)
Definition: ilist.h:565
#define dlist_container(type, membername, ptr)
Definition: ilist.h:593
void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat.c:1199
static dlist_head pgStatPending
Definition: pgstat.c:226
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 737 of file pgstat.c.

739 {
740  pgStatForceNextFlush = true;
static bool pgStatForceNextFlush
Definition: pgstat.c:233

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

1287 {
1288  for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1289  {
1290  if (pg_strcasecmp(kind_str, pgstat_kind_infos[kind].name) == 0)
1291  return kind;
1292  }
1293 
1294  ereport(ERROR,
1295  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1296  errmsg("invalid statistics kind: \"%s\"", kind_str)));
1297  return PGSTAT_KIND_DATABASE; /* avoid compiler warnings */
int errcode(int sqlerrcode)
Definition: elog.c:853
#define ERROR
Definition: elog.h:39
static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS]
Definition: pgstat.c:265
@ PGSTAT_KIND_DATABASE
Definition: pgstat.h:41
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
const char * name

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

Referenced by pg_stat_have_stats().

◆ pgstat_get_kind_info()

◆ pgstat_get_stat_snapshot_timestamp()

TimestampTz pgstat_get_stat_snapshot_timestamp ( bool have_snapshot)

Definition at line 948 of file pgstat.c.

950 {
953 
955  {
956  *have_snapshot = true;
958  }
959 
960  *have_snapshot = false;
961 
962  return 0;
void pgstat_clear_snapshot(void)
Definition: pgstat.c:825

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

967 {
968  /* fixed-numbered stats always exist */
969  if (pgstat_get_kind_info(kind)->fixed_amount)
970  return true;
971 
972  return pgstat_get_entry_ref(kind, dboid, objoid, false, NULL) != NULL;

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

582 {
583  Assert(!pgstat_is_initialized);
584 
586 
587  pgstat_init_wal();
588 
589  /* Set up a process-exit hook to clean up */
591 
592 #ifdef USE_ASSERT_CHECKING
593  pgstat_is_initialized = true;
594 #endif
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:337
static void pgstat_shutdown_hook(int code, Datum arg)
Definition: pgstat.c:546
void pgstat_attach_shmem(void)
Definition: pgstat_shmem.c:219
void pgstat_init_wal(void)
Definition: pgstat_wal.c:141

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

Referenced by BaseInit().

◆ pgstat_is_kind_valid()

static bool pgstat_is_kind_valid ( int  ikind)
inlinestatic

Definition at line 1300 of file pgstat.c.

1302 {
1303  return ikind >= PGSTAT_KIND_FIRST_VALID && ikind <= PGSTAT_KIND_LAST;

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

1150 {
1151  PgStat_EntryRef *entry_ref;
1152 
1153  /* need to be able to flush out */
1154  Assert(pgstat_get_kind_info(kind)->flush_pending_cb != NULL);
1155 
1157  {
1160  "PgStat Pending",
1162  }
1163 
1164  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid,
1165  true, created_entry);
1166 
1167  if (entry_ref->pending == NULL)
1168  {
1169  size_t entrysize = pgstat_get_kind_info(kind)->pending_size;
1170 
1171  Assert(entrysize != (size_t) -1);
1172 
1173  entry_ref->pending = MemoryContextAllocZero(pgStatPendingContext, entrysize);
1175  }
1176 
1177  return entry_ref;
#define unlikely(x)
Definition: c.h:311
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:364
MemoryContext TopMemoryContext
Definition: mcxt.c:149
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1215
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:170
static MemoryContext pgStatPendingContext
Definition: pgstat.c:218

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

1505 {
1506  FILE *fpin;
1507  int32 format_id;
1508  bool found;
1509  const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
1511 
1512  /* shouldn't be called from postmaster */
1514 
1515  elog(DEBUG2, "reading stats file \"%s\"", statfile);
1516 
1517  /*
1518  * Try to open the stats file. If it doesn't exist, the backends simply
1519  * returns zero for anything and statistics simply starts from scratch
1520  * with empty counters.
1521  *
1522  * ENOENT is a possibility if stats collection was previously disabled or
1523  * has not yet written the stats file for the first time. Any other
1524  * failure condition is suspicious.
1525  */
1526  if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
1527  {
1528  if (errno != ENOENT)
1529  ereport(LOG,
1531  errmsg("could not open statistics file \"%s\": %m",
1532  statfile)));
1534  return;
1535  }
1536 
1537  /*
1538  * Verify it's of the expected format.
1539  */
1540  if (!read_chunk_s(fpin, &format_id) ||
1541  format_id != PGSTAT_FILE_FORMAT_ID)
1542  goto error;
1543 
1544  /*
1545  * We found an existing statistics file. Read it and put all the stats
1546  * data into place.
1547  */
1548  for (;;)
1549  {
1550  int t = fgetc(fpin);
1551 
1552  switch (t)
1553  {
1555  {
1556  PgStat_Kind kind;
1557  const PgStat_KindInfo *info;
1558  char *ptr;
1559 
1560  /* entry for fixed-numbered stats */
1561  if (!read_chunk_s(fpin, &kind))
1562  goto error;
1563 
1564  if (!pgstat_is_kind_valid(kind))
1565  goto error;
1566 
1567  info = pgstat_get_kind_info(kind);
1568 
1569  if (!info->fixed_amount)
1570  goto error;
1571 
1572  /* Load back stats into shared memory */
1573  ptr = ((char *) shmem) + info->shared_ctl_off +
1574  info->shared_data_off;
1575 
1576  if (!read_chunk(fpin, ptr, info->shared_data_len))
1577  goto error;
1578 
1579  break;
1580  }
1583  {
1586  PgStatShared_Common *header;
1587 
1589 
1590  if (t == PGSTAT_FILE_ENTRY_HASH)
1591  {
1592  /* normal stats entry, identified by PgStat_HashKey */
1593  if (!read_chunk_s(fpin, &key))
1594  goto error;
1595 
1596  if (!pgstat_is_kind_valid(key.kind))
1597  goto error;
1598  }
1599  else
1600  {
1601  /* stats entry identified by name on disk (e.g. slots) */
1602  const PgStat_KindInfo *kind_info = NULL;
1603  PgStat_Kind kind;
1604  NameData name;
1605 
1606  if (!read_chunk_s(fpin, &kind))
1607  goto error;
1608  if (!read_chunk_s(fpin, &name))
1609  goto error;
1610  if (!pgstat_is_kind_valid(kind))
1611  goto error;
1612 
1613  kind_info = pgstat_get_kind_info(kind);
1614 
1615  if (!kind_info->from_serialized_name)
1616  goto error;
1617 
1618  if (!kind_info->from_serialized_name(&name, &key))
1619  {
1620  /* skip over data for entry we don't care about */
1621  if (fseek(fpin, pgstat_get_entry_len(kind), SEEK_CUR) != 0)
1622  goto error;
1623 
1624  continue;
1625  }
1626 
1627  Assert(key.kind == kind);
1628  }
1629 
1630  /*
1631  * This intentionally doesn't use pgstat_get_entry_ref() -
1632  * putting all stats into checkpointer's
1633  * pgStatEntryRefHash would be wasted effort and memory.
1634  */
1636 
1637  /* don't allow duplicate entries */
1638  if (found)
1639  {
1641  elog(WARNING, "found duplicate stats entry %d/%u/%u",
1642  key.kind, key.dboid, key.objoid);
1643  goto error;
1644  }
1645 
1646  header = pgstat_init_entry(key.kind, p);
1648 
1649  if (!read_chunk(fpin,
1650  pgstat_get_entry_data(key.kind, header),
1651  pgstat_get_entry_len(key.kind)))
1652  goto error;
1653 
1654  break;
1655  }
1656  case PGSTAT_FILE_ENTRY_END:
1657 
1658  /*
1659  * check that PGSTAT_FILE_ENTRY_END actually signals end of
1660  * file
1661  */
1662  if (fgetc(fpin) != EOF)
1663  goto error;
1664 
1665  goto done;
1666 
1667  default:
1668  goto error;
1669  }
1670  }
1671 
1672 done:
1673  FreeFile(fpin);
1674 
1675  elog(DEBUG2, "removing permanent stats file \"%s\"", statfile);
1676  unlink(statfile);
1677 
1678  return;
1679 
1680 error:
1681  ereport(LOG,
1682  (errmsg("corrupted statistics file \"%s\"", statfile)));
1683 
1685 
1686  goto done;
#define PG_BINARY_R
Definition: c.h:1275
signed int int32
Definition: c.h:494
void dshash_release_lock(dshash_table *hash_table, void *entry)
Definition: dshash.c:558
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
Definition: dshash.c:433
#define WARNING
Definition: elog.h:36
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2583
int FreeFile(FILE *file)
Definition: fd.c:2781
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define PGSTAT_FILE_ENTRY_END
Definition: pgstat.c:134
#define PGSTAT_FILE_ENTRY_HASH
Definition: pgstat.c:137
#define PGSTAT_FILE_ENTRY_FIXED
Definition: pgstat.c:135
static bool read_chunk(FILE *fpin, void *ptr, size_t len)
Definition: pgstat.c:1489
#define read_chunk_s(fpin, ptr)
Definition: pgstat.c:1494
#define PGSTAT_FILE_ENTRY_NAME
Definition: pgstat.c:136
#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:268
static void error(void)
Definition: sql-dyntest.c:147
bool(* from_serialized_name)(const NameData *name, PgStat_HashKey *key)
Definition: c.h:741

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

Referenced by pgstat_restore_stats().

◆ pgstat_report_stat()

long pgstat_report_stat ( bool  force)

Definition at line 622 of file pgstat.c.

624 {
625  static TimestampTz pending_since = 0;
626  static TimestampTz last_flush = 0;
627  bool partial_flush;
629  bool nowait;
630 
633 
634  /* "absorb" the forced flush even if there's nothing to flush */
636  {
637  force = true;
638  pgStatForceNextFlush = false;
639  }
640 
641  /* Don't expend a clock check if nothing to do */
643  !have_iostats &&
644  !have_slrustats &&
646  {
647  Assert(pending_since == 0);
648  return 0;
649  }
650 
651  /*
652  * There should never be stats to report once stats are shut down. Can't
653  * assert that before the checks above, as there is an unconditional
654  * pgstat_report_stat() call in pgstat_shutdown_hook() - which at least
655  * the process that ran pgstat_before_server_shutdown() will still call.
656  */
658 
659  if (force)
660  {
661  /*
662  * Stats reports are forced either when it's been too long since stats
663  * have been reported or in processes that force stats reporting to
664  * happen at specific points (including shutdown). In the former case
665  * the transaction stop time might be quite old, in the latter it
666  * would never get cleared.
667  */
669  }
670  else
671  {
673 
674  if (pending_since > 0 &&
676  {
677  /* don't keep pending updates longer than PGSTAT_MAX_INTERVAL */
678  force = true;
679  }
680  else if (last_flush > 0 &&
682  {
683  /* don't flush too frequently */
684  if (pending_since == 0)
685  pending_since = now;
686 
687  return PGSTAT_IDLE_INTERVAL;
688  }
689  }
690 
692 
693  /* don't wait for lock acquisition when !force */
694  nowait = !force;
695 
696  partial_flush = false;
697 
698  /* flush database / relation / function / ... stats */
699  partial_flush |= pgstat_flush_pending_entries(nowait);
700 
701  /* flush IO stats */
702  partial_flush |= pgstat_flush_io(nowait);
703 
704  /* flush wal stats */
705  partial_flush |= pgstat_flush_wal(nowait);
706 
707  /* flush SLRU stats */
708  partial_flush |= pgstat_slru_flush(nowait);
709 
710  last_flush = now;
711 
712  /*
713  * If some of the pending stats could not be flushed due to lock
714  * contention, let the caller know when to retry.
715  */
716  if (partial_flush)
717  {
718  /* force should have prevented us from getting here */
719  Assert(!force);
720 
721  /* remember since when stats have been pending */
722  if (pending_since == 0)
723  pending_since = now;
724 
725  return PGSTAT_IDLE_INTERVAL;
726  }
727 
728  pending_since = 0;
729 
730  return 0;
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1791
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1619
int64 TimestampTz
Definition: timestamp.h:39
#define PGSTAT_MIN_INTERVAL
Definition: pgstat.c:117
static bool pgstat_flush_pending_entries(bool nowait)
Definition: pgstat.c:1223
#define PGSTAT_MAX_INTERVAL
Definition: pgstat.c:119
#define PGSTAT_IDLE_INTERVAL
Definition: pgstat.c:121
void pgstat_update_dbstats(TimestampTz ts)
bool pgstat_flush_io(bool nowait)
Definition: pgstat_io.c:173
bool have_iostats
Definition: pgstat_io.c:32
bool have_slrustats
Definition: pgstat_slru.c:35
bool pgstat_slru_flush(bool nowait)
Definition: pgstat_slru.c:156
bool pgstat_have_pending_wal(void)
Definition: pgstat_wal.c:159
bool pgstat_flush_wal(bool nowait)
Definition: pgstat_wal.c:82
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4976
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition: xact.c:889

References Assert, dlist_is_empty(), GetCurrentTimestamp(), GetCurrentTransactionStopTimestamp(), have_iostats, have_slrustats, PgStat_ShmemControl::is_shutdown, IsTransactionOrTransactionBlock(), now(), pgstat_assert_is_up, pgstat_flush_io(), pgstat_flush_pending_entries(), pgstat_flush_wal(), pgstat_have_pending_wal(), PGSTAT_IDLE_INTERVAL, PGSTAT_MAX_INTERVAL, PGSTAT_MIN_INTERVAL, pgstat_slru_flush(), pgstat_update_dbstats(), pgStatForceNextFlush, pgStatLocal, pgStatPending, PgStat_LocalState::shmem, and TimestampDifferenceExceeds().

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

◆ pgstat_reset()

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

Definition at line 777 of file pgstat.c.

779 {
780  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
782 
783  /* not needed atm, and doesn't make sense with the current signature */
784  Assert(!pgstat_get_kind_info(kind)->fixed_amount);
785 
786  /* reset the "single counter" */
787  pgstat_reset_entry(kind, dboid, objoid, ts);
788 
789  if (!kind_info->accessed_across_databases)
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:958

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

1695 {
1697 
1698  /* reset fixed-numbered stats */
1699  for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1700  {
1701  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1702 
1703  if (!kind_info->fixed_amount)
1704  continue;
1705 
1706  kind_info->reset_all_cb(ts);
1707  }
1708 
1709  /* and drop variable-numbered ones */
void pgstat_drop_all_entries(void)
Definition: pgstat_shmem.c:920
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 758 of file pgstat.c.

760 {
762 
765  ts);
static bool match_db_entries(PgStatShared_HashEntry *entry, Datum match_data)
Definition: pgstat.c:746
void pgstat_reset_matching_entries(bool(*do_reset)(PgStatShared_HashEntry *, Datum), Datum match_data, TimestampTz ts)
Definition: pgstat_shmem.c:978
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 799 of file pgstat.c.

801 {
802  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
804 
805  if (kind_info->fixed_amount)
806  kind_info->reset_all_cb(ts);
807  else
void pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts)

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

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

◆ pgstat_restore_stats()

void pgstat_restore_stats ( void  )

Definition at line 450 of file pgstat.c.

452 {
static void pgstat_read_statsfile(void)
Definition: pgstat.c:1503

References pgstat_read_statsfile().

Referenced by StartupXLOG().

◆ pgstat_shutdown_hook()

static void pgstat_shutdown_hook ( int  code,
Datum  arg 
)
static

Definition at line 546 of file pgstat.c.

548 {
549  Assert(!pgstat_is_shutdown);
551 
552  /*
553  * If we got as far as discovering our own database ID, we can flush out
554  * what we did so far. Otherwise, we'd be reporting an invalid database
555  * ID, so forget it. (This means that accesses to pg_database during
556  * failed backend starts might never get counted.)
557  */
560 
561  pgstat_report_stat(true);
562 
563  /* there shouldn't be any pending changes left */
566 
568 
569 #ifdef USE_ASSERT_CHECKING
570  pgstat_is_shutdown = true;
571 #endif
#define OidIsValid(objectId)
Definition: c.h:775
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:239

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

1353 {
1354  FILE *fpout;
1355  int32 format_id;
1356  const char *tmpfile = PGSTAT_STAT_PERMANENT_TMPFILE;
1357  const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
1358  dshash_seq_status hstat;
1360 
1362 
1363  /* should be called only by the checkpointer or single user mode */
1365 
1366  /* we're shutting down, so it's ok to just override this */
1368 
1369  elog(DEBUG2, "writing stats file \"%s\"", statfile);
1370 
1371  /*
1372  * Open the statistics temp file to write out the current values.
1373  */
1374  fpout = AllocateFile(tmpfile, PG_BINARY_W);
1375  if (fpout == NULL)
1376  {
1377  ereport(LOG,
1379  errmsg("could not open temporary statistics file \"%s\": %m",
1380  tmpfile)));
1381  return;
1382  }
1383 
1384  /*
1385  * Write the file header --- currently just a format ID.
1386  */
1387  format_id = PGSTAT_FILE_FORMAT_ID;
1388  write_chunk_s(fpout, &format_id);
1389 
1390  /* Write various stats structs for fixed number of objects */
1391  for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1392  {
1393  char *ptr;
1394  const PgStat_KindInfo *info = pgstat_get_kind_info(kind);
1395 
1396  if (!info->fixed_amount)
1397  continue;
1398 
1399  Assert(info->snapshot_ctl_off != 0);
1400 
1402  ptr = ((char *) &pgStatLocal.snapshot) + info->snapshot_ctl_off;
1403 
1404  fputc(PGSTAT_FILE_ENTRY_FIXED, fpout);
1405  write_chunk_s(fpout, &kind);
1406  write_chunk(fpout, ptr, info->shared_data_len);
1407  }
1408 
1409  /*
1410  * Walk through the stats entries
1411  */
1412  dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
1413  while ((ps = dshash_seq_next(&hstat)) != NULL)
1414  {
1415  PgStatShared_Common *shstats;
1416  const PgStat_KindInfo *kind_info = NULL;
1417 
1419 
1420  /* we may have some "dropped" entries not yet removed, skip them */
1421  Assert(!ps->dropped);
1422  if (ps->dropped)
1423  continue;
1424 
1425  shstats = (PgStatShared_Common *) dsa_get_address(pgStatLocal.dsa, ps->body);
1426 
1427  kind_info = pgstat_get_kind_info(ps->key.kind);
1428 
1429  /* if not dropped the valid-entry refcount should exist */
1430  Assert(pg_atomic_read_u32(&ps->refcount) > 0);
1431 
1432  if (!kind_info->to_serialized_name)
1433  {
1434  /* normal stats entry, identified by PgStat_HashKey */
1435  fputc(PGSTAT_FILE_ENTRY_HASH, fpout);
1436  write_chunk_s(fpout, &ps->key);
1437  }
1438  else
1439  {
1440  /* stats entry identified by name on disk (e.g. slots) */
1441  NameData name;
1442 
1443  kind_info->to_serialized_name(&ps->key, shstats, &name);
1444 
1445  fputc(PGSTAT_FILE_ENTRY_NAME, fpout);
1446  write_chunk_s(fpout, &ps->key.kind);
1447  write_chunk_s(fpout, &name);
1448  }
1449 
1450  /* Write except the header part of the entry */
1451  write_chunk(fpout,
1452  pgstat_get_entry_data(ps->key.kind, shstats),
1453  pgstat_get_entry_len(ps->key.kind));
1454  }
1455  dshash_seq_term(&hstat);
1456 
1457  /*
1458  * No more output to be done. Close the temp file and replace the old
1459  * pgstat.stat with it. The ferror() check replaces testing for error
1460  * after each individual fputc or fwrite (in write_chunk()) above.
1461  */
1462  fputc(PGSTAT_FILE_ENTRY_END, fpout);
1463 
1464  if (ferror(fpout))
1465  {
1466  ereport(LOG,
1468  errmsg("could not write temporary statistics file \"%s\": %m",
1469  tmpfile)));
1470  FreeFile(fpout);
1471  unlink(tmpfile);
1472  }
1473  else if (FreeFile(fpout) < 0)
1474  {
1475  ereport(LOG,
1477  errmsg("could not close temporary statistics file \"%s\": %m",
1478  tmpfile)));
1479  unlink(tmpfile);
1480  }
1481  else if (durable_rename(tmpfile, statfile, LOG) < 0)
1482  {
1483  /* durable_rename already emitted log message */
1484  unlink(tmpfile);
1485  }
#define PG_BINARY_W
Definition: c.h:1276
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:782
struct parser_state ps
@ B_CHECKPOINTER
Definition: miscadmin.h:357
BackendType MyBackendType
Definition: miscinit.c:63
static void write_chunk(FILE *fpout, void *ptr, size_t len)
Definition: pgstat.c:1334
#define write_chunk_s(fpout, ptr)
Definition: pgstat.c:1344
#define PGSTAT_STAT_PERMANENT_TMPFILE
Definition: pgstat.h:29
void(* to_serialized_name)(const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name)

References AllocateFile(), Assert, B_CHECKPOINTER, CHECK_FOR_INTERRUPTS, DEBUG2, PgStat_LocalState::dsa, dsa_get_address(), dshash_seq_init(), dshash_seq_next(), dshash_seq_term(), durable_rename(), elog, ereport, errcode_for_file_access(), errmsg(), PgStat_KindInfo::fixed_amount, FreeFile(), IsUnderPostmaster, LOG, MyBackendType, name, pg_atomic_read_u32(), PG_BINARY_W, pgstat_assert_is_up, pgstat_build_snapshot_fixed(), pgstat_fetch_consistency, PGSTAT_FETCH_CONSISTENCY_NONE, PGSTAT_FILE_ENTRY_END, PGSTAT_FILE_ENTRY_FIXED, PGSTAT_FILE_ENTRY_HASH, PGSTAT_FILE_ENTRY_NAME, PGSTAT_FILE_FORMAT_ID, pgstat_get_entry_data(), pgstat_get_entry_len(), pgstat_get_kind_info(), PGSTAT_KIND_FIRST_VALID, PGSTAT_KIND_LAST, PGSTAT_STAT_PERMANENT_FILENAME, PGSTAT_STAT_PERMANENT_TMPFILE, pgStatLocal, ps, PgStat_KindInfo::shared_data_len, PgStat_LocalState::shared_hash, PgStat_LocalState::snapshot, PgStat_KindInfo::snapshot_ctl_off, PgStat_KindInfo::to_serialized_name, 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 1489 of file pgstat.c.

1491 {
1492  return fread(ptr, 1, len, fpin) == len;
const void size_t len

References len.

Referenced by pgstat_read_statsfile().

◆ write_chunk()

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

Definition at line 1334 of file pgstat.c.

1336 {
1337  int rc;
1338 
1339  rc = fwrite(ptr, len, 1, fpout);
1340 
1341  /* we'll check for errors with ferror once at the end */
1342  (void) rc;

References len.

Referenced by pgstat_write_statsfile().

Variable Documentation

◆ force_stats_snapshot_clear

◆ pgstat_fetch_consistency

◆ pgstat_kind_infos

const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS]
static

Definition at line 265 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 233 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 218 of file pgstat.c.

Referenced by pgstat_prep_pending_entry().