PostgreSQL Source Code  git master
pgstat.h File Reference
Include dependency graph for pgstat.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PgStat_TableCounts
 
struct  PgStat_TableStatus
 
struct  PgStat_TableXactStatus
 
struct  PgStat_MsgHdr
 
struct  PgStat_MsgDummy
 
struct  PgStat_MsgInquiry
 
struct  PgStat_TableEntry
 
struct  PgStat_MsgTabstat
 
struct  PgStat_MsgTabpurge
 
struct  PgStat_MsgDropdb
 
struct  PgStat_MsgResetcounter
 
struct  PgStat_MsgResetsharedcounter
 
struct  PgStat_MsgResetsinglecounter
 
struct  PgStat_MsgResetslrucounter
 
struct  PgStat_MsgResetreplslotcounter
 
struct  PgStat_MsgAutovacStart
 
struct  PgStat_MsgVacuum
 
struct  PgStat_MsgAnalyze
 
struct  PgStat_MsgArchiver
 
struct  PgStat_MsgBgWriter
 
struct  PgStat_MsgCheckpointer
 
struct  PgStat_MsgWal
 
struct  PgStat_MsgSLRU
 
struct  PgStat_MsgReplSlot
 
struct  PgStat_MsgRecoveryConflict
 
struct  PgStat_MsgTempFile
 
struct  PgStat_FunctionCounts
 
struct  PgStat_BackendFunctionEntry
 
struct  PgStat_FunctionEntry
 
struct  PgStat_MsgFuncstat
 
struct  PgStat_MsgFuncpurge
 
struct  PgStat_MsgDeadlock
 
struct  PgStat_MsgChecksumFailure
 
struct  PgStat_MsgConnect
 
struct  PgStat_MsgDisconnect
 
union  PgStat_Msg
 
struct  PgStat_StatDBEntry
 
struct  PgStat_StatTabEntry
 
struct  PgStat_StatFuncEntry
 
struct  PgStat_ArchiverStats
 
struct  PgStat_BgWriterStats
 
struct  PgStat_CheckpointerStats
 
struct  PgStat_GlobalStats
 
struct  PgStat_WalStats
 
struct  PgStat_SLRUStats
 
struct  PgStat_StatReplSlotEntry
 
struct  PgStat_FunctionCallUsage
 

Macros

#define PGSTAT_STAT_PERMANENT_DIRECTORY   "pg_stat"
 
#define PGSTAT_STAT_PERMANENT_FILENAME   "pg_stat/global.stat"
 
#define PGSTAT_STAT_PERMANENT_TMPFILE   "pg_stat/global.tmp"
 
#define PG_STAT_TMP_DIR   "pg_stat_tmp"
 
#define PGSTAT_MAX_MSG_SIZE   1000
 
#define PGSTAT_MSG_PAYLOAD   (PGSTAT_MAX_MSG_SIZE - sizeof(PgStat_MsgHdr))
 
#define PGSTAT_NUM_TABENTRIES
 
#define PGSTAT_NUM_TABPURGE
 
#define PGSTAT_NUM_FUNCENTRIES
 
#define PGSTAT_NUM_FUNCPURGE
 
#define PGSTAT_FILE_FORMAT_ID   0x01A5BCA4
 
#define pgstat_count_heap_scan(rel)
 
#define pgstat_count_heap_getnext(rel)
 
#define pgstat_count_heap_fetch(rel)
 
#define pgstat_count_index_scan(rel)
 
#define pgstat_count_index_tuples(rel, n)
 
#define pgstat_count_buffer_read(rel)
 
#define pgstat_count_buffer_hit(rel)
 
#define pgstat_count_buffer_read_time(n)   (pgStatBlockReadTime += (n))
 
#define pgstat_count_buffer_write_time(n)   (pgStatBlockWriteTime += (n))
 
#define pgstat_count_conn_active_time(n)   (pgStatActiveTime += (n))
 
#define pgstat_count_conn_txn_idle_time(n)   (pgStatTransactionIdleTime += (n))
 

Typedefs

typedef enum TrackFunctionsLevel TrackFunctionsLevel
 
typedef enum SessionEndType SessionEndType
 
typedef enum StatMsgType StatMsgType
 
typedef int64 PgStat_Counter
 
typedef struct PgStat_TableCounts PgStat_TableCounts
 
typedef enum PgStat_Shared_Reset_Target PgStat_Shared_Reset_Target
 
typedef enum PgStat_Single_Reset_Type PgStat_Single_Reset_Type
 
typedef struct PgStat_TableStatus PgStat_TableStatus
 
typedef struct PgStat_TableXactStatus PgStat_TableXactStatus
 
typedef struct PgStat_MsgHdr PgStat_MsgHdr
 
typedef struct PgStat_MsgDummy PgStat_MsgDummy
 
typedef struct PgStat_MsgInquiry PgStat_MsgInquiry
 
typedef struct PgStat_TableEntry PgStat_TableEntry
 
typedef struct PgStat_MsgTabstat PgStat_MsgTabstat
 
typedef struct PgStat_MsgTabpurge PgStat_MsgTabpurge
 
typedef struct PgStat_MsgDropdb PgStat_MsgDropdb
 
typedef struct PgStat_MsgResetcounter PgStat_MsgResetcounter
 
typedef struct PgStat_MsgResetsharedcounter PgStat_MsgResetsharedcounter
 
typedef struct PgStat_MsgResetsinglecounter PgStat_MsgResetsinglecounter
 
typedef struct PgStat_MsgResetslrucounter PgStat_MsgResetslrucounter
 
typedef struct PgStat_MsgResetreplslotcounter PgStat_MsgResetreplslotcounter
 
typedef struct PgStat_MsgAutovacStart PgStat_MsgAutovacStart
 
typedef struct PgStat_MsgVacuum PgStat_MsgVacuum
 
typedef struct PgStat_MsgAnalyze PgStat_MsgAnalyze
 
typedef struct PgStat_MsgArchiver PgStat_MsgArchiver
 
typedef struct PgStat_MsgBgWriter PgStat_MsgBgWriter
 
typedef struct PgStat_MsgCheckpointer PgStat_MsgCheckpointer
 
typedef struct PgStat_MsgWal PgStat_MsgWal
 
typedef struct PgStat_MsgSLRU PgStat_MsgSLRU
 
typedef struct PgStat_MsgReplSlot PgStat_MsgReplSlot
 
typedef struct PgStat_MsgRecoveryConflict PgStat_MsgRecoveryConflict
 
typedef struct PgStat_MsgTempFile PgStat_MsgTempFile
 
typedef struct PgStat_FunctionCounts PgStat_FunctionCounts
 
typedef struct PgStat_BackendFunctionEntry PgStat_BackendFunctionEntry
 
typedef struct PgStat_FunctionEntry PgStat_FunctionEntry
 
typedef struct PgStat_MsgFuncstat PgStat_MsgFuncstat
 
typedef struct PgStat_MsgFuncpurge PgStat_MsgFuncpurge
 
typedef struct PgStat_MsgDeadlock PgStat_MsgDeadlock
 
typedef struct PgStat_MsgChecksumFailure PgStat_MsgChecksumFailure
 
typedef struct PgStat_MsgConnect PgStat_MsgConnect
 
typedef struct PgStat_MsgDisconnect PgStat_MsgDisconnect
 
typedef union PgStat_Msg PgStat_Msg
 
typedef struct PgStat_StatDBEntry PgStat_StatDBEntry
 
typedef struct PgStat_StatTabEntry PgStat_StatTabEntry
 
typedef struct PgStat_StatFuncEntry PgStat_StatFuncEntry
 
typedef struct PgStat_ArchiverStats PgStat_ArchiverStats
 
typedef struct PgStat_BgWriterStats PgStat_BgWriterStats
 
typedef struct PgStat_CheckpointerStats PgStat_CheckpointerStats
 
typedef struct PgStat_GlobalStats PgStat_GlobalStats
 
typedef struct PgStat_WalStats PgStat_WalStats
 
typedef struct PgStat_SLRUStats PgStat_SLRUStats
 
typedef struct PgStat_StatReplSlotEntry PgStat_StatReplSlotEntry
 
typedef struct PgStat_FunctionCallUsage PgStat_FunctionCallUsage
 

Enumerations

enum  TrackFunctionsLevel { TRACK_FUNC_OFF, TRACK_FUNC_PL, TRACK_FUNC_ALL }
 
enum  SessionEndType {
  DISCONNECT_NOT_YET, DISCONNECT_NORMAL, DISCONNECT_CLIENT_EOF, DISCONNECT_FATAL,
  DISCONNECT_KILLED
}
 
enum  StatMsgType {
  PGSTAT_MTYPE_DUMMY, PGSTAT_MTYPE_INQUIRY, PGSTAT_MTYPE_TABSTAT, PGSTAT_MTYPE_TABPURGE,
  PGSTAT_MTYPE_DROPDB, PGSTAT_MTYPE_RESETCOUNTER, PGSTAT_MTYPE_RESETSHAREDCOUNTER, PGSTAT_MTYPE_RESETSINGLECOUNTER,
  PGSTAT_MTYPE_RESETSLRUCOUNTER, PGSTAT_MTYPE_RESETREPLSLOTCOUNTER, PGSTAT_MTYPE_AUTOVAC_START, PGSTAT_MTYPE_VACUUM,
  PGSTAT_MTYPE_ANALYZE, PGSTAT_MTYPE_ARCHIVER, PGSTAT_MTYPE_BGWRITER, PGSTAT_MTYPE_CHECKPOINTER,
  PGSTAT_MTYPE_WAL, PGSTAT_MTYPE_SLRU, PGSTAT_MTYPE_FUNCSTAT, PGSTAT_MTYPE_FUNCPURGE,
  PGSTAT_MTYPE_RECOVERYCONFLICT, PGSTAT_MTYPE_TEMPFILE, PGSTAT_MTYPE_DEADLOCK, PGSTAT_MTYPE_CHECKSUMFAILURE,
  PGSTAT_MTYPE_REPLSLOT, PGSTAT_MTYPE_CONNECT, PGSTAT_MTYPE_DISCONNECT
}
 
enum  PgStat_Shared_Reset_Target { RESET_ARCHIVER, RESET_BGWRITER, RESET_WAL }
 
enum  PgStat_Single_Reset_Type { RESET_TABLE, RESET_FUNCTION }
 

Functions

void pgstat_init (void)
 
int pgstat_start (void)
 
void pgstat_reset_all (void)
 
void allow_immediate_pgstat_restart (void)
 
void pgstat_ping (void)
 
void pgstat_report_stat (bool force)
 
void pgstat_vacuum_stat (void)
 
void pgstat_drop_database (Oid databaseid)
 
void pgstat_clear_snapshot (void)
 
void pgstat_reset_counters (void)
 
void pgstat_reset_shared_counters (const char *)
 
void pgstat_reset_single_counter (Oid objectid, PgStat_Single_Reset_Type type)
 
void pgstat_reset_slru_counter (const char *)
 
void pgstat_reset_replslot_counter (const char *name)
 
void pgstat_report_connect (Oid dboid)
 
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_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_replslot (const PgStat_StatReplSlotEntry *repSlotStat)
 
void pgstat_report_replslot_create (const char *slotname)
 
void pgstat_report_replslot_drop (const char *slotname)
 
void pgstat_initialize (void)
 
PgStat_TableStatusfind_tabstat_entry (Oid rel_id)
 
PgStat_BackendFunctionEntryfind_funcstat_entry (Oid func_id)
 
void pgstat_initstats (Relation rel)
 
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)
 
void pgstat_count_truncate (Relation rel)
 
void pgstat_update_heap_dead_tuples (Relation rel, int delta)
 
void pgstat_init_function_usage (struct FunctionCallInfoBaseData *fcinfo, PgStat_FunctionCallUsage *fcu)
 
void pgstat_end_function_usage (PgStat_FunctionCallUsage *fcu, bool finalize)
 
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)
 
void pgstat_send_archiver (const char *xlog, bool failed)
 
void pgstat_send_bgwriter (void)
 
void pgstat_send_checkpointer (void)
 
void pgstat_send_wal (bool force)
 
PgStat_StatDBEntrypgstat_fetch_stat_dbentry (Oid dbid)
 
PgStat_StatTabEntrypgstat_fetch_stat_tabentry (Oid relid)
 
PgStat_StatFuncEntrypgstat_fetch_stat_funcentry (Oid funcid)
 
PgStat_ArchiverStatspgstat_fetch_stat_archiver (void)
 
PgStat_BgWriterStatspgstat_fetch_stat_bgwriter (void)
 
PgStat_CheckpointerStatspgstat_fetch_stat_checkpointer (void)
 
PgStat_GlobalStatspgstat_fetch_global (void)
 
PgStat_WalStatspgstat_fetch_stat_wal (void)
 
PgStat_SLRUStatspgstat_fetch_slru (void)
 
PgStat_StatReplSlotEntrypgstat_fetch_replslot (NameData slotname)
 
void pgstat_count_slru_page_zeroed (int slru_idx)
 
