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_MsgAnlAncestors
 
struct  PgStat_MsgArchiver
 
struct  PgStat_MsgBgWriter
 
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_MsgConn
 
union  PgStat_Msg
 
struct  PgStat_StatDBEntry
 
struct  PgStat_StatTabEntry
 
struct  PgStat_StatFuncEntry
 
struct  PgStat_ArchiverStats
 
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_ANCESTORENTRIES
 
#define PGSTAT_NUM_FUNCENTRIES
 
#define PGSTAT_NUM_FUNCPURGE
 
#define PGSTAT_FILE_FORMAT_ID   0x01A5BCA2
 
#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_MsgAnlAncestors PgStat_MsgAnlAncestors
 
typedef struct PgStat_MsgArchiver PgStat_MsgArchiver
 
typedef struct PgStat_MsgBgWriter PgStat_MsgBgWriter
 
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_MsgConn PgStat_MsgConn
 
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_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_ANL_ANCESTORS, PGSTAT_MTYPE_ARCHIVER, PGSTAT_MTYPE_BGWRITER,
  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_CONNECTION
}
 
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_autovac (Oid dboid)
 
void pgstat_report_vacuum (Oid tableoid, bool shared, PgStat_Counter livetuples, PgStat_Counter deadtuples)
 
void pgstat_report_analyze (Relation rel, PgStat_Counter livetuples, PgStat_Counter deadtuples, bool resetcounter)
 
void pgstat_report_anl_ancestors (Oid relid)
 
void pgstat_report_recovery_conflict (int reason)
 
void pgstat_report_deadlock (void)
 
void pgstat_report_checksum_failures_in_db (Oid dboid, int failurecount)
 
void pgstat_report_checksum_failure (void)
 
void pgstat_report_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_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_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 BgWriterStats
 
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 1055 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 1050 of file pgstat.h.

Referenced by ReadBufferExtended().

◆ pgstat_count_buffer_read_time

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

Definition at line 1060 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 1062 of file pgstat.h.

Referenced by FlushBuffer().

◆ pgstat_count_conn_active_time

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

Definition at line 1064 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 1066 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 1035 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 1030 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 1025 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 1040 of file pgstat.h.

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

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

Referenced by index_getbitmap(), and index_getnext_tid().

◆ PGSTAT_FILE_FORMAT_ID

#define PGSTAT_FILE_FORMAT_ID   0x01A5BCA2

◆ PGSTAT_MAX_MSG_SIZE

#define PGSTAT_MAX_MSG_SIZE   1000

Definition at line 224 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 225 of file pgstat.h.

◆ PGSTAT_NUM_ANCESTORENTRIES

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

Definition at line 440 of file pgstat.h.

Referenced by pgstat_report_anl_ancestors().

◆ 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:225

Definition at line 611 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:225

Definition at line 628 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) - 2 * sizeof(PgStat_Counter)) \
/ sizeof(PgStat_TableEntry))
int64 PgStat_Counter
Definition: pgstat.h:91
unsigned int Oid
Definition: postgres_ext.h:31
struct PgStat_TableEntry PgStat_TableEntry
#define PGSTAT_MSG_PAYLOAD
Definition: pgstat.h:225

Definition at line 281 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:225

Definition at line 303 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_Counter

typedef int64 PgStat_Counter

Definition at line 91 of file pgstat.h.

◆ PgStat_FunctionCallUsage

◆ PgStat_FunctionCounts

◆ PgStat_FunctionEntry

◆ PgStat_GlobalStats

◆ PgStat_Msg

typedef union PgStat_Msg PgStat_Msg

◆ PgStat_MsgAnalyze

◆ PgStat_MsgAnlAncestors

◆ PgStat_MsgArchiver

◆ PgStat_MsgAutovacStart

◆ PgStat_MsgBgWriter

◆ PgStat_MsgChecksumFailure

◆ PgStat_MsgConn

◆ PgStat_MsgDeadlock

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

137 {
140  RESET_WAL
PgStat_Shared_Reset_Target
Definition: pgstat.h:136

◆ PgStat_Single_Reset_Type

Enumerator
RESET_TABLE 
RESET_FUNCTION 

Definition at line 144 of file pgstat.h.

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

◆ 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_ANL_ANCESTORS 
PGSTAT_MTYPE_ARCHIVER 
PGSTAT_MTYPE_BGWRITER 
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_CONNECTION 

Definition at line 57 of file pgstat.h.

58 {
85 } 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 817 of file pgstat.c.

References last_pgstat_start_time.

Referenced by HandleChildCrash().

818 {
820 }
static time_t last_pgstat_start_time
Definition: pgstat.c:176

◆ AtEOSubXact_PgStat()

void AtEOSubXact_PgStat ( bool  isCommit,
int  nestDepth 
)

Definition at line 2462 of file pgstat.c.

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

Referenced by AbortSubTransaction(), and CommitSubTransaction().

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

◆ AtEOXact_PgStat()

void AtEOXact_PgStat ( bool  isCommit,
bool  parallel 
)

Definition at line 2375 of file pgstat.c.

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

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

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

◆ AtPrepare_PgStat()

void AtPrepare_PgStat ( void  )

Definition at line 2561 of file pgstat.c.

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

Referenced by PrepareTransaction().

2562 {
2563  PgStat_SubXactStatus *xact_state;
2564 
2565  xact_state = pgStatXactStack;
2566  if (xact_state != NULL)
2567  {
2569 
2570  Assert(xact_state->nest_level == 1);
2571  Assert(xact_state->prev == NULL);
2572  for (trans = xact_state->first; trans != NULL; trans = trans->next)
2573  {
2574  PgStat_TableStatus *tabstat;
2575  TwoPhasePgStatRecord record;
2576 
2577  Assert(trans->nest_level == 1);
2578  Assert(trans->upper == NULL);
2579  tabstat = trans->parent;
2580  Assert(tabstat->trans == trans);
2581 
2582  record.tuples_inserted = trans->tuples_inserted;
2583  record.tuples_updated = trans->tuples_updated;
2584  record.tuples_deleted = trans->tuples_deleted;
2585  record.inserted_pre_trunc = trans->inserted_pre_trunc;
2586  record.updated_pre_trunc = trans->updated_pre_trunc;
2587  record.deleted_pre_trunc = trans->deleted_pre_trunc;
2588  record.t_id = tabstat->t_id;
2589  record.t_shared = tabstat->t_shared;
2590  record.t_truncated = trans->truncated;
2591 
2593  &record, sizeof(TwoPhasePgStatRecord));
2594  }
2595  }
2596 }
PgStat_Counter tuples_updated
Definition: pgstat.h:186
struct PgStat_TableXactStatus * next
Definition: pgstat.h:197
PgStat_Counter deleted_pre_trunc
Definition: pgstat.h:191
PgStat_Counter deleted_pre_trunc
Definition: pgstat.c:260
void RegisterTwoPhaseRecord(TwoPhaseRmgrId rmid, uint16 info, const void *data, uint32 len)
Definition: twophase.c:1182
PgStat_Counter tuples_inserted
Definition: pgstat.h:185
PgStat_Counter updated_pre_trunc
Definition: pgstat.c:259
PgStat_Counter inserted_pre_trunc
Definition: pgstat.c:258
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:194
PgStat_Counter tuples_updated
Definition: pgstat.c:256
PgStat_Counter tuples_deleted
Definition: pgstat.h:187
struct PgStat_SubXactStatus * prev
Definition: pgstat.c:238
#define Assert(condition)
Definition: c.h:804
PgStat_Counter inserted_pre_trunc
Definition: pgstat.h:189
PgStat_Counter tuples_inserted
Definition: pgstat.c:255
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:242
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:175
PgStat_Counter tuples_deleted
Definition: pgstat.c:257
#define TWOPHASE_RM_PGSTAT_ID
Definition: twophase_rmgr.h:26
PgStat_Counter updated_pre_trunc
Definition: pgstat.h:190
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:400
PgStat_TableXactStatus * first
Definition: pgstat.c:239
PgStat_TableStatus * parent
Definition: pgstat.h:195

