PostgreSQL Source Code  git master
pgstat.c File Reference
#include "postgres.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/tableam.h"
#include "access/transam.h"
#include "access/twophase_rmgr.h"
#include "access/xact.h"
#include "access/xlogprefetch.h"
#include "catalog/partition.h"
#include "catalog/pg_database.h"
#include "catalog/pg_proc.h"
#include "common/ip.h"
#include "executor/instrument.h"
#include "libpq/libpq.h"
#include "libpq/pqsignal.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/autovacuum.h"
#include "postmaster/fork_process.h"
#include "postmaster/interrupt.h"
#include "postmaster/postmaster.h"
#include "replication/slot.h"
#include "replication/walsender.h"
#include "storage/backendid.h"
#include "storage/dsm.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/lmgr.h"
#include "storage/pg_shmem.h"
#include "storage/proc.h"
#include "storage/procsignal.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/ps_status.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
#include "utils/timestamp.h"
Include dependency graph for pgstat.c:

Go to the source code of this file.

Data Structures

struct  TabStatusArray
 
struct  TabStatHashEntry
 
struct  PgStat_SubXactStatus
 
struct  TwoPhasePgStatRecord
 

Macros

#define PGSTAT_STAT_INTERVAL
 
#define PGSTAT_RETRY_DELAY
 
#define PGSTAT_MAX_WAIT_TIME
 
#define PGSTAT_INQ_INTERVAL
 
#define PGSTAT_RESTART_INTERVAL
 
#define PGSTAT_POLL_LOOP_COUNT   (PGSTAT_MAX_WAIT_TIME / PGSTAT_RETRY_DELAY)
 
#define PGSTAT_INQ_LOOP_COUNT   (PGSTAT_INQ_INTERVAL / PGSTAT_RETRY_DELAY)
 
#define PGSTAT_MIN_RCVBUF   (100 * 1024)
 
#define PGSTAT_DB_HASH_SIZE   16
 
#define PGSTAT_TAB_HASH_SIZE   512
 
#define PGSTAT_FUNCTION_HASH_SIZE   512
 
#define SLRU_NUM_ELEMENTS   lengthof(slru_names)
 
#define TABSTAT_QUANTUM   100 /* we alloc this many at a time */
 
#define TESTBYTEVAL   ((char) 199)
 

Typedefs

typedef struct TabStatusArray TabStatusArray
 
typedef struct TabStatHashEntry TabStatHashEntry
 
typedef struct PgStat_SubXactStatus PgStat_SubXactStatus
 
typedef struct TwoPhasePgStatRecord TwoPhasePgStatRecord
 

Functions

NON_EXEC_STATIC void PgstatCollectorMain (int argc, char *argv[]) pg_attribute_noreturn()
 
static PgStat_StatDBEntrypgstat_get_db_entry (Oid databaseid, bool create)
 
static PgStat_StatTabEntrypgstat_get_tab_entry (PgStat_StatDBEntry *dbentry, Oid tableoid, bool create)
 
static void pgstat_write_statsfiles (bool permanent, bool allDbs)
 
static void pgstat_write_db_statsfile (PgStat_StatDBEntry *dbentry, bool permanent)
 
static HTABpgstat_read_statsfiles (Oid onlydb, bool permanent, bool deep)
 
static void pgstat_read_db_statsfile (Oid databaseid, HTAB *tabhash, HTAB *funchash, bool permanent)
 
static void backend_read_statsfile (void)
 
static bool pgstat_write_statsfile_needed (void)
 
static bool pgstat_db_requested (Oid databaseid)
 
static int pgstat_replslot_index (const char *name, bool create_it)
 
static void pgstat_reset_replslot (int i, TimestampTz ts)
 
static void pgstat_send_tabstat (PgStat_MsgTabstat *tsmsg)
 
static void pgstat_send_funcstats (void)
 
static void pgstat_send_slru (void)
 
static HTABpgstat_collect_oids (Oid catalogid, AttrNumber anum_oid)
 
static void pgstat_send_connstats (bool disconnect, TimestampTz last_report)
 
static PgStat_TableStatusget_tabstat_entry (Oid rel_id, bool isshared)
 
static void pgstat_setup_memcxt (void)
 
static void pgstat_setheader (PgStat_MsgHdr *hdr, StatMsgType mtype)
 
static void pgstat_send (void *msg, int len)
 
static void pgstat_recv_inquiry (PgStat_MsgInquiry *msg, int len)
 
static void pgstat_recv_tabstat (PgStat_MsgTabstat *msg, int len)
 
static void pgstat_recv_tabpurge (PgStat_MsgTabpurge *msg, int len)
 
static void pgstat_recv_dropdb (PgStat_MsgDropdb *msg, int len)
 
static void pgstat_recv_resetcounter (PgStat_MsgResetcounter *msg, int len)
 
static void pgstat_recv_resetsharedcounter (PgStat_MsgResetsharedcounter *msg, int len)
 
static void pgstat_recv_resetsinglecounter (PgStat_MsgResetsinglecounter *msg, int len)
 
static void pgstat_recv_resetslrucounter (PgStat_MsgResetslrucounter *msg, int len)
 
static void pgstat_recv_resetreplslotcounter (PgStat_MsgResetreplslotcounter *msg, int len)
 
static void pgstat_recv_autovac (PgStat_MsgAutovacStart *msg, int len)
 
static void pgstat_recv_vacuum (PgStat_MsgVacuum *msg, int len)
 
static void pgstat_recv_analyze (PgStat_MsgAnalyze *msg, int len)
 
static void pgstat_recv_anl_ancestors (PgStat_MsgAnlAncestors *msg, int len)
 
static void pgstat_recv_archiver (PgStat_MsgArchiver *msg, int len)
 
static void pgstat_recv_bgwriter (PgStat_MsgBgWriter *msg, int len)
 
static void pgstat_recv_wal (PgStat_MsgWal *msg, int len)
 
static void pgstat_recv_slru (PgStat_MsgSLRU *msg, int len)
 
static void pgstat_recv_recoveryprefetch (PgStat_MsgRecoveryPrefetch *msg, int len)
 
static void pgstat_recv_funcstat (PgStat_MsgFuncstat *msg, int len)
 
static void pgstat_recv_funcpurge (PgStat_MsgFuncpurge *msg, int len)
 
static void pgstat_recv_recoveryconflict (PgStat_MsgRecoveryConflict *msg, int len)
 
static void pgstat_recv_deadlock (PgStat_MsgDeadlock *msg, int len)
 
static void pgstat_recv_checksum_failure (PgStat_MsgChecksumFailure *msg, int len)
 
static void pgstat_recv_connstat (PgStat_MsgConn *msg, int len)
 
static void pgstat_recv_replslot (PgStat_MsgReplSlot *msg, int len)
 
static void pgstat_recv_tempfile (PgStat_MsgTempFile *msg, int len)
 
void pgstat_init (void)
 
static void pgstat_reset_remove_files (const char *directory)
 
void pgstat_reset_all (void)
 
int pgstat_start (void)
 
void allow_immediate_pgstat_restart (void)
 
void pgstat_report_stat (bool disconnect)
 
void pgstat_vacuum_stat (void)
 
void pgstat_drop_database (Oid databaseid)
 
void pgstat_reset_counters (void)
 
void pgstat_reset_shared_counters (const char *target)
 
void pgstat_reset_single_counter (Oid objoid, PgStat_Single_Reset_Type type)
 
void pgstat_reset_slru_counter (const char *name)
 
void pgstat_reset_replslot_counter (const char *name)
 
void pgstat_report_autovac (Oid dboid)
 
void pgstat_report_vacuum (Oid tableoid, bool shared, PgStat_Counter livetuples, PgStat_Counter deadtuples)
 
void pgstat_report_analyze (Relation rel, PgStat_Counter livetuples, PgStat_Counter deadtuples, bool resetcounter)
 
void pgstat_report_anl_ancestors (Oid relid)
 
void pgstat_report_recovery_conflict (int reason)
 
void pgstat_report_deadlock (void)
 
void pgstat_report_checksum_failures_in_db (Oid dboid, int failurecount)
 
void pgstat_report_checksum_failure (void)
 
void pgstat_report_tempfile (size_t filesize)
 
void pgstat_report_replslot (const PgStat_ReplSlotStats *repSlotStat)
 
void pgstat_report_replslot_drop (const char *slotname)
 
void pgstat_ping (void)
 
static void pgstat_send_inquiry (TimestampTz clock_time, TimestampTz cutoff_time, Oid databaseid)
 
void pgstat_init_function_usage (FunctionCallInfo fcinfo, PgStat_FunctionCallUsage *fcu)
 
PgStat_BackendFunctionEntryfind_funcstat_entry (Oid func_id)
 
void pgstat_end_function_usage (PgStat_FunctionCallUsage *fcu, bool finalize)
 
void pgstat_initstats (Relation rel)
 
PgStat_TableStatusfind_tabstat_entry (Oid rel_id)
 
static PgStat_SubXactStatusget_tabstat_stack_level (int nest_level)
 
static void add_tabstat_xact_level (PgStat_TableStatus *pgstat_info, int nest_level)
 
void pgstat_count_heap_insert (Relation rel, PgStat_Counter n)
 
void pgstat_count_heap_update (Relation rel, bool hot)
 
void pgstat_count_heap_delete (Relation rel)
 
static void pgstat_truncate_save_counters (PgStat_TableXactStatus *trans)
 
static void pgstat_truncate_restore_counters (PgStat_TableXactStatus *trans)
 
void pgstat_count_truncate (Relation rel)
 
void pgstat_update_heap_dead_tuples (Relation rel, int delta)
 
void AtEOXact_PgStat (bool isCommit, bool parallel)
 
void AtEOSubXact_PgStat (bool isCommit, int nestDepth)
 
void AtPrepare_PgStat (void)
 
void PostPrepare_PgStat (void)
 
void pgstat_twophase_postcommit (TransactionId xid, uint16 info, void *recdata, uint32 len)
 
void pgstat_twophase_postabort (TransactionId xid, uint16 info, void *recdata, uint32 len)
 
PgStat_StatDBEntrypgstat_fetch_stat_dbentry (Oid dbid)
 
PgStat_StatTabEntrypgstat_fetch_stat_tabentry (Oid relid)
 
PgStat_StatFuncEntrypgstat_fetch_stat_funcentry (Oid func_id)
 
PgStat_ArchiverStatspgstat_fetch_stat_archiver (void)
 
PgStat_GlobalStatspgstat_fetch_global (void)
 
PgStat_WalStatspgstat_fetch_stat_wal (void)
 
PgStat_SLRUStatspgstat_fetch_slru (void)
 
PgStat_ReplSlotStatspgstat_fetch_replslot (int *nslots_p)
 
PgStat_RecoveryPrefetchStatspgstat_fetch_recoveryprefetch (void)
 
static void pgstat_shutdown_hook (int code, Datum arg)
 
void pgstat_initialize (void)
 
void pgstat_send_archiver (const char *xlog, bool failed)
 
void pgstat_send_bgwriter (void)
 
void pgstat_report_wal (void)
 
bool pgstat_send_wal (bool force)
 
void pgstat_send_recoveryprefetch (PgStat_RecoveryPrefetchStats *stats)
 
static void reset_dbentry_counters (PgStat_StatDBEntry *dbentry)
 
static void get_dbstat_filename (bool permanent, bool tempname, Oid databaseid, char *filename, int len)
 
static bool pgstat_read_db_statsfile_timestamp (Oid databaseid, bool permanent, TimestampTz *ts)
 
void pgstat_clear_snapshot (void)
 
int pgstat_slru_index (const char *name)
 
const char * pgstat_slru_name (int slru_idx)
 
static PgStat_MsgSLRUslru_entry (int slru_idx)
 
void pgstat_count_slru_page_zeroed (int slru_idx)
 
void pgstat_count_slru_page_hit (int slru_idx)
 
void pgstat_count_slru_page_exists (int slru_idx)
 
void pgstat_count_slru_page_read (int slru_idx)
 
void pgstat_count_slru_page_written (int slru_idx)
 
void pgstat_count_slru_flush (int slru_idx)
 
void pgstat_count_slru_truncate (int slru_idx)
 

Variables

bool pgstat_track_counts = false
 
int pgstat_track_functions = TRACK_FUNC_OFF
 
char * pgstat_stat_directory = NULL
 
char * pgstat_stat_filename = NULL
 
char * pgstat_stat_tmpname = NULL
 
PgStat_MsgBgWriter BgWriterStats
 
PgStat_MsgWal WalStats
 
static WalUsage prevWalUsage
 
static const char *const slru_names []
 
static PgStat_MsgSLRU SLRUStats [SLRU_NUM_ELEMENTS]
 
NON_EXEC_STATIC pgsocket pgStatSock = PGINVALID_SOCKET
 
static struct sockaddr_storage pgStatAddr
 
static time_t last_pgstat_start_time
 
static bool pgStatRunningInCollector = false
 
static TabStatusArraypgStatTabList = NULL
 
static HTABpgStatTabHash = NULL
 
static HTABpgStatFunctions = NULL
 
static bool have_function_stats = false
 
static PgStat_SubXactStatuspgStatXactStack = NULL
 
static int pgStatXactCommit = 0
 
static int pgStatXactRollback = 0
 
PgStat_Counter pgStatBlockReadTime = 0
 
PgStat_Counter pgStatBlockWriteTime = 0
 
PgStat_Counter pgStatActiveTime = 0
 
PgStat_Counter pgStatTransactionIdleTime = 0
 
SessionEndType pgStatSessionEndCause = DISCONNECT_NORMAL
 
static MemoryContext pgStatLocalContext = NULL
 
static HTABpgStatDBHash = NULL
 
static PgStat_ArchiverStats archiverStats
 
static PgStat_GlobalStats globalStats
 
static PgStat_WalStats walStats
 
static PgStat_SLRUStats slruStats [SLRU_NUM_ELEMENTS]
 
static PgStat_ReplSlotStatsreplSlotStats
 
static int nReplSlotStats
 
static PgStat_RecoveryPrefetchStats recoveryPrefetchStats
 
static Listpending_write_requests = NIL
 
static instr_time total_func_time
 

Macro Definition Documentation

◆ PGSTAT_DB_HASH_SIZE

#define PGSTAT_DB_HASH_SIZE   16

Definition at line 106 of file pgstat.c.

Referenced by pgstat_read_statsfiles().

◆ PGSTAT_FUNCTION_HASH_SIZE

#define PGSTAT_FUNCTION_HASH_SIZE   512

◆ PGSTAT_INQ_INTERVAL

#define PGSTAT_INQ_INTERVAL
Value:
640 /* How often to ping the collector for a
* new file; in milliseconds. */

Definition at line 88 of file pgstat.c.

◆ PGSTAT_INQ_LOOP_COUNT

#define PGSTAT_INQ_LOOP_COUNT   (PGSTAT_INQ_INTERVAL / PGSTAT_RETRY_DELAY)

Definition at line 96 of file pgstat.c.

Referenced by backend_read_statsfile().

◆ PGSTAT_MAX_WAIT_TIME

#define PGSTAT_MAX_WAIT_TIME
Value:
10000 /* Maximum time to wait for a stats
* file update; in milliseconds. */

Definition at line 85 of file pgstat.c.

◆ PGSTAT_MIN_RCVBUF

#define PGSTAT_MIN_RCVBUF   (100 * 1024)

Definition at line 99 of file pgstat.c.

Referenced by pgstat_init().

◆ PGSTAT_POLL_LOOP_COUNT

#define PGSTAT_POLL_LOOP_COUNT   (PGSTAT_MAX_WAIT_TIME / PGSTAT_RETRY_DELAY)

Definition at line 95 of file pgstat.c.

Referenced by backend_read_statsfile().

◆ PGSTAT_RESTART_INTERVAL

#define PGSTAT_RESTART_INTERVAL
Value:
60 /* How often to attempt to restart a
* failed statistics collector; in
* seconds. */

Definition at line 91 of file pgstat.c.

Referenced by pgstat_start().

◆ PGSTAT_RETRY_DELAY

#define PGSTAT_RETRY_DELAY
Value:
10 /* How long to wait between checks for a
* new file; in milliseconds. */

Definition at line 82 of file pgstat.c.

Referenced by backend_read_statsfile().

◆ PGSTAT_STAT_INTERVAL

#define PGSTAT_STAT_INTERVAL
Value:
500 /* Minimum time between stats file
* updates; in milliseconds. */

Definition at line 79 of file pgstat.c.

Referenced by backend_read_statsfile(), pgstat_report_stat(), and pgstat_send_wal().

◆ PGSTAT_TAB_HASH_SIZE

#define PGSTAT_TAB_HASH_SIZE   512

Definition at line 107 of file pgstat.c.

Referenced by pgstat_collect_oids(), pgstat_read_statsfiles(), and reset_dbentry_counters().

◆ SLRU_NUM_ELEMENTS

◆ TABSTAT_QUANTUM

#define TABSTAT_QUANTUM   100 /* we alloc this many at a time */

Definition at line 191 of file pgstat.c.

Referenced by get_tabstat_entry().

◆ TESTBYTEVAL

#define TESTBYTEVAL   ((char) 199)

Referenced by pgstat_init().

Typedef Documentation

◆ PgStat_SubXactStatus

◆ TabStatHashEntry

◆ TabStatusArray

◆ TwoPhasePgStatRecord

Function Documentation

◆ add_tabstat_xact_level()

static void add_tabstat_xact_level ( PgStat_TableStatus pgstat_info,
int  nest_level 
)
static

Definition at line 2190 of file pgstat.c.

References PgStat_SubXactStatus::first, get_tabstat_stack_level(), MemoryContextAllocZero(), PgStat_TableXactStatus::nest_level, PgStat_TableXactStatus::next, PgStat_TableXactStatus::parent, TopTransactionContext, PgStat_TableStatus::trans, trans, and PgStat_TableXactStatus::upper.

Referenced by pgstat_count_heap_delete(), pgstat_count_heap_insert(), pgstat_count_heap_update(), and pgstat_count_truncate().

2191 {
2192  PgStat_SubXactStatus *xact_state;
2194 
2195  /*
2196  * If this is the first rel to be modified at the current nest level, we
2197  * first have to push a transaction stack entry.
2198  */
2199  xact_state = get_tabstat_stack_level(nest_level);
2200 
2201  /* Now make a per-table stack entry */
2202  trans = (PgStat_TableXactStatus *)
2204  sizeof(PgStat_TableXactStatus));
2205  trans->nest_level = nest_level;
2206  trans->upper = pgstat_info->trans;
2207  trans->parent = pgstat_info;
2208  trans->next = xact_state->first;
2209  xact_state->first = trans;
2210  pgstat_info->trans = trans;
2211 }
MemoryContext TopTransactionContext
Definition: mcxt.c:53
struct PgStat_TableXactStatus * next
Definition: pgstat.h:198
static PgStat_SubXactStatus * get_tabstat_stack_level(int nest_level)
Definition: pgstat.c:2168
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:195
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:906
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:176
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:400
PgStat_TableXactStatus * first
Definition: pgstat.c:239
PgStat_TableStatus * parent
Definition: pgstat.h:196

◆ allow_immediate_pgstat_restart()

void allow_immediate_pgstat_restart ( void  )

Definition at line 820 of file pgstat.c.

References last_pgstat_start_time.

Referenced by HandleChildCrash().

821 {
823 }
static time_t last_pgstat_start_time
Definition: pgstat.c:176

◆ AtEOSubXact_PgStat()

void AtEOSubXact_PgStat ( bool  isCommit,
int  nestDepth 
)

Definition at line 2450 of file pgstat.c.

References Assert, PgStat_SubXactStatus::first, get_tabstat_stack_level(), PgStat_TableXactStatus::nest_level, PgStat_SubXactStatus::nest_level, PgStat_TableXactStatus::next, PgStat_TableXactStatus::parent, pfree(), pgstat_truncate_restore_counters(), pgstat_truncate_save_counters(), pgStatXactStack, PgStat_SubXactStatus::prev, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_delta_dead_tuples, PgStat_TableCounts::t_tuples_deleted, PgStat_TableCounts::t_tuples_inserted, PgStat_TableCounts::t_tuples_updated, PgStat_TableStatus::trans, trans, PgStat_TableXactStatus::truncated, PgStat_TableXactStatus::tuples_deleted, PgStat_TableXactStatus::tuples_inserted, PgStat_TableXactStatus::tuples_updated, and PgStat_TableXactStatus::upper.

Referenced by AbortSubTransaction(), and CommitSubTransaction().

2451 {
2452  PgStat_SubXactStatus *xact_state;
2453 
2454  /*
2455  * Transfer transactional insert/update counts into the next higher
2456  * subtransaction state.
2457  */
2458  xact_state = pgStatXactStack;
2459  if (xact_state != NULL &&
2460  xact_state->nest_level >= nestDepth)
2461  {
2463  PgStat_TableXactStatus *next_trans;
2464 
2465  /* delink xact_state from stack immediately to simplify reuse case */
2466  pgStatXactStack = xact_state->prev;
2467 
2468  for (trans = xact_state->first; trans != NULL; trans = next_trans)
2469  {
2470  PgStat_TableStatus *tabstat;
2471 
2472  next_trans = trans->next;
2473  Assert(trans->nest_level == nestDepth);
2474  tabstat = trans->parent;
2475  Assert(tabstat->trans == trans);
2476  if (isCommit)
2477  {
2478  if (trans->upper && trans->upper->nest_level == nestDepth - 1)
2479  {
2480  if (trans->truncated)
2481  {
2482  /* propagate the truncate status one level up */
2484  /* replace upper xact stats with ours */
2485  trans->upper->tuples_inserted = trans->tuples_inserted;
2486  trans->upper->tuples_updated = trans->tuples_updated;
2487  trans->upper->tuples_deleted = trans->tuples_deleted;
2488  }
2489  else
2490  {
2491  trans->upper->tuples_inserted += trans->tuples_inserted;
2492  trans->upper->tuples_updated += trans->tuples_updated;
2493  trans->upper->tuples_deleted += trans->tuples_deleted;
2494  }
2495  tabstat->trans = trans->upper;
2496  pfree(trans);
2497  }
2498  else
2499  {
2500  /*
2501  * When there isn't an immediate parent state, we can just
2502  * reuse the record instead of going through a
2503  * palloc/pfree pushup (this works since it's all in
2504  * TopTransactionContext anyway). We have to re-link it
2505  * into the parent level, though, and that might mean
2506  * pushing a new entry into the pgStatXactStack.
2507  */
2508  PgStat_SubXactStatus *upper_xact_state;
2509 
2510  upper_xact_state = get_tabstat_stack_level(nestDepth - 1);
2511  trans->next = upper_xact_state->first;
2512  upper_xact_state->first = trans;
2513  trans->nest_level = nestDepth - 1;
2514  }
2515  }
2516  else
2517  {
2518  /*
2519  * On abort, update top-level tabstat counts, then forget the
2520  * subtransaction
2521  */
2522 
2523  /* first restore values obliterated by truncate */
2525  /* count attempted actions regardless of commit/abort */
2526  tabstat->t_counts.t_tuples_inserted += trans->tuples_inserted;
2527  tabstat->t_counts.t_tuples_updated += trans->tuples_updated;
2528  tabstat->t_counts.t_tuples_deleted += trans->tuples_deleted;
2529  /* inserted tuples are dead, deleted tuples are unaffected */
2530  tabstat->t_counts.t_delta_dead_tuples +=
2531  trans->tuples_inserted + trans->tuples_updated;
2532  tabstat->trans = trans->upper;
2533  pfree(trans);
2534  }
2535  }
2536  pfree(xact_state);
2537  }
2538 }
PgStat_Counter tuples_updated
Definition: pgstat.h:187
struct PgStat_TableXactStatus * next
Definition: pgstat.h:198
PgStat_TableCounts t_counts
Definition: pgstat.h:177
static PgStat_SubXactStatus * get_tabstat_stack_level(int nest_level)
Definition: pgstat.c:2168
void pfree(void *pointer)
Definition: mcxt.c:1169
PgStat_Counter t_tuples_updated
Definition: pgstat.h:123
static void pgstat_truncate_restore_counters(PgStat_TableXactStatus *trans)
Definition: pgstat.c:2304
PgStat_Counter tuples_inserted
Definition: pgstat.h:186
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:129
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:195
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:124
PgStat_Counter tuples_deleted
Definition: pgstat.h:188
struct PgStat_SubXactStatus * prev
Definition: pgstat.c:238
#define Assert(condition)
Definition: c.h:804
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:242
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:176
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:400
PgStat_TableXactStatus * first
Definition: pgstat.c:239
static void pgstat_truncate_save_counters(PgStat_TableXactStatus *trans)
Definition: pgstat.c:2289
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:122
PgStat_TableStatus * parent
Definition: pgstat.h:196

