PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pgstat.h File Reference
#include "datatype/timestamp.h"
#include "fmgr.h"
#include "libpq/pqcomm.h"
#include "port/atomics.h"
#include "portability/instr_time.h"
#include "postmaster/pgarch.h"
#include "storage/proc.h"
#include "utils/hsearch.h"
#include "utils/relcache.h"
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_MsgAutovacStart
 
struct  PgStat_MsgVacuum
 
struct  PgStat_MsgAnalyze
 
struct  PgStat_MsgArchiver
 
struct  PgStat_MsgBgWriter
 
struct  PgStat_MsgRecoveryConflict
 
struct  PgStat_MsgTempFile
 
struct  PgStat_FunctionCounts
 
struct  PgStat_BackendFunctionEntry
 
struct  PgStat_FunctionEntry
 
struct  PgStat_MsgFuncstat
 
struct  PgStat_MsgFuncpurge
 
struct  PgStat_MsgDeadlock
 
union  PgStat_Msg
 
struct  PgStat_StatDBEntry
 
struct  PgStat_StatTabEntry
 
struct  PgStat_StatFuncEntry
 
struct  PgStat_ArchiverStats
 
struct  PgStat_GlobalStats
 
struct  PgBackendSSLStatus
 
struct  PgBackendStatus
 
struct  LocalPgBackendStatus
 
struct  PgStat_FunctionCallUsage
 

Macros

#define PGSTAT_STAT_PERMANENT_DIRECTORY   "pg_stat"
 
#define PGSTAT_STAT_PERMANENT_FILENAME   "pg_stat/global.stat"
 
#define PGSTAT_STAT_PERMANENT_TMPFILE   "pg_stat/global.tmp"
 
#define PG_STAT_TMP_DIR   "pg_stat_tmp"
 
#define PGSTAT_MAX_MSG_SIZE   1000
 
#define PGSTAT_MSG_PAYLOAD   (PGSTAT_MAX_MSG_SIZE - sizeof(PgStat_MsgHdr))
 
#define PGSTAT_NUM_TABENTRIES
 
#define PGSTAT_NUM_TABPURGE
 
#define PGSTAT_NUM_FUNCENTRIES
 
#define PGSTAT_NUM_FUNCPURGE
 
#define PGSTAT_FILE_FORMAT_ID   0x01A5BC9D
 
#define PG_WAIT_LWLOCK   0x01000000U
 
#define PG_WAIT_LOCK   0x03000000U
 
#define PG_WAIT_BUFFER_PIN   0x04000000U
 
#define PG_WAIT_ACTIVITY   0x05000000U
 
#define PG_WAIT_CLIENT   0x06000000U
 
#define PG_WAIT_EXTENSION   0x07000000U
 
#define PG_WAIT_IPC   0x08000000U
 
#define PG_WAIT_TIMEOUT   0x09000000U
 
#define PG_WAIT_IO   0x0A000000U
 
#define PGSTAT_NUM_PROGRESS_PARAM   10
 
#define pgstat_increment_changecount_before(beentry)
 
#define pgstat_increment_changecount_after(beentry)
 
#define pgstat_save_changecount_before(beentry, save_changecount)
 
#define pgstat_save_changecount_after(beentry, save_changecount)
 
#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))
 

Typedefs

typedef enum TrackFunctionsLevel TrackFunctionsLevel
 
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_MsgAutovacStart 
PgStat_MsgAutovacStart
 
typedef struct PgStat_MsgVacuum PgStat_MsgVacuum
 
typedef struct PgStat_MsgAnalyze PgStat_MsgAnalyze
 
typedef struct PgStat_MsgArchiver PgStat_MsgArchiver
 
typedef struct PgStat_MsgBgWriter PgStat_MsgBgWriter
 
typedef struct
PgStat_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 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 enum BackendType BackendType
 
typedef enum BackendState BackendState
 
typedef enum ProgressCommandType ProgressCommandType
 
typedef struct PgBackendSSLStatus PgBackendSSLStatus
 
typedef struct PgBackendStatus PgBackendStatus
 
typedef struct LocalPgBackendStatus LocalPgBackendStatus
 
typedef struct
PgStat_FunctionCallUsage 
PgStat_FunctionCallUsage
 

Enumerations

enum  TrackFunctionsLevel { TRACK_FUNC_OFF, TRACK_FUNC_PL, TRACK_FUNC_ALL }
 
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_AUTOVAC_START, PGSTAT_MTYPE_VACUUM, PGSTAT_MTYPE_ANALYZE, PGSTAT_MTYPE_ARCHIVER,
  PGSTAT_MTYPE_BGWRITER, PGSTAT_MTYPE_FUNCSTAT, PGSTAT_MTYPE_FUNCPURGE, PGSTAT_MTYPE_RECOVERYCONFLICT,
  PGSTAT_MTYPE_TEMPFILE, PGSTAT_MTYPE_DEADLOCK
}
 
enum  PgStat_Shared_Reset_Target { RESET_ARCHIVER, RESET_BGWRITER }
 
enum  PgStat_Single_Reset_Type { RESET_TABLE, RESET_FUNCTION }
 
enum  BackendType {
  B_AUTOVAC_LAUNCHER, B_AUTOVAC_WORKER, B_BACKEND, B_BG_WORKER,
  B_BG_WRITER, B_CHECKPOINTER, B_STARTUP, B_WAL_RECEIVER,
  B_WAL_SENDER, B_WAL_WRITER
}
 
enum  BackendState {
  STATE_UNDEFINED, STATE_IDLE, STATE_RUNNING, STATE_IDLEINTRANSACTION,
  STATE_FASTPATH, STATE_IDLEINTRANSACTION_ABORTED, STATE_DISABLED
}
 
enum  WaitEventActivity {
  WAIT_EVENT_ARCHIVER_MAIN = PG_WAIT_ACTIVITY, WAIT_EVENT_AUTOVACUUM_MAIN, WAIT_EVENT_BGWRITER_HIBERNATE, WAIT_EVENT_BGWRITER_MAIN,
  WAIT_EVENT_CHECKPOINTER_MAIN, WAIT_EVENT_PGSTAT_MAIN, WAIT_EVENT_RECOVERY_WAL_ALL, WAIT_EVENT_RECOVERY_WAL_STREAM,
  WAIT_EVENT_SYSLOGGER_MAIN, WAIT_EVENT_WAL_RECEIVER_MAIN, WAIT_EVENT_WAL_SENDER_MAIN, WAIT_EVENT_WAL_WRITER_MAIN,
  WAIT_EVENT_LOGICAL_LAUNCHER_MAIN, WAIT_EVENT_LOGICAL_APPLY_MAIN
}
 
enum  WaitEventClient {
  WAIT_EVENT_CLIENT_READ = PG_WAIT_CLIENT, WAIT_EVENT_CLIENT_WRITE, WAIT_EVENT_SSL_OPEN_SERVER, WAIT_EVENT_WAL_RECEIVER_WAIT_START,
  WAIT_EVENT_LIBPQWALRECEIVER, WAIT_EVENT_WAL_SENDER_WAIT_WAL, WAIT_EVENT_WAL_SENDER_WRITE_DATA
}
 
enum  WaitEventIPC {
  WAIT_EVENT_BGWORKER_SHUTDOWN = PG_WAIT_IPC, WAIT_EVENT_BGWORKER_STARTUP, WAIT_EVENT_BTREE_PAGE, WAIT_EVENT_EXECUTE_GATHER,
  WAIT_EVENT_MQ_INTERNAL, WAIT_EVENT_MQ_PUT_MESSAGE, WAIT_EVENT_MQ_RECEIVE, WAIT_EVENT_MQ_SEND,
  WAIT_EVENT_PARALLEL_FINISH, WAIT_EVENT_PARALLEL_BITMAP_SCAN, WAIT_EVENT_PROCARRAY_GROUP_UPDATE, WAIT_EVENT_SAFE_SNAPSHOT,
  WAIT_EVENT_SYNC_REP, WAIT_EVENT_LOGICAL_SYNC_DATA, WAIT_EVENT_LOGICAL_SYNC_STATE_CHANGE
}
 
enum  WaitEventTimeout { WAIT_EVENT_BASE_BACKUP_THROTTLE = PG_WAIT_TIMEOUT, WAIT_EVENT_PG_SLEEP, WAIT_EVENT_RECOVERY_APPLY_DELAY }
 
enum  WaitEventIO {
  WAIT_EVENT_BUFFILE_READ = PG_WAIT_IO, WAIT_EVENT_BUFFILE_WRITE, WAIT_EVENT_CONTROL_FILE_READ, WAIT_EVENT_CONTROL_FILE_SYNC,
  WAIT_EVENT_CONTROL_FILE_SYNC_UPDATE, WAIT_EVENT_CONTROL_FILE_WRITE, WAIT_EVENT_CONTROL_FILE_WRITE_UPDATE, WAIT_EVENT_COPY_FILE_READ,
  WAIT_EVENT_COPY_FILE_WRITE, WAIT_EVENT_DATA_FILE_EXTEND, WAIT_EVENT_DATA_FILE_FLUSH, WAIT_EVENT_DATA_FILE_IMMEDIATE_SYNC,
  WAIT_EVENT_DATA_FILE_PREFETCH, WAIT_EVENT_DATA_FILE_READ, WAIT_EVENT_DATA_FILE_SYNC, WAIT_EVENT_DATA_FILE_TRUNCATE,
  WAIT_EVENT_DATA_FILE_WRITE, WAIT_EVENT_DSM_FILL_ZERO_WRITE, WAIT_EVENT_LOCK_FILE_ADDTODATADIR_READ, WAIT_EVENT_LOCK_FILE_ADDTODATADIR_SYNC,
  WAIT_EVENT_LOCK_FILE_ADDTODATADIR_WRITE, WAIT_EVENT_LOCK_FILE_CREATE_READ, WAIT_EVENT_LOCK_FILE_CREATE_SYNC, WAIT_EVENT_LOCK_FILE_CREATE_WRITE,
  WAIT_EVENT_LOCK_FILE_RECHECKDATADIR_READ, WAIT_EVENT_LOGICAL_REWRITE_CHECKPOINT_SYNC, WAIT_EVENT_LOGICAL_REWRITE_MAPPING_SYNC, WAIT_EVENT_LOGICAL_REWRITE_MAPPING_WRITE,
  WAIT_EVENT_LOGICAL_REWRITE_SYNC, WAIT_EVENT_LOGICAL_REWRITE_TRUNCATE, WAIT_EVENT_LOGICAL_REWRITE_WRITE, WAIT_EVENT_RELATION_MAP_READ,
  WAIT_EVENT_RELATION_MAP_SYNC, WAIT_EVENT_RELATION_MAP_WRITE, WAIT_EVENT_REORDER_BUFFER_READ, WAIT_EVENT_REORDER_BUFFER_WRITE,
  WAIT_EVENT_REORDER_LOGICAL_MAPPING_READ, WAIT_EVENT_REPLICATION_SLOT_READ, WAIT_EVENT_REPLICATION_SLOT_RESTORE_SYNC, WAIT_EVENT_REPLICATION_SLOT_SYNC,
  WAIT_EVENT_REPLICATION_SLOT_WRITE, WAIT_EVENT_SLRU_FLUSH_SYNC, WAIT_EVENT_SLRU_READ, WAIT_EVENT_SLRU_SYNC,
  WAIT_EVENT_SLRU_WRITE, WAIT_EVENT_SNAPBUILD_READ, WAIT_EVENT_SNAPBUILD_SYNC, WAIT_EVENT_SNAPBUILD_WRITE,
  WAIT_EVENT_TIMELINE_HISTORY_FILE_SYNC, WAIT_EVENT_TIMELINE_HISTORY_FILE_WRITE, WAIT_EVENT_TIMELINE_HISTORY_READ, WAIT_EVENT_TIMELINE_HISTORY_SYNC,
  WAIT_EVENT_TIMELINE_HISTORY_WRITE, WAIT_EVENT_TWOPHASE_FILE_READ, WAIT_EVENT_TWOPHASE_FILE_SYNC, WAIT_EVENT_TWOPHASE_FILE_WRITE,
  WAIT_EVENT_WALSENDER_TIMELINE_HISTORY_READ, WAIT_EVENT_WAL_BOOTSTRAP_SYNC, WAIT_EVENT_WAL_BOOTSTRAP_WRITE, WAIT_EVENT_WAL_COPY_READ,
  WAIT_EVENT_WAL_COPY_SYNC, WAIT_EVENT_WAL_COPY_WRITE, WAIT_EVENT_WAL_INIT_SYNC, WAIT_EVENT_WAL_INIT_WRITE,
  WAIT_EVENT_WAL_READ, WAIT_EVENT_WAL_SYNC_METHOD_ASSIGN, WAIT_EVENT_WAL_WRITE
}
 
enum  ProgressCommandType { PROGRESS_COMMAND_INVALID, PROGRESS_COMMAND_VACUUM }
 

Functions

Size BackendStatusShmemSize (void)
 
void CreateSharedBackendStatus (void)
 
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_report_autovac (Oid dboid)
 
void pgstat_report_vacuum (Oid tableoid, bool shared, PgStat_Counter livetuples, PgStat_Counter deadtuples)
 
void pgstat_report_analyze (Relation rel, PgStat_Counter livetuples, PgStat_Counter deadtuples, bool resetcounter)
 
void pgstat_report_recovery_conflict (int reason)
 
void pgstat_report_deadlock (void)
 
void pgstat_initialize (void)
 
void pgstat_bestart (void)
 
void pgstat_report_activity (BackendState state, const char *cmd_str)
 
void pgstat_report_tempfile (size_t filesize)
 
void pgstat_report_appname (const char *appname)
 
void pgstat_report_xact_timestamp (TimestampTz tstamp)
 
const char * pgstat_get_wait_event (uint32 wait_event_info)
 
const char * pgstat_get_wait_event_type (uint32 wait_event_info)
 
const char * pgstat_get_backend_current_activity (int pid, bool checkUser)
 
const char * pgstat_get_crashed_backend_activity (int pid, char *buffer, int buflen)
 
const char * pgstat_get_backend_desc (BackendType backendType)
 
void pgstat_progress_start_command (ProgressCommandType cmdtype, Oid relid)
 
void pgstat_progress_update_param (int index, int64 val)
 
void pgstat_progress_update_multi_param (int nparam, const int *index, const int64 *val)
 
void pgstat_progress_end_command (void)
 
PgStat_TableStatusfind_tabstat_entry (Oid rel_id)
 
PgStat_BackendFunctionEntryfind_funcstat_entry (Oid func_id)
 
void pgstat_initstats (Relation rel)
 
static void pgstat_report_wait_start (uint32 wait_event_info)
 
static void pgstat_report_wait_end (void)
 
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 (FunctionCallInfoData *fcinfo, PgStat_FunctionCallUsage *fcu)
 
void pgstat_end_function_usage (PgStat_FunctionCallUsage *fcu, bool finalize)
 
void AtEOXact_PgStat (bool isCommit)
 
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)
 
PgStat_StatDBEntrypgstat_fetch_stat_dbentry (Oid dbid)
 
PgStat_StatTabEntrypgstat_fetch_stat_tabentry (Oid relid)
 
PgBackendStatuspgstat_fetch_stat_beentry (int beid)
 
LocalPgBackendStatuspgstat_fetch_stat_local_beentry (int beid)
 
PgStat_StatFuncEntrypgstat_fetch_stat_funcentry (Oid funcid)
 
int pgstat_fetch_stat_numbackends (void)
 
PgStat_ArchiverStatspgstat_fetch_stat_archiver (void)
 
PgStat_GlobalStatspgstat_fetch_global (void)
 

Variables

bool pgstat_track_activities
 
bool pgstat_track_counts
 
int pgstat_track_functions
 
PGDLLIMPORT int pgstat_track_activity_query_size
 
char * pgstat_stat_directory
 
char * pgstat_stat_tmpname
 
char * pgstat_stat_filename
 
PgStat_MsgBgWriter BgWriterStats
 
PgStat_Counter pgStatBlockReadTime
 
PgStat_Counter pgStatBlockWriteTime
 

Macro Definition Documentation

#define PG_STAT_TMP_DIR   "pg_stat_tmp"

Definition at line 34 of file pgstat.h.

#define PG_WAIT_ACTIVITY   0x05000000U

Definition at line 740 of file pgstat.h.

Referenced by pgstat_get_wait_event(), and pgstat_get_wait_event_type().

#define PG_WAIT_BUFFER_PIN   0x04000000U
#define PG_WAIT_CLIENT   0x06000000U

Definition at line 741 of file pgstat.h.

Referenced by pgstat_get_wait_event(), and pgstat_get_wait_event_type().

#define PG_WAIT_EXTENSION   0x07000000U
#define PG_WAIT_IO   0x0A000000U

Definition at line 745 of file pgstat.h.

Referenced by pgstat_get_wait_event(), and pgstat_get_wait_event_type().

#define PG_WAIT_IPC   0x08000000U

Definition at line 743 of file pgstat.h.