◆ find_funcstat_entry()

PgStat_BackendFunctionEntry* find_funcstat_entry ( Oid  func_id)

Definition at line 1959 of file pgstat.c.

References HASH_FIND, and hash_search().

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

1960 {
1961  if (pgStatFunctions == NULL)
1962  return NULL;
1963 
1965  (void *) &func_id,
1966  HASH_FIND, NULL);
1967 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
static HTAB * pgStatFunctions
Definition: pgstat.c:220

◆ find_tabstat_entry()

PgStat_TableStatus* find_tabstat_entry ( Oid  rel_id)

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

2161 {
2162  TabStatHashEntry *hash_entry;
2163 
2164  /* If hashtable doesn't exist, there are no entries at all */
2165  if (!pgStatTabHash)
2166  return NULL;
2167 
2168  hash_entry = hash_search(pgStatTabHash, &rel_id, HASH_FIND, NULL);
2169  if (!hash_entry)
2170  return NULL;
2171 
2172  /* Note that this step could also return NULL, but that's correct */
2173  return hash_entry->tsa_entry;
2174 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
static HTAB * pgStatTabHash
Definition: pgstat.c:214
PgStat_TableStatus * tsa_entry
Definition: pgstat.c:208

◆ pgstat_clear_snapshot()

void pgstat_clear_snapshot ( void  )

Definition at line 4709 of file pgstat.c.

References MemoryContextDelete(), and pgstat_clear_backend_activity_snapshot().

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

4710 {
4711  /* Release memory, if any was allocated */
4712  if (pgStatLocalContext)
4714 
4715  /* Reset variables */
4716  pgStatLocalContext = NULL;
4717  pgStatDBHash = NULL;
4718  replSlotStatHash = NULL;
4719 
4720  /*
4721  * Historically the backend_status.c facilities lived in this file, and
4722  * were reset with the same function. For now keep it that way, and
4723  * forward the reset request.
4724  */
4726 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
static HTAB * pgStatDBHash
Definition: pgstat.c:270
static HTAB * replSlotStatHash
Definition: pgstat.c:281
static MemoryContext pgStatLocalContext
Definition: pgstat.c:269
void pgstat_clear_backend_activity_snapshot(void)

◆ pgstat_count_heap_delete()

void pgstat_count_heap_delete ( Relation  rel)

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

2276 {
2277  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2278 
2279  if (pgstat_info != NULL)
2280  {
2281  /* We have to log the effect at the proper transactional level */
2282  int nest_level = GetCurrentTransactionNestLevel();
2283 
2284  if (pgstat_info->trans == NULL ||
2285  pgstat_info->trans->nest_level != nest_level)
2286  add_tabstat_xact_level(pgstat_info, nest_level);
2287 
2288  pgstat_info->trans->tuples_deleted++;
2289  }
2290 }
PgStat_Counter tuples_deleted
Definition: pgstat.h:187
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:2202
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:175

◆ pgstat_count_heap_insert()

void pgstat_count_heap_insert ( Relation  rel,
PgStat_Counter  n 
)

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

2230 {
2231  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2232 
2233  if (pgstat_info != NULL)
2234  {
2235  /* We have to log the effect at the proper transactional level */
2236  int nest_level = GetCurrentTransactionNestLevel();
2237 
2238  if (pgstat_info->trans == NULL ||
2239  pgstat_info->trans->nest_level != nest_level)
2240  add_tabstat_xact_level(pgstat_info, nest_level);
2241 
2242  pgstat_info->trans->tuples_inserted += n;
2243  }
2244 }
PgStat_Counter tuples_inserted
Definition: pgstat.h:185
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:2202
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:175

◆ pgstat_count_heap_update()

void pgstat_count_heap_update ( Relation  rel,
bool  hot 
)

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

2251 {
2252  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2253 
2254  if (pgstat_info != NULL)
2255  {
2256  /* We have to log the effect at the proper transactional level */
2257  int nest_level = GetCurrentTransactionNestLevel();
2258 
2259  if (pgstat_info->trans == NULL ||
2260  pgstat_info->trans->nest_level != nest_level)
2261  add_tabstat_xact_level(pgstat_info, nest_level);
2262 
2263  pgstat_info->trans->tuples_updated++;
2264 
2265  /* t_tuples_hot_updated is nontransactional, so just advance it */
2266  if (hot)
2267  pgstat_info->t_counts.t_tuples_hot_updated++;
2268  }
2269 }
PgStat_Counter tuples_updated
Definition: pgstat.h:186
PgStat_Counter t_tuples_hot_updated
Definition: pgstat.h:124
PgStat_TableCounts t_counts
Definition: pgstat.h:176
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:2202
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:175

◆ pgstat_count_slru_flush()

void pgstat_count_slru_flush ( int  slru_idx)

Definition at line 5864 of file pgstat.c.

References PgStat_MsgSLRU::m_flush, and slru_entry().

Referenced by SimpleLruWriteAll().

5865 {
5866  slru_entry(slru_idx)->m_flush += 1;
5867 }
PgStat_Counter m_flush
Definition: pgstat.h:517
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5816

◆ pgstat_count_slru_page_exists()

void pgstat_count_slru_page_exists ( int  slru_idx)

Definition at line 5846 of file pgstat.c.

References PgStat_MsgSLRU::m_blocks_exists, and slru_entry().

Referenced by SimpleLruDoesPhysicalPageExist().

5847 {
5848  slru_entry(slru_idx)->m_blocks_exists += 1;
5849 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5816
PgStat_Counter m_blocks_exists
Definition: pgstat.h:516

◆ pgstat_count_slru_page_hit()

void pgstat_count_slru_page_hit ( int  slru_idx)

Definition at line 5840 of file pgstat.c.

References PgStat_MsgSLRU::m_blocks_hit, and slru_entry().

Referenced by SimpleLruReadPage(), and SimpleLruReadPage_ReadOnly().

5841 {
5842  slru_entry(slru_idx)->m_blocks_hit += 1;
5843 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5816
PgStat_Counter m_blocks_hit
Definition: pgstat.h:513

◆ pgstat_count_slru_page_read()

void pgstat_count_slru_page_read ( int  slru_idx)

Definition at line 5852 of file pgstat.c.

References PgStat_MsgSLRU::m_blocks_read, and slru_entry().

Referenced by SimpleLruReadPage().

5853 {
5854  slru_entry(slru_idx)->m_blocks_read += 1;
5855 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5816
PgStat_Counter m_blocks_read
Definition: pgstat.h:514

◆ pgstat_count_slru_page_written()

void pgstat_count_slru_page_written ( int  slru_idx)

Definition at line 5858 of file pgstat.c.

References PgStat_MsgSLRU::m_blocks_written, and slru_entry().

Referenced by SlruPhysicalWritePage().

5859 {
5860  slru_entry(slru_idx)->m_blocks_written += 1;
5861 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5816
PgStat_Counter m_blocks_written
Definition: pgstat.h:515

◆ pgstat_count_slru_page_zeroed()

void pgstat_count_slru_page_zeroed ( int  slru_idx)

Definition at line 5834 of file pgstat.c.

References PgStat_MsgSLRU::m_blocks_zeroed, and slru_entry().

Referenced by SimpleLruZeroPage().

5835 {
5836  slru_entry(slru_idx)->m_blocks_zeroed += 1;
5837 }
static PgStat_MsgSLRU * slru_entry(int slru_idx)
Definition: pgstat.c:5816
PgStat_Counter m_blocks_zeroed
Definition: pgstat.h:512

◆ pgstat_count_slru_truncate()

void pgstat_count_slru_truncate ( int  slru_idx)

Definition at line 5870 of file pgstat.c.

References PgStat_MsgSLRU::m_truncate, and slru_entry().

Referenced by SimpleLruTruncate().

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

◆ pgstat_count_truncate()

void pgstat_count_truncate ( Relation  rel)

Definition at line 2330 of file pgstat.c.

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

Referenced by ExecRefreshMatView(), and ExecuteTruncateGuts().

2331 {
2332  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2333 
2334  if (pgstat_info != NULL)
2335  {
2336  /* We have to log the effect at the proper transactional level */
2337  int nest_level = GetCurrentTransactionNestLevel();
2338 
2339  if (pgstat_info->trans == NULL ||
2340  pgstat_info->trans->nest_level != nest_level)
2341  add_tabstat_xact_level(pgstat_info, nest_level);
2342 
2343  pgstat_truncate_save_counters(pgstat_info->trans);
2344  pgstat_info->trans->tuples_inserted = 0;
2345  pgstat_info->trans->tuples_updated = 0;
2346  pgstat_info->trans->tuples_deleted = 0;
2347  }
2348 }
PgStat_Counter tuples_updated
Definition: pgstat.h:186
PgStat_Counter tuples_inserted
Definition: pgstat.h:185
PgStat_Counter tuples_deleted
Definition: pgstat.h:187
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:2202
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:175
static void pgstat_truncate_save_counters(PgStat_TableXactStatus *trans)
Definition: pgstat.c:2301

◆ pgstat_drop_database()

void pgstat_drop_database ( Oid  databaseid)

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

1324 {
1325  PgStat_MsgDropdb msg;
1326 
1328  return;
1329 
1331  msg.m_databaseid = databaseid;
1332  pgstat_send(&msg, sizeof(msg));
1333 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
Oid m_databaseid
Definition: pgstat.h:324
PgStat_MsgHdr m_hdr
Definition: pgstat.h:323
#define PGINVALID_SOCKET
Definition: port.h:33
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:2954

◆ pgstat_end_function_usage()

void pgstat_end_function_usage ( PgStat_FunctionCallUsage fcu,
bool  finalize 
)

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

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

◆ pgstat_fetch_global()

◆ pgstat_fetch_replslot()

PgStat_StatReplSlotEntry* pgstat_fetch_replslot ( NameData  slotname)

Definition at line 2891 of file pgstat.c.

References backend_read_statsfile(), and pgstat_get_replslot_entry().

Referenced by pg_stat_get_replication_slot().

2892 {
2894 
2895  return pgstat_get_replslot_entry(slotname, false);
2896 }
static void backend_read_statsfile(void)
Definition: pgstat.c:4554
static PgStat_StatReplSlotEntry * pgstat_get_replslot_entry(NameData name, bool create_it)
Definition: pgstat.c:5704

◆ pgstat_fetch_slru()

PgStat_SLRUStats* pgstat_fetch_slru ( void  )

Definition at line 2875 of file pgstat.c.

References backend_read_statsfile(), and slruStats.

Referenced by pg_stat_get_slru().

2876 {
2878 
2879  return slruStats;
2880 }
static void backend_read_statsfile(void)
Definition: pgstat.c:4554
static PgStat_SLRUStats slruStats[SLRU_NUM_ELEMENTS]
Definition: pgstat.c:280

◆ pgstat_fetch_stat_archiver()

PgStat_ArchiverStats* pgstat_fetch_stat_archiver ( void  )

Definition at line 2826 of file pgstat.c.

References archiverStats, and backend_read_statsfile().

Referenced by pg_stat_get_archiver().

2827 {
2829 
2830  return &archiverStats;
2831 }
static void backend_read_statsfile(void)
Definition: pgstat.c:4554
static PgStat_ArchiverStats archiverStats
Definition: pgstat.c:277

◆ pgstat_fetch_stat_dbentry()

PgStat_StatDBEntry* pgstat_fetch_stat_dbentry ( Oid  dbid)

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

2714 {
2715  /*
2716  * If not done for this transaction, read the statistics collector stats
2717  * file into some hash tables.
2718  */
2720 
2721  /*
2722  * Lookup the requested database; return NULL if not found
2723  */
2725  (void *) &dbid,
2726  HASH_FIND, NULL);
2727 }
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:4554
static HTAB * pgStatDBHash
Definition: pgstat.c:270

◆ pgstat_fetch_stat_funcentry()

PgStat_StatFuncEntry* pgstat_fetch_stat_funcentry ( Oid  funcid)

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

2797 {
2798  PgStat_StatDBEntry *dbentry;
2799  PgStat_StatFuncEntry *funcentry = NULL;
2800 
2801  /* load the stats file if needed */
2803 
2804  /* Lookup our database, then find the requested function. */
2806  if (dbentry != NULL && dbentry->functions != NULL)
2807  {
2808  funcentry = (PgStat_StatFuncEntry *) hash_search(dbentry->functions,
2809  (void *) &func_id,
2810  HASH_FIND, NULL);
2811  }
2812 
2813  return funcentry;
2814 }
HTAB * functions
Definition: pgstat.h:771
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2713
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:4554
Oid MyDatabaseId
Definition: globals.c:88

◆ pgstat_fetch_stat_tabentry()

PgStat_StatTabEntry* pgstat_fetch_stat_tabentry ( Oid  relid)

Definition at line 2740 of file pgstat.c.

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

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

2741 {
2742  Oid dbid;
2743  PgStat_StatDBEntry *dbentry;
2744  PgStat_StatTabEntry *tabentry;
2745 
2746  /*
2747  * If not done for this transaction, read the statistics collector stats
2748  * file into some hash tables.
2749  */
2751 
2752  /*
2753  * Lookup our database, then look in its table hash table.
2754  */
2755  dbid = MyDatabaseId;
2757  (void *) &dbid,
2758  HASH_FIND, NULL);
2759  if (dbentry != NULL && dbentry->tables != NULL)
2760  {
2761  tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2762  (void *) &relid,
2763  HASH_FIND, NULL);
2764  if (tabentry)
2765  return tabentry;
2766  }
2767 
2768  /*
2769  * If we didn't find it, maybe it's a shared table.
2770  */
2771  dbid = InvalidOid;
2773  (void *) &dbid,
2774  HASH_FIND, NULL);
2775  if (dbentry != NULL && dbentry->tables != NULL)
2776  {
2777  tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2778  (void *) &relid,
2779  HASH_FIND, NULL);
2780  if (tabentry)
2781  return tabentry;
2782  }
2783 
2784  return NULL;
2785 }
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:4554
Oid MyDatabaseId
Definition: globals.c:88
#define InvalidOid
Definition: postgres_ext.h:36
static HTAB * pgStatDBHash
Definition: pgstat.c:270

◆ pgstat_fetch_stat_wal()

PgStat_WalStats* pgstat_fetch_stat_wal ( void  )

Definition at line 2859 of file pgstat.c.

References backend_read_statsfile(), and walStats.

Referenced by pg_stat_get_wal().

2860 {
2862 
2863  return &walStats;
2864 }
static PgStat_WalStats walStats
Definition: pgstat.c:279
static void backend_read_statsfile(void)
Definition: pgstat.c:4554

◆ pgstat_init()

void pgstat_init ( void  )

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

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

◆ pgstat_init_function_usage()

void pgstat_init_function_usage ( struct FunctionCallInfoBaseData fcinfo,
PgStat_FunctionCallUsage fcu 
)

◆ pgstat_initialize()

void pgstat_initialize ( void  )

Definition at line 2928 of file pgstat.c.

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

Referenced by AuxiliaryProcessMain(), and InitPostgres().

2929 {
2930  /*
2931  * Initialize prevWalUsage with pgWalUsage so that pgstat_send_wal() can
2932  * calculate how much pgWalUsage counters are increased by substracting
2933  * prevWalUsage from pgWalUsage.
2934  */
2936 
2937  /* Set up a process-exit hook to clean up */
2939 }
static void pgstat_shutdown_hook(int code, Datum arg)
Definition: pgstat.c:2906
WalUsage pgWalUsage
Definition: instrument.c:22
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
static WalUsage prevWalUsage
Definition: pgstat.c:140

◆ pgstat_initstats()

void pgstat_initstats ( Relation  rel)

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

2037 {
2038  Oid rel_id = rel->rd_id;
2039  char relkind = rel->rd_rel->relkind;
2040 
2041  /* We only count stats for things that have storage */
2042  if (!RELKIND_HAS_STORAGE(relkind) &&
2043  relkind != RELKIND_PARTITIONED_TABLE)
2044  {
2045  rel->pgstat_info = NULL;
2046  return;
2047  }
2048 
2050  {
2051  /* We're not counting at all */
2052  rel->pgstat_info = NULL;
2053  return;
2054  }
2055 
2056  /*
2057  * If we already set up this relation in the current transaction, nothing
2058  * to do.
2059  */
2060  if (rel->pgstat_info != NULL &&
2061  rel->pgstat_info->t_id == rel_id)
2062  return;
2063 
2064  /* Else find or make the PgStat_TableStatus entry, and update link */
2065  rel->pgstat_info = get_tabstat_entry(rel_id, rel->rd_rel->relisshared);
2066 }
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
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:2072
#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 1873 of file pgstat.c.

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

1874 {
1875  PgStat_MsgDummy msg;
1876 
1878  return;
1879 
1881  pgstat_send(&msg, sizeof(msg));
1882 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
#define PGINVALID_SOCKET
Definition: port.h:33
PgStat_MsgHdr m_hdr
Definition: pgstat.h:234
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:2954

◆ pgstat_report_analyze()

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

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

1611 {
1612  PgStat_MsgAnalyze msg;
1613 
1615  return;
1616 
1617  /*
1618  * Unlike VACUUM, ANALYZE might be running inside a transaction that has
1619  * already inserted and/or deleted rows in the target table. ANALYZE will
1620  * have counted such rows as live or dead respectively. Because we will
1621  * report our counts of such rows at transaction end, we should subtract
1622  * off these counts from what we send to the collector now, else they'll
1623  * be double-counted after commit. (This approach also ensures that the
1624  * collector ends up with the right numbers if we abort instead of
1625  * committing.)
1626  *
1627  * For partitioned tables, we don't report live and dead tuples, because
1628  * such tables don't have any data.
1629  */
1630  if (rel->pgstat_info != NULL)
1631  {
1633 
1634  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1635  /* If this rel is partitioned, skip modifying */
1636  livetuples = deadtuples = 0;
1637  else
1638  {
1639  for (trans = rel->pgstat_info->trans; trans; trans = trans->upper)
1640  {
1641  livetuples -= trans->tuples_inserted - trans->tuples_deleted;
1642  deadtuples -= trans->tuples_updated + trans->tuples_deleted;
1643  }
1644  /* count stuff inserted by already-aborted subxacts, too */
1645  deadtuples -= rel->pgstat_info->t_counts.t_delta_dead_tuples;
1646  /* Since ANALYZE's counts are estimates, we could have underflowed */
1647  livetuples = Max(livetuples, 0);
1648  deadtuples = Max(deadtuples, 0);
1649  }
1650 
1651  }
1652 
1654  msg.m_databaseid = rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId;
1655  msg.m_tableoid = RelationGetRelid(rel);
1657  msg.m_resetcounter = resetcounter;
1659  msg.m_live_tuples = livetuples;
1660  msg.m_dead_tuples = deadtuples;
1661  pgstat_send(&msg, sizeof(msg));
1662 
1663 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
PgStat_Counter tuples_updated
Definition: pgstat.h:186
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
TimestampTz m_analyzetime
Definition: pgstat.h:428
PgStat_TableCounts t_counts
Definition: pgstat.h:176
Form_pg_class rd_rel
Definition: rel.h:109
bool m_resetcounter
Definition: pgstat.h:427
bool pgstat_track_counts
Definition: pgstat.c:115
PgStat_Counter tuples_inserted
Definition: pgstat.h:185
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:128
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3454
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:194
PgStat_Counter m_live_tuples
Definition: pgstat.h:429
bool m_autovacuum
Definition: pgstat.h:426
PgStat_Counter tuples_deleted
Definition: pgstat.h:187
#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:2954
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:249
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:175
PgStat_MsgHdr m_hdr
Definition: pgstat.h:423
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:400
PgStat_Counter m_dead_tuples
Definition: pgstat.h:430
#define RelationGetRelid(relation)
Definition: rel.h:477

◆ pgstat_report_anl_ancestors()

void pgstat_report_anl_ancestors ( Oid  relid)

Definition at line 1674 of file pgstat.c.

References get_partition_ancestors(), lfirst_oid, list_free(), PgStat_MsgAnlAncestors::m_ancestors, PgStat_MsgAnlAncestors::m_databaseid, PgStat_MsgAnlAncestors::m_hdr, PgStat_MsgAnlAncestors::m_nancestors, PgStat_MsgAnlAncestors::m_tableoid, MyDatabaseId, offsetof, PGSTAT_MTYPE_ANL_ANCESTORS, PGSTAT_NUM_ANCESTORENTRIES, pgstat_send(), and pgstat_setheader().

Referenced by do_analyze_rel(), and do_autovacuum().

1675 {
1677  List *ancestors;
1678  ListCell *lc;
1679 
1681  msg.m_databaseid = MyDatabaseId;
1682  msg.m_tableoid = relid;
1683  msg.m_nancestors = 0;
1684 
1685  ancestors = get_partition_ancestors(relid);
1686  foreach(lc, ancestors)
1687  {
1688  Oid ancestor = lfirst_oid(lc);
1689 
1690  msg.m_ancestors[msg.m_nancestors] = ancestor;
1692  {
1693  pgstat_send(&msg, offsetof(PgStat_MsgAnlAncestors, m_ancestors[0]) +
1694  msg.m_nancestors * sizeof(Oid));
1695  msg.m_nancestors = 0;
1696  }
1697  }
1698 
1699  if (msg.m_nancestors > 0)
1700  pgstat_send(&msg, offsetof(PgStat_MsgAnlAncestors, m_ancestors[0]) +
1701  msg.m_nancestors * sizeof(Oid));
1702 
1703  list_free(ancestors);
1704 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
unsigned int Oid
Definition: postgres_ext.h:31
Oid MyDatabaseId
Definition: globals.c:88
PgStat_MsgHdr m_hdr
Definition: pgstat.h:446
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:2954
Oid m_ancestors[PGSTAT_NUM_ANCESTORENTRIES]
Definition: pgstat.h:450
void list_free(List *list)
Definition: list.c:1391
Definition: pg_list.h:50
List * get_partition_ancestors(Oid relid)
Definition: partition.c:133
#define PGSTAT_NUM_ANCESTORENTRIES
Definition: pgstat.h:440
#define offsetof(type, field)
Definition: c.h:727
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ pgstat_report_autovac()

void pgstat_report_autovac ( Oid  dboid)

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

1556 {
1558 
1560  return;
1561 
1563  msg.m_databaseid = dboid;
1565 
1566  pgstat_send(&msg, sizeof(msg));
1567 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
PgStat_MsgHdr m_hdr
Definition: pgstat.h:393
#define PGINVALID_SOCKET
Definition: port.h:33
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:2954
TimestampTz m_start_time
Definition: pgstat.h:395

◆ pgstat_report_checksum_failure()

void pgstat_report_checksum_failure ( void  )

Definition at line 1776 of file pgstat.c.

References MyDatabaseId, and pgstat_report_checksum_failures_in_db().

Referenced by PageIsVerifiedExtended().

1777 {
1779 }
void pgstat_report_checksum_failures_in_db(Oid dboid, int failurecount)
Definition: pgstat.c:1754
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 1754 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().

1755 {
1757 
1759  return;
1760 
1762  msg.m_databaseid = dboid;
1763  msg.m_failurecount = failurecount;
1765 
1766  pgstat_send(&msg, sizeof(msg));
1767 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
PgStat_MsgHdr m_hdr
Definition: pgstat.h:658
bool pgstat_track_counts
Definition: pgstat.c:115
TimestampTz m_failure_time
Definition: pgstat.h:661
#define PGINVALID_SOCKET
Definition: port.h:33
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:2954

◆ pgstat_report_deadlock()

void pgstat_report_deadlock ( void  )

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

1734 {
1735  PgStat_MsgDeadlock msg;
1736 
1738  return;
1739 
1741  msg.m_databaseid = MyDatabaseId;
1742  pgstat_send(&msg, sizeof(msg));
1743 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
bool pgstat_track_counts
Definition: pgstat.c:115
PgStat_MsgHdr m_hdr
Definition: pgstat.h:647
#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:2954

◆ pgstat_report_recovery_conflict()

void pgstat_report_recovery_conflict ( int  reason)

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

1714 {
1716 
1718  return;
1719 
1721  msg.m_databaseid = MyDatabaseId;
1722  msg.m_reason = reason;
1723  pgstat_send(&msg, sizeof(msg));
1724 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
bool pgstat_track_counts
Definition: pgstat.c:115
PgStat_MsgHdr m_hdr
Definition: pgstat.h:549
#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:2954

◆ pgstat_report_replslot()

void pgstat_report_replslot ( const PgStat_StatReplSlotEntry repSlotStat)

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

1809 {
1810  PgStat_MsgReplSlot msg;
1811 
1812  /*
1813  * Prepare and send the message
1814  */
1816  namestrcpy(&msg.m_slotname, NameStr(repSlotStat->slotname));
1817  msg.m_create = false;
1818  msg.m_drop = false;
1819  msg.m_spill_txns = repSlotStat->spill_txns;
1820  msg.m_spill_count = repSlotStat->spill_count;
1821  msg.m_spill_bytes = repSlotStat->spill_bytes;
1822  msg.m_stream_txns = repSlotStat->stream_txns;
1823  msg.m_stream_count = repSlotStat->stream_count;
1824  msg.m_stream_bytes = repSlotStat->stream_bytes;
1825  msg.m_total_txns = repSlotStat->total_txns;
1826  msg.m_total_bytes = repSlotStat->total_bytes;
1827  pgstat_send(&msg, sizeof(PgStat_MsgReplSlot));
1828 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
PgStat_Counter spill_bytes
Definition: pgstat.h:902
void namestrcpy(Name name, const char *str)
Definition: name.c:233
PgStat_Counter m_spill_bytes
Definition: pgstat.h:534
PgStat_Counter m_stream_bytes
Definition: pgstat.h:537
PgStat_Counter m_spill_txns
Definition: pgstat.h:532
PgStat_Counter total_bytes
Definition: pgstat.h:907
PgStat_Counter stream_bytes
Definition: pgstat.h:905
PgStat_Counter spill_txns
Definition: pgstat.h:900
PgStat_Counter m_stream_txns
Definition: pgstat.h:535
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:2954
PgStat_Counter m_total_txns
Definition: pgstat.h:538
PgStat_Counter m_spill_count
Definition: pgstat.h:533
PgStat_Counter m_total_bytes
Definition: pgstat.h:539
PgStat_Counter spill_count
Definition: pgstat.h:901
PgStat_Counter stream_count
Definition: pgstat.h:904
#define NameStr(name)
Definition: c.h:681
PgStat_MsgHdr m_hdr
Definition: pgstat.h:528
PgStat_Counter total_txns
Definition: pgstat.h:906
PgStat_Counter m_stream_count
Definition: pgstat.h:536
NameData m_slotname
Definition: pgstat.h:529
PgStat_Counter stream_txns
Definition: pgstat.h:903

◆ pgstat_report_replslot_create()

void pgstat_report_replslot_create ( const char *  slotname)

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

1838 {
1839  PgStat_MsgReplSlot msg;
1840 
1842  namestrcpy(&msg.m_slotname, slotname);
1843  msg.m_create = true;
1844  msg.m_drop = false;
1845  pgstat_send(&msg, sizeof(PgStat_MsgReplSlot));
1846 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
void namestrcpy(Name name, const char *str)
Definition: name.c:233
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:2954
PgStat_MsgHdr m_hdr
Definition: pgstat.h:528
NameData m_slotname
Definition: pgstat.h:529

◆ pgstat_report_replslot_drop()

void pgstat_report_replslot_drop ( const char *  slotname)

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

1856 {
1857  PgStat_MsgReplSlot msg;
1858 
1860  namestrcpy(&msg.m_slotname, slotname);
1861  msg.m_create = false;
1862  msg.m_drop = true;
1863  pgstat_send(&msg, sizeof(PgStat_MsgReplSlot));
1864 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
void namestrcpy(Name name, const char *str)
Definition: name.c:233
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:2954
PgStat_MsgHdr m_hdr
Definition: pgstat.h:528
NameData m_slotname
Definition: pgstat.h:529

◆ pgstat_report_stat()

void pgstat_report_stat ( bool  force)

Definition at line 843 of file pgstat.c.

References Assert, B_BACKEND, 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, MyBackendType, MyDatabaseId, now(), PGSTAT_NUM_TABENTRIES, pgstat_send_connstats(), 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 apply_handle_commit_internal(), initialize_worker_spi(), LogicalRepSyncTableStart(), pg_attribute_noreturn(), pgstat_shutdown_hook(), PostgresMain(), and process_syncing_tables_for_apply().

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

1579 {
1580  PgStat_MsgVacuum msg;
1581 
1583  return;
1584 
1586  msg.m_databaseid = shared ? InvalidOid : MyDatabaseId;
1587  msg.m_tableoid = tableoid;
1590  msg.m_live_tuples = livetuples;
1591  msg.m_dead_tuples = deadtuples;
1592  pgstat_send(&msg, sizeof(msg));
1593 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
PgStat_Counter m_dead_tuples
Definition: pgstat.h:412
TimestampTz m_vacuumtime
Definition: pgstat.h:410
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
PgStat_Counter m_live_tuples
Definition: pgstat.h:411
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
Oid m_databaseid
Definition: pgstat.h:407
bool m_autovacuum
Definition: pgstat.h:409
bool pgstat_track_counts
Definition: pgstat.c:115
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3454
#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:2954
PgStat_MsgHdr m_hdr
Definition: pgstat.h:406

◆ pgstat_reset_all()

void pgstat_reset_all ( void  )

Definition at line 712 of file pgstat.c.

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

Referenced by StartupXLOG().

713 {
716 }
#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:664

◆ pgstat_reset_counters()

void pgstat_reset_counters ( void  )

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

1422 {
1424 
1426  return;
1427 
1429  msg.m_databaseid = MyDatabaseId;
1430  pgstat_send(&msg, sizeof(msg));
1431 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
PgStat_MsgHdr m_hdr
Definition: pgstat.h:335
#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:2954

◆ pgstat_reset_replslot_counter()

void pgstat_reset_replslot_counter ( const char *  name)

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

1527 {
1529 
1531  return;
1532 
1533  if (name)
1534  {
1535  namestrcpy(&msg.m_slotname, name);
1536  msg.clearall = false;
1537  }
1538  else
1539  msg.clearall = true;
1540 
1542 
1543  pgstat_send(&msg, sizeof(msg));
1544 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
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:2954
const char * name
Definition: encode.c:515

◆ pgstat_reset_shared_counters()

void pgstat_reset_shared_counters ( const char *  )

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

1444 {
1446 
1448  return;
1449 
1450  if (strcmp(target, "archiver") == 0)
1452  else if (strcmp(target, "bgwriter") == 0)
1454  else if (strcmp(target, "wal") == 0)
1455  msg.m_resettarget = RESET_WAL;
1456  else
1457  ereport(ERROR,
1458  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1459  errmsg("unrecognized reset target: \"%s\"", target),
1460  errhint("Target must be \"archiver\", \"bgwriter\" or \"wal\".")));
1461 
1463  pgstat_send(&msg, sizeof(msg));
1464 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
int errhint(const char *fmt,...)
Definition: elog.c:1156
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
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:347
#define ereport(elevel,...)
Definition: elog.h:157
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:2954
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 1476 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().

1477 {
1479 
1481  return;
1482 
1484  msg.m_databaseid = MyDatabaseId;
1485  msg.m_resettype = type;
1486  msg.m_objectid = objoid;
1487 
1488  pgstat_send(&msg, sizeof(msg));
1489 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
#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:2954
PgStat_Single_Reset_Type m_resettype
Definition: pgstat.h:359

◆ pgstat_reset_slru_counter()

void pgstat_reset_slru_counter ( const char *  )

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

1503 {
1505 
1507  return;
1508 
1510  msg.m_index = (name) ? pgstat_slru_index(name) : -1;
1511 
1512  pgstat_send(&msg, sizeof(msg));
1513 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
int pgstat_slru_index(const char *name)
Definition: pgstat.c:5779
PgStat_MsgHdr m_hdr
Definition: pgstat.h:370
#define PGINVALID_SOCKET
Definition: port.h:33
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:2954
const char * name
Definition: encode.c:515

◆ pgstat_send_archiver()

void pgstat_send_archiver ( const char *  xlog,
bool  failed 
)

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

2998 {
2999  PgStat_MsgArchiver msg;
3000 
3001  /*
3002  * Prepare and send the message
3003  */
3005  msg.m_failed = failed;
3006  strlcpy(msg.m_xlog, xlog, sizeof(msg.m_xlog));
3008  pgstat_send(&msg, sizeof(msg));
3009 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
char m_xlog[MAX_XFN_CHARS+1]
Definition: pgstat.h:461
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
TimestampTz m_timestamp
Definition: pgstat.h:462
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:2954
PgStat_MsgHdr m_hdr
Definition: pgstat.h:459

◆ pgstat_send_bgwriter()

void pgstat_send_bgwriter ( void  )

Definition at line 3018 of file pgstat.c.

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

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

3019 {
3020  /* We assume this initializes to zeroes */
3021  static const PgStat_MsgBgWriter all_zeroes;
3022 
3023  /*
3024  * This function can be called even if nothing at all has happened. In
3025  * this case, avoid sending a completely empty message to the stats
3026  * collector.
3027  */
3028  if (memcmp(&BgWriterStats, &all_zeroes, sizeof(PgStat_MsgBgWriter)) == 0)
3029  return;
3030 
3031  /*
3032  * Prepare and send the message
3033  */
3036 
3037  /*
3038  * Clear out the statistics buffer, so it can be re-used.
3039  */
3040  MemSet(&BgWriterStats, 0, sizeof(BgWriterStats));
3041 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
PgStat_MsgHdr m_hdr
Definition: pgstat.h:471
PgStat_MsgBgWriter BgWriterStats
Definition: pgstat.c:131
#define MemSet(start, val, len)
Definition: c.h:1008
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:2954

◆ pgstat_send_wal()

void pgstat_send_wal ( bool  force)

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

3054 {
3055  static TimestampTz sendTime = 0;
3056 
3057  /*
3058  * This function can be called even if nothing at all has happened. In
3059  * this case, avoid sending a completely empty message to the stats
3060  * collector.
3061  *
3062  * Check wal_records counter to determine whether any WAL activity has
3063  * happened since last time. Note that other WalUsage counters don't need
3064  * to be checked because they are incremented always together with
3065  * wal_records counter.
3066  *
3067  * m_wal_buffers_full also doesn't need to be checked because it's
3068  * incremented only when at least one WAL record is generated (i.e.,
3069  * wal_records counter is incremented). But for safely, we assert that
3070  * m_wal_buffers_full is always zero when no WAL record is generated
3071  *
3072  * This function can be called by a process like walwriter that normally
3073  * generates no WAL records. To determine whether any WAL activity has
3074  * happened at that process since the last time, the numbers of WAL writes
3075  * and syncs are also checked.
3076  */
3078  WalStats.m_wal_write == 0 && WalStats.m_wal_sync == 0)
3079  {
3081  return;
3082  }
3083 
3084  if (!force)
3085  {
3087 
3088  /*
3089  * Don't send a message unless it's been at least PGSTAT_STAT_INTERVAL
3090  * msec since we last sent one to avoid overloading the stats
3091  * collector.
3092  */
3093  if (!TimestampDifferenceExceeds(sendTime, now, PGSTAT_STAT_INTERVAL))
3094  return;
3095  sendTime = now;
3096  }
3097 
3098  /*
3099  * Set the counters related to generated WAL data if the counters were
3100  * updated.
3101  */
3103  {
3104  WalUsage walusage;
3105 
3106  /*
3107  * Calculate how much WAL usage counters were increased by
3108  * substracting the previous counters from the current ones. Fill the
3109  * results in WAL stats message.
3110  */
3111  MemSet(&walusage, 0, sizeof(WalUsage));
3112  WalUsageAccumDiff(&walusage, &pgWalUsage, &prevWalUsage);
3113 
3114  WalStats.m_wal_records = walusage.wal_records;
3115  WalStats.m_wal_fpi = walusage.wal_fpi;
3116  WalStats.m_wal_bytes = walusage.wal_bytes;
3117 
3118  /*
3119  * Save the current counters for the subsequent calculation of WAL
3120  * usage.
3121  */
3123  }
3124 
3125  /*
3126  * Prepare and send the message
3127  */
3129  pgstat_send(&WalStats, sizeof(WalStats));
3130 
3131  /*
3132  * Clear out the statistics buffer, so it can be re-used.
3133  */
3134  MemSet(&WalStats, 0, sizeof(WalStats));
3135 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
PgStat_MsgHdr m_hdr
Definition: pgstat.h:491
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:497
uint64 m_wal_bytes
Definition: pgstat.h:494
#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:495
PgStat_Counter m_wal_write
Definition: pgstat.h:496
PgStat_MsgWal WalStats
Definition: pgstat.c:132
#define Assert(condition)
Definition: c.h:804
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:2954
PgStat_Counter m_wal_fpi
Definition: pgstat.h:493
int64 wal_records
Definition: instrument.h:49
static WalUsage prevWalUsage
Definition: pgstat.c:140
uint64 wal_bytes
Definition: instrument.h:51
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1544
PgStat_Counter m_wal_records
Definition: pgstat.h:492

◆ pgstat_slru_index()

int pgstat_slru_index ( const char *  name)

Definition at line 5779 of file pgstat.c.

References i, slru_names, and SLRU_NUM_ELEMENTS.

Referenced by pgstat_reset_slru_counter(), and SimpleLruInit().

5780 {
5781  int i;
5782 
5783  for (i = 0; i < SLRU_NUM_ELEMENTS; i++)
5784  {
5785  if (strcmp(slru_names[i], name) == 0)
5786  return i;
5787  }
5788 
5789  /* return index of the last entry (which is the "other" one) */
5790  return (SLRU_NUM_ELEMENTS - 1);
5791 }
#define SLRU_NUM_ELEMENTS
Definition: pgstat.c:158
static const char *const slru_names[]
Definition: pgstat.c:147
const char * name
Definition: encode.c:515
int i

◆ pgstat_slru_name()

const char* pgstat_slru_name ( int  slru_idx)

Definition at line 5801 of file pgstat.c.

References slru_names, and SLRU_NUM_ELEMENTS.

Referenced by pg_stat_get_slru().

5802 {
5803  if (slru_idx < 0 || slru_idx >= SLRU_NUM_ELEMENTS)
5804  return NULL;
5805 
5806  return slru_names[slru_idx];
5807 }
#define SLRU_NUM_ELEMENTS
Definition: pgstat.c:158
static const char *const slru_names[]
Definition: pgstat.c:147

◆ pgstat_start()

int pgstat_start ( void  )

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

755 {
756  time_t curtime;
757  pid_t pgStatPid;
758 
759  /*
760  * Check that the socket is there, else pgstat_init failed and we can do
761  * nothing useful.
762  */
764  return 0;
765 
766  /*
767  * Do nothing if too soon since last collector start. This is a safety
768  * valve to protect against continuous respawn attempts if the collector
769  * is dying immediately at launch. Note that since we will be re-called
770  * from the postmaster main loop, we will get another chance later.
771  */
772  curtime = time(NULL);
773  if ((unsigned int) (curtime - last_pgstat_start_time) <
774  (unsigned int) PGSTAT_RESTART_INTERVAL)
775  return 0;
776  last_pgstat_start_time = curtime;
777 
778  /*
779  * Okay, fork off the collector.
780  */
781 #ifdef EXEC_BACKEND
782  switch ((pgStatPid = pgstat_forkexec()))
783 #else
784  switch ((pgStatPid = fork_process()))
785 #endif
786  {
787  case -1:
788  ereport(LOG,
789  (errmsg("could not fork statistics collector: %m")));
790  return 0;
791 
792 #ifndef EXEC_BACKEND
793  case 0:
794  /* in postmaster child ... */
796 
797  /* Close the postmaster's sockets */
798  ClosePostmasterPorts(false);
799 
800  /* Drop our connection to postmaster's shared memory, as well */
801  dsm_detach_all();
803 
804  PgstatCollectorMain(0, NULL);
805  break;
806 #endif
807 
808  default:
809  return (int) pgStatPid;
810  }
811 
812  /* shouldn't get here */
813  return 0;
814 }
#define PGSTAT_RESTART_INTERVAL
Definition: pgstat.c:90
NON_EXEC_STATIC void PgstatCollectorMain(int argc, char *argv[]) pg_attribute_noreturn()
Definition: pgstat.c:3186
void InitPostmasterChild(void)
Definition: miscinit.c:93
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
pid_t fork_process(void)
Definition: fork_process.c:29
#define LOG
Definition: elog.h:26
void ClosePostmasterPorts(bool am_syslogger)
Definition: postmaster.c:2556
void PGSharedMemoryDetach(void)
Definition: sysv_shmem.c:909
#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:176
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 2679 of file pgstat.c.

References TwoPhasePgStatRecord::deleted_pre_trunc, get_tabstat_entry(), TwoPhasePgStatRecord::inserted_pre_trunc, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_delta_dead_tuples, TwoPhasePgStatRecord::t_id, TwoPhasePgStatRecord::t_shared, TwoPhasePgStatRecord::t_truncated, 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_trunc.

2681 {
2682  TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
2683  PgStat_TableStatus *pgstat_info;
2684 
2685  /* Find or create a tabstat entry for the rel */
2686  pgstat_info = get_tabstat_entry(rec->t_id, rec->t_shared);
2687 
2688  /* Same math as in AtEOXact_PgStat, abort case */
2689  if (rec->t_truncated)
2690  {
2691  rec->tuples_inserted = rec->inserted_pre_trunc;
2692  rec->tuples_updated = rec->updated_pre_trunc;
2693  rec->tuples_deleted = rec->deleted_pre_trunc;
2694  }
2695  pgstat_info->t_counts.t_tuples_inserted += rec->tuples_inserted;
2696  pgstat_info->t_counts.t_tuples_updated += rec->tuples_updated;
2697  pgstat_info->t_counts.t_tuples_deleted += rec->tuples_deleted;
2698  pgstat_info->t_counts.t_delta_dead_tuples +=
2699  rec->tuples_inserted + rec->tuples_updated;
2700 }
PgStat_TableCounts t_counts
Definition: pgstat.h:176
PgStat_Counter deleted_pre_trunc
Definition: pgstat.c:260
PgStat_Counter t_tuples_updated
Definition: pgstat.h:122
PgStat_Counter updated_pre_trunc
Definition: pgstat.c:259
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:128
PgStat_Counter inserted_pre_trunc
Definition: pgstat.c:258
PgStat_Counter tuples_updated
Definition: pgstat.c:256
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:123
static PgStat_TableStatus * get_tabstat_entry(Oid rel_id, bool isshared)
Definition: pgstat.c:2072
PgStat_Counter tuples_inserted
Definition: pgstat.c:255
PgStat_Counter tuples_deleted
Definition: pgstat.c:257
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:121

◆ pgstat_twophase_postcommit()

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

Definition at line 2643 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_truncated, TwoPhasePgStatRecord::t_truncated, 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.

2645 {
2646  TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
2647  PgStat_TableStatus *pgstat_info;
2648 
2649  /* Find or create a tabstat entry for the rel */
2650  pgstat_info = get_tabstat_entry(rec->t_id, rec->t_shared);
2651 
2652  /* Same math as in AtEOXact_PgStat, commit case */
2653  pgstat_info->t_counts.t_tuples_inserted += rec->tuples_inserted;
2654  pgstat_info->t_counts.t_tuples_updated += rec->tuples_updated;
2655  pgstat_info->t_counts.t_tuples_deleted += rec->tuples_deleted;
2656  pgstat_info->t_counts.t_truncated = rec->t_truncated;
2657  if (rec->t_truncated)
2658  {
2659  /* forget live/dead stats seen by backend thus far */
2660  pgstat_info->t_counts.t_delta_live_tuples = 0;
2661  pgstat_info->t_counts.t_delta_dead_tuples = 0;
2662  }
2663  pgstat_info->t_counts.t_delta_live_tuples +=
2664  rec->tuples_inserted - rec->tuples_deleted;
2665  pgstat_info->t_counts.t_delta_dead_tuples +=
2666  rec->tuples_updated + rec->tuples_deleted;
2667  pgstat_info->t_counts.t_changed_tuples +=
2668  rec->tuples_inserted + rec->tuples_updated +
2669  rec->tuples_deleted;
2670 }
PgStat_Counter t_delta_live_tuples
Definition: pgstat.h:127
PgStat_TableCounts t_counts
Definition: pgstat.h:176
PgStat_Counter t_tuples_updated
Definition: pgstat.h:122
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:128
PgStat_Counter tuples_updated
Definition: pgstat.c:256
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:123
static PgStat_TableStatus * get_tabstat_entry(Oid rel_id, bool isshared)
Definition: pgstat.c:2072
PgStat_Counter t_changed_tuples
Definition: pgstat.h:129
PgStat_Counter tuples_inserted
Definition: pgstat.c:255
PgStat_Counter tuples_deleted
Definition: pgstat.c:257
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:121

◆ pgstat_update_heap_dead_tuples()

void pgstat_update_heap_dead_tuples ( Relation  rel,
int  delta 
)

Definition at line 2359 of file pgstat.c.

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

Referenced by heap_page_prune().

2360 {
2361  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2362 
2363  if (pgstat_info != NULL)
2364  pgstat_info->t_counts.t_delta_dead_tuples -= delta;
2365 }
PgStat_TableCounts t_counts
Definition: pgstat.h:176
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:128
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:249

◆ pgstat_vacuum_stat()

void pgstat_vacuum_stat ( void  )

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

1075 {
1076  HTAB *htab;
1077  PgStat_MsgTabpurge msg;
1078  PgStat_MsgFuncpurge f_msg;
1079  HASH_SEQ_STATUS hstat;
1080  PgStat_StatDBEntry *dbentry;
1081  PgStat_StatTabEntry *tabentry;
1082  PgStat_StatFuncEntry *funcentry;
1083  int len;
1084 
1086  return;
1087 
1088  /*
1089  * If not done for this transaction, read the statistics collector stats
1090  * file into some hash tables.
1091  */
1093 
1094  /*
1095  * Read pg_database and make a list of OIDs of all existing databases
1096  */
1097  htab = pgstat_collect_oids(DatabaseRelationId, Anum_pg_database_oid);
1098 
1099  /*
1100  * Search the database hash table for dead databases and tell the
1101  * collector to drop them.
1102  */
1103  hash_seq_init(&hstat, pgStatDBHash);
1104  while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
1105  {
1106  Oid dbid = dbentry->databaseid;
1107 
1109 
1110  /* the DB entry for shared tables (with InvalidOid) is never dropped */
1111  if (OidIsValid(dbid) &&
1112  hash_search(htab, (void *) &dbid, HASH_FIND, NULL) == NULL)
1113  pgstat_drop_database(dbid);
1114  }
1115 
1116  /* Clean up */
1117  hash_destroy(htab);
1118 
1119  /*
1120  * Search for all the dead replication slots in stats hashtable and tell
1121  * the stats collector to drop them.
1122  */
1123  if (replSlotStatHash)
1124  {
1125  PgStat_StatReplSlotEntry *slotentry;
1126 
1128  while ((slotentry = (PgStat_StatReplSlotEntry *) hash_seq_search(&hstat)) != NULL)
1129  {
1131 
1132  if (SearchNamedReplicationSlot(NameStr(slotentry->slotname), true) == NULL)
1134  }
1135  }
1136 
1137  /*
1138  * Lookup our own database entry; if not found, nothing more to do.
1139  */
1141  (void *) &MyDatabaseId,
1142  HASH_FIND, NULL);
1143  if (dbentry == NULL || dbentry->tables == NULL)
1144  return;
1145 
1146  /*
1147  * Similarly to above, make a list of all known relations in this DB.
1148  */
1149  htab = pgstat_collect_oids(RelationRelationId, Anum_pg_class_oid);
1150 
1151  /*
1152  * Initialize our messages table counter to zero
1153  */
1154  msg.m_nentries = 0;
1155 
1156  /*
1157  * Check for all tables listed in stats hashtable if they still exist.
1158  */
1159  hash_seq_init(&hstat, dbentry->tables);
1160  while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&hstat)) != NULL)
1161  {
1162  Oid tabid = tabentry->tableid;
1163 
1165 
1166  if (hash_search(htab, (void *) &tabid, HASH_FIND, NULL) != NULL)
1167  continue;
1168 
1169  /*
1170  * Not there, so add this table's Oid to the message
1171  */
1172  msg.m_tableid[msg.m_nentries++] = tabid;
1173 
1174  /*
1175  * If the message is full, send it out and reinitialize to empty
1176  */
1177  if (msg.m_nentries >= PGSTAT_NUM_TABPURGE)
1178  {
1179  len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
1180  + msg.m_nentries * sizeof(Oid);
1181 
1183  msg.m_databaseid = MyDatabaseId;
1184  pgstat_send(&msg, len);
1185 
1186  msg.m_nentries = 0;
1187  }
1188  }
1189 
1190  /*
1191  * Send the rest
1192  */
1193  if (msg.m_nentries > 0)
1194  {
1195  len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
1196  + msg.m_nentries * sizeof(Oid);
1197 
1199  msg.m_databaseid = MyDatabaseId;
1200  pgstat_send(&msg, len);
1201  }
1202 
1203  /* Clean up */
1204  hash_destroy(htab);
1205 
1206  /*
1207  * Now repeat the above steps for functions. However, we needn't bother
1208  * in the common case where no function stats are being collected.
1209  */
1210  if (dbentry->functions != NULL &&
1211  hash_get_num_entries(dbentry->functions) > 0)
1212  {
1213  htab = pgstat_collect_oids(ProcedureRelationId, Anum_pg_proc_oid);
1214 
1216  f_msg.m_databaseid = MyDatabaseId;
1217  f_msg.m_nentries = 0;
1218 
1219  hash_seq_init(&hstat, dbentry->functions);
1220  while ((funcentry = (PgStat_StatFuncEntry *) hash_seq_search(&hstat)) != NULL)
1221  {
1222  Oid funcid = funcentry->functionid;
1223 
1225 
1226  if (hash_search(htab, (void *) &funcid, HASH_FIND, NULL) != NULL)
1227  continue;
1228 
1229  /*
1230  * Not there, so add this function's Oid to the message
1231  */
1232  f_msg.m_functionid[f_msg.m_nentries++] = funcid;
1233 
1234  /*
1235  * If the message is full, send it out and reinitialize to empty
1236  */
1237  if (f_msg.m_nentries >= PGSTAT_NUM_FUNCPURGE)
1238  {
1239  len = offsetof(PgStat_MsgFuncpurge, m_functionid[0])
1240  + f_msg.m_nentries * sizeof(Oid);
1241 
1242  pgstat_send(&f_msg, len);
1243 
1244  f_msg.m_nentries = 0;
1245  }
1246  }
1247 
1248  /*
1249  * Send the rest
1250  */
1251  if (f_msg.m_nentries > 0)
1252  {
1253  len = offsetof(PgStat_MsgFuncpurge, m_functionid[0])
1254  + f_msg.m_nentries * sizeof(Oid);
1255 
1256  pgstat_send(&f_msg, len);
1257  }
1258 
1259  hash_destroy(htab);
1260  }
1261 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:2967
ReplicationSlot * SearchNamedReplicationSlot(const char *name, bool need_lock)
Definition: slot.c:347
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:862
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:172
#define PGSTAT_NUM_TABPURGE
Definition: pgstat.h:303
PgStat_MsgHdr m_hdr
Definition: pgstat.h:634
HTAB * functions
Definition: pgstat.h:771
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:1274
Oid m_tableid[PGSTAT_NUM_TABPURGE]
Definition: pgstat.h:312
static void backend_read_statsfile(void)
Definition: pgstat.c:4554
void pgstat_drop_database(Oid databaseid)
Definition: pgstat.c:1323
PgStat_MsgHdr m_hdr
Definition: pgstat.h:309
#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:2954
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:270
void pgstat_report_replslot_drop(const char *slotname)
Definition: pgstat.c:1855
static HTAB * replSlotStatHash
Definition: pgstat.c:281
#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:628
Oid m_functionid[PGSTAT_NUM_FUNCPURGE]
Definition: pgstat.h:637

◆ PostPrepare_PgStat()

void PostPrepare_PgStat ( void  )

Definition at line 2610 of file pgstat.c.

References PgStat_SubXactStatus::first, PgStat_TableXactStatus::next, PgStat_TableXactStatus::parent, pgstat_clear_snapshot(), pgStatXactStack, PgStat_TableStatus::trans, and trans.

Referenced by PrepareTransaction().

2611 {
2612  PgStat_SubXactStatus *xact_state;
2613 
2614  /*
2615  * We don't bother to free any of the transactional state, since it's all
2616  * in TopTransactionContext and will go away anyway.
2617  */
2618  xact_state = pgStatXactStack;
2619  if (xact_state != NULL)
2620  {
2622 
2623  for (trans = xact_state->first; trans != NULL; trans = trans->next)
2624  {
2625  PgStat_TableStatus *tabstat;
2626 
2627  tabstat = trans->parent;
2628  tabstat->trans = NULL;
2629  }
2630  }
2631  pgStatXactStack = NULL;
2632 
2633  /* Make sure any stats snapshot is thrown away */
2635 }
struct PgStat_TableXactStatus * next
Definition: pgstat.h:197
void pgstat_clear_snapshot(void)
Definition: pgstat.c:4709
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:242
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:175
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:400
PgStat_TableXactStatus * first
Definition: pgstat.c:239
PgStat_TableStatus * parent
Definition: pgstat.h:195

Variable Documentation

◆ BgWriterStats

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

Referenced by pgstat_send_connstats().

◆ pgStatBlockReadTime

PgStat_Counter pgStatBlockReadTime

Definition at line 246 of file pgstat.c.

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

◆ pgStatBlockWriteTime

PgStat_Counter pgStatBlockWriteTime

Definition at line 247 of file pgstat.c.

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

◆ pgStatSessionEndCause

SessionEndType pgStatSessionEndCause

Definition at line 250 of file pgstat.c.

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

◆ pgStatTransactionIdleTime

PgStat_Counter pgStatTransactionIdleTime

Definition at line 249 of file pgstat.c.

Referenced by pgstat_send_connstats().

◆ WalStats

PgStat_MsgWal WalStats

Definition at line 132 of file pgstat.c.

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