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 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 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  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_READ, 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_SAFE_SNAPSHOT, WAIT_EVENT_SYNC_REP
}
 
enum  WaitEventTimeout { WAIT_EVENT_BASE_BACKUP_THROTTLE = PG_WAIT_TIMEOUT, WAIT_EVENT_PG_SLEEP, WAIT_EVENT_RECOVERY_APPLY_DELAY }
 
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)
 
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, int 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 721 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 722 of file pgstat.h.

Referenced by pgstat_get_wait_event(), and pgstat_get_wait_event_type().

#define PG_WAIT_EXTENSION   0x07000000U
#define PG_WAIT_IPC   0x08000000U

Definition at line 724 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 725 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:226

Definition at line 1170 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:226

Definition at line 1165 of file pgstat.h.

Referenced by ReadBufferExtended().

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

Definition at line 1175 of file pgstat.h.

Referenced by ReadBuffer_common().

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

Definition at line 1177 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:226

Definition at line 1150 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:226

Definition at line 1145 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:226

Definition at line 1140 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:226

Definition at line 1155 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:226

Definition at line 1160 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:671
#define pg_write_barrier()
Definition: atomics.h:161

Definition at line 930 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:160

Definition at line 943 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:160

Definition at line 937 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 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 811 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 735 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_READ 
WAIT_EVENT_WAL_SENDER_WAIT_WAL 
WAIT_EVENT_WAL_SENDER_WRITE_DATA 

Definition at line 761 of file pgstat.h.

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_SAFE_SNAPSHOT 
WAIT_EVENT_SYNC_REP 

Definition at line 779 of file pgstat.h.

Enumerator
WAIT_EVENT_BASE_BACKUP_THROTTLE 
WAIT_EVENT_PG_SLEEP 
WAIT_EVENT_RECOVERY_APPLY_DELAY 

Definition at line 800 of file pgstat.h.

Function Documentation

void allow_immediate_pgstat_restart ( void  )

Definition at line 726 of file pgstat.c.

References last_pgstat_start_time.

Referenced by HandleChildCrash().

727 {
729 }
static time_t last_pgstat_start_time
Definition: pgstat.c:137
void AtEOSubXact_PgStat ( bool  isCommit,
int  nestDepth 
)

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

2035 {
2036  PgStat_SubXactStatus *xact_state;
2037 
2038  /*
2039  * Transfer transactional insert/update counts into the next higher
2040  * subtransaction state.
2041  */
2042  xact_state = pgStatXactStack;
2043  if (xact_state != NULL &&
2044  xact_state->nest_level >= nestDepth)
2045  {
2047  PgStat_TableXactStatus *next_trans;
2048 
2049  /* delink xact_state from stack immediately to simplify reuse case */
2050  pgStatXactStack = xact_state->prev;
2051 
2052  for (trans = xact_state->first; trans != NULL; trans = next_trans)
2053  {
2054  PgStat_TableStatus *tabstat;
2055 
2056  next_trans = trans->next;
2057  Assert(trans->nest_level == nestDepth);
2058  tabstat = trans->parent;
2059  Assert(tabstat->trans == trans);
2060  if (isCommit)
2061  {
2062  if (trans->upper && trans->upper->nest_level == nestDepth - 1)
2063  {
2064  if (trans->truncated)
2065  {
2066  /* propagate the truncate status one level up */
2068  /* replace upper xact stats with ours */
2069  trans->upper->tuples_inserted = trans->tuples_inserted;
2070  trans->upper->tuples_updated = trans->tuples_updated;
2071  trans->upper->tuples_deleted = trans->tuples_deleted;
2072  }
2073  else
2074  {
2075  trans->upper->tuples_inserted += trans->tuples_inserted;
2076  trans->upper->tuples_updated += trans->tuples_updated;
2077  trans->upper->tuples_deleted += trans->tuples_deleted;
2078  }
2079  tabstat->trans = trans->upper;
2080  pfree(trans);
2081  }
2082  else
2083  {
2084  /*
2085  * When there isn't an immediate parent state, we can just
2086  * reuse the record instead of going through a
2087  * palloc/pfree pushup (this works since it's all in
2088  * TopTransactionContext anyway). We have to re-link it
2089  * into the parent level, though, and that might mean
2090  * pushing a new entry into the pgStatXactStack.
2091  */
2092  PgStat_SubXactStatus *upper_xact_state;
2093 
2094  upper_xact_state = get_tabstat_stack_level(nestDepth - 1);
2095  trans->next = upper_xact_state->first;
2096  upper_xact_state->first = trans;
2097  trans->nest_level = nestDepth - 1;
2098  }
2099  }
2100  else
2101  {
2102  /*
2103  * On abort, update top-level tabstat counts, then forget the
2104  * subtransaction
2105  */
2106 
2107  /* first restore values obliterated by truncate */
2109  /* count attempted actions regardless of commit/abort */
2110  tabstat->t_counts.t_tuples_inserted += trans->tuples_inserted;
2111  tabstat->t_counts.t_tuples_updated += trans->tuples_updated;
2112  tabstat->t_counts.t_tuples_deleted += trans->tuples_deleted;
2113  /* inserted tuples are dead, deleted tuples are unaffected */
2114  tabstat->t_counts.t_delta_dead_tuples +=
2115  trans->tuples_inserted + trans->tuples_updated;
2116  tabstat->trans = trans->upper;
2117  pfree(trans);
2118  }
2119  }
2120  pfree(xact_state);
2121  }
2122 }
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:1756
void pfree(void *pointer)
Definition: mcxt.c:992
PgStat_Counter t_tuples_updated
Definition: pgstat.h:105
static void pgstat_truncate_restore_counters(PgStat_TableXactStatus *trans)
Definition: pgstat.c:1892
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:185
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:189
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:383
PgStat_TableXactStatus * first
Definition: pgstat.c:186
static void pgstat_truncate_save_counters(PgStat_TableXactStatus *trans)
Definition: pgstat.c:1877
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:104
PgStat_TableStatus * parent
Definition: pgstat.h:177
void AtEOXact_PgStat ( bool  isCommit)

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

1952 {
1953  PgStat_SubXactStatus *xact_state;
1954 
1955  /*
1956  * Count transaction commit or abort. (We use counters, not just bools,
1957  * in case the reporting message isn't sent right away.)
1958  */
1959  if (isCommit)
1960  pgStatXactCommit++;
1961  else
1963 
1964  /*
1965  * Transfer transactional insert/update counts into the base tabstat
1966  * entries. We don't bother to free any of the transactional state, since
1967  * it's all in TopTransactionContext and will go away anyway.
1968  */
1969  xact_state = pgStatXactStack;
1970  if (xact_state != NULL)
1971  {
1973 
1974  Assert(xact_state->nest_level == 1);
1975  Assert(xact_state->prev == NULL);
1976  for (trans = xact_state->first; trans != NULL; trans = trans->next)
1977  {
1978  PgStat_TableStatus *tabstat;
1979 
1980  Assert(trans->nest_level == 1);
1981  Assert(trans->upper == NULL);
1982  tabstat = trans->parent;
1983  Assert(tabstat->trans == trans);
1984  /* restore pre-truncate stats (if any) in case of aborted xact */
1985  if (!isCommit)
1987  /* count attempted actions regardless of commit/abort */
1988  tabstat->t_counts.t_tuples_inserted += trans->tuples_inserted;
1989  tabstat->t_counts.t_tuples_updated += trans->tuples_updated;
1990  tabstat->t_counts.t_tuples_deleted += trans->tuples_deleted;
1991  if (isCommit)
1992  {
1993  tabstat->t_counts.t_truncated = trans->truncated;
1994  if (trans->truncated)
1995  {
1996  /* forget live/dead stats seen by backend thus far */
1997  tabstat->t_counts.t_delta_live_tuples = 0;
1998  tabstat->t_counts.t_delta_dead_tuples = 0;
1999  }
2000  /* insert adds a live tuple, delete removes one */
2001  tabstat->t_counts.t_delta_live_tuples +=
2002  trans->tuples_inserted - trans->tuples_deleted;
2003  /* update and delete each create a dead tuple */
2004  tabstat->t_counts.t_delta_dead_tuples +=
2005  trans->tuples_updated + trans->tuples_deleted;
2006  /* insert, update, delete each count as one change event */
2007  tabstat->t_counts.t_changed_tuples +=
2008  trans->tuples_inserted + trans->tuples_updated +
2009  trans->tuples_deleted;
2010  }
2011  else
2012  {
2013  /* inserted tuples are dead, deleted tuples are unaffected */
2014  tabstat->t_counts.t_delta_dead_tuples +=
2015  trans->tuples_inserted + trans->tuples_updated;
2016  /* an aborted xact generates no changed_tuple events */
2017  }
2018  tabstat->trans = NULL;
2019  }
2020  }
2022 
2023  /* Make sure any stats snapshot is thrown away */
2025 }
PgStat_Counter t_delta_live_tuples
Definition: pgstat.h:110
PgStat_Counter tuples_updated
Definition: pgstat.h:168
static int pgStatXactCommit
Definition: pgstat.c:191
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:1892
static int pgStatXactRollback
Definition: pgstat.c:192
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:185
PgStat_Counter t_changed_tuples
Definition: pgstat.h:112
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
void pgstat_clear_snapshot(void)
Definition: pgstat.c:5045
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:189
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:383
PgStat_TableXactStatus * first
Definition: pgstat.c:186
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:104
PgStat_TableStatus * parent
Definition: pgstat.h:177
void AtPrepare_PgStat ( void  )

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

2134 {
2135  PgStat_SubXactStatus *xact_state;
2136 
2137  xact_state = pgStatXactStack;
2138  if (xact_state != NULL)
2139  {
2141 
2142  Assert(xact_state->nest_level == 1);
2143  Assert(xact_state->prev == NULL);
2144  for (trans = xact_state->first; trans != NULL; trans = trans->next)
2145  {
2146  PgStat_TableStatus *tabstat;
2147  TwoPhasePgStatRecord record;
2148 
2149  Assert(trans->nest_level == 1);
2150  Assert(trans->upper == NULL);
2151  tabstat = trans->parent;
2152  Assert(tabstat->trans == trans);
2153 
2154  record.tuples_inserted = trans->tuples_inserted;
2155  record.tuples_updated = trans->tuples_updated;
2156  record.tuples_deleted = trans->tuples_deleted;
2157  record.inserted_pre_trunc = trans->inserted_pre_trunc;
2158  record.updated_pre_trunc = trans->updated_pre_trunc;
2159  record.deleted_pre_trunc = trans->deleted_pre_trunc;
2160  record.t_id = tabstat->t_id;
2161  record.t_shared = tabstat->t_shared;
2162  record.t_truncated = trans->truncated;
2163 
2165  &record, sizeof(TwoPhasePgStatRecord));
2166  }
2167  }
2168 }
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:204
void RegisterTwoPhaseRecord(TwoPhaseRmgrId rmid, uint16 info, const void *data, uint32 len)
Definition: twophase.c:1121
PgStat_Counter tuples_inserted
Definition: pgstat.h:167
PgStat_Counter updated_pre_trunc
Definition: pgstat.c:203
PgStat_Counter inserted_pre_trunc
Definition: pgstat.c:202
struct PgStat_TableXactStatus * upper
Definition: pgstat.h:176
PgStat_Counter tuples_updated
Definition: pgstat.c:200
PgStat_Counter tuples_deleted
Definition: pgstat.h:169
struct PgStat_SubXactStatus * prev
Definition: pgstat.c:185
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
PgStat_Counter inserted_pre_trunc
Definition: pgstat.h:171
PgStat_Counter tuples_inserted
Definition: pgstat.c:199
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:189
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
PgStat_Counter tuples_deleted
Definition: pgstat.c:201
#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:383
PgStat_TableXactStatus * first
Definition: pgstat.c:186
PgStat_TableStatus * parent
Definition: pgstat.h:177
Size BackendStatusShmemSize ( void  )

Definition at line 2502 of file pgstat.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