Referenced by pgstat_get_wait_event(), and pgstat_get_wait_event_type().

#define PG_WAIT_LOCK   0x03000000U
#define PG_WAIT_LWLOCK   0x01000000U
#define PG_WAIT_TIMEOUT   0x09000000U

Definition at line 744 of file pgstat.h.

Referenced by pgstat_get_wait_event(), and pgstat_get_wait_event_type().

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

Definition at line 1278 of file pgstat.h.

Referenced by ReadBufferExtended().

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

Definition at line 1273 of file pgstat.h.

Referenced by ReadBufferExtended().

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

Definition at line 1283 of file pgstat.h.

Referenced by ReadBuffer_common().

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

Definition at line 1285 of file pgstat.h.

Referenced by FlushBuffer().

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

Definition at line 1258 of file pgstat.h.

Referenced by BitmapHeapNext(), heap_fetch(), and index_fetch_heap().

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

Definition at line 1253 of file pgstat.h.

Referenced by heap_getnext(), and tablesample_getnext().

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

Definition at line 1248 of file pgstat.h.

Referenced by initscan().

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

Definition at line 1263 of file pgstat.h.

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

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

Definition at line 1268 of file pgstat.h.

Referenced by index_getbitmap(), and index_getnext_tid().

#define PGSTAT_FILE_FORMAT_ID   0x01A5BC9D
#define pgstat_increment_changecount_after (   beentry)
Value:
do { \
beentry->st_changecount++; \
Assert((beentry->st_changecount & 1) == 0); \
} while (0)
#define Assert(condition)
Definition: c.h:675
#define pg_write_barrier()
Definition: atomics.h:162

Definition at line 1037 of file pgstat.h.

Referenced by pgstat_beshutdown_hook(), pgstat_bestart(), pgstat_progress_end_command(), pgstat_progress_start_command(), pgstat_progress_update_multi_param(), pgstat_progress_update_param(), pgstat_report_activity(), pgstat_report_appname(), and pgstat_report_xact_timestamp().

#define pgstat_increment_changecount_before (   beentry)
#define PGSTAT_MAX_MSG_SIZE   1000

Definition at line 206 of file pgstat.h.

Referenced by pgstat_init().

#define PGSTAT_MSG_PAYLOAD   (PGSTAT_MAX_MSG_SIZE - sizeof(PgStat_MsgHdr))

Definition at line 207 of file pgstat.h.

#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:207

Definition at line 493 of file pgstat.h.

Referenced by pgstat_send_funcstats().

#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:207

Definition at line 510 of file pgstat.h.

Referenced by pgstat_vacuum_stat().

#define PGSTAT_NUM_PROGRESS_PARAM   10
#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:74
unsigned int Oid
Definition: postgres_ext.h:31
struct PgStat_TableEntry PgStat_TableEntry
#define PGSTAT_MSG_PAYLOAD
Definition: pgstat.h:207

Definition at line 263 of file pgstat.h.

Referenced by pgstat_report_stat().

#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:207

Definition at line 285 of file pgstat.h.

Referenced by pgstat_vacuum_stat().

#define pgstat_save_changecount_after (   beentry,
  save_changecount 
)
Value:
do { \
save_changecount = beentry->st_changecount; \
} while (0)
#define pg_read_barrier()
Definition: atomics.h:161

Definition at line 1050 of file pgstat.h.

Referenced by pgstat_get_backend_current_activity(), and pgstat_read_current_status().

#define pgstat_save_changecount_before (   beentry,
  save_changecount 
)
Value:
do { \
save_changecount = beentry->st_changecount; \
} while (0)
#define pg_read_barrier()
Definition: atomics.h:161

Definition at line 1044 of file pgstat.h.

Referenced by pgstat_get_backend_current_activity(), and pgstat_read_current_status().

#define PGSTAT_STAT_PERMANENT_DIRECTORY   "pg_stat"

Definition at line 29 of file pgstat.h.

Referenced by get_dbstat_filename(), and pgstat_reset_all().

#define PGSTAT_STAT_PERMANENT_FILENAME   "pg_stat/global.stat"
#define PGSTAT_STAT_PERMANENT_TMPFILE   "pg_stat/global.tmp"

Definition at line 31 of file pgstat.h.

Referenced by pgstat_write_statsfiles().

Typedef Documentation

Definition at line 74 of file pgstat.h.

Enumeration Type Documentation

Enumerator
STATE_UNDEFINED 
STATE_IDLE 
STATE_RUNNING 
STATE_IDLEINTRANSACTION 
STATE_FASTPATH 
STATE_IDLEINTRANSACTION_ABORTED 
STATE_DISABLED 

Definition at line 721 of file pgstat.h.

Enumerator
B_AUTOVAC_LAUNCHER 
B_AUTOVAC_WORKER 
B_BACKEND 
B_BG_WORKER 
B_BG_WRITER 
B_CHECKPOINTER 
B_STARTUP 
B_WAL_RECEIVER 
B_WAL_SENDER 
B_WAL_WRITER 

Definition at line 702 of file pgstat.h.

Enumerator
RESET_ARCHIVER 
RESET_BGWRITER 

Definition at line 119 of file pgstat.h.