◆ AtEOXact_PgStat()

void AtEOXact_PgStat ( bool  isCommit,
bool  parallel 
)

Definition at line 2363 of file pgstat.c.

References Assert, PgStat_SubXactStatus::first, PgStat_TableXactStatus::nest_level, PgStat_SubXactStatus::nest_level, PgStat_TableXactStatus::next, PgStat_TableXactStatus::parent, pgstat_clear_snapshot(), pgstat_truncate_restore_counters(), pgStatXactCommit, pgStatXactRollback, pgStatXactStack, PgStat_SubXactStatus::prev, PgStat_TableCounts::t_changed_tuples, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_delta_dead_tuples, PgStat_TableCounts::t_delta_live_tuples, PgStat_TableCounts::t_truncated, PgStat_TableCounts::t_tuples_deleted, PgStat_TableCounts::t_tuples_inserted, PgStat_TableCounts::t_tuples_updated, PgStat_TableStatus::trans, trans, PgStat_TableXactStatus::truncated, PgStat_TableXactStatus::tuples_deleted, PgStat_TableXactStatus::tuples_inserted, PgStat_TableXactStatus::tuples_updated, and PgStat_TableXactStatus::upper.

Referenced by AbortTransaction(), CommitTransaction(), and FinishPreparedTransaction().

2364 {
2365  PgStat_SubXactStatus *xact_state;
2366 
2367  /* Don't count parallel worker transaction stats */
2368  if (!parallel)
2369  {
2370  /*
2371  * Count transaction commit or abort. (We use counters, not just
2372  * bools, in case the reporting message isn't sent right away.)
2373  */
2374  if (isCommit)
2375  pgStatXactCommit++;
2376  else
2378  }
2379 
2380  /*
2381  * Transfer transactional insert/update counts into the base tabstat
2382  * entries. We don't bother to free any of the transactional state, since
2383  * it's all in TopTransactionContext and will go away anyway.
2384  */
2385  xact_state = pgStatXactStack;
2386  if (xact_state != NULL)
2387  {
2389 
2390  Assert(xact_state->nest_level == 1);
2391  Assert(xact_state->prev == NULL);
2392  for (trans = xact_state->first; trans != NULL; trans = trans->next)
2393  {
2394  PgStat_TableStatus *tabstat;
2395 
2396  Assert(trans->nest_level == 1);
2397  Assert(trans->upper == NULL);
2398  tabstat = trans->parent;
2399  Assert(tabstat->trans == trans);
2400  /* restore pre-truncate stats (if any) in case of aborted xact */
2401  if (!isCommit)
2403  /* count attempted actions regardless of commit/abort */
2404  tabstat->t_counts.t_tuples_inserted += trans->tuples_inserted;
2405  tabstat->t_counts.t_tuples_updated += trans->tuples_updated;
2406  tabstat->t_counts.t_tuples_deleted += trans->tuples_deleted;
2407  if (isCommit)
2408  {
2409  tabstat->t_counts.t_truncated = trans->truncated;
2410  if (trans->truncated)
2411  {
2412  /* forget live/dead stats seen by backend thus far */
2413  tabstat->t_counts.t_delta_live_tuples = 0;
2414  tabstat->t_counts.t_delta_dead_tuples = 0;
2415  }
2416  /* insert adds a live tuple, delete removes one */
2417  tabstat->t_counts.t_delta_live_tuples +=
2418  trans->tuples_inserted - trans->tuples_deleted;
2419  /* update and delete each create a dead tuple */
2420  tabstat->t_counts.t_delta_dead_tuples +=
2421  trans->tuples_updated + trans->tuples_deleted;
2422  /* insert, update, delete each count as one change event */
2423  tabstat->t_counts.t_changed_tuples +=
2424  trans->tuples_inserted + trans->tuples_updated +
2425  trans->tuples_deleted;
2426  }
2427  else
2428  {
2429  /* inserted tuples are dead, deleted tuples are unaffected */
2430  tabstat->t_counts.t_delta_dead_tuples +=
2431  trans->tuples_inserted + trans->tuples_updated;
2432  /* an aborted xact generates no changed_tuple events */
2433  }
2434  tabstat->trans = NULL;
2435  }
2436  }
2437  pgStatXactStack = NULL;
2438 
2439  /* Make sure any stats snapshot is thrown away */
2441 }
PgStat_Counter t_delta_live_tuples
Definition: pgstat.h:128
PgStat_Counter tuples_updated
Definition: pgstat.h:187
static int pgStatXactCommit
Definition: pgstat.c:244
struct PgStat_TableXactStatus * next
Definition: pgstat.h:198
PgStat_TableCounts t_counts
Definition: pgstat.h:177
PgStat_Counter t_tuples_updated
Definition: pgstat.h:123
static void pgstat_truncate_restore_counters(PgStat_TableXactStatus *trans)
Definition: pgstat.c:2304
static int pgStatXactRollback
Definition: pgstat.c:245
PgStat_Counter tuples_inserted
Definition: pgstat.h:186
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:129
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:195
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:124
PgStat_Counter tuples_deleted
Definition: pgstat.h:188
struct PgStat_SubXactStatus * prev
Definition: pgstat.c:238
PgStat_Counter t_changed_tuples
Definition: pgstat.h:130
#define Assert(condition)
Definition: c.h:804
void pgstat_clear_snapshot(void)
Definition: pgstat.c:4758
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:242
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:176
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:400
PgStat_TableXactStatus * first
Definition: pgstat.c:239
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:122
PgStat_TableStatus * parent
Definition: pgstat.h:196

◆ AtPrepare_PgStat()

void AtPrepare_PgStat ( void  )

Definition at line 2549 of file pgstat.c.

References Assert, PgStat_TableXactStatus::deleted_pre_trunc, TwoPhasePgStatRecord::deleted_pre_trunc, PgStat_SubXactStatus::first, PgStat_TableXactStatus::inserted_pre_trunc, TwoPhasePgStatRecord::inserted_pre_trunc, PgStat_TableXactStatus::nest_level, PgStat_SubXactStatus::nest_level, PgStat_TableXactStatus::next, PgStat_TableXactStatus::parent, pgStatXactStack, PgStat_SubXactStatus::prev, RegisterTwoPhaseRecord(), PgStat_TableStatus::t_id, TwoPhasePgStatRecord::t_id, PgStat_TableStatus::t_shared, TwoPhasePgStatRecord::t_shared, TwoPhasePgStatRecord::t_truncated, PgStat_TableStatus::trans, trans, PgStat_TableXactStatus::truncated, PgStat_TableXactStatus::tuples_deleted, TwoPhasePgStatRecord::tuples_deleted, PgStat_TableXactStatus::tuples_inserted, TwoPhasePgStatRecord::tuples_inserted, PgStat_TableXactStatus::tuples_updated, TwoPhasePgStatRecord::tuples_updated, TWOPHASE_RM_PGSTAT_ID, PgStat_TableXactStatus::updated_pre_trunc, TwoPhasePgStatRecord::updated_pre_trunc, and PgStat_TableXactStatus::upper.

Referenced by PrepareTransaction().

2550 {
2551  PgStat_SubXactStatus *xact_state;
2552 
2553  xact_state = pgStatXactStack;
2554  if (xact_state != NULL)
2555  {
2557 
2558  Assert(xact_state->nest_level == 1);
2559  Assert(xact_state->prev == NULL);
2560  for (trans = xact_state->first; trans != NULL; trans = trans->next)
2561  {
2562  PgStat_TableStatus *tabstat;
2563  TwoPhasePgStatRecord record;
2564 
2565  Assert(trans->nest_level == 1);
2566  Assert(trans->upper == NULL);
2567  tabstat = trans->parent;
2568  Assert(tabstat->trans == trans);
2569 
2570  record.tuples_inserted = trans->tuples_inserted;
2571  record.tuples_updated = trans->tuples_updated;
2572  record.tuples_deleted = trans->tuples_deleted;
2573  record.inserted_pre_trunc = trans->inserted_pre_trunc;
2574  record.updated_pre_trunc = trans->updated_pre_trunc;
2575  record.deleted_pre_trunc = trans->deleted_pre_trunc;
2576  record.t_id = tabstat->t_id;
2577  record.t_shared = tabstat->t_shared;
2578  record.t_truncated = trans->truncated;
2579 
2581  &record, sizeof(TwoPhasePgStatRecord));
2582  }
2583  }
2584 }
PgStat_Counter tuples_updated
Definition: pgstat.h:187
struct PgStat_TableXactStatus * next
Definition: pgstat.h:198
PgStat_Counter deleted_pre_trunc
Definition: pgstat.h:192
PgStat_Counter deleted_pre_trunc
Definition: pgstat.c:260
void RegisterTwoPhaseRecord(TwoPhaseRmgrId rmid, uint16 info, const void *data, uint32 len)
Definition: twophase.c:1182
PgStat_Counter tuples_inserted
Definition: pgstat.h:186
PgStat_Counter updated_pre_trunc
Definition: pgstat.c:259
PgStat_Counter inserted_pre_trunc
Definition: pgstat.c:258
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:195
PgStat_Counter tuples_updated
Definition: pgstat.c:256
PgStat_Counter tuples_deleted
Definition: pgstat.h:188
struct PgStat_SubXactStatus * prev
Definition: pgstat.c:238
#define Assert(condition)
Definition: c.h:804
PgStat_Counter inserted_pre_trunc
Definition: pgstat.h:190
PgStat_Counter tuples_inserted
Definition: pgstat.c:255
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:242
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:176
PgStat_Counter tuples_deleted
Definition: pgstat.c:257
#define TWOPHASE_RM_PGSTAT_ID
Definition: twophase_rmgr.h:26
PgStat_Counter updated_pre_trunc
Definition: pgstat.h:191
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:400
PgStat_TableXactStatus * first
Definition: pgstat.c:239
PgStat_TableStatus * parent
Definition: pgstat.h:196

◆ backend_read_statsfile()

static void backend_read_statsfile ( void  )
static

Definition at line 4596 of file pgstat.c.

References Assert, CHECK_FOR_INTERRUPTS, ereport, errmsg(), GetCurrentTimestamp(), InRecovery, InvalidOid, IsAutoVacuumLauncherProcess(), IsAutoVacuumWorkerProcess(), LOG, MyDatabaseId, pfree(), pg_usleep(), PGSTAT_INQ_LOOP_COUNT, PGSTAT_POLL_LOOP_COUNT, pgstat_read_db_statsfile_timestamp(), pgstat_read_statsfiles(), PGSTAT_RETRY_DELAY, pgstat_send_inquiry(), PGSTAT_STAT_INTERVAL, pgStatRunningInCollector, pstrdup(), timestamptz_to_str(), and TimestampTzPlusMilliseconds.

Referenced by pgstat_fetch_global(), pgstat_fetch_recoveryprefetch(), pgstat_fetch_replslot(), pgstat_fetch_slru(), pgstat_fetch_stat_archiver(), pgstat_fetch_stat_dbentry(), pgstat_fetch_stat_funcentry(), pgstat_fetch_stat_tabentry(), pgstat_fetch_stat_wal(), and pgstat_vacuum_stat().

4597 {
4598  TimestampTz min_ts = 0;
4599  TimestampTz ref_ts = 0;
4600  Oid inquiry_db;
4601  int count;
4602 
4603  /* already read it? */
4604  if (pgStatDBHash)
4605  return;
4607 
4608  /*
4609  * In a normal backend, we check staleness of the data for our own DB, and
4610  * so we send MyDatabaseId in inquiry messages. In the autovac launcher,
4611  * check staleness of the shared-catalog data, and send InvalidOid in
4612  * inquiry messages so as not to force writing unnecessary data.
4613  */
4615  inquiry_db = InvalidOid;
4616  else
4617  inquiry_db = MyDatabaseId;
4618 
4619  /*
4620  * Loop until fresh enough stats file is available or we ran out of time.
4621  * The stats inquiry message is sent repeatedly in case collector drops
4622  * it; but not every single time, as that just swamps the collector.
4623  */
4624  for (count = 0; count < PGSTAT_POLL_LOOP_COUNT; count++)
4625  {
4626  bool ok;
4627  TimestampTz file_ts = 0;
4628  TimestampTz cur_ts;
4629 
4631 
4632  ok = pgstat_read_db_statsfile_timestamp(inquiry_db, false, &file_ts);
4633 
4634  cur_ts = GetCurrentTimestamp();
4635  /* Calculate min acceptable timestamp, if we didn't already */
4636  if (count == 0 || cur_ts < ref_ts)
4637  {
4638  /*
4639  * We set the minimum acceptable timestamp to PGSTAT_STAT_INTERVAL
4640  * msec before now. This indirectly ensures that the collector
4641  * needn't write the file more often than PGSTAT_STAT_INTERVAL. In
4642  * an autovacuum worker, however, we want a lower delay to avoid
4643  * using stale data, so we use PGSTAT_RETRY_DELAY (since the
4644  * number of workers is low, this shouldn't be a problem).
4645  *
4646  * We don't recompute min_ts after sleeping, except in the
4647  * unlikely case that cur_ts went backwards. So we might end up
4648  * accepting a file a bit older than PGSTAT_STAT_INTERVAL. In
4649  * practice that shouldn't happen, though, as long as the sleep
4650  * time is less than PGSTAT_STAT_INTERVAL; and we don't want to
4651  * tell the collector that our cutoff time is less than what we'd
4652  * actually accept.
4653  */
4654  ref_ts = cur_ts;
4656  min_ts = TimestampTzPlusMilliseconds(ref_ts,
4658  else
4659  min_ts = TimestampTzPlusMilliseconds(ref_ts,
4661  }
4662 
4663  /*
4664  * If the file timestamp is actually newer than cur_ts, we must have
4665  * had a clock glitch (system time went backwards) or there is clock
4666  * skew between our processor and the stats collector's processor.
4667  * Accept the file, but send an inquiry message anyway to make
4668  * pgstat_recv_inquiry do a sanity check on the collector's time.
4669  */
4670  if (ok && file_ts > cur_ts)
4671  {
4672  /*
4673  * A small amount of clock skew between processors isn't terribly
4674  * surprising, but a large difference is worth logging. We
4675  * arbitrarily define "large" as 1000 msec.
4676  */
4677  if (file_ts >= TimestampTzPlusMilliseconds(cur_ts, 1000))
4678  {
4679  char *filetime;
4680  char *mytime;
4681 
4682  /* Copy because timestamptz_to_str returns a static buffer */
4683  filetime = pstrdup(timestamptz_to_str(file_ts));
4684  mytime = pstrdup(timestamptz_to_str(cur_ts));
4685  ereport(LOG,
4686  (errmsg("statistics collector's time %s is later than backend local time %s",
4687  filetime, mytime)));
4688  pfree(filetime);
4689  pfree(mytime);
4690  }
4691 
4692  pgstat_send_inquiry(cur_ts, min_ts, inquiry_db);
4693  break;
4694  }
4695 
4696  /* Normal acceptance case: file is not older than cutoff time */
4697  if (ok && file_ts >= min_ts)
4698  break;
4699 
4700  /*
4701  * If we're in crash recovery, the collector may not even be running,
4702  * so work with what we have.
4703  */
4704  if (InRecovery)
4705  break;
4706 
4707  /* Not there or too old, so kick the collector and wait a bit */
4708  if ((count % PGSTAT_INQ_LOOP_COUNT) == 0)
4709  pgstat_send_inquiry(cur_ts, min_ts, inquiry_db);
4710 
4711  pg_usleep(PGSTAT_RETRY_DELAY * 1000L);
4712  }
4713 
4714  if (count >= PGSTAT_POLL_LOOP_COUNT)
4715  ereport(LOG,
4716  (errmsg("using stale statistics instead of current ones "
4717  "because stats collector is not responding")));
4718 
4719  /*
4720  * Autovacuum launcher wants stats about all databases, but a shallow read
4721  * is sufficient. Regular backends want a deep read for just the tables
4722  * they can see (MyDatabaseId + shared catalogs).
4723  */
4726  else
4728 }
#define PGSTAT_RETRY_DELAY
Definition: pgstat.c:82
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
int64 TimestampTz
Definition: timestamp.h:39
#define PGSTAT_INQ_LOOP_COUNT
Definition: pgstat.c:96
char * pstrdup(const char *in)
Definition: mcxt.c:1299
bool InRecovery
Definition: xlog.c:209
static bool pgStatRunningInCollector
Definition: pgstat.c:178
#define LOG
Definition: elog.h:26
unsigned int Oid
Definition: postgres_ext.h:31
#define PGSTAT_POLL_LOOP_COUNT
Definition: pgstat.c:95
void pg_usleep(long microsec)
Definition: signal.c:53
void pfree(void *pointer)
Definition: mcxt.c:1169
static void pgstat_send_inquiry(TimestampTz clock_time, TimestampTz cutoff_time, Oid databaseid)
Definition: pgstat.c:1879
#define PGSTAT_STAT_INTERVAL
Definition: pgstat.c:79
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3448
static HTAB * pgstat_read_statsfiles(Oid onlydb, bool permanent, bool deep)
Definition: pgstat.c:3952
Oid MyDatabaseId
Definition: globals.c:88
#define InvalidOid
Definition: postgres_ext.h:36
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:56
#define ereport(elevel,...)
Definition: elog.h:157
static bool pgstat_read_db_statsfile_timestamp(Oid databaseid, bool permanent, TimestampTz *ts)
Definition: pgstat.c:4419
bool IsAutoVacuumLauncherProcess(void)
Definition: autovacuum.c:3442
#define Assert(condition)
Definition: c.h:804
static HTAB * pgStatDBHash
Definition: pgstat.c:270
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:102
const char * timestamptz_to_str(TimestampTz t)
Definition: timestamp.c:1774

◆ find_funcstat_entry()

PgStat_BackendFunctionEntry* find_funcstat_entry ( Oid  func_id)

Definition at line 1947 of file pgstat.c.

References HASH_FIND, and hash_search().

Referenced by pg_stat_get_xact_function_calls(), pg_stat_get_xact_function_self_time(), and pg_stat_get_xact_function_total_time().

1948 {
1949  if (pgStatFunctions == NULL)
1950  return NULL;
1951 
1953  (void *) &func_id,
1954  HASH_FIND, NULL);
1955 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
static HTAB * pgStatFunctions
Definition: pgstat.c:220

◆ find_tabstat_entry()

PgStat_TableStatus* find_tabstat_entry ( Oid  rel_id)

Definition at line 2148 of file pgstat.c.

References HASH_FIND, hash_search(), and TabStatHashEntry::tsa_entry.

Referenced by pg_stat_get_xact_blocks_fetched(), pg_stat_get_xact_blocks_hit(), pg_stat_get_xact_numscans(), pg_stat_get_xact_tuples_deleted(), pg_stat_get_xact_tuples_fetched(), pg_stat_get_xact_tuples_hot_updated(), pg_stat_get_xact_tuples_inserted(), pg_stat_get_xact_tuples_returned(), and pg_stat_get_xact_tuples_updated().

2149 {
2150  TabStatHashEntry *hash_entry;
2151 
2152  /* If hashtable doesn't exist, there are no entries at all */
2153  if (!pgStatTabHash)
2154  return NULL;
2155 
2156  hash_entry = hash_search(pgStatTabHash, &rel_id, HASH_FIND, NULL);
2157  if (!hash_entry)
2158  return NULL;
2159 
2160  /* Note that this step could also return NULL, but that's correct */
2161  return hash_entry->tsa_entry;
2162 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
static HTAB * pgStatTabHash
Definition: pgstat.c:214
PgStat_TableStatus * tsa_entry
Definition: pgstat.c:208

◆ get_dbstat_filename()

static void get_dbstat_filename ( bool  permanent,
bool  tempname,
Oid  databaseid,
char *  filename,
int  len 
)
static

Definition at line 3803 of file pgstat.c.

References elog, ERROR, pgstat_stat_directory, PGSTAT_STAT_PERMANENT_DIRECTORY, and snprintf.

Referenced by pgstat_read_db_statsfile(), pgstat_recv_dropdb(), and pgstat_write_db_statsfile().

3805 {
3806  int printed;
3807 
3808  /* NB -- pgstat_reset_remove_files knows about the pattern this uses */
3809  printed = snprintf(filename, len, "%s/db_%u.%s",
3810  permanent ? PGSTAT_STAT_PERMANENT_DIRECTORY :
3812  databaseid,
3813  tempname ? "tmp" : "stat");
3814  if (printed >= len)
3815  elog(ERROR, "overlength pgstat path");
3816 }
#define PGSTAT_STAT_PERMANENT_DIRECTORY
Definition: pgstat.h:28
#define ERROR
Definition: elog.h:46
char * pgstat_stat_directory
Definition: pgstat.c:122
static char * filename
Definition: pg_dumpall.c:91
#define elog(elevel,...)
Definition: elog.h:232
#define snprintf
Definition: port.h:216

◆ get_tabstat_entry()

static PgStat_TableStatus * get_tabstat_entry ( Oid  rel_id,
bool  isshared 
)
static

Definition at line 2060 of file pgstat.c.

References HASHCTL::entrysize, HASH_BLOBS, hash_create(), HASH_ELEM, HASH_ENTER, hash_search(), HASHCTL::keysize, MemoryContextAllocZero(), pgStatTabList, PgStat_TableStatus::t_id, PgStat_TableStatus::t_shared, TABSTAT_QUANTUM, TopMemoryContext, TabStatusArray::tsa_entries, TabStatHashEntry::tsa_entry, TabStatusArray::tsa_next, and TabStatusArray::tsa_used.

Referenced by pgstat_initstats(), pgstat_twophase_postabort(), and pgstat_twophase_postcommit().

2061 {
2062  TabStatHashEntry *hash_entry;
2063  PgStat_TableStatus *entry;
2064  TabStatusArray *tsa;
2065  bool found;
2066 
2067  /*
2068  * Create hash table if we don't have it already.
2069  */
2070  if (pgStatTabHash == NULL)
2071  {
2072  HASHCTL ctl;
2073 
2074  ctl.keysize = sizeof(Oid);
2075  ctl.entrysize = sizeof(TabStatHashEntry);
2076 
2077  pgStatTabHash = hash_create("pgstat TabStatusArray lookup hash table",
2079  &ctl,
2080  HASH_ELEM | HASH_BLOBS);
2081  }
2082 
2083  /*
2084  * Find an entry or create a new one.
2085  */
2086  hash_entry = hash_search(pgStatTabHash, &rel_id, HASH_ENTER, &found);
2087  if (!found)
2088  {
2089  /* initialize new entry with null pointer */
2090  hash_entry->tsa_entry = NULL;
2091  }
2092 
2093  /*
2094  * If entry is already valid, we're done.
2095  */
2096  if (hash_entry->tsa_entry)
2097  return hash_entry->tsa_entry;
2098 
2099  /*
2100  * Locate the first pgStatTabList entry with free space, making a new list
2101  * entry if needed. Note that we could get an OOM failure here, but if so
2102  * we have left the hashtable and the list in a consistent state.
2103  */
2104  if (pgStatTabList == NULL)
2105  {
2106  /* Set up first pgStatTabList entry */
2109  sizeof(TabStatusArray));
2110  }
2111 
2112  tsa = pgStatTabList;
2113  while (tsa->tsa_used >= TABSTAT_QUANTUM)
2114  {
2115  if (tsa->tsa_next == NULL)
2116  tsa->tsa_next = (TabStatusArray *)
2118  sizeof(TabStatusArray));
2119  tsa = tsa->tsa_next;
2120  }
2121 
2122  /*
2123  * Allocate a PgStat_TableStatus entry within this list entry. We assume
2124  * the entry was already zeroed, either at creation or after last use.
2125  */
2126  entry = &tsa->tsa_entries[tsa->tsa_used++];
2127  entry->t_id = rel_id;
2128  entry->t_shared = isshared;
2129 
2130  /*
2131  * Now we can fill the entry in pgStatTabHash.
2132  */
2133  hash_entry->tsa_entry = entry;
2134 
2135  return entry;
2136 }
int tsa_used
Definition: pgstat.c:196
#define HASH_ELEM
Definition: hsearch.h:95
Size entrysize
Definition: hsearch.h:76
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
struct TabStatusArray * tsa_next
Definition: pgstat.c:195
unsigned int Oid
Definition: postgres_ext.h:31
struct TabStatHashEntry TabStatHashEntry
static HTAB * pgStatTabHash
Definition: pgstat.c:214
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
PgStat_TableStatus tsa_entries[TABSTAT_QUANTUM]
Definition: pgstat.c:197
MemoryContext TopMemoryContext
Definition: mcxt.c:48
#define HASH_BLOBS
Definition: hsearch.h:97
PgStat_TableStatus * tsa_entry
Definition: pgstat.c:208
Size keysize
Definition: hsearch.h:75
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:906
static TabStatusArray * pgStatTabList
Definition: pgstat.c:200
#define TABSTAT_QUANTUM
Definition: pgstat.c:191

◆ get_tabstat_stack_level()

static PgStat_SubXactStatus* get_tabstat_stack_level ( int  nest_level)
static

Definition at line 2168 of file pgstat.c.

References PgStat_SubXactStatus::first, MemoryContextAlloc(), PgStat_SubXactStatus::nest_level, pgStatXactStack, PgStat_SubXactStatus::prev, and TopTransactionContext.

Referenced by add_tabstat_xact_level(), and AtEOSubXact_PgStat().

2169 {
2170  PgStat_SubXactStatus *xact_state;
2171 
2172  xact_state = pgStatXactStack;
2173  if (xact_state == NULL || xact_state->nest_level != nest_level)
2174  {
2175  xact_state = (PgStat_SubXactStatus *)
2177  sizeof(PgStat_SubXactStatus));
2178  xact_state->nest_level = nest_level;
2179  xact_state->prev = pgStatXactStack;
2180  xact_state->first = NULL;
2181  pgStatXactStack = xact_state;
2182  }
2183  return xact_state;
2184 }
MemoryContext TopTransactionContext
Definition: mcxt.c:53
struct PgStat_SubXactStatus * prev
Definition: pgstat.c:238
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:242
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
PgStat_TableXactStatus * first
Definition: pgstat.c:239