2503 {
2504  Size size;
2505 
2506  /* BackendStatusArray: */
2507  size = mul_size(sizeof(PgBackendStatus), MaxBackends);
2508  /* BackendAppnameBuffer: */
2509  size = add_size(size,
2511  /* BackendClientHostnameBuffer: */
2512  size = add_size(size,
2514  /* BackendActivityBuffer: */
2515  size = add_size(size,
2517 #ifdef USE_SSL
2518  /* BackendSslStatusBuffer: */
2519  size = add_size(size,
2521 #endif
2522  return size;
2523 }
#define NAMEDATALEN
int MaxBackends
Definition: globals.c:126
int pgstat_track_activity_query_size
Definition: pgstat.c:112
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:353
void CreateSharedBackendStatus ( void  )

Definition at line 2530 of file pgstat.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

2531 {
2532  Size size;
2533  bool found;
2534  int i;
2535  char *buffer;
2536 
2537  /* Create or attach to the shared array */
2538  size = mul_size(sizeof(PgBackendStatus), MaxBackends);
2540  ShmemInitStruct("Backend Status Array", size, &found);
2541 
2542  if (!found)
2543  {
2544  /*
2545  * We're the first - initialize.
2546  */
2547  MemSet(BackendStatusArray, 0, size);
2548  }
2549 
2550  /* Create or attach to the shared appname buffer */
2551  size = mul_size(NAMEDATALEN, MaxBackends);
2552  BackendAppnameBuffer = (char *)
2553  ShmemInitStruct("Backend Application Name Buffer", size, &found);
2554 
2555  if (!found)
2556  {
2557  MemSet(BackendAppnameBuffer, 0, size);
2558 
2559  /* Initialize st_appname pointers. */
2560  buffer = BackendAppnameBuffer;
2561  for (i = 0; i < MaxBackends; i++)
2562  {
2563  BackendStatusArray[i].st_appname = buffer;
2564  buffer += NAMEDATALEN;
2565  }
2566  }
2567 
2568  /* Create or attach to the shared client hostname buffer */
2569  size = mul_size(NAMEDATALEN, MaxBackends);
2570  BackendClientHostnameBuffer = (char *)
2571  ShmemInitStruct("Backend Client Host Name Buffer", size, &found);
2572 
2573  if (!found)
2574  {
2576 
2577  /* Initialize st_clienthostname pointers. */
2578  buffer = BackendClientHostnameBuffer;
2579  for (i = 0; i < MaxBackends; i++)
2580  {
2582  buffer += NAMEDATALEN;
2583  }
2584  }
2585 
2586  /* Create or attach to the shared activity buffer */
2588  MaxBackends);
2589  BackendActivityBuffer = (char *)
2590  ShmemInitStruct("Backend Activity Buffer",
2592  &found);
2593 
2594  if (!found)
2595  {
2596  MemSet(BackendActivityBuffer, 0, size);
2597 
2598  /* Initialize st_activity pointers. */
2599  buffer = BackendActivityBuffer;
2600  for (i = 0; i < MaxBackends; i++)
2601  {
2602  BackendStatusArray[i].st_activity = buffer;
2604  }
2605  }
2606 
2607 #ifdef USE_SSL
2608  /* Create or attach to the shared SSL status buffer */
2609  size = mul_size(sizeof(PgBackendSSLStatus), MaxBackends);
2610  BackendSslStatusBuffer = (PgBackendSSLStatus *)
2611  ShmemInitStruct("Backend SSL Status Buffer", size, &found);
2612 
2613  if (!found)
2614  {
2615  PgBackendSSLStatus *ptr;
2616 
2617  MemSet(BackendSslStatusBuffer, 0, size);
2618 
2619  /* Initialize st_sslstatus pointers. */
2620  ptr = BackendSslStatusBuffer;
2621  for (i = 0; i < MaxBackends; i++)
2622  {
2624  ptr++;
2625  }
2626  }
2627 #endif
2628 }
static PgBackendStatus * BackendStatusArray
Definition: pgstat.c:2487
static char * BackendActivityBuffer
Definition: pgstat.c:2491
static Size BackendActivityBufferSize
Definition: pgstat.c:2492
#define MemSet(start, val, len)
Definition: c.h:853
char * st_clienthostname
Definition: pgstat.h:883
#define NAMEDATALEN
static char * BackendAppnameBuffer
Definition: pgstat.c:2489
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:112
char * st_appname
Definition: pgstat.h:893
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
char * st_activity
Definition: pgstat.h:896
size_t Size
Definition: c.h:353
int i
PgBackendSSLStatus * st_sslstatus
Definition: pgstat.h:887
static char * BackendClientHostnameBuffer
Definition: pgstat.c:2490
PgStat_BackendFunctionEntry* find_funcstat_entry ( Oid  func_id)

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

1558 {
1559  if (pgStatFunctions == NULL)
1560  return NULL;
1561 
1563  (void *) &func_id,
1564  HASH_FIND, NULL);
1565 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
static HTAB * pgStatFunctions
Definition: pgstat.c:167
#define NULL
Definition: c.h:226
PgStat_TableStatus* find_tabstat_entry ( Oid  rel_id)

Definition at line 1732 of file pgstat.c.

References i, NULL, PgStat_TableStatus::t_id, TabStatusArray::tsa_entries, TabStatusArray::tsa_next, and TabStatusArray::tsa_used.

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

1733 {
1734  PgStat_TableStatus *entry;
1735  TabStatusArray *tsa;
1736  int i;
1737 
1738  for (tsa = pgStatTabList; tsa != NULL; tsa = tsa->tsa_next)
1739  {
1740  for (i = 0; i < tsa->tsa_used; i++)
1741  {
1742  entry = &tsa->tsa_entries[i];
1743  if (entry->t_id == rel_id)
1744  return entry;
1745  }
1746  }
1747 
1748  /* Not present */
1749  return NULL;
1750 }
int tsa_used
Definition: pgstat.c:157
struct TabStatusArray * tsa_next
Definition: pgstat.c:156
PgStat_TableStatus tsa_entries[TABSTAT_QUANTUM]
Definition: pgstat.c:158
static TabStatusArray * pgStatTabList
Definition: pgstat.c:161
#define NULL
Definition: c.h:226
int i
void pgstat_bestart ( void  )

Definition at line 2662 of file pgstat.c.

References application_name, be_tls_get_cipher(), be_tls_get_cipher_bits(), be_tls_get_compression(), be_tls_get_peerdn_name(), be_tls_get_version(), GetCurrentTimestamp(), GetSessionUserId(), InvalidOid, MemSet, MyBEEntry, MyDatabaseId, MyProcPid, MyProcPort, NAMEDATALEN, NULL, pgstat_increment_changecount_after, pgstat_increment_changecount_before, pgstat_report_appname(), pgstat_track_activity_query_size, 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_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, STATE_UNDEFINED, and strlcpy().

Referenced by InitPostgres().

2663 {
2664  TimestampTz proc_start_timestamp;
2665  Oid userid;
2666  SockAddr clientaddr;
2667  volatile PgBackendStatus *beentry;
2668 
2669  /*
2670  * To minimize the time spent modifying the PgBackendStatus entry, fetch
2671  * all the needed data first.
2672  *
2673  * If we have a MyProcPort, use its session start time (for consistency,
2674  * and to save a kernel call).
2675  */
2676  if (MyProcPort)
2677  proc_start_timestamp = MyProcPort->SessionStartTime;
2678  else
2679  proc_start_timestamp = GetCurrentTimestamp();
2680  userid = GetSessionUserId();
2681 
2682  /*
2683  * We may not have a MyProcPort (eg, if this is the autovacuum process).
2684  * If so, use all-zeroes client address, which is dealt with specially in
2685  * pg_stat_get_backend_client_addr and pg_stat_get_backend_client_port.
2686  */
2687  if (MyProcPort)
2688  memcpy(&clientaddr, &MyProcPort->raddr, sizeof(clientaddr));
2689  else
2690  MemSet(&clientaddr, 0, sizeof(clientaddr));
2691 
2692  /*
2693  * Initialize my status entry, following the protocol of bumping
2694  * st_changecount before and after; and make sure it's even afterwards. We
2695  * use a volatile pointer here to ensure the compiler doesn't try to get
2696  * cute.
2697  */
2698  beentry = MyBEEntry;
2699  do
2700  {
2702  } while ((beentry->st_changecount & 1) == 0);
2703 
2704  beentry->st_procpid = MyProcPid;
2705  beentry->st_proc_start_timestamp = proc_start_timestamp;
2706  beentry->st_activity_start_timestamp = 0;
2707  beentry->st_state_start_timestamp = 0;
2708  beentry->st_xact_start_timestamp = 0;
2709  beentry->st_databaseid = MyDatabaseId;
2710  beentry->st_userid = userid;
2711  beentry->st_clientaddr = clientaddr;
2714  NAMEDATALEN);
2715  else
2716  beentry->st_clienthostname[0] = '\0';
2717 #ifdef USE_SSL
2718  if (MyProcPort && MyProcPort->ssl != NULL)
2719  {
2720  beentry->st_ssl = true;
2726  }
2727  else
2728  {
2729  beentry->st_ssl = false;
2730  }
2731 #else
2732  beentry->st_ssl = false;
2733 #endif
2734  beentry->st_state = STATE_UNDEFINED;
2735  beentry->st_appname[0] = '\0';
2736  beentry->st_activity[0] = '\0';
2737  /* Also make sure the last byte in each string area is always 0 */
2738  beentry->st_clienthostname[NAMEDATALEN - 1] = '\0';
2739  beentry->st_appname[NAMEDATALEN - 1] = '\0';
2740  beentry->st_activity[pgstat_track_activity_query_size - 1] = '\0';
2743 
2744  /*
2745  * we don't zero st_progress_param here to save cycles; nobody should
2746  * examine it until st_progress_command has been set to something other
2747  * than PROGRESS_COMMAND_INVALID
2748  */
2749 
2751 
2752  /* Update app name to current GUC setting */
2753  if (application_name)
2755 }
bool ssl_compression
Definition: pgstat.h:835
char ssl_cipher[NAMEDATALEN]
Definition: pgstat.h:837
Oid st_progress_command_target
Definition: pgstat.h:908
int MyProcPid
Definition: globals.c:38
struct Port * MyProcPort
Definition: globals.c:40
TimestampTz st_activity_start_timestamp
Definition: pgstat.h:876
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1687
char ssl_version[NAMEDATALEN]
Definition: pgstat.h:836
SockAddr st_clientaddr
Definition: pgstat.h:882
#define MemSet(start, val, len)
Definition: c.h:853
ProgressCommandType st_progress_command
Definition: pgstat.h:907
unsigned int Oid
Definition: postgres_ext.h:31
BackendState st_state
Definition: pgstat.h:890
char * st_clienthostname
Definition: pgstat.h:883
Oid GetSessionUserId(void)
Definition: miscinit.c:317
Oid st_databaseid
Definition: pgstat.h:880
#define NAMEDATALEN
char * remote_hostname
Definition: libpq-be.h:126
SockAddr raddr
Definition: libpq-be.h:124
double TimestampTz
Definition: timestamp.h:51
bool be_tls_get_compression(Port *port)
TimestampTz SessionStartTime
Definition: libpq-be.h:153
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2488
#define pgstat_increment_changecount_before(beentry)
Definition: pgstat.h:924
int pgstat_track_activity_query_size
Definition: pgstat.c:112
TimestampTz st_state_start_timestamp
Definition: pgstat.h:877
#define pgstat_increment_changecount_after(beentry)
Definition: pgstat.h:930
char * st_appname
Definition: pgstat.h:893
int be_tls_get_cipher_bits(Port *port)
Oid MyDatabaseId
Definition: globals.c:76
void be_tls_get_version(Port *port, char *ptr, size_t len)
void pgstat_report_appname(const char *appname)
Definition: pgstat.c:2974
#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
#define NULL
Definition: c.h:226
char * st_activity
Definition: pgstat.h:896
int st_procpid
Definition: pgstat.h:871
char * application_name
Definition: guc.c:471
void be_tls_get_cipher(Port *port, char *ptr, size_t len)
TimestampTz st_xact_start_timestamp
Definition: pgstat.h:875
int st_changecount
Definition: pgstat.h:868
TimestampTz st_proc_start_timestamp
Definition: pgstat.h:874
PgBackendSSLStatus * st_sslstatus
Definition: pgstat.h:887
char ssl_clientdn[NAMEDATALEN]
Definition: pgstat.h:838
void pgstat_clear_snapshot ( void  )

Definition at line 5045 of file pgstat.c.

References localNumBackends, MemoryContextDelete(), and NULL.

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

5046 {
5047  /* Release memory, if any was allocated */
5048  if (pgStatLocalContext)
5050 
5051  /* Reset variables */
5053  pgStatDBHash = NULL;
5055  localNumBackends = 0;
5056 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
#define NULL
Definition: c.h:226
static LocalPgBackendStatus * localBackendStatusTable
Definition: pgstat.c:215
static HTAB * pgStatDBHash
Definition: pgstat.c:214
static int localNumBackends
Definition: pgstat.c:216
static MemoryContext pgStatLocalContext
Definition: pgstat.c:213
void pgstat_count_heap_delete ( Relation  rel)

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

1852 {
1853  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1854 
1855  if (pgstat_info != NULL)
1856  {
1857  /* We have to log the effect at the proper transactional level */
1858  int nest_level = GetCurrentTransactionNestLevel();
1859 
1860  if (pgstat_info->trans == NULL ||
1861  pgstat_info->trans->nest_level != nest_level)
1862  add_tabstat_xact_level(pgstat_info, nest_level);
1863 
1864  pgstat_info->trans->tuples_deleted++;
1865  }
1866 }
PgStat_Counter tuples_deleted
Definition: pgstat.h:169
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:760
#define NULL
Definition: c.h:226
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:214
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
Definition: pgstat.c:1778
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
void pgstat_count_heap_insert ( Relation  rel,
int  n 
)

Definition at line 1805 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 heap_insert(), and heap_multi_insert().

1806 {
1807  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1808 
1809  if (pgstat_info != NULL)
1810  {
1811  /* We have to log the effect at the proper transactional level */
1812  int nest_level = GetCurrentTransactionNestLevel();
1813 
1814  if (pgstat_info->trans == NULL ||
1815  pgstat_info->trans->nest_level != nest_level)
1816  add_tabstat_xact_level(pgstat_info, nest_level);
1817 
1818  pgstat_info->trans->tuples_inserted += n;
1819  }
1820 }
PgStat_Counter tuples_inserted
Definition: pgstat.h:167
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:760
#define NULL
Definition: c.h:226
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:214
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
Definition: pgstat.c:1778
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
void pgstat_count_heap_update ( Relation  rel,
bool  hot 
)

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

1827 {
1828  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1829 
1830  if (pgstat_info != NULL)
1831  {
1832  /* We have to log the effect at the proper transactional level */
1833  int nest_level = GetCurrentTransactionNestLevel();
1834 
1835  if (pgstat_info->trans == NULL ||
1836  pgstat_info->trans->nest_level != nest_level)
1837  add_tabstat_xact_level(pgstat_info, nest_level);
1838 
1839  pgstat_info->trans->tuples_updated++;
1840 
1841  /* t_tuples_hot_updated is nontransactional, so just advance it */
1842  if (hot)
1843  pgstat_info->t_counts.t_tuples_hot_updated++;
1844  }
1845 }
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:760
#define NULL
Definition: c.h:226
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:214
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
Definition: pgstat.c:1778
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
void pgstat_count_truncate ( Relation  rel)

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

1907 {
1908  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1909 
1910  if (pgstat_info != NULL)
1911  {
1912  /* We have to log the effect at the proper transactional level */
1913  int nest_level = GetCurrentTransactionNestLevel();
1914 
1915  if (pgstat_info->trans == NULL ||
1916  pgstat_info->trans->nest_level != nest_level)
1917  add_tabstat_xact_level(pgstat_info, nest_level);
1918 
1919  pgstat_truncate_save_counters(pgstat_info->trans);
1920  pgstat_info->trans->tuples_inserted = 0;
1921  pgstat_info->trans->tuples_updated = 0;
1922  pgstat_info->trans->tuples_deleted = 0;
1923  }
1924 }
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:760
#define NULL
Definition: c.h:226
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:214
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
Definition: pgstat.c:1778
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
static void pgstat_truncate_save_counters(PgStat_TableXactStatus *trans)
Definition: pgstat.c:1877
void pgstat_drop_database ( Oid  databaseid)

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

1175 {
1176  PgStat_MsgDropdb msg;
1177 
1179  return;
1180 
1182  msg.m_databaseid = databaseid;
1183  pgstat_send(&msg, sizeof(msg));
1184 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3617
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:133
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:3604
void pgstat_end_function_usage ( PgStat_FunctionCallUsage fcu,
bool  finalize 
)

Definition at line 1577 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 EventTriggerInvoke(), ExecCallTriggerFunc(), ExecMakeFunctionResultNoSets(), ExecMakeFunctionResultSet(), ExecMakeTableFunctionResult(), and fmgr_security_definer().

1578 {
1579  PgStat_FunctionCounts *fs = fcu->fs;
1580  instr_time f_total;
1581  instr_time f_others;
1582  instr_time f_self;
1583 
1584  /* stats not wanted? */
1585  if (fs == NULL)
1586  return;
1587 
1588  /* total elapsed time in this function call */
1589  INSTR_TIME_SET_CURRENT(f_total);
1590  INSTR_TIME_SUBTRACT(f_total, fcu->f_start);
1591 
1592  /* self usage: elapsed minus anything already charged to other calls */
1593  f_others = total_func_time;
1594  INSTR_TIME_SUBTRACT(f_others, fcu->save_total);
1595  f_self = f_total;
1596  INSTR_TIME_SUBTRACT(f_self, f_others);
1597 
1598  /* update backend-wide total time */
1600 
1601  /*
1602  * Compute the new f_total_time as the total elapsed time added to the
1603  * pre-call value of f_total_time. This is necessary to avoid
1604  * double-counting any time taken by recursive calls of myself. (We do
1605  * not need any similar kluge for self time, since that already excludes
1606  * any recursive calls.)
1607  */
1608  INSTR_TIME_ADD(f_total, fcu->save_f_total_time);
1609 
1610  /* update counters in function stats table */
1611  if (finalize)
1612  fs->f_numcalls++;
1613  fs->f_total_time = f_total;
1614  INSTR_TIME_ADD(fs->f_self_time, f_self);
1615 
1616  /* indicate that we have something to send */
1617  have_function_stats = true;
1618 }
instr_time f_self_time
Definition: pgstat.h:463
instr_time f_start
Definition: pgstat.h:990
instr_time save_total
Definition: pgstat.h:988
struct timeval instr_time
Definition: instr_time.h:147
instr_time f_total_time
Definition: pgstat.h:462
PgStat_FunctionCounts * fs
Definition: pgstat.h:984
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:242
static bool have_function_stats
Definition: pgstat.c:173
#define NULL
Definition: c.h:226
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:153
instr_time save_f_total_time
Definition: pgstat.h:986
PgStat_ArchiverStats* pgstat_fetch_stat_archiver ( void  )

Definition at line 2457 of file pgstat.c.

References archiverStats, and backend_read_statsfile().

Referenced by pg_stat_get_archiver().

2458 {
2460 
2461  return &archiverStats;
2462 }
static void backend_read_statsfile(void)
Definition: pgstat.c:4891
static PgStat_ArchiverStats archiverStats
Definition: pgstat.c:223
PgBackendStatus* pgstat_fetch_stat_beentry ( int  beid)
PgStat_StatDBEntry* pgstat_fetch_stat_dbentry ( Oid  dbid)

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

2286 {
2287  /*
2288  * If not done for this transaction, read the statistics collector stats
2289  * file into some hash tables.
2290  */
2292 
2293  /*
2294  * Lookup the requested database; return NULL if not found
2295  */
2297  (void *) &dbid,
2298  HASH_FIND, NULL);
2299 }
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:4891
#define NULL
Definition: c.h:226
static HTAB * pgStatDBHash
Definition: pgstat.c:214
PgStat_StatFuncEntry* pgstat_fetch_stat_funcentry ( Oid  funcid)

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

2369 {
2370  PgStat_StatDBEntry *dbentry;
2371  PgStat_StatFuncEntry *funcentry = NULL;
2372 
2373  /* load the stats file if needed */
2375 
2376  /* Lookup our database, then find the requested function. */
2378  if (dbentry != NULL && dbentry->functions != NULL)
2379  {
2380  funcentry = (PgStat_StatFuncEntry *) hash_search(dbentry->functions,
2381  (void *) &func_id,
2382  HASH_FIND, NULL);
2383  }
2384 
2385  return funcentry;
2386 }
HTAB * functions
Definition: pgstat.h:607
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dbid)
Definition: pgstat.c:2285
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:4891
Oid MyDatabaseId
Definition: globals.c:76
#define NULL
Definition: c.h:226
LocalPgBackendStatus* pgstat_fetch_stat_local_beentry ( int  beid)

Definition at line 2422 of file pgstat.c.

References localNumBackends, NULL, and pgstat_read_current_status().

Referenced by pg_stat_get_activity(), and pg_stat_get_progress_info().

2423 {
2425 
2426  if (beid < 1 || beid > localNumBackends)
2427  return NULL;
2428 
2429  return &localBackendStatusTable[beid - 1];
2430 }
static void pgstat_read_current_status(void)
Definition: pgstat.c:3028
#define NULL
Definition: c.h:226
static LocalPgBackendStatus * localBackendStatusTable
Definition: pgstat.c:215
static int localNumBackends
Definition: pgstat.c:216
int pgstat_fetch_stat_numbackends ( void  )

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

2442 {
2444 
2445  return localNumBackends;
2446 }
static void pgstat_read_current_status(void)
Definition: pgstat.c:3028
static int localNumBackends
Definition: pgstat.c:216
PgStat_StatTabEntry* pgstat_fetch_stat_tabentry ( Oid  relid)

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

2313 {
2314  Oid dbid;
2315  PgStat_StatDBEntry *dbentry;
2316  PgStat_StatTabEntry *tabentry;
2317 
2318  /*
2319  * If not done for this transaction, read the statistics collector stats
2320  * file into some hash tables.
2321  */
2323 
2324  /*
2325  * Lookup our database, then look in its table hash table.
2326  */
2327  dbid = MyDatabaseId;
2329  (void *) &dbid,
2330  HASH_FIND, NULL);
2331  if (dbentry != NULL && dbentry->tables != NULL)
2332  {
2333  tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2334  (void *) &relid,
2335  HASH_FIND, NULL);
2336  if (tabentry)
2337  return tabentry;
2338  }
2339 
2340  /*
2341  * If we didn't find it, maybe it's a shared table.
2342  */
2343  dbid = InvalidOid;
2345  (void *) &dbid,
2346  HASH_FIND, NULL);
2347  if (dbentry != NULL && dbentry->tables != NULL)
2348  {
2349  tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2350  (void *) &relid,
2351  HASH_FIND, NULL);
2352  if (tabentry)
2353  return tabentry;
2354  }
2355 
2356  return NULL;
2357 }
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:4891
Oid MyDatabaseId
Definition: globals.c:76
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:226
static HTAB * pgStatDBHash
Definition: pgstat.c:214
const char* pgstat_get_backend_current_activity ( int  pid,
bool  checkUser 
)

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