120 {
PgStat_Shared_Reset_Target
Definition: pgstat.h:119
Enumerator
RESET_TABLE 
RESET_FUNCTION 

Definition at line 126 of file pgstat.h.

127 {
128  RESET_TABLE,
PgStat_Single_Reset_Type
Definition: pgstat.h:126
Enumerator
PROGRESS_COMMAND_INVALID 
PROGRESS_COMMAND_VACUUM 

Definition at line 912 of file pgstat.h.

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_AUTOVAC_START 
PGSTAT_MTYPE_VACUUM 
PGSTAT_MTYPE_ANALYZE 
PGSTAT_MTYPE_ARCHIVER 
PGSTAT_MTYPE_BGWRITER 
PGSTAT_MTYPE_FUNCSTAT 
PGSTAT_MTYPE_FUNCPURGE 
PGSTAT_MTYPE_RECOVERYCONFLICT 
PGSTAT_MTYPE_TEMPFILE 
PGSTAT_MTYPE_DEADLOCK 

Definition at line 48 of file pgstat.h.

Enumerator
TRACK_FUNC_OFF 
TRACK_FUNC_PL 
TRACK_FUNC_ALL 

Definition at line 37 of file pgstat.h.

Enumerator
WAIT_EVENT_ARCHIVER_MAIN 
WAIT_EVENT_AUTOVACUUM_MAIN 
WAIT_EVENT_BGWRITER_HIBERNATE 
WAIT_EVENT_BGWRITER_MAIN 
WAIT_EVENT_CHECKPOINTER_MAIN 
WAIT_EVENT_PGSTAT_MAIN 
WAIT_EVENT_RECOVERY_WAL_ALL 
WAIT_EVENT_RECOVERY_WAL_STREAM 
WAIT_EVENT_SYSLOGGER_MAIN 
WAIT_EVENT_WAL_RECEIVER_MAIN 
WAIT_EVENT_WAL_SENDER_MAIN 
WAIT_EVENT_WAL_WRITER_MAIN 
WAIT_EVENT_LOGICAL_LAUNCHER_MAIN 
WAIT_EVENT_LOGICAL_APPLY_MAIN 

Definition at line 755 of file pgstat.h.

Enumerator
WAIT_EVENT_CLIENT_READ 
WAIT_EVENT_CLIENT_WRITE 
WAIT_EVENT_SSL_OPEN_SERVER 
WAIT_EVENT_WAL_RECEIVER_WAIT_START 
WAIT_EVENT_LIBPQWALRECEIVER 
WAIT_EVENT_WAL_SENDER_WAIT_WAL 
WAIT_EVENT_WAL_SENDER_WRITE_DATA 

Definition at line 781 of file pgstat.h.

Enumerator
WAIT_EVENT_BUFFILE_READ 
WAIT_EVENT_BUFFILE_WRITE 
WAIT_EVENT_CONTROL_FILE_READ 
WAIT_EVENT_CONTROL_FILE_SYNC 
WAIT_EVENT_CONTROL_FILE_SYNC_UPDATE 
WAIT_EVENT_CONTROL_FILE_WRITE 
WAIT_EVENT_CONTROL_FILE_WRITE_UPDATE 
WAIT_EVENT_COPY_FILE_READ 
WAIT_EVENT_COPY_FILE_WRITE 
WAIT_EVENT_DATA_FILE_EXTEND 
WAIT_EVENT_DATA_FILE_FLUSH 
WAIT_EVENT_DATA_FILE_IMMEDIATE_SYNC 
WAIT_EVENT_DATA_FILE_PREFETCH 
WAIT_EVENT_DATA_FILE_READ 
WAIT_EVENT_DATA_FILE_SYNC 
WAIT_EVENT_DATA_FILE_TRUNCATE 
WAIT_EVENT_DATA_FILE_WRITE 
WAIT_EVENT_DSM_FILL_ZERO_WRITE 
WAIT_EVENT_LOCK_FILE_ADDTODATADIR_READ 
WAIT_EVENT_LOCK_FILE_ADDTODATADIR_SYNC 
WAIT_EVENT_LOCK_FILE_ADDTODATADIR_WRITE 
WAIT_EVENT_LOCK_FILE_CREATE_READ 
WAIT_EVENT_LOCK_FILE_CREATE_SYNC 
WAIT_EVENT_LOCK_FILE_CREATE_WRITE 
WAIT_EVENT_LOCK_FILE_RECHECKDATADIR_READ 
WAIT_EVENT_LOGICAL_REWRITE_CHECKPOINT_SYNC 
WAIT_EVENT_LOGICAL_REWRITE_MAPPING_SYNC 
WAIT_EVENT_LOGICAL_REWRITE_MAPPING_WRITE 
WAIT_EVENT_LOGICAL_REWRITE_SYNC 
WAIT_EVENT_LOGICAL_REWRITE_TRUNCATE 
WAIT_EVENT_LOGICAL_REWRITE_WRITE 
WAIT_EVENT_RELATION_MAP_READ 
WAIT_EVENT_RELATION_MAP_SYNC 
WAIT_EVENT_RELATION_MAP_WRITE 
WAIT_EVENT_REORDER_BUFFER_READ 
WAIT_EVENT_REORDER_BUFFER_WRITE 
WAIT_EVENT_REORDER_LOGICAL_MAPPING_READ 
WAIT_EVENT_REPLICATION_SLOT_READ 
WAIT_EVENT_REPLICATION_SLOT_RESTORE_SYNC 
WAIT_EVENT_REPLICATION_SLOT_SYNC 
WAIT_EVENT_REPLICATION_SLOT_WRITE 
WAIT_EVENT_SLRU_FLUSH_SYNC 
WAIT_EVENT_SLRU_READ 
WAIT_EVENT_SLRU_SYNC 
WAIT_EVENT_SLRU_WRITE 
WAIT_EVENT_SNAPBUILD_READ 
WAIT_EVENT_SNAPBUILD_SYNC 
WAIT_EVENT_SNAPBUILD_WRITE 
WAIT_EVENT_TIMELINE_HISTORY_FILE_SYNC 
WAIT_EVENT_TIMELINE_HISTORY_FILE_WRITE 
WAIT_EVENT_TIMELINE_HISTORY_READ 
WAIT_EVENT_TIMELINE_HISTORY_SYNC 
WAIT_EVENT_TIMELINE_HISTORY_WRITE 
WAIT_EVENT_TWOPHASE_FILE_READ 
WAIT_EVENT_TWOPHASE_FILE_SYNC 
WAIT_EVENT_TWOPHASE_FILE_WRITE 
WAIT_EVENT_WALSENDER_TIMELINE_HISTORY_READ 
WAIT_EVENT_WAL_BOOTSTRAP_SYNC 
WAIT_EVENT_WAL_BOOTSTRAP_WRITE 
WAIT_EVENT_WAL_COPY_READ 
WAIT_EVENT_WAL_COPY_SYNC 
WAIT_EVENT_WAL_COPY_WRITE 
WAIT_EVENT_WAL_INIT_SYNC 
WAIT_EVENT_WAL_INIT_WRITE 
WAIT_EVENT_WAL_READ 
WAIT_EVENT_WAL_SYNC_METHOD_ASSIGN 
WAIT_EVENT_WAL_WRITE 

Definition at line 837 of file pgstat.h.

838 {
906 } WaitEventIO;
#define PG_WAIT_IO
Definition: pgstat.h:745
WaitEventIO
Definition: pgstat.h:837
Enumerator
WAIT_EVENT_BGWORKER_SHUTDOWN 
WAIT_EVENT_BGWORKER_STARTUP 
WAIT_EVENT_BTREE_PAGE 
WAIT_EVENT_EXECUTE_GATHER 
WAIT_EVENT_MQ_INTERNAL 
WAIT_EVENT_MQ_PUT_MESSAGE 
WAIT_EVENT_MQ_RECEIVE 
WAIT_EVENT_MQ_SEND 
WAIT_EVENT_PARALLEL_FINISH 
WAIT_EVENT_PARALLEL_BITMAP_SCAN 
WAIT_EVENT_PROCARRAY_GROUP_UPDATE 
WAIT_EVENT_SAFE_SNAPSHOT 
WAIT_EVENT_SYNC_REP 
WAIT_EVENT_LOGICAL_SYNC_DATA 
WAIT_EVENT_LOGICAL_SYNC_STATE_CHANGE 

Definition at line 799 of file pgstat.h.

Enumerator
WAIT_EVENT_BASE_BACKUP_THROTTLE 
WAIT_EVENT_PG_SLEEP 
WAIT_EVENT_RECOVERY_APPLY_DELAY 

Definition at line 824 of file pgstat.h.

Function Documentation

void allow_immediate_pgstat_restart ( void  )

Definition at line 758 of file pgstat.c.

References last_pgstat_start_time.

Referenced by HandleChildCrash().

759 {
761 }
static time_t last_pgstat_start_time
Definition: pgstat.c:150
void AtEOSubXact_PgStat ( bool  isCommit,
int  nestDepth 
)

Definition at line 2100 of file pgstat.c.

References Assert, PgStat_SubXactStatus::first, get_tabstat_stack_level(), PgStat_TableXactStatus::nest_level, PgStat_SubXactStatus::nest_level, PgStat_TableXactStatus::next, NULL, 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().

2101 {
2102  PgStat_SubXactStatus *xact_state;
2103 
2104  /*
2105  * Transfer transactional insert/update counts into the next higher
2106  * subtransaction state.
2107  */
2108  xact_state = pgStatXactStack;
2109  if (xact_state != NULL &&
2110  xact_state->nest_level >= nestDepth)
2111  {
2113  PgStat_TableXactStatus *next_trans;
2114 
2115  /* delink xact_state from stack immediately to simplify reuse case */
2116  pgStatXactStack = xact_state->prev;
2117 
2118  for (trans = xact_state->first; trans != NULL; trans = next_trans)
2119  {
2120  PgStat_TableStatus *tabstat;
2121 
2122  next_trans = trans->next;
2123  Assert(trans->nest_level == nestDepth);
2124  tabstat = trans->parent;
2125  Assert(tabstat->trans == trans);
2126  if (isCommit)
2127  {
2128  if (trans->upper && trans->upper->nest_level == nestDepth - 1)
2129  {
2130  if (trans->truncated)
2131  {
2132  /* propagate the truncate status one level up */
2134  /* replace upper xact stats with ours */
2135  trans->upper->tuples_inserted = trans->tuples_inserted;
2136  trans->upper->tuples_updated = trans->tuples_updated;
2137  trans->upper->tuples_deleted = trans->tuples_deleted;
2138  }
2139  else
2140  {
2141  trans->upper->tuples_inserted += trans->tuples_inserted;
2142  trans->upper->tuples_updated += trans->tuples_updated;
2143  trans->upper->tuples_deleted += trans->tuples_deleted;
2144  }
2145  tabstat->trans = trans->upper;
2146  pfree(trans);
2147  }
2148  else
2149  {
2150  /*
2151  * When there isn't an immediate parent state, we can just
2152  * reuse the record instead of going through a
2153  * palloc/pfree pushup (this works since it's all in
2154  * TopTransactionContext anyway). We have to re-link it
2155  * into the parent level, though, and that might mean
2156  * pushing a new entry into the pgStatXactStack.
2157  */
2158  PgStat_SubXactStatus *upper_xact_state;
2159 
2160  upper_xact_state = get_tabstat_stack_level(nestDepth - 1);
2161  trans->next = upper_xact_state->first;
2162  upper_xact_state->first = trans;
2163  trans->nest_level = nestDepth - 1;
2164  }
2165  }
2166  else
2167  {
2168  /*
2169  * On abort, update top-level tabstat counts, then forget the
2170  * subtransaction
2171  */
2172 
2173  /* first restore values obliterated by truncate */
2175  /* count attempted actions regardless of commit/abort */
2176  tabstat->t_counts.t_tuples_inserted += trans->tuples_inserted;
2177  tabstat->t_counts.t_tuples_updated += trans->tuples_updated;
2178  tabstat->t_counts.t_tuples_deleted += trans->tuples_deleted;
2179  /* inserted tuples are dead, deleted tuples are unaffected */
2180  tabstat->t_counts.t_delta_dead_tuples +=
2181  trans->tuples_inserted + trans->tuples_updated;
2182  tabstat->trans = trans->upper;
2183  pfree(trans);
2184  }
2185  }
2186  pfree(xact_state);
2187  }
2188 }
PgStat_Counter tuples_updated
Definition: pgstat.h:168
struct PgStat_TableXactStatus * next
Definition: pgstat.h:179
PgStat_TableCounts t_counts
Definition: pgstat.h:158
static PgStat_SubXactStatus * get_tabstat_stack_level(int nest_level)
Definition: pgstat.c:1822
void pfree(void *pointer)
Definition: mcxt.c:950
PgStat_Counter t_tuples_updated
Definition: pgstat.h:105
static void pgstat_truncate_restore_counters(PgStat_TableXactStatus *trans)
Definition: pgstat.c:1958
PgStat_Counter tuples_inserted
Definition: pgstat.h:167
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:111
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:176
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:106
PgStat_Counter tuples_deleted
Definition: pgstat.h:169
struct PgStat_SubXactStatus * prev
Definition: pgstat.c:212
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:216
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:382
PgStat_TableXactStatus * first
Definition: pgstat.c:213
static void pgstat_truncate_save_counters(PgStat_TableXactStatus *trans)
Definition: pgstat.c:1943
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:104
PgStat_TableStatus * parent
Definition: pgstat.h:177
void AtEOXact_PgStat ( bool  isCommit)

Definition at line 2017 of file pgstat.c.

References Assert, PgStat_SubXactStatus::first, PgStat_TableXactStatus::nest_level, PgStat_SubXactStatus::nest_level, PgStat_TableXactStatus::next, NULL, 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().

2018 {
2019  PgStat_SubXactStatus *xact_state;
2020 
2021  /*
2022  * Count transaction commit or abort. (We use counters, not just bools,
2023  * in case the reporting message isn't sent right away.)
2024  */
2025  if (isCommit)
2026  pgStatXactCommit++;
2027  else
2029 
2030  /*
2031  * Transfer transactional insert/update counts into the base tabstat
2032  * entries. We don't bother to free any of the transactional state, since
2033  * it's all in TopTransactionContext and will go away anyway.
2034  */
2035  xact_state = pgStatXactStack;
2036  if (xact_state != NULL)
2037  {
2039 
2040  Assert(xact_state->nest_level == 1);
2041  Assert(xact_state->prev == NULL);
2042  for (trans = xact_state->first; trans != NULL; trans = trans->next)
2043  {
2044  PgStat_TableStatus *tabstat;
2045 
2046  Assert(trans->nest_level == 1);
2047  Assert(trans->upper == NULL);
2048  tabstat = trans->parent;
2049  Assert(tabstat->trans == trans);
2050  /* restore pre-truncate stats (if any) in case of aborted xact */
2051  if (!isCommit)
2053  /* count attempted actions regardless of commit/abort */
2054  tabstat->t_counts.t_tuples_inserted += trans->tuples_inserted;
2055  tabstat->t_counts.t_tuples_updated += trans->tuples_updated;
2056  tabstat->t_counts.t_tuples_deleted += trans->tuples_deleted;
2057  if (isCommit)
2058  {
2059  tabstat->t_counts.t_truncated = trans->truncated;
2060  if (trans->truncated)
2061  {
2062  /* forget live/dead stats seen by backend thus far */
2063  tabstat->t_counts.t_delta_live_tuples = 0;
2064  tabstat->t_counts.t_delta_dead_tuples = 0;
2065  }
2066  /* insert adds a live tuple, delete removes one */
2067  tabstat->t_counts.t_delta_live_tuples +=
2068  trans->tuples_inserted - trans->tuples_deleted;
2069  /* update and delete each create a dead tuple */
2070  tabstat->t_counts.t_delta_dead_tuples +=
2071  trans->tuples_updated + trans->tuples_deleted;
2072  /* insert, update, delete each count as one change event */
2073  tabstat->t_counts.t_changed_tuples +=
2074  trans->tuples_inserted + trans->tuples_updated +
2075  trans->tuples_deleted;
2076  }
2077  else
2078  {
2079  /* inserted tuples are dead, deleted tuples are unaffected */
2080  tabstat->t_counts.t_delta_dead_tuples +=
2081  trans->tuples_inserted + trans->tuples_updated;
2082  /* an aborted xact generates no changed_tuple events */
2083  }
2084  tabstat->trans = NULL;
2085  }
2086  }
2088 
2089  /* Make sure any stats snapshot is thrown away */
2091 }
PgStat_Counter t_delta_live_tuples
Definition: pgstat.h:110
PgStat_Counter tuples_updated
Definition: pgstat.h:168
static int pgStatXactCommit
Definition: pgstat.c:218
struct PgStat_TableXactStatus * next
Definition: pgstat.h:179
PgStat_TableCounts t_counts
Definition: pgstat.h:158
PgStat_Counter t_tuples_updated
Definition: pgstat.h:105
static void pgstat_truncate_restore_counters(PgStat_TableXactStatus *trans)
Definition: pgstat.c:1958
static int pgStatXactRollback
Definition: pgstat.c:219
PgStat_Counter tuples_inserted
Definition: pgstat.h:167
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:111
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:176
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:106
PgStat_Counter tuples_deleted
Definition: pgstat.h:169
struct PgStat_SubXactStatus * prev
Definition: pgstat.c:212
PgStat_Counter t_changed_tuples
Definition: pgstat.h:112
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void pgstat_clear_snapshot(void)
Definition: pgstat.c:5482
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:216
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:382
PgStat_TableXactStatus * first
Definition: pgstat.c:213
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:104
PgStat_TableStatus * parent
Definition: pgstat.h:177
void AtPrepare_PgStat ( void  )

Definition at line 2199 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, NULL, 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().

2200 {
2201  PgStat_SubXactStatus *xact_state;
2202 
2203  xact_state = pgStatXactStack;
2204  if (xact_state != NULL)
2205  {
2207 
2208  Assert(xact_state->nest_level == 1);
2209  Assert(xact_state->prev == NULL);
2210  for (trans = xact_state->first; trans != NULL; trans = trans->next)
2211  {
2212  PgStat_TableStatus *tabstat;
2213  TwoPhasePgStatRecord record;
2214 
2215  Assert(trans->nest_level == 1);
2216  Assert(trans->upper == NULL);
2217  tabstat = trans->parent;
2218  Assert(tabstat->trans == trans);
2219 
2220  record.tuples_inserted = trans->tuples_inserted;
2221  record.tuples_updated = trans->tuples_updated;
2222  record.tuples_deleted = trans->tuples_deleted;
2223  record.inserted_pre_trunc = trans->inserted_pre_trunc;
2224  record.updated_pre_trunc = trans->updated_pre_trunc;
2225  record.deleted_pre_trunc = trans->deleted_pre_trunc;
2226  record.t_id = tabstat->t_id;
2227  record.t_shared = tabstat->t_shared;
2228  record.t_truncated = trans->truncated;
2229 
2231  &record, sizeof(TwoPhasePgStatRecord));
2232  }
2233  }
2234 }
PgStat_Counter tuples_updated
Definition: pgstat.h:168
struct PgStat_TableXactStatus * next
Definition: pgstat.h:179
PgStat_Counter deleted_pre_trunc
Definition: pgstat.h:173
PgStat_Counter deleted_pre_trunc
Definition: pgstat.c:231
void RegisterTwoPhaseRecord(TwoPhaseRmgrId rmid, uint16 info, const void *data, uint32 len)
Definition: twophase.c:1165
PgStat_Counter tuples_inserted
Definition: pgstat.h:167
PgStat_Counter updated_pre_trunc
Definition: pgstat.c:230
PgStat_Counter inserted_pre_trunc
Definition: pgstat.c:229
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:176
PgStat_Counter tuples_updated
Definition: pgstat.c:227
PgStat_Counter tuples_deleted
Definition: pgstat.h:169
struct PgStat_SubXactStatus * prev
Definition: pgstat.c:212
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
PgStat_Counter inserted_pre_trunc
Definition: pgstat.h:171
PgStat_Counter tuples_inserted
Definition: pgstat.c:226
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:216
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
PgStat_Counter tuples_deleted
Definition: pgstat.c:228
#define TWOPHASE_RM_PGSTAT_ID
Definition: twophase_rmgr.h:26
PgStat_Counter updated_pre_trunc
Definition: pgstat.h:172
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:382
PgStat_TableXactStatus * first
Definition: pgstat.c:213
PgStat_TableStatus * parent
Definition: pgstat.h:177
Size BackendStatusShmemSize ( void  )

Definition at line 2568 of file pgstat.c.

References add_size(), mul_size(), NAMEDATALEN, NumBackendStatSlots, and pgstat_track_activity_query_size.

Referenced by CreateSharedMemoryAndSemaphores().

2569 {
2570  Size size;
2571 
2572  /* BackendStatusArray: */
2573  size = mul_size(sizeof(PgBackendStatus), NumBackendStatSlots);
2574  /* BackendAppnameBuffer: */
2575  size = add_size(size,
2577  /* BackendClientHostnameBuffer: */
2578  size = add_size(size,
2580  /* BackendActivityBuffer: */
2581  size = add_size(size,
2583 #ifdef USE_SSL
2584  /* BackendSslStatusBuffer: */
2585  size = add_size(size,
2587 #endif
2588  return size;
2589 }
#define NumBackendStatSlots
Definition: pgstat.c:115
#define NAMEDATALEN
int pgstat_track_activity_query_size
Definition: pgstat.c:125
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
size_t Size
Definition: c.h:356
void CreateSharedBackendStatus ( void  )

Definition at line 2596 of file pgstat.c.

References BackendActivityBuffer, BackendAppnameBuffer, BackendClientHostnameBuffer, buffer, i, MaxBackends, MemSet, mul_size(), NAMEDATALEN, NumBackendStatSlots, pgstat_track_activity_query_size, ShmemInitStruct(), PgBackendStatus::st_activity, PgBackendStatus::st_appname, PgBackendStatus::st_clienthostname, and PgBackendStatus::st_sslstatus.

Referenced by CreateSharedMemoryAndSemaphores().

2597 {
2598  Size size;
2599  bool found;
2600  int i;
2601  char *buffer;
2602 
2603  /* Create or attach to the shared array */
2604  size = mul_size(sizeof(PgBackendStatus), NumBackendStatSlots);
2606  ShmemInitStruct("Backend Status Array", size, &found);
2607 
2608  if (!found)
2609  {
2610  /*
2611  * We're the first - initialize.
2612  */
2613  MemSet(BackendStatusArray, 0, size);
2614  }
2615 
2616  /* Create or attach to the shared appname buffer */
2617  size = mul_size(NAMEDATALEN, MaxBackends);
2618  BackendAppnameBuffer = (char *)
2619  ShmemInitStruct("Backend Application Name Buffer", size, &found);
2620 
2621  if (!found)
2622  {
2623  MemSet(BackendAppnameBuffer, 0, size);
2624 
2625  /* Initialize st_appname pointers. */
2626  buffer = BackendAppnameBuffer;
2627  for (i = 0; i < NumBackendStatSlots; i++)
2628  {
2630  buffer += NAMEDATALEN;
2631  }
2632  }
2633 
2634  /* Create or attach to the shared client hostname buffer */
2635  size = mul_size(NAMEDATALEN, MaxBackends);
2636  BackendClientHostnameBuffer = (char *)
2637  ShmemInitStruct("Backend Client Host Name Buffer", size, &found);
2638 
2639  if (!found)
2640  {
2642 
2643  /* Initialize st_clienthostname pointers. */
2644  buffer = BackendClientHostnameBuffer;
2645  for (i = 0; i < NumBackendStatSlots; i++)
2646  {
2648  buffer += NAMEDATALEN;
2649  }
2650  }
2651 
2652  /* Create or attach to the shared activity buffer */
2655  BackendActivityBuffer = (char *)
2656  ShmemInitStruct("Backend Activity Buffer",
2658  &found);
2659 
2660  if (!found)
2661  {
2662  MemSet(BackendActivityBuffer, 0, size);
2663 
2664  /* Initialize st_activity pointers. */
2665  buffer = BackendActivityBuffer;
2666  for (i = 0; i < NumBackendStatSlots; i++)
2667  {
2670  }
2671  }
2672 
2673 #ifdef USE_SSL
2674  /* Create or attach to the shared SSL status buffer */
2676  BackendSslStatusBuffer = (PgBackendSSLStatus *)
2677  ShmemInitStruct("Backend SSL Status Buffer", size, &found);
2678 
2679  if (!found)
2680  {
2681  PgBackendSSLStatus *ptr;
2682 
2683  MemSet(BackendSslStatusBuffer, 0, size);
2684 
2685  /* Initialize st_sslstatus pointers. */
2686  ptr = BackendSslStatusBuffer;
2687  for (i = 0; i < NumBackendStatSlots; i++)
2688  {
2690  ptr++;
2691  }
2692  }
2693 #endif
2694 }
static PgBackendStatus * BackendStatusArray
Definition: pgstat.c:2553
static char * BackendActivityBuffer
Definition: pgstat.c:2557
static Size BackendActivityBufferSize
Definition: pgstat.c:2558
#define MemSet(start, val, len)
Definition: c.h:857
#define NumBackendStatSlots
Definition: pgstat.c:115
char * st_clienthostname
Definition: pgstat.h:990
#define NAMEDATALEN
static char * BackendAppnameBuffer
Definition: pgstat.c:2555
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
int MaxBackends
Definition: globals.c:126
int pgstat_track_activity_query_size
Definition: pgstat.c:125
char * st_appname
Definition: pgstat.h:1000
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
char * st_activity
Definition: pgstat.h:1003
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:207
size_t Size
Definition: c.h:356
int i
PgBackendSSLStatus * st_sslstatus
Definition: pgstat.h:994
static char * BackendClientHostnameBuffer
Definition: pgstat.c:2556
PgStat_BackendFunctionEntry* find_funcstat_entry ( Oid  func_id)

Definition at line 1598 of file pgstat.c.

References HASH_FIND, hash_search(), and NULL.

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

1599 {
1600  if (pgStatFunctions == NULL)
1601  return NULL;
1602 
1604  (void *) &func_id,
1605  HASH_FIND, NULL);
1606 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
static HTAB * pgStatFunctions
Definition: pgstat.c:194
#define NULL
Definition: c.h:229
PgStat_TableStatus* find_tabstat_entry ( Oid  rel_id)

Definition at line 1801 of file pgstat.c.

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

1802 {
1803  TabStatHashEntry* hash_entry;
1804 
1805  /*
1806  * There are no entries at all.
1807  */
1808  if(!pgStatTabHash)
1809  return NULL;
1810 
1811  hash_entry = hash_search(pgStatTabHash, &rel_id, HASH_FIND, NULL);
1812  if(!hash_entry)
1813  return NULL;
1814 
1815  return hash_entry->tsa_entry;
1816 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
static HTAB * pgStatTabHash
Definition: pgstat.c:188
PgStat_TableStatus * tsa_entry
Definition: pgstat.c:182
#define NULL
Definition: c.h:229
void pgstat_bestart ( void  )

Definition at line 2749 of file pgstat.c.

References am_walsender, application_name, Assert, B_AUTOVAC_LAUNCHER, B_AUTOVAC_WORKER, B_BACKEND, B_BG_WORKER, B_BG_WRITER, B_CHECKPOINTER, B_STARTUP, B_WAL_RECEIVER, B_WAL_SENDER, B_WAL_WRITER, be_tls_get_cipher(), be_tls_get_cipher_bits(), be_tls_get_compression(), be_tls_get_peerdn_name(), be_tls_get_version(), BgWriterProcess, CheckpointerProcess, elog, FATAL, GetCurrentTimestamp(), GetSessionUserId(), InvalidBackendId, InvalidOid, IsAutoVacuumLauncherProcess(), IsAutoVacuumWorkerProcess(), IsBackgroundWorker, MemSet, MyAuxProcType, MyBackendId, MyBEEntry, MyDatabaseId, MyProcPid, MyProcPort, NAMEDATALEN, NotAnAuxProcess, NULL, pgstat_increment_changecount_after, pgstat_increment_changecount_before, pgstat_report_appname(), pgstat_track_activity_query_size, proc_exit(), PROGRESS_COMMAND_INVALID, Port::raddr, Port::remote_hostname, Port::SessionStartTime, PgBackendSSLStatus::ssl_bits, PgBackendSSLStatus::ssl_cipher, PgBackendSSLStatus::ssl_clientdn, PgBackendSSLStatus::ssl_compression, PgBackendSSLStatus::ssl_version, PgBackendStatus::st_activity, PgBackendStatus::st_activity_start_timestamp, PgBackendStatus::st_appname, PgBackendStatus::st_backendType, PgBackendStatus::st_changecount, PgBackendStatus::st_clientaddr, PgBackendStatus::st_clienthostname, PgBackendStatus::st_databaseid, PgBackendStatus::st_proc_start_timestamp, PgBackendStatus::st_procpid, PgBackendStatus::st_progress_command, PgBackendStatus::st_progress_command_target, PgBackendStatus::st_ssl, PgBackendStatus::st_sslstatus, PgBackendStatus::st_state, PgBackendStatus::st_state_start_timestamp, PgBackendStatus::st_userid, PgBackendStatus::st_xact_start_timestamp, StartupProcess, STATE_UNDEFINED, strlcpy(), WalReceiverProcess, and WalWriterProcess.

Referenced by AuxiliaryProcessMain(), and InitPostgres().

2750 {
2751  TimestampTz proc_start_timestamp;
2752  SockAddr clientaddr;
2753  volatile PgBackendStatus *beentry;
2754 
2755  /*
2756  * To minimize the time spent modifying the PgBackendStatus entry, fetch
2757  * all the needed data first.
2758  *
2759  * If we have a MyProcPort, use its session start time (for consistency,
2760  * and to save a kernel call).
2761  */
2762  if (MyProcPort)
2763  proc_start_timestamp = MyProcPort->SessionStartTime;
2764  else
2765  proc_start_timestamp = GetCurrentTimestamp();
2766 
2767  /*
2768  * We may not have a MyProcPort (eg, if this is the autovacuum process).
2769  * If so, use all-zeroes client address, which is dealt with specially in
2770  * pg_stat_get_backend_client_addr and pg_stat_get_backend_client_port.
2771  */
2772  if (MyProcPort)
2773  memcpy(&clientaddr, &MyProcPort->raddr, sizeof(clientaddr));
2774  else
2775  MemSet(&clientaddr, 0, sizeof(clientaddr));
2776 
2777  /*
2778  * Initialize my status entry, following the protocol of bumping
2779  * st_changecount before and after; and make sure it's even afterwards. We
2780  * use a volatile pointer here to ensure the compiler doesn't try to get
2781  * cute.
2782  */
2783  beentry = MyBEEntry;
2784 
2785  /* pgstats state must be initialized from pgstat_initialize() */
2786  Assert(beentry != NULL);
2787 
2789  {
2791  {
2792  /* Autovacuum Launcher */
2794  }
2795  else if (IsAutoVacuumWorkerProcess())
2796  {
2797  /* Autovacuum Worker */
2798  beentry->st_backendType = B_AUTOVAC_WORKER;
2799  }
2800  else if (am_walsender)
2801  {
2802  /* Wal sender */
2803  beentry->st_backendType = B_WAL_SENDER;
2804  }
2805  else if (IsBackgroundWorker)
2806  {
2807  /* bgworker */
2808  beentry->st_backendType = B_BG_WORKER;
2809  }
2810  else
2811  {
2812  /* client-backend */
2813  beentry->st_backendType = B_BACKEND;
2814  }
2815  }
2816  else
2817  {
2818  /* Must be an auxiliary process */
2820  switch (MyAuxProcType)
2821  {
2822  case StartupProcess:
2823  beentry->st_backendType = B_STARTUP;
2824  break;
2825  case BgWriterProcess:
2826  beentry->st_backendType = B_BG_WRITER;
2827  break;
2828  case CheckpointerProcess:
2829  beentry->st_backendType = B_CHECKPOINTER;
2830  break;
2831  case WalWriterProcess:
2832  beentry->st_backendType = B_WAL_WRITER;
2833  break;
2834  case WalReceiverProcess:
2835  beentry->st_backendType = B_WAL_RECEIVER;
2836  break;
2837  default:
2838  elog(FATAL, "unrecognized process type: %d",
2839  (int) MyAuxProcType);
2840  proc_exit(1);
2841  }
2842  }
2843 
2844  do
2845  {
2847  } while ((beentry->st_changecount & 1) == 0);
2848 
2849  beentry->st_procpid = MyProcPid;
2850  beentry->st_proc_start_timestamp = proc_start_timestamp;
2851  beentry->st_activity_start_timestamp = 0;
2852  beentry->st_state_start_timestamp = 0;
2853  beentry->st_xact_start_timestamp = 0;
2854  beentry->st_databaseid = MyDatabaseId;
2855 
2856  /* We have userid for client-backends, wal-sender and bgworker processes */
2857  if (beentry->st_backendType == B_BACKEND
2858  || beentry->st_backendType == B_WAL_SENDER
2859  || beentry->st_backendType == B_BG_WORKER)
2860  beentry->st_userid = GetSessionUserId();
2861  else
2862  beentry->st_userid = InvalidOid;
2863 
2864  beentry->st_clientaddr = clientaddr;
2867  NAMEDATALEN);
2868  else
2869  beentry->st_clienthostname[0] = '\0';
2870 #ifdef USE_SSL
2871  if (MyProcPort && MyProcPort->ssl != NULL)
2872  {
2873  beentry->st_ssl = true;
2879  }
2880  else
2881  {
2882  beentry->st_ssl = false;
2883  }
2884 #else
2885  beentry->st_ssl = false;
2886 #endif
2887  beentry->st_state = STATE_UNDEFINED;
2888  beentry->st_appname[0] = '\0';
2889  beentry->st_activity[0] = '\0';
2890  /* Also make sure the last byte in each string area is always 0 */
2891  beentry->st_clienthostname[NAMEDATALEN - 1] = '\0';
2892  beentry->st_appname[NAMEDATALEN - 1] = '\0';
2893  beentry->st_activity[pgstat_track_activity_query_size - 1] = '\0';
2896 
2897  /*
2898  * we don't zero st_progress_param here to save cycles; nobody should
2899  * examine it until st_progress_command has been set to something other
2900  * than PROGRESS_COMMAND_INVALID
2901  */
2902 
2904 
2905  /* Update app name to current GUC setting */
2906  if (application_name)
2908 }
bool ssl_compression
Definition: pgstat.h:936
char ssl_cipher[NAMEDATALEN]
Definition: pgstat.h:938
Oid st_progress_command_target
Definition: pgstat.h:1015
int MyProcPid
Definition: globals.c:38
BackendId MyBackendId
Definition: globals.c:72
struct Port * MyProcPort
Definition: globals.c:40
TimestampTz st_activity_start_timestamp
Definition: pgstat.h:983
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
int64 TimestampTz
Definition: timestamp.h:39
char ssl_version[NAMEDATALEN]
Definition: pgstat.h:937
SockAddr st_clientaddr
Definition: pgstat.h:989
void proc_exit(int code)
Definition: ipc.c:99
#define MemSet(start, val, len)
Definition: c.h:857
ProgressCommandType st_progress_command
Definition: pgstat.h:1014
BackendState st_state
Definition: pgstat.h:997
char * st_clienthostname
Definition: pgstat.h:990
Oid GetSessionUserId(void)
Definition: miscinit.c:317
bool IsBackgroundWorker
Definition: globals.c:102
Oid st_databaseid
Definition: pgstat.h:987
#define NAMEDATALEN
char * remote_hostname
Definition: libpq-be.h:124
SockAddr raddr
Definition: libpq-be.h:122
bool am_walsender
Definition: walsender.c:108
bool be_tls_get_compression(Port *port)
#define FATAL
Definition: elog.h:52
TimestampTz SessionStartTime
Definition: libpq-be.h:151
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2554
#define pgstat_increment_changecount_before(beentry)
Definition: pgstat.h:1031
int pgstat_track_activity_query_size
Definition: pgstat.c:125
TimestampTz st_state_start_timestamp
Definition: pgstat.h:984
#define pgstat_increment_changecount_after(beentry)
Definition: pgstat.h:1037
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3360
char * st_appname
Definition: pgstat.h:1000
#define InvalidBackendId
Definition: backendid.h:23
int be_tls_get_cipher_bits(Port *port)
AuxProcType MyAuxProcType
Definition: bootstrap.c:68
Oid MyDatabaseId
Definition: globals.c:76
BackendType st_backendType
Definition: pgstat.h:978
void be_tls_get_version(Port *port, char *ptr, size_t len)
void pgstat_report_appname(const char *appname)
Definition: pgstat.c:3127
#define InvalidOid
Definition: postgres_ext.h:36
void be_tls_get_peerdn_name(Port *port, char *ptr, size_t len)
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
bool IsAutoVacuumLauncherProcess(void)
Definition: autovacuum.c:3354
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
char * st_activity
Definition: pgstat.h:1003
int st_procpid
Definition: pgstat.h:975
char * application_name
Definition: guc.c:473
void be_tls_get_cipher(Port *port, char *ptr, size_t len)
TimestampTz st_xact_start_timestamp
Definition: pgstat.h:982
int st_changecount
Definition: pgstat.h:972
#define elog
Definition: elog.h:219
TimestampTz st_proc_start_timestamp
Definition: pgstat.h:981
PgBackendSSLStatus * st_sslstatus
Definition: pgstat.h:994
char ssl_clientdn[NAMEDATALEN]
Definition: pgstat.h:939
void pgstat_clear_snapshot ( void  )

Definition at line 5482 of file pgstat.c.

References localNumBackends, MemoryContextDelete(), and NULL.

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

5483 {
5484  /* Release memory, if any was allocated */
5485  if (pgStatLocalContext)
5487 
5488  /* Reset variables */
5490  pgStatDBHash = NULL;
5492  localNumBackends = 0;
5493 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
#define NULL
Definition: c.h:229
static LocalPgBackendStatus * localBackendStatusTable
Definition: pgstat.c:244
static HTAB * pgStatDBHash
Definition: pgstat.c:241
static int localNumBackends
Definition: pgstat.c:247
static MemoryContext pgStatLocalContext
Definition: pgstat.c:240
void pgstat_count_heap_delete ( Relation  rel)

Definition at line 1917 of file pgstat.c.

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

Referenced by heap_abort_speculative(), and heap_delete().

1918 {
1919  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1920 
1921  if (pgstat_info != NULL)
1922  {
1923  /* We have to log the effect at the proper transactional level */
1924  int nest_level = GetCurrentTransactionNestLevel();
1925 
1926  if (pgstat_info->trans == NULL ||
1927  pgstat_info->trans->nest_level != nest_level)
1928  add_tabstat_xact_level(pgstat_info, nest_level);
1929 
1930  pgstat_info->trans->tuples_deleted++;
1931  }
1932 }
PgStat_Counter tuples_deleted
Definition: pgstat.h:169
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:761
#define NULL
Definition: c.h:229
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:218
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
Definition: pgstat.c:1844
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
void pgstat_count_heap_insert ( Relation  rel,
PgStat_Counter  n 
)

Definition at line 1871 of file pgstat.c.

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

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

1872 {
1873  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1874 
1875  if (pgstat_info != NULL)
1876  {
1877  /* We have to log the effect at the proper transactional level */
1878  int nest_level = GetCurrentTransactionNestLevel();
1879 
1880  if (pgstat_info->trans == NULL ||
1881  pgstat_info->trans->nest_level != nest_level)
1882  add_tabstat_xact_level(pgstat_info, nest_level);
1883 
1884  pgstat_info->trans->tuples_inserted += n;
1885  }
1886 }
PgStat_Counter tuples_inserted
Definition: pgstat.h:167
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:761
#define NULL
Definition: c.h:229
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:218
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
Definition: pgstat.c:1844
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
void pgstat_count_heap_update ( Relation  rel,
bool  hot 
)

Definition at line 1892 of file pgstat.c.

References add_tabstat_xact_level(), GetCurrentTransactionNestLevel(), PgStat_TableXactStatus::nest_level, NULL, 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().

1893 {
1894  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1895 
1896  if (pgstat_info != NULL)
1897  {
1898  /* We have to log the effect at the proper transactional level */
1899  int nest_level = GetCurrentTransactionNestLevel();
1900 
1901  if (pgstat_info->trans == NULL ||
1902  pgstat_info->trans->nest_level != nest_level)
1903  add_tabstat_xact_level(pgstat_info, nest_level);
1904 
1905  pgstat_info->trans->tuples_updated++;
1906 
1907  /* t_tuples_hot_updated is nontransactional, so just advance it */
1908  if (hot)
1909  pgstat_info->t_counts.t_tuples_hot_updated++;
1910  }
1911 }
PgStat_Counter tuples_updated
Definition: pgstat.h:168
PgStat_Counter t_tuples_hot_updated
Definition: pgstat.h:107
PgStat_TableCounts t_counts
Definition: pgstat.h:158
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:761
#define NULL
Definition: c.h:229
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:218
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
Definition: pgstat.c:1844
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
void pgstat_count_truncate ( Relation  rel)

Definition at line 1972 of file pgstat.c.

References add_tabstat_xact_level(), GetCurrentTransactionNestLevel(), PgStat_TableXactStatus::nest_level, NULL, 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 ExecuteTruncate().

1973 {
1974  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1975 
1976  if (pgstat_info != NULL)
1977  {
1978  /* We have to log the effect at the proper transactional level */
1979  int nest_level = GetCurrentTransactionNestLevel();
1980 
1981  if (pgstat_info->trans == NULL ||
1982  pgstat_info->trans->nest_level != nest_level)
1983  add_tabstat_xact_level(pgstat_info, nest_level);
1984 
1985  pgstat_truncate_save_counters(pgstat_info->trans);
1986  pgstat_info->trans->tuples_inserted = 0;
1987  pgstat_info->trans->tuples_updated = 0;
1988  pgstat_info->trans->tuples_deleted = 0;
1989  }
1990 }
PgStat_Counter tuples_updated
Definition: pgstat.h:168
PgStat_Counter tuples_inserted
Definition: pgstat.h:167
PgStat_Counter tuples_deleted
Definition: pgstat.h:169
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:761
#define NULL
Definition: c.h:229
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:218
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
Definition: pgstat.c:1844
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
static void pgstat_truncate_save_counters(PgStat_TableXactStatus *trans)
Definition: pgstat.c:1943
void pgstat_drop_database ( Oid  databaseid)

Definition at line 1215 of file pgstat.c.

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

Referenced by dropdb(), and pgstat_vacuum_stat().

1216 {
1217  PgStat_MsgDropdb msg;
1218 
1220  return;
1221 
1223  msg.m_databaseid = databaseid;
1224  pgstat_send(&msg, sizeof(msg));
1225 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:4054
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:146
Oid m_databaseid
Definition: pgstat.h:306
PgStat_MsgHdr m_hdr
Definition: pgstat.h:305
#define PGINVALID_SOCKET
Definition: port.h:24
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:4041
void pgstat_end_function_usage ( PgStat_FunctionCallUsage fcu,
bool  finalize 
)

Definition at line 1618 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, NULL, PgStat_FunctionCallUsage::save_f_total_time, PgStat_FunctionCallUsage::save_total, and total_func_time.

Referenced by call_pltcl_start_proc(), EventTriggerInvoke(), ExecCallTriggerFunc(), ExecInterpExpr(), ExecMakeFunctionResultSet(), ExecMakeTableFunctionResult(), and fmgr_security_definer().

1619 {
1620  PgStat_FunctionCounts *fs = fcu->fs;
1621  instr_time f_total;
1622  instr_time f_others;
1623  instr_time f_self;
1624 
1625  /* stats not wanted? */
1626  if (fs == NULL)
1627  return;
1628 
1629  /* total elapsed time in this function call */
1630  INSTR_TIME_SET_CURRENT(f_total);
1631  INSTR_TIME_SUBTRACT(f_total, fcu->f_start);
1632 
1633  /* self usage: elapsed minus anything already charged to other calls */
1634  f_others = total_func_time;
1635  INSTR_TIME_SUBTRACT(f_others, fcu->save_total);
1636  f_self = f_total;
1637  INSTR_TIME_SUBTRACT(f_self, f_others);
1638 
1639  /* update backend-wide total time */
1641 
1642  /*
1643  * Compute the new f_total_time as the total elapsed time added to the
1644  * pre-call value of f_total_time. This is necessary to avoid
1645  * double-counting any time taken by recursive calls of myself. (We do
1646  * not need any similar kluge for self time, since that already excludes
1647  * any recursive calls.)
1648  */
1649  INSTR_TIME_ADD(f_total, fcu->save_f_total_time);
1650 
1651  /* update counters in function stats table */
1652  if (finalize)
1653  fs->f_numcalls++;
1654  fs->f_total_time = f_total;
1655  INSTR_TIME_ADD(fs->f_self_time, f_self);
1656 
1657  /* indicate that we have something to send */
1658  have_function_stats = true;
1659 }
instr_time f_self_time
Definition: pgstat.h:463
instr_time save_total
Definition: pgstat.h:1095
struct timeval instr_time
Definition: instr_time.h:147
instr_time f_total_time
Definition: pgstat.h:462
PgStat_FunctionCounts * fs
Definition: pgstat.h:1091
PgStat_Counter f_numcalls
Definition: pgstat.h:461
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:167
#define INSTR_TIME_ADD(x, y)
Definition: instr_time.h:155
static instr_time total_func_time
Definition: pgstat.c:273
static bool have_function_stats
Definition: pgstat.c:200
#define NULL
Definition: c.h:229
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:153
instr_time save_f_total_time
Definition: pgstat.h:1093
PgStat_ArchiverStats* pgstat_fetch_stat_archiver ( void  )

Definition at line 2523 of file pgstat.c.

References archiverStats, and backend_read_statsfile().

Referenced by pg_stat_get_archiver().

2524 {
2526 
2527  return &archiverStats;
2528 }
static void backend_read_statsfile(void)
Definition: pgstat.c:5328
static PgStat_ArchiverStats archiverStats
Definition: pgstat.c:254
PgBackendStatus* pgstat_fetch_stat_beentry ( int  beid)
PgStat_StatDBEntry* pgstat_fetch_stat_dbentry ( Oid  dbid)

Definition at line 2351 of file pgstat.c.

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

Referenced by do_autovacuum(), do_start_worker(), 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_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_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 table_recheck_autovac().

2352 {
2353  /*
2354  * If not done for this transaction, read the statistics collector stats
2355  * file into some hash tables.
2356  */
2358 
2359  /*
2360  * Lookup the requested database; return NULL if not found
2361  */
2363  (void *) &dbid,
2364  HASH_FIND, NULL);
2365 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
static void backend_read_statsfile(void)
Definition: pgstat.c:5328
#define NULL
Definition: c.h:229
static HTAB * pgStatDBHash
Definition: pgstat.c:241
PgStat_StatFuncEntry* pgstat_fetch_stat_funcentry ( Oid  funcid)

Definition at line 2434 of file pgstat.c.

References backend_read_statsfile(), PgStat_StatDBEntry::functions, HASH_FIND, hash_search(), MyDatabaseId, NULL, 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().

2435 {
2436  PgStat_StatDBEntry *dbentry;
2437  PgStat_StatFuncEntry *funcentry = NULL;
2438 
2439  /* load the stats file if needed */
2441 
2442  /* Lookup our database, then find the requested function. */
2444  if (dbentry != NULL && dbentry->functions != NULL)
2445  {
2446  funcentry = (PgStat_StatFuncEntry *) hash_search(dbentry->functions,
2447  (void *) &func_id,
2448  HASH_FIND, NULL);
2449  }
2450 
2451  return funcentry;
2452 }
HTAB * functions
Definition: pgstat.h:607
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2351
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
static void backend_read_statsfile(void)
Definition: pgstat.c:5328
Oid MyDatabaseId
Definition: globals.c:76
#define NULL
Definition: c.h:229
LocalPgBackendStatus* pgstat_fetch_stat_local_beentry ( int  beid)

Definition at line 2488 of file pgstat.c.

References localNumBackends, NULL, and pgstat_read_current_status().

Referenced by pg_stat_get_activity(), and pg_stat_get_progress_info().

2489 {
2491 
2492  if (beid < 1 || beid > localNumBackends)
2493  return NULL;
2494 
2495  return &localBackendStatusTable[beid - 1];
2496 }
static void pgstat_read_current_status(void)
Definition: pgstat.c:3181
#define NULL
Definition: c.h:229
static LocalPgBackendStatus * localBackendStatusTable
Definition: pgstat.c:244
static int localNumBackends
Definition: pgstat.c:247
int pgstat_fetch_stat_numbackends ( void  )

Definition at line 2507 of file pgstat.c.

References localNumBackends, and pgstat_read_current_status().

Referenced by pg_stat_get_activity(), pg_stat_get_backend_idset(), pg_stat_get_db_numbackends(), and pg_stat_get_progress_info().

2508 {
2510 
2511  return localNumBackends;
2512 }
static void pgstat_read_current_status(void)
Definition: pgstat.c:3181
static int localNumBackends
Definition: pgstat.c:247
PgStat_StatTabEntry* pgstat_fetch_stat_tabentry ( Oid  relid)

Definition at line 2378 of file pgstat.c.

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

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

2379 {
2380  Oid dbid;
2381  PgStat_StatDBEntry *dbentry;
2382  PgStat_StatTabEntry *tabentry;
2383 
2384  /*
2385  * If not done for this transaction, read the statistics collector stats
2386  * file into some hash tables.
2387  */
2389 
2390  /*
2391  * Lookup our database, then look in its table hash table.
2392  */
2393  dbid = MyDatabaseId;
2395  (void *) &dbid,
2396  HASH_FIND, NULL);
2397  if (dbentry != NULL && dbentry->tables != NULL)
2398  {
2399  tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2400  (void *) &relid,
2401  HASH_FIND, NULL);
2402  if (tabentry)
2403  return tabentry;
2404  }
2405 
2406  /*
2407  * If we didn't find it, maybe it's a shared table.
2408  */
2409  dbid = InvalidOid;
2411  (void *) &dbid,
2412  HASH_FIND, NULL);
2413  if (dbentry != NULL && dbentry->tables != NULL)
2414  {
2415  tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2416  (void *) &relid,
2417  HASH_FIND, NULL);
2418  if (tabentry)
2419  return tabentry;
2420  }
2421 
2422  return NULL;
2423 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
unsigned int Oid
Definition: postgres_ext.h:31
static void backend_read_statsfile(void)
Definition: pgstat.c:5328
Oid MyDatabaseId
Definition: globals.c:76
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
static HTAB * pgStatDBHash
Definition: pgstat.c:241
const char* pgstat_get_backend_current_activity ( int  pid,
bool  checkUser 
)

Definition at line 3855 of file pgstat.c.

References BackendStatusArray, CHECK_FOR_INTERRUPTS, GetUserId(), i, MaxBackends, pgstat_save_changecount_after, pgstat_save_changecount_before, PgBackendStatus::st_activity, PgBackendStatus::st_procpid, PgBackendStatus::st_userid, and superuser().

Referenced by DeadLockReport().

3856 {
3857  PgBackendStatus *beentry;
3858  int i;
3859 
3860  beentry = BackendStatusArray;
3861  for (i = 1; i <= MaxBackends; i++)
3862  {
3863  /*
3864  * Although we expect the target backend's entry to be stable, that
3865  * doesn't imply that anyone else's is. To avoid identifying the
3866  * wrong backend, while we check for a match to the desired PID we
3867  * must follow the protocol of retrying if st_changecount changes
3868  * while we examine the entry, or if it's odd. (This might be
3869  * unnecessary, since fetching or storing an int is almost certainly
3870  * atomic, but let's play it safe.) We use a volatile pointer here to
3871  * ensure the compiler doesn't try to get cute.
3872  */
3873  volatile PgBackendStatus *vbeentry = beentry;
3874  bool found;
3875 
3876  for (;;)
3877  {
3878  int before_changecount;
3879  int after_changecount;
3880 
3881  pgstat_save_changecount_before(vbeentry, before_changecount);
3882 
3883  found = (vbeentry->st_procpid == pid);
3884 
3885  pgstat_save_changecount_after(vbeentry, after_changecount);
3886 
3887  if (before_changecount == after_changecount &&
3888  (before_changecount & 1) == 0)
3889  break;
3890 
3891  /* Make sure we can break out of loop if stuck... */
3893  }
3894 
3895  if (found)
3896  {
3897  /* Now it is safe to use the non-volatile pointer */
3898  if (checkUser && !superuser() && beentry->st_userid != GetUserId())
3899  return "<insufficient privilege>";
3900  else if (*(beentry->st_activity) == '\0')
3901  return "<command string not enabled>";
3902  else
3903  return beentry->st_activity;
3904  }
3905 
3906  beentry++;
3907  }
3908 
3909  /* If we get here, caller is in error ... */
3910  return "<backend information not available>";
3911 }
static PgBackendStatus * BackendStatusArray
Definition: pgstat.c:2553
Oid GetUserId(void)
Definition: miscinit.c:283
bool superuser(void)
Definition: superuser.c:47
int MaxBackends
Definition: globals.c:126
#define pgstat_save_changecount_before(beentry, save_changecount)
Definition: pgstat.h:1044
char * st_activity
Definition: pgstat.h:1003
#define pgstat_save_changecount_after(beentry, save_changecount)
Definition: pgstat.h:1050
int st_procpid
Definition: pgstat.h:975
int i
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
const char* pgstat_get_backend_desc ( BackendType  backendType)

Definition at line 3988 of file pgstat.c.

References B_AUTOVAC_LAUNCHER, B_AUTOVAC_WORKER, B_BACKEND, B_BG_WORKER, B_BG_WRITER, B_CHECKPOINTER, B_STARTUP, B_WAL_RECEIVER, B_WAL_SENDER, and B_WAL_WRITER.

Referenced by pg_stat_get_activity().

3989 {
3990  const char *backendDesc = "unknown process type";
3991 
3992  switch (backendType)
3993  {
3994  case B_AUTOVAC_LAUNCHER:
3995  backendDesc = "autovacuum launcher";
3996  break;
3997  case B_AUTOVAC_WORKER:
3998  backendDesc = "autovacuum worker";
3999  break;
4000  case B_BACKEND:
4001  backendDesc = "client backend";
4002  break;
4003  case B_BG_WORKER:
4004  backendDesc = "background worker";
4005  break;
4006  case B_BG_WRITER:
4007  backendDesc = "background writer";
4008  break;
4009  case B_CHECKPOINTER:
4010  backendDesc = "checkpointer";
4011  break;
4012  case B_STARTUP:
4013  backendDesc = "startup";
4014  break;
4015  case B_WAL_RECEIVER:
4016  backendDesc = "walreceiver";
4017  break;
4018  case B_WAL_SENDER:
4019  backendDesc = "walsender";
4020  break;
4021  case B_WAL_WRITER:
4022  backendDesc = "walwriter";
4023  break;
4024  }
4025 
4026  return backendDesc;
4027 }
const char* pgstat_get_crashed_backend_activity ( int  pid,
char *  buffer,
int  buflen 
)

Definition at line 3930 of file pgstat.c.

References ascii_safe_strlcpy(), BackendStatusArray, buffer, i, MaxBackends, Min, NULL, pgstat_track_activity_query_size, PgBackendStatus::st_activity, and PgBackendStatus::st_procpid.

Referenced by LogChildExit().

3931 {
3932  volatile PgBackendStatus *beentry;
3933  int i;
3934 
3935  beentry = BackendStatusArray;
3936 
3937  /*
3938  * We probably shouldn't get here before shared memory has been set up,
3939  * but be safe.
3940  */
3941  if (beentry == NULL || BackendActivityBuffer == NULL)
3942  return NULL;
3943 
3944  for (i = 1; i <= MaxBackends; i++)
3945  {
3946  if (beentry->st_procpid == pid)
3947  {
3948  /* Read pointer just once, so it can't change after validation */
3949  const char *activity = beentry->st_activity;
3950  const char *activity_last;
3951 
3952  /*
3953  * We mustn't access activity string before we verify that it
3954  * falls within the BackendActivityBuffer. To make sure that the
3955  * entire string including its ending is contained within the
3956  * buffer, subtract one activity length from the buffer size.
3957  */
3960 
3961  if (activity < BackendActivityBuffer ||
3962  activity > activity_last)
3963  return NULL;
3964 
3965  /* If no string available, no point in a report */
3966  if (activity[0] == '\0')
3967  return NULL;
3968 
3969  /*
3970  * Copy only ASCII-safe characters so we don't run into encoding
3971  * problems when reporting the message; and be sure not to run off
3972  * the end of memory.
3973  */
3974  ascii_safe_strlcpy(buffer, activity,
3975  Min(buflen, pgstat_track_activity_query_size));
3976 
3977  return buffer;
3978  }
3979 
3980  beentry++;
3981  }
3982 
3983  /* PID not found */
3984  return NULL;
3985 }
static PgBackendStatus * BackendStatusArray
Definition: pgstat.c:2553
static char * BackendActivityBuffer
Definition: pgstat.c:2557
#define Min(x, y)
Definition: c.h:806
static Size BackendActivityBufferSize
Definition: pgstat.c:2558
void ascii_safe_strlcpy(char *dest, const char *src, size_t destsiz)
Definition: ascii.c:173
int MaxBackends
Definition: globals.c:126
int pgstat_track_activity_query_size
Definition: pgstat.c:125
#define NULL
Definition: c.h:229
char * st_activity
Definition: pgstat.h:1003
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:207
int st_procpid
Definition: pgstat.h:975
int i
const char* pgstat_get_wait_event ( uint32  wait_event_info)

Definition at line 3350 of file pgstat.c.

References GetLockNameFromTagType(), GetLWLockIdentifier(), NULL, PG_WAIT_ACTIVITY, PG_WAIT_BUFFER_PIN, PG_WAIT_CLIENT, PG_WAIT_EXTENSION, PG_WAIT_IO, PG_WAIT_IPC, PG_WAIT_LOCK, PG_WAIT_LWLOCK, PG_WAIT_TIMEOUT, pgstat_get_wait_activity(), pgstat_get_wait_client(), pgstat_get_wait_io(), pgstat_get_wait_ipc(), and pgstat_get_wait_timeout().

Referenced by pg_stat_get_activity(), and pg_stat_get_backend_wait_event().

3351 {
3352  uint32 classId;
3353  uint16 eventId;
3354  const char *event_name;
3355 
3356  /* report process as not waiting. */
3357  if (wait_event_info == 0)
3358  return NULL;
3359 
3360  classId = wait_event_info & 0xFF000000;
3361  eventId = wait_event_info & 0x0000FFFF;
3362 
3363  switch (classId)
3364  {
3365  case PG_WAIT_LWLOCK:
3366  event_name = GetLWLockIdentifier(classId, eventId);
3367  break;
3368  case PG_WAIT_LOCK:
3369  event_name = GetLockNameFromTagType(eventId);
3370  break;
3371  case PG_WAIT_BUFFER_PIN:
3372  event_name = "BufferPin";
3373  break;
3374  case PG_WAIT_ACTIVITY:
3375  {
3376  WaitEventActivity w = (WaitEventActivity) wait_event_info;
3377 
3378  event_name = pgstat_get_wait_activity(w);
3379  break;
3380  }
3381  case PG_WAIT_CLIENT:
3382  {
3383  WaitEventClient w = (WaitEventClient) wait_event_info;
3384 
3385  event_name = pgstat_get_wait_client(w);
3386  break;
3387  }
3388  case PG_WAIT_EXTENSION:
3389  event_name = "Extension";
3390  break;
3391  case PG_WAIT_IPC:
3392  {
3393  WaitEventIPC w = (WaitEventIPC) wait_event_info;
3394 
3395  event_name = pgstat_get_wait_ipc(w);
3396  break;
3397  }
3398  case PG_WAIT_TIMEOUT:
3399  {
3400  WaitEventTimeout w = (WaitEventTimeout) wait_event_info;
3401 
3402  event_name = pgstat_get_wait_timeout(w);
3403  break;
3404  }
3405  case PG_WAIT_IO:
3406  {
3407  WaitEventIO w = (WaitEventIO) wait_event_info;
3408 
3409  event_name = pgstat_get_wait_io(w);
3410  break;
3411  }
3412  default:
3413  event_name = "unknown wait event";
3414  break;
3415  }
3416 
3417  return event_name;
3418 }
#define PG_WAIT_LOCK
Definition: pgstat.h:738
WaitEventIPC
Definition: pgstat.h:799
static const char * pgstat_get_wait_timeout(WaitEventTimeout w)
Definition: pgstat.c:3592
static const char * pgstat_get_wait_ipc(WaitEventIPC w)
Definition: pgstat.c:3528
#define PG_WAIT_ACTIVITY
Definition: pgstat.h:740
#define PG_WAIT_IO
Definition: pgstat.h:745
static const char * pgstat_get_wait_activity(WaitEventActivity w)
Definition: pgstat.c:3427
unsigned short uint16
Definition: c.h:267
#define PG_WAIT_CLIENT
Definition: pgstat.h:741
WaitEventClient
Definition: pgstat.h:781
static const char * pgstat_get_wait_client(WaitEventClient w)
Definition: pgstat.c:3488
unsigned int uint32
Definition: c.h:268
WaitEventTimeout
Definition: pgstat.h:824
#define PG_WAIT_EXTENSION
Definition: pgstat.h:742
#define PG_WAIT_BUFFER_PIN
Definition: pgstat.h:739
#define NULL
Definition: c.h:229
const char * GetLockNameFromTagType(uint16 locktag_type)
Definition: lmgr.c:1039
WaitEventIO
Definition: pgstat.h:837
#define PG_WAIT_TIMEOUT
Definition: pgstat.h:744
#define PG_WAIT_IPC
Definition: pgstat.h:743
const char * GetLWLockIdentifier(uint32 classId, uint16 eventId)
Definition: lwlock.c:703
static const char * pgstat_get_wait_io(WaitEventIO w)
Definition: pgstat.c:3620
#define PG_WAIT_LWLOCK
Definition: pgstat.h:737
WaitEventActivity
Definition: pgstat.h:755
const char* pgstat_get_wait_event_type ( uint32  wait_event_info)

Definition at line 3295 of file pgstat.c.

References NULL, PG_WAIT_ACTIVITY, PG_WAIT_BUFFER_PIN, PG_WAIT_CLIENT, PG_WAIT_EXTENSION, PG_WAIT_IO, PG_WAIT_IPC, PG_WAIT_LOCK, PG_WAIT_LWLOCK, and PG_WAIT_TIMEOUT.

Referenced by pg_stat_get_activity(), and pg_stat_get_backend_wait_event_type().

3296 {
3297  uint32 classId;
3298  const char *event_type;
3299 
3300  /* report process as not waiting. */
3301  if (wait_event_info == 0)
3302  return NULL;
3303 
3304  classId = wait_event_info & 0xFF000000;
3305 
3306  switch (classId)
3307  {
3308  case PG_WAIT_LWLOCK:
3309  event_type = "LWLock";
3310  break;
3311  case PG_WAIT_LOCK:
3312  event_type = "Lock";
3313  break;
3314  case PG_WAIT_BUFFER_PIN:
3315  event_type = "BufferPin";
3316  break;
3317  case PG_WAIT_ACTIVITY:
3318  event_type = "Activity";
3319  break;
3320  case PG_WAIT_CLIENT:
3321  event_type = "Client";
3322  break;
3323  case PG_WAIT_EXTENSION:
3324  event_type = "Extension";
3325  break;
3326  case PG_WAIT_IPC:
3327  event_type = "IPC";
3328  break;
3329  case PG_WAIT_TIMEOUT:
3330  event_type = "Timeout";
3331  break;
3332  case PG_WAIT_IO:
3333  event_type = "IO";
3334  break;
3335  default:
3336  event_type = "???";
3337  break;
3338  }
3339 
3340  return event_type;
3341 }
#define PG_WAIT_LOCK
Definition: pgstat.h:738
#define PG_WAIT_ACTIVITY
Definition: pgstat.h:740
#define PG_WAIT_IO
Definition: pgstat.h:745
#define PG_WAIT_CLIENT
Definition: pgstat.h:741
unsigned int uint32
Definition: c.h:268
#define PG_WAIT_EXTENSION
Definition: pgstat.h:742
#define PG_WAIT_BUFFER_PIN
Definition: pgstat.h:739
#define NULL
Definition: c.h:229
#define PG_WAIT_TIMEOUT
Definition: pgstat.h:744
#define PG_WAIT_IPC
Definition: pgstat.h:743
#define PG_WAIT_LWLOCK
Definition: pgstat.h:737
void pgstat_init ( void  )

Definition at line 352 of file pgstat.c.

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

Referenced by PostmasterMain().

353 {
354  ACCEPT_TYPE_ARG3 alen;
355  struct addrinfo *addrs = NULL,
356  *addr,
357  hints;
358  int ret;
359  fd_set rset;
360  struct timeval tv;
361  char test_byte;
362  int sel_res;
363  int tries = 0;
364 
365 #define TESTBYTEVAL ((char) 199)
366 
367  /*
368  * This static assertion verifies that we didn't mess up the calculations
369  * involved in selecting maximum payload sizes for our UDP messages.
370  * Because the only consequence of overrunning PGSTAT_MAX_MSG_SIZE would
371  * be silent performance loss from fragmentation, it seems worth having a
372  * compile-time cross-check that we didn't.
373  */
375  "maximum stats message size exceeds PGSTAT_MAX_MSG_SIZE");
376 
377  /*
378  * Create the UDP socket for sending and receiving statistic messages
379  */
380  hints.ai_flags = AI_PASSIVE;
381  hints.ai_family = AF_UNSPEC;
382  hints.ai_socktype = SOCK_DGRAM;
383  hints.ai_protocol = 0;
384  hints.ai_addrlen = 0;
385  hints.ai_addr = NULL;
386  hints.ai_canonname = NULL;
387  hints.ai_next = NULL;
388  ret = pg_getaddrinfo_all("localhost", NULL, &hints, &addrs);
389  if (ret || !addrs)
390  {
391  ereport(LOG,
392  (errmsg("could not resolve \"localhost\": %s",
393  gai_strerror(ret))));
394  goto startup_failed;
395  }
396 
397  /*
398  * On some platforms, pg_getaddrinfo_all() may return multiple addresses
399  * only one of which will actually work (eg, both IPv6 and IPv4 addresses
400  * when kernel will reject IPv6). Worse, the failure may occur at the
401  * bind() or perhaps even connect() stage. So we must loop through the
402  * results till we find a working combination. We will generate LOG
403  * messages, but no error, for bogus combinations.
404  */
405  for (addr = addrs; addr; addr = addr->ai_next)
406  {
407 #ifdef HAVE_UNIX_SOCKETS
408  /* Ignore AF_UNIX sockets, if any are returned. */
409  if (addr->ai_family == AF_UNIX)
410  continue;
411 #endif
412 
413  if (++tries > 1)
414  ereport(LOG,
415  (errmsg("trying another address for the statistics collector")));
416 
417  /*
418  * Create the socket.
419  */
420  if ((pgStatSock = socket(addr->ai_family, SOCK_DGRAM, 0)) == PGINVALID_SOCKET)
421  {
422  ereport(LOG,
424  errmsg("could not create socket for statistics collector: %m")));
425  continue;
426  }
427 
428  /*
429  * Bind it to a kernel assigned port on localhost and get the assigned
430  * port via getsockname().
431  */
432  if (bind(pgStatSock, addr->ai_addr, addr->ai_addrlen) < 0)
433  {
434  ereport(LOG,
436  errmsg("could not bind socket for statistics collector: %m")));
439  continue;
440  }
441 
442  alen = sizeof(pgStatAddr);
443  if (getsockname(pgStatSock, (struct sockaddr *) & pgStatAddr, &alen) < 0)
444  {
445  ereport(LOG,
447  errmsg("could not get address of socket for statistics collector: %m")));
450  continue;
451  }
452 
453  /*
454  * Connect the socket to its own address. This saves a few cycles by
455  * not having to respecify the target address on every send. This also
456  * provides a kernel-level check that only packets from this same
457  * address will be received.
458  */
459  if (connect(pgStatSock, (struct sockaddr *) & pgStatAddr, alen) < 0)
460  {
461  ereport(LOG,
463  errmsg("could not connect socket for statistics collector: %m")));
466  continue;
467  }
468 
469  /*
470  * Try to send and receive a one-byte test message on the socket. This
471  * is to catch situations where the socket can be created but will not
472  * actually pass data (for instance, because kernel packet filtering
473  * rules prevent it).
474  */
475  test_byte = TESTBYTEVAL;
476 
477 retry1:
478  if (send(pgStatSock, &test_byte, 1, 0) != 1)
479  {
480  if (errno == EINTR)
481  goto retry1; /* if interrupted, just retry */
482  ereport(LOG,
484  errmsg("could not send test message on socket for statistics collector: %m")));
487  continue;
488  }
489 
490  /*
491  * There could possibly be a little delay before the message can be
492  * received. We arbitrarily allow up to half a second before deciding
493  * it's broken.
494  */
495  for (;;) /* need a loop to handle EINTR */
496  {
497  FD_ZERO(&rset);
498  FD_SET(pgStatSock, &rset);
499 
500  tv.tv_sec = 0;
501  tv.tv_usec = 500000;
502  sel_res = select(pgStatSock + 1, &rset, NULL, NULL, &tv);
503  if (sel_res >= 0 || errno != EINTR)
504  break;
505  }
506  if (sel_res < 0)
507  {
508  ereport(LOG,
510  errmsg("select() failed in statistics collector: %m")));
513  continue;
514  }
515  if (sel_res == 0 || !FD_ISSET(pgStatSock, &rset))
516  {
517  /*
518  * This is the case we actually think is likely, so take pains to
519  * give a specific message for it.
520  *
521  * errno will not be set meaningfully here, so don't use it.
522  */
523  ereport(LOG,
524  (errcode(ERRCODE_CONNECTION_FAILURE),
525  errmsg("test message did not get through on socket for statistics collector")));
528  continue;
529  }
530 
531  test_byte++; /* just make sure variable is changed */
532 
533 retry2:
534  if (recv(pgStatSock, &test_byte, 1, 0) != 1)
535  {
536  if (errno == EINTR)
537  goto retry2; /* if interrupted, just retry */
538  ereport(LOG,
540  errmsg("could not receive test message on socket for statistics collector: %m")));
543  continue;
544  }
545 
546  if (test_byte != TESTBYTEVAL) /* strictly paranoia ... */
547  {
548  ereport(LOG,
549  (errcode(ERRCODE_INTERNAL_ERROR),
550  errmsg("incorrect test message transmission on socket for statistics collector")));
553  continue;
554  }
555 
556  /* If we get here, we have a working socket */
557  break;
558  }
559 
560  /* Did we find a working address? */
561  if (!addr || pgStatSock == PGINVALID_SOCKET)
562  goto startup_failed;
563 
564  /*
565  * Set the socket to non-blocking IO. This ensures that if the collector
566  * falls behind, statistics messages will be discarded; backends won't
567  * block waiting to send messages to the collector.
568  */
570  {
571  ereport(LOG,
573  errmsg("could not set statistics collector socket to nonblocking mode: %m")));
574  goto startup_failed;
575  }
576 
577  pg_freeaddrinfo_all(hints.ai_family, addrs);
578 
579  return;
580 
581 startup_failed:
582  ereport(LOG,
583  (errmsg("disabling statistics collector for lack of working socket")));
584 
585  if (addrs)
586  pg_freeaddrinfo_all(hints.ai_family, addrs);
587 
591 
592  /*
593  * Adjust GUC variables to suppress useless activity, and for debugging
594  * purposes (seeing track_counts off is a clue that we failed here). We
595  * use PGC_S_OVERRIDE because there is no point in trying to turn it back
596  * on from postgresql.conf without a restart.
597  */
598  SetConfigOption("track_counts", "off", PGC_INTERNAL, PGC_S_OVERRIDE);
599 }
#define send(s, buf, len, flags)
Definition: win32.h:376
#define connect(s, name, namelen)
Definition: win32.h:373
void pg_freeaddrinfo_all(int hint_ai_family, struct addrinfo *ai)
Definition: ip.c:88
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:146
#define closesocket
Definition: port.h:328
#define socket(af, type, protocol)
Definition: win32.h:369
int errcode(int sqlerrcode)
Definition: elog.c:575
#define select(n, r, w, e, timeout)
Definition: win32.h:374
#define recv(s, buf, len, flags)
Definition: win32.h:375
#define LOG
Definition: elog.h:26
#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:757
#define bind(s, addr, addrlen)
Definition: win32.h:370
#define TESTBYTEVAL
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:6676
#define AI_PASSIVE
Definition: getaddrinfo.h:62
#define ereport(elevel, rest)
Definition: elog.h:122
int errcode_for_socket_access(void)
Definition: elog.c:669
#define PGSTAT_MAX_MSG_SIZE
Definition: pgstat.h:206
#define PGINVALID_SOCKET
Definition: port.h:24
#define EINTR
Definition: win32.h:285
static struct sockaddr_storage pgStatAddr
Definition: pgstat.c:148
#define NULL
Definition: c.h:229
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:797
void pgstat_init_function_usage ( FunctionCallInfoData fcinfo,
PgStat_FunctionCallUsage fcu 
)

