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_MsgSubscriptionPurge
 
struct  PgStat_MsgSubWorkerError
 
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_StatSubWorkerKey
 
struct  PgStat_StatSubWorkerEntry
 
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_SUBSCRIPTIONPURGE    ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) / sizeof(Oid))
 
#define PGSTAT_SUBWORKERERROR_MSGLEN   256
 
#define PGSTAT_NUM_FUNCENTRIES
 
#define PGSTAT_NUM_FUNCPURGE
 
#define PGSTAT_FILE_FORMAT_ID   0x01A5BCA5
 
#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_MsgSubscriptionPurge PgStat_MsgSubscriptionPurge
 
typedef struct PgStat_MsgSubWorkerError PgStat_MsgSubWorkerError
 
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_StatSubWorkerKey PgStat_StatSubWorkerKey
 
typedef struct PgStat_StatSubWorkerEntry PgStat_StatSubWorkerEntry
 
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 , PGSTAT_MTYPE_SUBSCRIPTIONPURGE ,
  PGSTAT_MTYPE_SUBWORKERERROR
}
 
enum  PgStat_Shared_Reset_Target { RESET_ARCHIVER , RESET_BGWRITER , RESET_WAL }
 
enum  PgStat_Single_Reset_Type { RESET_TABLE , RESET_FUNCTION , RESET_SUBWORKER }
 

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, Oid subobjectid, 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_report_subworker_error (Oid subid, Oid subrelid, Oid relid, LogicalRepMsgType command, TransactionId xid, const char *errmsg)
 
void pgstat_report_subscription_drop (Oid subid)
 
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_StatSubWorkerEntrypgstat_fetch_stat_subworker_entry (Oid subid, Oid subrelid)
 
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 34 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 1179 of file pgstat.h.

◆ 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 1174 of file pgstat.h.

◆ pgstat_count_buffer_read_time

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

Definition at line 1184 of file pgstat.h.

◆ pgstat_count_buffer_write_time

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

Definition at line 1186 of file pgstat.h.

◆ pgstat_count_conn_active_time

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

Definition at line 1188 of file pgstat.h.

◆ pgstat_count_conn_txn_idle_time

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

Definition at line 1190 of file pgstat.h.

◆ 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 1159 of file pgstat.h.

◆ 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 1154 of file pgstat.h.

◆ 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 1149 of file pgstat.h.

◆ 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 1164 of file pgstat.h.

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

◆ PGSTAT_FILE_FORMAT_ID

#define PGSTAT_FILE_FORMAT_ID   0x01A5BCA5

Definition at line 783 of file pgstat.h.

◆ PGSTAT_MAX_MSG_SIZE

#define PGSTAT_MAX_MSG_SIZE   1000

Definition at line 231 of file pgstat.h.

◆ PGSTAT_MSG_PAYLOAD

#define PGSTAT_MSG_PAYLOAD   (PGSTAT_MAX_MSG_SIZE - sizeof(PgStat_MsgHdr))

Definition at line 232 of file pgstat.h.

◆ PGSTAT_NUM_FUNCENTRIES

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

Definition at line 661 of file pgstat.h.

◆ PGSTAT_NUM_FUNCPURGE