3459 {
3460  PgBackendStatus *beentry;
3461  int i;
3462 
3463  beentry = BackendStatusArray;
3464  for (i = 1; i <= MaxBackends; i++)
3465  {
3466  /*
3467  * Although we expect the target backend's entry to be stable, that
3468  * doesn't imply that anyone else's is. To avoid identifying the
3469  * wrong backend, while we check for a match to the desired PID we
3470  * must follow the protocol of retrying if st_changecount changes
3471  * while we examine the entry, or if it's odd. (This might be
3472  * unnecessary, since fetching or storing an int is almost certainly
3473  * atomic, but let's play it safe.) We use a volatile pointer here to
3474  * ensure the compiler doesn't try to get cute.
3475  */
3476  volatile PgBackendStatus *vbeentry = beentry;
3477  bool found;
3478 
3479  for (;;)
3480  {
3481  int before_changecount;
3482  int after_changecount;
3483 
3484  pgstat_save_changecount_before(vbeentry, before_changecount);
3485 
3486  found = (vbeentry->st_procpid == pid);
3487 
3488  pgstat_save_changecount_after(vbeentry, after_changecount);
3489 
3490  if (before_changecount == after_changecount &&
3491  (before_changecount & 1) == 0)
3492  break;
3493 
3494  /* Make sure we can break out of loop if stuck... */
3496  }
3497 
3498  if (found)
3499  {
3500  /* Now it is safe to use the non-volatile pointer */
3501  if (checkUser && !superuser() && beentry->st_userid != GetUserId())
3502  return "<insufficient privilege>";
3503  else if (*(beentry->st_activity) == '\0')
3504  return "<command string not enabled>";
3505  else
3506  return beentry->st_activity;
3507  }
3508 
3509  beentry++;
3510  }
3511 
3512  /* If we get here, caller is in error ... */
3513  return "<backend information not available>";
3514 }
static PgBackendStatus * BackendStatusArray
Definition: pgstat.c:2487
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:937
char * st_activity
Definition: pgstat.h:896
#define pgstat_save_changecount_after(beentry, save_changecount)
Definition: pgstat.h:943
int st_procpid
Definition: pgstat.h:871
int i
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
const char* pgstat_get_crashed_backend_activity ( int  pid,
char *  buffer,
int  buflen 
)