◆ pgstat_clear_snapshot()

void pgstat_clear_snapshot ( void  )

Definition at line 4758 of file pgstat.c.

References MemoryContextDelete(), nReplSlotStats, and pgstat_clear_backend_activity_snapshot().

Referenced by AtEOXact_PgStat(), autovac_refresh_stats(), AutoVacLauncherMain(), pg_stat_clear_snapshot(), and PostPrepare_PgStat().

4759 {
4760  /* Release memory, if any was allocated */
4761  if (pgStatLocalContext)
4763 
4764  /* Reset variables */
4765  pgStatLocalContext = NULL;
4766  pgStatDBHash = NULL;
4767  replSlotStats = NULL;
4768  nReplSlotStats = 0;
4769 
4770  /*
4771  * Historically the backend_status.c facilities lived in this file, and
4772  * were reset with the same function. For now keep it that way, and
4773  * forward the reset request.
4774  */
4776 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
static int nReplSlotStats
Definition: pgstat.c:282
static HTAB * pgStatDBHash
Definition: pgstat.c:270
static MemoryContext pgStatLocalContext
Definition: pgstat.c:269
static PgStat_ReplSlotStats * replSlotStats
Definition: pgstat.c:281
void pgstat_clear_backend_activity_snapshot(void)

◆ pgstat_collect_oids()

static HTAB * pgstat_collect_oids ( Oid  catalogid,
AttrNumber  anum_oid 
)
static

Definition at line 1249 of file pgstat.c.

References AccessShareLock, Assert, CHECK_FOR_INTERRUPTS, CurrentMemoryContext, HASHCTL::entrysize, ForwardScanDirection, GetLatestSnapshot(), HASH_BLOBS, HASH_CONTEXT, hash_create(), HASH_ELEM, HASH_ENTER, hash_search(), HASHCTL::hcxt, heap_getattr, heap_getnext(), HASHCTL::keysize, PGSTAT_TAB_HASH_SIZE, RegisterSnapshot(), RelationGetDescr, table_beginscan(), table_close(), table_endscan(), table_open(), and UnregisterSnapshot().

Referenced by pgstat_vacuum_stat().

1250 {
1251  HTAB *htab;
1252  HASHCTL hash_ctl;
1253  Relation rel;
1254  TableScanDesc scan;
1255  HeapTuple tup;
1256  Snapshot snapshot;
1257 
1258  hash_ctl.keysize = sizeof(Oid);
1259  hash_ctl.entrysize = sizeof(Oid);
1260  hash_ctl.hcxt = CurrentMemoryContext;
1261  htab = hash_create("Temporary table of OIDs",
1263  &hash_ctl,
1265 
1266  rel = table_open(catalogid, AccessShareLock);
1267  snapshot = RegisterSnapshot(GetLatestSnapshot());
1268  scan = table_beginscan(rel, snapshot, 0, NULL);
1269  while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
1270  {
1271  Oid thisoid;
1272  bool isnull;
1273 
1274  thisoid = heap_getattr(tup, anum_oid, RelationGetDescr(rel), &isnull);
1275  Assert(!isnull);
1276 
1278 
1279  (void) hash_search(htab, (void *) &thisoid, HASH_ENTER, NULL);
1280  }
1281  table_endscan(scan);
1282  UnregisterSnapshot(snapshot);
1284 
1285  return htab;
1286 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:810
MemoryContext hcxt
Definition: hsearch.h:86
#define RelationGetDescr(relation)
Definition: rel.h:483
#define PGSTAT_TAB_HASH_SIZE
Definition: pgstat.c:107
#define AccessShareLock
Definition: lockdefs.h:36
Size entrysize
Definition: hsearch.h:76
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
unsigned int Oid
Definition: postgres_ext.h:31
Definition: dynahash.c:219
static TableScanDesc table_beginscan(Relation rel, Snapshot snapshot, int nkeys, struct ScanKeyData *key)
Definition: tableam.h:883
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1340
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:852
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:761
#define HASH_BLOBS
Definition: hsearch.h:97
Size keysize
Definition: hsearch.h:75
#define Assert(condition)
Definition: c.h:804
Snapshot GetLatestSnapshot(void)
Definition: snapmgr.c:325
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:991
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:102
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ pgstat_count_heap_delete()

void pgstat_count_heap_delete ( Relation  rel)

Definition at line 2263 of file pgstat.c.

References add_tabstat_xact_level(), GetCurrentTransactionNestLevel(), PgStat_TableXactStatus::nest_level, RelationData::pgstat_info, PgStat_TableStatus::trans, and PgStat_TableXactStatus::tuples_deleted.

Referenced by heap_abort_speculative(), and heap_delete().

2264 {
2265  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2266 
2267  if (pgstat_info != NULL)
2268  {
2269  /* We have to log the effect at the proper transactional level */
2270  int nest_level = GetCurrentTransactionNestLevel();
2271 
2272  if (pgstat_info->trans == NULL ||
2273  pgstat_info->trans->nest_level != nest_level)
2274  add_tabstat_xact_level(pgstat_info, nest_level);
2275 
2276  pgstat_info->trans->tuples_deleted++;
2277  }
2278 }
PgStat_Counter tuples_deleted
Definition: pgstat.h:188
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:857
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:237
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
Definition: pgstat.c:2190
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:176

◆ pgstat_count_heap_insert()

void pgstat_count_heap_insert ( Relation  rel,
PgStat_Counter  n 
)

Definition at line 2217 of file pgstat.c.

References add_tabstat_xact_level(), GetCurrentTransactionNestLevel(), PgStat_TableXactStatus::nest_level, RelationData::pgstat_info, PgStat_TableStatus::trans, and PgStat_TableXactStatus::tuples_inserted.

Referenced by ExecRefreshMatView(), heap_insert(), and heap_multi_insert().

2218 {
2219  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2220 
2221  if (pgstat_info != NULL)
2222  {
2223  /* We have to log the effect at the proper transactional level */
2224  int nest_level = GetCurrentTransactionNestLevel();
2225 
2226  if (pgstat_info->trans == NULL ||
2227  pgstat_info->trans->nest_level != nest_level)
2228  add_tabstat_xact_level(pgstat_info, nest_level);
2229 
2230  pgstat_info->trans->tuples_inserted += n;
2231  }
2232 }
PgStat_Counter tuples_inserted
Definition: pgstat.h:186
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:857
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:237
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
Definition: pgstat.c:2190
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:176

◆ pgstat_count_heap_update()

void pgstat_count_heap_update ( Relation  rel,
bool  hot 
)

Definition at line 2238 of file pgstat.c.

References add_tabstat_xact_level(), GetCurrentTransactionNestLevel(), PgStat_TableXactStatus::nest_level, RelationData::pgstat_info, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_tuples_hot_updated, PgStat_TableStatus::trans, and PgStat_TableXactStatus::tuples_updated.

Referenced by heap_update().

2239 {
2240  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2241 
2242  if (pgstat_info != NULL)
2243  {
2244  /* We have to log the effect at the proper transactional level */
2245  int nest_level = GetCurrentTransactionNestLevel();
2246 
2247  if (pgstat_info->trans == NULL ||
2248  pgstat_info->trans->nest_level != nest_level)
2249  add_tabstat_xact_level(pgstat_info, nest_level);
2250 
2251  pgstat_info->trans->tuples_updated++;
2252 
2253  /* t_tuples_hot_updated is nontransactional, so just advance it */
2254  if (hot)
2255  pgstat_info->t_counts.t_tuples_hot_updated++;
2256  }
2257 }
PgStat_Counter tuples_updated
Definition: pgstat.h:187
PgStat_Counter t_tuples_hot_updated
Definition: pgstat.h:125
PgStat_TableCounts t_counts
Definition: pgstat.h:177
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:857
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:237
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
Definition: pgstat.c:2190
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:176

◆ pgstat_count_slru_flush()

void pgstat_count_slru_flush ( int  slru_idx)

Definition at line 5900 of file pgstat.c.

References PgStat_MsgSLRU::m_flush, and slru_entry().

Referenced by SimpleLruWriteAll().

5901 {
5902  slru_entry(slru_idx)->m_flush += 1;
5903 }
PgStat_Counter m_flush
Definition: pgstat.h:531
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5852

◆ pgstat_count_slru_page_exists()

void pgstat_count_slru_page_exists ( int  slru_idx)

Definition at line 5882 of file pgstat.c.

References PgStat_MsgSLRU::m_blocks_exists, and slru_entry().

Referenced by SimpleLruDoesPhysicalPageExist().

5883 {
5884  slru_entry(slru_idx)->m_blocks_exists += 1;
5885 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5852
PgStat_Counter m_blocks_exists
Definition: pgstat.h:530

◆ pgstat_count_slru_page_hit()

void pgstat_count_slru_page_hit ( int  slru_idx)

Definition at line 5876 of file pgstat.c.

References PgStat_MsgSLRU::m_blocks_hit, and slru_entry().

Referenced by SimpleLruReadPage(), and SimpleLruReadPage_ReadOnly().

5877 {
5878  slru_entry(slru_idx)->m_blocks_hit += 1;
5879 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5852
PgStat_Counter m_blocks_hit
Definition: pgstat.h:527

◆ pgstat_count_slru_page_read()

void pgstat_count_slru_page_read ( int  slru_idx)

Definition at line 5888 of file pgstat.c.

References PgStat_MsgSLRU::m_blocks_read, and slru_entry().

Referenced by SimpleLruReadPage().

5889 {
5890  slru_entry(slru_idx)->m_blocks_read += 1;
5891 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5852
PgStat_Counter m_blocks_read
Definition: pgstat.h:528

◆ pgstat_count_slru_page_written()

void pgstat_count_slru_page_written ( int  slru_idx)

Definition at line 5894 of file pgstat.c.

References PgStat_MsgSLRU::m_blocks_written, and slru_entry().

Referenced by SlruPhysicalWritePage().

5895 {
5896  slru_entry(slru_idx)->m_blocks_written += 1;
5897 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5852
PgStat_Counter m_blocks_written
Definition: pgstat.h:529

◆ pgstat_count_slru_page_zeroed()

void pgstat_count_slru_page_zeroed ( int  slru_idx)

Definition at line 5870 of file pgstat.c.

References PgStat_MsgSLRU::m_blocks_zeroed, and slru_entry().

Referenced by SimpleLruZeroPage().

5871 {
5872  slru_entry(slru_idx)->m_blocks_zeroed += 1;
5873 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5852
PgStat_Counter m_blocks_zeroed
Definition: pgstat.h:526

◆ pgstat_count_slru_truncate()

void pgstat_count_slru_truncate ( int  slru_idx)

Definition at line 5906 of file pgstat.c.

References PgStat_MsgSLRU::m_truncate, and slru_entry().

Referenced by SimpleLruTruncate().

5907 {
5908  slru_entry(slru_idx)->m_truncate += 1;
5909 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5852
PgStat_Counter m_truncate
Definition: pgstat.h:532

◆ pgstat_count_truncate()

void pgstat_count_truncate ( Relation  rel)

Definition at line 2318 of file pgstat.c.

References add_tabstat_xact_level(), GetCurrentTransactionNestLevel(), PgStat_TableXactStatus::nest_level, RelationData::pgstat_info, pgstat_truncate_save_counters(), PgStat_TableStatus::trans, PgStat_TableXactStatus::tuples_deleted, PgStat_TableXactStatus::tuples_inserted, and PgStat_TableXactStatus::tuples_updated.

Referenced by ExecRefreshMatView(), and ExecuteTruncateGuts().

2319 {
2320  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2321 
2322  if (pgstat_info != NULL)
2323  {
2324  /* We have to log the effect at the proper transactional level */
2325  int nest_level = GetCurrentTransactionNestLevel();
2326 
2327  if (pgstat_info->trans == NULL ||
2328  pgstat_info->trans->nest_level != nest_level)
2329  add_tabstat_xact_level(pgstat_info, nest_level);
2330 
2331  pgstat_truncate_save_counters(pgstat_info->trans);
2332  pgstat_info->trans->tuples_inserted = 0;
2333  pgstat_info->trans->tuples_updated = 0;
2334  pgstat_info->trans->tuples_deleted = 0;
2335  }
2336 }
PgStat_Counter tuples_updated
Definition: pgstat.h:187
PgStat_Counter tuples_inserted
Definition: pgstat.h:186
PgStat_Counter tuples_deleted
Definition: pgstat.h:188
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:857
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:237
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
Definition: pgstat.c:2190
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:176
static void pgstat_truncate_save_counters(PgStat_TableXactStatus *trans)
Definition: pgstat.c:2289

◆ pgstat_db_requested()

static bool pgstat_db_requested ( Oid  databaseid)
static

Definition at line 5733 of file pgstat.c.

References InvalidOid, list_member_oid(), and NIL.

Referenced by pgstat_write_statsfiles().

5734 {
5735  /*
5736  * If any requests are outstanding at all, we should write the stats for
5737  * shared catalogs (the "database" with OID 0). This ensures that
5738  * backends will see up-to-date stats for shared catalogs, even though
5739  * they send inquiry messages mentioning only their own DB.
5740  */
5741  if (databaseid == InvalidOid && pending_write_requests != NIL)
5742  return true;
5743 
5744  /* Search to see if there's an open request to write this database. */
5745  if (list_member_oid(pending_write_requests, databaseid))
5746  return true;
5747 
5748  return false;
5749 }
#define NIL
Definition: pg_list.h:65
static List * pending_write_requests
Definition: pgstat.c:290
#define InvalidOid
Definition: postgres_ext.h:36
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:689

◆ pgstat_drop_database()

void pgstat_drop_database ( Oid  databaseid)

Definition at line 1298 of file pgstat.c.

References PgStat_MsgTabpurge::m_databaseid, PgStat_MsgDropdb::m_databaseid, PgStat_MsgTabpurge::m_hdr, PgStat_MsgDropdb::m_hdr, PgStat_MsgTabpurge::m_nentries, PgStat_MsgTabpurge::m_tableid, MyDatabaseId, offsetof, PGINVALID_SOCKET, PGSTAT_MTYPE_DROPDB, PGSTAT_MTYPE_TABPURGE, pgstat_send(), pgstat_setheader(), and pgStatSock.

Referenced by dropdb(), and pgstat_vacuum_stat().

1299 {
1300  PgStat_MsgDropdb msg;
1301 
1303  return;
1304 
1306  msg.m_databaseid = databaseid;
1307  pgstat_send(&msg, sizeof(msg));
1308 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2973
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
Oid m_databaseid
Definition: pgstat.h:338
PgStat_MsgHdr m_hdr
Definition: pgstat.h:337
#define PGINVALID_SOCKET
Definition: port.h:33
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:2960

◆ pgstat_end_function_usage()

void pgstat_end_function_usage ( PgStat_FunctionCallUsage fcu,
bool  finalize 
)

Definition at line 1967 of file pgstat.c.

References PgStat_FunctionCounts::f_numcalls, PgStat_FunctionCounts::f_self_time, PgStat_FunctionCallUsage::f_start, PgStat_FunctionCounts::f_total_time, PgStat_FunctionCallUsage::fs, have_function_stats, INSTR_TIME_ADD, INSTR_TIME_SET_CURRENT, INSTR_TIME_SUBTRACT, PgStat_FunctionCallUsage::save_f_total_time, PgStat_FunctionCallUsage::save_total, and total_func_time.

Referenced by call_pltcl_start_proc(), EventTriggerInvoke(), ExecCallTriggerFunc(), ExecEvalFuncExprFusage(), ExecEvalFuncExprStrictFusage(), ExecMakeFunctionResultSet(), ExecMakeTableFunctionResult(), ExecuteCallStmt(), and fmgr_security_definer().

1968 {
1969  PgStat_FunctionCounts *fs = fcu->fs;
1970  instr_time f_total;
1971  instr_time f_others;
1972  instr_time f_self;
1973 
1974  /* stats not wanted? */
1975  if (fs == NULL)
1976  return;
1977 
1978  /* total elapsed time in this function call */
1979  INSTR_TIME_SET_CURRENT(f_total);
1980  INSTR_TIME_SUBTRACT(f_total, fcu->f_start);
1981 
1982  /* self usage: elapsed minus anything already charged to other calls */
1983  f_others = total_func_time;
1984  INSTR_TIME_SUBTRACT(f_others, fcu->save_total);
1985  f_self = f_total;
1986  INSTR_TIME_SUBTRACT(f_self, f_others);
1987 
1988  /* update backend-wide total time */
1990 
1991  /*
1992  * Compute the new f_total_time as the total elapsed time added to the
1993  * pre-call value of f_total_time. This is necessary to avoid
1994  * double-counting any time taken by recursive calls of myself. (We do
1995  * not need any similar kluge for self time, since that already excludes
1996  * any recursive calls.)
1997  */
1998  INSTR_TIME_ADD(f_total, fcu->save_f_total_time);
1999 
2000  /* update counters in function stats table */
2001  if (finalize)
2002  fs->f_numcalls++;
2003  fs->f_total_time = f_total;
2004  INSTR_TIME_ADD(fs->f_self_time, f_self);
2005 
2006  /* indicate that we have something to send */
2007  have_function_stats = true;
2008 }
instr_time f_self_time
Definition: pgstat.h:603
instr_time f_start
Definition: pgstat.h:948
instr_time save_total
Definition: pgstat.h:946
struct timeval instr_time
Definition: instr_time.h:150
instr_time f_total_time
Definition: pgstat.h:602
PgStat_FunctionCounts * fs
Definition: pgstat.h:942
PgStat_Counter f_numcalls
Definition: pgstat.h:601
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
#define INSTR_TIME_ADD(x, y)
Definition: instr_time.h:158
static instr_time total_func_time
Definition: pgstat.c:297
static bool have_function_stats
Definition: pgstat.c:226
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
instr_time save_f_total_time
Definition: pgstat.h:944

◆ pgstat_fetch_global()

◆ pgstat_fetch_recoveryprefetch()

PgStat_RecoveryPrefetchStats* pgstat_fetch_recoveryprefetch ( void  )

Definition at line 2896 of file pgstat.c.

References backend_read_statsfile(), and recoveryPrefetchStats.

Referenced by XLogPrefetchRestoreStats().

2897 {
2899 
2900  return &recoveryPrefetchStats;
2901 }
static void backend_read_statsfile(void)
Definition: pgstat.c:4596
static PgStat_RecoveryPrefetchStats recoveryPrefetchStats
Definition: pgstat.c:283

◆ pgstat_fetch_replslot()

PgStat_ReplSlotStats* pgstat_fetch_replslot ( int *  nslots_p)

Definition at line 2880 of file pgstat.c.

References backend_read_statsfile(), nReplSlotStats, and replSlotStats.

Referenced by pg_stat_get_replication_slots().

2881 {
2883 
2884  *nslots_p = nReplSlotStats;
2885  return replSlotStats;
2886 }
static void backend_read_statsfile(void)
Definition: pgstat.c:4596
static int nReplSlotStats
Definition: pgstat.c:282
static PgStat_ReplSlotStats * replSlotStats
Definition: pgstat.c:281

◆ pgstat_fetch_slru()

PgStat_SLRUStats* pgstat_fetch_slru ( void  )

Definition at line 2863 of file pgstat.c.

References backend_read_statsfile(), and slruStats.

Referenced by pg_stat_get_slru().

2864 {
2866 
2867  return slruStats;
2868 }
static void backend_read_statsfile(void)
Definition: pgstat.c:4596
static PgStat_SLRUStats slruStats[SLRU_NUM_ELEMENTS]
Definition: pgstat.c:280

◆ pgstat_fetch_stat_archiver()

PgStat_ArchiverStats* pgstat_fetch_stat_archiver ( void  )

Definition at line 2814 of file pgstat.c.

References archiverStats, and backend_read_statsfile().

Referenced by pg_stat_get_archiver().

2815 {
2817 
2818  return &archiverStats;
2819 }
static void backend_read_statsfile(void)
Definition: pgstat.c:4596
static PgStat_ArchiverStats archiverStats
Definition: pgstat.c:277

◆ pgstat_fetch_stat_dbentry()

PgStat_StatDBEntry* pgstat_fetch_stat_dbentry ( Oid  dbid)

Definition at line 2701 of file pgstat.c.

References backend_read_statsfile(), HASH_FIND, and hash_search().

Referenced by do_autovacuum(), do_start_worker(), pg_stat_get_db_active_time(), pg_stat_get_db_blk_read_time(), pg_stat_get_db_blk_write_time(), pg_stat_get_db_blocks_fetched(), pg_stat_get_db_blocks_hit(), pg_stat_get_db_checksum_failures(), pg_stat_get_db_checksum_last_failure(), pg_stat_get_db_conflict_all(), pg_stat_get_db_conflict_bufferpin(), pg_stat_get_db_conflict_lock(), pg_stat_get_db_conflict_snapshot(), pg_stat_get_db_conflict_startup_deadlock(), pg_stat_get_db_conflict_tablespace(), pg_stat_get_db_deadlocks(), pg_stat_get_db_idle_in_transaction_time(), pg_stat_get_db_session_time(), pg_stat_get_db_sessions(), pg_stat_get_db_sessions_abandoned(), pg_stat_get_db_sessions_fatal(), pg_stat_get_db_sessions_killed(), pg_stat_get_db_stat_reset_time(), pg_stat_get_db_temp_bytes(), pg_stat_get_db_temp_files(), pg_stat_get_db_tuples_deleted(), pg_stat_get_db_tuples_fetched(), pg_stat_get_db_tuples_inserted(), pg_stat_get_db_tuples_returned(), pg_stat_get_db_tuples_updated(), pg_stat_get_db_xact_commit(), pg_stat_get_db_xact_rollback(), pgstat_fetch_stat_funcentry(), rebuild_database_list(), and recheck_relation_needs_vacanalyze().

2702 {
2703  /*
2704  * If not done for this transaction, read the statistics collector stats
2705  * file into some hash tables.
2706  */
2708 
2709  /*
2710  * Lookup the requested database; return NULL if not found
2711  */
2713  (void *) &dbid,
2714  HASH_FIND, NULL);
2715 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
static void backend_read_statsfile(void)
Definition: pgstat.c:4596
static HTAB * pgStatDBHash
Definition: pgstat.c:270

◆ pgstat_fetch_stat_funcentry()

PgStat_StatFuncEntry* pgstat_fetch_stat_funcentry ( Oid  func_id)

Definition at line 2784 of file pgstat.c.

References backend_read_statsfile(), PgStat_StatDBEntry::functions, HASH_FIND, hash_search(), MyDatabaseId, and pgstat_fetch_stat_dbentry().

Referenced by pg_stat_get_function_calls(), pg_stat_get_function_self_time(), and pg_stat_get_function_total_time().

2785 {
2786  PgStat_StatDBEntry *dbentry;
2787  PgStat_StatFuncEntry *funcentry = NULL;
2788 
2789  /* load the stats file if needed */
2791 
2792  /* Lookup our database, then find the requested function. */
2794  if (dbentry != NULL && dbentry->functions != NULL)
2795  {
2796  funcentry = (PgStat_StatFuncEntry *) hash_search(dbentry->functions,
2797  (void *) &func_id,
2798  HASH_FIND, NULL);
2799  }
2800 
2801  return funcentry;
2802 }
HTAB * functions
Definition: pgstat.h:794
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2701
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
static void backend_read_statsfile(void)
Definition: pgstat.c:4596
Oid MyDatabaseId
Definition: globals.c:88

◆ pgstat_fetch_stat_tabentry()

PgStat_StatTabEntry* pgstat_fetch_stat_tabentry ( Oid  relid)

Definition at line 2728 of file pgstat.c.

References backend_read_statsfile(), HASH_FIND, hash_search(), InvalidOid, MyDatabaseId, and PgStat_StatDBEntry::tables.

Referenced by do_autovacuum(), index_concurrently_swap(), pg_stat_get_analyze_count(), pg_stat_get_autoanalyze_count(), pg_stat_get_autovacuum_count(), pg_stat_get_blocks_fetched(), pg_stat_get_blocks_hit(), pg_stat_get_dead_tuples(), pg_stat_get_ins_since_vacuum(), pg_stat_get_last_analyze_time(), pg_stat_get_last_autoanalyze_time(), pg_stat_get_last_autovacuum_time(), pg_stat_get_last_vacuum_time(), pg_stat_get_live_tuples(), pg_stat_get_mod_since_analyze(), pg_stat_get_numscans(), pg_stat_get_tuples_deleted(), pg_stat_get_tuples_fetched(), pg_stat_get_tuples_hot_updated(), pg_stat_get_tuples_inserted(), pg_stat_get_tuples_returned(), pg_stat_get_tuples_updated(), and pg_stat_get_vacuum_count().

2729 {
2730  Oid dbid;
2731  PgStat_StatDBEntry *dbentry;
2732  PgStat_StatTabEntry *tabentry;
2733 
2734  /*
2735  * If not done for this transaction, read the statistics collector stats
2736  * file into some hash tables.
2737  */
2739 
2740  /*
2741  * Lookup our database, then look in its table hash table.
2742  */
2743  dbid = MyDatabaseId;
2745  (void *) &dbid,
2746  HASH_FIND, NULL);
2747  if (dbentry != NULL && dbentry->tables != NULL)
2748  {
2749  tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2750  (void *) &relid,
2751  HASH_FIND, NULL);
2752  if (tabentry)
2753  return tabentry;
2754  }
2755 
2756  /*
2757  * If we didn't find it, maybe it's a shared table.
2758  */
2759  dbid = InvalidOid;
2761  (void *) &dbid,
2762  HASH_FIND, NULL);
2763  if (dbentry != NULL && dbentry->tables != NULL)
2764  {
2765  tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2766  (void *) &relid,
2767  HASH_FIND, NULL);
2768  if (tabentry)
2769  return tabentry;
2770  }
2771 
2772  return NULL;
2773 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
unsigned int Oid
Definition: postgres_ext.h:31
static void backend_read_statsfile(void)
Definition: pgstat.c:4596
Oid MyDatabaseId
Definition: globals.c:88
#define InvalidOid
Definition: postgres_ext.h:36
static HTAB * pgStatDBHash
Definition: pgstat.c:270

◆ pgstat_fetch_stat_wal()

PgStat_WalStats* pgstat_fetch_stat_wal ( void  )

Definition at line 2847 of file pgstat.c.

References backend_read_statsfile(), and walStats.

Referenced by pg_stat_get_wal().

2848 {
2850 
2851  return &walStats;
2852 }
static PgStat_WalStats walStats
Definition: pgstat.c:279
static void backend_read_statsfile(void)
Definition: pgstat.c:4596

◆ pgstat_get_db_entry()

static PgStat_StatDBEntry * pgstat_get_db_entry ( Oid  databaseid,
bool  create 
)
static

Definition at line 3557 of file pgstat.c.

References generate_unaccent_rules::action, HASH_ENTER, HASH_FIND, hash_search(), and reset_dbentry_counters().

Referenced by pgstat_recv_analyze(), pgstat_recv_anl_ancestors(), pgstat_recv_autovac(), pgstat_recv_checksum_failure(), pgstat_recv_connstat(), pgstat_recv_deadlock(), pgstat_recv_dropdb(), pgstat_recv_funcpurge(), pgstat_recv_funcstat(), pgstat_recv_inquiry(), pgstat_recv_recoveryconflict(), pgstat_recv_resetcounter(), pgstat_recv_resetsinglecounter(), pgstat_recv_tabpurge(), pgstat_recv_tabstat(), pgstat_recv_tempfile(), and pgstat_recv_vacuum().

3558 {
3559  PgStat_StatDBEntry *result;
3560  bool found;
3561  HASHACTION action = (create ? HASH_ENTER : HASH_FIND);
3562 
3563  /* Lookup or create the hash table entry for this database */
3565  &databaseid,
3566  action, &found);
3567 
3568  if (!create && !found)
3569  return NULL;
3570 
3571  /*
3572  * If not found, initialize the new one. This creates empty hash tables
3573  * for tables and functions, too.
3574  */
3575  if (!found)
3576  reset_dbentry_counters(result);
3577 
3578  return result;
3579 }
static void reset_dbentry_counters(PgStat_StatDBEntry *dbentry)
Definition: pgstat.c:3499
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
static HTAB * pgStatDBHash
Definition: pgstat.c:270
HASHACTION
Definition: hsearch.h:111

◆ pgstat_get_tab_entry()

static PgStat_StatTabEntry * pgstat_get_tab_entry ( PgStat_StatDBEntry dbentry,
Oid  tableoid,
bool  create 
)
static

Definition at line 3588 of file pgstat.c.

References generate_unaccent_rules::action, PgStat_StatTabEntry::analyze_count, PgStat_StatTabEntry::analyze_timestamp, PgStat_StatTabEntry::autovac_analyze_count, PgStat_StatTabEntry::autovac_analyze_timestamp, PgStat_StatTabEntry::autovac_vacuum_count, PgStat_StatTabEntry::autovac_vacuum_timestamp, PgStat_StatTabEntry::blocks_fetched, PgStat_StatTabEntry::blocks_hit, PgStat_StatTabEntry::changes_since_analyze, PgStat_StatTabEntry::changes_since_analyze_reported, HASH_ENTER, HASH_FIND, hash_search(), PgStat_StatTabEntry::inserts_since_vacuum, PgStat_StatTabEntry::n_dead_tuples, PgStat_StatTabEntry::n_live_tuples, PgStat_StatTabEntry::numscans, PgStat_StatDBEntry::tables, PgStat_StatTabEntry::tuples_deleted, PgStat_StatTabEntry::tuples_fetched, PgStat_StatTabEntry::tuples_hot_updated, PgStat_StatTabEntry::tuples_inserted, PgStat_StatTabEntry::tuples_returned, PgStat_StatTabEntry::tuples_updated, PgStat_StatTabEntry::vacuum_count, and PgStat_StatTabEntry::vacuum_timestamp.

Referenced by pgstat_recv_analyze(), pgstat_recv_anl_ancestors(), and pgstat_recv_vacuum().

3589 {
3590  PgStat_StatTabEntry *result;
3591  bool found;
3592  HASHACTION action = (create ? HASH_ENTER : HASH_FIND);
3593 
3594  /* Lookup or create the hash table entry for this table */
3595  result = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
3596  &tableoid,
3597  action, &found);
3598 
3599  if (!create && !found)
3600  return NULL;
3601 
3602  /* If not found, initialize the new one. */
3603  if (!found)
3604  {
3605  result->numscans = 0;
3606  result->tuples_returned = 0;
3607  result->tuples_fetched = 0;
3608  result->tuples_inserted = 0;
3609  result->tuples_updated = 0;
3610  result->tuples_deleted = 0;
3611  result->tuples_hot_updated = 0;
3612  result->n_live_tuples = 0;
3613  result->n_dead_tuples = 0;
3614  result->changes_since_analyze = 0;
3615  result->changes_since_analyze_reported = 0;
3616  result->inserts_since_vacuum = 0;
3617  result->blocks_fetched = 0;
3618  result->blocks_hit = 0;
3619  result->vacuum_timestamp = 0;
3620  result->vacuum_count = 0;
3621  result->autovac_vacuum_timestamp = 0;
3622  result->autovac_vacuum_count = 0;
3623  result->analyze_timestamp = 0;
3624  result->analyze_count = 0;
3625  result->autovac_analyze_timestamp = 0;
3626  result->autovac_analyze_count = 0;
3627  }
3628 
3629  return result;
3630 }
PgStat_Counter analyze_count
Definition: pgstat.h:830
PgStat_Counter tuples_returned
Definition: pgstat.h:808
TimestampTz autovac_analyze_timestamp
Definition: pgstat.h:831
PgStat_Counter vacuum_count
Definition: pgstat.h:826
PgStat_Counter numscans
Definition: pgstat.h:806
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
PgStat_Counter tuples_hot_updated
Definition: pgstat.h:814
PgStat_Counter tuples_inserted
Definition: pgstat.h:811
PgStat_Counter n_live_tuples
Definition: pgstat.h:816
PgStat_Counter inserts_since_vacuum
Definition: pgstat.h:820
TimestampTz vacuum_timestamp
Definition: pgstat.h:825
PgStat_Counter n_dead_tuples
Definition: pgstat.h:817
PgStat_Counter tuples_fetched
Definition: pgstat.h:809
PgStat_Counter blocks_hit
Definition: pgstat.h:823
TimestampTz analyze_timestamp
Definition: pgstat.h:829
PgStat_Counter changes_since_analyze
Definition: pgstat.h:818
PgStat_Counter tuples_deleted
Definition: pgstat.h:813
PgStat_Counter tuples_updated
Definition: pgstat.h:812
TimestampTz autovac_vacuum_timestamp
Definition: pgstat.h:827
PgStat_Counter autovac_analyze_count
Definition: pgstat.h:832
PgStat_Counter changes_since_analyze_reported
Definition: pgstat.h:819
PgStat_Counter blocks_fetched
Definition: pgstat.h:822
HASHACTION
Definition: hsearch.h:111
PgStat_Counter autovac_vacuum_count
Definition: pgstat.h:828

◆ pgstat_init()

void pgstat_init ( void  )

Definition at line 380 of file pgstat.c.

References addrinfo::ai_next, AI_PASSIVE, bind, closesocket, connect, EINTR, ereport, errcode(), errcode_for_socket_access(), errmsg(), gai_strerror, LOG, pg_freeaddrinfo_all(), pg_getaddrinfo_all(), pg_set_noblock(), PGC_INTERNAL, PGC_S_OVERRIDE, PGINVALID_SOCKET, PGSTAT_MAX_MSG_SIZE, PGSTAT_MIN_RCVBUF, pgStatAddr, pgStatSock, recv, ReserveExternalFD(), select, send, SetConfigOption(), socket, StaticAssertStmt, and TESTBYTEVAL.

Referenced by PostmasterMain().

381 {
382  ACCEPT_TYPE_ARG3 alen;
383  struct addrinfo *addrs = NULL,
384  *addr,
385  hints;
386  int ret;
387  fd_set rset;
388  struct timeval tv;
389  char test_byte;
390  int sel_res;
391  int tries = 0;
392 
393 #define TESTBYTEVAL ((char) 199)
394 
395  /*
396  * This static assertion verifies that we didn't mess up the calculations
397  * involved in selecting maximum payload sizes for our UDP messages.
398  * Because the only consequence of overrunning PGSTAT_MAX_MSG_SIZE would
399  * be silent performance loss from fragmentation, it seems worth having a
400  * compile-time cross-check that we didn't.
401  */
403  "maximum stats message size exceeds PGSTAT_MAX_MSG_SIZE");
404 
405  /*
406  * Create the UDP socket for sending and receiving statistic messages
407  */
408  hints.ai_flags = AI_PASSIVE;
409  hints.ai_family = AF_UNSPEC;
410  hints.ai_socktype = SOCK_DGRAM;
411  hints.ai_protocol = 0;
412  hints.ai_addrlen = 0;
413  hints.ai_addr = NULL;
414  hints.ai_canonname = NULL;
415  hints.ai_next = NULL;
416  ret = pg_getaddrinfo_all("localhost", NULL, &hints, &addrs);
417  if (ret || !addrs)
418  {
419  ereport(LOG,
420  (errmsg("could not resolve \"localhost\": %s",
421  gai_strerror(ret))));
422  goto startup_failed;
423  }
424 
425  /*
426  * On some platforms, pg_getaddrinfo_all() may return multiple addresses
427  * only one of which will actually work (eg, both IPv6 and IPv4 addresses
428  * when kernel will reject IPv6). Worse, the failure may occur at the
429  * bind() or perhaps even connect() stage. So we must loop through the
430  * results till we find a working combination. We will generate LOG
431  * messages, but no error, for bogus combinations.
432  */
433  for (addr = addrs; addr; addr = addr->ai_next)
434  {
435 #ifdef HAVE_UNIX_SOCKETS
436  /* Ignore AF_UNIX sockets, if any are returned. */
437  if (addr->ai_family == AF_UNIX)
438  continue;
439 #endif
440 
441  if (++tries > 1)
442  ereport(LOG,
443  (errmsg("trying another address for the statistics collector")));
444 
445  /*
446  * Create the socket.
447  */
448  if ((pgStatSock = socket(addr->ai_family, SOCK_DGRAM, 0)) == PGINVALID_SOCKET)
449  {
450  ereport(LOG,
452  errmsg("could not create socket for statistics collector: %m")));
453  continue;
454  }
455 
456  /*
457  * Bind it to a kernel assigned port on localhost and get the assigned
458  * port via getsockname().
459  */
460  if (bind(pgStatSock, addr->ai_addr, addr->ai_addrlen) < 0)
461  {
462  ereport(LOG,
464  errmsg("could not bind socket for statistics collector: %m")));
467  continue;
468  }
469 
470  alen = sizeof(pgStatAddr);
471  if (getsockname(pgStatSock, (struct sockaddr *) &pgStatAddr, &alen) < 0)
472  {
473  ereport(LOG,
475  errmsg("could not get address of socket for statistics collector: %m")));
478  continue;
479  }
480 
481  /*
482  * Connect the socket to its own address. This saves a few cycles by
483  * not having to respecify the target address on every send. This also
484  * provides a kernel-level check that only packets from this same
485  * address will be received.
486  */
487  if (connect(pgStatSock, (struct sockaddr *) &pgStatAddr, alen) < 0)
488  {
489  ereport(LOG,
491  errmsg("could not connect socket for statistics collector: %m")));
494  continue;
495  }
496 
497  /*
498  * Try to send and receive a one-byte test message on the socket. This
499  * is to catch situations where the socket can be created but will not
500  * actually pass data (for instance, because kernel packet filtering
501  * rules prevent it).
502  */
503  test_byte = TESTBYTEVAL;
504 
505 retry1:
506  if (send(pgStatSock, &test_byte, 1, 0) != 1)
507  {
508  if (errno == EINTR)
509  goto retry1; /* if interrupted, just retry */
510  ereport(LOG,
512  errmsg("could not send test message on socket for statistics collector: %m")));
515  continue;
516  }
517 
518  /*
519  * There could possibly be a little delay before the message can be
520  * received. We arbitrarily allow up to half a second before deciding
521  * it's broken.
522  */
523  for (;;) /* need a loop to handle EINTR */
524  {
525  FD_ZERO(&rset);
526  FD_SET(pgStatSock, &rset);
527 
528  tv.tv_sec = 0;
529  tv.tv_usec = 500000;
530  sel_res = select(pgStatSock + 1, &rset, NULL, NULL, &tv);
531  if (sel_res >= 0 || errno != EINTR)
532  break;
533  }
534  if (sel_res < 0)
535  {
536  ereport(LOG,
538  errmsg("select() failed in statistics collector: %m")));
541  continue;
542  }
543  if (sel_res == 0 || !FD_ISSET(pgStatSock, &rset))
544  {
545  /*
546  * This is the case we actually think is likely, so take pains to
547  * give a specific message for it.
548  *
549  * errno will not be set meaningfully here, so don't use it.
550  */
551  ereport(LOG,
552  (errcode(ERRCODE_CONNECTION_FAILURE),
553  errmsg("test message did not get through on socket for statistics collector")));
556  continue;
557  }
558 
559  test_byte++; /* just make sure variable is changed */
560 
561 retry2:
562  if (recv(pgStatSock, &test_byte, 1, 0) != 1)
563  {
564  if (errno == EINTR)
565  goto retry2; /* if interrupted, just retry */
566  ereport(LOG,
568  errmsg("could not receive test message on socket for statistics collector: %m")));
571  continue;
572  }
573 
574  if (test_byte != TESTBYTEVAL) /* strictly paranoia ... */
575  {
576  ereport(LOG,
577  (errcode(ERRCODE_INTERNAL_ERROR),
578  errmsg("incorrect test message transmission on socket for statistics collector")));
581  continue;
582  }
583 
584  /* If we get here, we have a working socket */
585  break;
586  }
587 
588  /* Did we find a working address? */
589  if (!addr || pgStatSock == PGINVALID_SOCKET)
590  goto startup_failed;
591 
592  /*
593  * Set the socket to non-blocking IO. This ensures that if the collector
594  * falls behind, statistics messages will be discarded; backends won't
595  * block waiting to send messages to the collector.
596  */
598  {
599  ereport(LOG,
601  errmsg("could not set statistics collector socket to nonblocking mode: %m")));
602  goto startup_failed;
603  }
604 
605  /*
606  * Try to ensure that the socket's receive buffer is at least
607  * PGSTAT_MIN_RCVBUF bytes, so that it won't easily overflow and lose
608  * data. Use of UDP protocol means that we are willing to lose data under
609  * heavy load, but we don't want it to happen just because of ridiculously
610  * small default buffer sizes (such as 8KB on older Windows versions).
611  */
612  {
613  int old_rcvbuf;
614  int new_rcvbuf;
615  ACCEPT_TYPE_ARG3 rcvbufsize = sizeof(old_rcvbuf);
616 
617  if (getsockopt(pgStatSock, SOL_SOCKET, SO_RCVBUF,
618  (char *) &old_rcvbuf, &rcvbufsize) < 0)
619  {
620  ereport(LOG,
621  (errmsg("getsockopt(%s) failed: %m", "SO_RCVBUF")));
622  /* if we can't get existing size, always try to set it */
623  old_rcvbuf = 0;
624  }
625 
626  new_rcvbuf = PGSTAT_MIN_RCVBUF;
627  if (old_rcvbuf < new_rcvbuf)
628  {
629  if (setsockopt(pgStatSock, SOL_SOCKET, SO_RCVBUF,
630  (char *) &new_rcvbuf, sizeof(new_rcvbuf)) < 0)
631  ereport(LOG,
632  (errmsg("setsockopt(%s) failed: %m", "SO_RCVBUF")));
633  }
634  }
635 
636  pg_freeaddrinfo_all(hints.ai_family, addrs);
637 
638  /* Now that we have a long-lived socket, tell fd.c about it. */
640 
641  return;
642 
643 startup_failed:
644  ereport(LOG,
645  (errmsg("disabling statistics collector for lack of working socket")));
646 
647  if (addrs)
648  pg_freeaddrinfo_all(hints.ai_family, addrs);
649 
653 
654  /*
655  * Adjust GUC variables to suppress useless activity, and for debugging
656  * purposes (seeing track_counts off is a clue that we failed here). We
657  * use PGC_S_OVERRIDE because there is no point in trying to turn it back
658  * on from postgresql.conf without a restart.
659  */
660  SetConfigOption("track_counts", "off", PGC_INTERNAL, PGC_S_OVERRIDE);
661 }
void pg_freeaddrinfo_all(int hint_ai_family, struct addrinfo *ai)
Definition: ip.c:88
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
#define closesocket
Definition: port.h:332
int errcode(int sqlerrcode)
Definition: elog.c:698
#define connect(s, name, namelen)
Definition: win32_port.h:463
#define LOG
Definition: elog.h:26
#define bind(s, addr, addrlen)
Definition: win32_port.h:460
#define recv(s, buf, len, flags)
Definition: win32_port.h:465
#define gai_strerror
Definition: getaddrinfo.h:146
int pg_getaddrinfo_all(const char *hostname, const char *servname, const struct addrinfo *hintp, struct addrinfo **result)
Definition: ip.c:57
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:918
#define PGSTAT_MIN_RCVBUF
Definition: pgstat.c:99
#define TESTBYTEVAL
void ReserveExternalFD(void)
Definition: fd.c:1130
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:8103
#define AI_PASSIVE
Definition: getaddrinfo.h:62
#define select(n, r, w, e, timeout)
Definition: win32_port.h:464
int errcode_for_socket_access(void)
Definition: elog.c:792
#define socket(af, type, protocol)
Definition: win32_port.h:459
#define PGSTAT_MAX_MSG_SIZE
Definition: pgstat.h:238
#define PGINVALID_SOCKET
Definition: port.h:33
#define ereport(elevel,...)
Definition: elog.h:157
static struct sockaddr_storage pgStatAddr
Definition: pgstat.c:174
bool pg_set_noblock(pgsocket sock)
Definition: noblock.c:25
struct addrinfo * ai_next
Definition: getaddrinfo.h:107
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define EINTR
Definition: win32_port.h:343
#define send(s, buf, len, flags)
Definition: win32_port.h:466

