LCOV - code coverage report
Current view: top level - src/include - pgstat.h (source / functions) Hit Total Coverage
Test: PostgreSQL Lines: 10 10 100.0 %
Date: 2017-09-29 15:12:54 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* ----------
       2             :  *  pgstat.h
       3             :  *
       4             :  *  Definitions for the PostgreSQL statistics collector daemon.
       5             :  *
       6             :  *  Copyright (c) 2001-2017, PostgreSQL Global Development Group
       7             :  *
       8             :  *  src/include/pgstat.h
       9             :  * ----------
      10             :  */
      11             : #ifndef PGSTAT_H
      12             : #define PGSTAT_H
      13             : 
      14             : #include "datatype/timestamp.h"
      15             : #include "fmgr.h"
      16             : #include "libpq/pqcomm.h"
      17             : #include "port/atomics.h"
      18             : #include "portability/instr_time.h"
      19             : #include "postmaster/pgarch.h"
      20             : #include "storage/proc.h"
      21             : #include "utils/hsearch.h"
      22             : #include "utils/relcache.h"
      23             : 
      24             : 
      25             : /* ----------
      26             :  * Paths for the statistics files (relative to installation's $PGDATA).
      27             :  * ----------
      28             :  */
      29             : #define PGSTAT_STAT_PERMANENT_DIRECTORY     "pg_stat"
      30             : #define PGSTAT_STAT_PERMANENT_FILENAME      "pg_stat/global.stat"
      31             : #define PGSTAT_STAT_PERMANENT_TMPFILE       "pg_stat/global.tmp"
      32             : 
      33             : /* Default directory to store temporary statistics data in */
      34             : #define PG_STAT_TMP_DIR     "pg_stat_tmp"
      35             : 
      36             : /* Values for track_functions GUC variable --- order is significant! */
      37             : typedef enum TrackFunctionsLevel
      38             : {
      39             :     TRACK_FUNC_OFF,
      40             :     TRACK_FUNC_PL,
      41             :     TRACK_FUNC_ALL
      42             : }           TrackFunctionsLevel;
      43             : 
      44             : /* ----------
      45             :  * The types of backend -> collector messages
      46             :  * ----------
      47             :  */
      48             : typedef enum StatMsgType
      49             : {
      50             :     PGSTAT_MTYPE_DUMMY,
      51             :     PGSTAT_MTYPE_INQUIRY,
      52             :     PGSTAT_MTYPE_TABSTAT,
      53             :     PGSTAT_MTYPE_TABPURGE,
      54             :     PGSTAT_MTYPE_DROPDB,
      55             :     PGSTAT_MTYPE_RESETCOUNTER,
      56             :     PGSTAT_MTYPE_RESETSHAREDCOUNTER,
      57             :     PGSTAT_MTYPE_RESETSINGLECOUNTER,
      58             :     PGSTAT_MTYPE_AUTOVAC_START,
      59             :     PGSTAT_MTYPE_VACUUM,
      60             :     PGSTAT_MTYPE_ANALYZE,
      61             :     PGSTAT_MTYPE_ARCHIVER,
      62             :     PGSTAT_MTYPE_BGWRITER,
      63             :     PGSTAT_MTYPE_FUNCSTAT,
      64             :     PGSTAT_MTYPE_FUNCPURGE,
      65             :     PGSTAT_MTYPE_RECOVERYCONFLICT,
      66             :     PGSTAT_MTYPE_TEMPFILE,
      67             :     PGSTAT_MTYPE_DEADLOCK
      68             : } StatMsgType;
      69             : 
      70             : /* ----------
      71             :  * The data type used for counters.
      72             :  * ----------
      73             :  */
      74             : typedef int64 PgStat_Counter;
      75             : 
      76             : /* ----------
      77             :  * PgStat_TableCounts           The actual per-table counts kept by a backend
      78             :  *
      79             :  * This struct should contain only actual event counters, because we memcmp
      80             :  * it against zeroes to detect whether there are any counts to transmit.
      81             :  * It is a component of PgStat_TableStatus (within-backend state) and
      82             :  * PgStat_TableEntry (the transmitted message format).
      83             :  *
      84             :  * Note: for a table, tuples_returned is the number of tuples successfully
      85             :  * fetched by heap_getnext, while tuples_fetched is the number of tuples
      86             :  * successfully fetched by heap_fetch under the control of bitmap indexscans.
      87             :  * For an index, tuples_returned is the number of index entries returned by
      88             :  * the index AM, while tuples_fetched is the number of tuples successfully
      89             :  * fetched by heap_fetch under the control of simple indexscans for this index.
      90             :  *
      91             :  * tuples_inserted/updated/deleted/hot_updated count attempted actions,
      92             :  * regardless of whether the transaction committed.  delta_live_tuples,
      93             :  * delta_dead_tuples, and changed_tuples are set depending on commit or abort.
      94             :  * Note that delta_live_tuples and delta_dead_tuples can be negative!
      95             :  * ----------
      96             :  */
      97             : typedef struct PgStat_TableCounts
      98             : {
      99             :     PgStat_Counter t_numscans;
     100             : 
     101             :     PgStat_Counter t_tuples_returned;
     102             :     PgStat_Counter t_tuples_fetched;
     103             : 
     104             :     PgStat_Counter t_tuples_inserted;
     105             :     PgStat_Counter t_tuples_updated;
     106             :     PgStat_Counter t_tuples_deleted;
     107             :     PgStat_Counter t_tuples_hot_updated;
     108             :     bool        t_truncated;
     109             : 
     110             :     PgStat_Counter t_delta_live_tuples;
     111             :     PgStat_Counter t_delta_dead_tuples;
     112             :     PgStat_Counter t_changed_tuples;
     113             : 
     114             :     PgStat_Counter t_blocks_fetched;
     115             :     PgStat_Counter t_blocks_hit;
     116             : } PgStat_TableCounts;
     117             : 
     118             : /* Possible targets for resetting cluster-wide shared values */
     119             : typedef enum PgStat_Shared_Reset_Target
     120             : {
     121             :     RESET_ARCHIVER,
     122             :     RESET_BGWRITER
     123             : } PgStat_Shared_Reset_Target;
     124             : 
     125             : /* Possible object types for resetting single counters */
     126             : typedef enum PgStat_Single_Reset_Type
     127             : {
     128             :     RESET_TABLE,
     129             :     RESET_FUNCTION
     130             : } PgStat_Single_Reset_Type;
     131             : 
     132             : /* ------------------------------------------------------------
     133             :  * Structures kept in backend local memory while accumulating counts
     134             :  * ------------------------------------------------------------
     135             :  */
     136             : 
     137             : 
     138             : /* ----------
     139             :  * PgStat_TableStatus           Per-table status within a backend
     140             :  *
     141             :  * Many of the event counters are nontransactional, ie, we count events
     142             :  * in committed and aborted transactions alike.  For these, we just count
     143             :  * directly in the PgStat_TableStatus.  However, delta_live_tuples,
     144             :  * delta_dead_tuples, and changed_tuples must be derived from event counts
     145             :  * with awareness of whether the transaction or subtransaction committed or
     146             :  * aborted.  Hence, we also keep a stack of per-(sub)transaction status
     147             :  * records for every table modified in the current transaction.  At commit
     148             :  * or abort, we propagate tuples_inserted/updated/deleted up to the
     149             :  * parent subtransaction level, or out to the parent PgStat_TableStatus,
     150             :  * as appropriate.
     151             :  * ----------
     152             :  */
     153             : typedef struct PgStat_TableStatus
     154             : {
     155             :     Oid         t_id;           /* table's OID */
     156             :     bool        t_shared;       /* is it a shared catalog? */
     157             :     struct PgStat_TableXactStatus *trans;   /* lowest subxact's counts */
     158             :     PgStat_TableCounts t_counts;    /* event counts to be sent */
     159             : } PgStat_TableStatus;
     160             : 
     161             : /* ----------
     162             :  * PgStat_TableXactStatus       Per-table, per-subtransaction status
     163             :  * ----------
     164             :  */
     165             : typedef struct PgStat_TableXactStatus
     166             : {
     167             :     PgStat_Counter tuples_inserted; /* tuples inserted in (sub)xact */
     168             :     PgStat_Counter tuples_updated;  /* tuples updated in (sub)xact */
     169             :     PgStat_Counter tuples_deleted;  /* tuples deleted in (sub)xact */
     170             :     bool        truncated;      /* relation truncated in this (sub)xact */
     171             :     PgStat_Counter inserted_pre_trunc;  /* tuples inserted prior to truncate */
     172             :     PgStat_Counter updated_pre_trunc;   /* tuples updated prior to truncate */
     173             :     PgStat_Counter deleted_pre_trunc;   /* tuples deleted prior to truncate */
     174             :     int         nest_level;     /* subtransaction nest level */
     175             :     /* links to other structs for same relation: */
     176             :     struct PgStat_TableXactStatus *upper;   /* next higher subxact if any */
     177             :     PgStat_TableStatus *parent; /* per-table status */
     178             :     /* structs of same subxact level are linked here: */
     179             :     struct PgStat_TableXactStatus *next;    /* next of same subxact */
     180             : } PgStat_TableXactStatus;
     181             : 
     182             : 
     183             : /* ------------------------------------------------------------
     184             :  * Message formats follow
     185             :  * ------------------------------------------------------------
     186             :  */
     187             : 
     188             : 
     189             : /* ----------
     190             :  * PgStat_MsgHdr                The common message header
     191             :  * ----------
     192             :  */
     193             : typedef struct PgStat_MsgHdr
     194             : {
     195             :     StatMsgType m_type;
     196             :     int         m_size;
     197             : } PgStat_MsgHdr;
     198             : 
     199             : /* ----------
     200             :  * Space available in a message.  This will keep the UDP packets below 1K,
     201             :  * which should fit unfragmented into the MTU of the loopback interface.
     202             :  * (Larger values of PGSTAT_MAX_MSG_SIZE would work for that on most
     203             :  * platforms, but we're being conservative here.)
     204             :  * ----------
     205             :  */
     206             : #define PGSTAT_MAX_MSG_SIZE 1000
     207             : #define PGSTAT_MSG_PAYLOAD  (PGSTAT_MAX_MSG_SIZE - sizeof(PgStat_MsgHdr))
     208             : 
     209             : 
     210             : /* ----------
     211             :  * PgStat_MsgDummy              A dummy message, ignored by the collector
     212             :  * ----------
     213             :  */
     214             : typedef struct PgStat_MsgDummy
     215             : {
     216             :     PgStat_MsgHdr m_hdr;
     217             : } PgStat_MsgDummy;
     218             : 
     219             : 
     220             : /* ----------
     221             :  * PgStat_MsgInquiry            Sent by a backend to ask the collector
     222             :  *                              to write the stats file(s).
     223             :  *
     224             :  * Ordinarily, an inquiry message prompts writing of the global stats file,
     225             :  * the stats file for shared catalogs, and the stats file for the specified
     226             :  * database.  If databaseid is InvalidOid, only the first two are written.
     227             :  *
     228             :  * New file(s) will be written only if the existing file has a timestamp
     229             :  * older than the specified cutoff_time; this prevents duplicated effort
     230             :  * when multiple requests arrive at nearly the same time, assuming that
     231             :  * backends send requests with cutoff_times a little bit in the past.
     232             :  *
     233             :  * clock_time should be the requestor's current local time; the collector
     234             :  * uses this to check for the system clock going backward, but it has no
     235             :  * effect unless that occurs.  We assume clock_time >= cutoff_time, though.
     236             :  * ----------
     237             :  */
     238             : 
     239             : typedef struct PgStat_MsgInquiry
     240             : {
     241             :     PgStat_MsgHdr m_hdr;
     242             :     TimestampTz clock_time;     /* observed local clock time */
     243             :     TimestampTz cutoff_time;    /* minimum acceptable file timestamp */
     244             :     Oid         databaseid;     /* requested DB (InvalidOid => shared only) */
     245             : } PgStat_MsgInquiry;
     246             : 
     247             : 
     248             : /* ----------
     249             :  * PgStat_TableEntry            Per-table info in a MsgTabstat
     250             :  * ----------
     251             :  */
     252             : typedef struct PgStat_TableEntry
     253             : {
     254             :     Oid         t_id;
     255             :     PgStat_TableCounts t_counts;
     256             : } PgStat_TableEntry;
     257             : 
     258             : /* ----------
     259             :  * PgStat_MsgTabstat            Sent by the backend to report table
     260             :  *                              and buffer access statistics.
     261             :  * ----------
     262             :  */
     263             : #define PGSTAT_NUM_TABENTRIES  \
     264             :     ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - 3 * sizeof(int) - 2 * sizeof(PgStat_Counter))  \
     265             :      / sizeof(PgStat_TableEntry))
     266             : 
     267             : typedef struct PgStat_MsgTabstat
     268             : {
     269             :     PgStat_MsgHdr m_hdr;
     270             :     Oid         m_databaseid;
     271             :     int         m_nentries;
     272             :     int         m_xact_commit;
     273             :     int         m_xact_rollback;
     274             :     PgStat_Counter m_block_read_time;   /* times in microseconds */
     275             :     PgStat_Counter m_block_write_time;
     276             :     PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES];
     277             : } PgStat_MsgTabstat;
     278             : 
     279             : 
     280             : /* ----------
     281             :  * PgStat_MsgTabpurge           Sent by the backend to tell the collector
     282             :  *                              about dead tables.
     283             :  * ----------
     284             :  */
     285             : #define PGSTAT_NUM_TABPURGE  \
     286             :     ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int))  \
     287             :      / sizeof(Oid))
     288             : 
     289             : typedef struct PgStat_MsgTabpurge
     290             : {
     291             :     PgStat_MsgHdr m_hdr;
     292             :     Oid         m_databaseid;
     293             :     int         m_nentries;
     294             :     Oid         m_tableid[PGSTAT_NUM_TABPURGE];
     295             : } PgStat_MsgTabpurge;
     296             : 
     297             : 
     298             : /* ----------
     299             :  * PgStat_MsgDropdb             Sent by the backend to tell the collector
     300             :  *                              about a dropped database
     301             :  * ----------
     302             :  */
     303             : typedef struct PgStat_MsgDropdb
     304             : {
     305             :     PgStat_MsgHdr m_hdr;
     306             :     Oid         m_databaseid;
     307             : } PgStat_MsgDropdb;
     308             : 
     309             : 
     310             : /* ----------
     311             :  * PgStat_MsgResetcounter       Sent by the backend to tell the collector
     312             :  *                              to reset counters
     313             :  * ----------
     314             :  */
     315             : typedef struct PgStat_MsgResetcounter
     316             : {
     317             :     PgStat_MsgHdr m_hdr;
     318             :     Oid         m_databaseid;
     319             : } PgStat_MsgResetcounter;
     320             : 
     321             : /* ----------
     322             :  * PgStat_MsgResetsharedcounter Sent by the backend to tell the collector
     323             :  *                              to reset a shared counter
     324             :  * ----------
     325             :  */
     326             : typedef struct PgStat_MsgResetsharedcounter
     327             : {
     328             :     PgStat_MsgHdr m_hdr;
     329             :     PgStat_Shared_Reset_Target m_resettarget;
     330             : } PgStat_MsgResetsharedcounter;
     331             : 
     332             : /* ----------
     333             :  * PgStat_MsgResetsinglecounter Sent by the backend to tell the collector
     334             :  *                              to reset a single counter
     335             :  * ----------
     336             :  */
     337             : typedef struct PgStat_MsgResetsinglecounter
     338             : {
     339             :     PgStat_MsgHdr m_hdr;
     340             :     Oid         m_databaseid;
     341             :     PgStat_Single_Reset_Type m_resettype;
     342             :     Oid         m_objectid;
     343             : } PgStat_MsgResetsinglecounter;
     344             : 
     345             : /* ----------
     346             :  * PgStat_MsgAutovacStart       Sent by the autovacuum daemon to signal
     347             :  *                              that a database is going to be processed
     348             :  * ----------
     349             :  */
     350             : typedef struct PgStat_MsgAutovacStart
     351             : {
     352             :     PgStat_MsgHdr m_hdr;
     353             :     Oid         m_databaseid;
     354             :     TimestampTz m_start_time;
     355             : } PgStat_MsgAutovacStart;
     356             : 
     357             : 
     358             : /* ----------
     359             :  * PgStat_MsgVacuum             Sent by the backend or autovacuum daemon
     360             :  *                              after VACUUM
     361             :  * ----------
     362             :  */
     363             : typedef struct PgStat_MsgVacuum
     364             : {
     365             :     PgStat_MsgHdr m_hdr;
     366             :     Oid         m_databaseid;
     367             :     Oid         m_tableoid;
     368             :     bool        m_autovacuum;
     369             :     TimestampTz m_vacuumtime;
     370             :     PgStat_Counter m_live_tuples;
     371             :     PgStat_Counter m_dead_tuples;
     372             : } PgStat_MsgVacuum;
     373             : 
     374             : 
     375             : /* ----------
     376             :  * PgStat_MsgAnalyze            Sent by the backend or autovacuum daemon
     377             :  *                              after ANALYZE
     378             :  * ----------
     379             :  */
     380             : typedef struct PgStat_MsgAnalyze
     381             : {
     382             :     PgStat_MsgHdr m_hdr;
     383             :     Oid         m_databaseid;
     384             :     Oid         m_tableoid;
     385             :     bool        m_autovacuum;
     386             :     bool        m_resetcounter;
     387             :     TimestampTz m_analyzetime;
     388             :     PgStat_Counter m_live_tuples;
     389             :     PgStat_Counter m_dead_tuples;
     390             : } PgStat_MsgAnalyze;
     391             : 
     392             : 
     393             : /* ----------
     394             :  * PgStat_MsgArchiver           Sent by the archiver to update statistics.
     395             :  * ----------
     396             :  */
     397             : typedef struct PgStat_MsgArchiver
     398             : {
     399             :     PgStat_MsgHdr m_hdr;
     400             :     bool        m_failed;       /* Failed attempt */
     401             :     char        m_xlog[MAX_XFN_CHARS + 1];
     402             :     TimestampTz m_timestamp;
     403             : } PgStat_MsgArchiver;
     404             : 
     405             : /* ----------
     406             :  * PgStat_MsgBgWriter           Sent by the bgwriter to update statistics.
     407             :  * ----------
     408             :  */
     409             : typedef struct PgStat_MsgBgWriter
     410             : {
     411             :     PgStat_MsgHdr m_hdr;
     412             : 
     413             :     PgStat_Counter m_timed_checkpoints;
     414             :     PgStat_Counter m_requested_checkpoints;
     415             :     PgStat_Counter m_buf_written_checkpoints;
     416             :     PgStat_Counter m_buf_written_clean;
     417             :     PgStat_Counter m_maxwritten_clean;
     418             :     PgStat_Counter m_buf_written_backend;
     419             :     PgStat_Counter m_buf_fsync_backend;
     420             :     PgStat_Counter m_buf_alloc;
     421             :     PgStat_Counter m_checkpoint_write_time; /* times in milliseconds */
     422             :     PgStat_Counter m_checkpoint_sync_time;
     423             : } PgStat_MsgBgWriter;
     424             : 
     425             : /* ----------
     426             :  * PgStat_MsgRecoveryConflict   Sent by the backend upon recovery conflict
     427             :  * ----------
     428             :  */
     429             : typedef struct PgStat_MsgRecoveryConflict
     430             : {
     431             :     PgStat_MsgHdr m_hdr;
     432             : 
     433             :     Oid         m_databaseid;
     434             :     int         m_reason;
     435             : } PgStat_MsgRecoveryConflict;
     436             : 
     437             : /* ----------
     438             :  * PgStat_MsgTempFile   Sent by the backend upon creating a temp file
     439             :  * ----------
     440             :  */
     441             : typedef struct PgStat_MsgTempFile
     442             : {
     443             :     PgStat_MsgHdr m_hdr;
     444             : 
     445             :     Oid         m_databaseid;
     446             :     size_t      m_filesize;
     447             : } PgStat_MsgTempFile;
     448             : 
     449             : /* ----------
     450             :  * PgStat_FunctionCounts    The actual per-function counts kept by a backend
     451             :  *
     452             :  * This struct should contain only actual event counters, because we memcmp
     453             :  * it against zeroes to detect whether there are any counts to transmit.
     454             :  *
     455             :  * Note that the time counters are in instr_time format here.  We convert to
     456             :  * microseconds in PgStat_Counter format when transmitting to the collector.
     457             :  * ----------
     458             :  */
     459             : typedef struct PgStat_FunctionCounts
     460             : {
     461             :     PgStat_Counter f_numcalls;
     462             :     instr_time  f_total_time;
     463             :     instr_time  f_self_time;
     464             : } PgStat_FunctionCounts;
     465             : 
     466             : /* ----------
     467             :  * PgStat_BackendFunctionEntry  Entry in backend's per-function hash table
     468             :  * ----------
     469             :  */
     470             : typedef struct PgStat_BackendFunctionEntry
     471             : {
     472             :     Oid         f_id;
     473             :     PgStat_FunctionCounts f_counts;
     474             : } PgStat_BackendFunctionEntry;
     475             : 
     476             : /* ----------
     477             :  * PgStat_FunctionEntry         Per-function info in a MsgFuncstat
     478             :  * ----------
     479             :  */
     480             : typedef struct PgStat_FunctionEntry
     481             : {
     482             :     Oid         f_id;
     483             :     PgStat_Counter f_numcalls;
     484             :     PgStat_Counter f_total_time;    /* times in microseconds */
     485             :     PgStat_Counter f_self_time;
     486             : } PgStat_FunctionEntry;
     487             : 
     488             : /* ----------
     489             :  * PgStat_MsgFuncstat           Sent by the backend to report function
     490             :  *                              usage statistics.
     491             :  * ----------
     492             :  */
     493             : #define PGSTAT_NUM_FUNCENTRIES  \
     494             :     ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int))  \
     495             :      / sizeof(PgStat_FunctionEntry))
     496             : 
     497             : typedef struct PgStat_MsgFuncstat
     498             : {
     499             :     PgStat_MsgHdr m_hdr;
     500             :     Oid         m_databaseid;
     501             :     int         m_nentries;
     502             :     PgStat_FunctionEntry m_entry[PGSTAT_NUM_FUNCENTRIES];
     503             : } PgStat_MsgFuncstat;
     504             : 
     505             : /* ----------
     506             :  * PgStat_MsgFuncpurge          Sent by the backend to tell the collector
     507             :  *                              about dead functions.
     508             :  * ----------
     509             :  */
     510             : #define PGSTAT_NUM_FUNCPURGE  \
     511             :     ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int))  \
     512             :      / sizeof(Oid))
     513             : 
     514             : typedef struct PgStat_MsgFuncpurge
     515             : {
     516             :     PgStat_MsgHdr m_hdr;
     517             :     Oid         m_databaseid;
     518             :     int         m_nentries;
     519             :     Oid         m_functionid[PGSTAT_NUM_FUNCPURGE];
     520             : } PgStat_MsgFuncpurge;
     521             : 
     522             : /* ----------
     523             :  * PgStat_MsgDeadlock           Sent by the backend to tell the collector
     524             :  *                              about a deadlock that occurred.
     525             :  * ----------
     526             :  */
     527             : typedef struct PgStat_MsgDeadlock
     528             : {
     529             :     PgStat_MsgHdr m_hdr;
     530             :     Oid         m_databaseid;
     531             : } PgStat_MsgDeadlock;
     532             : 
     533             : 
     534             : /* ----------
     535             :  * PgStat_Msg                   Union over all possible messages.
     536             :  * ----------
     537             :  */
     538             : typedef union PgStat_Msg
     539             : {
     540             :     PgStat_MsgHdr msg_hdr;
     541             :     PgStat_MsgDummy msg_dummy;
     542             :     PgStat_MsgInquiry msg_inquiry;
     543             :     PgStat_MsgTabstat msg_tabstat;
     544             :     PgStat_MsgTabpurge msg_tabpurge;
     545             :     PgStat_MsgDropdb msg_dropdb;
     546             :     PgStat_MsgResetcounter msg_resetcounter;
     547             :     PgStat_MsgResetsharedcounter msg_resetsharedcounter;
     548             :     PgStat_MsgResetsinglecounter msg_resetsinglecounter;
     549             :     PgStat_MsgAutovacStart msg_autovacuum;
     550             :     PgStat_MsgVacuum msg_vacuum;
     551             :     PgStat_MsgAnalyze msg_analyze;
     552             :     PgStat_MsgArchiver msg_archiver;
     553             :     PgStat_MsgBgWriter msg_bgwriter;
     554             :     PgStat_MsgFuncstat msg_funcstat;
     555             :     PgStat_MsgFuncpurge msg_funcpurge;
     556             :     PgStat_MsgRecoveryConflict msg_recoveryconflict;
     557             :     PgStat_MsgDeadlock msg_deadlock;
     558             : } PgStat_Msg;
     559             : 
     560             : 
     561             : /* ------------------------------------------------------------
     562             :  * Statistic collector data structures follow
     563             :  *
     564             :  * PGSTAT_FILE_FORMAT_ID should be changed whenever any of these
     565             :  * data structures change.
     566             :  * ------------------------------------------------------------
     567             :  */
     568             : 
     569             : #define PGSTAT_FILE_FORMAT_ID   0x01A5BC9D
     570             : 
     571             : /* ----------
     572             :  * PgStat_StatDBEntry           The collector's data per database
     573             :  * ----------
     574             :  */
     575             : typedef struct PgStat_StatDBEntry
     576             : {
     577             :     Oid         databaseid;
     578             :     PgStat_Counter n_xact_commit;
     579             :     PgStat_Counter n_xact_rollback;
     580             :     PgStat_Counter n_blocks_fetched;
     581             :     PgStat_Counter n_blocks_hit;
     582             :     PgStat_Counter n_tuples_returned;
     583             :     PgStat_Counter n_tuples_fetched;
     584             :     PgStat_Counter n_tuples_inserted;
     585             :     PgStat_Counter n_tuples_updated;
     586             :     PgStat_Counter n_tuples_deleted;
     587             :     TimestampTz last_autovac_time;
     588             :     PgStat_Counter n_conflict_tablespace;
     589             :     PgStat_Counter n_conflict_lock;
     590             :     PgStat_Counter n_conflict_snapshot;
     591             :     PgStat_Counter n_conflict_bufferpin;
     592             :     PgStat_Counter n_conflict_startup_deadlock;
     593             :     PgStat_Counter n_temp_files;
     594             :     PgStat_Counter n_temp_bytes;
     595             :     PgStat_Counter n_deadlocks;
     596             :     PgStat_Counter n_block_read_time;   /* times in microseconds */
     597             :     PgStat_Counter n_block_write_time;
     598             : 
     599             :     TimestampTz stat_reset_timestamp;
     600             :     TimestampTz stats_timestamp;    /* time of db stats file update */
     601             : 
     602             :     /*
     603             :      * tables and functions must be last in the struct, because we don't write
     604             :      * the pointers out to the stats file.
     605             :      */
     606             :     HTAB       *tables;
     607             :     HTAB       *functions;
     608             : } PgStat_StatDBEntry;
     609             : 
     610             : 
     611             : /* ----------
     612             :  * PgStat_StatTabEntry          The collector's data per table (or index)
     613             :  * ----------
     614             :  */
     615             : typedef struct PgStat_StatTabEntry
     616             : {
     617             :     Oid         tableid;
     618             : 
     619             :     PgStat_Counter numscans;
     620             : 
     621             :     PgStat_Counter tuples_returned;
     622             :     PgStat_Counter tuples_fetched;
     623             : 
     624             :     PgStat_Counter tuples_inserted;
     625             :     PgStat_Counter tuples_updated;
     626             :     PgStat_Counter tuples_deleted;
     627             :     PgStat_Counter tuples_hot_updated;
     628             : 
     629             :     PgStat_Counter n_live_tuples;
     630             :     PgStat_Counter n_dead_tuples;
     631             :     PgStat_Counter changes_since_analyze;
     632             : 
     633             :     PgStat_Counter blocks_fetched;
     634             :     PgStat_Counter blocks_hit;
     635             : 
     636             :     TimestampTz vacuum_timestamp;   /* user initiated vacuum */
     637             :     PgStat_Counter vacuum_count;
     638             :     TimestampTz autovac_vacuum_timestamp;   /* autovacuum initiated */
     639             :     PgStat_Counter autovac_vacuum_count;
     640             :     TimestampTz analyze_timestamp;  /* user initiated */
     641             :     PgStat_Counter analyze_count;
     642             :     TimestampTz autovac_analyze_timestamp;  /* autovacuum initiated */
     643             :     PgStat_Counter autovac_analyze_count;
     644             : } PgStat_StatTabEntry;
     645             : 
     646             : 
     647             : /* ----------
     648             :  * PgStat_StatFuncEntry         The collector's data per function
     649             :  * ----------
     650             :  */
     651             : typedef struct PgStat_StatFuncEntry
     652             : {
     653             :     Oid         functionid;
     654             : 
     655             :     PgStat_Counter f_numcalls;
     656             : 
     657             :     PgStat_Counter f_total_time;    /* times in microseconds */
     658             :     PgStat_Counter f_self_time;
     659             : } PgStat_StatFuncEntry;
     660             : 
     661             : 
     662             : /*
     663             :  * Archiver statistics kept in the stats collector
     664             :  */
     665             : typedef struct PgStat_ArchiverStats
     666             : {
     667             :     PgStat_Counter archived_count;  /* archival successes */
     668             :     char        last_archived_wal[MAX_XFN_CHARS + 1];   /* last WAL file
     669             :                                                          * archived */
     670             :     TimestampTz last_archived_timestamp;    /* last archival success time */
     671             :     PgStat_Counter failed_count;    /* failed archival attempts */
     672             :     char        last_failed_wal[MAX_XFN_CHARS + 1]; /* WAL file involved in
     673             :                                                      * last failure */
     674             :     TimestampTz last_failed_timestamp;  /* last archival failure time */
     675             :     TimestampTz stat_reset_timestamp;
     676             : } PgStat_ArchiverStats;
     677             : 
     678             : /*
     679             :  * Global statistics kept in the stats collector
     680             :  */
     681             : typedef struct PgStat_GlobalStats
     682             : {
     683             :     TimestampTz stats_timestamp;    /* time of stats file update */
     684             :     PgStat_Counter timed_checkpoints;
     685             :     PgStat_Counter requested_checkpoints;
     686             :     PgStat_Counter checkpoint_write_time;   /* times in milliseconds */
     687             :     PgStat_Counter checkpoint_sync_time;
     688             :     PgStat_Counter buf_written_checkpoints;
     689             :     PgStat_Counter buf_written_clean;
     690             :     PgStat_Counter maxwritten_clean;
     691             :     PgStat_Counter buf_written_backend;
     692             :     PgStat_Counter buf_fsync_backend;
     693             :     PgStat_Counter buf_alloc;
     694             :     TimestampTz stat_reset_timestamp;
     695             : } PgStat_GlobalStats;
     696             : 
     697             : 
     698             : /* ----------
     699             :  * Backend types
     700             :  * ----------
     701             :  */
     702             : typedef enum BackendType
     703             : {
     704             :     B_AUTOVAC_LAUNCHER,
     705             :     B_AUTOVAC_WORKER,
     706             :     B_BACKEND,
     707             :     B_BG_WORKER,
     708             :     B_BG_WRITER,
     709             :     B_CHECKPOINTER,
     710             :     B_STARTUP,
     711             :     B_WAL_RECEIVER,
     712             :     B_WAL_SENDER,
     713             :     B_WAL_WRITER
     714             : } BackendType;
     715             : 
     716             : 
     717             : /* ----------
     718             :  * Backend states
     719             :  * ----------
     720             :  */
     721             : typedef enum BackendState
     722             : {
     723             :     STATE_UNDEFINED,
     724             :     STATE_IDLE,
     725             :     STATE_RUNNING,
     726             :     STATE_IDLEINTRANSACTION,
     727             :     STATE_FASTPATH,
     728             :     STATE_IDLEINTRANSACTION_ABORTED,
     729             :     STATE_DISABLED
     730             : } BackendState;
     731             : 
     732             : 
     733             : /* ----------
     734             :  * Wait Classes
     735             :  * ----------
     736             :  */
     737             : #define PG_WAIT_LWLOCK              0x01000000U
     738             : #define PG_WAIT_LOCK                0x03000000U
     739             : #define PG_WAIT_BUFFER_PIN          0x04000000U
     740             : #define PG_WAIT_ACTIVITY            0x05000000U
     741             : #define PG_WAIT_CLIENT              0x06000000U
     742             : #define PG_WAIT_EXTENSION           0x07000000U
     743             : #define PG_WAIT_IPC                 0x08000000U
     744             : #define PG_WAIT_TIMEOUT             0x09000000U
     745             : #define PG_WAIT_IO                  0x0A000000U
     746             : 
     747             : /* ----------
     748             :  * Wait Events - Activity
     749             :  *
     750             :  * Use this category when a process is waiting because it has no work to do,
     751             :  * unless the "Client" or "Timeout" category describes the situation better.
     752             :  * Typically, this should only be used for background processes.
     753             :  * ----------
     754             :  */
     755             : typedef enum
     756             : {
     757             :     WAIT_EVENT_ARCHIVER_MAIN = PG_WAIT_ACTIVITY,
     758             :     WAIT_EVENT_AUTOVACUUM_MAIN,
     759             :     WAIT_EVENT_BGWRITER_HIBERNATE,
     760             :     WAIT_EVENT_BGWRITER_MAIN,
     761             :     WAIT_EVENT_CHECKPOINTER_MAIN,
     762             :     WAIT_EVENT_LOGICAL_LAUNCHER_MAIN,
     763             :     WAIT_EVENT_LOGICAL_APPLY_MAIN,
     764             :     WAIT_EVENT_PGSTAT_MAIN,
     765             :     WAIT_EVENT_RECOVERY_WAL_ALL,
     766             :     WAIT_EVENT_RECOVERY_WAL_STREAM,
     767             :     WAIT_EVENT_SYSLOGGER_MAIN,
     768             :     WAIT_EVENT_WAL_RECEIVER_MAIN,
     769             :     WAIT_EVENT_WAL_SENDER_MAIN,
     770             :     WAIT_EVENT_WAL_WRITER_MAIN
     771             : } WaitEventActivity;
     772             : 
     773             : /* ----------
     774             :  * Wait Events - Client
     775             :  *
     776             :  * Use this category when a process is waiting to send data to or receive data
     777             :  * from the frontend process to which it is connected.  This is never used for
     778             :  * a background process, which has no client connection.
     779             :  * ----------
     780             :  */
     781             : typedef enum
     782             : {
     783             :     WAIT_EVENT_CLIENT_READ = PG_WAIT_CLIENT,
     784             :     WAIT_EVENT_CLIENT_WRITE,
     785             :     WAIT_EVENT_LIBPQWALRECEIVER_CONNECT,
     786             :     WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE,
     787             :     WAIT_EVENT_SSL_OPEN_SERVER,
     788             :     WAIT_EVENT_WAL_RECEIVER_WAIT_START,
     789             :     WAIT_EVENT_WAL_SENDER_WAIT_WAL,
     790             :     WAIT_EVENT_WAL_SENDER_WRITE_DATA
     791             : } WaitEventClient;
     792             : 
     793             : /* ----------
     794             :  * Wait Events - IPC
     795             :  *
     796             :  * Use this category when a process cannot complete the work it is doing because
     797             :  * it is waiting for a notification from another process.
     798             :  * ----------
     799             :  */
     800             : typedef enum
     801             : {
     802             :     WAIT_EVENT_BGWORKER_SHUTDOWN = PG_WAIT_IPC,
     803             :     WAIT_EVENT_BGWORKER_STARTUP,
     804             :     WAIT_EVENT_BTREE_PAGE,
     805             :     WAIT_EVENT_EXECUTE_GATHER,
     806             :     WAIT_EVENT_LOGICAL_SYNC_DATA,
     807             :     WAIT_EVENT_LOGICAL_SYNC_STATE_CHANGE,
     808             :     WAIT_EVENT_MQ_INTERNAL,
     809             :     WAIT_EVENT_MQ_PUT_MESSAGE,
     810             :     WAIT_EVENT_MQ_RECEIVE,
     811             :     WAIT_EVENT_MQ_SEND,
     812             :     WAIT_EVENT_PARALLEL_FINISH,
     813             :     WAIT_EVENT_PARALLEL_BITMAP_SCAN,
     814             :     WAIT_EVENT_PROCARRAY_GROUP_UPDATE,
     815             :     WAIT_EVENT_CLOG_GROUP_UPDATE,
     816             :     WAIT_EVENT_REPLICATION_ORIGIN_DROP,
     817             :     WAIT_EVENT_REPLICATION_SLOT_DROP,
     818             :     WAIT_EVENT_SAFE_SNAPSHOT,
     819             :     WAIT_EVENT_SYNC_REP
     820             : } WaitEventIPC;
     821             : 
     822             : /* ----------
     823             :  * Wait Events - Timeout
     824             :  *
     825             :  * Use this category when a process is waiting for a timeout to expire.
     826             :  * ----------
     827             :  */
     828             : typedef enum
     829             : {
     830             :     WAIT_EVENT_BASE_BACKUP_THROTTLE = PG_WAIT_TIMEOUT,
     831             :     WAIT_EVENT_PG_SLEEP,
     832             :     WAIT_EVENT_RECOVERY_APPLY_DELAY
     833             : } WaitEventTimeout;
     834             : 
     835             : /* ----------
     836             :  * Wait Events - IO
     837             :  *
     838             :  * Use this category when a process is waiting for a IO.
     839             :  * ----------
     840             :  */
     841             : typedef enum
     842             : {
     843             :     WAIT_EVENT_BUFFILE_READ = PG_WAIT_IO,
     844             :     WAIT_EVENT_BUFFILE_WRITE,
     845             :     WAIT_EVENT_CONTROL_FILE_READ,
     846             :     WAIT_EVENT_CONTROL_FILE_SYNC,
     847             :     WAIT_EVENT_CONTROL_FILE_SYNC_UPDATE,
     848             :     WAIT_EVENT_CONTROL_FILE_WRITE,
     849             :     WAIT_EVENT_CONTROL_FILE_WRITE_UPDATE,
     850             :     WAIT_EVENT_COPY_FILE_READ,
     851             :     WAIT_EVENT_COPY_FILE_WRITE,
     852             :     WAIT_EVENT_DATA_FILE_EXTEND,
     853             :     WAIT_EVENT_DATA_FILE_FLUSH,
     854             :     WAIT_EVENT_DATA_FILE_IMMEDIATE_SYNC,
     855             :     WAIT_EVENT_DATA_FILE_PREFETCH,
     856             :     WAIT_EVENT_DATA_FILE_READ,
     857             :     WAIT_EVENT_DATA_FILE_SYNC,
     858             :     WAIT_EVENT_DATA_FILE_TRUNCATE,
     859             :     WAIT_EVENT_DATA_FILE_WRITE,
     860             :     WAIT_EVENT_DSM_FILL_ZERO_WRITE,
     861             :     WAIT_EVENT_LOCK_FILE_ADDTODATADIR_READ,
     862             :     WAIT_EVENT_LOCK_FILE_ADDTODATADIR_SYNC,
     863             :     WAIT_EVENT_LOCK_FILE_ADDTODATADIR_WRITE,
     864             :     WAIT_EVENT_LOCK_FILE_CREATE_READ,
     865             :     WAIT_EVENT_LOCK_FILE_CREATE_SYNC,
     866             :     WAIT_EVENT_LOCK_FILE_CREATE_WRITE,
     867             :     WAIT_EVENT_LOCK_FILE_RECHECKDATADIR_READ,
     868             :     WAIT_EVENT_LOGICAL_REWRITE_CHECKPOINT_SYNC,
     869             :     WAIT_EVENT_LOGICAL_REWRITE_MAPPING_SYNC,
     870             :     WAIT_EVENT_LOGICAL_REWRITE_MAPPING_WRITE,
     871             :     WAIT_EVENT_LOGICAL_REWRITE_SYNC,
     872             :     WAIT_EVENT_LOGICAL_REWRITE_TRUNCATE,
     873             :     WAIT_EVENT_LOGICAL_REWRITE_WRITE,
     874             :     WAIT_EVENT_RELATION_MAP_READ,
     875             :     WAIT_EVENT_RELATION_MAP_SYNC,
     876             :     WAIT_EVENT_RELATION_MAP_WRITE,
     877             :     WAIT_EVENT_REORDER_BUFFER_READ,
     878             :     WAIT_EVENT_REORDER_BUFFER_WRITE,
     879             :     WAIT_EVENT_REORDER_LOGICAL_MAPPING_READ,
     880             :     WAIT_EVENT_REPLICATION_SLOT_READ,
     881             :     WAIT_EVENT_REPLICATION_SLOT_RESTORE_SYNC,
     882             :     WAIT_EVENT_REPLICATION_SLOT_SYNC,
     883             :     WAIT_EVENT_REPLICATION_SLOT_WRITE,
     884             :     WAIT_EVENT_SLRU_FLUSH_SYNC,
     885             :     WAIT_EVENT_SLRU_READ,
     886             :     WAIT_EVENT_SLRU_SYNC,
     887             :     WAIT_EVENT_SLRU_WRITE,
     888             :     WAIT_EVENT_SNAPBUILD_READ,
     889             :     WAIT_EVENT_SNAPBUILD_SYNC,
     890             :     WAIT_EVENT_SNAPBUILD_WRITE,
     891             :     WAIT_EVENT_TIMELINE_HISTORY_FILE_SYNC,
     892             :     WAIT_EVENT_TIMELINE_HISTORY_FILE_WRITE,
     893             :     WAIT_EVENT_TIMELINE_HISTORY_READ,
     894             :     WAIT_EVENT_TIMELINE_HISTORY_SYNC,
     895             :     WAIT_EVENT_TIMELINE_HISTORY_WRITE,
     896             :     WAIT_EVENT_TWOPHASE_FILE_READ,
     897             :     WAIT_EVENT_TWOPHASE_FILE_SYNC,
     898             :     WAIT_EVENT_TWOPHASE_FILE_WRITE,
     899             :     WAIT_EVENT_WALSENDER_TIMELINE_HISTORY_READ,
     900             :     WAIT_EVENT_WAL_BOOTSTRAP_SYNC,
     901             :     WAIT_EVENT_WAL_BOOTSTRAP_WRITE,
     902             :     WAIT_EVENT_WAL_COPY_READ,
     903             :     WAIT_EVENT_WAL_COPY_SYNC,
     904             :     WAIT_EVENT_WAL_COPY_WRITE,
     905             :     WAIT_EVENT_WAL_INIT_SYNC,
     906             :     WAIT_EVENT_WAL_INIT_WRITE,
     907             :     WAIT_EVENT_WAL_READ,
     908             :     WAIT_EVENT_WAL_SYNC_METHOD_ASSIGN,
     909             :     WAIT_EVENT_WAL_WRITE
     910             : } WaitEventIO;
     911             : 
     912             : /* ----------
     913             :  * Command type for progress reporting purposes
     914             :  * ----------
     915             :  */
     916             : typedef enum ProgressCommandType
     917             : {
     918             :     PROGRESS_COMMAND_INVALID,
     919             :     PROGRESS_COMMAND_VACUUM
     920             : } ProgressCommandType;
     921             : 
     922             : #define PGSTAT_NUM_PROGRESS_PARAM   10
     923             : 
     924             : /* ----------
     925             :  * Shared-memory data structures
     926             :  * ----------
     927             :  */
     928             : 
     929             : 
     930             : /*
     931             :  * PgBackendSSLStatus
     932             :  *
     933             :  * For each backend, we keep the SSL status in a separate struct, that
     934             :  * is only filled in if SSL is enabled.
     935             :  */
     936             : typedef struct PgBackendSSLStatus
     937             : {
     938             :     /* Information about SSL connection */
     939             :     int         ssl_bits;
     940             :     bool        ssl_compression;
     941             :     char        ssl_version[NAMEDATALEN];   /* MUST be null-terminated */
     942             :     char        ssl_cipher[NAMEDATALEN];    /* MUST be null-terminated */
     943             :     char        ssl_clientdn[NAMEDATALEN];  /* MUST be null-terminated */
     944             : } PgBackendSSLStatus;
     945             : 
     946             : 
     947             : /* ----------
     948             :  * PgBackendStatus
     949             :  *
     950             :  * Each live backend maintains a PgBackendStatus struct in shared memory
     951             :  * showing its current activity.  (The structs are allocated according to
     952             :  * BackendId, but that is not critical.)  Note that the collector process
     953             :  * has no involvement in, or even access to, these structs.
     954             :  *
     955             :  * Each auxiliary process also maintains a PgBackendStatus struct in shared
     956             :  * memory.
     957             :  * ----------
     958             :  */
     959             : typedef struct PgBackendStatus
     960             : {
     961             :     /*
     962             :      * To avoid locking overhead, we use the following protocol: a backend
     963             :      * increments st_changecount before modifying its entry, and again after
     964             :      * finishing a modification.  A would-be reader should note the value of
     965             :      * st_changecount, copy the entry into private memory, then check
     966             :      * st_changecount again.  If the value hasn't changed, and if it's even,
     967             :      * the copy is valid; otherwise start over.  This makes updates cheap
     968             :      * while reads are potentially expensive, but that's the tradeoff we want.
     969             :      *
     970             :      * The above protocol needs the memory barriers to ensure that the
     971             :      * apparent order of execution is as it desires. Otherwise, for example,
     972             :      * the CPU might rearrange the code so that st_changecount is incremented
     973             :      * twice before the modification on a machine with weak memory ordering.
     974             :      * This surprising result can lead to bugs.
     975             :      */
     976             :     int         st_changecount;
     977             : 
     978             :     /* The entry is valid iff st_procpid > 0, unused if st_procpid == 0 */
     979             :     int         st_procpid;
     980             : 
     981             :     /* Type of backends */
     982             :     BackendType st_backendType;
     983             : 
     984             :     /* Times when current backend, transaction, and activity started */
     985             :     TimestampTz st_proc_start_timestamp;
     986             :     TimestampTz st_xact_start_timestamp;
     987             :     TimestampTz st_activity_start_timestamp;
     988             :     TimestampTz st_state_start_timestamp;
     989             : 
     990             :     /* Database OID, owning user's OID, connection client address */
     991             :     Oid         st_databaseid;
     992             :     Oid         st_userid;
     993             :     SockAddr    st_clientaddr;
     994             :     char       *st_clienthostname;  /* MUST be null-terminated */
     995             : 
     996             :     /* Information about SSL connection */
     997             :     bool        st_ssl;
     998             :     PgBackendSSLStatus *st_sslstatus;
     999             : 
    1000             :     /* current state */
    1001             :     BackendState st_state;
    1002             : 
    1003             :     /* application name; MUST be null-terminated */
    1004             :     char       *st_appname;
    1005             : 
    1006             :     /* current command string; MUST be null-terminated */
    1007             :     char       *st_activity;
    1008             : 
    1009             :     /*
    1010             :      * Command progress reporting.  Any command which wishes can advertise
    1011             :      * that it is running by setting st_progress_command,
    1012             :      * st_progress_command_target, and st_progress_param[].
    1013             :      * st_progress_command_target should be the OID of the relation which the
    1014             :      * command targets (we assume there's just one, as this is meant for
    1015             :      * utility commands), but the meaning of each element in the
    1016             :      * st_progress_param array is command-specific.
    1017             :      */
    1018             :     ProgressCommandType st_progress_command;
    1019             :     Oid         st_progress_command_target;
    1020             :     int64       st_progress_param[PGSTAT_NUM_PROGRESS_PARAM];
    1021             : } PgBackendStatus;
    1022             : 
    1023             : /*
    1024             :  * Macros to load and store st_changecount with the memory barriers.
    1025             :  *
    1026             :  * pgstat_increment_changecount_before() and
    1027             :  * pgstat_increment_changecount_after() need to be called before and after
    1028             :  * PgBackendStatus entries are modified, respectively. This makes sure that
    1029             :  * st_changecount is incremented around the modification.
    1030             :  *
    1031             :  * Also pgstat_save_changecount_before() and pgstat_save_changecount_after()
    1032             :  * need to be called before and after PgBackendStatus entries are copied into
    1033             :  * private memory, respectively.
    1034             :  */
    1035             : #define pgstat_increment_changecount_before(beentry)    \
    1036             :     do {    \
    1037             :         beentry->st_changecount++;   \
    1038             :         pg_write_barrier(); \
    1039             :     } while (0)
    1040             : 
    1041             : #define pgstat_increment_changecount_after(beentry) \
    1042             :     do {    \
    1043             :         pg_write_barrier(); \
    1044             :         beentry->st_changecount++;   \
    1045             :         Assert((beentry->st_changecount & 1) == 0); \
    1046             :     } while (0)
    1047             : 
    1048             : #define pgstat_save_changecount_before(beentry, save_changecount)   \
    1049             :     do {    \
    1050             :         save_changecount = beentry->st_changecount; \
    1051             :         pg_read_barrier();  \
    1052             :     } while (0)
    1053             : 
    1054             : #define pgstat_save_changecount_after(beentry, save_changecount)    \
    1055             :     do {    \
    1056             :         pg_read_barrier();  \
    1057             :         save_changecount = beentry->st_changecount; \
    1058             :     } while (0)
    1059             : 
    1060             : /* ----------
    1061             :  * LocalPgBackendStatus
    1062             :  *
    1063             :  * When we build the backend status array, we use LocalPgBackendStatus to be
    1064             :  * able to add new values to the struct when needed without adding new fields
    1065             :  * to the shared memory. It contains the backend status as a first member.
    1066             :  * ----------
    1067             :  */
    1068             : typedef struct LocalPgBackendStatus
    1069             : {
    1070             :     /*
    1071             :      * Local version of the backend status entry.
    1072             :      */
    1073             :     PgBackendStatus backendStatus;
    1074             : 
    1075             :     /*
    1076             :      * The xid of the current transaction if available, InvalidTransactionId
    1077             :      * if not.
    1078             :      */
    1079             :     TransactionId backend_xid;
    1080             : 
    1081             :     /*
    1082             :      * The xmin of the current session if available, InvalidTransactionId if
    1083             :      * not.
    1084             :      */
    1085             :     TransactionId backend_xmin;
    1086             : } LocalPgBackendStatus;
    1087             : 
    1088             : /*
    1089             :  * Working state needed to accumulate per-function-call timing statistics.
    1090             :  */
    1091             : typedef struct PgStat_FunctionCallUsage
    1092             : {
    1093             :     /* Link to function's hashtable entry (must still be there at exit!) */
    1094             :     /* NULL means we are not tracking the current function call */
    1095             :     PgStat_FunctionCounts *fs;
    1096             :     /* Total time previously charged to function, as of function start */
    1097             :     instr_time  save_f_total_time;
    1098             :     /* Backend-wide total time as of function start */
    1099             :     instr_time  save_total;
    1100             :     /* system clock as of function start */
    1101             :     instr_time  f_start;
    1102             : } PgStat_FunctionCallUsage;
    1103             : 
    1104             : 
    1105             : /* ----------
    1106             :  * GUC parameters
    1107             :  * ----------
    1108             :  */
    1109             : extern bool pgstat_track_activities;
    1110             : extern bool pgstat_track_counts;
    1111             : extern int  pgstat_track_functions;
    1112             : extern PGDLLIMPORT int pgstat_track_activity_query_size;
    1113             : extern char *pgstat_stat_directory;
    1114             : extern char *pgstat_stat_tmpname;
    1115             : extern char *pgstat_stat_filename;
    1116             : 
    1117             : /*
    1118             :  * BgWriter statistics counters are updated directly by bgwriter and bufmgr
    1119             :  */
    1120             : extern PgStat_MsgBgWriter BgWriterStats;
    1121             : 
    1122             : /*
    1123             :  * Updated by pgstat_count_buffer_*_time macros
    1124             :  */
    1125             : extern PgStat_Counter pgStatBlockReadTime;
    1126             : extern PgStat_Counter pgStatBlockWriteTime;
    1127             : 
    1128             : /* ----------
    1129             :  * Functions called from postmaster
    1130             :  * ----------
    1131             :  */
    1132             : extern Size BackendStatusShmemSize(void);
    1133             : extern void CreateSharedBackendStatus(void);
    1134             : 
    1135             : extern void pgstat_init(void);
    1136             : extern int  pgstat_start(void);
    1137             : extern void pgstat_reset_all(void);
    1138             : extern void allow_immediate_pgstat_restart(void);
    1139             : 
    1140             : #ifdef EXEC_BACKEND
    1141             : extern void PgstatCollectorMain(int argc, char *argv[]) pg_attribute_noreturn();
    1142             : #endif
    1143             : 
    1144             : 
    1145             : /* ----------
    1146             :  * Functions called from backends
    1147             :  * ----------
    1148             :  */
    1149             : extern void pgstat_ping(void);
    1150             : 
    1151             : extern void pgstat_report_stat(bool force);
    1152             : extern void pgstat_vacuum_stat(void);
    1153             : extern void pgstat_drop_database(Oid databaseid);
    1154             : 
    1155             : extern void pgstat_clear_snapshot(void);
    1156             : extern void pgstat_reset_counters(void);
    1157             : extern void pgstat_reset_shared_counters(const char *);
    1158             : extern void pgstat_reset_single_counter(Oid objectid, PgStat_Single_Reset_Type type);
    1159             : 
    1160             : extern void pgstat_report_autovac(Oid dboid);
    1161             : extern void pgstat_report_vacuum(Oid tableoid, bool shared,
    1162             :                      PgStat_Counter livetuples, PgStat_Counter deadtuples);
    1163             : extern void pgstat_report_analyze(Relation rel,
    1164             :                       PgStat_Counter livetuples, PgStat_Counter deadtuples,
    1165             :                       bool resetcounter);
    1166             : 
    1167             : extern void pgstat_report_recovery_conflict(int reason);
    1168             : extern void pgstat_report_deadlock(void);
    1169             : 
    1170             : extern void pgstat_initialize(void);
    1171             : extern void pgstat_bestart(void);
    1172             : 
    1173             : extern void pgstat_report_activity(BackendState state, const char *cmd_str);
    1174             : extern void pgstat_report_tempfile(size_t filesize);
    1175             : extern void pgstat_report_appname(const char *appname);
    1176             : extern void pgstat_report_xact_timestamp(TimestampTz tstamp);
    1177             : extern const char *pgstat_get_wait_event(uint32 wait_event_info);
    1178             : extern const char *pgstat_get_wait_event_type(uint32 wait_event_info);
    1179             : extern const char *pgstat_get_backend_current_activity(int pid, bool checkUser);
    1180             : extern const char *pgstat_get_crashed_backend_activity(int pid, char *buffer,
    1181             :                                     int buflen);
    1182             : extern const char *pgstat_get_backend_desc(BackendType backendType);
    1183             : 
    1184             : extern void pgstat_progress_start_command(ProgressCommandType cmdtype,
    1185             :                               Oid relid);
    1186             : extern void pgstat_progress_update_param(int index, int64 val);
    1187             : extern void pgstat_progress_update_multi_param(int nparam, const int *index,
    1188             :                                    const int64 *val);
    1189             : extern void pgstat_progress_end_command(void);
    1190             : 
    1191             : extern PgStat_TableStatus *find_tabstat_entry(Oid rel_id);
    1192             : extern PgStat_BackendFunctionEntry *find_funcstat_entry(Oid func_id);
    1193             : 
    1194             : extern void pgstat_initstats(Relation rel);
    1195             : 
    1196             : /* ----------
    1197             :  * pgstat_report_wait_start() -
    1198             :  *
    1199             :  *  Called from places where server process needs to wait.  This is called
    1200             :  *  to report wait event information.  The wait information is stored
    1201             :  *  as 4-bytes where first byte represents the wait event class (type of
    1202             :  *  wait, for different types of wait, refer WaitClass) and the next
    1203             :  *  3-bytes represent the actual wait event.  Currently 2-bytes are used
    1204             :  *  for wait event which is sufficient for current usage, 1-byte is
    1205             :  *  reserved for future usage.
    1206             :  *
    1207             :  * NB: this *must* be able to survive being called before MyProc has been
    1208             :  * initialized.
    1209             :  * ----------
    1210             :  */
    1211             : static inline void
    1212       93938 : pgstat_report_wait_start(uint32 wait_event_info)
    1213             : {
    1214       93938 :     volatile PGPROC *proc = MyProc;
    1215             : 
    1216       93938 :     if (!pgstat_track_activities || !proc)
    1217       98244 :         return;
    1218             : 
    1219             :     /*
    1220             :      * Since this is a four-byte field which is always read and written as
    1221             :      * four-bytes, updates are atomic.
    1222             :      */
    1223       89632 :     proc->wait_event_info = wait_event_info;
    1224             : }
    1225             : 
    1226             : /* ----------
    1227             :  * pgstat_report_wait_end() -
    1228             :  *
    1229             :  *  Called to report end of a wait.
    1230             :  *
    1231             :  * NB: this *must* be able to survive being called before MyProc has been
    1232             :  * initialized.
    1233             :  * ----------
    1234             :  */
    1235             : static inline void
    1236       97571 : pgstat_report_wait_end(void)
    1237             : {
    1238       97571 :     volatile PGPROC *proc = MyProc;
    1239             : 
    1240       97571 :     if (!pgstat_track_activities || !proc)
    1241      101877 :         return;
    1242             : 
    1243             :     /*
    1244             :      * Since this is a four-byte field which is always read and written as
    1245             :      * four-bytes, updates are atomic.
    1246             :      */
    1247       93265 :     proc->wait_event_info = 0;
    1248             : }
    1249             : 
    1250             : /* nontransactional event counts are simple enough to inline */
    1251             : 
    1252             : #define pgstat_count_heap_scan(rel)                                 \
    1253             :     do {                                                            \
    1254             :         if ((rel)->pgstat_info != NULL)                              \
    1255             :             (rel)->pgstat_info->t_counts.t_numscans++;                \
    1256             :     } while (0)
    1257             : #define pgstat_count_heap_getnext(rel)                              \
    1258             :     do {                                                            \
    1259             :         if ((rel)->pgstat_info != NULL)                              \
    1260             :             (rel)->pgstat_info->t_counts.t_tuples_returned++;     \
    1261             :     } while (0)
    1262             : #define pgstat_count_heap_fetch(rel)                                \
    1263             :     do {                                                            \
    1264             :         if ((rel)->pgstat_info != NULL)                              \
    1265             :             (rel)->pgstat_info->t_counts.t_tuples_fetched++;      \
    1266             :     } while (0)
    1267             : #define pgstat_count_index_scan(rel)                                \
    1268             :     do {                                                            \
    1269             :         if ((rel)->pgstat_info != NULL)                              \
    1270             :             (rel)->pgstat_info->t_counts.t_numscans++;                \
    1271             :     } while (0)
    1272             : #define pgstat_count_index_tuples(rel, n)                           \
    1273             :     do {                                                            \
    1274             :         if ((rel)->pgstat_info != NULL)                              \
    1275             :             (rel)->pgstat_info->t_counts.t_tuples_returned += (n);    \
    1276             :     } while (0)
    1277             : #define pgstat_count_buffer_read(rel)                               \
    1278             :     do {                                                            \
    1279             :         if ((rel)->pgstat_info != NULL)                              \
    1280             :             (rel)->pgstat_info->t_counts.t_blocks_fetched++;      \
    1281             :     } while (0)
    1282             : #define pgstat_count_buffer_hit(rel)                                \
    1283             :     do {                                                            \
    1284             :         if ((rel)->pgstat_info != NULL)                              \
    1285             :             (rel)->pgstat_info->t_counts.t_blocks_hit++;          \
    1286             :     } while (0)
    1287             : #define pgstat_count_buffer_read_time(n)                            \
    1288             :     (pgStatBlockReadTime += (n))
    1289             : #define pgstat_count_buffer_write_time(n)                           \
    1290             :     (pgStatBlockWriteTime += (n))
    1291             : 
    1292             : extern void pgstat_count_heap_insert(Relation rel, PgStat_Counter n);
    1293             : extern void pgstat_count_heap_update(Relation rel, bool hot);
    1294             : extern void pgstat_count_heap_delete(Relation rel);
    1295             : extern void pgstat_count_truncate(Relation rel);
    1296             : extern void pgstat_update_heap_dead_tuples(Relation rel, int delta);
    1297             : 
    1298             : extern void pgstat_init_function_usage(FunctionCallInfoData *fcinfo,
    1299             :                            PgStat_FunctionCallUsage *fcu);
    1300             : extern void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu,
    1301             :                           bool finalize);
    1302             : 
    1303             : extern void AtEOXact_PgStat(bool isCommit);
    1304             : extern void AtEOSubXact_PgStat(bool isCommit, int nestDepth);
    1305             : 
    1306             : extern void AtPrepare_PgStat(void);
    1307             : extern void PostPrepare_PgStat(void);
    1308             : 
    1309             : extern void pgstat_twophase_postcommit(TransactionId xid, uint16 info,
    1310             :                            void *recdata, uint32 len);
    1311             : extern void pgstat_twophase_postabort(TransactionId xid, uint16 info,
    1312             :                           void *recdata, uint32 len);
    1313             : 
    1314             : extern void pgstat_send_archiver(const char *xlog, bool failed);
    1315             : extern void pgstat_send_bgwriter(void);
    1316             : 
    1317             : /* ----------
    1318             :  * Support functions for the SQL-callable functions to
    1319             :  * generate the pgstat* views.
    1320             :  * ----------
    1321             :  */
    1322             : extern PgStat_StatDBEntry *pgstat_fetch_stat_dbentry(Oid dbid);
    1323             : extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry(Oid relid);
    1324             : extern PgBackendStatus *pgstat_fetch_stat_beentry(int beid);
    1325             : extern LocalPgBackendStatus *pgstat_fetch_stat_local_beentry(int beid);
    1326             : extern PgStat_StatFuncEntry *pgstat_fetch_stat_funcentry(Oid funcid);
    1327             : extern int  pgstat_fetch_stat_numbackends(void);
    1328             : extern PgStat_ArchiverStats *pgstat_fetch_stat_archiver(void);
    1329             : extern PgStat_GlobalStats *pgstat_fetch_global(void);
    1330             : 
    1331             : #endif                          /* PGSTAT_H */

Generated by: LCOV version 1.11