Definition at line 3533 of file pgstat.c.

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

Referenced by LogChildExit().

3534 {
3535  volatile PgBackendStatus *beentry;
3536  int i;
3537 
3538  beentry = BackendStatusArray;
3539 
3540  /*
3541  * We probably shouldn't get here before shared memory has been set up,
3542  * but be safe.
3543  */
3544  if (beentry == NULL || BackendActivityBuffer == NULL)
3545  return NULL;
3546 
3547  for (i = 1; i <= MaxBackends; i++)
3548  {
3549  if (beentry->st_procpid == pid)
3550  {
3551  /* Read pointer just once, so it can't change after validation */
3552  const char *activity = beentry->st_activity;
3553  const char *activity_last;
3554 
3555  /*
3556  * We mustn't access activity string before we verify that it
3557  * falls within the BackendActivityBuffer. To make sure that the
3558  * entire string including its ending is contained within the
3559  * buffer, subtract one activity length from the buffer size.
3560  */
3563 
3564  if (activity < BackendActivityBuffer ||
3565  activity > activity_last)
3566  return NULL;
3567 
3568  /* If no string available, no point in a report */
3569  if (activity[0] == '\0')
3570  return NULL;
3571 
3572  /*
3573  * Copy only ASCII-safe characters so we don't run into encoding
3574  * problems when reporting the message; and be sure not to run off
3575  * the end of memory.
3576  */
3577  ascii_safe_strlcpy(buffer, activity,
3578  Min(buflen, pgstat_track_activity_query_size));
3579 
3580  return buffer;
3581  }
3582 
3583  beentry++;
3584  }
3585 
3586  /* PID not found */
3587  return NULL;
3588 }
static PgBackendStatus * BackendStatusArray
Definition: pgstat.c:2487
static char * BackendActivityBuffer
Definition: pgstat.c:2491
#define Min(x, y)
Definition: c.h:802
static Size BackendActivityBufferSize
Definition: pgstat.c:2492
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:112
#define NULL
Definition: c.h:226
char * st_activity
Definition: pgstat.h:896
int st_procpid
Definition: pgstat.h:871
int i
const char* pgstat_get_wait_event ( uint32  wait_event_info)

Definition at line 3194 of file pgstat.c.

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

Referenced by pg_stat_get_activity(), and pg_stat_get_backend_wait_event().

3195 {
3196  uint32 classId;
3197  uint16 eventId;
3198  const char *event_name;
3199 
3200  /* report process as not waiting. */
3201  if (wait_event_info == 0)
3202  return NULL;
3203 
3204  classId = wait_event_info & 0xFF000000;
3205  eventId = wait_event_info & 0x0000FFFF;
3206 
3207  switch (classId)
3208  {
3209  case PG_WAIT_LWLOCK:
3210  event_name = GetLWLockIdentifier(classId, eventId);
3211  break;
3212  case PG_WAIT_LOCK:
3213  event_name = GetLockNameFromTagType(eventId);
3214  break;
3215  case PG_WAIT_BUFFER_PIN:
3216  event_name = "BufferPin";
3217  break;
3218  case PG_WAIT_ACTIVITY:
3219  {
3220  WaitEventActivity w = (WaitEventActivity) wait_event_info;
3221 
3222  event_name = pgstat_get_wait_activity(w);
3223  break;
3224  }
3225  case PG_WAIT_CLIENT:
3226  {
3227  WaitEventClient w = (WaitEventClient) wait_event_info;
3228 
3229  event_name = pgstat_get_wait_client(w);
3230  break;
3231  }
3232  case PG_WAIT_EXTENSION:
3233  event_name = "Extension";
3234  break;
3235  case PG_WAIT_IPC:
3236  {
3237  WaitEventIPC w = (WaitEventIPC) wait_event_info;
3238 
3239  event_name = pgstat_get_wait_ipc(w);
3240  break;
3241  }
3242  case PG_WAIT_TIMEOUT:
3243  {
3244  WaitEventTimeout w = (WaitEventTimeout) wait_event_info;
3245 
3246  event_name = pgstat_get_wait_timeout(w);
3247  break;
3248  }
3249  default:
3250  event_name = "unknown wait event";
3251  break;
3252  }
3253 
3254  return event_name;
3255 }
#define PG_WAIT_LOCK
Definition: pgstat.h:719
WaitEventIPC
Definition: pgstat.h:779
static const char * pgstat_get_wait_timeout(WaitEventTimeout w)
Definition: pgstat.c:3417
static const char * pgstat_get_wait_ipc(WaitEventIPC w)
Definition: pgstat.c:3365
#define PG_WAIT_ACTIVITY
Definition: pgstat.h:721
static const char * pgstat_get_wait_activity(WaitEventActivity w)
Definition: pgstat.c:3264
unsigned short uint16
Definition: c.h:264
#define PG_WAIT_CLIENT
Definition: pgstat.h:722
WaitEventClient
Definition: pgstat.h:761
static const char * pgstat_get_wait_client(WaitEventClient w)
Definition: pgstat.c:3325
unsigned int uint32
Definition: c.h:265
WaitEventTimeout
Definition: pgstat.h:800
#define PG_WAIT_EXTENSION
Definition: pgstat.h:723
#define PG_WAIT_BUFFER_PIN
Definition: pgstat.h:720
#define NULL
Definition: c.h:226
const char * GetLockNameFromTagType(uint16 locktag_type)
Definition: lmgr.c:1039
#define PG_WAIT_TIMEOUT
Definition: pgstat.h:725
#define PG_WAIT_IPC
Definition: pgstat.h:724
const char * GetLWLockIdentifier(uint32 classId, uint16 eventId)
Definition: lwlock.c:702
#define PG_WAIT_LWLOCK
Definition: pgstat.h:718
WaitEventActivity
Definition: pgstat.h:735
const char* pgstat_get_wait_event_type ( uint32  wait_event_info)

Definition at line 3142 of file pgstat.c.

References NULL, PG_WAIT_ACTIVITY, PG_WAIT_BUFFER_PIN, PG_WAIT_CLIENT, PG_WAIT_EXTENSION, 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().

3143 {
3144  uint32 classId;
3145  const char *event_type;
3146 
3147  /* report process as not waiting. */
3148  if (wait_event_info == 0)
3149  return NULL;
3150 
3151  classId = wait_event_info & 0xFF000000;
3152 
3153  switch (classId)
3154  {
3155  case PG_WAIT_LWLOCK:
3156  event_type = "LWLock";
3157  break;
3158  case PG_WAIT_LOCK:
3159  event_type = "Lock";
3160  break;
3161  case PG_WAIT_BUFFER_PIN:
3162  event_type = "BufferPin";
3163  break;
3164  case PG_WAIT_ACTIVITY:
3165  event_type = "Activity";
3166  break;
3167  case PG_WAIT_CLIENT:
3168  event_type = "Client";
3169  break;
3170  case PG_WAIT_EXTENSION:
3171  event_type = "Extension";
3172  break;
3173  case PG_WAIT_IPC:
3174  event_type = "IPC";
3175  break;
3176  case PG_WAIT_TIMEOUT:
3177  event_type = "Timeout";
3178  break;
3179  default:
3180  event_type = "???";
3181  break;
3182  }
3183 
3184  return event_type;
3185 }
#define PG_WAIT_LOCK
Definition: pgstat.h:719
#define PG_WAIT_ACTIVITY
Definition: pgstat.h:721
#define PG_WAIT_CLIENT
Definition: pgstat.h:722
unsigned int uint32
Definition: c.h:265
#define PG_WAIT_EXTENSION
Definition: pgstat.h:723
#define PG_WAIT_BUFFER_PIN
Definition: pgstat.h:720
#define NULL
Definition: c.h:226
#define PG_WAIT_TIMEOUT
Definition: pgstat.h:725
#define PG_WAIT_IPC
Definition: pgstat.h:724
#define PG_WAIT_LWLOCK
Definition: pgstat.h:718
void pgstat_init ( void  )

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