◆ pgstat_init_function_usage()

void pgstat_init_function_usage ( FunctionCallInfo  fcinfo,
PgStat_FunctionCallUsage fcu 
)

Definition at line 1896 of file pgstat.c.

References HASHCTL::entrysize, PgStat_BackendFunctionEntry::f_counts, PgStat_FunctionCallUsage::f_start, PgStat_FunctionCounts::f_total_time, FunctionCallInfoBaseData::flinfo, FmgrInfo::fn_oid, PgStat_FunctionCallUsage::fs, HASH_BLOBS, hash_create(), HASH_ELEM, HASH_ENTER, hash_search(), INSTR_TIME_SET_CURRENT, HASHCTL::keysize, MemSet, PGSTAT_FUNCTION_HASH_SIZE, PgStat_FunctionCallUsage::save_f_total_time, PgStat_FunctionCallUsage::save_total, and total_func_time.

Referenced by call_pltcl_start_proc(), EventTriggerInvoke(), ExecCallTriggerFunc(), ExecEvalFuncExprFusage(), ExecEvalFuncExprStrictFusage(), ExecMakeFunctionResultSet(), ExecMakeTableFunctionResult(), ExecuteCallStmt(), and fmgr_security_definer().

1898 {
1899  PgStat_BackendFunctionEntry *htabent;
1900  bool found;
1901 
1902  if (pgstat_track_functions <= fcinfo->flinfo->fn_stats)
1903  {
1904  /* stats not wanted */
1905  fcu->fs = NULL;
1906  return;
1907  }
1908 
1909  if (!pgStatFunctions)
1910  {
1911  /* First time through - initialize function stat table */
1912  HASHCTL hash_ctl;
1913 
1914  hash_ctl.keysize = sizeof(Oid);
1915  hash_ctl.entrysize = sizeof(PgStat_BackendFunctionEntry);
1916  pgStatFunctions = hash_create("Function stat entries",
1918  &hash_ctl,
1919  HASH_ELEM | HASH_BLOBS);
1920  }
1921 
1922  /* Get the stats entry for this function, create if necessary */
1923  htabent = hash_search(pgStatFunctions, &fcinfo->flinfo->fn_oid,
1924  HASH_ENTER, &found);
1925  if (!found)
1926  MemSet(&htabent->f_counts, 0, sizeof(PgStat_FunctionCounts));
1927 
1928  fcu->fs = &htabent->f_counts;
1929 
1930  /* save stats for this function, later used to compensate for recursion */
1931  fcu->save_f_total_time = htabent->f_counts.f_total_time;
1932 
1933  /* save current backend-wide total time */
1934  fcu->save_total = total_func_time;
1935 
1936  /* get clock time as of function start */
1938 }
instr_time f_start
Definition: pgstat.h:948
#define HASH_ELEM
Definition: hsearch.h:95
instr_time save_total
Definition: pgstat.h:946
Size entrysize
Definition: hsearch.h:76
#define MemSet(start, val, len)
Definition: c.h:1008
instr_time f_total_time
Definition: pgstat.h:602
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_FunctionCounts * fs
Definition: pgstat.h:942
PgStat_FunctionCounts f_counts
Definition: pgstat.h:613
#define PGSTAT_FUNCTION_HASH_SIZE
Definition: pgstat.c:108
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
#define HASH_BLOBS
Definition: hsearch.h:97
FmgrInfo * flinfo
Definition: fmgr.h:87
Size keysize
Definition: hsearch.h:75
Oid fn_oid
Definition: fmgr.h:59
static instr_time total_func_time
Definition: pgstat.c:297
static HTAB * pgStatFunctions
Definition: pgstat.c:220
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
instr_time save_f_total_time
Definition: pgstat.h:944
struct PgStat_BackendFunctionEntry PgStat_BackendFunctionEntry