Definition at line 1546 of file pgstat.c.

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

Referenced by call_pltcl_start_proc(), EventTriggerInvoke(), ExecCallTriggerFunc(), ExecInterpExpr(), ExecMakeFunctionResultSet(), ExecMakeTableFunctionResult(), and fmgr_security_definer().

1548 {
1549  PgStat_BackendFunctionEntry *htabent;
1550  bool found;
1551 
1552  if (pgstat_track_functions <= fcinfo->flinfo->fn_stats)
1553  {
1554  /* stats not wanted */
1555  fcu->fs = NULL;
1556  return;
1557  }
1558 
1559  if (!pgStatFunctions)
1560  {
1561  /* First time through - initialize function stat table */
1562  HASHCTL hash_ctl;
1563 
1564  memset(&hash_ctl, 0, sizeof(hash_ctl));
1565  hash_ctl.keysize = sizeof(Oid);
1566  hash_ctl.entrysize = sizeof(PgStat_BackendFunctionEntry);
1567  pgStatFunctions = hash_create("Function stat entries",
1569  &hash_ctl,
1570  HASH_ELEM | HASH_BLOBS);
1571  }
1572 
1573  /* Get the stats entry for this function, create if necessary */
1574  htabent = hash_search(pgStatFunctions, &fcinfo->flinfo->fn_oid,
1575  HASH_ENTER, &found);
1576  if (!found)
1577  MemSet(&htabent->f_counts, 0, sizeof(PgStat_FunctionCounts));
1578 
1579  fcu->fs = &htabent->f_counts;
1580 
1581  /* save stats for this function, later used to compensate for recursion */
1582  fcu->save_f_total_time = htabent->f_counts.f_total_time;
1583 
1584  /* save current backend-wide total time */
1585  fcu->save_total = total_func_time;
1586 
1587  /* get clock time as of function start */
1589 }
#define HASH_ELEM
Definition: hsearch.h:87
instr_time save_total
Definition: pgstat.h:1095
Size entrysize
Definition: hsearch.h:73
#define MemSet(start, val, len)
Definition: c.h:857
instr_time f_total_time
Definition: pgstat.h:462
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_FunctionCounts * fs
Definition: pgstat.h:1091
FmgrInfo * flinfo
Definition: fmgr.h:79
PgStat_FunctionCounts f_counts
Definition: pgstat.h:473
#define PGSTAT_FUNCTION_HASH_SIZE
Definition: pgstat.c:103
#define HASH_BLOBS
Definition: hsearch.h:88
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:301
Size keysize
Definition: hsearch.h:72
Oid fn_oid
Definition: fmgr.h:59
static instr_time total_func_time
Definition: pgstat.c:273
static HTAB * pgStatFunctions
Definition: pgstat.c:194
#define NULL
Definition: c.h:229
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:153
instr_time save_f_total_time
Definition: pgstat.h:1093
struct PgStat_BackendFunctionEntry PgStat_BackendFunctionEntry
void pgstat_initialize ( void  )