321 {
322  ACCEPT_TYPE_ARG3 alen;
323  struct addrinfo *addrs = NULL,
324  *addr,
325  hints;
326  int ret;
327  fd_set rset;
328  struct timeval tv;
329  char test_byte;
330  int sel_res;
331  int tries = 0;
332 
333 #define TESTBYTEVAL ((char) 199)
334 
335  /*
336  * This static assertion verifies that we didn't mess up the calculations
337  * involved in selecting maximum payload sizes for our UDP messages.
338  * Because the only consequence of overrunning PGSTAT_MAX_MSG_SIZE would
339  * be silent performance loss from fragmentation, it seems worth having a
340  * compile-time cross-check that we didn't.
341  */
343  "maximum stats message size exceeds PGSTAT_MAX_MSG_SIZE");
344 
345  /*
346  * Create the UDP socket for sending and receiving statistic messages
347  */
348  hints.ai_flags = AI_PASSIVE;
349  hints.ai_family = AF_UNSPEC;
350  hints.ai_socktype = SOCK_DGRAM;
351  hints.ai_protocol = 0;
352  hints.ai_addrlen = 0;
353  hints.ai_addr = NULL;
354  hints.ai_canonname = NULL;
355  hints.ai_next = NULL;
356  ret = pg_getaddrinfo_all("localhost", NULL, &hints, &addrs);
357  if (ret || !addrs)
358  {
359  ereport(LOG,
360  (errmsg("could not resolve \"localhost\": %s",
361  gai_strerror(ret))));
362  goto startup_failed;
363  }
364 
365  /*
366  * On some platforms, pg_getaddrinfo_all() may return multiple addresses
367  * only one of which will actually work (eg, both IPv6 and IPv4 addresses
368  * when kernel will reject IPv6). Worse, the failure may occur at the
369  * bind() or perhaps even connect() stage. So we must loop through the
370  * results till we find a working combination. We will generate LOG
371  * messages, but no error, for bogus combinations.
372  */
373  for (addr = addrs; addr; addr = addr->ai_next)
374  {
375 #ifdef HAVE_UNIX_SOCKETS
376  /* Ignore AF_UNIX sockets, if any are returned. */
377  if (addr->ai_family == AF_UNIX)
378  continue;
379 #endif
380 
381  if (++tries > 1)
382  ereport(LOG,
383  (errmsg("trying another address for the statistics collector")));
384 
385  /*
386  * Create the socket.
387  */
388  if ((pgStatSock = socket(addr->ai_family, SOCK_DGRAM, 0)) == PGINVALID_SOCKET)
389  {
390  ereport(LOG,
392  errmsg("could not create socket for statistics collector: %m")));
393  continue;
394  }
395 
396  /*
397  * Bind it to a kernel assigned port on localhost and get the assigned
398  * port via getsockname().
399  */
400  if (bind(pgStatSock, addr->ai_addr, addr->ai_addrlen) < 0)
401  {
402  ereport(LOG,
404  errmsg("could not bind socket for statistics collector: %m")));
407  continue;
408  }
409 
410  alen = sizeof(pgStatAddr);
411  if (getsockname(pgStatSock, (struct sockaddr *) & pgStatAddr, &alen) < 0)
412  {
413  ereport(LOG,
415  errmsg("could not get address of socket for statistics collector: %m")));
418  continue;
419  }
420 
421  /*
422  * Connect the socket to its own address. This saves a few cycles by
423  * not having to respecify the target address on every send. This also
424  * provides a kernel-level check that only packets from this same
425  * address will be received.
426  */
427  if (connect(pgStatSock, (struct sockaddr *) & pgStatAddr, alen) < 0)
428  {
429  ereport(LOG,
431  errmsg("could not connect socket for statistics collector: %m")));
434  continue;
435  }
436 
437  /*
438  * Try to send and receive a one-byte test message on the socket. This
439  * is to catch situations where the socket can be created but will not
440  * actually pass data (for instance, because kernel packet filtering
441  * rules prevent it).
442  */
443  test_byte = TESTBYTEVAL;
444 
445 retry1:
446  if (send(pgStatSock, &test_byte, 1, 0) != 1)
447  {
448  if (errno == EINTR)
449  goto retry1; /* if interrupted, just retry */
450  ereport(LOG,
452  errmsg("could not send test message on socket for statistics collector: %m")));
455  continue;
456  }
457 
458  /*
459  * There could possibly be a little delay before the message can be
460  * received. We arbitrarily allow up to half a second before deciding
461  * it's broken.
462  */
463  for (;;) /* need a loop to handle EINTR */
464  {
465  FD_ZERO(&rset);
466  FD_SET(pgStatSock, &rset);
467 
468  tv.tv_sec = 0;
469  tv.tv_usec = 500000;
470  sel_res = select(pgStatSock + 1, &rset, NULL, NULL, &tv);
471  if (sel_res >= 0 || errno != EINTR)
472  break;
473  }
474  if (sel_res < 0)
475  {
476  ereport(LOG,
478  errmsg("select() failed in statistics collector: %m")));
481  continue;
482  }
483  if (sel_res == 0 || !FD_ISSET(pgStatSock, &rset))
484  {
485  /*
486  * This is the case we actually think is likely, so take pains to
487  * give a specific message for it.
488  *
489  * errno will not be set meaningfully here, so don't use it.
490  */
491  ereport(LOG,
492  (errcode(ERRCODE_CONNECTION_FAILURE),
493  errmsg("test message did not get through on socket for statistics collector")));
496  continue;
497  }
498 
499  test_byte++; /* just make sure variable is changed */
500 
501 retry2:
502  if (recv(pgStatSock, &test_byte, 1, 0) != 1)
503  {
504  if (errno == EINTR)
505  goto retry2; /* if interrupted, just retry */
506  ereport(LOG,
508  errmsg("could not receive test message on socket for statistics collector: %m")));
511  continue;
512  }
513 
514  if (test_byte != TESTBYTEVAL) /* strictly paranoia ... */
515  {
516  ereport(LOG,
517  (errcode(ERRCODE_INTERNAL_ERROR),
518  errmsg("incorrect test message transmission on socket for statistics collector")));
521  continue;
522  }
523 
524  /* If we get here, we have a working socket */
525  break;
526  }
527 
528  /* Did we find a working address? */
529  if (!addr || pgStatSock == PGINVALID_SOCKET)
530  goto startup_failed;
531 
532  /*
533  * Set the socket to non-blocking IO. This ensures that if the collector
534  * falls behind, statistics messages will be discarded; backends won't
535  * block waiting to send messages to the collector.
536  */
538  {
539  ereport(LOG,
541  errmsg("could not set statistics collector socket to nonblocking mode: %m")));
542  goto startup_failed;
543  }
544 
545  pg_freeaddrinfo_all(hints.ai_family, addrs);
546 
547  return;
548 
549 startup_failed:
550  ereport(LOG,
551  (errmsg("disabling statistics collector for lack of working socket")));
552 
553  if (addrs)
554  pg_freeaddrinfo_all(hints.ai_family, addrs);
555 
559 
560  /*
561  * Adjust GUC variables to suppress useless activity, and for debugging
562  * purposes (seeing track_counts off is a clue that we failed here). We
563  * use PGC_S_OVERRIDE because there is no point in trying to turn it back
564  * on from postgresql.conf without a restart.
565  */
566  SetConfigOption("track_counts", "off", PGC_INTERNAL, PGC_S_OVERRIDE);
567 }
#define send(s, buf, len, flags)
Definition: win32.h:386
#define connect(s, name, namelen)
Definition: win32.h:383
void pg_freeaddrinfo_all(int hint_ai_family, struct addrinfo *ai)
Definition: ip.c:89
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:133
#define closesocket
Definition: port.h:328
#define socket(af, type, protocol)
Definition: win32.h:379
int errcode(int sqlerrcode)
Definition: elog.c:575
#define select(n, r, w, e, timeout)
Definition: win32.h:384
#define recv(s, buf, len, flags)
Definition: win32.h:385
#define LOG
Definition: elog.h:26
#define gai_strerror
Definition: getaddrinfo.h:148
int pg_getaddrinfo_all(const char *hostname, const char *servname, const struct addrinfo *hintp, struct addrinfo **result)
Definition: ip.c:58
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:753
#define bind(s, addr, addrlen)
Definition: win32.h:380
#define TESTBYTEVAL
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:6633
#define AI_PASSIVE
Definition: getaddrinfo.h:64
#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:295
static struct sockaddr_storage pgStatAddr
Definition: pgstat.c:135
#define NULL
Definition: c.h:226
bool pg_set_noblock(pgsocket sock)
Definition: noblock.c:21
struct addrinfo * ai_next
Definition: getaddrinfo.h:109
int errmsg(const char *fmt,...)
Definition: elog.c:797
void pgstat_init_function_usage ( FunctionCallInfoData fcinfo,
PgStat_FunctionCallUsage fcu 
)

Definition at line 1505 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 EventTriggerInvoke(), ExecCallTriggerFunc(), ExecMakeFunctionResultNoSets(), ExecMakeFunctionResultSet(), ExecMakeTableFunctionResult(), and fmgr_security_definer().

1507 {
1508  PgStat_BackendFunctionEntry *htabent;
1509  bool found;
1510 
1511  if (pgstat_track_functions <= fcinfo->flinfo->fn_stats)
1512  {
1513  /* stats not wanted */
1514  fcu->fs = NULL;
1515  return;
1516  }
1517 
1518  if (!pgStatFunctions)
1519  {
1520  /* First time through - initialize function stat table */
1521  HASHCTL hash_ctl;
1522 
1523  memset(&hash_ctl, 0, sizeof(hash_ctl));
1524  hash_ctl.keysize = sizeof(Oid);
1525  hash_ctl.entrysize = sizeof(PgStat_BackendFunctionEntry);
1526  pgStatFunctions = hash_create("Function stat entries",
1528  &hash_ctl,
1529  HASH_ELEM | HASH_BLOBS);
1530  }
1531 
1532  /* Get the stats entry for this function, create if necessary */
1533  htabent = hash_search(pgStatFunctions, &fcinfo->flinfo->fn_oid,
1534  HASH_ENTER, &found);
1535  if (!found)
1536  MemSet(&htabent->f_counts, 0, sizeof(PgStat_FunctionCounts));
1537 
1538  fcu->fs = &htabent->f_counts;
1539 
1540  /* save stats for this function, later used to compensate for recursion */
1541  fcu->save_f_total_time = htabent->f_counts.f_total_time;
1542 
1543  /* save current backend-wide total time */
1544  fcu->save_total = total_func_time;
1545 
1546  /* get clock time as of function start */
1548 }
instr_time f_start
Definition: pgstat.h:990
#define HASH_ELEM
Definition: hsearch.h:87
instr_time save_total
Definition: pgstat.h:988
Size entrysize
Definition: hsearch.h:73
#define MemSet(start, val, len)
Definition: c.h:853
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:984
FmgrInfo * flinfo
Definition: fmgr.h:71
PgStat_FunctionCounts f_counts
Definition: pgstat.h:473
#define PGSTAT_FUNCTION_HASH_SIZE
Definition: pgstat.c:102
#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:56
static instr_time total_func_time
Definition: pgstat.c:242
static HTAB * pgStatFunctions
Definition: pgstat.c:167
#define NULL
Definition: c.h:226
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:153
instr_time save_f_total_time
Definition: pgstat.h:986
struct PgStat_BackendFunctionEntry PgStat_BackendFunctionEntry
void pgstat_initialize ( void  )

Definition at line 2642 of file pgstat.c.

References Assert, MaxBackends, MyBackendId, on_shmem_exit(), and pgstat_beshutdown_hook().

Referenced by InitPostgres().

2643 {
2644  /* Initialize MyBEEntry */
2647 
2648  /* Set up a process-exit hook to clean up */
2650 }
static PgBackendStatus * BackendStatusArray
Definition: pgstat.c:2487
BackendId MyBackendId
Definition: globals.c:72
int MaxBackends
Definition: globals.c:126
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2488
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:2767
#define Assert(condition)
Definition: c.h:671
void pgstat_initstats ( Relation  rel)

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

1635 {
1636  Oid rel_id = rel->rd_id;
1637  char relkind = rel->rd_rel->relkind;
1638 
1639  /* We only count stats for things that have storage */
1640  if (!(relkind == RELKIND_RELATION ||
1641  relkind == RELKIND_MATVIEW ||
1642  relkind == RELKIND_INDEX ||
1643  relkind == RELKIND_TOASTVALUE ||
1644  relkind == RELKIND_SEQUENCE))
1645  {
1646  rel->pgstat_info = NULL;
1647  return;
1648  }
1649 
1651  {
1652  /* We're not counting at all */
1653  rel->pgstat_info = NULL;
1654  return;
1655  }
1656 
1657  /*
1658  * If we already set up this relation in the current transaction, nothing
1659  * to do.
1660  */
1661  if (rel->pgstat_info != NULL &&
1662  rel->pgstat_info->t_id == rel_id)
1663  return;
1664 
1665  /* Else find or make the PgStat_TableStatus entry, and update link */
1666  rel->pgstat_info = get_tabstat_entry(rel_id, rel->rd_rel->relisshared);
1667 }
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:133
#define RELKIND_MATVIEW
Definition: pg_class.h:167
Form_pg_class rd_rel
Definition: rel.h:113
unsigned int Oid
Definition: postgres_ext.h:31
bool pgstat_track_counts
Definition: pgstat.c:110
Oid rd_id
Definition: rel.h:115
static PgStat_TableStatus * get_tabstat_entry(Oid rel_id, bool isshared)
Definition: pgstat.c:1673
#define RELKIND_TOASTVALUE
Definition: pg_class.h:163
#define PGINVALID_SOCKET
Definition: port.h:24
#define NULL
Definition: c.h:226
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:214
#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 1470 of file pgstat.c.

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