◆ pgstat_initialize()

void pgstat_initialize ( void  )

Definition at line 2934 of file pgstat.c.

References on_shmem_exit(), pgstat_shutdown_hook(), and pgWalUsage.

Referenced by AuxiliaryProcessMain(), and InitPostgres().

2935 {
2936  /*
2937  * Initialize prevWalUsage with pgWalUsage so that pgstat_report_wal() can
2938  * calculate how much pgWalUsage counters are increased by substracting
2939  * prevWalUsage from pgWalUsage.
2940  */
2942 
2943  /* Set up a process-exit hook to clean up */
2945 }
static void pgstat_shutdown_hook(int code, Datum arg)
Definition: pgstat.c:2912
WalUsage pgWalUsage
Definition: instrument.c:22
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
static WalUsage prevWalUsage
Definition: pgstat.c:140

◆ pgstat_initstats()

void pgstat_initstats ( Relation  rel)

Definition at line 2024 of file pgstat.c.

References get_tabstat_entry(), PGINVALID_SOCKET, RelationData::pgstat_info, pgstat_track_counts, pgStatSock, RelationData::rd_id, RelationData::rd_rel, and PgStat_TableStatus::t_id.

Referenced by relation_open(), and try_relation_open().

2025 {
2026  Oid rel_id = rel->rd_id;
2027  char relkind = rel->rd_rel->relkind;
2028 
2029  /* We only count stats for things that have storage */
2030  if (!RELKIND_HAS_STORAGE(relkind) &&
2031  relkind != RELKIND_PARTITIONED_TABLE)
2032  {
2033  rel->pgstat_info = NULL;
2034  return;
2035  }
2036 
2038  {
2039  /* We're not counting at all */
2040  rel->pgstat_info = NULL;
2041  return;
2042  }
2043 
2044  /*
2045  * If we already set up this relation in the current transaction, nothing
2046  * to do.
2047  */
2048  if (rel->pgstat_info != NULL &&
2049  rel->pgstat_info->t_id == rel_id)
2050  return;
2051 
2052  /* Else find or make the PgStat_TableStatus entry, and update link */
2053  rel->pgstat_info = get_tabstat_entry(rel_id, rel->rd_rel->relisshared);
2054 }
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
Form_pg_class rd_rel
Definition: rel.h:110
unsigned int Oid
Definition: postgres_ext.h:31
bool pgstat_track_counts
Definition: pgstat.c:115
Oid rd_id
Definition: rel.h:112
static PgStat_TableStatus * get_tabstat_entry(Oid rel_id, bool isshared)
Definition: pgstat.c:2060
#define PGINVALID_SOCKET
Definition: port.h:33
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:237

◆ pgstat_ping()

void pgstat_ping ( void  )

Definition at line 1861 of file pgstat.c.

References PgStat_MsgDummy::m_hdr, PGINVALID_SOCKET, PGSTAT_MTYPE_DUMMY, pgstat_send(), pgstat_setheader(), and pgStatSock.

1862 {
1863  PgStat_MsgDummy msg;
1864 
1866  return;
1867 
1869  pgstat_send(&msg, sizeof(msg));
1870 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2973
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
#define PGINVALID_SOCKET
Definition: port.h:33
PgStat_MsgHdr m_hdr
Definition: pgstat.h:248
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:2960

◆ pgstat_read_db_statsfile()

static void pgstat_read_db_statsfile ( Oid  databaseid,
HTAB tabhash,
HTAB funchash,
bool  permanent 
)
static

Definition at line 4256 of file pgstat.c.

References AllocateFile(), DEBUG2, elog, ereport, errcode_for_file_access(), errmsg(), FreeFile(), PgStat_StatFuncEntry::functionid, get_dbstat_filename(), HASH_ENTER, hash_search(), LOG, MAXPGPATH, PG_BINARY_R, PGSTAT_FILE_FORMAT_ID, pgStatRunningInCollector, PgStat_StatTabEntry::tableid, and WARNING.

Referenced by pgstat_read_statsfiles().

4258 {
4259  PgStat_StatTabEntry *tabentry;
4260  PgStat_StatTabEntry tabbuf;
4261  PgStat_StatFuncEntry funcbuf;
4262  PgStat_StatFuncEntry *funcentry;
4263  FILE *fpin;
4264  int32 format_id;
4265  bool found;
4266  char statfile[MAXPGPATH];
4267 
4268  get_dbstat_filename(permanent, false, databaseid, statfile, MAXPGPATH);
4269 
4270  /*
4271  * Try to open the stats file. If it doesn't exist, the backends simply
4272  * return zero for anything and the collector simply starts from scratch
4273  * with empty counters.
4274  *
4275  * ENOENT is a possibility if the stats collector is not running or has
4276  * not yet written the stats file the first time. Any other failure
4277  * condition is suspicious.
4278  */
4279  if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
4280  {
4281  if (errno != ENOENT)
4284  errmsg("could not open statistics file \"%s\": %m",
4285  statfile)));
4286  return;
4287  }
4288 
4289  /*
4290  * Verify it's of the expected format.
4291  */
4292  if (fread(&format_id, 1, sizeof(format_id), fpin) != sizeof(format_id) ||
4293  format_id != PGSTAT_FILE_FORMAT_ID)
4294  {
4295  ereport(pgStatRunningInCollector ? LOG : WARNING,
4296  (errmsg("corrupted statistics file \"%s\"", statfile)));
4297  goto done;
4298  }
4299 
4300  /*
4301  * We found an existing collector stats file. Read it and put all the
4302  * hashtable entries into place.
4303  */
4304  for (;;)
4305  {
4306  switch (fgetc(fpin))
4307  {
4308  /*
4309  * 'T' A PgStat_StatTabEntry follows.
4310  */
4311  case 'T':
4312  if (fread(&tabbuf, 1, sizeof(PgStat_StatTabEntry),
4313  fpin) != sizeof(PgStat_StatTabEntry))
4314  {
4315  ereport(pgStatRunningInCollector ? LOG : WARNING,
4316  (errmsg("corrupted statistics file \"%s\"",
4317  statfile)));
4318  goto done;
4319  }
4320 
4321  /*
4322  * Skip if table data not wanted.
4323  */
4324  if (tabhash == NULL)
4325  break;
4326 
4327  tabentry = (PgStat_StatTabEntry *) hash_search(tabhash,
4328  (void *) &tabbuf.tableid,
4329  HASH_ENTER, &found);
4330 
4331  if (found)
4332  {
4333  ereport(pgStatRunningInCollector ? LOG : WARNING,
4334  (errmsg("corrupted statistics file \"%s\"",
4335  statfile)));
4336  goto done;
4337  }
4338 
4339  memcpy(tabentry, &tabbuf, sizeof(tabbuf));
4340  break;
4341 
4342  /*
4343  * 'F' A PgStat_StatFuncEntry follows.
4344  */
4345  case 'F':
4346  if (fread(&funcbuf, 1, sizeof(PgStat_StatFuncEntry),
4347  fpin) != sizeof(PgStat_StatFuncEntry))
4348  {
4349  ereport(pgStatRunningInCollector ? LOG : WARNING,
4350  (errmsg("corrupted statistics file \"%s\"",
4351  statfile)));
4352  goto done;
4353  }
4354 
4355  /*
4356  * Skip if function data not wanted.
4357  */
4358  if (funchash == NULL)
4359  break;
4360 
4361  funcentry = (PgStat_StatFuncEntry *) hash_search(funchash,
4362  (void *) &funcbuf.functionid,
4363  HASH_ENTER, &found);
4364 
4365  if (found)
4366  {
4367  ereport(pgStatRunningInCollector ? LOG : WARNING,
4368  (errmsg("corrupted statistics file \"%s\"",
4369  statfile)));
4370  goto done;
4371  }
4372 
4373  memcpy(funcentry, &funcbuf, sizeof(funcbuf));
4374  break;
4375 
4376  /*
4377  * 'E' The EOF marker of a complete stats file.
4378  */
4379  case 'E':
4380  goto done;
4381 
4382  default:
4383  ereport(pgStatRunningInCollector ? LOG : WARNING,
4384  (errmsg("corrupted statistics file \"%s\"",
4385  statfile)));
4386  goto done;
4387  }
4388  }
4389 
4390 done:
4391  FreeFile(fpin);
4392 
4393  if (permanent)
4394  {
4395  elog(DEBUG2, "removing permanent stats file \"%s\"", statfile);
4396  unlink(statfile);
4397  }
4398 }
static bool pgStatRunningInCollector
Definition: pgstat.c:178
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
#define LOG
Definition: elog.h:26
#define PG_BINARY_R
Definition: c.h:1273
signed int int32
Definition: c.h:429
#define MAXPGPATH
#define DEBUG2
Definition: elog.h:24
int errcode_for_file_access(void)
Definition: elog.c:721
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2373
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:157
int FreeFile(FILE *file)
Definition: fd.c:2572
#define PGSTAT_FILE_FORMAT_ID
Definition: pgstat.h:747
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
static void get_dbstat_filename(bool permanent, bool tempname, Oid databaseid, char *filename, int len)
Definition: pgstat.c:3803

◆ pgstat_read_db_statsfile_timestamp()

static bool pgstat_read_db_statsfile_timestamp ( Oid  databaseid,
bool  permanent,
TimestampTz ts 
)
static

Definition at line 4419 of file pgstat.c.

References AllocateFile(), PgStat_StatDBEntry::databaseid, ereport, errcode_for_file_access(), errmsg(), FreeFile(), LOG, offsetof, PG_BINARY_R, PGSTAT_FILE_FORMAT_ID, pgstat_stat_filename, PGSTAT_STAT_PERMANENT_FILENAME, pgStatRunningInCollector, SLRU_NUM_ELEMENTS, PgStat_StatDBEntry::stats_timestamp, PgStat_GlobalStats::stats_timestamp, and WARNING.

Referenced by backend_read_statsfile().

4421 {
4422  PgStat_StatDBEntry dbentry;
4423  PgStat_GlobalStats myGlobalStats;
4424  PgStat_ArchiverStats myArchiverStats;
4425  PgStat_WalStats myWalStats;
4426  PgStat_SLRUStats mySLRUStats[SLRU_NUM_ELEMENTS];
4427  PgStat_ReplSlotStats myReplSlotStats;
4428  PgStat_RecoveryPrefetchStats myRecoveryPrefetchStats;
4429  FILE *fpin;
4430  int32 format_id;
4431  const char *statfile = permanent ? PGSTAT_STAT_PERMANENT_FILENAME : pgstat_stat_filename;
4432 
4433  /*
4434  * Try to open the stats file. As above, anything but ENOENT is worthy of
4435  * complaining about.
4436  */
4437  if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
4438  {
4439  if (errno != ENOENT)
4442  errmsg("could not open statistics file \"%s\": %m",
4443  statfile)));
4444  return false;
4445  }
4446 
4447  /*
4448  * Verify it's of the expected format.
4449  */
4450  if (fread(&format_id, 1, sizeof(format_id), fpin) != sizeof(format_id) ||
4451  format_id != PGSTAT_FILE_FORMAT_ID)
4452  {
4453  ereport(pgStatRunningInCollector ? LOG : WARNING,
4454  (errmsg("corrupted statistics file \"%s\"", statfile)));
4455  FreeFile(fpin);
4456  return false;
4457  }
4458 
4459  /*
4460  * Read global stats struct
4461  */
4462  if (fread(&myGlobalStats, 1, sizeof(myGlobalStats),
4463  fpin) != sizeof(myGlobalStats))
4464  {
4465  ereport(pgStatRunningInCollector ? LOG : WARNING,
4466  (errmsg("corrupted statistics file \"%s\"", statfile)));
4467  FreeFile(fpin);
4468  return false;
4469  }
4470 
4471  /*
4472  * Read archiver stats struct
4473  */
4474  if (fread(&myArchiverStats, 1, sizeof(myArchiverStats),
4475  fpin) != sizeof(myArchiverStats))
4476  {
4477  ereport(pgStatRunningInCollector ? LOG : WARNING,
4478  (errmsg("corrupted statistics file \"%s\"", statfile)));
4479  FreeFile(fpin);
4480  return false;
4481  }
4482 
4483  /*
4484  * Read WAL stats struct
4485  */
4486  if (fread(&myWalStats, 1, sizeof(myWalStats), fpin) != sizeof(myWalStats))
4487  {
4488  ereport(pgStatRunningInCollector ? LOG : WARNING,
4489  (errmsg("corrupted statistics file \"%s\"", statfile)));
4490  FreeFile(fpin);
4491  return false;
4492  }
4493 
4494  /*
4495  * Read SLRU stats struct
4496  */
4497  if (fread(mySLRUStats, 1, sizeof(mySLRUStats), fpin) != sizeof(mySLRUStats))
4498  {
4499  ereport(pgStatRunningInCollector ? LOG : WARNING,
4500  (errmsg("corrupted statistics file \"%s\"", statfile)));
4501  FreeFile(fpin);
4502  return false;
4503  }
4504 
4505  /*
4506  * Read recovery prefetch stats struct
4507  */
4508  if (fread(&myRecoveryPrefetchStats, 1, sizeof(myRecoveryPrefetchStats),
4509  fpin) != sizeof(myRecoveryPrefetchStats))
4510  {
4511  ereport(pgStatRunningInCollector ? LOG : WARNING,
4512  (errmsg("corrupted statistics file \"%s\"", statfile)));
4513  FreeFile(fpin);
4514  return false;
4515  }
4516 
4517  /* By default, we're going to return the timestamp of the global file. */
4518  *ts = myGlobalStats.stats_timestamp;
4519 
4520  /*
4521  * We found an existing collector stats file. Read it and look for a
4522  * record for the requested database. If found, use its timestamp.
4523  */
4524  for (;;)
4525  {
4526  switch (fgetc(fpin))
4527  {
4528  /*
4529  * 'D' A PgStat_StatDBEntry struct describing a database
4530  * follows.
4531  */
4532  case 'D':
4533  if (fread(&dbentry, 1, offsetof(PgStat_StatDBEntry, tables),
4534  fpin) != offsetof(PgStat_StatDBEntry, tables))
4535  {
4536  ereport(pgStatRunningInCollector ? LOG : WARNING,
4537  (errmsg("corrupted statistics file \"%s\"",
4538  statfile)));
4539  FreeFile(fpin);
4540  return false;
4541  }
4542 
4543  /*
4544  * If this is the DB we're looking for, save its timestamp and
4545  * we're done.
4546  */
4547  if (dbentry.databaseid == databaseid)
4548  {
4549  *ts = dbentry.stats_timestamp;
4550  goto done;
4551  }
4552 
4553  break;
4554 
4555  /*
4556  * 'R' A PgStat_ReplSlotStats struct describing a replication
4557  * slot follows.
4558  */
4559  case 'R':
4560  if (fread(&myReplSlotStats, 1, sizeof(PgStat_ReplSlotStats), fpin)
4561  != sizeof(PgStat_ReplSlotStats))
4562  {
4563  ereport(pgStatRunningInCollector ? LOG : WARNING,
4564  (errmsg("corrupted statistics file \"%s\"",
4565  statfile)));
4566  FreeFile(fpin);
4567  return false;
4568  }
4569  break;
4570 
4571  case 'E':
4572  goto done;
4573 
4574  default:
4575  {
4576  ereport(pgStatRunningInCollector ? LOG : WARNING,
4577  (errmsg("corrupted statistics file \"%s\"",
4578  statfile)));
4579  FreeFile(fpin);
4580  return false;
4581  }
4582  }
4583  }
4584 
4585 done:
4586  FreeFile(fpin);
4587  return true;
4588 }
TimestampTz stats_timestamp
Definition: pgstat.h:787
#define SLRU_NUM_ELEMENTS
Definition: pgstat.c:158
static bool pgStatRunningInCollector
Definition: pgstat.c:178
TimestampTz stats_timestamp
Definition: pgstat.h:872
#define PGSTAT_STAT_PERMANENT_FILENAME
Definition: pgstat.h:29
#define LOG
Definition: elog.h:26
#define PG_BINARY_R
Definition: c.h:1273
signed int int32
Definition: c.h:429
char * pgstat_stat_filename
Definition: pgstat.c:123
int errcode_for_file_access(void)
Definition: elog.c:721
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2373
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:157
int FreeFile(FILE *file)
Definition: fd.c:2572
#define PGSTAT_FILE_FORMAT_ID
Definition: pgstat.h:747
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define offsetof(type, field)
Definition: c.h:727

◆ pgstat_read_statsfiles()

static HTAB * pgstat_read_statsfiles ( Oid  onlydb,
bool  permanent,
bool  deep 
)
static

Definition at line 3952 of file pgstat.c.

References AllocateFile(), PgStat_StatDBEntry::databaseid, DEBUG2, elog, HASHCTL::entrysize, ereport, errcode_for_file_access(), errmsg(), FreeFile(), PgStat_StatDBEntry::functions, GetCurrentTimestamp(), HASH_BLOBS, HASH_CONTEXT, hash_create(), HASH_ELEM, HASH_ENTER, hash_search(), HASHCTL::hcxt, i, InvalidOid, HASHCTL::keysize, LOG, max_replication_slots, MemoryContextAllocZero(), nReplSlotStats, offsetof, PG_BINARY_R, PGSTAT_DB_HASH_SIZE, PGSTAT_FILE_FORMAT_ID, PGSTAT_FUNCTION_HASH_SIZE, pgstat_read_db_statsfile(), pgstat_setup_memcxt(), pgstat_stat_filename, PGSTAT_STAT_PERMANENT_FILENAME, PGSTAT_TAB_HASH_SIZE, pgStatLocalContext, pgStatRunningInCollector, SLRU_NUM_ELEMENTS, PgStat_ArchiverStats::stat_reset_timestamp, PgStat_GlobalStats::stat_reset_timestamp, PgStat_WalStats::stat_reset_timestamp, PgStat_StatDBEntry::stats_timestamp, PgStat_GlobalStats::stats_timestamp, PgStat_StatDBEntry::tables, and WARNING.