void pgstat_count_slru_page_hit (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_page_exists (int slru_idx)
 
void pgstat_count_slru_flush (int slru_idx)
 
void pgstat_count_slru_truncate (int slru_idx)
 
const char * pgstat_slru_name (int slru_idx)
 
int pgstat_slru_index (const char *name)
 

Variables

PGDLLIMPORT bool pgstat_track_counts
 
PGDLLIMPORT int pgstat_track_functions
 
char * pgstat_stat_directory
 
char * pgstat_stat_tmpname
 
char * pgstat_stat_filename
 
PgStat_MsgBgWriter PendingBgWriterStats
 
PgStat_MsgCheckpointer PendingCheckpointerStats
 
PgStat_MsgWal WalStats
 
PgStat_Counter pgStatBlockReadTime
 
PgStat_Counter pgStatBlockWriteTime
 
PgStat_Counter pgStatActiveTime
 
PgStat_Counter pgStatTransactionIdleTime
 
SessionEndType pgStatSessionEndCause
 

Macro Definition Documentation

◆ PG_STAT_TMP_DIR

#define PG_STAT_TMP_DIR   "pg_stat_tmp"

Definition at line 33 of file pgstat.h.

◆ pgstat_count_buffer_hit

#define pgstat_count_buffer_hit (   rel)
Value:
do { \
if ((rel)->pgstat_info != NULL) \
(rel)->pgstat_info->t_counts.t_blocks_hit++; \
} while (0)

Definition at line 1082 of file pgstat.h.

Referenced by ReadBufferExtended().

◆ pgstat_count_buffer_read

#define pgstat_count_buffer_read (   rel)
Value:
do { \
if ((rel)->pgstat_info != NULL) \
(rel)->pgstat_info->t_counts.t_blocks_fetched++; \
} while (0)

Definition at line 1077 of file pgstat.h.

Referenced by ReadBufferExtended().

◆ pgstat_count_buffer_read_time

#define pgstat_count_buffer_read_time (   n)    (pgStatBlockReadTime += (n))

Definition at line 1087 of file pgstat.h.

Referenced by ReadBuffer_common().

◆ pgstat_count_buffer_write_time

#define pgstat_count_buffer_write_time (   n)    (pgStatBlockWriteTime += (n))

Definition at line 1089 of file pgstat.h.

Referenced by FlushBuffer().

◆ pgstat_count_conn_active_time

#define pgstat_count_conn_active_time (   n)    (pgStatActiveTime += (n))

Definition at line 1091 of file pgstat.h.

Referenced by pgstat_report_activity().

◆ pgstat_count_conn_txn_idle_time

#define pgstat_count_conn_txn_idle_time (   n)    (pgStatTransactionIdleTime += (n))

Definition at line 1093 of file pgstat.h.

Referenced by pgstat_report_activity().

◆ pgstat_count_heap_fetch

#define pgstat_count_heap_fetch (   rel)
Value:
do { \
if ((rel)->pgstat_info != NULL) \
(rel)->pgstat_info->t_counts.t_tuples_fetched++; \
} while (0)

Definition at line 1062 of file pgstat.h.

Referenced by heapam_scan_bitmap_next_tuple(), and index_fetch_heap().

◆ pgstat_count_heap_getnext

#define pgstat_count_heap_getnext (   rel)
Value:
do { \
if ((rel)->pgstat_info != NULL) \
(rel)->pgstat_info->t_counts.t_tuples_returned++; \
} while (0)

Definition at line 1057 of file pgstat.h.

Referenced by heap_getnext(), heap_getnextslot(), heap_getnextslot_tidrange(), and heapam_scan_sample_next_tuple().

◆ pgstat_count_heap_scan

#define pgstat_count_heap_scan (   rel)
Value:
do { \
if ((rel)->pgstat_info != NULL) \
(rel)->pgstat_info->t_counts.t_numscans++; \
} while (0)

Definition at line 1052 of file pgstat.h.

Referenced by initscan().

◆ pgstat_count_index_scan

#define pgstat_count_index_scan (   rel)
Value:
do { \
if ((rel)->pgstat_info != NULL) \
(rel)->pgstat_info->t_counts.t_numscans++; \
} while (0)

Definition at line 1067 of file pgstat.h.

Referenced by _bt_first(), _hash_first(), bringetbitmap(), gistgetbitmap(), gistgettuple(), and spgrescan().

◆ pgstat_count_index_tuples

#define pgstat_count_index_tuples (   rel,
 
)
Value:
do { \
if ((rel)->pgstat_info != NULL) \
(rel)->pgstat_info->t_counts.t_tuples_returned += (n); \
} while (0)

Definition at line 1072 of file pgstat.h.

Referenced by index_getbitmap(), and index_getnext_tid().

◆ PGSTAT_FILE_FORMAT_ID

#define PGSTAT_FILE_FORMAT_ID   0x01A5BCA4

◆ PGSTAT_MAX_MSG_SIZE

#define PGSTAT_MAX_MSG_SIZE   1000

Definition at line 227 of file pgstat.h.

Referenced by pgstat_init().

◆ PGSTAT_MSG_PAYLOAD

#define PGSTAT_MSG_PAYLOAD   (PGSTAT_MAX_MSG_SIZE - sizeof(PgStat_MsgHdr))

Definition at line 228 of file pgstat.h.

◆ PGSTAT_NUM_FUNCENTRIES