Definition at line 2709 of file pgstat.c.

References Assert, InvalidBackendId, MaxBackends, MyAuxProcType, MyBackendId, NotAnAuxProcess, on_shmem_exit(), and pgstat_beshutdown_hook().

Referenced by AuxiliaryProcessMain(), and InitPostgres().

2710 {
2711  /* Initialize MyBEEntry */
2713  {
2716  }
2717  else
2718  {
2719  /* Must be an auxiliary process */
2721 
2722  /*
2723  * Assign the MyBEEntry for an auxiliary process. Since it doesn't
2724  * have a BackendId, the slot is statically allocated based on the
2725  * auxiliary process type (MyAuxProcType). Backends use slots indexed
2726  * in the range from 1 to MaxBackends (inclusive), so we use
2727  * MaxBackends + AuxBackendType + 1 as the index of the slot for an
2728  * auxiliary process.
2729  */
2731  }
2732 
2733  /* Set up a process-exit hook to clean up */
2735 }
static PgBackendStatus * BackendStatusArray
Definition: pgstat.c:2553
BackendId MyBackendId
Definition: globals.c:72
int MaxBackends
Definition: globals.c:126
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2554
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:348
static void pgstat_beshutdown_hook(int code, Datum arg)
Definition: pgstat.c:2920
#define InvalidBackendId
Definition: backendid.h:23
AuxProcType MyAuxProcType
Definition: bootstrap.c:68
#define Assert(condition)
Definition: c.h:675
void pgstat_initstats ( Relation  rel)