Referenced by backend_read_statsfile(), and PgstatCollectorMain().

3953 {
3954  PgStat_StatDBEntry *dbentry;
3955  PgStat_StatDBEntry dbbuf;
3956  HASHCTL hash_ctl;
3957  HTAB *dbhash;
3958  FILE *fpin;
3959  int32 format_id;
3960  bool found;
3961  const char *statfile = permanent ? PGSTAT_STAT_PERMANENT_FILENAME : pgstat_stat_filename;
3962  int i;
3963 
3964  /*
3965  * The tables will live in pgStatLocalContext.
3966  */
3968 
3969  /*
3970  * Create the DB hashtable
3971  */
3972  hash_ctl.keysize = sizeof(Oid);
3973  hash_ctl.entrysize = sizeof(PgStat_StatDBEntry);
3974  hash_ctl.hcxt = pgStatLocalContext;
3975  dbhash = hash_create("Databases hash", PGSTAT_DB_HASH_SIZE, &hash_ctl,
3977 
3978  /* Allocate the space for replication slot statistics */
3981  * sizeof(PgStat_ReplSlotStats));
3982  nReplSlotStats = 0;
3983 
3984  /*
3985  * Clear out global, archiver, WAL and SLRU statistics so they start from
3986  * zero in case we can't load an existing statsfile.
3987  */
3988  memset(&globalStats, 0, sizeof(globalStats));
3989  memset(&archiverStats, 0, sizeof(archiverStats));
3990  memset(&walStats, 0, sizeof(walStats));
3991  memset(&slruStats, 0, sizeof(slruStats));
3992  memset(&recoveryPrefetchStats, 0, sizeof(recoveryPrefetchStats));
3993 
3994  /*
3995  * Set the current timestamp (will be kept only in case we can't load an
3996  * existing statsfile).
3997  */
4001 
4002  /*
4003  * Set the same reset timestamp for all SLRU items too.
4004  */
4005  for (i = 0; i < SLRU_NUM_ELEMENTS; i++)
4006  slruStats[i].stat_reset_timestamp = globalStats.stat_reset_timestamp;
4007 
4008  /*
4009  * Set the same reset timestamp for all replication slots too.
4010  */
4011  for (i = 0; i < max_replication_slots; i++)
4012  replSlotStats[i].stat_reset_timestamp = globalStats.stat_reset_timestamp;
4013 
4014  /*
4015  * Try to open the stats file. If it doesn't exist, the backends simply
4016  * return zero for anything and the collector simply starts from scratch
4017  * with empty counters.
4018  *
4019  * ENOENT is a possibility if the stats collector is not running or has
4020  * not yet written the stats file the first time. Any other failure
4021  * condition is suspicious.
4022  */
4023  if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
4024  {
4025  if (errno != ENOENT)
4028  errmsg("could not open statistics file \"%s\": %m",
4029  statfile)));
4030  return dbhash;
4031  }
4032 
4033  /*
4034  * Verify it's of the expected format.
4035  */
4036  if (fread(&format_id, 1, sizeof(format_id), fpin) != sizeof(format_id) ||
4037  format_id != PGSTAT_FILE_FORMAT_ID)
4038  {
4039  ereport(pgStatRunningInCollector ? LOG : WARNING,
4040  (errmsg("corrupted statistics file \"%s\"", statfile)));
4041  goto done;
4042  }
4043 
4044  /*
4045  * Read global stats struct
4046  */
4047  if (fread(&globalStats, 1, sizeof(globalStats), fpin) != sizeof(globalStats))
4048  {
4049  ereport(pgStatRunningInCollector ? LOG : WARNING,
4050  (errmsg("corrupted statistics file \"%s\"", statfile)));
4051  memset(&globalStats, 0, sizeof(globalStats));
4052  goto done;
4053  }
4054 
4055  /*
4056  * In the collector, disregard the timestamp we read from the permanent
4057  * stats file; we should be willing to write a temp stats file immediately
4058  * upon the first request from any backend. This only matters if the old
4059  * file's timestamp is less than PGSTAT_STAT_INTERVAL ago, but that's not
4060  * an unusual scenario.
4061  */
4064 
4065  /*
4066  * Read archiver stats struct
4067  */
4068  if (fread(&archiverStats, 1, sizeof(archiverStats), fpin) != sizeof(archiverStats))
4069  {
4070  ereport(pgStatRunningInCollector ? LOG : WARNING,
4071  (errmsg("corrupted statistics file \"%s\"", statfile)));
4072  memset(&archiverStats, 0, sizeof(archiverStats));
4073  goto done;
4074  }
4075 
4076  /*
4077  * Read WAL stats struct
4078  */
4079  if (fread(&walStats, 1, sizeof(walStats), fpin) != sizeof(walStats))
4080  {
4081  ereport(pgStatRunningInCollector ? LOG : WARNING,
4082  (errmsg("corrupted statistics file \"%s\"", statfile)));
4083  memset(&walStats, 0, sizeof(walStats));
4084  goto done;
4085  }
4086 
4087  /*
4088  * Read SLRU stats struct
4089  */
4090  if (fread(slruStats, 1, sizeof(slruStats), fpin) != sizeof(slruStats))
4091  {
4092  ereport(pgStatRunningInCollector ? LOG : WARNING,
4093  (errmsg("corrupted statistics file \"%s\"", statfile)));
4094  memset(&slruStats, 0, sizeof(slruStats));
4095  goto done;
4096  }
4097 
4098  /*
4099  * Read recoveryPrefetchStats struct
4100  */
4101  if (fread(&recoveryPrefetchStats, 1, sizeof(recoveryPrefetchStats),
4102  fpin) != sizeof(recoveryPrefetchStats))
4103  {
4104  ereport(pgStatRunningInCollector ? LOG : WARNING,
4105  (errmsg("corrupted statistics file \"%s\"", statfile)));
4106  memset(&recoveryPrefetchStats, 0, sizeof(recoveryPrefetchStats));
4107  goto done;
4108  }
4109 
4110  /*
4111  * We found an existing collector stats file. Read it and put all the
4112  * hashtable entries into place.
4113  */
4114  for (;;)
4115  {
4116  switch (fgetc(fpin))
4117  {
4118  /*
4119  * 'D' A PgStat_StatDBEntry struct describing a database
4120  * follows.
4121  */
4122  case 'D':
4123  if (fread(&dbbuf, 1, offsetof(PgStat_StatDBEntry, tables),
4124  fpin) != offsetof(PgStat_StatDBEntry, tables))
4125  {
4126  ereport(pgStatRunningInCollector ? LOG : WARNING,
4127  (errmsg("corrupted statistics file \"%s\"",
4128  statfile)));
4129  goto done;
4130  }
4131 
4132  /*
4133  * Add to the DB hash
4134  */
4135  dbentry = (PgStat_StatDBEntry *) hash_search(dbhash,
4136  (void *) &dbbuf.databaseid,
4137  HASH_ENTER,
4138  &found);
4139  if (found)
4140  {
4141  ereport(pgStatRunningInCollector ? LOG : WARNING,
4142  (errmsg("corrupted statistics file \"%s\"",
4143  statfile)));
4144  goto done;
4145  }
4146 
4147  memcpy(dbentry, &dbbuf, sizeof(PgStat_StatDBEntry));
4148  dbentry->tables = NULL;
4149  dbentry->functions = NULL;
4150 
4151  /*
4152  * In the collector, disregard the timestamp we read from the
4153  * permanent stats file; we should be willing to write a temp
4154  * stats file immediately upon the first request from any
4155  * backend.
4156  */
4158  dbentry->stats_timestamp = 0;
4159 
4160  /*
4161  * Don't create tables/functions hashtables for uninteresting
4162  * databases.
4163  */
4164  if (onlydb != InvalidOid)
4165  {
4166  if (dbbuf.databaseid != onlydb &&
4167  dbbuf.databaseid != InvalidOid)
4168  break;
4169  }
4170 
4171  hash_ctl.keysize = sizeof(Oid);
4172  hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
4173  hash_ctl.hcxt = pgStatLocalContext;
4174  dbentry->tables = hash_create("Per-database table",
4176  &hash_ctl,
4178 
4179  hash_ctl.keysize = sizeof(Oid);
4180  hash_ctl.entrysize = sizeof(PgStat_StatFuncEntry);
4181  hash_ctl.hcxt = pgStatLocalContext;
4182  dbentry->functions = hash_create("Per-database function",
4184  &hash_ctl,
4186 
4187  /*
4188  * If requested, read the data from the database-specific
4189  * file. Otherwise we just leave the hashtables empty.
4190  */
4191  if (deep)
4193  dbentry->tables,
4194  dbentry->functions,
4195  permanent);
4196 
4197  break;
4198 
4199  /*
4200  * 'R' A PgStat_ReplSlotStats struct describing a replication
4201  * slot follows.
4202  */
4203  case 'R':
4204  if (fread(&replSlotStats[nReplSlotStats], 1, sizeof(PgStat_ReplSlotStats), fpin)
4205  != sizeof(PgStat_ReplSlotStats))
4206  {
4207  ereport(pgStatRunningInCollector ? LOG : WARNING,
4208  (errmsg("corrupted statistics file \"%s\"",
4209  statfile)));
4210  memset(&replSlotStats[nReplSlotStats], 0, sizeof(PgStat_ReplSlotStats));
4211  goto done;
4212  }
4213  nReplSlotStats++;
4214  break;
4215 
4216  case 'E':
4217  goto done;
4218 
4219  default:
4220  ereport(pgStatRunningInCollector ? LOG : WARNING,
4221  (errmsg("corrupted statistics file \"%s\"",
4222  statfile)));
4223  goto done;
4224  }
4225  }
4226 
4227 done:
4228  FreeFile(fpin);
4229 
4230  /* If requested to read the permanent file, also get rid of it. */
4231  if (permanent)
4232  {
4233  elog(DEBUG2, "removing permanent stats file \"%s\"", statfile);
4234  unlink(statfile);
4235  }
4236 
4237  return dbhash;
4238 }
#define PGSTAT_DB_HASH_SIZE
Definition: pgstat.c:106
TimestampTz stat_reset_timestamp
Definition: pgstat.h:899
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
TimestampTz stats_timestamp
Definition: pgstat.h:787
MemoryContext hcxt
Definition: hsearch.h:86
static PgStat_GlobalStats globalStats
Definition: pgstat.c:278
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
#define SLRU_NUM_ELEMENTS
Definition: pgstat.c:158
static PgStat_WalStats walStats
Definition: pgstat.c:279
#define PGSTAT_TAB_HASH_SIZE
Definition: pgstat.c:107
static bool pgStatRunningInCollector
Definition: pgstat.c:178
HTAB * functions
Definition: pgstat.h:794
Size entrysize
Definition: hsearch.h:76
TimestampTz stats_timestamp
Definition: pgstat.h:872
#define PGSTAT_STAT_PERMANENT_FILENAME
Definition: pgstat.h:29
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
#define LOG
Definition: elog.h:26
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_BINARY_R
Definition: c.h:1273
struct PgStat_StatFuncEntry PgStat_StatFuncEntry
signed int int32
Definition: c.h:429
Definition: dynahash.c:219
char * pgstat_stat_filename
Definition: pgstat.c:123
#define DEBUG2
Definition: elog.h:24
#define PGSTAT_FUNCTION_HASH_SIZE
Definition: pgstat.c:108
int errcode_for_file_access(void)
Definition: elog.c:721
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2373
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
static void pgstat_setup_memcxt(void)
Definition: pgstat.c:4738
static int nReplSlotStats
Definition: pgstat.c:282
static void pgstat_read_db_statsfile(Oid databaseid, HTAB *tabhash, HTAB *funchash, bool permanent)
Definition: pgstat.c:4256
#define WARNING
Definition: elog.h:40
#define HASH_BLOBS
Definition: hsearch.h:97
Size keysize
Definition: hsearch.h:75
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:906
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
int max_replication_slots
Definition: slot.c:99
static PgStat_RecoveryPrefetchStats recoveryPrefetchStats
Definition: pgstat.c:283
struct PgStat_StatTabEntry PgStat_StatTabEntry
static PgStat_ArchiverStats archiverStats
Definition: pgstat.c:277
TimestampTz stat_reset_timestamp
Definition: pgstat.h:883
struct PgStat_StatDBEntry PgStat_StatDBEntry
int FreeFile(FILE *file)
Definition: fd.c:2572
#define PGSTAT_FILE_FORMAT_ID
Definition: pgstat.h:747
TimestampTz stat_reset_timestamp
Definition: pgstat.h:864
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
static PgStat_SLRUStats slruStats[SLRU_NUM_ELEMENTS]
Definition: pgstat.c:280
int i
static MemoryContext pgStatLocalContext
Definition: pgstat.c:269
static PgStat_ReplSlotStats * replSlotStats
Definition: pgstat.c:281
#define offsetof(type, field)
Definition: c.h:727

◆ pgstat_recv_analyze()

static void pgstat_recv_analyze ( PgStat_MsgAnalyze msg,
int  len 
)
static

Definition at line 5294 of file pgstat.c.

References PgStat_StatTabEntry::analyze_count, PgStat_StatTabEntry::analyze_timestamp, PgStat_StatTabEntry::autovac_analyze_count, PgStat_StatTabEntry::autovac_analyze_timestamp, PgStat_StatTabEntry::changes_since_analyze, PgStat_StatTabEntry::changes_since_analyze_reported, PgStat_MsgAnalyze::m_analyzetime, PgStat_MsgAnalyze::m_autovacuum, PgStat_MsgAnalyze::m_databaseid, PgStat_MsgAnalyze::m_dead_tuples, PgStat_MsgAnalyze::m_live_tuples, PgStat_MsgAnalyze::m_resetcounter, PgStat_MsgAnalyze::m_tableoid, PgStat_StatTabEntry::n_dead_tuples, PgStat_StatTabEntry::n_live_tuples, pgstat_get_db_entry(), and pgstat_get_tab_entry().

Referenced by PgstatCollectorMain().

5295 {
5296  PgStat_StatDBEntry *dbentry;
5297  PgStat_StatTabEntry *tabentry;
5298 
5299  /*
5300  * Store the data in the table's hashtable entry.
5301  */
5302  dbentry = pgstat_get_db_entry(msg->m_databaseid, true);
5303 
5304  tabentry = pgstat_get_tab_entry(dbentry, msg->m_tableoid, true);
5305 
5306  tabentry->n_live_tuples = msg->m_live_tuples;
5307  tabentry->n_dead_tuples = msg->m_dead_tuples;
5308 
5309  /*
5310  * If commanded, reset changes_since_analyze to zero. This forgets any
5311  * changes that were committed while the ANALYZE was in progress, but we
5312  * have no good way to estimate how many of those there were.
5313  */
5314  if (msg->m_resetcounter)
5315  {
5316  tabentry->changes_since_analyze = 0;
5317  tabentry->changes_since_analyze_reported = 0;
5318  }
5319 
5320  if (msg->m_autovacuum)
5321  {
5322  tabentry->autovac_analyze_timestamp = msg->m_analyzetime;
5323  tabentry->autovac_analyze_count++;
5324  }
5325  else
5326  {
5327  tabentry->analyze_timestamp = msg->m_analyzetime;
5328  tabentry->analyze_count++;
5329  }
5330 }
PgStat_Counter analyze_count
Definition: pgstat.h:830
static PgStat_StatTabEntry * pgstat_get_tab_entry(PgStat_StatDBEntry *dbentry, Oid tableoid, bool create)
Definition: pgstat.c:3588
TimestampTz autovac_analyze_timestamp
Definition: pgstat.h:831
TimestampTz m_analyzetime
Definition: pgstat.h:442
bool m_resetcounter
Definition: pgstat.h:441
PgStat_Counter n_live_tuples
Definition: pgstat.h:816
PgStat_Counter n_dead_tuples
Definition: pgstat.h:817
PgStat_Counter m_live_tuples
Definition: pgstat.h:443
bool m_autovacuum
Definition: pgstat.h:440
TimestampTz analyze_timestamp
Definition: pgstat.h:829
PgStat_Counter changes_since_analyze
Definition: pgstat.h:818
static PgStat_StatDBEntry * pgstat_get_db_entry(Oid databaseid, bool create)
Definition: pgstat.c:3557
PgStat_Counter autovac_analyze_count
Definition: pgstat.h:832
PgStat_Counter changes_since_analyze_reported
Definition: pgstat.h:819
PgStat_Counter m_dead_tuples
Definition: pgstat.h:444

◆ pgstat_recv_anl_ancestors()

static void pgstat_recv_anl_ancestors ( PgStat_MsgAnlAncestors msg,
int  len 
)
static

Definition at line 5333 of file pgstat.c.

References PgStat_StatTabEntry::changes_since_analyze, PgStat_StatTabEntry::changes_since_analyze_reported, i, PgStat_MsgAnlAncestors::m_ancestors, PgStat_MsgAnlAncestors::m_databaseid, PgStat_MsgAnlAncestors::m_nancestors, PgStat_MsgAnlAncestors::m_tableoid, pgstat_get_db_entry(), and pgstat_get_tab_entry().

Referenced by PgstatCollectorMain().

5334 {
5335  PgStat_StatDBEntry *dbentry;
5336  PgStat_StatTabEntry *tabentry;
5337 
5338  dbentry = pgstat_get_db_entry(msg->m_databaseid, true);
5339 
5340  tabentry = pgstat_get_tab_entry(dbentry, msg->m_tableoid, true);
5341 
5342  for (int i = 0; i < msg->m_nancestors; i++)
5343  {
5344  Oid ancestor_relid = msg->m_ancestors[i];
5345  PgStat_StatTabEntry *ancestor;
5346 
5347  ancestor = pgstat_get_tab_entry(dbentry, ancestor_relid, true);
5348  ancestor->changes_since_analyze +=
5350  }
5351 
5353 
5354 }
static PgStat_StatTabEntry * pgstat_get_tab_entry(PgStat_StatDBEntry *dbentry, Oid tableoid, bool create)
Definition: pgstat.c:3588
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter changes_since_analyze
Definition: pgstat.h:818
static PgStat_StatDBEntry * pgstat_get_db_entry(Oid databaseid, bool create)
Definition: pgstat.c:3557
Oid m_ancestors[PGSTAT_NUM_ANCESTORENTRIES]
Definition: pgstat.h:464
PgStat_Counter changes_since_analyze_reported
Definition: pgstat.h:819
int i

◆ pgstat_recv_archiver()

static void pgstat_recv_archiver ( PgStat_MsgArchiver msg,
int  len 
)
static

Definition at line 5363 of file pgstat.c.

References PgStat_ArchiverStats::archived_count, PgStat_ArchiverStats::failed_count, PgStat_ArchiverStats::last_archived_timestamp, PgStat_ArchiverStats::last_archived_wal, PgStat_ArchiverStats::last_failed_timestamp, PgStat_ArchiverStats::last_failed_wal, PgStat_MsgArchiver::m_failed, PgStat_MsgArchiver::m_timestamp, and PgStat_MsgArchiver::m_xlog.

Referenced by PgstatCollectorMain().