#define PGSTAT_NUM_FUNCENTRIES
Value:
((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
unsigned int Oid
Definition: postgres_ext.h:31
#define PGSTAT_MSG_PAYLOAD
Definition: pgstat.h:228

Definition at line 608 of file pgstat.h.

Referenced by pgstat_send_funcstats().

◆ PGSTAT_NUM_FUNCPURGE

#define PGSTAT_NUM_FUNCPURGE
Value:
((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
/ sizeof(Oid))
unsigned int Oid
Definition: postgres_ext.h:31
#define PGSTAT_MSG_PAYLOAD
Definition: pgstat.h:228

Definition at line 625 of file pgstat.h.

Referenced by pgstat_vacuum_stat().

◆ PGSTAT_NUM_TABENTRIES

#define PGSTAT_NUM_TABENTRIES
Value:
((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - 3 * sizeof(int) - 5 * sizeof(PgStat_Counter)) \
/ sizeof(PgStat_TableEntry))
int64 PgStat_Counter
Definition: pgstat.h:92
unsigned int Oid
Definition: postgres_ext.h:31
struct PgStat_TableEntry PgStat_TableEntry
#define PGSTAT_MSG_PAYLOAD
Definition: pgstat.h:228

Definition at line 284 of file pgstat.h.

Referenced by pgstat_report_stat().

◆ PGSTAT_NUM_TABPURGE

#define PGSTAT_NUM_TABPURGE
Value:
((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
/ sizeof(Oid))
unsigned int Oid
Definition: postgres_ext.h:31
#define PGSTAT_MSG_PAYLOAD
Definition: pgstat.h:228

Definition at line 309 of file pgstat.h.

Referenced by pgstat_vacuum_stat().

◆ PGSTAT_STAT_PERMANENT_DIRECTORY

#define PGSTAT_STAT_PERMANENT_DIRECTORY   "pg_stat"

Definition at line 28 of file pgstat.h.

Referenced by get_dbstat_filename(), and pgstat_reset_all().

◆ PGSTAT_STAT_PERMANENT_FILENAME

#define PGSTAT_STAT_PERMANENT_FILENAME   "pg_stat/global.stat"

◆ PGSTAT_STAT_PERMANENT_TMPFILE

#define PGSTAT_STAT_PERMANENT_TMPFILE   "pg_stat/global.tmp"

Definition at line 30 of file pgstat.h.

Referenced by pgstat_write_statsfiles().

Typedef Documentation

◆ PgStat_ArchiverStats

◆ PgStat_BackendFunctionEntry

◆ PgStat_BgWriterStats

◆ PgStat_CheckpointerStats

◆ PgStat_Counter

typedef int64 PgStat_Counter

Definition at line 92 of file pgstat.h.

◆ PgStat_FunctionCallUsage

◆ PgStat_FunctionCounts

◆ PgStat_FunctionEntry

◆ PgStat_GlobalStats

◆ PgStat_Msg

typedef union PgStat_Msg PgStat_Msg

◆ PgStat_MsgAnalyze

◆ PgStat_MsgArchiver

◆ PgStat_MsgAutovacStart

◆ PgStat_MsgBgWriter

◆ PgStat_MsgCheckpointer

◆ PgStat_MsgChecksumFailure

◆ PgStat_MsgConnect

◆ PgStat_MsgDeadlock

◆ PgStat_MsgDisconnect

◆ PgStat_MsgDropdb

◆ PgStat_MsgDummy

◆ PgStat_MsgFuncpurge

◆ PgStat_MsgFuncstat

◆ PgStat_MsgHdr

typedef struct PgStat_MsgHdr PgStat_MsgHdr

◆ PgStat_MsgInquiry

◆ PgStat_MsgRecoveryConflict

◆ PgStat_MsgReplSlot

◆ PgStat_MsgResetcounter

◆ PgStat_MsgResetreplslotcounter

◆ PgStat_MsgResetsharedcounter

◆ PgStat_MsgResetsinglecounter

◆ PgStat_MsgResetslrucounter

◆ PgStat_MsgSLRU

◆ PgStat_MsgTabpurge

◆ PgStat_MsgTabstat

◆ PgStat_MsgTempFile

◆ PgStat_MsgVacuum

◆ PgStat_MsgWal

typedef struct PgStat_MsgWal PgStat_MsgWal

◆ PgStat_Shared_Reset_Target

◆ PgStat_Single_Reset_Type

◆ PgStat_SLRUStats

◆ PgStat_StatDBEntry

◆ PgStat_StatFuncEntry

◆ PgStat_StatReplSlotEntry

◆ PgStat_StatTabEntry

◆ PgStat_TableCounts

◆ PgStat_TableEntry

◆ PgStat_TableStatus

◆ PgStat_TableXactStatus

◆ PgStat_WalStats

◆ SessionEndType

◆ StatMsgType

typedef enum StatMsgType StatMsgType

◆ TrackFunctionsLevel

Enumeration Type Documentation

◆ PgStat_Shared_Reset_Target

Enumerator
RESET_ARCHIVER 
RESET_BGWRITER 
RESET_WAL 

Definition at line 137 of file pgstat.h.

138 {
141  RESET_WAL
PgStat_Shared_Reset_Target
Definition: pgstat.h:137

◆ PgStat_Single_Reset_Type

Enumerator
RESET_TABLE 
RESET_FUNCTION 

Definition at line 145 of file pgstat.h.

146 {
147  RESET_TABLE,
PgStat_Single_Reset_Type
Definition: pgstat.h:145

◆ SessionEndType

Enumerator
DISCONNECT_NOT_YET 
DISCONNECT_NORMAL 
DISCONNECT_CLIENT_EOF 
DISCONNECT_FATAL 
DISCONNECT_KILLED 

Definition at line 44 of file pgstat.h.

◆ StatMsgType

Enumerator
PGSTAT_MTYPE_DUMMY 
PGSTAT_MTYPE_INQUIRY 
PGSTAT_MTYPE_TABSTAT 
PGSTAT_MTYPE_TABPURGE 
PGSTAT_MTYPE_DROPDB 
PGSTAT_MTYPE_RESETCOUNTER 
PGSTAT_MTYPE_RESETSHAREDCOUNTER 
PGSTAT_MTYPE_RESETSINGLECOUNTER 
PGSTAT_MTYPE_RESETSLRUCOUNTER 
PGSTAT_MTYPE_RESETREPLSLOTCOUNTER 
PGSTAT_MTYPE_AUTOVAC_START 
PGSTAT_MTYPE_VACUUM 
PGSTAT_MTYPE_ANALYZE 
PGSTAT_MTYPE_ARCHIVER 
PGSTAT_MTYPE_BGWRITER 
PGSTAT_MTYPE_CHECKPOINTER 
PGSTAT_MTYPE_WAL 
PGSTAT_MTYPE_SLRU 
PGSTAT_MTYPE_FUNCSTAT 
PGSTAT_MTYPE_FUNCPURGE 
PGSTAT_MTYPE_RECOVERYCONFLICT 
PGSTAT_MTYPE_TEMPFILE 
PGSTAT_MTYPE_DEADLOCK 
PGSTAT_MTYPE_CHECKSUMFAILURE 
PGSTAT_MTYPE_REPLSLOT 
PGSTAT_MTYPE_CONNECT 
PGSTAT_MTYPE_DISCONNECT 

Definition at line 57 of file pgstat.h.

58 {
86 } StatMsgType;
StatMsgType
Definition: pgstat.h:57

◆ TrackFunctionsLevel

Enumerator
TRACK_FUNC_OFF 
TRACK_FUNC_PL 
TRACK_FUNC_ALL 

Definition at line 36 of file pgstat.h.

Function Documentation

◆ allow_immediate_pgstat_restart()

void allow_immediate_pgstat_restart ( void  )

Definition at line 832 of file pgstat.c.

References last_pgstat_start_time.

Referenced by HandleChildCrash().

833 {
835 }
static time_t last_pgstat_start_time
Definition: pgstat.c:177

◆ AtEOSubXact_PgStat()

void AtEOSubXact_PgStat ( bool  isCommit,
int  nestDepth 
)

Definition at line 2576 of file pgstat.c.

References AtEOSubXact_PgStat_Relations(), PgStat_SubXactStatus::nest_level, pfree(), pgStatXactStack, and PgStat_SubXactStatus::prev.

Referenced by AbortSubTransaction(), and CommitSubTransaction().

2577 {
2578  PgStat_SubXactStatus *xact_state;
2579 
2580  /* merge the sub-transaction's transactional stats into the parent */
2581  xact_state = pgStatXactStack;
2582  if (xact_state != NULL &&
2583  xact_state->nest_level >= nestDepth)
2584  {
2585  /* delink xact_state from stack immediately to simplify reuse case */
2586  pgStatXactStack = xact_state->prev;
2587 
2588  AtEOSubXact_PgStat_Relations(xact_state, isCommit, nestDepth);
2589 
2590  pfree(xact_state);
2591  }
2592 }
void pfree(void *pointer)
Definition: mcxt.c:1169
struct PgStat_SubXactStatus * prev
Definition: pgstat.c:239
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:243
static void AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, int nestDepth)
Definition: pgstat.c:2493

◆ AtEOXact_PgStat()

void AtEOXact_PgStat ( bool  isCommit,
bool  parallel 
)

Definition at line 2464 of file pgstat.c.

References Assert, AtEOXact_PgStat_Database(), AtEOXact_PgStat_Relations(), PgStat_SubXactStatus::nest_level, pgstat_clear_snapshot(), pgStatXactStack, and PgStat_SubXactStatus::prev.

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

2465 {
2466  PgStat_SubXactStatus *xact_state;
2467 
2468  AtEOXact_PgStat_Database(isCommit, parallel);
2469 
2470  /* handle transactional stats information */
2471  xact_state = pgStatXactStack;
2472  if (xact_state != NULL)
2473  {
2474  Assert(xact_state->nest_level == 1);
2475  Assert(xact_state->prev == NULL);
2476 
2477  AtEOXact_PgStat_Relations(xact_state, isCommit);
2478  }
2479  pgStatXactStack = NULL;
2480 
2481  /* Make sure any stats snapshot is thrown away */
2483 }
static void AtEOXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit)
Definition: pgstat.c:2390
static void AtEOXact_PgStat_Database(bool isCommit, bool parallel)
Definition: pgstat.c:2441
struct PgStat_SubXactStatus * prev
Definition: pgstat.c:239
#define Assert(condition)
Definition: c.h:804
void pgstat_clear_snapshot(void)
Definition: pgstat.c:4867
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:243

◆ AtPrepare_PgStat()

void AtPrepare_PgStat ( void  )

Definition at line 2633 of file pgstat.c.

References Assert, AtPrepare_PgStat_Relations(), PgStat_SubXactStatus::nest_level, pgStatXactStack, and PgStat_SubXactStatus::prev.

Referenced by PrepareTransaction().

2634 {
2635  PgStat_SubXactStatus *xact_state;
2636 
2637  xact_state = pgStatXactStack;
2638  if (xact_state != NULL)
2639  {
2640  Assert(xact_state->nest_level == 1);
2641  Assert(xact_state->prev == NULL);
2642 
2643  AtPrepare_PgStat_Relations(xact_state);
2644  }
2645 }
struct PgStat_SubXactStatus * prev
Definition: pgstat.c:239
#define Assert(condition)
Definition: c.h:804
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:243
static void AtPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
Definition: pgstat.c:2599

◆ find_funcstat_entry()

PgStat_BackendFunctionEntry* find_funcstat_entry ( Oid  func_id)

Definition at line 1965 of file pgstat.c.

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

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

1966 {
1968 
1969  if (pgStatFunctions == NULL)
1970  return NULL;
1971 
1973  (void *) &func_id,
1974  HASH_FIND, NULL);
1975 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
static HTAB * pgStatFunctions
Definition: pgstat.c:221
static void pgstat_assert_is_up(void)
Definition: pgstat.c:4850

◆ find_tabstat_entry()

PgStat_TableStatus* find_tabstat_entry ( Oid  rel_id)

Definition at line 2171 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().

2172 {
2173  TabStatHashEntry *hash_entry;
2174 
2175  /* If hashtable doesn't exist, there are no entries at all */
2176  if (!pgStatTabHash)
2177  return NULL;
2178 
2179  hash_entry = hash_search(pgStatTabHash, &rel_id, HASH_FIND, NULL);
2180  if (!hash_entry)
2181  return NULL;
2182 
2183  /* Note that this step could also return NULL, but that's correct */
2184  return hash_entry->tsa_entry;
2185 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
static HTAB * pgStatTabHash
Definition: pgstat.c:215
PgStat_TableStatus * tsa_entry
Definition: pgstat.c:209

◆ pgstat_clear_snapshot()

void pgstat_clear_snapshot ( void  )

Definition at line 4867 of file pgstat.c.

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

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

4868 {
4870 
4871  /* Release memory, if any was allocated */
4872  if (pgStatLocalContext)
4874 
4875  /* Reset variables */
4876  pgStatLocalContext = NULL;
4877  pgStatDBHash = NULL;
4878  replSlotStatHash = NULL;
4879 
4880  /*
4881  * Historically the backend_status.c facilities lived in this file, and
4882  * were reset with the same function. For now keep it that way, and
4883  * forward the reset request.
4884  */
4886 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
static HTAB * pgStatDBHash
Definition: pgstat.c:273
static HTAB * replSlotStatHash
Definition: pgstat.c:284
static void pgstat_assert_is_up(void)
Definition: pgstat.c:4850
static MemoryContext pgStatLocalContext
Definition: pgstat.c:272
void pgstat_clear_backend_activity_snapshot(void)

◆ pgstat_count_heap_delete()

void pgstat_count_heap_delete ( Relation  rel)

Definition at line 2286 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().

2287 {
2288  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2289 
2290  if (pgstat_info != NULL)
2291  {
2292  /* We have to log the effect at the proper transactional level */
2293  int nest_level = GetCurrentTransactionNestLevel();
2294 
2295  if (pgstat_info->trans == NULL ||
2296  pgstat_info->trans->nest_level != nest_level)
2297  add_tabstat_xact_level(pgstat_info, nest_level);
2298 
2299  pgstat_info->trans->tuples_deleted++;
2300  }
2301 }
PgStat_Counter tuples_deleted
Definition: pgstat.h:188
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:857
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:249
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
Definition: pgstat.c:2213
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 2240 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().

2241 {
2242  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2243 
2244  if (pgstat_info != NULL)
2245  {
2246  /* We have to log the effect at the proper transactional level */
2247  int nest_level = GetCurrentTransactionNestLevel();
2248 
2249  if (pgstat_info->trans == NULL ||
2250  pgstat_info->trans->nest_level != nest_level)
2251  add_tabstat_xact_level(pgstat_info, nest_level);
2252 
2253  pgstat_info->trans->tuples_inserted += n;
2254  }
2255 }
PgStat_Counter tuples_inserted
Definition: pgstat.h:186
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:857
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:249
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
Definition: pgstat.c:2213
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:176

◆ pgstat_count_heap_update()

void pgstat_count_heap_update ( Relation  rel,
bool  hot 
)

Definition at line 2261 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().

2262 {
2263  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2264 
2265  if (pgstat_info != NULL)
2266  {
2267  /* We have to log the effect at the proper transactional level */
2268  int nest_level = GetCurrentTransactionNestLevel();
2269 
2270  if (pgstat_info->trans == NULL ||
2271  pgstat_info->trans->nest_level != nest_level)
2272  add_tabstat_xact_level(pgstat_info, nest_level);
2273 
2274  pgstat_info->trans->tuples_updated++;
2275 
2276  /* t_tuples_hot_updated is nontransactional, so just advance it */
2277  if (hot)
2278  pgstat_info->t_counts.t_tuples_hot_updated++;
2279  }
2280 }
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:249
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
Definition: pgstat.c:2213
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:176

◆ pgstat_count_slru_flush()

void pgstat_count_slru_flush ( int  slru_idx)

Definition at line 6032 of file pgstat.c.

References PgStat_MsgSLRU::m_flush, and slru_entry().

Referenced by SimpleLruWriteAll().

6033 {
6034  slru_entry(slru_idx)->m_flush += 1;
6035 }
PgStat_Counter m_flush
Definition: pgstat.h:514
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5982

◆ pgstat_count_slru_page_exists()

void pgstat_count_slru_page_exists ( int  slru_idx)

Definition at line 6014 of file pgstat.c.

References PgStat_MsgSLRU::m_blocks_exists, and slru_entry().

Referenced by SimpleLruDoesPhysicalPageExist().

6015 {
6016  slru_entry(slru_idx)->m_blocks_exists += 1;
6017 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5982
PgStat_Counter m_blocks_exists
Definition: pgstat.h:513

◆ pgstat_count_slru_page_hit()

void pgstat_count_slru_page_hit ( int  slru_idx)

Definition at line 6008 of file pgstat.c.

References PgStat_MsgSLRU::m_blocks_hit, and slru_entry().

Referenced by SimpleLruReadPage(), and SimpleLruReadPage_ReadOnly().

6009 {
6010  slru_entry(slru_idx)->m_blocks_hit += 1;
6011 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5982
PgStat_Counter m_blocks_hit
Definition: pgstat.h:510

◆ pgstat_count_slru_page_read()

void pgstat_count_slru_page_read ( int  slru_idx)

Definition at line 6020 of file pgstat.c.

References PgStat_MsgSLRU::m_blocks_read, and slru_entry().

Referenced by SimpleLruReadPage().

6021 {
6022  slru_entry(slru_idx)->m_blocks_read += 1;
6023 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5982
PgStat_Counter m_blocks_read
Definition: pgstat.h:511

◆ pgstat_count_slru_page_written()

void pgstat_count_slru_page_written ( int  slru_idx)

Definition at line 6026 of file pgstat.c.

References PgStat_MsgSLRU::m_blocks_written, and slru_entry().

Referenced by SlruPhysicalWritePage().

6027 {
6028  slru_entry(slru_idx)->m_blocks_written += 1;
6029 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5982
PgStat_Counter m_blocks_written
Definition: pgstat.h:512

◆ pgstat_count_slru_page_zeroed()

void pgstat_count_slru_page_zeroed ( int  slru_idx)

Definition at line 6002 of file pgstat.c.

References PgStat_MsgSLRU::m_blocks_zeroed, and slru_entry().

Referenced by SimpleLruZeroPage().

6003 {
6004  slru_entry(slru_idx)->m_blocks_zeroed += 1;
6005 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5982
PgStat_Counter m_blocks_zeroed
Definition: pgstat.h:509

◆ pgstat_count_slru_truncate()

void pgstat_count_slru_truncate ( int  slru_idx)

Definition at line 6038 of file pgstat.c.

References PgStat_MsgSLRU::m_truncate, and slru_entry().

Referenced by SimpleLruTruncate().

6039 {
6040  slru_entry(slru_idx)->m_truncate += 1;
6041 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5982
PgStat_Counter m_truncate
Definition: pgstat.h:515

◆ pgstat_count_truncate()

void pgstat_count_truncate ( Relation  rel)

Definition at line 2344 of file pgstat.c.

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

Referenced by ExecRefreshMatView(), and ExecuteTruncateGuts().

2345 {
2346  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2347 
2348  if (pgstat_info != NULL)
2349  {
2350  /* We have to log the effect at the proper transactional level */
2351  int nest_level = GetCurrentTransactionNestLevel();
2352 
2353  if (pgstat_info->trans == NULL ||
2354  pgstat_info->trans->nest_level != nest_level)
2355  add_tabstat_xact_level(pgstat_info, nest_level);
2356 
2357  pgstat_truncdrop_save_counters(pgstat_info->trans, false);
2358  pgstat_info->trans->tuples_inserted = 0;
2359  pgstat_info->trans->tuples_updated = 0;
2360  pgstat_info->trans->tuples_deleted = 0;
2361  }
2362 }
PgStat_Counter tuples_updated
Definition: pgstat.h:187
static void pgstat_truncdrop_save_counters(PgStat_TableXactStatus *trans, bool is_drop)
Definition: pgstat.c:2315
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:249
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
Definition: pgstat.c:2213
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:176

◆ pgstat_drop_database()

void pgstat_drop_database ( Oid  databaseid)

Definition at line 1367 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().

1368 {
1369  PgStat_MsgDropdb msg;
1370 
1372  return;
1373 
1375  msg.m_databaseid = databaseid;
1376  pgstat_send(&msg, sizeof(msg));
1377 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:173
Oid m_databaseid
Definition: pgstat.h:330
PgStat_MsgHdr m_hdr
Definition: pgstat.h:329
#define PGINVALID_SOCKET
Definition: port.h:33
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058

◆ pgstat_end_function_usage()

void pgstat_end_function_usage ( PgStat_FunctionCallUsage fcu,
bool  finalize 
)

Definition at line 1987 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().

1988 {
1989  PgStat_FunctionCounts *fs = fcu->fs;
1990  instr_time f_total;
1991  instr_time f_others;
1992  instr_time f_self;
1993 
1994  /* stats not wanted? */
1995  if (fs == NULL)
1996  return;
1997 
1998  /* total elapsed time in this function call */
1999  INSTR_TIME_SET_CURRENT(f_total);
2000  INSTR_TIME_SUBTRACT(f_total, fcu->f_start);
2001 
2002  /* self usage: elapsed minus anything already charged to other calls */
2003  f_others = total_func_time;
2004  INSTR_TIME_SUBTRACT(f_others, fcu->save_total);
2005  f_self = f_total;
2006  INSTR_TIME_SUBTRACT(f_self, f_others);
2007 
2008  /* update backend-wide total time */
2010 
2011  /*
2012  * Compute the new f_total_time as the total elapsed time added to the
2013  * pre-call value of f_total_time. This is necessary to avoid
2014  * double-counting any time taken by recursive calls of myself. (We do
2015  * not need any similar kluge for self time, since that already excludes
2016  * any recursive calls.)
2017  */
2018  INSTR_TIME_ADD(f_total, fcu->save_f_total_time);
2019 
2020  /* update counters in function stats table */
2021  if (finalize)
2022  fs->f_numcalls++;
2023  fs->f_total_time = f_total;
2024  INSTR_TIME_ADD(fs->f_self_time, f_self);
2025 
2026  /* indicate that we have something to send */
2027  have_function_stats = true;
2028 }
instr_time f_self_time
Definition: pgstat.h:578
instr_time f_start
Definition: pgstat.h:946
instr_time save_total
Definition: pgstat.h:944
struct timeval instr_time
Definition: instr_time.h:150
instr_time f_total_time
Definition: pgstat.h:577
PgStat_FunctionCounts * fs
Definition: pgstat.h:940
PgStat_Counter f_numcalls
Definition: pgstat.h:576
#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:298
static bool have_function_stats
Definition: pgstat.c:227
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
instr_time save_f_total_time
Definition: pgstat.h:942

◆ pgstat_fetch_global()

PgStat_GlobalStats* pgstat_fetch_global ( void  )

Definition at line 2935 of file pgstat.c.

References backend_read_statsfile(), and globalStats.

Referenced by pg_stat_get_snapshot_timestamp().

2936 {
2938 
2939  return &globalStats;
2940 }
static PgStat_GlobalStats globalStats
Definition: pgstat.c:281
static void backend_read_statsfile(void)
Definition: pgstat.c:4699

◆ pgstat_fetch_replslot()

PgStat_StatReplSlotEntry* pgstat_fetch_replslot ( NameData  slotname)

Definition at line 2983 of file pgstat.c.

References backend_read_statsfile(), and pgstat_get_replslot_entry().

Referenced by pg_stat_get_replication_slot().

2984 {
2986 
2987  return pgstat_get_replslot_entry(slotname, false);
2988 }
static void backend_read_statsfile(void)
Definition: pgstat.c:4699
static PgStat_StatReplSlotEntry * pgstat_get_replslot_entry(NameData name, bool create_it)
Definition: pgstat.c:5870

◆ pgstat_fetch_slru()

PgStat_SLRUStats* pgstat_fetch_slru ( void  )

Definition at line 2967 of file pgstat.c.

References backend_read_statsfile(), and slruStats.

Referenced by pg_stat_get_slru().

2968 {
2970 
2971  return slruStats;
2972 }
static void backend_read_statsfile(void)
Definition: pgstat.c:4699
static PgStat_SLRUStats slruStats[SLRU_NUM_ELEMENTS]
Definition: pgstat.c:283

◆ pgstat_fetch_stat_archiver()

PgStat_ArchiverStats* pgstat_fetch_stat_archiver ( void  )

Definition at line 2887 of file pgstat.c.

References archiverStats, and backend_read_statsfile().

Referenced by pg_stat_get_archiver().

2888 {
2890 
2891  return &archiverStats;
2892 }
static void backend_read_statsfile(void)
Definition: pgstat.c:4699
static PgStat_ArchiverStats archiverStats
Definition: pgstat.c:280

◆ pgstat_fetch_stat_bgwriter()

PgStat_BgWriterStats* pgstat_fetch_stat_bgwriter ( void  )

Definition at line 2903 of file pgstat.c.

References backend_read_statsfile(), and PgStat_GlobalStats::bgwriter.

Referenced by pg_stat_get_bgwriter_buf_written_clean(), pg_stat_get_bgwriter_maxwritten_clean(), pg_stat_get_bgwriter_stat_reset_time(), and pg_stat_get_buf_alloc().

2904 {
2906 
2907  return &globalStats.bgwriter;
2908 }
static PgStat_GlobalStats globalStats
Definition: pgstat.c:281
PgStat_BgWriterStats bgwriter
Definition: pgstat.h:881
static void backend_read_statsfile(void)
Definition: pgstat.c:4699

◆ pgstat_fetch_stat_checkpointer()

◆ pgstat_fetch_stat_dbentry()

PgStat_StatDBEntry* pgstat_fetch_stat_dbentry ( Oid  dbid)

Definition at line 2774 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().

2775 {
2776  /*
2777  * If not done for this transaction, read the statistics collector stats
2778  * file into some hash tables.
2779  */
2781 
2782  /*
2783  * Lookup the requested database; return NULL if not found
2784  */
2786  (void *) &dbid,
2787  HASH_FIND, NULL);
2788 }
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:4699
static HTAB * pgStatDBHash
Definition: pgstat.c:273

◆ pgstat_fetch_stat_funcentry()

PgStat_StatFuncEntry* pgstat_fetch_stat_funcentry ( Oid  funcid)

Definition at line 2857 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().

2858 {
2859  PgStat_StatDBEntry *dbentry;
2860  PgStat_StatFuncEntry *funcentry = NULL;
2861 
2862  /* load the stats file if needed */
2864 
2865  /* Lookup our database, then find the requested function. */
2867  if (dbentry != NULL && dbentry->functions != NULL)
2868  {
2869  funcentry = (PgStat_StatFuncEntry *) hash_search(dbentry->functions,
2870  (void *) &func_id,
2871  HASH_FIND, NULL);
2872  }
2873 
2874  return funcentry;
2875 }
HTAB * functions
Definition: pgstat.h:775
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2774
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:4699
Oid MyDatabaseId
Definition: globals.c:88

◆ pgstat_fetch_stat_tabentry()

PgStat_StatTabEntry* pgstat_fetch_stat_tabentry ( Oid  relid)

Definition at line 2801 of file pgstat.c.

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

Referenced by 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().

2802 {
2803  Oid dbid;
2804  PgStat_StatDBEntry *dbentry;
2805  PgStat_StatTabEntry *tabentry;
2806 
2807  /*
2808  * If not done for this transaction, read the statistics collector stats
2809  * file into some hash tables.
2810  */
2812 
2813  /*
2814  * Lookup our database, then look in its table hash table.
2815  */
2816  dbid = MyDatabaseId;
2818  (void *) &dbid,
2819  HASH_FIND, NULL);
2820  if (dbentry != NULL && dbentry->tables != NULL)
2821  {
2822  tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2823  (void *) &relid,
2824  HASH_FIND, NULL);
2825  if (tabentry)
2826  return tabentry;
2827  }
2828 
2829  /*
2830  * If we didn't find it, maybe it's a shared table.
2831  */
2832  dbid = InvalidOid;
2834  (void *) &dbid,
2835  HASH_FIND, NULL);
2836  if (dbentry != NULL && dbentry->tables != NULL)
2837  {
2838  tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2839  (void *) &relid,
2840  HASH_FIND, NULL);
2841  if (tabentry)
2842  return tabentry;
2843  }
2844 
2845  return NULL;
2846 }
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:4699
Oid MyDatabaseId
Definition: globals.c:88
#define InvalidOid
Definition: postgres_ext.h:36
static HTAB * pgStatDBHash
Definition: pgstat.c:273

◆ pgstat_fetch_stat_wal()

PgStat_WalStats* pgstat_fetch_stat_wal ( void  )

Definition at line 2951 of file pgstat.c.

References backend_read_statsfile(), and walStats.

Referenced by pg_stat_get_wal().

2952 {
2954 
2955  return &walStats;
2956 }
static PgStat_WalStats walStats
Definition: pgstat.c:282
static void backend_read_statsfile(void)
Definition: pgstat.c:4699

◆ pgstat_init()

void pgstat_init ( void  )

Definition at line 392 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().

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

◆ pgstat_initialize()

void pgstat_initialize ( void  )

Definition at line 3026 of file pgstat.c.

References Assert, before_shmem_exit(), pgstat_shutdown_hook(), and pgWalUsage.

Referenced by BaseInit().

3027 {
3028  Assert(!pgstat_is_initialized);
3029 
3030  /*
3031  * Initialize prevWalUsage with pgWalUsage so that pgstat_send_wal() can
3032  * calculate how much pgWalUsage counters are increased by subtracting
3033  * prevWalUsage from pgWalUsage.
3034  */
3036 
3037  /* Set up a process-exit hook to clean up */
3039 
3040 #ifdef USE_ASSERT_CHECKING
3041  pgstat_is_initialized = true;
3042 #endif
3043 }
static void pgstat_shutdown_hook(int code, Datum arg)
Definition: pgstat.c:2998
WalUsage pgWalUsage
Definition: instrument.c:22
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:333
#define Assert(condition)
Definition: c.h:804
static WalUsage prevWalUsage
Definition: pgstat.c:141

◆ pgstat_initstats()

void pgstat_initstats ( Relation  rel)

Definition at line 2044 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().

2045 {
2046  Oid rel_id = rel->rd_id;
2047  char relkind = rel->rd_rel->relkind;
2048 
2049  /*
2050  * We only count stats for relations with storage and partitioned tables
2051  */
2052  if (!RELKIND_HAS_STORAGE(relkind) && relkind != RELKIND_PARTITIONED_TABLE)
2053  {
2054  rel->pgstat_info = NULL;
2055  return;
2056  }
2057 
2059  {
2060  /* We're not counting at all */
2061  rel->pgstat_info = NULL;
2062  return;
2063  }
2064 
2065  /*
2066  * If we already set up this relation in the current transaction, nothing
2067  * to do.
2068  */
2069  if (rel->pgstat_info != NULL &&
2070  rel->pgstat_info->t_id == rel_id)
2071  return;
2072 
2073  /* Else find or make the PgStat_TableStatus entry, and update link */
2074  rel->pgstat_info = get_tabstat_entry(rel_id, rel->rd_rel->relisshared);
2075 }
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:173
Form_pg_class rd_rel
Definition: rel.h:109
unsigned int Oid
Definition: postgres_ext.h:31
bool pgstat_track_counts
Definition: pgstat.c:115
Oid rd_id
Definition: rel.h:111
static PgStat_TableStatus * get_tabstat_entry(Oid rel_id, bool isshared)
Definition: pgstat.c:2081
#define PGINVALID_SOCKET
Definition: port.h:33
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:249

◆ pgstat_ping()

void pgstat_ping ( void  )

Definition at line 1879 of file pgstat.c.

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

1880 {
1881  PgStat_MsgDummy msg;
1882 
1884  return;
1885 
1887  pgstat_send(&msg, sizeof(msg));
1888 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:173
#define PGINVALID_SOCKET
Definition: port.h:33
PgStat_MsgHdr m_hdr
Definition: pgstat.h:237
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058

◆ pgstat_report_analyze()

void pgstat_report_analyze ( Relation  rel,
PgStat_Counter  livetuples,
PgStat_Counter  deadtuples,
bool  resetcounter 
)

Definition at line 1606 of file pgstat.c.

References GetCurrentTimestamp(), InvalidOid, IsAutoVacuumWorkerProcess(), PgStat_MsgAnalyze::m_analyzetime, PgStat_MsgAnalyze::m_autovacuum, PgStat_MsgAnalyze::m_databaseid, PgStat_MsgAnalyze::m_dead_tuples, PgStat_MsgAnalyze::m_hdr, PgStat_MsgAnalyze::m_live_tuples, PgStat_MsgAnalyze::m_resetcounter, PgStat_MsgAnalyze::m_tableoid, Max, MyDatabaseId, PGINVALID_SOCKET, RelationData::pgstat_info, PGSTAT_MTYPE_ANALYZE, pgstat_send(), pgstat_setheader(), pgstat_track_counts, pgStatSock, RelationData::rd_rel, RelationGetRelid, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_delta_dead_tuples, PgStat_TableStatus::trans, trans, PgStat_TableXactStatus::tuples_deleted, PgStat_TableXactStatus::tuples_inserted, PgStat_TableXactStatus::tuples_updated, and PgStat_TableXactStatus::upper.

Referenced by do_analyze_rel().

1609 {
1610  PgStat_MsgAnalyze msg;
1611 
1613  return;
1614 
1615  /*
1616  * Unlike VACUUM, ANALYZE might be running inside a transaction that has
1617  * already inserted and/or deleted rows in the target table. ANALYZE will
1618  * have counted such rows as live or dead respectively. Because we will
1619  * report our counts of such rows at transaction end, we should subtract
1620  * off these counts from what we send to the collector now, else they'll
1621  * be double-counted after commit. (This approach also ensures that the
1622  * collector ends up with the right numbers if we abort instead of
1623  * committing.)
1624  *
1625  * Waste no time on partitioned tables, though.
1626  */
1627  if (rel->pgstat_info != NULL &&
1628  rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
1629  {
1631 
1632  for (trans = rel->pgstat_info->trans; trans; trans = trans->upper)
1633  {
1634  livetuples -= trans->tuples_inserted - trans->tuples_deleted;
1635  deadtuples -= trans->tuples_updated + trans->tuples_deleted;
1636  }
1637  /* count stuff inserted by already-aborted subxacts, too */
1638  deadtuples -= rel->pgstat_info->t_counts.t_delta_dead_tuples;
1639  /* Since ANALYZE's counts are estimates, we could have underflowed */
1640  livetuples = Max(livetuples, 0);
1641  deadtuples = Max(deadtuples, 0);
1642  }
1643 
1645  msg.m_databaseid = rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId;
1646  msg.m_tableoid = RelationGetRelid(rel);
1648  msg.m_resetcounter = resetcounter;
1650  msg.m_live_tuples = livetuples;
1651  msg.m_dead_tuples = deadtuples;
1652  pgstat_send(&msg, sizeof(msg));
1653 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
PgStat_Counter tuples_updated
Definition: pgstat.h:187
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:173
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
TimestampTz m_analyzetime
Definition: pgstat.h:434
PgStat_TableCounts t_counts
Definition: pgstat.h:177
Form_pg_class rd_rel
Definition: rel.h:109
bool m_resetcounter
Definition: pgstat.h:433
bool pgstat_track_counts
Definition: pgstat.c:115
PgStat_Counter tuples_inserted
Definition: pgstat.h:186
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:129
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3406
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:197
PgStat_Counter m_live_tuples
Definition: pgstat.h:435
bool m_autovacuum
Definition: pgstat.h:432
PgStat_Counter tuples_deleted
Definition: pgstat.h:188
#define PGINVALID_SOCKET
Definition: port.h:33
Oid MyDatabaseId
Definition: globals.c:88
#define InvalidOid
Definition: postgres_ext.h:36
#define Max(x, y)
Definition: c.h:980
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:249
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:176
PgStat_MsgHdr m_hdr
Definition: pgstat.h:429
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:400
PgStat_Counter m_dead_tuples
Definition: pgstat.h:436
#define RelationGetRelid(relation)
Definition: rel.h:477

◆ pgstat_report_autovac()

void pgstat_report_autovac ( Oid  dboid)

Definition at line 1556 of file pgstat.c.

References GetCurrentTimestamp(), PgStat_MsgAutovacStart::m_databaseid, PgStat_MsgAutovacStart::m_hdr, PgStat_MsgAutovacStart::m_start_time, PGINVALID_SOCKET, PGSTAT_MTYPE_AUTOVAC_START, pgstat_send(), pgstat_setheader(), and pgStatSock.

Referenced by AutoVacWorkerMain().

1557 {
1559 
1561  return;
1562 
1564  msg.m_databaseid = dboid;
1566 
1567  pgstat_send(&msg, sizeof(msg));
1568 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:173
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
PgStat_MsgHdr m_hdr
Definition: pgstat.h:399
#define PGINVALID_SOCKET
Definition: port.h:33
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058
TimestampTz m_start_time
Definition: pgstat.h:401

◆ pgstat_report_checksum_failure()

void pgstat_report_checksum_failure ( void  )

Definition at line 1725 of file pgstat.c.

References MyDatabaseId, and pgstat_report_checksum_failures_in_db().

Referenced by PageIsVerifiedExtended().

1726 {
1728 }
void pgstat_report_checksum_failures_in_db(Oid dboid, int failurecount)
Definition: pgstat.c:1703
Oid MyDatabaseId
Definition: globals.c:88

◆ pgstat_report_checksum_failures_in_db()

void pgstat_report_checksum_failures_in_db ( Oid  dboid,
int  failurecount 
)

Definition at line 1703 of file pgstat.c.

References GetCurrentTimestamp(), PgStat_MsgChecksumFailure::m_databaseid, PgStat_MsgChecksumFailure::m_failure_time, PgStat_MsgChecksumFailure::m_failurecount, PgStat_MsgChecksumFailure::m_hdr, PGINVALID_SOCKET, PGSTAT_MTYPE_CHECKSUMFAILURE, pgstat_send(), pgstat_setheader(), pgstat_track_counts, and pgStatSock.

Referenced by pgstat_report_checksum_failure(), and sendFile().

1704 {
1706 
1708  return;
1709 
1711  msg.m_databaseid = dboid;
1712  msg.m_failurecount = failurecount;
1714 
1715  pgstat_send(&msg, sizeof(msg));
1716 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:173
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
PgStat_MsgHdr m_hdr
Definition: pgstat.h:655
bool pgstat_track_counts
Definition: pgstat.c:115
TimestampTz m_failure_time
Definition: pgstat.h:658
#define PGINVALID_SOCKET
Definition: port.h:33
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058

◆ pgstat_report_connect()

void pgstat_report_connect ( Oid  dboid)

Definition at line 1757 of file pgstat.c.

References PgStat_MsgConnect::m_databaseid, PgStat_MsgConnect::m_hdr, MyDatabaseId, MyStartTimestamp, pgLastSessionReportTime, PGSTAT_MTYPE_CONNECT, pgstat_send(), pgstat_setheader(), and pgstat_should_report_connstat().

Referenced by PostgresMain().

1758 {
1759  PgStat_MsgConnect msg;
1760 
1762  return;
1763 
1765 
1767  msg.m_databaseid = MyDatabaseId;
1768  pgstat_send(&msg, sizeof(PgStat_MsgConnect));
1769 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
PgStat_MsgHdr m_hdr
Definition: pgstat.h:668
static PgStat_Counter pgLastSessionReportTime
Definition: pgstat.c:249
static bool pgstat_should_report_connstat(void)
Definition: pgstat.c:1802
TimestampTz MyStartTimestamp
Definition: globals.c:45
Oid MyDatabaseId
Definition: globals.c:88
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058

◆ pgstat_report_deadlock()

void pgstat_report_deadlock ( void  )

Definition at line 1682 of file pgstat.c.

References PgStat_MsgDeadlock::m_databaseid, PgStat_MsgDeadlock::m_hdr, MyDatabaseId, PGINVALID_SOCKET, PGSTAT_MTYPE_DEADLOCK, pgstat_send(), pgstat_setheader(), pgstat_track_counts, and pgStatSock.

Referenced by DeadLockReport().

1683 {
1684  PgStat_MsgDeadlock msg;
1685 
1687  return;
1688 
1690  msg.m_databaseid = MyDatabaseId;
1691  pgstat_send(&msg, sizeof(msg));
1692 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:173
bool pgstat_track_counts
Definition: pgstat.c:115
PgStat_MsgHdr m_hdr
Definition: pgstat.h:644
#define PGINVALID_SOCKET
Definition: port.h:33
Oid MyDatabaseId
Definition: globals.c:88
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058

◆ pgstat_report_recovery_conflict()

void pgstat_report_recovery_conflict ( int  reason)

Definition at line 1662 of file pgstat.c.

References PgStat_MsgRecoveryConflict::m_databaseid, PgStat_MsgRecoveryConflict::m_hdr, PgStat_MsgRecoveryConflict::m_reason, MyDatabaseId, PGINVALID_SOCKET, PGSTAT_MTYPE_RECOVERYCONFLICT, pgstat_send(), pgstat_setheader(), pgstat_track_counts, and pgStatSock.

Referenced by ProcessInterrupts().

1663 {
1665 
1667  return;
1668 
1670  msg.m_databaseid = MyDatabaseId;
1671  msg.m_reason = reason;
1672  pgstat_send(&msg, sizeof(msg));
1673 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:173
bool pgstat_track_counts
Definition: pgstat.c:115
PgStat_MsgHdr m_hdr
Definition: pgstat.h:546
#define PGINVALID_SOCKET
Definition: port.h:33
Oid MyDatabaseId
Definition: globals.c:88
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058

◆ pgstat_report_replslot()

void pgstat_report_replslot ( const PgStat_StatReplSlotEntry repSlotStat)

Definition at line 1814 of file pgstat.c.

References PgStat_MsgReplSlot::m_create, PgStat_MsgReplSlot::m_drop, PgStat_MsgReplSlot::m_hdr, 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, namestrcpy(), PGSTAT_MTYPE_REPLSLOT, pgstat_send(), pgstat_setheader(), PgStat_StatReplSlotEntry::slotname, PgStat_StatReplSlotEntry::spill_bytes, PgStat_StatReplSlotEntry::spill_count, PgStat_StatReplSlotEntry::spill_txns, PgStat_StatReplSlotEntry::stream_bytes, PgStat_StatReplSlotEntry::stream_count, PgStat_StatReplSlotEntry::stream_txns, PgStat_StatReplSlotEntry::total_bytes, and PgStat_StatReplSlotEntry::total_txns.

Referenced by UpdateDecodingStats().

1815 {
1816  PgStat_MsgReplSlot msg;
1817 
1818  /*
1819  * Prepare and send the message
1820  */
1822  namestrcpy(&msg.m_slotname, NameStr(repSlotStat->slotname));
1823  msg.m_create = false;
1824  msg.m_drop = false;
1825  msg.m_spill_txns = repSlotStat->spill_txns;
1826  msg.m_spill_count = repSlotStat->spill_count;
1827  msg.m_spill_bytes = repSlotStat->spill_bytes;
1828  msg.m_stream_txns = repSlotStat->stream_txns;
1829  msg.m_stream_count = repSlotStat->stream_count;
1830  msg.m_stream_bytes = repSlotStat->stream_bytes;
1831  msg.m_total_txns = repSlotStat->total_txns;
1832  msg.m_total_bytes = repSlotStat->total_bytes;
1833  pgstat_send(&msg, sizeof(PgStat_MsgReplSlot));
1834 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
PgStat_Counter spill_bytes
Definition: pgstat.h:923
void namestrcpy(Name name, const char *str)
Definition: name.c:233
PgStat_Counter m_spill_bytes
Definition: pgstat.h:531
PgStat_Counter m_stream_bytes
Definition: pgstat.h:534
PgStat_Counter m_spill_txns
Definition: pgstat.h:529
PgStat_Counter total_bytes
Definition: pgstat.h:928
PgStat_Counter stream_bytes
Definition: pgstat.h:926
PgStat_Counter spill_txns
Definition: pgstat.h:921
PgStat_Counter m_stream_txns
Definition: pgstat.h:532
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058
PgStat_Counter m_total_txns
Definition: pgstat.h:535
PgStat_Counter m_spill_count
Definition: pgstat.h:530
PgStat_Counter m_total_bytes
Definition: pgstat.h:536
PgStat_Counter spill_count
Definition: pgstat.h:922
PgStat_Counter stream_count
Definition: pgstat.h:925
#define NameStr(name)
Definition: c.h:681
PgStat_MsgHdr m_hdr
Definition: pgstat.h:525
PgStat_Counter total_txns
Definition: pgstat.h:927
PgStat_Counter m_stream_count
Definition: pgstat.h:533
NameData m_slotname
Definition: pgstat.h:526
PgStat_Counter stream_txns
Definition: pgstat.h:924

◆ pgstat_report_replslot_create()

void pgstat_report_replslot_create ( const char *  slotname)

Definition at line 1843 of file pgstat.c.

References PgStat_MsgReplSlot::m_create, PgStat_MsgReplSlot::m_drop, PgStat_MsgReplSlot::m_hdr, PgStat_MsgReplSlot::m_slotname, namestrcpy(), PGSTAT_MTYPE_REPLSLOT, pgstat_send(), and pgstat_setheader().

Referenced by ReplicationSlotCreate().

1844 {
1845  PgStat_MsgReplSlot msg;
1846 
1848  namestrcpy(&msg.m_slotname, slotname);
1849  msg.m_create = true;
1850  msg.m_drop = false;
1851  pgstat_send(&msg, sizeof(PgStat_MsgReplSlot));
1852 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
void namestrcpy(Name name, const char *str)
Definition: name.c:233
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058
PgStat_MsgHdr m_hdr
Definition: pgstat.h:525
NameData m_slotname
Definition: pgstat.h:526

◆ pgstat_report_replslot_drop()

void pgstat_report_replslot_drop ( const char *  slotname)

Definition at line 1861 of file pgstat.c.

References PgStat_MsgReplSlot::m_create, PgStat_MsgReplSlot::m_drop, PgStat_MsgReplSlot::m_hdr, PgStat_MsgReplSlot::m_slotname, namestrcpy(), PGSTAT_MTYPE_REPLSLOT, pgstat_send(), and pgstat_setheader().

Referenced by pgstat_vacuum_stat(), and ReplicationSlotDropPtr().

1862 {
1863  PgStat_MsgReplSlot msg;
1864 
1866  namestrcpy(&msg.m_slotname, slotname);
1867  msg.m_create = false;
1868  msg.m_drop = true;
1869  pgstat_send(&msg, sizeof(PgStat_MsgReplSlot));
1870 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
void namestrcpy(Name name, const char *str)
Definition: name.c:233
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058
PgStat_MsgHdr m_hdr
Definition: pgstat.h:525
NameData m_slotname
Definition: pgstat.h:526

◆ pgstat_report_stat()

void pgstat_report_stat ( bool  force)

Definition at line 858 of file pgstat.c.

References Assert, GetCurrentTransactionStopTimestamp(), hash_destroy(), have_function_stats, i, InvalidOid, PgStat_MsgTabstat::m_databaseid, PgStat_MsgTabstat::m_entry, PgStat_MsgTabstat::m_nentries, PgStat_MsgWal::m_wal_sync, PgStat_MsgWal::m_wal_write, MemSet, MyDatabaseId, now(), pgstat_assert_is_up(), PGSTAT_NUM_TABENTRIES, pgstat_report_disconnect(), pgstat_send_funcstats(), pgstat_send_slru(), pgstat_send_tabstat(), pgstat_send_wal(), PGSTAT_STAT_INTERVAL, pgStatXactCommit, pgStatXactRollback, pgWalUsage, PgStat_TableStatus::t_counts, PgStat_TableEntry::t_counts, PgStat_TableStatus::t_id, PgStat_TableEntry::t_id, PgStat_TableStatus::t_shared, TimestampDifferenceExceeds(), PgStat_TableStatus::trans, TabStatusArray::tsa_entries, TabStatusArray::tsa_next, TabStatusArray::tsa_used, and WalUsage::wal_records.

Referenced by AllTablesyncsReady(), apply_handle_commit_internal(), apply_handle_commit_prepared(), apply_handle_prepare(), apply_handle_rollback_prepared(), apply_handle_stream_prepare(), initialize_worker_spi(), LogicalRepSyncTableStart(), pg_attribute_noreturn(), pgstat_shutdown_hook(), PostgresMain(), and process_syncing_tables_for_apply().

859 {
860  /* we assume this inits to all zeroes: */
861  static const PgStat_TableCounts all_zeroes;
862  static TimestampTz last_report = 0;
863 
865  PgStat_MsgTabstat regular_msg;
866  PgStat_MsgTabstat shared_msg;
867  TabStatusArray *tsa;
868  int i;
869 
871 
872  /*
873  * Don't expend a clock check if nothing to do.
874  *
875  * To determine whether any WAL activity has occurred since last time, not
876  * only the number of generated WAL records but also the numbers of WAL
877  * writes and syncs need to be checked. Because even transaction that
878  * generates no WAL records can write or sync WAL data when flushing the
879  * data pages.
880  */
881  if ((pgStatTabList == NULL || pgStatTabList->tsa_used == 0) &&
882  pgStatXactCommit == 0 && pgStatXactRollback == 0 &&
884  WalStats.m_wal_write == 0 && WalStats.m_wal_sync == 0 &&
885  !have_function_stats && !disconnect)
886  return;
887 
888  /*
889  * Don't send a message unless it's been at least PGSTAT_STAT_INTERVAL
890  * msec since we last sent one, or the backend is about to exit.
891  */
893  if (!disconnect &&
895  return;
896 
897  last_report = now;
898 
899  if (disconnect)
901 
902  /*
903  * Destroy pgStatTabHash before we start invalidating PgStat_TableEntry
904  * entries it points to. (Should we fail partway through the loop below,
905  * it's okay to have removed the hashtable already --- the only
906  * consequence is we'd get multiple entries for the same table in the
907  * pgStatTabList, and that's safe.)
908  */
909  if (pgStatTabHash)
911  pgStatTabHash = NULL;
912 
913  /*
914  * Scan through the TabStatusArray struct(s) to find tables that actually
915  * have counts, and build messages to send. We have to separate shared
916  * relations from regular ones because the databaseid field in the message
917  * header has to depend on that.
918  */
919  regular_msg.m_databaseid = MyDatabaseId;
920  shared_msg.m_databaseid = InvalidOid;
921  regular_msg.m_nentries = 0;
922  shared_msg.m_nentries = 0;
923 
924  for (tsa = pgStatTabList; tsa != NULL; tsa = tsa->tsa_next)
925  {
926  for (i = 0; i < tsa->tsa_used; i++)
927  {
928  PgStat_TableStatus *entry = &tsa->tsa_entries[i];
929  PgStat_MsgTabstat *this_msg;
930  PgStat_TableEntry *this_ent;
931 
932  /* Shouldn't have any pending transaction-dependent counts */
933  Assert(entry->trans == NULL);
934 
935  /*
936  * Ignore entries that didn't accumulate any actual counts, such
937  * as indexes that were opened by the planner but not used.
938  */
939  if (memcmp(&entry->t_counts, &all_zeroes,
940  sizeof(PgStat_TableCounts)) == 0)
941  continue;
942 
943  /*
944  * OK, insert data into the appropriate message, and send if full.
945  */
946  this_msg = entry->t_shared ? &shared_msg : &regular_msg;
947  this_ent = &this_msg->m_entry[this_msg->m_nentries];
948  this_ent->t_id = entry->t_id;
949  memcpy(&this_ent->t_counts, &entry->t_counts,
950  sizeof(PgStat_TableCounts));
951  if (++this_msg->m_nentries >= PGSTAT_NUM_TABENTRIES)
952  {
953  pgstat_send_tabstat(this_msg, now);
954  this_msg->m_nentries = 0;
955  }
956  }
957  /* zero out PgStat_TableStatus structs after use */
958  MemSet(tsa->tsa_entries, 0,
959  tsa->tsa_used * sizeof(PgStat_TableStatus));
960  tsa->tsa_used = 0;
961  }
962 
963  /*
964  * Send partial messages. Make sure that any pending xact commit/abort
965  * and connection stats get counted, even if there are no table stats to
966  * send.
967  */
968  if (regular_msg.m_nentries > 0 ||
969  pgStatXactCommit > 0 || pgStatXactRollback > 0 || disconnect)
970  pgstat_send_tabstat(&regular_msg, now);
971  if (shared_msg.m_nentries > 0)
972  pgstat_send_tabstat(&shared_msg, now);
973 
974  /* Now, send function statistics */
976 
977  /* Send WAL statistics */
978  pgstat_send_wal(true);
979 
980  /* Finally send SLRU statistics */
982 }
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:862
int tsa_used
Definition: pgstat.c:197
static int pgStatXactCommit
Definition: pgstat.c:245
static void pgstat_send_slru(void)
Definition: pgstat.c:3284
int64 TimestampTz
Definition: timestamp.h:39
WalUsage pgWalUsage
Definition: instrument.c:22
static void pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now)
Definition: pgstat.c:988
PgStat_Counter m_wal_sync
Definition: pgstat.h:494
#define MemSet(start, val, len)
Definition: c.h:1008
struct TabStatusArray * tsa_next
Definition: pgstat.c:196
PgStat_TableCounts t_counts
Definition: pgstat.h:177
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1711
static HTAB * pgStatTabHash
Definition: pgstat.c:215
static int pgStatXactRollback
Definition: pgstat.c:246
#define PGSTAT_STAT_INTERVAL
Definition: pgstat.c:78
PgStat_TableStatus tsa_entries[TABSTAT_QUANTUM]
Definition: pgstat.c:198
PgStat_Counter m_wal_write
Definition: pgstat.h:493
void pgstat_send_wal(bool force)
Definition: pgstat.c:3193
#define PGSTAT_NUM_TABENTRIES
Definition: pgstat.h:284
Oid MyDatabaseId
Definition: globals.c:88
PgStat_MsgWal WalStats
Definition: pgstat.c:133
static TabStatusArray * pgStatTabList
Definition: pgstat.c:201
#define InvalidOid
Definition: postgres_ext.h:36
static bool have_function_stats
Definition: pgstat.c:227
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition: xact.c:819
#define Assert(condition)
Definition: c.h:804
PgStat_TableCounts t_counts
Definition: pgstat.h:276
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:176
int64 wal_records
Definition: instrument.h:49
static WalUsage prevWalUsage
Definition: pgstat.c:141
PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES]
Definition: pgstat.h:300
int i
static void pgstat_assert_is_up(void)
Definition: pgstat.c:4850
static void pgstat_send_funcstats(void)
Definition: pgstat.c:1059
static void pgstat_report_disconnect(Oid dboid)
Definition: pgstat.c:1778
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1544

◆ pgstat_report_vacuum()

void pgstat_report_vacuum ( Oid  tableoid,
bool  shared,
PgStat_Counter  livetuples,
PgStat_Counter  deadtuples 
)

Definition at line 1578 of file pgstat.c.

References GetCurrentTimestamp(), InvalidOid, IsAutoVacuumWorkerProcess(), PgStat_MsgVacuum::m_autovacuum, PgStat_MsgVacuum::m_databaseid, PgStat_MsgVacuum::m_dead_tuples, PgStat_MsgVacuum::m_hdr, PgStat_MsgVacuum::m_live_tuples, PgStat_MsgVacuum::m_tableoid, PgStat_MsgVacuum::m_vacuumtime, MyDatabaseId, PGINVALID_SOCKET, PGSTAT_MTYPE_VACUUM, pgstat_send(), pgstat_setheader(), pgstat_track_counts, and pgStatSock.

Referenced by heap_vacuum_rel().

1580 {
1581  PgStat_MsgVacuum msg;
1582 
1584  return;
1585 
1587  msg.m_databaseid = shared ? InvalidOid : MyDatabaseId;
1588  msg.m_tableoid = tableoid;
1591  msg.m_live_tuples = livetuples;
1592  msg.m_dead_tuples = deadtuples;
1593  pgstat_send(&msg, sizeof(msg));
1594 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
PgStat_Counter m_dead_tuples
Definition: pgstat.h:418
TimestampTz m_vacuumtime
Definition: pgstat.h:416
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:173
PgStat_Counter m_live_tuples
Definition: pgstat.h:417
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
Oid m_databaseid
Definition: pgstat.h:413
bool m_autovacuum
Definition: pgstat.h:415
bool pgstat_track_counts
Definition: pgstat.c:115
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3406
#define PGINVALID_SOCKET
Definition: port.h:33
Oid MyDatabaseId
Definition: globals.c:88
#define InvalidOid
Definition: postgres_ext.h:36
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058
PgStat_MsgHdr m_hdr
Definition: pgstat.h:412

◆ pgstat_reset_all()

void pgstat_reset_all ( void  )

Definition at line 727 of file pgstat.c.

References Assert, av, lengthof, pgstat_reset_remove_files(), pgstat_stat_directory, and PGSTAT_STAT_PERMANENT_DIRECTORY.

Referenced by StartupXLOG().

728 {
731 }
#define PGSTAT_STAT_PERMANENT_DIRECTORY
Definition: pgstat.h:28
char * pgstat_stat_directory
Definition: pgstat.c:122
static void pgstat_reset_remove_files(const char *directory)
Definition: pgstat.c:679

◆ pgstat_reset_counters()

void pgstat_reset_counters ( void  )

Definition at line 1422 of file pgstat.c.

References PgStat_MsgResetcounter::m_databaseid, PgStat_MsgResetcounter::m_hdr, MyDatabaseId, PGINVALID_SOCKET, PGSTAT_MTYPE_RESETCOUNTER, pgstat_send(), pgstat_setheader(), and pgStatSock.

Referenced by pg_stat_reset().

1423 {
1425 
1427  return;
1428 
1430  msg.m_databaseid = MyDatabaseId;
1431  pgstat_send(&msg, sizeof(msg));
1432 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:173
PgStat_MsgHdr m_hdr
Definition: pgstat.h:341
#define PGINVALID_SOCKET
Definition: port.h:33
Oid MyDatabaseId
Definition: globals.c:88
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058

◆ pgstat_reset_replslot_counter()

void pgstat_reset_replslot_counter ( const char *  name)

Definition at line 1527 of file pgstat.c.

References PgStat_MsgResetreplslotcounter::clearall, PgStat_MsgResetreplslotcounter::m_hdr, PgStat_MsgResetreplslotcounter::m_slotname, namestrcpy(), PGINVALID_SOCKET, PGSTAT_MTYPE_RESETREPLSLOTCOUNTER, pgstat_send(), pgstat_setheader(), and pgStatSock.

Referenced by pg_stat_reset_replication_slot().

1528 {
1530 
1532  return;
1533 
1534  if (name)
1535  {
1536  namestrcpy(&msg.m_slotname, name);
1537  msg.clearall = false;
1538  }
1539  else
1540  msg.clearall = true;
1541 
1543 
1544  pgstat_send(&msg, sizeof(msg));
1545 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:173
void namestrcpy(Name name, const char *str)
Definition: name.c:233
#define PGINVALID_SOCKET
Definition: port.h:33
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058
const char * name
Definition: encode.c:561

◆ pgstat_reset_shared_counters()

void pgstat_reset_shared_counters ( const char *  )

Definition at line 1444 of file pgstat.c.

References ereport, errcode(), errhint(), errmsg(), ERROR, PgStat_MsgResetsharedcounter::m_hdr, PgStat_MsgResetsharedcounter::m_resettarget, PGINVALID_SOCKET, PGSTAT_MTYPE_RESETSHAREDCOUNTER, pgstat_send(), pgstat_setheader(), pgStatSock, RESET_ARCHIVER, RESET_BGWRITER, and RESET_WAL.

Referenced by pg_stat_reset_shared().

1445 {
1447 
1449  return;
1450 
1451  if (strcmp(target, "archiver") == 0)
1453  else if (strcmp(target, "bgwriter") == 0)
1455  else if (strcmp(target, "wal") == 0)
1456  msg.m_resettarget = RESET_WAL;
1457  else
1458  ereport(ERROR,
1459  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1460  errmsg("unrecognized reset target: \"%s\"", target),
1461  errhint("Target must be \"archiver\", \"bgwriter\", or \"wal\".")));
1462 
1464  pgstat_send(&msg, sizeof(msg));
1465 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
int errhint(const char *fmt,...)
Definition: elog.c:1156
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:173
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
#define PGINVALID_SOCKET
Definition: port.h:33
PgStat_Shared_Reset_Target m_resettarget
Definition: pgstat.h:353
#define ereport(elevel,...)
Definition: elog.h:157
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pgstat_reset_single_counter()

void pgstat_reset_single_counter ( Oid  objectid,
PgStat_Single_Reset_Type  type 
)

Definition at line 1477 of file pgstat.c.

References PgStat_MsgResetsinglecounter::m_databaseid, PgStat_MsgResetsinglecounter::m_hdr, PgStat_MsgResetsinglecounter::m_objectid, PgStat_MsgResetsinglecounter::m_resettype, MyDatabaseId, PGINVALID_SOCKET, PGSTAT_MTYPE_RESETSINGLECOUNTER, pgstat_send(), pgstat_setheader(), pgStatSock, and generate_unaccent_rules::type.

Referenced by pg_stat_reset_single_function_counters(), and pg_stat_reset_single_table_counters().

1478 {
1480 
1482  return;
1483 
1485  msg.m_databaseid = MyDatabaseId;
1486  msg.m_resettype = type;
1487  msg.m_objectid = objoid;
1488 
1489  pgstat_send(&msg, sizeof(msg));
1490 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:173
#define PGINVALID_SOCKET
Definition: port.h:33
Oid MyDatabaseId
Definition: globals.c:88
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058
PgStat_Single_Reset_Type m_resettype
Definition: pgstat.h:365

◆ pgstat_reset_slru_counter()

void pgstat_reset_slru_counter ( const char *  )

Definition at line 1503 of file pgstat.c.

References PgStat_MsgResetslrucounter::m_hdr, PgStat_MsgResetslrucounter::m_index, name, PGINVALID_SOCKET, PGSTAT_MTYPE_RESETSLRUCOUNTER, pgstat_send(), pgstat_setheader(), pgstat_slru_index(), and pgStatSock.

Referenced by pg_stat_reset_slru().

1504 {
1506 
1508  return;
1509 
1511  msg.m_index = (name) ? pgstat_slru_index(name) : -1;
1512 
1513  pgstat_send(&msg, sizeof(msg));
1514 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:173
int pgstat_slru_index(const char *name)
Definition: pgstat.c:5945
PgStat_MsgHdr m_hdr
Definition: pgstat.h:376
#define PGINVALID_SOCKET
Definition: port.h:33
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058
const char * name
Definition: encode.c:561

◆ pgstat_send_archiver()

void pgstat_send_archiver ( const char *  xlog,
bool  failed 
)

Definition at line 3103 of file pgstat.c.

References GetCurrentTimestamp(), PgStat_MsgArchiver::m_failed, PgStat_MsgArchiver::m_hdr, PgStat_MsgArchiver::m_timestamp, PgStat_MsgArchiver::m_xlog, PGSTAT_MTYPE_ARCHIVER, pgstat_send(), pgstat_setheader(), and strlcpy().

Referenced by pgarch_ArchiverCopyLoop().

3104 {
3105  PgStat_MsgArchiver msg;
3106 
3107  /*
3108  * Prepare and send the message
3109  */
3111  msg.m_failed = failed;
3112  strlcpy(msg.m_xlog, xlog, sizeof(msg.m_xlog));
3114  pgstat_send(&msg, sizeof(msg));
3115 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
char m_xlog[MAX_XFN_CHARS+1]
Definition: pgstat.h:448
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
TimestampTz m_timestamp
Definition: pgstat.h:449
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058
PgStat_MsgHdr m_hdr
Definition: pgstat.h:446

◆ pgstat_send_bgwriter()

void pgstat_send_bgwriter ( void  )

Definition at line 3124 of file pgstat.c.

References PgStat_MsgBgWriter::m_hdr, MemSet, pgstat_assert_is_up(), PGSTAT_MTYPE_BGWRITER, pgstat_send(), and pgstat_setheader().

Referenced by BackgroundWriterMain().

3125 {
3126  /* We assume this initializes to zeroes */
3127  static const PgStat_MsgBgWriter all_zeroes;
3128 
3130 
3131  /*
3132  * This function can be called even if nothing at all has happened. In
3133  * this case, avoid sending a completely empty message to the stats
3134  * collector.
3135  */
3136  if (memcmp(&PendingBgWriterStats, &all_zeroes, sizeof(PgStat_MsgBgWriter)) == 0)
3137  return;
3138 
3139  /*
3140  * Prepare and send the message
3141  */
3144 
3145  /*
3146  * Clear out the statistics buffer, so it can be re-used.
3147  */
3149 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
PgStat_MsgBgWriter PendingBgWriterStats
Definition: pgstat.c:131
PgStat_MsgHdr m_hdr
Definition: pgstat.h:458
#define MemSet(start, val, len)
Definition: c.h:1008
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058
static void pgstat_assert_is_up(void)
Definition: pgstat.c:4850

◆ pgstat_send_checkpointer()

void pgstat_send_checkpointer ( void  )

Definition at line 3158 of file pgstat.c.

References PgStat_MsgCheckpointer::m_hdr, MemSet, PGSTAT_MTYPE_CHECKPOINTER, pgstat_send(), and pgstat_setheader().

Referenced by CheckpointerMain(), CheckpointWriteDelay(), and HandleCheckpointerInterrupts().

3159 {
3160  /* We assume this initializes to zeroes */
3161  static const PgStat_MsgCheckpointer all_zeroes;
3162 
3163  /*
3164  * This function can be called even if nothing at all has happened. In
3165  * this case, avoid sending a completely empty message to the stats
3166  * collector.
3167  */
3168  if (memcmp(&PendingCheckpointerStats, &all_zeroes, sizeof(PgStat_MsgCheckpointer)) == 0)
3169  return;
3170 
3171  /*
3172  * Prepare and send the message
3173  */
3176 
3177  /*
3178  * Clear out the statistics buffer, so it can be re-used.
3179  */
3181 }
PgStat_MsgCheckpointer PendingCheckpointerStats
Definition: pgstat.c:132
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
#define MemSet(start, val, len)
Definition: c.h:1008
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058
PgStat_MsgHdr m_hdr
Definition: pgstat.h:471

◆ pgstat_send_wal()

void pgstat_send_wal ( bool  force)

Definition at line 3193 of file pgstat.c.

References Assert, GetCurrentTimestamp(), PgStat_MsgWal::m_hdr, PgStat_MsgWal::m_wal_buffers_full, PgStat_MsgWal::m_wal_bytes, PgStat_MsgWal::m_wal_fpi, PgStat_MsgWal::m_wal_records, PgStat_MsgWal::m_wal_sync, PgStat_MsgWal::m_wal_write, MemSet, now(), PGSTAT_MTYPE_WAL, pgstat_send(), pgstat_setheader(), PGSTAT_STAT_INTERVAL, pgWalUsage, TimestampDifferenceExceeds(), WalUsage::wal_bytes, WalUsage::wal_fpi, WalUsage::wal_records, and WalUsageAccumDiff().

Referenced by CheckpointerMain(), HandleCheckpointerInterrupts(), HandleWalWriterInterrupts(), pgstat_report_stat(), and WalWriterMain().

3194 {
3195  static TimestampTz sendTime = 0;
3196 
3197  /*
3198  * This function can be called even if nothing at all has happened. In
3199  * this case, avoid sending a completely empty message to the stats
3200  * collector.
3201  *
3202  * Check wal_records counter to determine whether any WAL activity has
3203  * happened since last time. Note that other WalUsage counters don't need
3204  * to be checked because they are incremented always together with
3205  * wal_records counter.
3206  *
3207  * m_wal_buffers_full also doesn't need to be checked because it's
3208  * incremented only when at least one WAL record is generated (i.e.,
3209  * wal_records counter is incremented). But for safely, we assert that
3210  * m_wal_buffers_full is always zero when no WAL record is generated
3211  *
3212  * This function can be called by a process like walwriter that normally
3213  * generates no WAL records. To determine whether any WAL activity has
3214  * happened at that process since the last time, the numbers of WAL writes
3215  * and syncs are also checked.
3216  */
3218  WalStats.m_wal_write == 0 && WalStats.m_wal_sync == 0)
3219  {
3221  return;
3222  }
3223 
3224  if (!force)
3225  {
3227 
3228  /*
3229  * Don't send a message unless it's been at least PGSTAT_STAT_INTERVAL
3230  * msec since we last sent one to avoid overloading the stats
3231  * collector.
3232  */
3233  if (!TimestampDifferenceExceeds(sendTime, now, PGSTAT_STAT_INTERVAL))
3234  return;
3235  sendTime = now;
3236  }
3237 
3238  /*
3239  * Set the counters related to generated WAL data if the counters were
3240  * updated.
3241  */
3243  {
3244  WalUsage walusage;
3245 
3246  /*
3247  * Calculate how much WAL usage counters were increased by
3248  * subtracting the previous counters from the current ones. Fill the
3249  * results in WAL stats message.
3250  */
3251  MemSet(&walusage, 0, sizeof(WalUsage));
3252  WalUsageAccumDiff(&walusage, &pgWalUsage, &prevWalUsage);
3253 
3254  WalStats.m_wal_records = walusage.wal_records;
3255  WalStats.m_wal_fpi = walusage.wal_fpi;
3256  WalStats.m_wal_bytes = walusage.wal_bytes;
3257 
3258  /*
3259  * Save the current counters for the subsequent calculation of WAL
3260  * usage.
3261  */
3263  }
3264 
3265  /*
3266  * Prepare and send the message
3267  */
3269  pgstat_send(&WalStats, sizeof(WalStats));
3270 
3271  /*
3272  * Clear out the statistics buffer, so it can be re-used.
3273  */
3274  MemSet(&WalStats, 0, sizeof(WalStats));
3275 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
PgStat_MsgHdr m_hdr
Definition: pgstat.h:488
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
int64 TimestampTz
Definition: timestamp.h:39
WalUsage pgWalUsage
Definition: instrument.c:22
int64 wal_fpi
Definition: instrument.h:50
void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
Definition: instrument.c:274
PgStat_Counter m_wal_sync
Definition: pgstat.h:494
uint64 m_wal_bytes
Definition: pgstat.h:491
#define MemSet(start, val, len)
Definition: c.h:1008
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1711
#define PGSTAT_STAT_INTERVAL
Definition: pgstat.c:78
PgStat_Counter m_wal_buffers_full
Definition: pgstat.h:492
PgStat_Counter m_wal_write
Definition: pgstat.h:493
PgStat_MsgWal WalStats
Definition: pgstat.c:133
#define Assert(condition)
Definition: c.h:804
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058
PgStat_Counter m_wal_fpi
Definition: pgstat.h:490
int64 wal_records
Definition: instrument.h:49
static WalUsage prevWalUsage
Definition: pgstat.c:141
uint64 wal_bytes
Definition: instrument.h:51
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1544
PgStat_Counter m_wal_records
Definition: pgstat.h:489

◆ pgstat_slru_index()

int pgstat_slru_index ( const char *  name)

Definition at line 5945 of file pgstat.c.

References i, slru_names, and SLRU_NUM_ELEMENTS.

Referenced by pgstat_reset_slru_counter(), and SimpleLruInit().

5946 {
5947  int i;
5948 
5949  for (i = 0; i < SLRU_NUM_ELEMENTS; i++)
5950  {
5951  if (strcmp(slru_names[i], name) == 0)
5952  return i;
5953  }
5954 
5955  /* return index of the last entry (which is the "other" one) */
5956  return (SLRU_NUM_ELEMENTS - 1);
5957 }
#define SLRU_NUM_ELEMENTS
Definition: pgstat.c:159
static const char *const slru_names[]
Definition: pgstat.c:148
const char * name
Definition: encode.c:561
int i

◆ pgstat_slru_name()

const char* pgstat_slru_name ( int  slru_idx)

Definition at line 5967 of file pgstat.c.

References slru_names, and SLRU_NUM_ELEMENTS.

Referenced by pg_stat_get_slru().

5968 {
5969  if (slru_idx < 0 || slru_idx >= SLRU_NUM_ELEMENTS)
5970  return NULL;
5971 
5972  return slru_names[slru_idx];
5973 }
#define SLRU_NUM_ELEMENTS
Definition: pgstat.c:159
static const char *const slru_names[]
Definition: pgstat.c:148

◆ pgstat_start()

int pgstat_start ( void  )

Definition at line 769 of file pgstat.c.

References ClosePostmasterPorts(), dsm_detach_all(), ereport, errmsg(), fork_process(), InitPostmasterChild(), last_pgstat_start_time, LOG, PGINVALID_SOCKET, PGSharedMemoryDetach(), PGSTAT_RESTART_INTERVAL, PgstatCollectorMain(), and pgStatSock.

Referenced by reaper(), ServerLoop(), and sigusr1_handler().

770 {
771  time_t curtime;
772  pid_t pgStatPid;
773 
774  /*
775  * Check that the socket is there, else pgstat_init failed and we can do
776  * nothing useful.
777  */
779  return 0;
780 
781  /*
782  * Do nothing if too soon since last collector start. This is a safety
783  * valve to protect against continuous respawn attempts if the collector
784  * is dying immediately at launch. Note that since we will be re-called
785  * from the postmaster main loop, we will get another chance later.
786  */
787  curtime = time(NULL);
788  if ((unsigned int) (curtime - last_pgstat_start_time) <
789  (unsigned int) PGSTAT_RESTART_INTERVAL)
790  return 0;
791  last_pgstat_start_time = curtime;
792 
793  /*
794  * Okay, fork off the collector.
795  */
796 #ifdef EXEC_BACKEND
797  switch ((pgStatPid = pgstat_forkexec()))
798 #else
799  switch ((pgStatPid = fork_process()))
800 #endif
801  {
802  case -1:
803  ereport(LOG,
804  (errmsg("could not fork statistics collector: %m")));
805  return 0;
806 
807 #ifndef EXEC_BACKEND
808  case 0:
809  /* in postmaster child ... */
811 
812  /* Close the postmaster's sockets */
813  ClosePostmasterPorts(false);
814 
815  /* Drop our connection to postmaster's shared memory, as well */
816  dsm_detach_all();
818 
819  PgstatCollectorMain(0, NULL);
820  break;
821 #endif
822 
823  default:
824  return (int) pgStatPid;
825  }
826 
827  /* shouldn't get here */
828  return 0;
829 }
#define PGSTAT_RESTART_INTERVAL
Definition: pgstat.c:90
NON_EXEC_STATIC void PgstatCollectorMain(int argc, char *argv[]) pg_attribute_noreturn()
Definition: pgstat.c:3326
void InitPostmasterChild(void)
Definition: miscinit.c:94
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:173
pid_t fork_process(void)
Definition: fork_process.c:29
#define LOG
Definition: elog.h:26
void ClosePostmasterPorts(bool am_syslogger)
Definition: postmaster.c:2612
void PGSharedMemoryDetach(void)
Definition: sysv_shmem.c:936
#define PGINVALID_SOCKET
Definition: port.h:33
#define ereport(elevel,...)
Definition: elog.h:157
void dsm_detach_all(void)
Definition: dsm.c:741
static time_t last_pgstat_start_time
Definition: pgstat.c:177
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pgstat_twophase_postabort()

void pgstat_twophase_postabort ( TransactionId  xid,
uint16  info,
void *  recdata,
uint32  len 
)

Definition at line 2740 of file pgstat.c.

References TwoPhasePgStatRecord::deleted_pre_truncdrop, get_tabstat_entry(), TwoPhasePgStatRecord::inserted_pre_truncdrop, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_delta_dead_tuples, TwoPhasePgStatRecord::t_id, TwoPhasePgStatRecord::t_shared, TwoPhasePgStatRecord::t_truncdropped, PgStat_TableCounts::t_tuples_deleted, PgStat_TableCounts::t_tuples_inserted, PgStat_TableCounts::t_tuples_updated, TwoPhasePgStatRecord::tuples_deleted, TwoPhasePgStatRecord::tuples_inserted, TwoPhasePgStatRecord::tuples_updated, and TwoPhasePgStatRecord::updated_pre_truncdrop.

2742 {
2743  TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
2744  PgStat_TableStatus *pgstat_info;
2745 
2746  /* Find or create a tabstat entry for the rel */
2747  pgstat_info = get_tabstat_entry(rec->t_id, rec->t_shared);
2748 
2749  /* Same math as in AtEOXact_PgStat, abort case */
2750  if (rec->t_truncdropped)
2751  {
2755  }
2756  pgstat_info->t_counts.t_tuples_inserted += rec->tuples_inserted;
2757  pgstat_info->t_counts.t_tuples_updated += rec->tuples_updated;
2758  pgstat_info->t_counts.t_tuples_deleted += rec->tuples_deleted;
2759  pgstat_info->t_counts.t_delta_dead_tuples +=
2760  rec->tuples_inserted + rec->tuples_updated;
2761 }
PgStat_Counter deleted_pre_truncdrop
Definition: pgstat.c:263
PgStat_Counter updated_pre_truncdrop
Definition: pgstat.c:262
PgStat_TableCounts t_counts
Definition: pgstat.h:177
PgStat_Counter t_tuples_updated
Definition: pgstat.h:123
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:129
PgStat_Counter tuples_updated
Definition: pgstat.c:258
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:124
static PgStat_TableStatus * get_tabstat_entry(Oid rel_id, bool isshared)
Definition: pgstat.c:2081
PgStat_Counter inserted_pre_truncdrop
Definition: pgstat.c:261
PgStat_Counter tuples_inserted
Definition: pgstat.c:257
PgStat_Counter tuples_deleted
Definition: pgstat.c:259
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:122

◆ pgstat_twophase_postcommit()

void pgstat_twophase_postcommit ( TransactionId  xid,
uint16  info,
void *  recdata,
uint32  len 
)

Definition at line 2704 of file pgstat.c.

References get_tabstat_entry(), PgStat_TableCounts::t_changed_tuples, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_delta_dead_tuples, PgStat_TableCounts::t_delta_live_tuples, TwoPhasePgStatRecord::t_id, TwoPhasePgStatRecord::t_shared, PgStat_TableCounts::t_truncdropped, TwoPhasePgStatRecord::t_truncdropped, PgStat_TableCounts::t_tuples_deleted, PgStat_TableCounts::t_tuples_inserted, PgStat_TableCounts::t_tuples_updated, TwoPhasePgStatRecord::tuples_deleted, TwoPhasePgStatRecord::tuples_inserted, and TwoPhasePgStatRecord::tuples_updated.

2706 {
2707  TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
2708  PgStat_TableStatus *pgstat_info;
2709 
2710  /* Find or create a tabstat entry for the rel */
2711  pgstat_info = get_tabstat_entry(rec->t_id, rec->t_shared);
2712 
2713  /* Same math as in AtEOXact_PgStat, commit case */
2714  pgstat_info->t_counts.t_tuples_inserted += rec->tuples_inserted;
2715  pgstat_info->t_counts.t_tuples_updated += rec->tuples_updated;
2716  pgstat_info->t_counts.t_tuples_deleted += rec->tuples_deleted;
2717  pgstat_info->t_counts.t_truncdropped = rec->t_truncdropped;
2718  if (rec->t_truncdropped)
2719  {
2720  /* forget live/dead stats seen by backend thus far */
2721  pgstat_info->t_counts.t_delta_live_tuples = 0;
2722  pgstat_info->t_counts.t_delta_dead_tuples = 0;
2723  }
2724  pgstat_info->t_counts.t_delta_live_tuples +=
2725  rec->tuples_inserted - rec->tuples_deleted;
2726  pgstat_info->t_counts.t_delta_dead_tuples +=
2727  rec->tuples_updated + rec->tuples_deleted;
2728  pgstat_info->t_counts.t_changed_tuples +=
2729  rec->tuples_inserted + rec->tuples_updated +
2730  rec->tuples_deleted;
2731 }
PgStat_Counter t_delta_live_tuples
Definition: pgstat.h:128
bool t_truncdropped
Definition: pgstat.h:126
PgStat_TableCounts t_counts
Definition: pgstat.h:177
PgStat_Counter t_tuples_updated
Definition: pgstat.h:123
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:129
PgStat_Counter tuples_updated
Definition: pgstat.c:258
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:124
static PgStat_TableStatus * get_tabstat_entry(Oid rel_id, bool isshared)
Definition: pgstat.c:2081
PgStat_Counter t_changed_tuples
Definition: pgstat.h:130
PgStat_Counter tuples_inserted
Definition: pgstat.c:257
PgStat_Counter tuples_deleted
Definition: pgstat.c:259
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:122

◆ pgstat_update_heap_dead_tuples()

void pgstat_update_heap_dead_tuples ( Relation  rel,
int  delta 
)

Definition at line 2373 of file pgstat.c.

References RelationData::pgstat_info, PgStat_TableStatus::t_counts, and PgStat_TableCounts::t_delta_dead_tuples.

Referenced by heap_page_prune().

2374 {
2375  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2376 
2377  if (pgstat_info != NULL)
2378  pgstat_info->t_counts.t_delta_dead_tuples -= delta;
2379 }
PgStat_TableCounts t_counts
Definition: pgstat.h:177
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:129
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:249

◆ pgstat_vacuum_stat()

void pgstat_vacuum_stat ( void  )

Definition at line 1118 of file pgstat.c.

References backend_read_statsfile(), CHECK_FOR_INTERRUPTS, PgStat_StatDBEntry::databaseid, PgStat_StatFuncEntry::functionid, PgStat_StatDBEntry::functions, hash_destroy(), HASH_FIND, hash_get_num_entries(), hash_search(), hash_seq_init(), hash_seq_search(), PgStat_MsgTabpurge::m_databaseid, PgStat_MsgFuncpurge::m_databaseid, PgStat_MsgFuncpurge::m_functionid, PgStat_MsgTabpurge::m_hdr, PgStat_MsgFuncpurge::m_hdr, PgStat_MsgTabpurge::m_nentries, PgStat_MsgFuncpurge::m_nentries, PgStat_MsgTabpurge::m_tableid, MyDatabaseId, NameStr, offsetof, OidIsValid, PGINVALID_SOCKET, pgstat_collect_oids(), pgstat_drop_database(), PGSTAT_MTYPE_FUNCPURGE, PGSTAT_MTYPE_TABPURGE, PGSTAT_NUM_FUNCPURGE, PGSTAT_NUM_TABPURGE, pgstat_report_replslot_drop(), pgstat_send(), pgstat_setheader(), pgStatSock, SearchNamedReplicationSlot(), PgStat_StatReplSlotEntry::slotname, PgStat_StatTabEntry::tableid, and PgStat_StatDBEntry::tables.

Referenced by do_autovacuum(), and vacuum().

1119 {
1120  HTAB *htab;
1121  PgStat_MsgTabpurge msg;
1122  PgStat_MsgFuncpurge f_msg;
1123  HASH_SEQ_STATUS hstat;
1124  PgStat_StatDBEntry *dbentry;
1125  PgStat_StatTabEntry *tabentry;
1126  PgStat_StatFuncEntry *funcentry;
1127  int len;
1128 
1130  return;
1131 
1132  /*
1133  * If not done for this transaction, read the statistics collector stats
1134  * file into some hash tables.
1135  */
1137 
1138  /*
1139  * Read pg_database and make a list of OIDs of all existing databases
1140  */
1141  htab = pgstat_collect_oids(DatabaseRelationId, Anum_pg_database_oid);
1142 
1143  /*
1144  * Search the database hash table for dead databases and tell the
1145  * collector to drop them.
1146  */
1147  hash_seq_init(&hstat, pgStatDBHash);
1148  while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
1149  {
1150  Oid dbid = dbentry->databaseid;
1151 
1153 
1154  /* the DB entry for shared tables (with InvalidOid) is never dropped */
1155  if (OidIsValid(dbid) &&
1156  hash_search(htab, (void *) &dbid, HASH_FIND, NULL) == NULL)
1157  pgstat_drop_database(dbid);
1158  }
1159 
1160  /* Clean up */
1161  hash_destroy(htab);
1162 
1163  /*
1164  * Search for all the dead replication slots in stats hashtable and tell
1165  * the stats collector to drop them.
1166  */
1167  if (replSlotStatHash)
1168  {
1169  PgStat_StatReplSlotEntry *slotentry;
1170 
1172  while ((slotentry = (PgStat_StatReplSlotEntry *) hash_seq_search(&hstat)) != NULL)
1173  {
1175 
1176  if (SearchNamedReplicationSlot(NameStr(slotentry->slotname), true) == NULL)
1178  }
1179  }
1180 
1181  /*
1182  * Lookup our own database entry; if not found, nothing more to do.
1183  */
1185  (void *) &MyDatabaseId,
1186  HASH_FIND, NULL);
1187  if (dbentry == NULL || dbentry->tables == NULL)
1188  return;
1189 
1190  /*
1191  * Similarly to above, make a list of all known relations in this DB.
1192  */
1193  htab = pgstat_collect_oids(RelationRelationId, Anum_pg_class_oid);
1194 
1195  /*
1196  * Initialize our messages table counter to zero
1197  */
1198  msg.m_nentries = 0;
1199 
1200  /*
1201  * Check for all tables listed in stats hashtable if they still exist.
1202  */
1203  hash_seq_init(&hstat, dbentry->tables);
1204  while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&hstat)) != NULL)
1205  {
1206  Oid tabid = tabentry->tableid;
1207 
1209 
1210  if (hash_search(htab, (void *) &tabid, HASH_FIND, NULL) != NULL)
1211  continue;
1212 
1213  /*
1214  * Not there, so add this table's Oid to the message
1215  */
1216  msg.m_tableid[msg.m_nentries++] = tabid;
1217 
1218  /*
1219  * If the message is full, send it out and reinitialize to empty
1220  */
1221  if (msg.m_nentries >= PGSTAT_NUM_TABPURGE)
1222  {
1223  len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
1224  + msg.m_nentries * sizeof(Oid);
1225 
1227  msg.m_databaseid = MyDatabaseId;
1228  pgstat_send(&msg, len);
1229 
1230  msg.m_nentries = 0;
1231  }
1232  }
1233 
1234  /*
1235  * Send the rest
1236  */
1237  if (msg.m_nentries > 0)
1238  {
1239  len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
1240  + msg.m_nentries * sizeof(Oid);
1241 
1243  msg.m_databaseid = MyDatabaseId;
1244  pgstat_send(&msg, len);
1245  }
1246 
1247  /* Clean up */
1248  hash_destroy(htab);
1249 
1250  /*
1251  * Now repeat the above steps for functions. However, we needn't bother
1252  * in the common case where no function stats are being collected.
1253  */
1254  if (dbentry->functions != NULL &&
1255  hash_get_num_entries(dbentry->functions) > 0)
1256  {
1257  htab = pgstat_collect_oids(ProcedureRelationId, Anum_pg_proc_oid);
1258 
1260  f_msg.m_databaseid = MyDatabaseId;
1261  f_msg.m_nentries = 0;
1262 
1263  hash_seq_init(&hstat, dbentry->functions);
1264  while ((funcentry = (PgStat_StatFuncEntry *) hash_seq_search(&hstat)) != NULL)
1265  {
1266  Oid funcid = funcentry->functionid;
1267 
1269 
1270  if (hash_search(htab, (void *) &funcid, HASH_FIND, NULL) != NULL)
1271  continue;
1272 
1273  /*
1274  * Not there, so add this function's Oid to the message
1275  */
1276  f_msg.m_functionid[f_msg.m_nentries++] = funcid;
1277 
1278  /*
1279  * If the message is full, send it out and reinitialize to empty
1280  */
1281  if (f_msg.m_nentries >= PGSTAT_NUM_FUNCPURGE)
1282  {
1283  len = offsetof(PgStat_MsgFuncpurge, m_functionid[0])
1284  + f_msg.m_nentries * sizeof(Oid);
1285 
1286  pgstat_send(&f_msg, len);
1287 
1288  f_msg.m_nentries = 0;
1289  }
1290  }
1291 
1292  /*
1293  * Send the rest
1294  */
1295  if (f_msg.m_nentries > 0)
1296  {
1297  len = offsetof(PgStat_MsgFuncpurge, m_functionid[0])
1298  + f_msg.m_nentries * sizeof(Oid);
1299 
1300  pgstat_send(&f_msg, len);
1301  }
1302 
1303  hash_destroy(htab);
1304  }
1305 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3071
ReplicationSlot * SearchNamedReplicationSlot(const char *name, bool need_lock)
Definition: slot.c:348
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:862
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:173
#define PGSTAT_NUM_TABPURGE
Definition: pgstat.h:309
PgStat_MsgHdr m_hdr
Definition: pgstat.h:631
HTAB * functions
Definition: pgstat.h:775
long hash_get_num_entries(HTAB *hashp)
Definition: dynahash.c:1382
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 OidIsValid(objectId)
Definition: c.h:710
Definition: dynahash.c:219
static HTAB * pgstat_collect_oids(Oid catalogid, AttrNumber anum_oid)
Definition: pgstat.c:1318
Oid m_tableid[PGSTAT_NUM_TABPURGE]
Definition: pgstat.h:318
static void backend_read_statsfile(void)
Definition: pgstat.c:4699
void pgstat_drop_database(Oid databaseid)
Definition: pgstat.c:1367
PgStat_MsgHdr m_hdr
Definition: pgstat.h:315
#define PGINVALID_SOCKET
Definition: port.h:33
Oid MyDatabaseId
Definition: globals.c:88
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3058
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1436
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1426
static HTAB * pgStatDBHash
Definition: pgstat.c:273
void pgstat_report_replslot_drop(const char *slotname)
Definition: pgstat.c:1861
static HTAB * replSlotStatHash
Definition: pgstat.c:284
#define NameStr(name)
Definition: c.h:681
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
#define offsetof(type, field)
Definition: c.h:727
#define PGSTAT_NUM_FUNCPURGE
Definition: pgstat.h:625
Oid m_functionid[PGSTAT_NUM_FUNCPURGE]
Definition: pgstat.h:634

◆ PostPrepare_PgStat()

void PostPrepare_PgStat ( void  )

Definition at line 2676 of file pgstat.c.

References Assert, PgStat_SubXactStatus::nest_level, pgstat_clear_snapshot(), pgStatXactStack, PostPrepare_PgStat_Relations(), and PgStat_SubXactStatus::prev.

Referenced by PrepareTransaction().

2677 {
2678  PgStat_SubXactStatus *xact_state;
2679 
2680  /*
2681  * We don't bother to free any of the transactional state, since it's all
2682  * in TopTransactionContext and will go away anyway.
2683  */
2684  xact_state = pgStatXactStack;
2685  if (xact_state != NULL)
2686  {
2687  Assert(xact_state->nest_level == 1);
2688  Assert(xact_state->prev == NULL);
2689 
2690  PostPrepare_PgStat_Relations(xact_state);
2691  }
2692  pgStatXactStack = NULL;
2693 
2694  /* Make sure any stats snapshot is thrown away */
2696 }
struct PgStat_SubXactStatus * prev
Definition: pgstat.c:239
#define Assert(condition)
Definition: c.h:804
static void PostPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
Definition: pgstat.c:2656
void pgstat_clear_snapshot(void)
Definition: pgstat.c:4867
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:243

Variable Documentation

◆ PendingBgWriterStats

PgStat_MsgBgWriter PendingBgWriterStats

Definition at line 131 of file pgstat.c.

Referenced by BgBufferSync().

◆ PendingCheckpointerStats

◆ pgstat_stat_directory

char* pgstat_stat_directory

◆ pgstat_stat_filename

char* pgstat_stat_filename

◆ pgstat_stat_tmpname

char* pgstat_stat_tmpname

Definition at line 124 of file pgstat.c.

Referenced by assign_pgstat_temp_directory(), and pgstat_write_statsfiles().

◆ pgstat_track_counts

◆ pgstat_track_functions

PGDLLIMPORT int pgstat_track_functions

Definition at line 116 of file pgstat.c.

◆ pgStatActiveTime

PgStat_Counter pgStatActiveTime

Definition at line 250 of file pgstat.c.

Referenced by pgstat_send_tabstat().

◆ pgStatBlockReadTime

PgStat_Counter pgStatBlockReadTime

Definition at line 247 of file pgstat.c.

Referenced by do_analyze_rel(), heap_vacuum_rel(), and pgstat_send_tabstat().

◆ pgStatBlockWriteTime

PgStat_Counter pgStatBlockWriteTime

Definition at line 248 of file pgstat.c.

Referenced by do_analyze_rel(), heap_vacuum_rel(), and pgstat_send_tabstat().

◆ pgStatSessionEndCause

SessionEndType pgStatSessionEndCause

Definition at line 252 of file pgstat.c.

Referenced by die(), errfinish(), pgstat_report_disconnect(), and PostgresMain().

◆ pgStatTransactionIdleTime

PgStat_Counter pgStatTransactionIdleTime

Definition at line 251 of file pgstat.c.

Referenced by pgstat_send_tabstat().

◆ WalStats

PgStat_MsgWal WalStats

Definition at line 133 of file pgstat.c.

Referenced by AdvanceXLInsertBuffer(), issue_xlog_fsync(), and XLogWrite().