Definition at line 1675 of file pgstat.c.

References get_tabstat_entry(), NULL, PGINVALID_SOCKET, RelationData::pgstat_info, pgstat_track_counts, pgStatSock, RelationData::rd_id, RelationData::rd_rel, RELKIND_INDEX, RELKIND_MATVIEW, RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_TOASTVALUE, and PgStat_TableStatus::t_id.

Referenced by relation_open(), and try_relation_open().

1676 {
1677  Oid rel_id = rel->rd_id;
1678  char relkind = rel->rd_rel->relkind;
1679 
1680  /* We only count stats for things that have storage */
1681  if (!(relkind == RELKIND_RELATION ||
1682  relkind == RELKIND_MATVIEW ||
1683  relkind == RELKIND_INDEX ||
1684  relkind == RELKIND_TOASTVALUE ||
1685  relkind == RELKIND_SEQUENCE))
1686  {
1687  rel->pgstat_info = NULL;
1688  return;
1689  }
1690 
1692  {
1693  /* We're not counting at all */
1694  rel->pgstat_info = NULL;
1695  return;
1696  }
1697 
1698  /*
1699  * If we already set up this relation in the current transaction, nothing
1700  * to do.
1701  */
1702  if (rel->pgstat_info != NULL &&
1703  rel->pgstat_info->t_id == rel_id)
1704  return;
1705 
1706  /* Else find or make the PgStat_TableStatus entry, and update link */
1707  rel->pgstat_info = get_tabstat_entry(rel_id, rel->rd_rel->relisshared);
1708 }
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:146
#define RELKIND_MATVIEW
Definition: pg_class.h:165
Form_pg_class rd_rel
Definition: rel.h:114
unsigned int Oid
Definition: postgres_ext.h:31
bool pgstat_track_counts
Definition: pgstat.c:123
Oid rd_id
Definition: rel.h:116
static PgStat_TableStatus * get_tabstat_entry(Oid rel_id, bool isshared)
Definition: pgstat.c:1749
#define RELKIND_TOASTVALUE
Definition: pg_class.h:163
#define PGINVALID_SOCKET
Definition: port.h:24
#define NULL
Definition: c.h:229
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:218
#define RELKIND_INDEX
Definition: pg_class.h:161
#define RELKIND_RELATION
Definition: pg_class.h:160
#define RELKIND_SEQUENCE
Definition: pg_class.h:162
void pgstat_ping ( void  )