1471 {
1472  PgStat_MsgDummy msg;
1473 
1475  return;
1476 
1478  pgstat_send(&msg, sizeof(msg));
1479 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3617
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:133
#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:3604
void pgstat_progress_end_command ( void  )

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

2952 {
2953  volatile PgBackendStatus *beentry = MyBEEntry;
2954 
2955  if (!beentry)
2956  return;
2959  return;
2960 
2965 }
Oid st_progress_command_target
Definition: pgstat.h:908
ProgressCommandType st_progress_command
Definition: pgstat.h:907
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2488
#define pgstat_increment_changecount_before(beentry)
Definition: pgstat.h:924
#define pgstat_increment_changecount_after(beentry)
Definition: pgstat.h:930
#define InvalidOid
Definition: postgres_ext.h:36
bool pgstat_track_activities
Definition: pgstat.c:109
void pgstat_progress_start_command ( ProgressCommandType  cmdtype,
Oid  relid 
)

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

2880 {
2881  volatile PgBackendStatus *beentry = MyBEEntry;
2882 
2883  if (!beentry || !pgstat_track_activities)
2884  return;
2885 
2887  beentry->st_progress_command = cmdtype;
2888  beentry->st_progress_command_target = relid;
2889  MemSet(&beentry->st_progress_param, 0, sizeof(beentry->st_progress_param));
2891 }
Oid st_progress_command_target
Definition: pgstat.h:908
#define MemSet(start, val, len)
Definition: c.h:853
ProgressCommandType st_progress_command
Definition: pgstat.h:907
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2488
#define pgstat_increment_changecount_before(beentry)
Definition: pgstat.h:924
#define pgstat_increment_changecount_after(beentry)
Definition: pgstat.h:930
int64 st_progress_param[PGSTAT_NUM_PROGRESS_PARAM]
Definition: pgstat.h:909
bool pgstat_track_activities
Definition: pgstat.c:109
void pgstat_progress_update_multi_param ( int  nparam,
const int *  index,
const int64 *  val 
)

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

2924 {
2925  volatile PgBackendStatus *beentry = MyBEEntry;
2926  int i;
2927 
2928  if (!beentry || !pgstat_track_activities || nparam == 0)
2929  return;
2930 
2932 
2933  for (i = 0; i < nparam; ++i)
2934  {
2935  Assert(index[i] >= 0 && index[i] < PGSTAT_NUM_PROGRESS_PARAM);
2936 
2937  beentry->st_progress_param[index[i]] = val[i];
2938  }
2939 
2941 }
Definition: type.h:90
#define PGSTAT_NUM_PROGRESS_PARAM
Definition: pgstat.h:817
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2488
#define pgstat_increment_changecount_before(beentry)
Definition: pgstat.h:924
#define pgstat_increment_changecount_after(beentry)
Definition: pgstat.h:930
#define Assert(condition)
Definition: c.h:671
int64 st_progress_param[PGSTAT_NUM_PROGRESS_PARAM]
Definition: pgstat.h:909
bool pgstat_track_activities
Definition: pgstat.c:109
int i
long val
Definition: informix.c:689
void pgstat_progress_update_param ( int  index,
int64  val 
)

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

2901 {
2902  volatile PgBackendStatus *beentry = MyBEEntry;
2903 
2905 
2906  if (!beentry || !pgstat_track_activities)
2907  return;
2908 
2910  beentry->st_progress_param[index] = val;
2912 }
Definition: type.h:90
#define PGSTAT_NUM_PROGRESS_PARAM
Definition: pgstat.h:817
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2488
#define pgstat_increment_changecount_before(beentry)
Definition: pgstat.h:924
#define pgstat_increment_changecount_after(beentry)
Definition: pgstat.h:930
#define Assert(condition)
Definition: c.h:671
int64 st_progress_param[PGSTAT_NUM_PROGRESS_PARAM]
Definition: pgstat.h:909
bool pgstat_track_activities
Definition: pgstat.c:109
long val
Definition: informix.c:689
void pgstat_report_activity ( BackendState  state,
const char *  cmd_str 
)

Definition at line 2805 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(), ApplyLoop(), autovac_report_activity(), exec_bind_message(), exec_execute_message(), exec_parse_message(), exec_simple_query(), initialize_worker_spi(), ParallelQueryMain(), PostgresMain(), and WalSndLoop().

2806 {
2807  volatile PgBackendStatus *beentry = MyBEEntry;
2808  TimestampTz start_timestamp;
2809  TimestampTz current_timestamp;
2810  int len = 0;
2811 
2812  TRACE_POSTGRESQL_STATEMENT_STATUS(cmd_str);
2813 
2814  if (!beentry)
2815  return;
2816 
2818  {
2819  if (beentry->st_state != STATE_DISABLED)
2820  {
2821  volatile PGPROC *proc = MyProc;
2822 
2823  /*
2824  * track_activities is disabled, but we last reported a
2825  * non-disabled state. As our final update, change the state and
2826  * clear fields we will not be updating anymore.
2827  */
2829  beentry->st_state = STATE_DISABLED;
2830  beentry->st_state_start_timestamp = 0;
2831  beentry->st_activity[0] = '\0';
2832  beentry->st_activity_start_timestamp = 0;
2833  /* st_xact_start_timestamp and wait_event_info are also disabled */
2834  beentry->st_xact_start_timestamp = 0;
2835  proc->wait_event_info = 0;
2837  }
2838  return;
2839  }
2840 
2841  /*
2842  * To minimize the time spent modifying the entry, fetch all the needed
2843  * data first.
2844  */
2845  start_timestamp = GetCurrentStatementStartTimestamp();
2846  if (cmd_str != NULL)
2847  {
2848  len = pg_mbcliplen(cmd_str, strlen(cmd_str),
2850  }
2851  current_timestamp = GetCurrentTimestamp();
2852 
2853  /*
2854  * Now update the status entry
2855  */
2857 
2858  beentry->st_state = state;
2859  beentry->st_state_start_timestamp = current_timestamp;
2860 
2861  if (cmd_str != NULL)
2862  {
2863  memcpy((char *) beentry->st_activity, cmd_str, len);
2864  beentry->st_activity[len] = '\0';
2865  beentry->st_activity_start_timestamp = start_timestamp;
2866  }
2867 
2869 }
uint32 wait_event_info
Definition: proc.h:162
TimestampTz st_activity_start_timestamp
Definition: pgstat.h:876
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1687
PGPROC * MyProc
Definition: proc.c:67
BackendState st_state
Definition: pgstat.h:890
double TimestampTz
Definition: timestamp.h:51
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:831
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2488
#define pgstat_increment_changecount_before(beentry)
Definition: pgstat.h:924
int pgstat_track_activity_query_size
Definition: pgstat.c:112
TimestampTz st_state_start_timestamp
Definition: pgstat.h:877
#define pgstat_increment_changecount_after(beentry)
Definition: pgstat.h:930
#define NULL
Definition: c.h:226
char * st_activity
Definition: pgstat.h:896
Definition: regguts.h:298
TimestampTz st_xact_start_timestamp
Definition: pgstat.h:875
bool pgstat_track_activities
Definition: pgstat.c:109
Definition: proc.h:84
TimestampTz GetCurrentStatementStartTimestamp(void)
Definition: xact.c:716
void pgstat_report_analyze ( Relation  rel,
PgStat_Counter  livetuples,
PgStat_Counter  deadtuples,
bool  resetcounter 
)

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

1360 {
1361  PgStat_MsgAnalyze msg;
1362 
1364  return;
1365 
1366  /*
1367  * Unlike VACUUM, ANALYZE might be running inside a transaction that has
1368  * already inserted and/or deleted rows in the target table. ANALYZE will
1369  * have counted such rows as live or dead respectively. Because we will
1370  * report our counts of such rows at transaction end, we should subtract
1371  * off these counts from what we send to the collector now, else they'll
1372  * be double-counted after commit. (This approach also ensures that the
1373  * collector ends up with the right numbers if we abort instead of
1374  * committing.)
1375  */
1376  if (rel->pgstat_info != NULL)
1377  {
1379 
1380  for (trans = rel->pgstat_info->trans; trans; trans = trans->upper)
1381  {
1382  livetuples -= trans->tuples_inserted - trans->tuples_deleted;
1383  deadtuples -= trans->tuples_updated + trans->tuples_deleted;
1384  }
1385  /* count stuff inserted by already-aborted subxacts, too */
1386  deadtuples -= rel->pgstat_info->t_counts.t_delta_dead_tuples;
1387  /* Since ANALYZE's counts are estimates, we could have underflowed */
1388  livetuples = Max(livetuples, 0);
1389  deadtuples = Max(deadtuples, 0);
1390  }
1391 
1393  msg.m_databaseid = rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId;
1394  msg.m_tableoid = RelationGetRelid(rel);
1396  msg.m_resetcounter = resetcounter;
1398  msg.m_live_tuples = livetuples;
1399  msg.m_dead_tuples = deadtuples;
1400  pgstat_send(&msg, sizeof(msg));
1401 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3617
PgStat_Counter tuples_updated
Definition: pgstat.h:168
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:133
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1687
TimestampTz m_analyzetime
Definition: pgstat.h:387
PgStat_TableCounts t_counts
Definition: pgstat.h:158
Form_pg_class rd_rel
Definition: rel.h:113
bool m_resetcounter
Definition: pgstat.h:386
bool pgstat_track_counts
Definition: pgstat.c:110
PgStat_Counter tuples_inserted
Definition: pgstat.h:167
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:111
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:2989
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:796
#define NULL
Definition: c.h:226
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3604
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:214
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:383
PgStat_Counter m_dead_tuples
Definition: pgstat.h:389
#define RelationGetRelid(relation)
Definition: rel.h:413
void pgstat_report_appname ( const char *  appname)

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

2975 {
2976  volatile PgBackendStatus *beentry = MyBEEntry;
2977  int len;
2978 
2979  if (!beentry)
2980  return;
2981 
2982  /* This should be unnecessary if GUC did its job, but be safe */
2983  len = pg_mbcliplen(appname, strlen(appname), NAMEDATALEN - 1);
2984 
2985  /*
2986  * Update my status entry, following the protocol of bumping
2987  * st_changecount before and after. We use a volatile pointer here to
2988  * ensure the compiler doesn't try to get cute.
2989  */
2991 
2992  memcpy((char *) beentry->st_appname, appname, len);
2993  beentry->st_appname[len] = '\0';
2994 
2996 }
#define NAMEDATALEN
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:831
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2488
#define pgstat_increment_changecount_before(beentry)
Definition: pgstat.h:924
#define pgstat_increment_changecount_after(beentry)
Definition: pgstat.h:930
char * st_appname
Definition: pgstat.h:893
void pgstat_report_autovac ( Oid  dboid)

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

1308 {
1310 
1312  return;
1313 
1315  msg.m_databaseid = dboid;
1317 
1318  pgstat_send(&msg, sizeof(msg));
1319 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3617
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:133
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1687
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:3604
TimestampTz m_start_time
Definition: pgstat.h:354
void pgstat_report_deadlock ( void  )

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

1431 {
1432  PgStat_MsgDeadlock msg;
1433 
1435  return;
1436 
1438  msg.m_databaseid = MyDatabaseId;
1439  pgstat_send(&msg, sizeof(msg));
1440 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3617
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:133
bool pgstat_track_counts
Definition: pgstat.c:110
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:3604
void pgstat_report_recovery_conflict ( int  reason)

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

1411 {
1413 
1415  return;
1416 
1418  msg.m_databaseid = MyDatabaseId;
1419  msg.m_reason = reason;
1420  pgstat_send(&msg, sizeof(msg));
1421 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3617
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:133
bool pgstat_track_counts
Definition: pgstat.c:110
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:3604
void pgstat_report_stat ( bool  force)

Definition at line 747 of file pgstat.c.

References Assert, GetCurrentTransactionStopTimestamp(), 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 pgstat_beshutdown_hook(), and PostgresMain().

748 {
749  /* we assume this inits to all zeroes: */
750  static const PgStat_TableCounts all_zeroes;
751  static TimestampTz last_report = 0;
752 
754  PgStat_MsgTabstat regular_msg;
755  PgStat_MsgTabstat shared_msg;
756  TabStatusArray *tsa;
757  int i;
758 
759  /* Don't expend a clock check if nothing to do */
760  if ((pgStatTabList == NULL || pgStatTabList->tsa_used == 0) &&
761  pgStatXactCommit == 0 && pgStatXactRollback == 0 &&
763  return;
764 
765  /*
766  * Don't send a message unless it's been at least PGSTAT_STAT_INTERVAL
767  * msec since we last sent one, or the caller wants to force stats out.
768  */
770  if (!force &&
772  return;
773  last_report = now;
774 
775  /*
776  * Scan through the TabStatusArray struct(s) to find tables that actually
777  * have counts, and build messages to send. We have to separate shared
778  * relations from regular ones because the databaseid field in the message
779  * header has to depend on that.
780  */
781  regular_msg.m_databaseid = MyDatabaseId;
782  shared_msg.m_databaseid = InvalidOid;
783  regular_msg.m_nentries = 0;
784  shared_msg.m_nentries = 0;
785 
786  for (tsa = pgStatTabList; tsa != NULL; tsa = tsa->tsa_next)
787  {
788  for (i = 0; i < tsa->tsa_used; i++)
789  {
790  PgStat_TableStatus *entry = &tsa->tsa_entries[i];
791  PgStat_MsgTabstat *this_msg;
792  PgStat_TableEntry *this_ent;
793 
794  /* Shouldn't have any pending transaction-dependent counts */
795  Assert(entry->trans == NULL);
796 
797  /*
798  * Ignore entries that didn't accumulate any actual counts, such
799  * as indexes that were opened by the planner but not used.
800  */
801  if (memcmp(&entry->t_counts, &all_zeroes,
802  sizeof(PgStat_TableCounts)) == 0)
803  continue;
804 
805  /*
806  * OK, insert data into the appropriate message, and send if full.
807  */
808  this_msg = entry->t_shared ? &shared_msg : &regular_msg;
809  this_ent = &this_msg->m_entry[this_msg->m_nentries];
810  this_ent->t_id = entry->t_id;
811  memcpy(&this_ent->t_counts, &entry->t_counts,
812  sizeof(PgStat_TableCounts));
813  if (++this_msg->m_nentries >= PGSTAT_NUM_TABENTRIES)
814  {
815  pgstat_send_tabstat(this_msg);
816  this_msg->m_nentries = 0;
817  }
818  }
819  /* zero out TableStatus structs after use */
820  MemSet(tsa->tsa_entries, 0,
821  tsa->tsa_used * sizeof(PgStat_TableStatus));
822  tsa->tsa_used = 0;
823  }
824 
825  /*
826  * Send partial messages. Make sure that any pending xact commit/abort
827  * gets counted, even if there are no table stats to send.
828  */
829  if (regular_msg.m_nentries > 0 ||
831  pgstat_send_tabstat(&regular_msg);
832  if (shared_msg.m_nentries > 0)
833  pgstat_send_tabstat(&shared_msg);
834 
835  /* Now, send function statistics */
837 }
int tsa_used
Definition: pgstat.c:157
static int pgStatXactCommit
Definition: pgstat.c:191
#define MemSet(start, val, len)
Definition: c.h:853
struct TabStatusArray * tsa_next
Definition: pgstat.c:156
PgStat_TableCounts t_counts
Definition: pgstat.h:158
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1820
double TimestampTz
Definition: timestamp.h:51
static void pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg)
Definition: pgstat.c:843
static int pgStatXactRollback
Definition: pgstat.c:192
#define PGSTAT_STAT_INTERVAL
Definition: pgstat.c:76
PgStat_TableStatus tsa_entries[TABSTAT_QUANTUM]
Definition: pgstat.c:158
#define PGSTAT_NUM_TABENTRIES
Definition: pgstat.h:263
Oid MyDatabaseId
Definition: globals.c:76
static TabStatusArray * pgStatTabList
Definition: pgstat.c:161
#define InvalidOid
Definition: postgres_ext.h:36
static bool have_function_stats
Definition: pgstat.c:173
#define NULL
Definition: c.h:226
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition: xact.c:728
#define Assert(condition)
Definition: c.h:671
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:887
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1651
void pgstat_report_tempfile ( size_t  filesize)

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

1450 {
1451  PgStat_MsgTempFile msg;
1452 
1454  return;
1455 
1457  msg.m_databaseid = MyDatabaseId;
1458  msg.m_filesize = filesize;
1459  pgstat_send(&msg, sizeof(msg));
1460 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3617
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:133
PgStat_MsgHdr m_hdr
Definition: pgstat.h:443
size_t m_filesize
Definition: pgstat.h:446
bool pgstat_track_counts
Definition: pgstat.c:110
#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:3604
void pgstat_report_vacuum ( Oid  tableoid,
bool  shared,
PgStat_Counter  livetuples,
PgStat_Counter  deadtuples 
)

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

1331 {
1332  PgStat_MsgVacuum msg;
1333 
1335  return;
1336 
1338  msg.m_databaseid = shared ? InvalidOid : MyDatabaseId;
1339  msg.m_tableoid = tableoid;
1342  msg.m_live_tuples = livetuples;
1343  msg.m_dead_tuples = deadtuples;
1344  pgstat_send(&msg, sizeof(msg));
1345 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3617
PgStat_Counter m_dead_tuples
Definition: pgstat.h:371
TimestampTz m_vacuumtime
Definition: pgstat.h:369
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:133
PgStat_Counter m_live_tuples
Definition: pgstat.h:370
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1687
Oid m_databaseid
Definition: pgstat.h:366
bool m_autovacuum
Definition: pgstat.h:368
bool pgstat_track_counts
Definition: pgstat.c:110
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:2989
#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:3604
PgStat_MsgHdr m_hdr
Definition: pgstat.h:365
static void pgstat_report_wait_end ( void  )
inlinestatic

Definition at line 1124 of file pgstat.h.

References MyProc, pgstat_track_activities, and PGPROC::wait_event_info.

Referenced by AbortSubTransaction(), AbortTransaction(), BackgroundWriterMain(), CheckpointerMain(), LWLockReportWaitEnd(), ShutdownAuxiliaryProcess(), WaitEventSetWait(), WalSndErrorCleanup(), and WalWriterMain().

1125 {
1126  volatile PGPROC *proc = MyProc;
1127 
1128  if (!pgstat_track_activities || !proc)
1129  return;
1130 
1131  /*
1132  * Since this is a four-byte field which is always read and written as
1133  * four-bytes, updates are atomic.
1134  */
1135  proc->wait_event_info = 0;
1136 }
uint32 wait_event_info
Definition: proc.h:162
PGPROC * MyProc
Definition: proc.c:67
bool pgstat_track_activities
Definition: pgstat.c:109
Definition: proc.h:84
static void pgstat_report_wait_start ( uint32  wait_event_info)
inlinestatic

Definition at line 1100 of file pgstat.h.

References MyProc, pgstat_track_activities, and PGPROC::wait_event_info.

Referenced by LWLockReportWaitStart(), and WaitEventSetWait().

1101 {
1102  volatile PGPROC *proc = MyProc;
1103 
1104  if (!pgstat_track_activities || !proc)
1105  return;
1106 
1107  /*
1108  * Since this is a four-byte field which is always read and written as
1109  * four-bytes, updates are atomic.
1110  */
1111  proc->wait_event_info = wait_event_info;
1112 }
uint32 wait_event_info
Definition: proc.h:162
PGPROC * MyProc
Definition: proc.c:67
bool pgstat_track_activities
Definition: pgstat.c:109
Definition: proc.h:84
void pgstat_report_xact_timestamp ( TimestampTz  tstamp)

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

3004 {
3005  volatile PgBackendStatus *beentry = MyBEEntry;
3006 
3007  if (!pgstat_track_activities || !beentry)
3008  return;
3009 
3010  /*
3011  * Update my status entry, following the protocol of bumping
3012  * st_changecount before and after. We use a volatile pointer here to
3013  * ensure the compiler doesn't try to get cute.
3014  */
3016  beentry->st_xact_start_timestamp = tstamp;
3018 }
static PgBackendStatus * MyBEEntry
Definition: pgstat.c:2488
#define pgstat_increment_changecount_before(beentry)
Definition: pgstat.h:924
#define pgstat_increment_changecount_after(beentry)
Definition: pgstat.h:930
TimestampTz st_xact_start_timestamp
Definition: pgstat.h:875
bool pgstat_track_activities
Definition: pgstat.c:109
void pgstat_reset_all ( void  )

Definition at line 621 of file pgstat.c.

References pgstat_reset_remove_files(), pgstat_stat_directory, and PGSTAT_STAT_PERMANENT_DIRECTORY.

Referenced by StartupXLOG().

622 {
625 }
#define PGSTAT_STAT_PERMANENT_DIRECTORY
Definition: pgstat.h:29
char * pgstat_stat_directory
Definition: pgstat.c:118
static void pgstat_reset_remove_files(const char *directory)
Definition: pgstat.c:573
void pgstat_reset_counters ( void  )

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

1231 {
1233 
1235  return;
1236 
1238  msg.m_databaseid = MyDatabaseId;
1239  pgstat_send(&msg, sizeof(msg));
1240 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3617
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:133
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:3604
void pgstat_reset_shared_counters ( const char *  )

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

1253 {
1255 
1257  return;
1258 
1259  if (strcmp(target, "archiver") == 0)
1261  else if (strcmp(target, "bgwriter") == 0)
1263  else
1264  ereport(ERROR,
1265  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1266  errmsg("unrecognized reset target: \"%s\"", target),
1267  errhint("Target must be \"archiver\" or \"bgwriter\".")));
1268 
1270  pgstat_send(&msg, sizeof(msg));
1271 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3617
int errhint(const char *fmt,...)
Definition: elog.c:987
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:133
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:3604
int errmsg(const char *fmt,...)
Definition: elog.c:797
void pgstat_reset_single_counter ( Oid  objectid,
PgStat_Single_Reset_Type  type 
)

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

1284 {
1286 
1288  return;
1289 
1291  msg.m_databaseid = MyDatabaseId;
1292  msg.m_resettype = type;
1293  msg.m_objectid = objoid;
1294 
1295  pgstat_send(&msg, sizeof(msg));
1296 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3617
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:133
#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:3604
PgStat_Single_Reset_Type m_resettype
Definition: pgstat.h:341
void pgstat_send_archiver ( const char *  xlog,
bool  failed 
)

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

3648 {
3649  PgStat_MsgArchiver msg;
3650 
3651  /*
3652  * Prepare and send the message
3653  */
3655  msg.m_failed = failed;
3656  StrNCpy(msg.m_xlog, xlog, sizeof(msg.m_xlog));
3658  pgstat_send(&msg, sizeof(msg));
3659 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3617
char m_xlog[MAX_XFN_CHARS+1]
Definition: pgstat.h:401
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1687
TimestampTz m_timestamp
Definition: pgstat.h:402
#define StrNCpy(dst, src, len)
Definition: c.h:826
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3604
PgStat_MsgHdr m_hdr
Definition: pgstat.h:399
void pgstat_send_bgwriter ( void  )

Definition at line 3668 of file pgstat.c.

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

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

3669 {
3670  /* We assume this initializes to zeroes */
3671  static const PgStat_MsgBgWriter all_zeroes;
3672 
3673  /*
3674  * This function can be called even if nothing at all has happened. In
3675  * this case, avoid sending a completely empty message to the stats
3676  * collector.
3677  */
3678  if (memcmp(&BgWriterStats, &all_zeroes, sizeof(PgStat_MsgBgWriter)) == 0)
3679  return;
3680 
3681  /*
3682  * Prepare and send the message
3683  */
3686 
3687  /*
3688  * Clear out the statistics buffer, so it can be re-used.
3689  */
3690  MemSet(&BgWriterStats, 0, sizeof(BgWriterStats));
3691 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3617
PgStat_MsgHdr m_hdr
Definition: pgstat.h:411
PgStat_MsgBgWriter BgWriterStats
Definition: pgstat.c:127
#define MemSet(start, val, len)
Definition: c.h:853
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3604
int pgstat_start ( void  )

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

664 {
665  time_t curtime;
666  pid_t pgStatPid;
667 
668  /*
669  * Check that the socket is there, else pgstat_init failed and we can do
670  * nothing useful.
671  */
673  return 0;
674 
675  /*
676  * Do nothing if too soon since last collector start. This is a safety
677  * valve to protect against continuous respawn attempts if the collector
678  * is dying immediately at launch. Note that since we will be re-called
679  * from the postmaster main loop, we will get another chance later.
680  */
681  curtime = time(NULL);
682  if ((unsigned int) (curtime - last_pgstat_start_time) <
683  (unsigned int) PGSTAT_RESTART_INTERVAL)
684  return 0;
685  last_pgstat_start_time = curtime;
686 
687  /*
688  * Okay, fork off the collector.
689  */
690 #ifdef EXEC_BACKEND
691  switch ((pgStatPid = pgstat_forkexec()))
692 #else
693  switch ((pgStatPid = fork_process()))
694 #endif
695  {
696  case -1:
697  ereport(LOG,
698  (errmsg("could not fork statistics collector: %m")));
699  return 0;
700 
701 #ifndef EXEC_BACKEND
702  case 0:
703  /* in postmaster child ... */
705 
706  /* Close the postmaster's sockets */
707  ClosePostmasterPorts(false);
708 
709  /* Drop our connection to postmaster's shared memory, as well */
710  dsm_detach_all();
712 
714  break;
715 #endif
716 
717  default:
718  return (int) pgStatPid;
719  }
720 
721  /* shouldn't get here */
722  return 0;
723 }
#define PGSTAT_RESTART_INTERVAL
Definition: pgstat.c:88
NON_EXEC_STATIC void PgstatCollectorMain(int argc, char *argv[]) pg_attribute_noreturn()
Definition: pgstat.c:3704
void InitPostmasterChild(void)
Definition: miscinit.c:175
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:133
pid_t fork_process(void)
Definition: fork_process.c:31
#define LOG
Definition: elog.h:26
void ClosePostmasterPorts(bool am_syslogger)
Definition: postmaster.c:2414
void PGSharedMemoryDetach(void)
Definition: sysv_shmem.c:768
#define ereport(elevel, rest)
Definition: elog.h:122
#define PGINVALID_SOCKET
Definition: port.h:24
#define NULL
Definition: c.h:226
void dsm_detach_all(void)
Definition: dsm.c:655
static time_t last_pgstat_start_time
Definition: pgstat.c:137
int errmsg(const char *fmt,...)
Definition: elog.c:797
void pgstat_twophase_postabort ( TransactionId  xid,
uint16  info,
void *  recdata,
uint32  len 
)

Definition at line 2251 of file pgstat.c.

References TwoPhasePgStatRecord::deleted_pre_trunc, get_tabstat_entry(), TwoPhasePgStatRecord::inserted_pre_trunc, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_delta_dead_tuples, TwoPhasePgStatRecord::t_id, TwoPhasePgStatRecord::t_shared, TwoPhasePgStatRecord::t_truncated, PgStat_TableCounts::t_tuples_deleted, PgStat_TableCounts::t_tuples_inserted, PgStat_TableCounts::t_tuples_updated, TwoPhasePgStatRecord::tuples_deleted, TwoPhasePgStatRecord::tuples_inserted, TwoPhasePgStatRecord::tuples_updated, and TwoPhasePgStatRecord::updated_pre_trunc.

2253 {
2254  TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
2255  PgStat_TableStatus *pgstat_info;
2256 
2257  /* Find or create a tabstat entry for the rel */
2258  pgstat_info = get_tabstat_entry(rec->t_id, rec->t_shared);
2259 
2260  /* Same math as in AtEOXact_PgStat, abort case */
2261  if (rec->t_truncated)
2262  {
2263  rec->tuples_inserted = rec->inserted_pre_trunc;
2264  rec->tuples_updated = rec->updated_pre_trunc;
2265  rec->tuples_deleted = rec->deleted_pre_trunc;
2266  }
2267  pgstat_info->t_counts.t_tuples_inserted += rec->tuples_inserted;
2268  pgstat_info->t_counts.t_tuples_updated += rec->tuples_updated;
2269  pgstat_info->t_counts.t_tuples_deleted += rec->tuples_deleted;
2270  pgstat_info->t_counts.t_delta_dead_tuples +=
2271  rec->tuples_inserted + rec->tuples_updated;
2272 }
PgStat_TableCounts t_counts
Definition: pgstat.h:158
PgStat_Counter deleted_pre_trunc
Definition: pgstat.c:204
PgStat_Counter t_tuples_updated
Definition: pgstat.h:105
PgStat_Counter updated_pre_trunc
Definition: pgstat.c:203
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:111
PgStat_Counter inserted_pre_trunc
Definition: pgstat.c:202
PgStat_Counter tuples_updated
Definition: pgstat.c:200
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:106
static PgStat_TableStatus * get_tabstat_entry(Oid rel_id, bool isshared)
Definition: pgstat.c:1673
PgStat_Counter tuples_inserted
Definition: pgstat.c:199
PgStat_Counter tuples_deleted
Definition: pgstat.c:201
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:104
void pgstat_twophase_postcommit ( TransactionId  xid,
uint16  info,
void *  recdata,
uint32  len 
)

Definition at line 2215 of file pgstat.c.

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

2217 {
2218  TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
2219  PgStat_TableStatus *pgstat_info;
2220 
2221  /* Find or create a tabstat entry for the rel */
2222  pgstat_info = get_tabstat_entry(rec->t_id, rec->t_shared);
2223 
2224  /* Same math as in AtEOXact_PgStat, commit case */
2225  pgstat_info->t_counts.t_tuples_inserted += rec->tuples_inserted;
2226  pgstat_info->t_counts.t_tuples_updated += rec->tuples_updated;
2227  pgstat_info->t_counts.t_tuples_deleted += rec->tuples_deleted;
2228  pgstat_info->t_counts.t_truncated = rec->t_truncated;
2229  if (rec->t_truncated)
2230  {
2231  /* forget live/dead stats seen by backend thus far */
2232  pgstat_info->t_counts.t_delta_live_tuples = 0;
2233  pgstat_info->t_counts.t_delta_dead_tuples = 0;
2234  }
2235  pgstat_info->t_counts.t_delta_live_tuples +=
2236  rec->tuples_inserted - rec->tuples_deleted;
2237  pgstat_info->t_counts.t_delta_dead_tuples +=
2238  rec->tuples_updated + rec->tuples_deleted;
2239  pgstat_info->t_counts.t_changed_tuples +=
2240  rec->tuples_inserted + rec->tuples_updated +
2241  rec->tuples_deleted;
2242 }
PgStat_Counter t_delta_live_tuples
Definition: pgstat.h:110
PgStat_TableCounts t_counts
Definition: pgstat.h:158
PgStat_Counter t_tuples_updated
Definition: pgstat.h:105
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:111
PgStat_Counter tuples_updated
Definition: pgstat.c:200
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:106
static PgStat_TableStatus * get_tabstat_entry(Oid rel_id, bool isshared)
Definition: pgstat.c:1673
PgStat_Counter t_changed_tuples
Definition: pgstat.h:112
PgStat_Counter tuples_inserted
Definition: pgstat.c:199
PgStat_Counter tuples_deleted
Definition: pgstat.c:201
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:104
void pgstat_update_heap_dead_tuples ( Relation  rel,
int  delta 
)

Definition at line 1935 of file pgstat.c.

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

Referenced by heap_page_prune().

1936 {
1937  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1938 
1939  if (pgstat_info != NULL)
1940  pgstat_info->t_counts.t_delta_dead_tuples -= delta;
1941 }
PgStat_TableCounts t_counts
Definition: pgstat.h:158
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:111
#define NULL
Definition: c.h:226
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:214
void pgstat_vacuum_stat ( void  )

Definition at line 946 of file pgstat.c.

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

Referenced by do_autovacuum(), and vacuum().

947 {
948  HTAB *htab;
949  PgStat_MsgTabpurge msg;
950  PgStat_MsgFuncpurge f_msg;
951  HASH_SEQ_STATUS hstat;
952  PgStat_StatDBEntry *dbentry;
953  PgStat_StatTabEntry *tabentry;
954  PgStat_StatFuncEntry *funcentry;
955  int len;
956 
958  return;
959 
960  /*
961  * If not done for this transaction, read the statistics collector stats
962  * file into some hash tables.
963  */
965 
966  /*
967  * Read pg_database and make a list of OIDs of all existing databases
968  */
970 
971  /*
972  * Search the database hash table for dead databases and tell the
973  * collector to drop them.
974  */
975  hash_seq_init(&hstat, pgStatDBHash);
976  while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
977  {
978  Oid dbid = dbentry->databaseid;
979 
981 
982  /* the DB entry for shared tables (with InvalidOid) is never dropped */
983  if (OidIsValid(dbid) &&
984  hash_search(htab, (void *) &dbid, HASH_FIND, NULL) == NULL)
985  pgstat_drop_database(dbid);
986  }
987 
988  /* Clean up */
989  hash_destroy(htab);
990 
991  /*
992  * Lookup our own database entry; if not found, nothing more to do.
993  */
995  (void *) &MyDatabaseId,
996  HASH_FIND, NULL);
997  if (dbentry == NULL || dbentry->tables == NULL)
998  return;
999 
1000  /*
1001  * Similarly to above, make a list of all known relations in this DB.
1002  */
1004 
1005  /*
1006  * Initialize our messages table counter to zero
1007  */
1008  msg.m_nentries = 0;
1009 
1010  /*
1011  * Check for all tables listed in stats hashtable if they still exist.
1012  */
1013  hash_seq_init(&hstat, dbentry->tables);
1014  while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&hstat)) != NULL)
1015  {
1016  Oid tabid = tabentry->tableid;
1017 
1019 
1020  if (hash_search(htab, (void *) &tabid, HASH_FIND, NULL) != NULL)
1021  continue;
1022 
1023  /*
1024  * Not there, so add this table's Oid to the message
1025  */
1026  msg.m_tableid[msg.m_nentries++] = tabid;
1027 
1028  /*
1029  * If the message is full, send it out and reinitialize to empty
1030  */
1031  if (msg.m_nentries >= PGSTAT_NUM_TABPURGE)
1032  {
1033  len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
1034  +msg.m_nentries * sizeof(Oid);
1035 
1037  msg.m_databaseid = MyDatabaseId;
1038  pgstat_send(&msg, len);
1039 
1040  msg.m_nentries = 0;
1041  }
1042  }
1043 
1044  /*
1045  * Send the rest
1046  */
1047  if (msg.m_nentries > 0)
1048  {
1049  len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
1050  +msg.m_nentries * sizeof(Oid);
1051 
1053  msg.m_databaseid = MyDatabaseId;
1054  pgstat_send(&msg, len);
1055  }
1056 
1057  /* Clean up */
1058  hash_destroy(htab);
1059 
1060  /*
1061  * Now repeat the above steps for functions. However, we needn't bother
1062  * in the common case where no function stats are being collected.
1063  */
1064  if (dbentry->functions != NULL &&
1065  hash_get_num_entries(dbentry->functions) > 0)
1066  {
1068 
1070  f_msg.m_databaseid = MyDatabaseId;
1071  f_msg.m_nentries = 0;
1072 
1073  hash_seq_init(&hstat, dbentry->functions);
1074  while ((funcentry = (PgStat_StatFuncEntry *) hash_seq_search(&hstat)) != NULL)
1075  {
1076  Oid funcid = funcentry->functionid;
1077 
1079 
1080  if (hash_search(htab, (void *) &funcid, HASH_FIND, NULL) != NULL)
1081  continue;
1082 
1083  /*
1084  * Not there, so add this function's Oid to the message
1085  */
1086  f_msg.m_functionid[f_msg.m_nentries++] = funcid;
1087 
1088  /*
1089  * If the message is full, send it out and reinitialize to empty
1090  */
1091  if (f_msg.m_nentries >= PGSTAT_NUM_FUNCPURGE)
1092  {
1093  len = offsetof(PgStat_MsgFuncpurge, m_functionid[0])
1094  +f_msg.m_nentries * sizeof(Oid);
1095 
1096  pgstat_send(&f_msg, len);
1097 
1098  f_msg.m_nentries = 0;
1099  }
1100  }
1101 
1102  /*
1103  * Send the rest
1104  */
1105  if (f_msg.m_nentries > 0)
1106  {
1107  len = offsetof(PgStat_MsgFuncpurge, m_functionid[0])
1108  +f_msg.m_nentries * sizeof(Oid);
1109 
1110  pgstat_send(&f_msg, len);
1111  }
1112 
1113  hash_destroy(htab);
1114  }
1115 }
static void pgstat_send(void *msg, int len)
Definition: pgstat.c:3617
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:793
NON_EXEC_STATIC pgsocket pgStatSock
Definition: pgstat.c:133
#define PGSTAT_NUM_TABPURGE
Definition: pgstat.h:285
#define ProcedureRelationId
Definition: pg_proc.h:33
#define DatabaseRelationId
Definition: pg_database.h:29
#define RelationRelationId
Definition: pg_class.h:29
PgStat_MsgHdr m_hdr
Definition: pgstat.h:516
HTAB * functions
Definition: pgstat.h:607
long hash_get_num_entries(HTAB *hashp)
Definition: dynahash.c:1297
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:534
Definition: dynahash.c:193
Oid m_tableid[PGSTAT_NUM_TABPURGE]
Definition: pgstat.h:294
static void backend_read_statsfile(void)
Definition: pgstat.c:4891
void pgstat_drop_database(Oid databaseid)
Definition: pgstat.c:1174
PgStat_MsgHdr m_hdr
Definition: pgstat.h:291
#define PGINVALID_SOCKET
Definition: port.h:24
static HTAB * pgstat_collect_oids(Oid catalogid)
Definition: pgstat.c:1128
Oid MyDatabaseId
Definition: globals.c:76
#define NULL
Definition: c.h:226
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
Definition: pgstat.c:3604
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1353
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1343
static HTAB * pgStatDBHash
Definition: pgstat.c:214
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define offsetof(type, field)
Definition: c.h:551
#define PGSTAT_NUM_FUNCPURGE
Definition: pgstat.h:510
Oid m_functionid[PGSTAT_NUM_FUNCPURGE]
Definition: pgstat.h:519
void PostPrepare_PgStat ( void  )

Definition at line 2182 of file pgstat.c.

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

Referenced by PrepareTransaction().

2183 {
2184  PgStat_SubXactStatus *xact_state;
2185 
2186  /*
2187  * We don't bother to free any of the transactional state, since it's all
2188  * in TopTransactionContext and will go away anyway.
2189  */
2190  xact_state = pgStatXactStack;
2191  if (xact_state != NULL)
2192  {
2194 
2195  for (trans = xact_state->first; trans != NULL; trans = trans->next)
2196  {
2197  PgStat_TableStatus *tabstat;
2198 
2199  tabstat = trans->parent;
2200  tabstat->trans = NULL;
2201  }
2202  }
2204 
2205  /* Make sure any stats snapshot is thrown away */
2207 }
struct PgStat_TableXactStatus * next
Definition: pgstat.h:179
#define NULL
Definition: c.h:226
void pgstat_clear_snapshot(void)
Definition: pgstat.c:5045
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat.c:189
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:157
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:383
PgStat_TableXactStatus * first
Definition: pgstat.c:186
PgStat_TableStatus * parent
Definition: pgstat.h:177

Variable Documentation

char* pgstat_stat_directory
char* pgstat_stat_filename
char* pgstat_stat_tmpname

Definition at line 120 of file pgstat.c.

Referenced by assign_pgstat_temp_directory(), and pgstat_write_statsfiles().

int pgstat_track_functions

Definition at line 111 of file pgstat.c.

PgStat_Counter pgStatBlockReadTime

Definition at line 193 of file pgstat.c.

Referenced by pgstat_send_tabstat().

PgStat_Counter pgStatBlockWriteTime

Definition at line 194 of file pgstat.c.

Referenced by pgstat_send_tabstat().