5364 {
5365  if (msg->m_failed)
5366  {
5367  /* Failed archival attempt */
5369  memcpy(archiverStats.last_failed_wal, msg->m_xlog,
5370  sizeof(archiverStats.last_failed_wal));
5372  }
5373  else
5374  {
5375  /* Successful archival operation */
5377  memcpy(archiverStats.last_archived_wal, msg->m_xlog,
5380  }
5381 }
char last_failed_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:861
char m_xlog[MAX_XFN_CHARS+1]
Definition: pgstat.h:475
PgStat_Counter archived_count
Definition: pgstat.h:856
TimestampTz m_timestamp
Definition: pgstat.h:476
TimestampTz last_failed_timestamp
Definition: pgstat.h:863
PgStat_Counter failed_count
Definition: pgstat.h:860
static PgStat_ArchiverStats archiverStats
Definition: pgstat.c:277
TimestampTz last_archived_timestamp
Definition: pgstat.h:859
char last_archived_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:857

◆ pgstat_recv_autovac()

static void pgstat_recv_autovac ( PgStat_MsgAutovacStart msg,
int  len 
)
static

Definition at line 5229 of file pgstat.c.

References PgStat_StatDBEntry::last_autovac_time, PgStat_MsgAutovacStart::m_databaseid, PgStat_MsgAutovacStart::m_start_time, and pgstat_get_db_entry().

Referenced by PgstatCollectorMain().

5230 {
5231  PgStat_StatDBEntry *dbentry;
5232 
5233  /*
5234  * Store the last autovacuum time in the database's hashtable entry.
5235  */
5236  dbentry = pgstat_get_db_entry(msg->m_databaseid, true);
5237 
5238  dbentry->last_autovac_time = msg->m_start_time;
5239 }
TimestampTz last_autovac_time
Definition: pgstat.h:765
static PgStat_StatDBEntry * pgstat_get_db_entry(Oid databaseid, bool create)
Definition: pgstat.c:3557
TimestampTz m_start_time
Definition: pgstat.h:409

◆ pgstat_recv_bgwriter()

static void pgstat_recv_bgwriter ( PgStat_MsgBgWriter msg,
int  len 
)
static

Definition at line 5390 of file pgstat.c.

References PgStat_GlobalStats::buf_alloc, PgStat_GlobalStats::buf_fsync_backend, PgStat_GlobalStats::buf_written_backend, PgStat_GlobalStats::buf_written_checkpoints, PgStat_GlobalStats::buf_written_clean, PgStat_GlobalStats::checkpoint_sync_time, PgStat_GlobalStats::checkpoint_write_time, PgStat_MsgBgWriter::m_buf_alloc, PgStat_MsgBgWriter::m_buf_fsync_backend, PgStat_MsgBgWriter::m_buf_written_backend, PgStat_MsgBgWriter::m_buf_written_checkpoints, PgStat_MsgBgWriter::m_buf_written_clean, PgStat_MsgBgWriter::m_checkpoint_sync_time, PgStat_MsgBgWriter::m_checkpoint_write_time, PgStat_MsgBgWriter::m_maxwritten_clean, PgStat_MsgBgWriter::m_requested_checkpoints, PgStat_MsgBgWriter::m_timed_checkpoints, PgStat_GlobalStats::maxwritten_clean, PgStat_GlobalStats::requested_checkpoints, and PgStat_GlobalStats::timed_checkpoints.

Referenced by PgstatCollectorMain().

5391 {
5402 }
PgStat_Counter m_buf_fsync_backend
Definition: pgstat.h:493
PgStat_Counter buf_alloc
Definition: pgstat.h:882
PgStat_Counter m_buf_written_checkpoints
Definition: pgstat.h:489
PgStat_Counter m_buf_alloc
Definition: pgstat.h:494
PgStat_Counter m_checkpoint_write_time
Definition: pgstat.h:495
static PgStat_GlobalStats globalStats
Definition: pgstat.c:278
PgStat_Counter m_timed_checkpoints
Definition: pgstat.h:487
PgStat_Counter m_maxwritten_clean
Definition: pgstat.h:491
PgStat_Counter timed_checkpoints
Definition: pgstat.h:873
PgStat_Counter m_buf_written_clean
Definition: pgstat.h:490
PgStat_Counter buf_fsync_backend
Definition: pgstat.h:881
PgStat_Counter m_requested_checkpoints
Definition: pgstat.h:488
PgStat_Counter buf_written_backend
Definition: pgstat.h:880
PgStat_Counter checkpoint_write_time
Definition: pgstat.h:875
PgStat_Counter buf_written_checkpoints
Definition: pgstat.h:877
PgStat_Counter buf_written_clean
Definition: pgstat.h:878
PgStat_Counter m_checkpoint_sync_time
Definition: pgstat.h:496
PgStat_Counter m_buf_written_backend
Definition: pgstat.h:492
PgStat_Counter checkpoint_sync_time
Definition: pgstat.h:876
PgStat_Counter maxwritten_clean
Definition: pgstat.h:879
PgStat_Counter requested_checkpoints
Definition: pgstat.h:874

◆ pgstat_recv_checksum_failure()

static void pgstat_recv_checksum_failure ( PgStat_MsgChecksumFailure msg,
int  len 
)
static

Definition at line 5516 of file pgstat.c.

References PgStat_StatDBEntry::last_checksum_failure, PgStat_MsgChecksumFailure::m_databaseid, PgStat_MsgChecksumFailure::m_failure_time, PgStat_MsgChecksumFailure::m_failurecount, PgStat_StatDBEntry::n_checksum_failures, and pgstat_get_db_entry().

Referenced by PgstatCollectorMain().

5517 {
5518  PgStat_StatDBEntry *dbentry;
5519 
5520  dbentry = pgstat_get_db_entry(msg->m_databaseid, true);
5521 
5522  dbentry->n_checksum_failures += msg->m_failurecount;
5523  dbentry->last_checksum_failure = msg->m_failure_time;
5524 }
PgStat_Counter n_checksum_failures
Definition: pgstat.h:774
TimestampTz last_checksum_failure
Definition: pgstat.h:775
TimestampTz m_failure_time
Definition: pgstat.h:683
static PgStat_StatDBEntry * pgstat_get_db_entry(Oid databaseid, bool create)
Definition: pgstat.c:3557

◆ pgstat_recv_connstat()

static void pgstat_recv_connstat ( PgStat_MsgConn msg,
int  len 
)
static

Definition at line 5585 of file pgstat.c.

References DISCONNECT_CLIENT_EOF, DISCONNECT_FATAL, DISCONNECT_KILLED, DISCONNECT_NORMAL, DISCONNECT_NOT_YET, PgStat_MsgConn::m_active_time, PgStat_MsgConn::m_count, PgStat_MsgConn::m_databaseid, PgStat_MsgConn::m_disconnect, PgStat_MsgConn::m_idle_in_xact_time, PgStat_MsgConn::m_session_time, PgStat_StatDBEntry::n_sessions, PgStat_StatDBEntry::n_sessions_abandoned, PgStat_StatDBEntry::n_sessions_fatal, PgStat_StatDBEntry::n_sessions_killed, pgstat_get_db_entry(), PgStat_StatDBEntry::total_active_time, PgStat_StatDBEntry::total_idle_in_xact_time, and PgStat_StatDBEntry::total_session_time.

Referenced by PgstatCollectorMain().

5586 {
5587  PgStat_StatDBEntry *dbentry;
5588 
5589  dbentry = pgstat_get_db_entry(msg->m_databaseid, true);
5590 
5591  dbentry->n_sessions += msg->m_count;
5592  dbentry->total_session_time += msg->m_session_time;
5593  dbentry->total_active_time += msg->m_active_time;
5595  switch (msg->m_disconnect)
5596  {
5597  case DISCONNECT_NOT_YET:
5598  case DISCONNECT_NORMAL:
5599  /* we don't collect these */
5600  break;
5601  case DISCONNECT_CLIENT_EOF:
5602  dbentry->n_sessions_abandoned++;
5603  break;
5604  case DISCONNECT_FATAL:
5605  dbentry->n_sessions_fatal++;
5606  break;
5607  case DISCONNECT_KILLED:
5608  dbentry->n_sessions_killed++;
5609  break;
5610  }
5611 }
PgStat_Counter m_active_time
Definition: pgstat.h:696
PgStat_Counter n_sessions_killed
Definition: pgstat.h:784
Oid m_databaseid
Definition: pgstat.h:693
PgStat_Counter total_active_time
Definition: pgstat.h:780
PgStat_Counter n_sessions_abandoned
Definition: pgstat.h:782
PgStat_Counter n_sessions_fatal
Definition: pgstat.h:783
PgStat_Counter total_idle_in_xact_time
Definition: pgstat.h:781
PgStat_Counter total_session_time
Definition: pgstat.h:779
PgStat_Counter m_idle_in_xact_time
Definition: pgstat.h:697
PgStat_Counter n_sessions
Definition: pgstat.h:778
static PgStat_StatDBEntry * pgstat_get_db_entry(Oid databaseid, bool create)
Definition: pgstat.c:3557
PgStat_Counter m_session_time
Definition: pgstat.h:695
PgStat_Counter m_count
Definition: pgstat.h:694
SessionEndType m_disconnect
Definition: pgstat.h:698

◆ pgstat_recv_deadlock()

static void pgstat_recv_deadlock ( PgStat_MsgDeadlock msg,
int  len 
)
static

Definition at line 5500 of file pgstat.c.

References PgStat_MsgDeadlock::m_databaseid, PgStat_StatDBEntry::n_deadlocks, and pgstat_get_db_entry().

Referenced by PgstatCollectorMain().

5501 {
5502  PgStat_StatDBEntry *dbentry;
5503 
5504  dbentry = pgstat_get_db_entry(msg->m_databaseid, true);
5505 
5506  dbentry->n_deadlocks++;
5507 }
static PgStat_StatDBEntry * pgstat_get_db_entry(Oid databaseid, bool create)
Definition: pgstat.c:3557
PgStat_Counter n_deadlocks
Definition: pgstat.h:773

◆ pgstat_recv_dropdb()

static void pgstat_recv_dropdb ( PgStat_MsgDropdb msg,
int  len 
)
static

Definition at line 5023 of file pgstat.c.

References DEBUG2, elog, ereport, errmsg(), ERROR, PgStat_StatDBEntry::functions, get_dbstat_filename(), hash_destroy(), HASH_REMOVE, hash_search(), PgStat_MsgDropdb::m_databaseid, MAXPGPATH, pgstat_get_db_entry(), and PgStat_StatDBEntry::tables.

Referenced by PgstatCollectorMain().

5024 {
5025  Oid dbid = msg->m_databaseid;
5026  PgStat_StatDBEntry *dbentry;
5027 
5028  /*
5029  * Lookup the database in the hashtable.
5030  */
5031  dbentry = pgstat_get_db_entry(dbid, false);
5032 
5033  /*
5034  * If found, remove it (along with the db statfile).
5035  */
5036  if (dbentry)
5037  {
5038  char statfile[MAXPGPATH];
5039 
5040  get_dbstat_filename(false, false, dbid, statfile, MAXPGPATH);
5041 
5042  elog(DEBUG2, "removing stats file \"%s\"", statfile);
5043  unlink(statfile);
5044 
5045  if (dbentry->tables != NULL)
5046  hash_destroy(dbentry->tables);
5047  if (dbentry->functions != NULL)
5048  hash_destroy(dbentry->functions);
5049 
5051  (void *) &dbid,
5052  HASH_REMOVE, NULL) == NULL)
5053  ereport(ERROR,
5054  (errmsg("database hash table corrupted during cleanup --- abort")));
5055  }
5056 }
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:862
HTAB * functions
Definition: pgstat.h:794
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:46
#define MAXPGPATH
#define DEBUG2
Definition: elog.h:24
Oid m_databaseid
Definition: pgstat.h:338
#define ereport(elevel,...)
Definition: elog.h:157
static PgStat_StatDBEntry * pgstat_get_db_entry(Oid databaseid, bool create)
Definition: pgstat.c:3557
static HTAB * pgStatDBHash
Definition: pgstat.c:270
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
static void get_dbstat_filename(bool permanent, bool tempname, Oid databaseid, char *filename, int len)
Definition: pgstat.c:3803

◆ pgstat_recv_funcpurge()

static void pgstat_recv_funcpurge ( PgStat_MsgFuncpurge msg,
int  len 
)
static

Definition at line 5685 of file pgstat.c.

References PgStat_StatDBEntry::functions, HASH_REMOVE, hash_search(), i, PgStat_MsgFuncpurge::m_databaseid, PgStat_MsgFuncpurge::m_functionid, PgStat_MsgFuncpurge::m_nentries, and pgstat_get_db_entry().

Referenced by PgstatCollectorMain().

5686 {
5687  PgStat_StatDBEntry *dbentry;
5688  int i;
5689 
5690  dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
5691 
5692  /*
5693  * No need to purge if we don't even know the database.
5694  */
5695  if (!dbentry || !dbentry->functions)
5696  return;
5697 
5698  /*
5699  * Process all function entries in the message.
5700  */
5701  for (i = 0; i < msg->m_nentries; i++)
5702  {
5703  /* Remove from hashtable if present; we don't care if it's not. */
5704  (void) hash_search(dbentry->functions,
5705  (void *) &(msg->m_functionid[i]),
5706  HASH_REMOVE, NULL);
5707  }
5708 }
HTAB * functions
Definition: pgstat.h:794
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
static PgStat_StatDBEntry * pgstat_get_db_entry(Oid databaseid, bool create)
Definition: pgstat.c:3557
int i
Oid m_functionid[PGSTAT_NUM_FUNCPURGE]
Definition: pgstat.h:659

◆ pgstat_recv_funcstat()

static void pgstat_recv_funcstat ( PgStat_MsgFuncstat msg,
int  len 
)
static

Definition at line 5637 of file pgstat.c.

References PgStat_FunctionEntry::f_id, PgStat_FunctionEntry::f_numcalls, PgStat_StatFuncEntry::f_numcalls, PgStat_FunctionEntry::f_self_time, PgStat_StatFuncEntry::f_self_time, PgStat_FunctionEntry::f_total_time, PgStat_StatFuncEntry::f_total_time, PgStat_StatDBEntry::functions, HASH_ENTER, hash_search(), i, PgStat_MsgFuncstat::m_databaseid, PgStat_MsgFuncstat::m_entry, PgStat_MsgFuncstat::m_nentries, and pgstat_get_db_entry().

Referenced by PgstatCollectorMain().

5638 {
5639  PgStat_FunctionEntry *funcmsg = &(msg->m_entry[0]);
5640  PgStat_StatDBEntry *dbentry;
5641  PgStat_StatFuncEntry *funcentry;
5642  int i;
5643  bool found;
5644 
5645  dbentry = pgstat_get_db_entry(msg->m_databaseid, true);
5646 
5647  /*
5648  * Process all function entries in the message.
5649  */
5650  for (i = 0; i < msg->m_nentries; i++, funcmsg++)
5651  {
5652  funcentry = (PgStat_StatFuncEntry *) hash_search(dbentry->functions,
5653  (void *) &(funcmsg->f_id),
5654  HASH_ENTER, &found);
5655 
5656  if (!found)
5657  {
5658  /*
5659  * If it's a new function entry, initialize counters to the values
5660  * we just got.
5661  */
5662  funcentry->f_numcalls = funcmsg->f_numcalls;
5663  funcentry->f_total_time = funcmsg->f_total_time;
5664  funcentry->f_self_time = funcmsg->f_self_time;
5665  }
5666  else
5667  {
5668  /*
5669  * Otherwise add the values to the existing entry.
5670  */
5671  funcentry->f_numcalls += funcmsg->f_numcalls;
5672  funcentry->f_total_time += funcmsg->f_total_time;
5673  funcentry->f_self_time += funcmsg->f_self_time;
5674  }
5675  }
5676 }
PgStat_Counter f_self_time
Definition: pgstat.h:625
HTAB * functions
Definition: pgstat.h:794
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
PgStat_Counter f_total_time
Definition: pgstat.h:846
PgStat_Counter f_numcalls
Definition: pgstat.h:844
PgStat_Counter f_total_time
Definition: pgstat.h:624
PgStat_FunctionEntry m_entry[PGSTAT_NUM_FUNCENTRIES]
Definition: pgstat.h:642
static PgStat_StatDBEntry * pgstat_get_db_entry(Oid databaseid, bool create)
Definition: pgstat.c:3557
int i
PgStat_Counter f_numcalls
Definition: pgstat.h:623
PgStat_Counter f_self_time
Definition: pgstat.h:847

◆ pgstat_recv_inquiry()

static void pgstat_recv_inquiry ( PgStat_MsgInquiry msg,
int  len 
)
static

Definition at line 4786 of file pgstat.c.

References PgStat_MsgInquiry::clock_time, PgStat_MsgInquiry::cutoff_time, PgStat_MsgInquiry::databaseid, PgStat_StatDBEntry::databaseid, DEBUG2, elog, ereport, errmsg(), GetCurrentTimestamp(), lappend_oid(), list_member_oid(), LOG, pfree(), pgstat_get_db_entry(), pstrdup(), PgStat_StatDBEntry::stats_timestamp, and timestamptz_to_str().

Referenced by PgstatCollectorMain().

4787 {
4788  PgStat_StatDBEntry *dbentry;
4789 
4790  elog(DEBUG2, "received inquiry for database %u", msg->databaseid);
4791 
4792  /*
4793  * If there's already a write request for this DB, there's nothing to do.
4794  *
4795  * Note that if a request is found, we return early and skip the below
4796  * check for clock skew. This is okay, since the only way for a DB
4797  * request to be present in the list is that we have been here since the
4798  * last write round. It seems sufficient to check for clock skew once per
4799  * write round.
4800  */
4802  return;
4803 
4804  /*
4805  * Check to see if we last wrote this database at a time >= the requested
4806  * cutoff time. If so, this is a stale request that was generated before
4807  * we updated the DB file, and we don't need to do so again.
4808  *
4809  * If the requestor's local clock time is older than stats_timestamp, we
4810  * should suspect a clock glitch, ie system time going backwards; though
4811  * the more likely explanation is just delayed message receipt. It is
4812  * worth expending a GetCurrentTimestamp call to be sure, since a large
4813  * retreat in the system clock reading could otherwise cause us to neglect
4814  * to update the stats file for a long time.
4815  */
4816  dbentry = pgstat_get_db_entry(msg->databaseid, false);
4817  if (dbentry == NULL)
4818  {
4819  /*
4820  * We have no data for this DB. Enter a write request anyway so that
4821  * the global stats will get updated. This is needed to prevent
4822  * backend_read_statsfile from waiting for data that we cannot supply,
4823  * in the case of a new DB that nobody has yet reported any stats for.
4824  * See the behavior of pgstat_read_db_statsfile_timestamp.
4825  */
4826  }
4827  else if (msg->clock_time < dbentry->stats_timestamp)
4828  {
4829  TimestampTz cur_ts = GetCurrentTimestamp();
4830 
4831  if (cur_ts < dbentry->stats_timestamp)
4832  {
4833  /*
4834  * Sure enough, time went backwards. Force a new stats file write
4835  * to get back in sync; but first, log a complaint.
4836  */
4837  char *writetime;
4838  char *mytime;
4839 
4840  /* Copy because timestamptz_to_str returns a static buffer */
4841  writetime = pstrdup(timestamptz_to_str(dbentry->stats_timestamp));
4842  mytime = pstrdup(timestamptz_to_str(cur_ts));
4843  ereport(LOG,
4844  (errmsg("stats_timestamp %s is later than collector's time %s for database %u",
4845  writetime, mytime, dbentry->databaseid)));
4846  pfree(writetime);
4847  pfree(mytime);
4848  }
4849  else
4850  {
4851  /*
4852  * Nope, it's just an old request. Assuming msg's clock_time is
4853  * >= its cutoff_time, it must be stale, so we can ignore it.
4854  */
4855  return;
4856  }
4857  }
4858  else if (msg->cutoff_time <= dbentry->stats_timestamp)
4859  {
4860  /* Stale request, ignore it */
4861  return;
4862  }
4863 
4864  /*
4865  * We need to write this DB, so create a request.
4866  */
4868  msg->databaseid);
4869 }
TimestampTz stats_timestamp
Definition: pgstat.h:787
static List * pending_write_requests
Definition: pgstat.c:290
TimestampTz clock_time
Definition: pgstat.h:274
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
int64 TimestampTz
Definition: timestamp.h:39
char * pstrdup(const char *in)
Definition: mcxt.c:1299
#define LOG
Definition: elog.h:26
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
void pfree(void *pointer)
Definition: mcxt.c:1169
#define DEBUG2
Definition: elog.h:24
#define ereport(elevel,...)
Definition: elog.h:157
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:689
static PgStat_StatDBEntry * pgstat_get_db_entry(Oid databaseid, bool create)
Definition: pgstat.c:3557
TimestampTz cutoff_time
Definition: pgstat.h:275
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
const char * timestamptz_to_str(TimestampTz t)
Definition: timestamp.c:1774

◆ pgstat_recv_recoveryconflict()

static void pgstat_recv_recoveryconflict ( PgStat_MsgRecoveryConflict msg,
int  len 
)
static

Definition at line 5460 of file pgstat.c.

References PgStat_MsgRecoveryConflict::m_databaseid, PgStat_MsgRecoveryConflict::m_reason, PgStat_StatDBEntry::n_conflict_bufferpin, PgStat_StatDBEntry::n_conflict_lock, PgStat_StatDBEntry::n_conflict_snapshot, PgStat_StatDBEntry::n_conflict_startup_deadlock, PgStat_StatDBEntry::n_conflict_tablespace, pgstat_get_db_entry(), PROCSIG_RECOVERY_CONFLICT_BUFFERPIN, PROCSIG_RECOVERY_CONFLICT_DATABASE, PROCSIG_RECOVERY_CONFLICT_LOCK, PROCSIG_RECOVERY_CONFLICT_SNAPSHOT, PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK, and PROCSIG_RECOVERY_CONFLICT_TABLESPACE.

Referenced by PgstatCollectorMain().

5461 {
5462  PgStat_StatDBEntry *dbentry;
5463 
5464  dbentry = pgstat_get_db_entry(msg->m_databaseid, true);
5465 
5466  switch (msg->m_reason)
5467  {
5469 
5470  /*
5471  * Since we drop the information about the database as soon as it
5472  * replicates, there is no point in counting these conflicts.
5473  */
5474  break;
5476  dbentry->n_conflict_tablespace++;
5477  break;
5479  dbentry->n_conflict_lock++;
5480  break;
5482  dbentry->n_conflict_snapshot++;
5483  break;
5485  dbentry->n_conflict_bufferpin++;
5486  break;
5488  dbentry->n_conflict_startup_deadlock++;
5489  break;
5490  }
5491 }
PgStat_Counter n_conflict_bufferpin
Definition: pgstat.h:769
PgStat_Counter n_conflict_startup_deadlock
Definition: pgstat.h:770
PgStat_Counter n_conflict_tablespace
Definition: pgstat.h:766
PgStat_Counter n_conflict_snapshot
Definition: pgstat.h:768
PgStat_Counter n_conflict_lock
Definition: pgstat.h:767
static PgStat_StatDBEntry * pgstat_get_db_entry(Oid databaseid, bool create)
Definition: pgstat.c:3557

◆ pgstat_recv_recoveryprefetch()

static void pgstat_recv_recoveryprefetch ( PgStat_MsgRecoveryPrefetch msg,
int  len 
)
static

Definition at line 5448 of file pgstat.c.

References PgStat_MsgRecoveryPrefetch::m_stats.

Referenced by PgstatCollectorMain().

5449 {
5451 }
PgStat_RecoveryPrefetchStats m_stats
Definition: pgstat.h:562
static PgStat_RecoveryPrefetchStats recoveryPrefetchStats
Definition: pgstat.c:283

◆ pgstat_recv_replslot()

static void pgstat_recv_replslot ( PgStat_MsgReplSlot msg,
int  len 
)
static

Definition at line 5533 of file pgstat.c.

References Assert, idx(), PgStat_MsgReplSlot::m_drop, PgStat_MsgReplSlot::m_slotname, PgStat_MsgReplSlot::m_spill_bytes, PgStat_MsgReplSlot::m_spill_count, PgStat_MsgReplSlot::m_spill_txns, PgStat_MsgReplSlot::m_stream_bytes, PgStat_MsgReplSlot::m_stream_count, PgStat_MsgReplSlot::m_stream_txns, PgStat_MsgReplSlot::m_total_bytes, PgStat_MsgReplSlot::m_total_txns, NameStr, nReplSlotStats, pgstat_replslot_index(), PgStat_ReplSlotStats::spill_bytes, PgStat_ReplSlotStats::spill_count, PgStat_ReplSlotStats::spill_txns, PgStat_ReplSlotStats::stream_bytes, PgStat_ReplSlotStats::stream_count, PgStat_ReplSlotStats::stream_txns, PgStat_ReplSlotStats::total_bytes, and PgStat_ReplSlotStats::total_txns.

Referenced by PgstatCollectorMain().

5534 {
5535  int idx;
5536 
5537  /*
5538  * Get the index of replication slot statistics. On dropping, we don't
5539  * create the new statistics.
5540  */
5541  idx = pgstat_replslot_index(NameStr(msg->m_slotname), !msg->m_drop);
5542 
5543  /*
5544  * The slot entry is not found or there is no space to accommodate the new
5545  * entry. This could happen when the message for the creation of a slot
5546  * reached before the drop message even though the actual operations
5547  * happen in reverse order. In such a case, the next update of the
5548  * statistics for the same slot will create the required entry.
5549  */
5550  if (idx < 0)
5551  return;
5552 
5553  /* it must be a valid replication slot index */
5554  Assert(idx < nReplSlotStats);
5555 
5556  if (msg->m_drop)
5557  {
5558  /* Remove the replication slot statistics with the given name */
5559  if (idx < nReplSlotStats - 1)
5560  memcpy(&replSlotStats[idx], &replSlotStats[nReplSlotStats - 1],
5561  sizeof(PgStat_ReplSlotStats));
5562  nReplSlotStats--;
5563  }
5564  else
5565  {
5566  /* Update the replication slot statistics */
5575  }
5576 }
PgStat_Counter stream_count
Definition: pgstat.h:927
PgStat_Counter total_txns
Definition: pgstat.h:929
PgStat_Counter m_spill_bytes
Definition: pgstat.h:547
PgStat_Counter spill_txns
Definition: pgstat.h:923
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
PgStat_Counter spill_bytes
Definition: pgstat.h:925
PgStat_Counter m_stream_bytes
Definition: pgstat.h:550
PgStat_Counter stream_bytes
Definition: pgstat.h:928
PgStat_Counter m_spill_txns
Definition: pgstat.h:545
static int nReplSlotStats
Definition: pgstat.c:282
static int pgstat_replslot_index(const char *name, bool create_it)
Definition: pgstat.c:5761
PgStat_Counter m_stream_txns
Definition: pgstat.h:548
#define Assert(condition)
Definition: c.h:804
PgStat_Counter m_total_txns
Definition: pgstat.h:551
PgStat_Counter m_spill_count
Definition: pgstat.h:546
PgStat_Counter m_total_bytes
Definition: pgstat.h:552
PgStat_Counter total_bytes
Definition: pgstat.h:930
#define NameStr(name)
Definition: c.h:681
PgStat_Counter m_stream_count
Definition: pgstat.h:549
NameData m_slotname
Definition: pgstat.h:543
static PgStat_ReplSlotStats * replSlotStats
Definition: pgstat.c:281
PgStat_Counter spill_count
Definition: pgstat.h:924
PgStat_Counter stream_txns
Definition: pgstat.h:926