#define PGSTAT_NUM_FUNCPURGE
Value:
((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
/ sizeof(Oid))

Definition at line 678 of file pgstat.h.

◆ PGSTAT_NUM_SUBSCRIPTIONPURGE

#define PGSTAT_NUM_SUBSCRIPTIONPURGE    ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) / sizeof(Oid))

Definition at line 549 of file pgstat.h.

◆ PGSTAT_NUM_TABENTRIES

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

Definition at line 288 of file pgstat.h.

◆ PGSTAT_NUM_TABPURGE

#define PGSTAT_NUM_TABPURGE
Value:
((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
/ sizeof(Oid))

Definition at line 313 of file pgstat.h.

◆ PGSTAT_STAT_PERMANENT_DIRECTORY

#define PGSTAT_STAT_PERMANENT_DIRECTORY   "pg_stat"

Definition at line 29 of file pgstat.h.

◆ PGSTAT_STAT_PERMANENT_FILENAME

#define PGSTAT_STAT_PERMANENT_FILENAME   "pg_stat/global.stat"

Definition at line 30 of file pgstat.h.

◆ PGSTAT_STAT_PERMANENT_TMPFILE

#define PGSTAT_STAT_PERMANENT_TMPFILE   "pg_stat/global.tmp"

Definition at line 31 of file pgstat.h.

◆ PGSTAT_SUBWORKERERROR_MSGLEN

#define PGSTAT_SUBWORKERERROR_MSGLEN   256

Definition at line 566 of file pgstat.h.

Typedef Documentation

◆ PgStat_ArchiverStats

◆ PgStat_BackendFunctionEntry

◆ PgStat_BgWriterStats

◆ PgStat_CheckpointerStats

◆ PgStat_Counter

typedef int64 PgStat_Counter

Definition at line 95 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_MsgSubscriptionPurge

◆ PgStat_MsgSubWorkerError

◆ 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_StatSubWorkerEntry

◆ PgStat_StatSubWorkerKey

◆ 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 140 of file pgstat.h.

141 {
144  RESET_WAL
PgStat_Shared_Reset_Target
Definition: pgstat.h:141
@ RESET_BGWRITER
Definition: pgstat.h:143
@ RESET_ARCHIVER
Definition: pgstat.h:142
@ RESET_WAL
Definition: pgstat.h:144

◆ PgStat_Single_Reset_Type

Enumerator
RESET_TABLE 
RESET_FUNCTION 
RESET_SUBWORKER 

Definition at line 148 of file pgstat.h.

149 {
150  RESET_TABLE,
PgStat_Single_Reset_Type
Definition: pgstat.h:149
@ RESET_FUNCTION
Definition: pgstat.h:151
@ RESET_TABLE
Definition: pgstat.h:150
@ RESET_SUBWORKER
Definition: pgstat.h:152

◆ SessionEndType

Enumerator
DISCONNECT_NOT_YET 
DISCONNECT_NORMAL 
DISCONNECT_CLIENT_EOF 
DISCONNECT_FATAL 
DISCONNECT_KILLED 

Definition at line 45 of file pgstat.h.

46 {
47  DISCONNECT_NOT_YET, /* still active */
SessionEndType
Definition: pgstat.h:46
@ DISCONNECT_NOT_YET
Definition: pgstat.h:47
@ DISCONNECT_FATAL
Definition: pgstat.h:50
@ DISCONNECT_KILLED
Definition: pgstat.h:51
@ DISCONNECT_CLIENT_EOF
Definition: pgstat.h:49
@ DISCONNECT_NORMAL
Definition: pgstat.h:48

◆ 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 
PGSTAT_MTYPE_SUBSCRIPTIONPURGE 
PGSTAT_MTYPE_SUBWORKERERROR 

Definition at line 58 of file pgstat.h.

59 {
89 } StatMsgType;
StatMsgType
Definition: pgstat.h:59
@ PGSTAT_MTYPE_DISCONNECT
Definition: pgstat.h:86
@ PGSTAT_MTYPE_AUTOVAC_START
Definition: pgstat.h:70
@ PGSTAT_MTYPE_DROPDB
Definition: pgstat.h:64
@ PGSTAT_MTYPE_SLRU
Definition: pgstat.h:77
@ PGSTAT_MTYPE_RESETSINGLECOUNTER
Definition: pgstat.h:67
@ PGSTAT_MTYPE_FUNCSTAT
Definition: pgstat.h:78
@ PGSTAT_MTYPE_CHECKPOINTER
Definition: pgstat.h:75
@ PGSTAT_MTYPE_CHECKSUMFAILURE
Definition: pgstat.h:83
@ PGSTAT_MTYPE_TABPURGE
Definition: pgstat.h:63
@ PGSTAT_MTYPE_CONNECT
Definition: pgstat.h:85
@ PGSTAT_MTYPE_DUMMY
Definition: pgstat.h:60
@ PGSTAT_MTYPE_SUBSCRIPTIONPURGE
Definition: pgstat.h:87
@ PGSTAT_MTYPE_ARCHIVER
Definition: pgstat.h:73
@ PGSTAT_MTYPE_VACUUM
Definition: pgstat.h:71
@ PGSTAT_MTYPE_INQUIRY
Definition: pgstat.h:61
@ PGSTAT_MTYPE_RECOVERYCONFLICT
Definition: pgstat.h:80
@ PGSTAT_MTYPE_WAL
Definition: pgstat.h:76
@ PGSTAT_MTYPE_FUNCPURGE
Definition: pgstat.h:79
@ PGSTAT_MTYPE_ANALYZE
Definition: pgstat.h:72
@ PGSTAT_MTYPE_RESETSHAREDCOUNTER
Definition: pgstat.h:66
@ PGSTAT_MTYPE_RESETREPLSLOTCOUNTER
Definition: pgstat.h:69
@ PGSTAT_MTYPE_RESETCOUNTER
Definition: pgstat.h:65
@ PGSTAT_MTYPE_SUBWORKERERROR
Definition: pgstat.h:88
@ PGSTAT_MTYPE_TEMPFILE
Definition: pgstat.h:81
@ PGSTAT_MTYPE_TABSTAT
Definition: pgstat.h:62
@ PGSTAT_MTYPE_BGWRITER
Definition: pgstat.h:74
@ PGSTAT_MTYPE_RESETSLRUCOUNTER
Definition: pgstat.h:68
@ PGSTAT_MTYPE_DEADLOCK
Definition: pgstat.h:82
@ PGSTAT_MTYPE_REPLSLOT
Definition: pgstat.h:84

◆ TrackFunctionsLevel

Enumerator
TRACK_FUNC_OFF 
TRACK_FUNC_PL 
TRACK_FUNC_ALL 

Definition at line 37 of file pgstat.h.

38 {
TrackFunctionsLevel
Definition: pgstat.h:38
@ TRACK_FUNC_PL
Definition: pgstat.h:40
@ TRACK_FUNC_ALL
Definition: pgstat.h:41
@ TRACK_FUNC_OFF
Definition: pgstat.h:39

Function Documentation

◆ allow_immediate_pgstat_restart()

void allow_immediate_pgstat_restart ( void  )

Definition at line 835 of file pgstat.c.

842 {

Referenced by HandleChildCrash().

◆ AtEOSubXact_PgStat()

void AtEOSubXact_PgStat ( bool  isCommit,
int  nestDepth 
)

Definition at line 2694 of file pgstat.c.

2701 {
2702  PgStat_SubXactStatus *xact_state;
2703 
2704  /* merge the sub-transaction's transactional stats into the parent */
2705  xact_state = pgStatXactStack;
2706  if (xact_state != NULL &&
2707  xact_state->nest_level >= nestDepth)
2708  {
2709  /* delink xact_state from stack immediately to simplify reuse case */
2710  pgStatXactStack = xact_state->prev;
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:239
struct PgStat_SubXactStatus * prev
Definition: pgstat.c:235

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

Referenced by AbortSubTransaction(), and CommitSubTransaction().

◆ AtEOXact_PgStat()

void AtEOXact_PgStat ( bool  isCommit,
bool  parallel 
)

Definition at line 2582 of file pgstat.c.

2589 {
2590  PgStat_SubXactStatus *xact_state;
2591 
2592  AtEOXact_PgStat_Database(isCommit, parallel);
2593 
2594  /* handle transactional stats information */
2595  xact_state = pgStatXactStack;
2596  if (xact_state != NULL)
2597  {
2598  Assert(xact_state->nest_level == 1);
2599  Assert(xact_state->prev == NULL);
2600 
2601  AtEOXact_PgStat_Relations(xact_state, isCommit);
Assert(fmt[strlen(fmt) - 1] !='\n')
static void AtEOXact_PgStat_Database(bool isCommit, bool parallel)
Definition: pgstat.c:2559
static void AtEOXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit)
Definition: pgstat.c:2508

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

◆ AtPrepare_PgStat()

void AtPrepare_PgStat ( void  )

Definition at line 2751 of file pgstat.c.

2758 {
2759  PgStat_SubXactStatus *xact_state;
2760 
2761  xact_state = pgStatXactStack;
2762  if (xact_state != NULL)
2763  {

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

Referenced by PrepareTransaction().

◆ find_funcstat_entry()

PgStat_BackendFunctionEntry* find_funcstat_entry ( Oid  func_id)

Definition at line 2083 of file pgstat.c.

2090 {
2092 
2093  if (pgStatFunctions == NULL)
static HTAB * pgStatFunctions
Definition: pgstat.c:217
static void pgstat_assert_is_up(void)
Definition: pgstat.c:5132

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

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

◆ find_tabstat_entry()

PgStat_TableStatus* find_tabstat_entry ( Oid  rel_id)

Definition at line 2289 of file pgstat.c.

2290  : if we got an error in the most recent execution of pgstat_report_stat,
2291  * it's possible that an entry exists but there's no hashtable entry for it.
2292  * That's okay, we'll treat this case as "doesn't exist".
2293  */
2295 find_tabstat_entry(Oid rel_id)
2296 {
2297  TabStatHashEntry *hash_entry;
2298 
2299  /* If hashtable doesn't exist, there are no entries at all */
2300  if (!pgStatTabHash)
2301  return NULL;
2302 
2303  hash_entry = hash_search(pgStatTabHash, &rel_id, HASH_FIND, NULL);
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
@ HASH_FIND
Definition: hsearch.h:113
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
Definition: pgstat.c:2289
static HTAB * pgStatTabHash
Definition: pgstat.c:211
void pgstat_report_stat(bool disconnect)
Definition: pgstat.c:861
static void error(void)
Definition: sql-dyntest.c:147

References HASH_FIND, hash_search(), pgStatTabHash, 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().

◆ pgstat_clear_snapshot()

void pgstat_clear_snapshot ( void  )

Definition at line 5149 of file pgstat.c.

5156 {
5158 
5159  /* Release memory, if any was allocated */
5160  if (pgStatLocalContext)
5162 
5163  /* Reset variables */
5164  pgStatLocalContext = NULL;
5165  pgStatDBHash = NULL;
5166  replSlotStatHash = NULL;
5167 
5168  /*
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
static HTAB * pgStatDBHash
Definition: pgstat.c:269
static HTAB * replSlotStatHash
Definition: pgstat.c:280
static MemoryContext pgStatLocalContext
Definition: pgstat.c:268

References MemoryContextDelete(), pgstat_assert_is_up(), pgstat_clear_backend_activity_snapshot(), pgStatDBHash, pgStatLocalContext, and replSlotStatHash.

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

◆ pgstat_count_heap_delete()

void pgstat_count_heap_delete ( Relation  rel)

Definition at line 2404 of file pgstat.c.

2411 {
2412  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2413 
2414  if (pgstat_info != NULL)
2415  {
2416  /* We have to log the effect at the proper transactional level */
2417  int nest_level = GetCurrentTransactionNestLevel();
2418 
2419  if (pgstat_info->trans == NULL ||
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:180
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:250
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:909

Referenced by heap_abort_speculative(), and heap_delete().

◆ pgstat_count_heap_insert()

void pgstat_count_heap_insert ( Relation  rel,
PgStat_Counter  n 
)

Definition at line 2358 of file pgstat.c.

2365 {
2366  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2367 
2368  if (pgstat_info != NULL)
2369  {
2370  /* We have to log the effect at the proper transactional level */
2371  int nest_level = GetCurrentTransactionNestLevel();
2372 
2373  if (pgstat_info->trans == NULL ||

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

◆ pgstat_count_heap_update()

void pgstat_count_heap_update ( Relation  rel,
bool  hot 
)

Definition at line 2379 of file pgstat.c.

2386 {
2387  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2388 
2389  if (pgstat_info != NULL)
2390  {
2391  /* We have to log the effect at the proper transactional level */
2392  int nest_level = GetCurrentTransactionNestLevel();
2393 
2394  if (pgstat_info->trans == NULL ||
2395  pgstat_info->trans->nest_level != nest_level)
2396  add_tabstat_xact_level(pgstat_info, nest_level);
2397 
2398  pgstat_info->trans->tuples_updated++;
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
Definition: pgstat.c:2331
PgStat_Counter tuples_updated
Definition: pgstat.h:191

Referenced by heap_update().

◆ pgstat_count_slru_flush()

void pgstat_count_slru_flush ( int  slru_idx)

Definition at line 6405 of file pgstat.c.

6406 {
6407  slru_entry(slru_idx)->m_blocks_written += 1;
6408 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:6355
PgStat_Counter m_blocks_written
Definition: pgstat.h:517

References PgStat_MsgSLRU::m_blocks_written, and slru_entry().

Referenced by SimpleLruWriteAll().

◆ pgstat_count_slru_page_exists()

void pgstat_count_slru_page_exists ( int  slru_idx)

Definition at line 6387 of file pgstat.c.

6388 {
6389  slru_entry(slru_idx)->m_blocks_hit += 1;
6390 }
PgStat_Counter m_blocks_hit
Definition: pgstat.h:515

References PgStat_MsgSLRU::m_blocks_hit, and slru_entry().

Referenced by SimpleLruDoesPhysicalPageExist().

◆ pgstat_count_slru_page_hit()

void pgstat_count_slru_page_hit ( int  slru_idx)

Definition at line 6381 of file pgstat.c.

6382 {
6383  slru_entry(slru_idx)->m_blocks_zeroed += 1;
6384 }
PgStat_Counter m_blocks_zeroed
Definition: pgstat.h:514

References PgStat_MsgSLRU::m_blocks_zeroed, and slru_entry().

Referenced by SimpleLruReadPage(), and SimpleLruReadPage_ReadOnly().

◆ pgstat_count_slru_page_read()

void pgstat_count_slru_page_read ( int  slru_idx)

Definition at line 6393 of file pgstat.c.

6394 {
6395  slru_entry(slru_idx)->m_blocks_exists += 1;
6396 }
PgStat_Counter m_blocks_exists
Definition: pgstat.h:518

References PgStat_MsgSLRU::m_blocks_exists, and slru_entry().

Referenced by SimpleLruReadPage().

◆ pgstat_count_slru_page_written()

void pgstat_count_slru_page_written ( int  slru_idx)

Definition at line 6399 of file pgstat.c.

6400 {
6401  slru_entry(slru_idx)->m_blocks_read += 1;
6402 }
PgStat_Counter m_blocks_read
Definition: pgstat.h:516

References PgStat_MsgSLRU::m_blocks_read, and slru_entry().

Referenced by SlruPhysicalWritePage().

◆ pgstat_count_slru_page_zeroed()

void pgstat_count_slru_page_zeroed ( int  slru_idx)

Definition at line 6375 of file pgstat.c.

6382 {

Referenced by SimpleLruZeroPage().

◆ pgstat_count_slru_truncate()

void pgstat_count_slru_truncate ( int  slru_idx)

Definition at line 6411 of file pgstat.c.

6412 {
6413  slru_entry(slru_idx)->m_flush += 1;
6414 }
PgStat_Counter m_flush
Definition: pgstat.h:519

References PgStat_MsgSLRU::m_flush, and slru_entry().

Referenced by SimpleLruTruncate().

◆ pgstat_count_truncate()

void pgstat_count_truncate ( Relation  rel)

Definition at line 2462 of file pgstat.c.

2469 {
2470  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2471 
2472  if (pgstat_info != NULL)
2473  {
2474  /* We have to log the effect at the proper transactional level */
2475  int nest_level = GetCurrentTransactionNestLevel();
2476 
2477  if (pgstat_info->trans == NULL ||
2478  pgstat_info->trans->nest_level != nest_level)
2479  add_tabstat_xact_level(pgstat_info, nest_level);
2480 

Referenced by ExecRefreshMatView(), and ExecuteTruncateGuts().

◆ pgstat_drop_database()

void pgstat_drop_database ( Oid  databaseid)

Definition at line 1438 of file pgstat.c.

1445 {
1446  PgStat_MsgDropdb msg;
1447 
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:169
#define PGINVALID_SOCKET
Definition: port.h:31

References PgStat_MsgDropdb::m_databaseid, PgStat_MsgDropdb::m_hdr, PGINVALID_SOCKET, PGSTAT_MTYPE_DROPDB, pgstat_send(), pgstat_setheader(), and pgStatSock.

Referenced by dropdb(), and pgstat_vacuum_stat().

◆ pgstat_end_function_usage()

void pgstat_end_function_usage ( PgStat_FunctionCallUsage fcu,
bool  finalize 
)

Definition at line 2105 of file pgstat.c.

2112 {
2113  PgStat_FunctionCounts *fs = fcu->fs;
2114  instr_time f_total;
2115  instr_time f_others;
2116  instr_time f_self;
2117 
2118  /* stats not wanted? */
2119  if (fs == NULL)
2120  return;
2121 
2122  /* total elapsed time in this function call */
2123  INSTR_TIME_SET_CURRENT(f_total);
2124  INSTR_TIME_SUBTRACT(f_total, fcu->f_start);
2125 
2126  /* self usage: elapsed minus anything already charged to other calls */
2127  f_others = total_func_time;
2128  INSTR_TIME_SUBTRACT(f_others, fcu->save_total);
2129  f_self = f_total;
2130  INSTR_TIME_SUBTRACT(f_self, f_others);
2131 
2132  /* update backend-wide total time */
2134 
2135  /*
2136  * Compute the new f_total_time as the total elapsed time added to the
2137  * pre-call value of f_total_time. This is necessary to avoid
2138  * double-counting any time taken by recursive calls of myself. (We do
2139  * not need any similar kluge for self time, since that already excludes
2140  * any recursive calls.)
2141  */
2142  INSTR_TIME_ADD(f_total, fcu->save_f_total_time);
2143 
2144  /* update counters in function stats table */
2145  if (finalize)
2146  fs->f_numcalls++;
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
#define INSTR_TIME_ADD(x, y)
Definition: instr_time.h:158
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
struct timeval instr_time
Definition: instr_time.h:150
static instr_time total_func_time
Definition: pgstat.c:294
instr_time save_total
Definition: pgstat.h:1036
PgStat_FunctionCounts * fs
Definition: pgstat.h:1032
instr_time save_f_total_time
Definition: pgstat.h:1034
PgStat_Counter f_numcalls
Definition: pgstat.h:629

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

◆ pgstat_fetch_global()

PgStat_GlobalStats* pgstat_fetch_global ( void  )

Definition at line 3082 of file pgstat.c.

3089 {

References backend_read_statsfile(), and globalStats.

Referenced by pg_stat_get_snapshot_timestamp().

◆ pgstat_fetch_replslot()

PgStat_StatReplSlotEntry* pgstat_fetch_replslot ( NameData  slotname)

Definition at line 3130 of file pgstat.c.

3137 {

References backend_read_statsfile(), and pgstat_get_replslot_entry().

Referenced by pg_stat_get_replication_slot().

◆ pgstat_fetch_slru()

PgStat_SLRUStats* pgstat_fetch_slru ( void  )

Definition at line 3114 of file pgstat.c.

3121 {

References backend_read_statsfile(), and slruStats.

Referenced by pg_stat_get_slru().

◆ pgstat_fetch_stat_archiver()

PgStat_ArchiverStats* pgstat_fetch_stat_archiver ( void  )

Definition at line 3034 of file pgstat.c.

3041 {

References archiverStats, and backend_read_statsfile().

Referenced by pg_stat_get_archiver().

◆ pgstat_fetch_stat_bgwriter()

◆ pgstat_fetch_stat_checkpointer()

◆ pgstat_fetch_stat_dbentry()

PgStat_StatDBEntry* pgstat_fetch_stat_dbentry ( Oid  dbid)

Definition at line 2892 of file pgstat.c.

2899 {
2900  /*
2901  * If not done for this transaction, read the statistics collector stats
2902  * file into some hash tables.
2903  */
2905 
2906  /*
static void backend_read_statsfile(void)
Definition: pgstat.c:4981

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

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(), pgstat_fetch_stat_subworker_entry(), rebuild_database_list(), and recheck_relation_needs_vacanalyze().

◆ pgstat_fetch_stat_funcentry()

PgStat_StatFuncEntry* pgstat_fetch_stat_funcentry ( Oid  funcid)

Definition at line 2975 of file pgstat.c.

2982 {
2983  PgStat_StatDBEntry *dbentry;
2984  PgStat_StatFuncEntry *funcentry = NULL;
2985 
2986  /* load the stats file if needed */
2988 
2989  /* Lookup our database, then find the requested function. */
2991  if (dbentry != NULL && dbentry->functions != NULL)
2992  {
2993  funcentry = (PgStat_StatFuncEntry *) hash_search(dbentry->functions,
Oid MyDatabaseId
Definition: globals.c:88
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2892
HTAB * functions
Definition: pgstat.h:834

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

◆ pgstat_fetch_stat_subworker_entry()

PgStat_StatSubWorkerEntry* pgstat_fetch_stat_subworker_entry ( Oid  subid,
Oid  subrelid 
)

Definition at line 3004 of file pgstat.c.

3011 {
3012  PgStat_StatDBEntry *dbentry;
3013  PgStat_StatSubWorkerEntry *wentry = NULL;
3014 
3015  /* Load the stats file if needed */
3017 
3018  /*
3019  * Lookup our database, then find the requested subscription worker stats.
3020  */
3022  if (dbentry != NULL && dbentry->subworkers != NULL)
3023  {
HTAB * subworkers
Definition: pgstat.h:835

References backend_read_statsfile(), MyDatabaseId, pgstat_fetch_stat_dbentry(), pgstat_get_subworker_entry(), and PgStat_StatDBEntry::subworkers.

Referenced by pg_stat_get_subscription_worker().

◆ pgstat_fetch_stat_tabentry()

PgStat_StatTabEntry* pgstat_fetch_stat_tabentry ( Oid  relid)

Definition at line 2919 of file pgstat.c.

2926 {
2927  Oid dbid;
2928  PgStat_StatDBEntry *dbentry;
2929  PgStat_StatTabEntry *tabentry;
2930 
2931  /*
2932  * If not done for this transaction, read the statistics collector stats
2933  * file into some hash tables.
2934  */
2936 
2937  /*
2938  * Lookup our database, then look in its table hash table.
2939  */
2940  dbid = MyDatabaseId;
2942  (void *) &dbid,
2943  HASH_FIND, NULL);
2944  if (dbentry != NULL && dbentry->tables != NULL)
2945  {
2946  tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2947  (void *) &relid,
2948  HASH_FIND, NULL);
2949  if (tabentry)
2950  return tabentry;
2951  }
2952 
2953  /*
2954  * If we didn't find it, maybe it's a shared table.
2955  */
2956  dbid = InvalidOid;
2958  (void *) &dbid,
2959  HASH_FIND, NULL);
2960  if (dbentry != NULL && dbentry->tables != NULL)
2961  {
2962  tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2963  (void *) &relid,
2964  HASH_FIND, NULL);
#define InvalidOid
Definition: postgres_ext.h:36

References backend_read_statsfile(), HASH_FIND, hash_search(), InvalidOid, MyDatabaseId, pgStatDBHash, 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().

◆ pgstat_fetch_stat_wal()

PgStat_WalStats* pgstat_fetch_stat_wal ( void  )

Definition at line 3098 of file pgstat.c.

3105 {

References backend_read_statsfile(), and walStats.

Referenced by pg_stat_get_wal().

◆ pgstat_init()

void pgstat_init ( void  )

Definition at line 395 of file pgstat.c.

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

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

◆ pgstat_init_function_usage()

void pgstat_init_function_usage ( struct FunctionCallInfoBaseData fcinfo,
PgStat_FunctionCallUsage fcu 
)

◆ pgstat_initialize()

void pgstat_initialize ( void  )

Definition at line 3173 of file pgstat.c.

3175  : MyDatabaseId isn't set yet; so the shutdown hook has to be careful.
3176  * ----------
3177  */
3178 void
3179 pgstat_initialize(void)
3180 {
3181  Assert(!pgstat_is_initialized);
3182 
3183  /*
3184  * Initialize prevWalUsage with pgWalUsage so that pgstat_send_wal() can
3185  * calculate how much pgWalUsage counters are increased by subtracting
3186  * prevWalUsage from pgWalUsage.
3187  */
3188  prevWalUsage = pgWalUsage;
3189 
3190  /* Set up a process-exit hook to clean up */

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

Referenced by BaseInit().

◆ pgstat_initstats()

void pgstat_initstats ( Relation  rel)

Definition at line 2162 of file pgstat.c.

2169 {
2170  Oid rel_id = rel->rd_id;
2171  char relkind = rel->rd_rel->relkind;
2172 
2173  /*
2174  * We only count stats for relations with storage and partitioned tables
2175  */
2176  if (!RELKIND_HAS_STORAGE(relkind) && relkind != RELKIND_PARTITIONED_TABLE)
2177  {
2178  rel->pgstat_info = NULL;
2179  return;
2180  }
2181 
2183  {
2184  /* We're not counting at all */
2185  rel->pgstat_info = NULL;
2186  return;
2187  }
2188 
2189  /*
2190  * If we already set up this relation in the current transaction, nothing
2191  * to do.
2192  */
2193  if (rel->pgstat_info != NULL &&
bool pgstat_track_counts
Definition: pgstat.c:111
Oid rd_id
Definition: rel.h:111
Form_pg_class rd_rel
Definition: rel.h:109

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

◆ pgstat_ping()

void pgstat_ping ( void  )

◆ pgstat_report_analyze()

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

Definition at line 1679 of file pgstat.c.

1688 {
1689  PgStat_MsgAnalyze msg;
1690 
1692  return;
1693 
1694  /*
1695  * Unlike VACUUM, ANALYZE might be running inside a transaction that has
1696  * already inserted and/or deleted rows in the target table. ANALYZE will
1697  * have counted such rows as live or dead respectively. Because we will
1698  * report our counts of such rows at transaction end, we should subtract
1699  * off these counts from what we send to the collector now, else they'll
1700  * be double-counted after commit. (This approach also ensures that the
1701  * collector ends up with the right numbers if we abort instead of
1702  * committing.)
1703  *
1704  * Waste no time on partitioned tables, though.
1705  */
1706  if (rel->pgstat_info != NULL &&
1707  rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
1708  {
1710 
1711  for (trans = rel->pgstat_info->trans; trans; trans = trans->upper)
1712  {
1713  livetuples -= trans->tuples_inserted - trans->tuples_deleted;
1714  deadtuples -= trans->tuples_updated + trans->tuples_deleted;
1715  }
1716  /* count stuff inserted by already-aborted subxacts, too */
1717  deadtuples -= rel->pgstat_info->t_counts.t_delta_dead_tuples;
1718  /* Since ANALYZE's counts are estimates, we could have underflowed */
1719  livetuples = Max(livetuples, 0);
1720  deadtuples = Max(deadtuples, 0);
1721  }
1722 
1724  msg.m_databaseid = rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId;
1725  msg.m_tableoid = RelationGetRelid(rel);
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3411
#define Max(x, y)
Definition: c.h:980
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3205
#define RelationGetRelid(relation)
Definition: rel.h:478
bool m_autovacuum
Definition: pgstat.h:437
PgStat_MsgHdr m_hdr
Definition: pgstat.h:434
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:132
PgStat_TableCounts t_counts
Definition: pgstat.h:181
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:400

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, and trans.

Referenced by do_analyze_rel().

◆ pgstat_report_autovac()

◆ pgstat_report_checksum_failure()

void pgstat_report_checksum_failure ( void  )

Definition at line 1798 of file pgstat.c.

1805 {

References MyDatabaseId, and pgstat_report_checksum_failures_in_db().

Referenced by PageIsVerifiedExtended().

◆ pgstat_report_checksum_failures_in_db()

◆ pgstat_report_connect()

void pgstat_report_connect ( Oid  dboid)

◆ pgstat_report_deadlock()

◆ pgstat_report_recovery_conflict()

◆ pgstat_report_replslot()

void pgstat_report_replslot ( const PgStat_StatReplSlotEntry repSlotStat)

Definition at line 1887 of file pgstat.c.

1894 {
1895  PgStat_MsgReplSlot msg;
1896 
1897  /*
1898  * Prepare and send the message
1899  */
1901  namestrcpy(&msg.m_slotname, NameStr(repSlotStat->slotname));
1902  msg.m_create = false;
1903  msg.m_drop = false;
1904  msg.m_spill_txns = repSlotStat->spill_txns;
1905  msg.m_spill_count = repSlotStat->spill_count;
1906  msg.m_spill_bytes = repSlotStat->spill_bytes;
1907  msg.m_stream_txns = repSlotStat->stream_txns;
#define NameStr(name)
Definition: c.h:681
void namestrcpy(Name name, const char *str)
Definition: name.c:233
PgStat_MsgHdr m_hdr
Definition: pgstat.h:530
PgStat_Counter m_spill_txns
Definition: pgstat.h:534
PgStat_Counter m_spill_bytes
Definition: pgstat.h:536
PgStat_Counter m_spill_count
Definition: pgstat.h:535
NameData m_slotname
Definition: pgstat.h:531
PgStat_Counter m_stream_txns
Definition: pgstat.h:537
PgStat_Counter spill_txns
Definition: pgstat.h:981
PgStat_Counter stream_txns
Definition: pgstat.h:984
PgStat_Counter spill_count
Definition: pgstat.h:982
PgStat_Counter spill_bytes
Definition: pgstat.h:983

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

◆ pgstat_report_replslot_create()

void pgstat_report_replslot_create ( const char *  slotname)

◆ pgstat_report_replslot_drop()

void pgstat_report_replslot_drop ( const char *  slotname)

◆ pgstat_report_stat()

void pgstat_report_stat ( bool  force)

Definition at line 861 of file pgstat.c.

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

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, pgStatTabHash, pgStatTabList, pgStatXactCommit, pgStatXactRollback, pgWalUsage, prevWalUsage, 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, WalUsage::wal_records, and WalStats.

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

◆ pgstat_report_subscription_drop()

◆ pgstat_report_subworker_error()

void pgstat_report_subworker_error ( Oid  subid,
Oid  subrelid,
Oid  relid,
LogicalRepMsgType  command,
TransactionId  xid,
const char *  errmsg 
)

Definition at line 1952 of file pgstat.c.

1961 {
1963  int len;
1964 
1966  msg.m_databaseid = MyDatabaseId;
1967  msg.m_subid = subid;
1968  msg.m_subrelid = subrelid;
1969  msg.m_relid = relid;
1970  msg.m_command = command;
1971  msg.m_xid = xid;
const void size_t len
LogicalRepMsgType m_command
Definition: pgstat.h:587
TransactionId m_xid
Definition: pgstat.h:588
PgStat_MsgHdr m_hdr
Definition: pgstat.h:569

References errmsg(), GetCurrentTimestamp(), len, PgStat_MsgSubWorkerError::m_command, PgStat_MsgSubWorkerError::m_databaseid, PgStat_MsgSubWorkerError::m_hdr, PgStat_MsgSubWorkerError::m_message, PgStat_MsgSubWorkerError::m_relid, PgStat_MsgSubWorkerError::m_subid, PgStat_MsgSubWorkerError::m_subrelid, PgStat_MsgSubWorkerError::m_timestamp, PgStat_MsgSubWorkerError::m_xid, MyDatabaseId, offsetof, PGSTAT_MTYPE_SUBWORKERERROR, pgstat_send(), pgstat_setheader(), PGSTAT_SUBWORKERERROR_MSGLEN, and strlcpy().

Referenced by ApplyWorkerMain().

◆ pgstat_report_vacuum()

◆ pgstat_reset_all()

void pgstat_reset_all ( void  )

Definition at line 730 of file pgstat.c.

737 {

References pgstat_reset_remove_files(), pgstat_stat_directory, and PGSTAT_STAT_PERMANENT_DIRECTORY.

Referenced by StartupXLOG().

◆ pgstat_reset_counters()

◆ pgstat_reset_replslot_counter()

void pgstat_reset_replslot_counter ( const char *  name)

◆ pgstat_reset_shared_counters()

void pgstat_reset_shared_counters ( const char *  target)

Definition at line 1515 of file pgstat.c.

1522 {
1524 
1526  return;
1527 
1528  if (strcmp(target, "archiver") == 0)
1530  else if (strcmp(target, "bgwriter") == 0)
1532  else if (strcmp(target, "wal") == 0)
1533  msg.m_resettarget = RESET_WAL;
1534  else
1535  ereport(ERROR,
1536  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
#define ERROR
Definition: elog.h:33
PgStat_Shared_Reset_Target m_resettarget
Definition: pgstat.h:357

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

◆ pgstat_reset_single_counter()

◆ pgstat_reset_slru_counter()

void pgstat_reset_slru_counter ( const char *  name)

◆ pgstat_send_archiver()

void pgstat_send_archiver ( const char *  xlog,
bool  failed 
)

◆ pgstat_send_bgwriter()

void pgstat_send_bgwriter ( void  )

Definition at line 3271 of file pgstat.c.

3278 {
3279  /* We assume this initializes to zeroes */
3280  static const PgStat_MsgBgWriter all_zeroes;
3281 
3283 
3284  /*
3285  * This function can be called even if nothing at all has happened. In
3286  * this case, avoid sending a completely empty message to the stats
3287  * collector.
3288  */
3289  if (memcmp(&PendingBgWriterStats, &all_zeroes, sizeof(PgStat_MsgBgWriter)) == 0)
3290  return;
3291 
3292  /*
3293  * Prepare and send the message
3294  */
PgStat_MsgBgWriter PendingBgWriterStats
Definition: pgstat.c:127
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3218
PgStat_MsgHdr m_hdr
Definition: pgstat.h:463

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

Referenced by BackgroundWriterMain().

◆ pgstat_send_checkpointer()

void pgstat_send_checkpointer ( void  )

Definition at line 3305 of file pgstat.c.

3312 {
3313  /* We assume this initializes to zeroes */
3314  static const PgStat_MsgCheckpointer all_zeroes;
3315 
3316  /*
3317  * This function can be called even if nothing at all has happened. In
3318  * this case, avoid sending a completely empty message to the stats
3319  * collector.
3320  */
3321  if (memcmp(&PendingCheckpointerStats, &all_zeroes, sizeof(PgStat_MsgCheckpointer)) == 0)
3322  return;
3323 
3324  /*
3325  * Prepare and send the message
3326  */
PgStat_MsgCheckpointer PendingCheckpointerStats
Definition: pgstat.c:128
PgStat_MsgHdr m_hdr
Definition: pgstat.h:476

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

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

◆ pgstat_send_wal()

void pgstat_send_wal ( bool  force)

Definition at line 3340 of file pgstat.c.

3347 {
3348  static TimestampTz sendTime = 0;
3349 
3350  /*
3351  * This function can be called even if nothing at all has happened. In
3352  * this case, avoid sending a completely empty message to the stats
3353  * collector.
3354  *
3355  * Check wal_records counter to determine whether any WAL activity has
3356  * happened since last time. Note that other WalUsage counters don't need
3357  * to be checked because they are incremented always together with
3358  * wal_records counter.
3359  *
3360  * m_wal_buffers_full also doesn't need to be checked because it's
3361  * incremented only when at least one WAL record is generated (i.e.,
3362  * wal_records counter is incremented). But for safely, we assert that
3363  * m_wal_buffers_full is always zero when no WAL record is generated
3364  *
3365  * This function can be called by a process like walwriter that normally
3366  * generates no WAL records. To determine whether any WAL activity has
3367  * happened at that process since the last time, the numbers of WAL writes
3368  * and syncs are also checked.
3369  */
3371  WalStats.m_wal_write == 0 && WalStats.m_wal_sync == 0)
3372  {
3374  return;
3375  }
3376 
3377  if (!force)
3378  {
3380 
3381  /*
3382  * Don't send a message unless it's been at least PGSTAT_STAT_INTERVAL
3383  * msec since we last sent one to avoid overloading the stats
3384  * collector.
3385  */
3387  return;
3388  sendTime = now;
3389  }
3390 
3391  /*
3392  * Set the counters related to generated WAL data if the counters were
3393  * updated.
3394  */
3396  {
3397  WalUsage walusage;
3398 
3399  /*
3400  * Calculate how much WAL usage counters were increased by
3401  * subtracting the previous counters from the current ones. Fill the
3402  * results in WAL stats message.
3403  */
3404  MemSet(&walusage, 0, sizeof(WalUsage));
3405  WalUsageAccumDiff(&walusage, &pgWalUsage, &prevWalUsage);
3406 
3407  WalStats.m_wal_records = walusage.wal_records;
3408  WalStats.m_wal_fpi = walusage.wal_fpi;
3409  WalStats.m_wal_bytes = walusage.wal_bytes;
3410 
3411  /*
3412  * Save the current counters for the subsequent calculation of WAL
3413  * usage.
3414  */
3416  }
3417 
3418  /*
3419  * Prepare and send the message
3420  */
3422  pgstat_send(&WalStats, sizeof(WalStats));
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
Definition: instrument.c:274
PgStat_Counter m_wal_buffers_full
Definition: pgstat.h:497
PgStat_MsgHdr m_hdr
Definition: pgstat.h:493
PgStat_Counter m_wal_fpi
Definition: pgstat.h:495
PgStat_Counter m_wal_records
Definition: pgstat.h:494
uint64 m_wal_bytes
Definition: pgstat.h:496
uint64 wal_bytes
Definition: instrument.h:51
int64 wal_fpi
Definition: instrument.h:50

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, prevWalUsage, TimestampDifferenceExceeds(), WalUsage::wal_bytes, WalUsage::wal_fpi, WalUsage::wal_records, WalStats, and WalUsageAccumDiff().

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

◆ pgstat_slru_index()

int pgstat_slru_index ( const char *  name)

Definition at line 6318 of file pgstat.c.

6325 {
6326  int i;
6327 
6328  for (i = 0; i < SLRU_NUM_ELEMENTS; i++)
6329  {
6330  if (strcmp(slru_names[i], name) == 0)
static const char *const slru_names[]
Definition: pgstat.c:144
#define SLRU_NUM_ELEMENTS
Definition: pgstat.c:155

References i, name, slru_names, and SLRU_NUM_ELEMENTS.

Referenced by pgstat_reset_slru_counter(), and SimpleLruInit().

◆ pgstat_slru_name()

const char* pgstat_slru_name ( int  slru_idx)

Definition at line 6340 of file pgstat.c.

6347 {

References slru_names, and SLRU_NUM_ELEMENTS.

Referenced by pg_stat_get_slru().

◆ pgstat_start()

int pgstat_start ( void  )

Definition at line 772 of file pgstat.c.

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

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

◆ pgstat_twophase_postabort()

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

Definition at line 2858 of file pgstat.c.

2866 {
2867  TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
2868  PgStat_TableStatus *pgstat_info;
2869 
2870  /* Find or create a tabstat entry for the rel */
2871  pgstat_info = get_tabstat_entry(rec->t_id, rec->t_shared);
2872 
2873  /* Same math as in AtEOXact_PgStat, abort case */
2874  if (rec->t_truncdropped)
2875  {
2879  }
static PgStat_TableStatus * get_tabstat_entry(Oid rel_id, bool isshared)
Definition: pgstat.c:2199
PgStat_Counter deleted_pre_truncdrop
Definition: pgstat.c:259
PgStat_Counter inserted_pre_truncdrop
Definition: pgstat.c:257
PgStat_Counter updated_pre_truncdrop
Definition: pgstat.c:258
PgStat_Counter tuples_deleted
Definition: pgstat.c:255
PgStat_Counter tuples_inserted
Definition: pgstat.c:253
PgStat_Counter tuples_updated
Definition: pgstat.c:254

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.

◆ pgstat_twophase_postcommit()

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

Definition at line 2822 of file pgstat.c.

2830 {
2831  TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
2832  PgStat_TableStatus *pgstat_info;
2833 
2834  /* Find or create a tabstat entry for the rel */
2835  pgstat_info = get_tabstat_entry(rec->t_id, rec->t_shared);
2836 
2837  /* Same math as in AtEOXact_PgStat, commit case */
2838  pgstat_info->t_counts.t_tuples_inserted += rec->tuples_inserted;
2839  pgstat_info->t_counts.t_tuples_updated += rec->tuples_updated;
2840  pgstat_info->t_counts.t_tuples_deleted += rec->tuples_deleted;
2841  pgstat_info->t_counts.t_truncdropped = rec->t_truncdropped;
2842  if (rec->t_truncdropped)
2843  {
2844  /* forget live/dead stats seen by backend thus far */
2845  pgstat_info->t_counts.t_delta_live_tuples = 0;
2846  pgstat_info->t_counts.t_delta_dead_tuples = 0;
2847  }
2848  pgstat_info->t_counts.t_delta_live_tuples +=
2849  rec->tuples_inserted - rec->tuples_deleted;
PgStat_Counter t_delta_live_tuples
Definition: pgstat.h:131
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:125
PgStat_Counter t_tuples_updated
Definition: pgstat.h:126
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:127
bool t_truncdropped
Definition: pgstat.h:129

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, TwoPhasePgStatRecord::t_truncdropped, PgStat_TableCounts::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.

◆ pgstat_update_heap_dead_tuples()

void pgstat_update_heap_dead_tuples ( Relation  rel,
int  delta 
)

◆ pgstat_vacuum_stat()

void pgstat_vacuum_stat ( void  )

Definition at line 1121 of file pgstat.c.

1128 {
1129  HTAB *htab;
1130  PgStat_MsgTabpurge msg;
1131  PgStat_MsgFuncpurge f_msg;
1132  HASH_SEQ_STATUS hstat;
1133  PgStat_StatDBEntry *dbentry;
1134  PgStat_StatTabEntry *tabentry;
1135  PgStat_StatFuncEntry *funcentry;
1136  int len;
1137 
1139  return;
1140 
1141  /*
1142  * If not done for this transaction, read the statistics collector stats
1143  * file into some hash tables.
1144  */
1146 
1147  /*
1148  * Read pg_database and make a list of OIDs of all existing databases
1149  */
1150  htab = pgstat_collect_oids(DatabaseRelationId, Anum_pg_database_oid);
1151 
1152  /*
1153  * Search the database hash table for dead databases and tell the
1154  * collector to drop them.
1155  */
1156  hash_seq_init(&hstat, pgStatDBHash);
1157  while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
1158  {
1159  Oid dbid = dbentry->databaseid;
1160 
1162 
1163  /* the DB entry for shared tables (with InvalidOid) is never dropped */
1164  if (OidIsValid(dbid) &&
1165  hash_search(htab, (void *) &dbid, HASH_FIND, NULL) == NULL)
1166  pgstat_drop_database(dbid);
1167  }
1168 
1169  /* Clean up */
1170  hash_destroy(htab);
1171 
1172  /*
1173  * Search for all the dead replication slots in stats hashtable and tell
1174  * the stats collector to drop them.
1175  */
1176  if (replSlotStatHash)
1177  {
1178  PgStat_StatReplSlotEntry *slotentry;
1179 
1181  while ((slotentry = (PgStat_StatReplSlotEntry *) hash_seq_search(&hstat)) != NULL)
1182  {
1184 
1185  if (SearchNamedReplicationSlot(NameStr(slotentry->slotname), true) == NULL)
1187  }
1188  }
1189 
1190  /*
1191  * Lookup our own database entry; if not found, nothing more to do.
1192  */
1194  (void *) &MyDatabaseId,
1195  HASH_FIND, NULL);
1196  if (dbentry == NULL || dbentry->tables == NULL)
1197  return;
1198 
1199  /*
1200  * Similarly to above, make a list of all known relations in this DB.
1201  */
1202  htab = pgstat_collect_oids(RelationRelationId, Anum_pg_class_oid);
1203 
1204  /*
1205  * Initialize our messages table counter to zero
1206  */
1207  msg.m_nentries = 0;
1208 
1209  /*
1210  * Check for all tables listed in stats hashtable if they still exist.
1211  */
1212  hash_seq_init(&hstat, dbentry->tables);
1213  while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&hstat)) != NULL)
1214  {
1215  Oid tabid = tabentry->tableid;
1216 
1218 
1219  if (hash_search(htab, (void *) &tabid, HASH_FIND, NULL) != NULL)
1220  continue;
1221 
1222  /*
1223  * Not there, so add this table's Oid to the message
1224  */
1225  msg.m_tableid[msg.m_nentries++] = tabid;
1226 
1227  /*
1228  * If the message is full, send it out and reinitialize to empty
1229  */
1230  if (msg.m_nentries >= PGSTAT_NUM_TABPURGE)
1231  {
1232  len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
1233  + msg.m_nentries * sizeof(Oid);
1234 
1236  msg.m_databaseid = MyDatabaseId;
1237  pgstat_send(&msg, len);
1238 
1239  msg.m_nentries = 0;
1240  }
1241  }
1242 
1243  /*
1244  * Send the rest
1245  */
1246  if (msg.m_nentries > 0)
1247  {
1248  len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
1249  + msg.m_nentries * sizeof(Oid);
1250 
1252  msg.m_databaseid = MyDatabaseId;
1253  pgstat_send(&msg, len);
1254  }
1255 
1256  /* Clean up */
1257  hash_destroy(htab);
1258 
1259  /*
1260  * Now repeat the above steps for functions. However, we needn't bother
1261  * in the common case where no function stats are being collected.
1262  */
1263  if (dbentry->functions != NULL &&
1264  hash_get_num_entries(dbentry->functions) > 0)
1265  {
1266  htab = pgstat_collect_oids(ProcedureRelationId, Anum_pg_proc_oid);
1267 
1269  f_msg.m_databaseid = MyDatabaseId;
1270  f_msg.m_nentries = 0;
1271 
1272  hash_seq_init(&hstat, dbentry->functions);
1273  while ((funcentry = (PgStat_StatFuncEntry *) hash_seq_search(&hstat)) != NULL)
1274  {
1275  Oid funcid = funcentry->functionid;
1276 
1278 
1279  if (hash_search(htab, (void *) &funcid, HASH_FIND, NULL) != NULL)
1280  continue;
1281 
1282  /*
1283  * Not there, so add this function's Oid to the message
1284  */
1285  f_msg.m_functionid[f_msg.m_nentries++] = funcid;
1286 
1287  /*
1288  * If the message is full, send it out and reinitialize to empty
1289  */
1290  if (f_msg.m_nentries >= PGSTAT_NUM_FUNCPURGE)
1291  {
1292  len = offsetof(PgStat_MsgFuncpurge, m_functionid[0])
1293  + f_msg.m_nentries * sizeof(Oid);
1294 
1295  pgstat_send(&f_msg, len);
1296 
1297  f_msg.m_nentries = 0;
1298  }
1299  }
1300 
1301  /*
1302  * Send the rest
1303  */
1304  if (f_msg.m_nentries > 0)
1305  {
1306  len = offsetof(PgStat_MsgFuncpurge, m_functionid[0])
1307  + f_msg.m_nentries * sizeof(Oid);
1308 
1309  pgstat_send(&f_msg, len);
1310  }
1311 
1312  hash_destroy(htab);
1313  }
1314 
1315  /*
1316  * Repeat for subscription workers. Similarly, we needn't bother in the
1317  * common case where no subscription workers' stats are being collected.
1318  */
1319  if (dbentry->subworkers != NULL &&
1320  hash_get_num_entries(dbentry->subworkers) > 0)
1321  {
1322  PgStat_StatSubWorkerEntry *subwentry;
1324 
1325  /*
1326  * Read pg_subscription and make a list of OIDs of all existing
1327  * subscriptions
1328  */
1329  htab = pgstat_collect_oids(SubscriptionRelationId, Anum_pg_subscription_oid);
1330 
1331  spmsg.m_databaseid = MyDatabaseId;
1332  spmsg.m_nentries = 0;
1333 
1334  hash_seq_init(&hstat, dbentry->subworkers);
1335  while ((subwentry = (PgStat_StatSubWorkerEntry *) hash_seq_search(&hstat)) != NULL)
1336  {
1337  bool exists = false;
1338  Oid subid = subwentry->key.subid;
1339 
1341 
1342  if (hash_search(htab, (void *) &subid, HASH_FIND, NULL) != NULL)
1343  continue;
1344 
1345  /*
1346  * It is possible that we have multiple entries for the
1347  * subscription corresponding to apply worker and tablesync
1348  * workers. In such cases, we don't need to add the same subid
1349  * again.
1350  */
1351  for (int i = 0; i < spmsg.m_nentries; i++)
1352  {
1353  if (spmsg.m_subids[i] == subid)
1354  {
1355  exists = true;
1356  break;
1357  }
1358  }
1359 
1360  if (exists)
1361  continue;
1362 
1363  /* This subscription is dead, add the subid to the message */
1364  spmsg.m_subids[spmsg.m_nentries++] = subid;
1365 
1366  /*
1367  * If the message is full, send it out and reinitialize to empty
1368  */
1370  {
1372  spmsg.m_nentries = 0;
1373  }
1374  }
1375 
1376  /* Send the rest of dead subscriptions */
#define offsetof(type, field)
Definition: c.h:727
#define OidIsValid(objectId)
Definition: c.h:710
long hash_get_num_entries(HTAB *hashp)
Definition: dynahash.c:1382
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
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
void pgstat_report_replslot_drop(const char *slotname)
Definition: pgstat.c:1934
static HTAB * pgstat_collect_oids(Oid catalogid, AttrNumber anum_oid)
Definition: pgstat.c:1389
static void pgstat_send_subscription_purge(PgStat_MsgSubscriptionPurge *msg)
Definition: pgstat.c:3469
void pgstat_drop_database(Oid databaseid)
Definition: pgstat.c:1438
#define PGSTAT_NUM_TABPURGE
Definition: pgstat.h:313
#define PGSTAT_NUM_SUBSCRIPTIONPURGE
Definition: pgstat.h:549
#define PGSTAT_NUM_FUNCPURGE
Definition: pgstat.h:678
ReplicationSlot * SearchNamedReplicationSlot(const char *name, bool need_lock)
Definition: slot.c:348
Definition: dynahash.c:220
Oid m_functionid[PGSTAT_NUM_FUNCPURGE]
Definition: pgstat.h:687
PgStat_MsgHdr m_hdr
Definition: pgstat.h:684
Oid m_subids[PGSTAT_NUM_SUBSCRIPTIONPURGE]
Definition: pgstat.h:557
PgStat_MsgHdr m_hdr
Definition: pgstat.h:319
Oid m_tableid[PGSTAT_NUM_TABPURGE]
Definition: pgstat.h:322
PgStat_StatSubWorkerKey key
Definition: pgstat.h:1010

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(), i, PgStat_StatSubWorkerEntry::key, len, PgStat_MsgTabpurge::m_databaseid, PgStat_MsgSubscriptionPurge::m_databaseid, PgStat_MsgFuncpurge::m_databaseid, PgStat_MsgFuncpurge::m_functionid, PgStat_MsgTabpurge::m_hdr, PgStat_MsgFuncpurge::m_hdr, PgStat_MsgTabpurge::m_nentries, PgStat_MsgSubscriptionPurge::m_nentries, PgStat_MsgFuncpurge::m_nentries, PgStat_MsgSubscriptionPurge::m_subids, 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_SUBSCRIPTIONPURGE, PGSTAT_NUM_TABPURGE, pgstat_report_replslot_drop(), pgstat_send(), pgstat_send_subscription_purge(), pgstat_setheader(), pgStatDBHash, pgStatSock, replSlotStatHash, SearchNamedReplicationSlot(), PgStat_StatReplSlotEntry::slotname, PgStat_StatSubWorkerKey::subid, PgStat_StatDBEntry::subworkers, PgStat_StatTabEntry::tableid, and PgStat_StatDBEntry::tables.

Referenced by do_autovacuum(), and vacuum().

◆ PostPrepare_PgStat()

void PostPrepare_PgStat ( void  )

Definition at line 2794 of file pgstat.c.

2797  : AtEOXact_PgStat is not called during PREPARE.
2798  */
2799 void
2800 PostPrepare_PgStat(void)
2801 {
2802  PgStat_SubXactStatus *xact_state;
2803 
2804  /*
2805  * We don't bother to free any of the transactional state, since it's all
2806  * in TopTransactionContext and will go away anyway.
2807  */
2808  xact_state = pgStatXactStack;
2809  if (xact_state != NULL)
2810  {
2811  Assert(xact_state->nest_level == 1);
2812  Assert(xact_state->prev == NULL);
2813 
2814  PostPrepare_PgStat_Relations(xact_state);
void AtEOXact_PgStat(bool isCommit, bool parallel)
Definition: pgstat.c:2582
static void PostPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
Definition: pgstat.c:2774
void PostPrepare_PgStat(void)
Definition: pgstat.c:2794

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

Referenced by PrepareTransaction().

Variable Documentation

◆ PendingBgWriterStats

PgStat_MsgBgWriter PendingBgWriterStats
extern

Definition at line 127 of file pgstat.c.

Referenced by BgBufferSync(), and pgstat_send_bgwriter().

◆ PendingCheckpointerStats

◆ pgstat_stat_directory

char* pgstat_stat_directory
extern

◆ pgstat_stat_filename

char* pgstat_stat_filename
extern

◆ pgstat_stat_tmpname

char* pgstat_stat_tmpname
extern

Definition at line 120 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
extern

Definition at line 112 of file pgstat.c.

◆ pgStatActiveTime

PgStat_Counter pgStatActiveTime
extern

Definition at line 246 of file pgstat.c.

◆ pgStatBlockReadTime

PgStat_Counter pgStatBlockReadTime
extern

Definition at line 243 of file pgstat.c.

Referenced by do_analyze_rel(), and heap_vacuum_rel().

◆ pgStatBlockWriteTime

PgStat_Counter pgStatBlockWriteTime
extern

Definition at line 244 of file pgstat.c.

Referenced by do_analyze_rel(), and heap_vacuum_rel().

◆ pgStatSessionEndCause

SessionEndType pgStatSessionEndCause
extern

Definition at line 248 of file pgstat.c.

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

◆ pgStatTransactionIdleTime

PgStat_Counter pgStatTransactionIdleTime
extern

Definition at line 247 of file pgstat.c.

◆ WalStats

PgStat_MsgWal WalStats
extern