Definition at line 1511 of file pgstat.c.

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

1512 {
1513  PgStat_MsgDummy msg;
1514 
1516  return;
1517 
1519  pgstat_send(&msg, sizeof(msg));
1520 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:4054
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:146
#define PGINVALID_SOCKET
Definition: port.h:24
PgStat_MsgHdr m_hdr
Definition: pgstat.h:216
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:4041
void pgstat_progress_end_command ( void  )

Definition at line 3104 of file pgstat.c.

References InvalidOid, MyBEEntry, pgstat_increment_changecount_after, pgstat_increment_changecount_before, pgstat_track_activities, PROGRESS_COMMAND_INVALID, PgBackendStatus::st_progress_command, and PgBackendStatus::st_progress_command_target.

Referenced by AbortSubTransaction(), AbortTransaction(), and lazy_vacuum_rel().

3105 {
3106  volatile PgBackendStatus *beentry = MyBEEntry;
3107 
3108  if (!beentry)
3109  return;
3112  return;
3113 
3118 }
Oid st_progress_command_target
Definition: pgstat.h:1015
ProgressCommandType st_progress_command
Definition: pgstat.h:1014
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2554
#define pgstat_increment_changecount_before(beentry)
Definition: pgstat.h:1031
#define pgstat_increment_changecount_after(beentry)
Definition: pgstat.h:1037
#define InvalidOid
Definition: postgres_ext.h:36
bool pgstat_track_activities
Definition: pgstat.c:122
void pgstat_progress_start_command ( ProgressCommandType  cmdtype,
Oid  relid 
)

Definition at line 3032 of file pgstat.c.

References MemSet, MyBEEntry, pgstat_increment_changecount_after, pgstat_increment_changecount_before, pgstat_track_activities, PgBackendStatus::st_progress_command, PgBackendStatus::st_progress_command_target, and PgBackendStatus::st_progress_param.

Referenced by lazy_vacuum_rel().

3033 {
3034  volatile PgBackendStatus *beentry = MyBEEntry;
3035 
3036  if (!beentry || !pgstat_track_activities)
3037  return;
3038 
3040  beentry->st_progress_command = cmdtype;
3041  beentry->st_progress_command_target = relid;
3042  MemSet(&beentry->st_progress_param, 0, sizeof(beentry->st_progress_param));
3044 }
Oid st_progress_command_target
Definition: pgstat.h:1015
#define MemSet(start, val, len)
Definition: c.h:857
ProgressCommandType st_progress_command
Definition: pgstat.h:1014
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2554
#define pgstat_increment_changecount_before(beentry)
Definition: pgstat.h:1031
#define pgstat_increment_changecount_after(beentry)
Definition: pgstat.h:1037
int64 st_progress_param[PGSTAT_NUM_PROGRESS_PARAM]
Definition: pgstat.h:1016
bool pgstat_track_activities
Definition: pgstat.c:122
void pgstat_progress_update_multi_param ( int  nparam,
const int *  index,
const int64 *  val 
)

Definition at line 3075 of file pgstat.c.

References Assert, i, MyBEEntry, pgstat_increment_changecount_after, pgstat_increment_changecount_before, PGSTAT_NUM_PROGRESS_PARAM, pgstat_track_activities, and PgBackendStatus::st_progress_param.

Referenced by lazy_scan_heap().

3077 {
3078  volatile PgBackendStatus *beentry = MyBEEntry;
3079  int i;
3080 
3081  if (!beentry || !pgstat_track_activities || nparam == 0)
3082  return;
3083 
3085 
3086  for (i = 0; i < nparam; ++i)
3087  {
3088  Assert(index[i] >= 0 && index[i] < PGSTAT_NUM_PROGRESS_PARAM);
3089 
3090  beentry->st_progress_param[index[i]] = val[i];
3091  }
3092 
3094 }
Definition: type.h:90
#define PGSTAT_NUM_PROGRESS_PARAM
Definition: pgstat.h:918
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2554
#define pgstat_increment_changecount_before(beentry)
Definition: pgstat.h:1031
#define pgstat_increment_changecount_after(beentry)
Definition: pgstat.h:1037
#define Assert(condition)
Definition: c.h:675
int64 st_progress_param[PGSTAT_NUM_PROGRESS_PARAM]
Definition: pgstat.h:1016
bool pgstat_track_activities
Definition: pgstat.c:122
int i
long val
Definition: informix.c:689
void pgstat_progress_update_param ( int  index,
int64  val 
)

Definition at line 3053 of file pgstat.c.

References Assert, MyBEEntry, pgstat_increment_changecount_after, pgstat_increment_changecount_before, PGSTAT_NUM_PROGRESS_PARAM, pgstat_track_activities, PgBackendStatus::st_progress_param, and val.

Referenced by lazy_record_dead_tuple(), lazy_scan_heap(), lazy_truncate_heap(), lazy_vacuum_page(), and lazy_vacuum_rel().

3054 {
3055  volatile PgBackendStatus *beentry = MyBEEntry;
3056 
3058 
3059  if (!beentry || !pgstat_track_activities)
3060  return;
3061 
3063  beentry->st_progress_param[index] = val;
3065 }
Definition: type.h:90
#define PGSTAT_NUM_PROGRESS_PARAM
Definition: pgstat.h:918
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2554
#define pgstat_increment_changecount_before(beentry)
Definition: pgstat.h:1031
#define pgstat_increment_changecount_after(beentry)
Definition: pgstat.h:1037
#define Assert(condition)
Definition: c.h:675
int64 st_progress_param[PGSTAT_NUM_PROGRESS_PARAM]
Definition: pgstat.h:1016
bool pgstat_track_activities
Definition: pgstat.c:122
long val
Definition: informix.c:689
void pgstat_report_activity ( BackendState  state,
const char *  cmd_str 
)

Definition at line 2958 of file pgstat.c.

References GetCurrentStatementStartTimestamp(), GetCurrentTimestamp(), MyBEEntry, MyProc, NULL, pg_mbcliplen(), pgstat_increment_changecount_after, pgstat_increment_changecount_before, pgstat_track_activities, pgstat_track_activity_query_size, PgBackendStatus::st_activity, PgBackendStatus::st_activity_start_timestamp, PgBackendStatus::st_state, PgBackendStatus::st_state_start_timestamp, PgBackendStatus::st_xact_start_timestamp, STATE_DISABLED, and PGPROC::wait_event_info.

Referenced by apply_handle_begin(), apply_handle_commit(), autovac_report_activity(), autovac_report_workitem(), exec_bind_message(), exec_execute_message(), exec_parse_message(), exec_simple_query(), initialize_worker_spi(), LogicalRepApplyLoop(), ParallelQueryMain(), PostgresMain(), and WalSndLoop().

2959 {
2960  volatile PgBackendStatus *beentry = MyBEEntry;
2961  TimestampTz start_timestamp;
2962  TimestampTz current_timestamp;
2963  int len = 0;
2964 
2965  TRACE_POSTGRESQL_STATEMENT_STATUS(cmd_str);
2966 
2967  if (!beentry)
2968  return;
2969 
2971  {
2972  if (beentry->st_state != STATE_DISABLED)
2973  {
2974  volatile PGPROC *proc = MyProc;
2975 
2976  /*
2977  * track_activities is disabled, but we last reported a
2978  * non-disabled state. As our final update, change the state and
2979  * clear fields we will not be updating anymore.
2980  */
2982  beentry->st_state = STATE_DISABLED;
2983  beentry->st_state_start_timestamp = 0;
2984  beentry->st_activity[0] = '\0';
2985  beentry->st_activity_start_timestamp = 0;
2986  /* st_xact_start_timestamp and wait_event_info are also disabled */
2987  beentry->st_xact_start_timestamp = 0;
2988  proc->wait_event_info = 0;
2990  }
2991  return;
2992  }
2993 
2994  /*
2995  * To minimize the time spent modifying the entry, fetch all the needed
2996  * data first.
2997  */
2998  start_timestamp = GetCurrentStatementStartTimestamp();
2999  if (cmd_str != NULL)
3000  {
3001  len = pg_mbcliplen(cmd_str, strlen(cmd_str),
3003  }
3004  current_timestamp = GetCurrentTimestamp();
3005 
3006  /*
3007  * Now update the status entry
3008  */
3010 
3011  beentry->st_state = state;
3012  beentry->st_state_start_timestamp = current_timestamp;
3013 
3014  if (cmd_str != NULL)
3015  {
3016  memcpy((char *) beentry->st_activity, cmd_str, len);
3017  beentry->st_activity[len] = '\0';
3018  beentry->st_activity_start_timestamp = start_timestamp;
3019  }
3020 
3022 }
uint32 wait_event_info
Definition: proc.h:172
TimestampTz st_activity_start_timestamp
Definition: pgstat.h:983
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
PGPROC * MyProc
Definition: proc.c:67
int64 TimestampTz
Definition: timestamp.h:39
BackendState st_state
Definition: pgstat.h:997
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:831
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2554
#define pgstat_increment_changecount_before(beentry)
Definition: pgstat.h:1031
int pgstat_track_activity_query_size
Definition: pgstat.c:125
TimestampTz st_state_start_timestamp
Definition: pgstat.h:984
#define pgstat_increment_changecount_after(beentry)
Definition: pgstat.h:1037
#define NULL
Definition: c.h:229
char * st_activity
Definition: pgstat.h:1003
Definition: regguts.h:298
TimestampTz st_xact_start_timestamp
Definition: pgstat.h:982
bool pgstat_track_activities
Definition: pgstat.c:122
Definition: proc.h:94
TimestampTz GetCurrentStatementStartTimestamp(void)
Definition: xact.c:717
void pgstat_report_analyze ( Relation  rel,
PgStat_Counter  livetuples,
PgStat_Counter  deadtuples,
bool  resetcounter 
)

Definition at line 1398 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, NULL, 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().

1401 {
1402  PgStat_MsgAnalyze msg;
1403 
1405  return;
1406 
1407  /*
1408  * Unlike VACUUM, ANALYZE might be running inside a transaction that has
1409  * already inserted and/or deleted rows in the target table. ANALYZE will
1410  * have counted such rows as live or dead respectively. Because we will
1411  * report our counts of such rows at transaction end, we should subtract
1412  * off these counts from what we send to the collector now, else they'll
1413  * be double-counted after commit. (This approach also ensures that the
1414  * collector ends up with the right numbers if we abort instead of
1415  * committing.)
1416  */
1417  if (rel->pgstat_info != NULL)
1418  {
1420 
1421  for (trans = rel->pgstat_info->trans; trans; trans = trans->upper)
1422  {
1423  livetuples -= trans->tuples_inserted - trans->tuples_deleted;
1424  deadtuples -= trans->tuples_updated + trans->tuples_deleted;
1425  }
1426  /* count stuff inserted by already-aborted subxacts, too */
1427  deadtuples -= rel->pgstat_info->t_counts.t_delta_dead_tuples;
1428  /* Since ANALYZE's counts are estimates, we could have underflowed */
1429  livetuples = Max(livetuples, 0);
1430  deadtuples = Max(deadtuples, 0);
1431  }
1432 
1434  msg.m_databaseid = rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId;
1435  msg.m_tableoid = RelationGetRelid(rel);
1437  msg.m_resetcounter = resetcounter;
1439  msg.m_live_tuples = livetuples;
1440  msg.m_dead_tuples = deadtuples;
1441  pgstat_send(&msg, sizeof(msg));
1442 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:4054
PgStat_Counter tuples_updated
Definition: pgstat.h:168
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:146
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
TimestampTz m_analyzetime
Definition: pgstat.h:387
PgStat_TableCounts t_counts
Definition: pgstat.h:158
Form_pg_class rd_rel
Definition: rel.h:114
bool m_resetcounter
Definition: pgstat.h:386
bool pgstat_track_counts
Definition: pgstat.c:123
PgStat_Counter tuples_inserted
Definition: pgstat.h:167
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:111
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3360
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:176
PgStat_Counter m_live_tuples
Definition: pgstat.h:388
bool m_autovacuum
Definition: pgstat.h:385
PgStat_Counter tuples_deleted
Definition: pgstat.h:169
#define PGINVALID_SOCKET
Definition: port.h:24
Oid MyDatabaseId
Definition: globals.c:76
#define InvalidOid
Definition: postgres_ext.h:36
#define Max(x, y)
Definition: c.h:800
#define NULL
Definition: c.h:229
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:4041
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:218
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
PgStat_MsgHdr m_hdr
Definition: pgstat.h:382
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:382
PgStat_Counter m_dead_tuples
Definition: pgstat.h:389
#define RelationGetRelid(relation)
Definition: rel.h:417
void pgstat_report_appname ( const char *  appname)

Definition at line 3127 of file pgstat.c.

References MyBEEntry, NAMEDATALEN, pg_mbcliplen(), pgstat_increment_changecount_after, pgstat_increment_changecount_before, and PgBackendStatus::st_appname.

Referenced by assign_application_name(), and pgstat_bestart().

3128 {
3129  volatile PgBackendStatus *beentry = MyBEEntry;
3130  int len;
3131 
3132  if (!beentry)
3133  return;
3134 
3135  /* This should be unnecessary if GUC did its job, but be safe */
3136  len = pg_mbcliplen(appname, strlen(appname), NAMEDATALEN - 1);
3137 
3138  /*
3139  * Update my status entry, following the protocol of bumping
3140  * st_changecount before and after. We use a volatile pointer here to
3141  * ensure the compiler doesn't try to get cute.
3142  */
3144 
3145  memcpy((char *) beentry->st_appname, appname, len);
3146  beentry->st_appname[len] = '\0';
3147 
3149 }
#define NAMEDATALEN
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:831
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2554
#define pgstat_increment_changecount_before(beentry)
Definition: pgstat.h:1031
#define pgstat_increment_changecount_after(beentry)
Definition: pgstat.h:1037
char * st_appname
Definition: pgstat.h:1000
void pgstat_report_autovac ( Oid  dboid)

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

1349 {
1351 
1353  return;
1354 
1356  msg.m_databaseid = dboid;
1358 
1359  pgstat_send(&msg, sizeof(msg));
1360 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:4054
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:146
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
PgStat_MsgHdr m_hdr
Definition: pgstat.h:352
#define PGINVALID_SOCKET
Definition: port.h:24
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:4041
TimestampTz m_start_time
Definition: pgstat.h:354
void pgstat_report_deadlock ( void  )

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

1472 {
1473  PgStat_MsgDeadlock msg;
1474 
1476  return;
1477 
1479  msg.m_databaseid = MyDatabaseId;
1480  pgstat_send(&msg, sizeof(msg));
1481 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:4054
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:146
bool pgstat_track_counts
Definition: pgstat.c:123
PgStat_MsgHdr m_hdr
Definition: pgstat.h:529
#define PGINVALID_SOCKET
Definition: port.h:24
Oid MyDatabaseId
Definition: globals.c:76
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:4041
void pgstat_report_recovery_conflict ( int  reason)

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

1452 {
1454 
1456  return;
1457 
1459  msg.m_databaseid = MyDatabaseId;
1460  msg.m_reason = reason;
1461  pgstat_send(&msg, sizeof(msg));
1462 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:4054
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:146
bool pgstat_track_counts
Definition: pgstat.c:123
PgStat_MsgHdr m_hdr
Definition: pgstat.h:431
#define PGINVALID_SOCKET
Definition: port.h:24
Oid MyDatabaseId
Definition: globals.c:76
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:4041
void pgstat_report_stat ( bool  force)

Definition at line 780 of file pgstat.c.

References Assert, GetCurrentTransactionStopTimestamp(), hash_destroy(), have_function_stats, i, InvalidOid, PgStat_MsgTabstat::m_databaseid, PgStat_MsgTabstat::m_entry, PgStat_MsgTabstat::m_nentries, MemSet, MyDatabaseId, now(), NULL, PGSTAT_NUM_TABENTRIES, pgstat_send_funcstats(), pgstat_send_tabstat(), PGSTAT_STAT_INTERVAL, pgStatXactCommit, pgStatXactRollback, 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, and TabStatusArray::tsa_used.

Referenced by apply_handle_commit(), pg_attribute_noreturn(), pgstat_beshutdown_hook(), and PostgresMain().

781 {
782  /* we assume this inits to all zeroes: */
783  static const PgStat_TableCounts all_zeroes;
784  static TimestampTz last_report = 0;
785 
787  PgStat_MsgTabstat regular_msg;
788  PgStat_MsgTabstat shared_msg;
789  TabStatusArray *tsa;
790  int i;
791 
792  /* Don't expend a clock check if nothing to do */
793  if ((pgStatTabList == NULL || pgStatTabList->tsa_used == 0) &&
794  pgStatXactCommit == 0 && pgStatXactRollback == 0 &&
796  return;
797 
798  /*
799  * Don't send a message unless it's been at least PGSTAT_STAT_INTERVAL
800  * msec since we last sent one, or the caller wants to force stats out.
801  */
803  if (!force &&
805  return;
806  last_report = now;
807 
808  /*
809  * Scan through the TabStatusArray struct(s) to find tables that actually
810  * have counts, and build messages to send. We have to separate shared
811  * relations from regular ones because the databaseid field in the message
812  * header has to depend on that.
813  */
814  regular_msg.m_databaseid = MyDatabaseId;
815  shared_msg.m_databaseid = InvalidOid;
816  regular_msg.m_nentries = 0;
817  shared_msg.m_nentries = 0;
818 
819  for (tsa = pgStatTabList; tsa != NULL; tsa = tsa->tsa_next)
820  {
821  for (i = 0; i < tsa->tsa_used; i++)
822  {
823  PgStat_TableStatus *entry = &tsa->tsa_entries[i];
824  PgStat_MsgTabstat *this_msg;
825  PgStat_TableEntry *this_ent;
826 
827  /* Shouldn't have any pending transaction-dependent counts */
828  Assert(entry->trans == NULL);
829 
830  /*
831  * Ignore entries that didn't accumulate any actual counts, such
832  * as indexes that were opened by the planner but not used.
833  */
834  if (memcmp(&entry->t_counts, &all_zeroes,
835  sizeof(PgStat_TableCounts)) == 0)
836  continue;
837 
838  /*
839  * OK, insert data into the appropriate message, and send if full.
840  */
841  this_msg = entry->t_shared ? &shared_msg : &regular_msg;
842  this_ent = &this_msg->m_entry[this_msg->m_nentries];
843  this_ent->t_id = entry->t_id;
844  memcpy(&this_ent->t_counts, &entry->t_counts,
845  sizeof(PgStat_TableCounts));
846  if (++this_msg->m_nentries >= PGSTAT_NUM_TABENTRIES)
847  {
848  pgstat_send_tabstat(this_msg);
849  this_msg->m_nentries = 0;
850  }
851  }
852  /* zero out TableStatus structs after use */
853  MemSet(tsa->tsa_entries, 0,
854  tsa->tsa_used * sizeof(PgStat_TableStatus));
855  tsa->tsa_used = 0;
856  }
857 
858  /*
859  * pgStatTabHash is outdated on this point so we have to clean it,
860  * hash_destroy() will remove hash memory context, allocated in
861  * make_sure_stat_tab_initialized()
862  */
865 
866  /*
867  * Send partial messages. Make sure that any pending xact commit/abort
868  * gets counted, even if there are no table stats to send.
869  */
870  if (regular_msg.m_nentries > 0 ||
872  pgstat_send_tabstat(&regular_msg);
873  if (shared_msg.m_nentries > 0)
874  pgstat_send_tabstat(&shared_msg);
875 
876  /* Now, send function statistics */
878 }
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:793
int tsa_used
Definition: pgstat.c:170
static int pgStatXactCommit
Definition: pgstat.c:218
int64 TimestampTz
Definition: timestamp.h:39
#define MemSet(start, val, len)
Definition: c.h:857
struct TabStatusArray * tsa_next
Definition: pgstat.c:169
PgStat_TableCounts t_counts
Definition: pgstat.h:158
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1649
static void pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg)
Definition: pgstat.c:884
static HTAB * pgStatTabHash
Definition: pgstat.c:188
static int pgStatXactRollback
Definition: pgstat.c:219
#define PGSTAT_STAT_INTERVAL
Definition: pgstat.c:77
PgStat_TableStatus tsa_entries[TABSTAT_QUANTUM]
Definition: pgstat.c:171
#define PGSTAT_NUM_TABENTRIES
Definition: pgstat.h:263
Oid MyDatabaseId
Definition: globals.c:76
static TabStatusArray * pgStatTabList
Definition: pgstat.c:174
#define InvalidOid
Definition: postgres_ext.h:36
static bool have_function_stats
Definition: pgstat.c:200
#define NULL
Definition: c.h:229
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition: xact.c:729
#define Assert(condition)
Definition: c.h:675
PgStat_TableCounts t_counts
Definition: pgstat.h:255
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES]
Definition: pgstat.h:276
int i
static void pgstat_send_funcstats(void)
Definition: pgstat.c:928
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1534
void pgstat_report_tempfile ( size_t  filesize)

Definition at line 1490 of file pgstat.c.

References PgStat_MsgTempFile::m_databaseid, PgStat_MsgTempFile::m_filesize, PgStat_MsgTempFile::m_hdr, MyDatabaseId, PGINVALID_SOCKET, PGSTAT_MTYPE_TEMPFILE, pgstat_send(), pgstat_setheader(), pgstat_track_counts, and pgStatSock.

Referenced by FileClose().

1491 {
1492  PgStat_MsgTempFile msg;
1493 
1495  return;
1496 
1498  msg.m_databaseid = MyDatabaseId;
1499  msg.m_filesize = filesize;
1500  pgstat_send(&msg, sizeof(msg));
1501 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:4054
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:146
PgStat_MsgHdr m_hdr
Definition: pgstat.h:443
size_t m_filesize
Definition: pgstat.h:446
bool pgstat_track_counts
Definition: pgstat.c:123
#define PGINVALID_SOCKET
Definition: port.h:24
Oid MyDatabaseId
Definition: globals.c:76
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:4041
void pgstat_report_vacuum ( Oid  tableoid,
bool  shared,
PgStat_Counter  livetuples,
PgStat_Counter  deadtuples 
)

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

1372 {
1373  PgStat_MsgVacuum msg;
1374 
1376  return;
1377 
1379  msg.m_databaseid = shared ? InvalidOid : MyDatabaseId;
1380  msg.m_tableoid = tableoid;
1383  msg.m_live_tuples = livetuples;
1384  msg.m_dead_tuples = deadtuples;
1385  pgstat_send(&msg, sizeof(msg));
1386 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:4054
PgStat_Counter m_dead_tuples
Definition: pgstat.h:371
TimestampTz m_vacuumtime
Definition: pgstat.h:369
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:146
PgStat_Counter m_live_tuples
Definition: pgstat.h:370
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
Oid m_databaseid
Definition: pgstat.h:366
bool m_autovacuum
Definition: pgstat.h:368
bool pgstat_track_counts
Definition: pgstat.c:123
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3360
#define PGINVALID_SOCKET
Definition: port.h:24
Oid MyDatabaseId
Definition: globals.c:76
#define InvalidOid
Definition: postgres_ext.h:36
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:4041
PgStat_MsgHdr m_hdr
Definition: pgstat.h:365
static void pgstat_report_wait_end ( void  )
inlinestatic

Definition at line 1232 of file pgstat.h.

References MyProc, pgstat_track_activities, and PGPROC::wait_event_info.

Referenced by AbortSubTransaction(), AbortTransaction(), AddToDataDirLockFile(), ApplyLogicalMappingFile(), assign_xlog_sync_method(), BackgroundWriterMain(), BootStrapXLOG(), CheckpointerMain(), CheckPointLogicalRewriteHeap(), copy_file(), CreateLockFile(), dsm_impl_mmap(), FilePrefetch(), FileRead(), FileSync(), FileTruncate(), FileWrite(), FileWriteback(), heap_xlog_logical_rewrite(), load_relmap_file(), LWLockReportWaitEnd(), ProcArrayGroupClearXid(), ReadControlFile(), ReadTwoPhaseFile(), RecheckDataDirLockFile(), RecreateTwoPhaseFile(), ReorderBufferRestoreChanges(), ReorderBufferSerializeChange(), RestoreSlotFromDisk(), SaveSlotToPath(), SendTimeLineHistory(), ShutdownAuxiliaryProcess(), SimpleLruFlush(), SlruPhysicalReadPage(), SlruPhysicalWritePage(), SnapBuildRestore(), SnapBuildSerialize(), UpdateControlFile(), WaitEventSetWait(), WalSndErrorCleanup(), WalWriterMain(), write_relmap_file(), WriteControlFile(), writeTimeLineHistory(), writeTimeLineHistoryFile(), XLogFileCopy(), XLogFileInit(), XLogPageRead(), XLogRead(), and XLogWrite().

1233 {
1234  volatile PGPROC *proc = MyProc;
1235 
1236  if (!pgstat_track_activities || !proc)
1237  return;
1238 
1239  /*
1240  * Since this is a four-byte field which is always read and written as
1241  * four-bytes, updates are atomic.
1242  */
1243  proc->wait_event_info = 0;
1244 }
uint32 wait_event_info
Definition: proc.h:172
PGPROC * MyProc
Definition: proc.c:67
bool pgstat_track_activities
Definition: pgstat.c:122
Definition: proc.h:94
static void pgstat_report_wait_start ( uint32  wait_event_info)
inlinestatic

Definition at line 1208 of file pgstat.h.

References MyProc, pgstat_track_activities, and PGPROC::wait_event_info.

Referenced by AddToDataDirLockFile(), ApplyLogicalMappingFile(), assign_xlog_sync_method(), BootStrapXLOG(), CheckPointLogicalRewriteHeap(), copy_file(), CreateLockFile(), dsm_impl_mmap(), FilePrefetch(), FileRead(), FileSync(), FileTruncate(), FileWrite(), FileWriteback(), heap_xlog_logical_rewrite(), load_relmap_file(), LWLockReportWaitStart(), ProcArrayGroupClearXid(), ReadControlFile(), ReadTwoPhaseFile(), RecheckDataDirLockFile(), RecreateTwoPhaseFile(), ReorderBufferRestoreChanges(), ReorderBufferSerializeChange(), RestoreSlotFromDisk(), SaveSlotToPath(), SendTimeLineHistory(), SimpleLruFlush(), SlruPhysicalReadPage(), SlruPhysicalWritePage(), SnapBuildRestore(), SnapBuildSerialize(), UpdateControlFile(), WaitEventSetWait(), write_relmap_file(), WriteControlFile(), writeTimeLineHistory(), writeTimeLineHistoryFile(), XLogFileCopy(), XLogFileInit(), XLogPageRead(), XLogRead(), and XLogWrite().

1209 {
1210  volatile PGPROC *proc = MyProc;
1211 
1212  if (!pgstat_track_activities || !proc)
1213  return;
1214 
1215  /*
1216  * Since this is a four-byte field which is always read and written as
1217  * four-bytes, updates are atomic.
1218  */
1219  proc->wait_event_info = wait_event_info;
1220 }
uint32 wait_event_info
Definition: proc.h:172
PGPROC * MyProc
Definition: proc.c:67
bool pgstat_track_activities
Definition: pgstat.c:122
Definition: proc.h:94
void pgstat_report_xact_timestamp ( TimestampTz  tstamp)

Definition at line 3156 of file pgstat.c.

References MyBEEntry, pgstat_increment_changecount_after, pgstat_increment_changecount_before, pgstat_track_activities, and PgBackendStatus::st_xact_start_timestamp.

Referenced by AbortTransaction(), CommitTransaction(), PrepareTransaction(), and StartTransaction().

3157 {
3158  volatile PgBackendStatus *beentry = MyBEEntry;
3159 
3160  if (!pgstat_track_activities || !beentry)
3161  return;
3162 
3163  /*
3164  * Update my status entry, following the protocol of bumping
3165  * st_changecount before and after. We use a volatile pointer here to
3166  * ensure the compiler doesn't try to get cute.
3167  */
3169  beentry->st_xact_start_timestamp = tstamp;
3171 }
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2554
#define pgstat_increment_changecount_before(beentry)
Definition: pgstat.h:1031
#define pgstat_increment_changecount_after(beentry)
Definition: pgstat.h:1037
TimestampTz st_xact_start_timestamp
Definition: pgstat.h:982
bool pgstat_track_activities
Definition: pgstat.c:122
void pgstat_reset_all ( void  )

Definition at line 653 of file pgstat.c.

References pgstat_reset_remove_files(), pgstat_stat_directory, and PGSTAT_STAT_PERMANENT_DIRECTORY.

Referenced by StartupXLOG().

654 {
657 }
#define PGSTAT_STAT_PERMANENT_DIRECTORY
Definition: pgstat.h:29
char * pgstat_stat_directory
Definition: pgstat.c:131
static void pgstat_reset_remove_files(const char *directory)
Definition: pgstat.c:605
void pgstat_reset_counters ( void  )

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

1272 {
1274 
1276  return;
1277 
1279  msg.m_databaseid = MyDatabaseId;
1280  pgstat_send(&msg, sizeof(msg));
1281 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:4054
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:146
PgStat_MsgHdr m_hdr
Definition: pgstat.h:317
#define PGINVALID_SOCKET
Definition: port.h:24
Oid MyDatabaseId
Definition: globals.c:76
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:4041
void pgstat_reset_shared_counters ( const char *  )

Definition at line 1293 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, and RESET_BGWRITER.

Referenced by pg_stat_reset_shared().

1294 {
1296 
1298  return;
1299 
1300  if (strcmp(target, "archiver") == 0)
1302  else if (strcmp(target, "bgwriter") == 0)
1304  else
1305  ereport(ERROR,
1306  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1307  errmsg("unrecognized reset target: \"%s\"", target),
1308  errhint("Target must be \"archiver\" or \"bgwriter\".")));
1309 
1311  pgstat_send(&msg, sizeof(msg));
1312 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:4054
int errhint(const char *fmt,...)
Definition: elog.c:987
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:146
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PGINVALID_SOCKET
Definition: port.h:24
PgStat_Shared_Reset_Target m_resettarget
Definition: pgstat.h:329
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:4041
int errmsg(const char *fmt,...)
Definition: elog.c:797
void pgstat_reset_single_counter ( Oid  objectid,
PgStat_Single_Reset_Type  type 
)

Definition at line 1324 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(), and pgStatSock.

Referenced by pg_stat_reset_single_function_counters(), and pg_stat_reset_single_table_counters().

1325 {
1327 
1329  return;
1330 
1332  msg.m_databaseid = MyDatabaseId;
1333  msg.m_resettype = type;
1334  msg.m_objectid = objoid;
1335 
1336  pgstat_send(&msg, sizeof(msg));
1337 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:4054
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:146
#define PGINVALID_SOCKET
Definition: port.h:24
Oid MyDatabaseId
Definition: globals.c:76
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:4041
PgStat_Single_Reset_Type m_resettype
Definition: pgstat.h:341
void pgstat_send_archiver ( const char *  xlog,
bool  failed 
)

Definition at line 4084 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 StrNCpy.

Referenced by pgarch_ArchiverCopyLoop().

4085 {
4086  PgStat_MsgArchiver msg;
4087 
4088  /*
4089  * Prepare and send the message
4090  */
4092  msg.m_failed = failed;
4093  StrNCpy(msg.m_xlog, xlog, sizeof(msg.m_xlog));
4095  pgstat_send(&msg, sizeof(msg));
4096 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:4054
char m_xlog[MAX_XFN_CHARS+1]
Definition: pgstat.h:401
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
TimestampTz m_timestamp
Definition: pgstat.h:402
#define StrNCpy(dst, src, len)
Definition: c.h:830
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:4041
PgStat_MsgHdr m_hdr
Definition: pgstat.h:399
void pgstat_send_bgwriter ( void  )

Definition at line 4105 of file pgstat.c.

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

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

4106 {
4107  /* We assume this initializes to zeroes */
4108  static const PgStat_MsgBgWriter all_zeroes;
4109 
4110  /*
4111  * This function can be called even if nothing at all has happened. In
4112  * this case, avoid sending a completely empty message to the stats
4113  * collector.
4114  */
4115  if (memcmp(&BgWriterStats, &all_zeroes, sizeof(PgStat_MsgBgWriter)) == 0)
4116  return;
4117 
4118  /*
4119  * Prepare and send the message
4120  */
4123 
4124  /*
4125  * Clear out the statistics buffer, so it can be re-used.
4126  */
4127  MemSet(&BgWriterStats, 0, sizeof(BgWriterStats));
4128 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:4054
PgStat_MsgHdr m_hdr
Definition: pgstat.h:411
PgStat_MsgBgWriter BgWriterStats
Definition: pgstat.c:140
#define MemSet(start, val, len)
Definition: c.h:857
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:4041
int pgstat_start ( void  )

Definition at line 695 of file pgstat.c.

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

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

696 {
697  time_t curtime;
698  pid_t pgStatPid;
699 
700  /*
701  * Check that the socket is there, else pgstat_init failed and we can do
702  * nothing useful.
703  */
705  return 0;
706 
707  /*
708  * Do nothing if too soon since last collector start. This is a safety
709  * valve to protect against continuous respawn attempts if the collector
710  * is dying immediate