◆ pgstat_recv_resetcounter()

static void pgstat_recv_resetcounter ( PgStat_MsgResetcounter msg,
int  len 
)
static

Definition at line 5066 of file pgstat.c.

References PgStat_StatDBEntry::functions, hash_destroy(), PgStat_MsgResetcounter::m_databaseid, pgstat_get_db_entry(), reset_dbentry_counters(), and PgStat_StatDBEntry::tables.

Referenced by PgstatCollectorMain().

5067 {
5068  PgStat_StatDBEntry *dbentry;
5069 
5070  /*
5071  * Lookup the database in the hashtable. Nothing to do if not there.
5072  */
5073  dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
5074 
5075  if (!dbentry)
5076  return;
5077 
5078  /*
5079  * We simply throw away all the database's table entries by recreating a
5080  * new hash table for them.
5081  */
5082  if (dbentry->tables != NULL)
5083  hash_destroy(dbentry->tables);
5084  if (dbentry->functions != NULL)
5085  hash_destroy(dbentry->functions);
5086 
5087  dbentry->tables = NULL;
5088  dbentry->functions = NULL;
5089 
5090  /*
5091  * Reset database-level stats, too. This creates empty hash tables for
5092  * tables and functions.
5093  */
5094  reset_dbentry_counters(dbentry);
5095 }
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:862
HTAB * functions
Definition: pgstat.h:794
static void reset_dbentry_counters(PgStat_StatDBEntry *dbentry)
Definition: pgstat.c:3499
static PgStat_StatDBEntry * pgstat_get_db_entry(Oid databaseid, bool create)
Definition: pgstat.c:3557

◆ pgstat_recv_resetreplslotcounter()

static void pgstat_recv_resetreplslotcounter ( PgStat_MsgResetreplslotcounter msg,
int  len 
)
static

Definition at line 5189 of file pgstat.c.

References PgStat_MsgResetreplslotcounter::clearall, GetCurrentTimestamp(), i, idx(), PgStat_MsgResetreplslotcounter::m_slotname, NameStr, nReplSlotStats, pgstat_replslot_index(), and pgstat_reset_replslot().

Referenced by PgstatCollectorMain().

5191 {
5192  int i;
5193  int idx = -1;
5194  TimestampTz ts;
5195 
5196  ts = GetCurrentTimestamp();
5197  if (msg->clearall)
5198  {
5199  for (i = 0; i < nReplSlotStats; i++)
5200  pgstat_reset_replslot(i, ts);
5201  }
5202  else
5203  {
5204  /* Get the index of replication slot statistics to reset */
5205  idx = pgstat_replslot_index(NameStr(msg->m_slotname), false);
5206 
5207  /*
5208  * Nothing to do if the given slot entry is not found. This could
5209  * happen when the slot with the given name is removed and the
5210  * corresponding statistics entry is also removed before receiving the
5211  * reset message.
5212  */
5213  if (idx < 0)
5214  return;
5215 
5216  /* Reset the stats for the requested replication slot */
5217  pgstat_reset_replslot(idx, ts);
5218  }
5219 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
int64 TimestampTz
Definition: timestamp.h:39
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
static void pgstat_reset_replslot(int i, TimestampTz ts)
Definition: pgstat.c:5793
static int nReplSlotStats
Definition: pgstat.c:282
static int pgstat_replslot_index(const char *name, bool create_it)
Definition: pgstat.c:5761
int i
#define NameStr(name)
Definition: c.h:681

◆ pgstat_recv_resetsharedcounter()

static void pgstat_recv_resetsharedcounter ( PgStat_MsgResetsharedcounter msg,
int  len 
)
static

Definition at line 5104 of file pgstat.c.

References GetCurrentTimestamp(), PgStat_MsgResetsharedcounter::m_resettarget, RESET_ARCHIVER, RESET_BGWRITER, RESET_WAL, PgStat_ArchiverStats::stat_reset_timestamp, PgStat_GlobalStats::stat_reset_timestamp, and PgStat_WalStats::stat_reset_timestamp.

Referenced by PgstatCollectorMain().

5105 {
5106  if (msg->m_resettarget == RESET_BGWRITER)
5107  {
5108  /* Reset the global background writer statistics for the cluster. */
5109  memset(&globalStats, 0, sizeof(globalStats));
5111  }
5112  else if (msg->m_resettarget == RESET_ARCHIVER)
5113  {
5114  /* Reset the archiver statistics for the cluster. */
5115  memset(&archiverStats, 0, sizeof(archiverStats));
5117  }
5118  else if (msg->m_resettarget == RESET_WAL)
5119  {
5120  /* Reset the WAL statistics for the cluster. */
5121  memset(&walStats, 0, sizeof(walStats));
5123  }
5124 
5125  /*
5126  * Presumably the sender of this message validated the target, don't
5127  * complain here if it's not valid
5128  */
5129 }
TimestampTz stat_reset_timestamp
Definition: pgstat.h:899
static PgStat_GlobalStats globalStats
Definition: pgstat.c:278
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
static PgStat_WalStats walStats
Definition: pgstat.c:279
PgStat_Shared_Reset_Target m_resettarget
Definition: pgstat.h:361
static PgStat_ArchiverStats archiverStats
Definition: pgstat.c:277
TimestampTz stat_reset_timestamp
Definition: pgstat.h:883
TimestampTz stat_reset_timestamp
Definition: pgstat.h:864

◆ pgstat_recv_resetsinglecounter()

static void pgstat_recv_resetsinglecounter ( PgStat_MsgResetsinglecounter msg,
int  len 
)
static

Definition at line 5138 of file pgstat.c.

References PgStat_StatDBEntry::functions, GetCurrentTimestamp(), HASH_REMOVE, hash_search(), PgStat_MsgResetsinglecounter::m_databaseid, PgStat_MsgResetsinglecounter::m_objectid, PgStat_MsgResetsinglecounter::m_resettype, pgstat_get_db_entry(), RESET_FUNCTION, RESET_TABLE, PgStat_StatDBEntry::stat_reset_timestamp, and PgStat_StatDBEntry::tables.

Referenced by PgstatCollectorMain().

5139 {
5140  PgStat_StatDBEntry *dbentry;
5141 
5142  dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
5143 
5144  if (!dbentry)
5145  return;
5146 
5147  /* Set the reset timestamp for the whole database */
5149 
5150  /* Remove object if it exists, ignore it if not */
5151  if (msg->m_resettype == RESET_TABLE)
5152  (void) hash_search(dbentry->tables, (void *) &(msg->m_objectid),
5153  HASH_REMOVE, NULL);
5154  else if (msg->m_resettype == RESET_FUNCTION)
5155  (void) hash_search(dbentry->functions, (void *) &(msg->m_objectid),
5156  HASH_REMOVE, NULL);
5157 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
HTAB * functions
Definition: pgstat.h:794
TimestampTz stat_reset_timestamp
Definition: pgstat.h:786
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
static PgStat_StatDBEntry * pgstat_get_db_entry(Oid databaseid, bool create)
Definition: pgstat.c:3557
PgStat_Single_Reset_Type m_resettype
Definition: pgstat.h:373

◆ pgstat_recv_resetslrucounter()

static void pgstat_recv_resetslrucounter ( PgStat_MsgResetslrucounter msg,
int  len 
)
static

Definition at line 5166 of file pgstat.c.

References GetCurrentTimestamp(), i, PgStat_MsgResetslrucounter::m_index, SLRU_NUM_ELEMENTS, and PgStat_SLRUStats::stat_reset_timestamp.

Referenced by PgstatCollectorMain().

5167 {
5168  int i;
5170 
5171  for (i = 0; i < SLRU_NUM_ELEMENTS; i++)
5172  {
5173  /* reset entry with the given index, or all entries (index is -1) */
5174  if ((msg->m_index == -1) || (msg->m_index == i))
5175  {
5176  memset(&slruStats[i], 0, sizeof(slruStats[i]));
5178  }
5179  }
5180 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
int64 TimestampTz
Definition: timestamp.h:39
#define SLRU_NUM_ELEMENTS
Definition: pgstat.c:158
TimestampTz stat_reset_timestamp
Definition: pgstat.h:914
static PgStat_SLRUStats slruStats[SLRU_NUM_ELEMENTS]
Definition: pgstat.c:280
int i

◆ pgstat_recv_slru()

static void pgstat_recv_slru ( PgStat_MsgSLRU msg,
int  len 
)
static

Definition at line 5430 of file pgstat.c.

References PgStat_SLRUStats::blocks_exists, PgStat_SLRUStats::blocks_hit, PgStat_SLRUStats::blocks_read, PgStat_SLRUStats::blocks_written, PgStat_SLRUStats::blocks_zeroed, PgStat_SLRUStats::flush, PgStat_MsgSLRU::m_blocks_exists, PgStat_MsgSLRU::m_blocks_hit, PgStat_MsgSLRU::m_blocks_read, PgStat_MsgSLRU::m_blocks_written, PgStat_MsgSLRU::m_blocks_zeroed, PgStat_MsgSLRU::m_flush, PgStat_MsgSLRU::m_index, PgStat_MsgSLRU::m_truncate, and PgStat_SLRUStats::truncate.

Referenced by PgstatCollectorMain().

5431 {
5433  slruStats[msg->m_index].blocks_hit += msg->m_blocks_hit;
5437  slruStats[msg->m_index].flush += msg->m_flush;
5438  slruStats[msg->m_index].truncate += msg->m_truncate;
5439 }
PgStat_Counter m_flush
Definition: pgstat.h:531
PgStat_Counter blocks_exists
Definition: pgstat.h:911
PgStat_Counter m_blocks_exists
Definition: pgstat.h:530
PgStat_Counter m_index
Definition: pgstat.h:525
PgStat_Counter m_blocks_written
Definition: pgstat.h:529
PgStat_Counter blocks_read
Definition: pgstat.h:909
PgStat_Counter blocks_hit
Definition: pgstat.h:908
PgStat_Counter blocks_zeroed
Definition: pgstat.h:907
PgStat_Counter m_blocks_read
Definition: pgstat.h:528
PgStat_Counter flush
Definition: pgstat.h:912
static PgStat_SLRUStats slruStats[SLRU_NUM_ELEMENTS]
Definition: pgstat.c:280
PgStat_Counter m_blocks_zeroed
Definition: pgstat.h:526
PgStat_Counter m_truncate
Definition: pgstat.h:532
PgStat_Counter truncate
Definition: pgstat.h:913
PgStat_Counter blocks_written
Definition: pgstat.h:910
PgStat_Counter m_blocks_hit
Definition: pgstat.h:527

◆ pgstat_recv_tabpurge()

static void pgstat_recv_tabpurge ( PgStat_MsgTabpurge msg,
int  len 
)
static

Definition at line 4990 of file pgstat.c.

References HASH_REMOVE, hash_search(), i, PgStat_MsgTabpurge::m_databaseid, PgStat_MsgTabpurge::m_nentries, PgStat_MsgTabpurge::m_tableid, pgstat_get_db_entry(), and PgStat_StatDBEntry::tables.

Referenced by PgstatCollectorMain().

4991 {
4992  PgStat_StatDBEntry *dbentry;
4993  int i;
4994 
4995  dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
4996 
4997  /*
4998  * No need to purge if we don't even know the database.
4999  */
5000  if (!dbentry || !dbentry->tables)
5001  return;
5002 
5003  /*
5004  * Process all table entries in the message.
5005  */
5006  for (i = 0; i < msg->m_nentries; i++)
5007  {
5008  /* Remove from hashtable if present; we don't care if it's not. */
5009  (void) hash_search(dbentry->tables,
5010  (void *) &(msg->m_tableid[i]),
5011  HASH_REMOVE, NULL);
5012  }
5013 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
Oid m_tableid[PGSTAT_NUM_TABPURGE]
Definition: pgstat.h:326
static PgStat_StatDBEntry * pgstat_get_db_entry(Oid databaseid, bool create)
Definition: pgstat.c:3557
int i

◆ pgstat_recv_tabstat()

static void pgstat_recv_tabstat ( PgStat_MsgTabstat msg,
int  len 
)
static

Definition at line 4879 of file pgstat.c.

References PgStat_StatTabEntry::analyze_count, PgStat_StatTabEntry::analyze_timestamp, PgStat_StatTabEntry::autovac_analyze_count, PgStat_StatTabEntry::autovac_analyze_timestamp, PgStat_StatTabEntry::autovac_vacuum_count, PgStat_StatTabEntry::autovac_vacuum_timestamp, PgStat_StatTabEntry::blocks_fetched, PgStat_StatTabEntry::blocks_hit, PgStat_StatTabEntry::changes_since_analyze, PgStat_StatTabEntry::changes_since_analyze_reported, HASH_ENTER, hash_search(), i, PgStat_StatTabEntry::inserts_since_vacuum, PgStat_MsgTabstat::m_block_read_time, PgStat_MsgTabstat::m_block_write_time, PgStat_MsgTabstat::m_databaseid, PgStat_MsgTabstat::m_entry, PgStat_MsgTabstat::m_nentries, PgStat_MsgTabstat::m_xact_commit, PgStat_MsgTabstat::m_xact_rollback, Max, PgStat_StatDBEntry::n_block_read_time, PgStat_StatDBEntry::n_block_write_time, PgStat_StatDBEntry::n_blocks_fetched, PgStat_StatDBEntry::n_blocks_hit, PgStat_StatTabEntry::n_dead_tuples, PgStat_StatTabEntry::n_live_tuples, PgStat_StatDBEntry::n_tuples_deleted, PgStat_StatDBEntry::n_tuples_fetched, PgStat_StatDBEntry::n_tuples_inserted, PgStat_StatDBEntry::n_tuples_returned, PgStat_StatDBEntry::n_tuples_updated, PgStat_StatDBEntry::n_xact_commit, PgStat_StatDBEntry::n_xact_rollback, PgStat_StatTabEntry::numscans, pgstat_get_db_entry(), PgStat_TableCounts::t_blocks_fetched, PgStat_TableCounts::t_blocks_hit, PgStat_TableCounts::t_changed_tuples, PgStat_TableEntry::t_counts, PgStat_TableCounts::t_delta_dead_tuples, PgStat_TableCounts::t_delta_live_tuples, PgStat_TableEntry::t_id, PgStat_TableCounts::t_numscans, PgStat_TableCounts::t_truncated, PgStat_TableCounts::t_tuples_deleted, PgStat_TableCounts::t_tuples_fetched, PgStat_TableCounts::t_tuples_hot_updated, PgStat_TableCounts::t_tuples_inserted, PgStat_TableCounts::t_tuples_returned, PgStat_TableCounts::t_tuples_updated, PgStat_StatDBEntry::tables, PgStat_StatTabEntry::tuples_deleted, PgStat_StatTabEntry::tuples_fetched, PgStat_StatTabEntry::tuples_hot_updated, PgStat_StatTabEntry::tuples_inserted, PgStat_StatTabEntry::tuples_returned, PgStat_StatTabEntry::tuples_updated, PgStat_StatTabEntry::vacuum_count, and PgStat_StatTabEntry::vacuum_timestamp.

Referenced by PgstatCollectorMain().

4880 {
4881  PgStat_StatDBEntry *dbentry;
4882  PgStat_StatTabEntry *tabentry;
4883  int i;
4884  bool found;
4885 
4886  dbentry = pgstat_get_db_entry(msg->m_databaseid, true);
4887 
4888  /*
4889  * Update database-wide stats.
4890  */
4891  dbentry->n_xact_commit += (PgStat_Counter) (msg->m_xact_commit);
4892  dbentry->n_xact_rollback += (PgStat_Counter) (msg->m_xact_rollback);
4893  dbentry->n_block_read_time += msg->m_block_read_time;
4894  dbentry->n_block_write_time += msg->m_block_write_time;
4895 
4896  /*
4897  * Process all table entries in the message.
4898  */
4899  for (i = 0; i < msg->m_nentries; i++)
4900  {
4901  PgStat_TableEntry *tabmsg = &(msg->m_entry[i]);
4902 
4903  tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
4904  (void *) &(tabmsg->t_id),
4905  HASH_ENTER, &found);
4906 
4907  if (!found)
4908  {
4909  /*
4910  * If it's a new table entry, initialize counters to the values we
4911  * just got.
4912  */
4913  tabentry->numscans = tabmsg->t_counts.t_numscans;
4914  tabentry->tuples_returned = tabmsg->t_counts.t_tuples_returned;
4915  tabentry->tuples_fetched = tabmsg->t_counts.t_tuples_fetched;
4916  tabentry->tuples_inserted = tabmsg->t_counts.t_tuples_inserted;
4917  tabentry->tuples_updated = tabmsg->t_counts.t_tuples_updated;
4918  tabentry->tuples_deleted = tabmsg->t_counts.t_tuples_deleted;
4919  tabentry->tuples_hot_updated = tabmsg->t_counts.t_tuples_hot_updated;
4920  tabentry->n_live_tuples = tabmsg->t_counts.t_delta_live_tuples;
4921  tabentry->n_dead_tuples = tabmsg->t_counts.t_delta_dead_tuples;
4922  tabentry->changes_since_analyze = tabmsg->t_counts.t_changed_tuples;
4923  tabentry->changes_since_analyze_reported = 0;
4924  tabentry->inserts_since_vacuum = tabmsg->t_counts.t_tuples_inserted;
4925  tabentry->blocks_fetched = tabmsg->t_counts.t_blocks_fetched;
4926  tabentry->blocks_hit = tabmsg->t_counts.t_blocks_hit;
4927 
4928  tabentry->vacuum_timestamp = 0;
4929  tabentry->vacuum_count = 0;
4930  tabentry->autovac_vacuum_timestamp = 0;
4931  tabentry->autovac_vacuum_count = 0;
4932  tabentry->analyze_timestamp = 0;
4933  tabentry->analyze_count = 0;
4934  tabentry->autovac_analyze_timestamp = 0;
4935  tabentry->autovac_analyze_count = 0;
4936  }
4937  else
4938  {
4939  /*
4940  * Otherwise add the values to the existing entry.
4941  */
4942  tabentry->numscans += tabmsg->t_counts.t_numscans;
4943  tabentry->tuples_returned += tabmsg->t_counts.t_tuples_returned;
4944  tabentry->tuples_fetched += tabmsg->t_counts.t_tuples_fetched;
4945  tabentry->tuples_inserted += tabmsg->t_counts.t_tuples_inserted;
4946  tabentry->tuples_updated += tabmsg->t_counts.t_tuples_updated;
4947  tabentry->tuples_deleted += tabmsg->t_counts.t_tuples_deleted;
4948  tabentry->tuples_hot_updated += tabmsg->t_counts.t_tuples_hot_updated;
4949  /* If table was truncated, first reset the live/dead counters */
4950  if (tabmsg->t_counts.t_truncated)
4951  {
4952  tabentry->n_live_tuples = 0;
4953  tabentry->n_dead_tuples = 0;
4954  tabentry->inserts_since_vacuum = 0;
4955  }
4956  tabentry->n_live_tuples += tabmsg->t_counts.t_delta_live_tuples;
4957  tabentry->n_dead_tuples += tabmsg->t_counts.t_delta_dead_tuples;
4958  tabentry->changes_since_analyze += tabmsg->t_counts.t_changed_tuples;
4959  tabentry->inserts_since_vacuum += tabmsg->t_counts.t_tuples_inserted;
4960  tabentry->blocks_fetched += tabmsg->t_counts.t_blocks_fetched;
4961  tabentry->blocks_hit += tabmsg->t_counts.t_blocks_hit;
4962  }
4963 
4964  /* Clamp n_live_tuples in case of negative delta_live_tuples */
4965  tabentry->n_live_tuples = Max(tabentry->n_live_tuples, 0);
4966  /* Likewise for n_dead_tuples */
4967  tabentry->n_dead_tuples = Max(tabentry->n_dead_tuples, 0);
4968 
4969  /*
4970  * Add per-table stats to the per-database entry, too.
4971  */
4972  dbentry->n_tuples_returned += tabmsg->t_counts.t_tuples_returned;
4973  dbentry->n_tuples_fetched += tabmsg->t_counts.t_tuples_fetched;
4974  dbentry->n_tuples_inserted += tabmsg->t_counts.t_tuples_inserted;
4975  dbentry->n_tuples_updated += tabmsg->t_counts.t_tuples_updated;
4976  dbentry->n_tuples_deleted += tabmsg->t_counts.t_tuples_deleted;
4977  dbentry->n_blocks_fetched += tabmsg->t_counts.t_blocks_fetched;
4978  dbentry->n_blocks_hit += tabmsg->t_counts.t_blocks_hit;
4979  }
4980 }
PgStat_Counter analyze_count
Definition: pgstat.h:830
PgStat_Counter t_delta_live_tuples
Definition: pgstat.h:128
int64 PgStat_Counter
Definition: pgstat.h:92
PgStat_Counter tuples_returned
Definition: pgstat.h:808
PgStat_Counter t_tuples_fetched
Definition: pgstat.h:120
PgStat_Counter n_blocks_hit
Definition: pgstat.h:759
PgStat_Counter t_tuples_hot_updated
Definition: pgstat.h:125
TimestampTz autovac_analyze_timestamp
Definition: pgstat.h:831
PgStat_Counter n_tuples_returned
Definition: pgstat.h:760
PgStat_Counter vacuum_count
Definition: pgstat.h:826
PgStat_Counter numscans
Definition: pgstat.h:806
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
PgStat_Counter tuples_hot_updated
Definition: pgstat.h:814
PgStat_Counter t_tuples_returned
Definition: pgstat.h:119
PgStat_Counter tuples_inserted
Definition: pgstat.h:811
PgStat_Counter n_xact_commit
Definition: pgstat.h:756
PgStat_Counter n_blocks_fetched
Definition: pgstat.h:758
PgStat_Counter n_live_tuples
Definition: pgstat.h:816
PgStat_Counter inserts_since_vacuum
Definition: pgstat.h:820
TimestampTz vacuum_timestamp
Definition: pgstat.h:825
PgStat_Counter t_tuples_updated
Definition: pgstat.h:123
PgStat_Counter n_dead_tuples
Definition: pgstat.h:817
PgStat_Counter n_tuples_inserted
Definition: pgstat.h:762
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:129
PgStat_Counter tuples_fetched
Definition: pgstat.h:809
PgStat_Counter m_block_write_time
Definition: pgstat.h:307
PgStat_Counter n_tuples_deleted
Definition: pgstat.h:764
PgStat_Counter t_blocks_hit
Definition: pgstat.h:133
PgStat_Counter n_xact_rollback
Definition: pgstat.h:757