LCOV - code coverage report
Current view: top level - src/include - pgstat.h (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 10 10 100.0 %
Date: 2019-09-19 02:07:14 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-2019, 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 "libpq/pqcomm.h"
      16             : #include "port/atomics.h"
      17             : #include "portability/instr_time.h"
      18             : #include "postmaster/pgarch.h"
      19             : #include "storage/proc.h"
      20             : #include "utils/hsearch.h"
      21             : #include "utils/relcache.h"
      22             : 
      23             : 
      24             : /* ----------
      25             :  * Paths for the statistics files (relative to installation's $PGDATA).
      26             :  * ----------
      27             :  */
      28             : #define PGSTAT_STAT_PERMANENT_DIRECTORY     "pg_stat"
      29             : #define PGSTAT_STAT_PERMANENT_FILENAME      "pg_stat/global.stat"
      30             : #define PGSTAT_STAT_PERMANENT_TMPFILE       "pg_stat/global.tmp"
      31             : 
      32             : /* Default directory to store temporary statistics data in */
      33             : #define PG_STAT_TMP_DIR     "pg_stat_tmp"
      34             : 
      35             : /* Values for track_functions GUC variable --- order is significant! */
      36             : typedef enum TrackFunctionsLevel
      37             : {
      38             :     TRACK_FUNC_OFF,
      39             :     TRACK_FUNC_PL,
      40             :     TRACK_FUNC_ALL
      41             : }           TrackFunctionsLevel;
      42             : 
      43             : /* ----------
      44             :  * The types of backend -> collector messages
      45             :  * ----------
      46             :  */
      47             : typedef enum StatMsgType
      48             : {
      49             :     PGSTAT_MTYPE_DUMMY,
      50             :     PGSTAT_MTYPE_INQUIRY,
      51             :     PGSTAT_MTYPE_TABSTAT,
      52             :     PGSTAT_MTYPE_TABPURGE,
      53             :     PGSTAT_MTYPE_DROPDB,
      54             :     PGSTAT_MTYPE_RESETCOUNTER,
      55             :     PGSTAT_MTYPE_RESETSHAREDCOUNTER,
      56             :     PGSTAT_MTYPE_RESETSINGLECOUNTER,
      57             :     PGSTAT_MTYPE_AUTOVAC_START,
      58             :     PGSTAT_MTYPE_VACUUM,
      59             :     PGSTAT_MTYPE_ANALYZE,
      60             :     PGSTAT_MTYPE_ARCHIVER,
      61             :     PGSTAT_MTYPE_BGWRITER,
      62             :     PGSTAT_MTYPE_FUNCSTAT,
      63             :     PGSTAT_MTYPE_FUNCPURGE,
      64             :     PGSTAT_MTYPE_RECOVERYCONFLICT,
      65             :     PGSTAT_MTYPE_TEMPFILE,
      66             :     PGSTAT_MTYPE_DEADLOCK,
      67             :     PGSTAT_MTYPE_CHECKSUMFAILURE
      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             :  * PgStat_MsgChecksumFailure    Sent by the backend to tell the collector
     535             :  *                              about checksum failures noticed.
     536             :  * ----------
     537             :  */
     538             : typedef struct PgStat_MsgChecksumFailure
     539             : {
     540             :     PgStat_MsgHdr m_hdr;
     541             :     Oid         m_databaseid;
     542             :     int         m_failurecount;
     543             :     TimestampTz m_failure_time;
     544             : } PgStat_MsgChecksumFailure;
     545             : 
     546             : 
     547             : /* ----------
     548             :  * PgStat_Msg                   Union over all possible messages.
     549             :  * ----------
     550             :  */
     551             : typedef union PgStat_Msg
     552             : {
     553             :     PgStat_MsgHdr msg_hdr;
     554             :     PgStat_MsgDummy msg_dummy;
     555             :     PgStat_MsgInquiry msg_inquiry;
     556             :     PgStat_MsgTabstat msg_tabstat;
     557             :     PgStat_MsgTabpurge msg_tabpurge;
     558             :     PgStat_MsgDropdb msg_dropdb;
     559             :     PgStat_MsgResetcounter msg_resetcounter;
     560             :     PgStat_MsgResetsharedcounter msg_resetsharedcounter;
     561             :     PgStat_MsgResetsinglecounter msg_resetsinglecounter;
     562             :     PgStat_MsgAutovacStart msg_autovacuum_start;
     563             :     PgStat_MsgVacuum msg_vacuum;
     564             :     PgStat_MsgAnalyze msg_analyze;
     565             :     PgStat_MsgArchiver msg_archiver;
     566             :     PgStat_MsgBgWriter msg_bgwriter;
     567             :     PgStat_MsgFuncstat msg_funcstat;
     568             :     PgStat_MsgFuncpurge msg_funcpurge;
     569             :     PgStat_MsgRecoveryConflict msg_recoveryconflict;
     570             :     PgStat_MsgDeadlock msg_deadlock;
     571             :     PgStat_MsgTempFile msg_tempfile;
     572             :     PgStat_MsgChecksumFailure msg_checksumfailure;
     573             : } PgStat_Msg;
     574             : 
     575             : 
     576             : /* ------------------------------------------------------------
     577             :  * Statistic collector data structures follow
     578             :  *
     579             :  * PGSTAT_FILE_FORMAT_ID should be changed whenever any of these
     580             :  * data structures change.
     581             :  * ------------------------------------------------------------
     582             :  */
     583             : 
     584             : #define PGSTAT_FILE_FORMAT_ID   0x01A5BC9D
     585             : 
     586             : /* ----------
     587             :  * PgStat_StatDBEntry           The collector's data per database
     588             :  * ----------
     589             :  */
     590             : typedef struct PgStat_StatDBEntry
     591             : {
     592             :     Oid         databaseid;
     593             :     PgStat_Counter n_xact_commit;
     594             :     PgStat_Counter n_xact_rollback;
     595             :     PgStat_Counter n_blocks_fetched;
     596             :     PgStat_Counter n_blocks_hit;
     597             :     PgStat_Counter n_tuples_returned;
     598             :     PgStat_Counter n_tuples_fetched;
     599             :     PgStat_Counter n_tuples_inserted;
     600             :     PgStat_Counter n_tuples_updated;
     601             :     PgStat_Counter n_tuples_deleted;
     602             :     TimestampTz last_autovac_time;
     603             :     PgStat_Counter n_conflict_tablespace;
     604             :     PgStat_Counter n_conflict_lock;
     605             :     PgStat_Counter n_conflict_snapshot;
     606             :     PgStat_Counter n_conflict_bufferpin;
     607             :     PgStat_Counter n_conflict_startup_deadlock;
     608             :     PgStat_Counter n_temp_files;
     609             :     PgStat_Counter n_temp_bytes;
     610             :     PgStat_Counter n_deadlocks;
     611             :     PgStat_Counter n_checksum_failures;
     612             :     TimestampTz last_checksum_failure;
     613             :     PgStat_Counter n_block_read_time;   /* times in microseconds */
     614             :     PgStat_Counter n_block_write_time;
     615             : 
     616             :     TimestampTz stat_reset_timestamp;
     617             :     TimestampTz stats_timestamp;    /* time of db stats file update */
     618             : 
     619             :     /*
     620             :      * tables and functions must be last in the struct, because we don't write
     621             :      * the pointers out to the stats file.
     622             :      */
     623             :     HTAB       *tables;
     624             :     HTAB       *functions;
     625             : } PgStat_StatDBEntry;
     626             : 
     627             : 
     628             : /* ----------
     629             :  * PgStat_StatTabEntry          The collector's data per table (or index)
     630             :  * ----------
     631             :  */
     632             : typedef struct PgStat_StatTabEntry
     633             : {
     634             :     Oid         tableid;
     635             : 
     636             :     PgStat_Counter numscans;
     637             : 
     638             :     PgStat_Counter tuples_returned;
     639             :     PgStat_Counter tuples_fetched;
     640             : 
     641             :     PgStat_Counter tuples_inserted;
     642             :     PgStat_Counter tuples_updated;
     643             :     PgStat_Counter tuples_deleted;
     644             :     PgStat_Counter tuples_hot_updated;
     645             : 
     646             :     PgStat_Counter n_live_tuples;
     647             :     PgStat_Counter n_dead_tuples;
     648             :     PgStat_Counter changes_since_analyze;
     649             : 
     650             :     PgStat_Counter blocks_fetched;
     651             :     PgStat_Counter blocks_hit;
     652             : 
     653             :     TimestampTz vacuum_timestamp;   /* user initiated vacuum */
     654             :     PgStat_Counter vacuum_count;
     655             :     TimestampTz autovac_vacuum_timestamp;   /* autovacuum initiated */
     656             :     PgStat_Counter autovac_vacuum_count;
     657             :     TimestampTz analyze_timestamp;  /* user initiated */
     658             :     PgStat_Counter analyze_count;
     659             :     TimestampTz autovac_analyze_timestamp;  /* autovacuum initiated */
     660             :     PgStat_Counter autovac_analyze_count;
     661             : } PgStat_StatTabEntry;
     662             : 
     663             : 
     664             : /* ----------
     665             :  * PgStat_StatFuncEntry         The collector's data per function
     666             :  * ----------
     667             :  */
     668             : typedef struct PgStat_StatFuncEntry
     669             : {
     670             :     Oid         functionid;
     671             : 
     672             :     PgStat_Counter f_numcalls;
     673             : 
     674             :     PgStat_Counter f_total_time;    /* times in microseconds */
     675             :     PgStat_Counter f_self_time;
     676             : } PgStat_StatFuncEntry;
     677             : 
     678             : 
     679             : /*
     680             :  * Archiver statistics kept in the stats collector
     681             :  */
     682             : typedef struct PgStat_ArchiverStats
     683             : {
     684             :     PgStat_Counter archived_count;  /* archival successes */
     685             :     char        last_archived_wal[MAX_XFN_CHARS + 1];   /* last WAL file
     686             :                                                          * archived */
     687             :     TimestampTz last_archived_timestamp;    /* last archival success time */
     688             :     PgStat_Counter failed_count;    /* failed archival attempts */
     689             :     char        last_failed_wal[MAX_XFN_CHARS + 1]; /* WAL file involved in
     690             :                                                      * last failure */
     691             :     TimestampTz last_failed_timestamp;  /* last archival failure time */
     692             :     TimestampTz stat_reset_timestamp;
     693             : } PgStat_ArchiverStats;
     694             : 
     695             : /*
     696             :  * Global statistics kept in the stats collector
     697             :  */
     698             : typedef struct PgStat_GlobalStats
     699             : {
     700             :     TimestampTz stats_timestamp;    /* time of stats file update */
     701             :     PgStat_Counter timed_checkpoints;
     702             :     PgStat_Counter requested_checkpoints;
     703             :     PgStat_Counter checkpoint_write_time;   /* times in milliseconds */
     704             :     PgStat_Counter checkpoint_sync_time;
     705             :     PgStat_Counter buf_written_checkpoints;
     706             :     PgStat_Counter buf_written_clean;
     707             :     PgStat_Counter maxwritten_clean;
     708             :     PgStat_Counter buf_written_backend;
     709             :     PgStat_Counter buf_fsync_backend;
     710             :     PgStat_Counter buf_alloc;
     711             :     TimestampTz stat_reset_timestamp;
     712             : } PgStat_GlobalStats;
     713             : 
     714             : 
     715             : /* ----------
     716             :  * Backend types
     717             :  * ----------
     718             :  */
     719             : typedef enum BackendType
     720             : {
     721             :     B_AUTOVAC_LAUNCHER,
     722             :     B_AUTOVAC_WORKER,
     723             :     B_BACKEND,
     724             :     B_BG_WORKER,
     725             :     B_BG_WRITER,
     726             :     B_CHECKPOINTER,
     727             :     B_STARTUP,
     728             :     B_WAL_RECEIVER,
     729             :     B_WAL_SENDER,
     730             :     B_WAL_WRITER
     731             : } BackendType;
     732             : 
     733             : 
     734             : /* ----------
     735             :  * Backend states
     736             :  * ----------
     737             :  */
     738             : typedef enum BackendState
     739             : {
     740             :     STATE_UNDEFINED,
     741             :     STATE_IDLE,
     742             :     STATE_RUNNING,
     743             :     STATE_IDLEINTRANSACTION,
     744             :     STATE_FASTPATH,
     745             :     STATE_IDLEINTRANSACTION_ABORTED,
     746             :     STATE_DISABLED
     747             : } BackendState;
     748             : 
     749             : 
     750             : /* ----------
     751             :  * Wait Classes
     752             :  * ----------
     753             :  */
     754             : #define PG_WAIT_LWLOCK              0x01000000U
     755             : #define PG_WAIT_LOCK                0x03000000U
     756             : #define PG_WAIT_BUFFER_PIN          0x04000000U
     757             : #define PG_WAIT_ACTIVITY            0x05000000U
     758             : #define PG_WAIT_CLIENT              0x06000000U
     759             : #define PG_WAIT_EXTENSION           0x07000000U
     760             : #define PG_WAIT_IPC                 0x08000000U
     761             : #define PG_WAIT_TIMEOUT             0x09000000U
     762             : #define PG_WAIT_IO                  0x0A000000U
     763             : 
     764             : /* ----------
     765             :  * Wait Events - Activity
     766             :  *
     767             :  * Use this category when a process is waiting because it has no work to do,
     768             :  * unless the "Client" or "Timeout" category describes the situation better.
     769             :  * Typically, this should only be used for background processes.
     770             :  * ----------
     771             :  */
     772             : typedef enum
     773             : {
     774             :     WAIT_EVENT_ARCHIVER_MAIN = PG_WAIT_ACTIVITY,
     775             :     WAIT_EVENT_AUTOVACUUM_MAIN,
     776             :     WAIT_EVENT_BGWRITER_HIBERNATE,
     777             :     WAIT_EVENT_BGWRITER_MAIN,
     778             :     WAIT_EVENT_CHECKPOINTER_MAIN,
     779             :     WAIT_EVENT_LOGICAL_APPLY_MAIN,
     780             :     WAIT_EVENT_LOGICAL_LAUNCHER_MAIN,
     781             :     WAIT_EVENT_PGSTAT_MAIN,
     782             :     WAIT_EVENT_RECOVERY_WAL_ALL,
     783             :     WAIT_EVENT_RECOVERY_WAL_STREAM,
     784             :     WAIT_EVENT_SYSLOGGER_MAIN,
     785             :     WAIT_EVENT_WAL_RECEIVER_MAIN,
     786             :     WAIT_EVENT_WAL_SENDER_MAIN,
     787             :     WAIT_EVENT_WAL_WRITER_MAIN
     788             : } WaitEventActivity;
     789             : 
     790             : /* ----------
     791             :  * Wait Events - Client
     792             :  *
     793             :  * Use this category when a process is waiting to send data to or receive data
     794             :  * from the frontend process to which it is connected.  This is never used for
     795             :  * a background process, which has no client connection.
     796             :  * ----------
     797             :  */
     798             : typedef enum
     799             : {
     800             :     WAIT_EVENT_CLIENT_READ = PG_WAIT_CLIENT,
     801             :     WAIT_EVENT_CLIENT_WRITE,
     802             :     WAIT_EVENT_LIBPQWALRECEIVER_CONNECT,
     803             :     WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE,
     804             :     WAIT_EVENT_SSL_OPEN_SERVER,
     805             :     WAIT_EVENT_WAL_RECEIVER_WAIT_START,
     806             :     WAIT_EVENT_WAL_SENDER_WAIT_WAL,
     807             :     WAIT_EVENT_WAL_SENDER_WRITE_DATA,
     808             :     WAIT_EVENT_GSS_OPEN_SERVER,
     809             : } WaitEventClient;
     810             : 
     811             : /* ----------
     812             :  * Wait Events - IPC
     813             :  *
     814             :  * Use this category when a process cannot complete the work it is doing because
     815             :  * it is waiting for a notification from another process.
     816             :  * ----------
     817             :  */
     818             : typedef enum
     819             : {
     820             :     WAIT_EVENT_BGWORKER_SHUTDOWN = PG_WAIT_IPC,
     821             :     WAIT_EVENT_BGWORKER_STARTUP,
     822             :     WAIT_EVENT_BTREE_PAGE,
     823             :     WAIT_EVENT_CLOG_GROUP_UPDATE,
     824             :     WAIT_EVENT_CHECKPOINT_DONE,
     825             :     WAIT_EVENT_CHECKPOINT_START,
     826             :     WAIT_EVENT_EXECUTE_GATHER,
     827             :     WAIT_EVENT_HASH_BATCH_ALLOCATING,
     828             :     WAIT_EVENT_HASH_BATCH_ELECTING,
     829             :     WAIT_EVENT_HASH_BATCH_LOADING,
     830             :     WAIT_EVENT_HASH_BUILD_ALLOCATING,
     831             :     WAIT_EVENT_HASH_BUILD_ELECTING,
     832             :     WAIT_EVENT_HASH_BUILD_HASHING_INNER,
     833             :     WAIT_EVENT_HASH_BUILD_HASHING_OUTER,
     834             :     WAIT_EVENT_HASH_GROW_BATCHES_ALLOCATING,
     835             :     WAIT_EVENT_HASH_GROW_BATCHES_DECIDING,
     836             :     WAIT_EVENT_HASH_GROW_BATCHES_ELECTING,
     837             :     WAIT_EVENT_HASH_GROW_BATCHES_FINISHING,
     838             :     WAIT_EVENT_HASH_GROW_BATCHES_REPARTITIONING,
     839             :     WAIT_EVENT_HASH_GROW_BUCKETS_ALLOCATING,
     840             :     WAIT_EVENT_HASH_GROW_BUCKETS_ELECTING,
     841             :     WAIT_EVENT_HASH_GROW_BUCKETS_REINSERTING,
     842             :     WAIT_EVENT_LOGICAL_SYNC_DATA,
     843             :     WAIT_EVENT_LOGICAL_SYNC_STATE_CHANGE,
     844             :     WAIT_EVENT_MQ_INTERNAL,
     845             :     WAIT_EVENT_MQ_PUT_MESSAGE,
     846             :     WAIT_EVENT_MQ_RECEIVE,
     847             :     WAIT_EVENT_MQ_SEND,
     848             :     WAIT_EVENT_PARALLEL_BITMAP_SCAN,
     849             :     WAIT_EVENT_PARALLEL_CREATE_INDEX_SCAN,
     850             :     WAIT_EVENT_PARALLEL_FINISH,
     851             :     WAIT_EVENT_PROCARRAY_GROUP_UPDATE,
     852             :     WAIT_EVENT_PROMOTE,
     853             :     WAIT_EVENT_REPLICATION_ORIGIN_DROP,
     854             :     WAIT_EVENT_REPLICATION_SLOT_DROP,
     855             :     WAIT_EVENT_SAFE_SNAPSHOT,
     856             :     WAIT_EVENT_SYNC_REP
     857             : } WaitEventIPC;
     858             : 
     859             : /* ----------
     860             :  * Wait Events - Timeout
     861             :  *
     862             :  * Use this category when a process is waiting for a timeout to expire.
     863             :  * ----------
     864             :  */
     865             : typedef enum
     866             : {
     867             :     WAIT_EVENT_BASE_BACKUP_THROTTLE = PG_WAIT_TIMEOUT,
     868             :     WAIT_EVENT_PG_SLEEP,
     869             :     WAIT_EVENT_RECOVERY_APPLY_DELAY
     870             : } WaitEventTimeout;
     871             : 
     872             : /* ----------
     873             :  * Wait Events - IO
     874             :  *
     875             :  * Use this category when a process is waiting for a IO.
     876             :  * ----------
     877             :  */
     878             : typedef enum
     879             : {
     880             :     WAIT_EVENT_BUFFILE_READ = PG_WAIT_IO,
     881             :     WAIT_EVENT_BUFFILE_WRITE,
     882             :     WAIT_EVENT_CONTROL_FILE_READ,
     883             :     WAIT_EVENT_CONTROL_FILE_SYNC,
     884             :     WAIT_EVENT_CONTROL_FILE_SYNC_UPDATE,
     885             :     WAIT_EVENT_CONTROL_FILE_WRITE,
     886             :     WAIT_EVENT_CONTROL_FILE_WRITE_UPDATE,
     887             :     WAIT_EVENT_COPY_FILE_READ,
     888             :     WAIT_EVENT_COPY_FILE_WRITE,
     889             :     WAIT_EVENT_DATA_FILE_EXTEND,
     890             :     WAIT_EVENT_DATA_FILE_FLUSH,
     891             :     WAIT_EVENT_DATA_FILE_IMMEDIATE_SYNC,
     892             :     WAIT_EVENT_DATA_FILE_PREFETCH,
     893             :     WAIT_EVENT_DATA_FILE_READ,
     894             :     WAIT_EVENT_DATA_FILE_SYNC,
     895             :     WAIT_EVENT_DATA_FILE_TRUNCATE,
     896             :     WAIT_EVENT_DATA_FILE_WRITE,
     897             :     WAIT_EVENT_DSM_FILL_ZERO_WRITE,
     898             :     WAIT_EVENT_LOCK_FILE_ADDTODATADIR_READ,
     899             :     WAIT_EVENT_LOCK_FILE_ADDTODATADIR_SYNC,
     900             :     WAIT_EVENT_LOCK_FILE_ADDTODATADIR_WRITE,
     901             :     WAIT_EVENT_LOCK_FILE_CREATE_READ,
     902             :     WAIT_EVENT_LOCK_FILE_CREATE_SYNC,
     903             :     WAIT_EVENT_LOCK_FILE_CREATE_WRITE,
     904             :     WAIT_EVENT_LOCK_FILE_RECHECKDATADIR_READ,
     905             :     WAIT_EVENT_LOGICAL_REWRITE_CHECKPOINT_SYNC,
     906             :     WAIT_EVENT_LOGICAL_REWRITE_MAPPING_SYNC,
     907             :     WAIT_EVENT_LOGICAL_REWRITE_MAPPING_WRITE,
     908             :     WAIT_EVENT_LOGICAL_REWRITE_SYNC,
     909             :     WAIT_EVENT_LOGICAL_REWRITE_TRUNCATE,
     910             :     WAIT_EVENT_LOGICAL_REWRITE_WRITE,
     911             :     WAIT_EVENT_RELATION_MAP_READ,
     912             :     WAIT_EVENT_RELATION_MAP_SYNC,
     913             :     WAIT_EVENT_RELATION_MAP_WRITE,
     914             :     WAIT_EVENT_REORDER_BUFFER_READ,
     915             :     WAIT_EVENT_REORDER_BUFFER_WRITE,
     916             :     WAIT_EVENT_REORDER_LOGICAL_MAPPING_READ,
     917             :     WAIT_EVENT_REPLICATION_SLOT_READ,
     918             :     WAIT_EVENT_REPLICATION_SLOT_RESTORE_SYNC,
     919             :     WAIT_EVENT_REPLICATION_SLOT_SYNC,
     920             :     WAIT_EVENT_REPLICATION_SLOT_WRITE,
     921             :     WAIT_EVENT_SLRU_FLUSH_SYNC,
     922             :     WAIT_EVENT_SLRU_READ,
     923             :     WAIT_EVENT_SLRU_SYNC,
     924             :     WAIT_EVENT_SLRU_WRITE,
     925             :     WAIT_EVENT_SNAPBUILD_READ,
     926             :     WAIT_EVENT_SNAPBUILD_SYNC,
     927             :     WAIT_EVENT_SNAPBUILD_WRITE,
     928             :     WAIT_EVENT_TIMELINE_HISTORY_FILE_SYNC,
     929             :     WAIT_EVENT_TIMELINE_HISTORY_FILE_WRITE,
     930             :     WAIT_EVENT_TIMELINE_HISTORY_READ,
     931             :     WAIT_EVENT_TIMELINE_HISTORY_SYNC,
     932             :     WAIT_EVENT_TIMELINE_HISTORY_WRITE,
     933             :     WAIT_EVENT_TWOPHASE_FILE_READ,
     934             :     WAIT_EVENT_TWOPHASE_FILE_SYNC,
     935             :     WAIT_EVENT_TWOPHASE_FILE_WRITE,
     936             :     WAIT_EVENT_WALSENDER_TIMELINE_HISTORY_READ,
     937             :     WAIT_EVENT_WAL_BOOTSTRAP_SYNC,
     938             :     WAIT_EVENT_WAL_BOOTSTRAP_WRITE,
     939             :     WAIT_EVENT_WAL_COPY_READ,
     940             :     WAIT_EVENT_WAL_COPY_SYNC,
     941             :     WAIT_EVENT_WAL_COPY_WRITE,
     942             :     WAIT_EVENT_WAL_INIT_SYNC,
     943             :     WAIT_EVENT_WAL_INIT_WRITE,
     944             :     WAIT_EVENT_WAL_READ,
     945             :     WAIT_EVENT_WAL_SYNC,
     946             :     WAIT_EVENT_WAL_SYNC_METHOD_ASSIGN,
     947             :     WAIT_EVENT_WAL_WRITE
     948             : } WaitEventIO;
     949             : 
     950             : /* ----------
     951             :  * Command type for progress reporting purposes
     952             :  * ----------
     953             :  */
     954             : typedef enum ProgressCommandType
     955             : {
     956             :     PROGRESS_COMMAND_INVALID,
     957             :     PROGRESS_COMMAND_VACUUM,
     958             :     PROGRESS_COMMAND_CLUSTER,
     959             :     PROGRESS_COMMAND_CREATE_INDEX
     960             : } ProgressCommandType;
     961             : 
     962             : #define PGSTAT_NUM_PROGRESS_PARAM   20
     963             : 
     964             : /* ----------
     965             :  * Shared-memory data structures
     966             :  * ----------
     967             :  */
     968             : 
     969             : 
     970             : /*
     971             :  * PgBackendSSLStatus
     972             :  *
     973             :  * For each backend, we keep the SSL status in a separate struct, that
     974             :  * is only filled in if SSL is enabled.
     975             :  *
     976             :  * All char arrays must be null-terminated.
     977             :  */
     978             : typedef struct PgBackendSSLStatus
     979             : {
     980             :     /* Information about SSL connection */
     981             :     int         ssl_bits;
     982             :     bool        ssl_compression;
     983             :     char        ssl_version[NAMEDATALEN];
     984             :     char        ssl_cipher[NAMEDATALEN];
     985             :     char        ssl_client_dn[NAMEDATALEN];
     986             : 
     987             :     /*
     988             :      * serial number is max "20 octets" per RFC 5280, so this size should be
     989             :      * fine
     990             :      */
     991             :     char        ssl_client_serial[NAMEDATALEN];
     992             : 
     993             :     char        ssl_issuer_dn[NAMEDATALEN];
     994             : } PgBackendSSLStatus;
     995             : 
     996             : /*
     997             :  * PgBackendGSSStatus
     998             :  *
     999             :  * For each backend, we keep the GSS status in a separate struct, that
    1000             :  * is only filled in if GSS is enabled.
    1001             :  *
    1002             :  * All char arrays must be null-terminated.
    1003             :  */
    1004             : typedef struct PgBackendGSSStatus
    1005             : {
    1006             :     /* Information about GSSAPI connection */
    1007             :     char        gss_princ[NAMEDATALEN]; /* GSSAPI Principal used to auth */
    1008             :     bool        gss_auth;       /* If GSSAPI authentication was used */
    1009             :     bool        gss_enc;        /* If encryption is being used */
    1010             : 
    1011             : } PgBackendGSSStatus;
    1012             : 
    1013             : 
    1014             : /* ----------
    1015             :  * PgBackendStatus
    1016             :  *
    1017             :  * Each live backend maintains a PgBackendStatus struct in shared memory
    1018             :  * showing its current activity.  (The structs are allocated according to
    1019             :  * BackendId, but that is not critical.)  Note that the collector process
    1020             :  * has no involvement in, or even access to, these structs.
    1021             :  *
    1022             :  * Each auxiliary process also maintains a PgBackendStatus struct in shared
    1023             :  * memory.
    1024             :  * ----------
    1025             :  */
    1026             : typedef struct PgBackendStatus
    1027             : {
    1028             :     /*
    1029             :      * To avoid locking overhead, we use the following protocol: a backend
    1030             :      * increments st_changecount before modifying its entry, and again after
    1031             :      * finishing a modification.  A would-be reader should note the value of
    1032             :      * st_changecount, copy the entry into private memory, then check
    1033             :      * st_changecount again.  If the value hasn't changed, and if it's even,
    1034             :      * the copy is valid; otherwise start over.  This makes updates cheap
    1035             :      * while reads are potentially expensive, but that's the tradeoff we want.
    1036             :      *
    1037             :      * The above protocol needs memory barriers to ensure that the apparent
    1038             :      * order of execution is as it desires.  Otherwise, for example, the CPU
    1039             :      * might rearrange the code so that st_changecount is incremented twice
    1040             :      * before the modification on a machine with weak memory ordering.  Hence,
    1041             :      * use the macros defined below for manipulating st_changecount, rather
    1042             :      * than touching it directly.
    1043             :      */
    1044             :     int         st_changecount;
    1045             : 
    1046             :     /* The entry is valid iff st_procpid > 0, unused if st_procpid == 0 */
    1047             :     int         st_procpid;
    1048             : 
    1049             :     /* Type of backends */
    1050             :     BackendType st_backendType;
    1051             : 
    1052             :     /* Times when current backend, transaction, and activity started */
    1053             :     TimestampTz st_proc_start_timestamp;
    1054             :     TimestampTz st_xact_start_timestamp;
    1055             :     TimestampTz st_activity_start_timestamp;
    1056             :     TimestampTz st_state_start_timestamp;
    1057             : 
    1058             :     /* Database OID, owning user's OID, connection client address */
    1059             :     Oid         st_databaseid;
    1060             :     Oid         st_userid;
    1061             :     SockAddr    st_clientaddr;
    1062             :     char       *st_clienthostname;  /* MUST be null-terminated */
    1063             : 
    1064             :     /* Information about SSL connection */
    1065             :     bool        st_ssl;
    1066             :     PgBackendSSLStatus *st_sslstatus;
    1067             : 
    1068             :     /* Information about GSSAPI connection */
    1069             :     bool        st_gss;
    1070             :     PgBackendGSSStatus *st_gssstatus;
    1071             : 
    1072             :     /* current state */
    1073             :     BackendState st_state;
    1074             : 
    1075             :     /* application name; MUST be null-terminated */
    1076             :     char       *st_appname;
    1077             : 
    1078             :     /*
    1079             :      * Current command string; MUST be null-terminated. Note that this string
    1080             :      * possibly is truncated in the middle of a multi-byte character. As
    1081             :      * activity strings are stored more frequently than read, that allows to
    1082             :      * move the cost of correct truncation to the display side. Use
    1083             :      * pgstat_clip_activity() to truncate correctly.
    1084             :      */
    1085             :     char       *st_activity_raw;
    1086             : 
    1087             :     /*
    1088             :      * Command progress reporting.  Any command which wishes can advertise
    1089             :      * that it is running by setting st_progress_command,
    1090             :      * st_progress_command_target, and st_progress_param[].
    1091             :      * st_progress_command_target should be the OID of the relation which the
    1092             :      * command targets (we assume there's just one, as this is meant for
    1093             :      * utility commands), but the meaning of each element in the
    1094             :      * st_progress_param array is command-specific.
    1095             :      */
    1096             :     ProgressCommandType st_progress_command;
    1097             :     Oid         st_progress_command_target;
    1098             :     int64       st_progress_param[PGSTAT_NUM_PROGRESS_PARAM];
    1099             : } PgBackendStatus;
    1100             : 
    1101             : /*
    1102             :  * Macros to load and store st_changecount with appropriate memory barriers.
    1103             :  *
    1104             :  * Use PGSTAT_BEGIN_WRITE_ACTIVITY() before, and PGSTAT_END_WRITE_ACTIVITY()
    1105             :  * after, modifying the current process's PgBackendStatus data.  Note that,
    1106             :  * since there is no mechanism for cleaning up st_changecount after an error,
    1107             :  * THESE MACROS FORM A CRITICAL SECTION.  Any error between them will be
    1108             :  * promoted to PANIC, causing a database restart to clean up shared memory!
    1109             :  * Hence, keep the critical section as short and straight-line as possible.
    1110             :  * Aside from being safer, that minimizes the window in which readers will
    1111             :  * have to loop.
    1112             :  *
    1113             :  * Reader logic should follow this sketch:
    1114             :  *
    1115             :  *      for (;;)
    1116             :  *      {
    1117             :  *          int before_ct, after_ct;
    1118             :  *
    1119             :  *          pgstat_begin_read_activity(beentry, before_ct);
    1120             :  *          ... copy beentry data to local memory ...
    1121             :  *          pgstat_end_read_activity(beentry, after_ct);
    1122             :  *          if (pgstat_read_activity_complete(before_ct, after_ct))
    1123             :  *              break;
    1124             :  *          CHECK_FOR_INTERRUPTS();
    1125             :  *      }
    1126             :  *
    1127             :  * For extra safety, we generally use volatile beentry pointers, although
    1128             :  * the memory barriers should theoretically be sufficient.
    1129             :  */
    1130             : #define PGSTAT_BEGIN_WRITE_ACTIVITY(beentry) \
    1131             :     do { \
    1132             :         START_CRIT_SECTION(); \
    1133             :         (beentry)->st_changecount++; \
    1134             :         pg_write_barrier(); \
    1135             :     } while (0)
    1136             : 
    1137             : #define PGSTAT_END_WRITE_ACTIVITY(beentry) \
    1138             :     do { \
    1139             :         pg_write_barrier(); \
    1140             :         (beentry)->st_changecount++; \
    1141             :         Assert(((beentry)->st_changecount & 1) == 0); \
    1142             :         END_CRIT_SECTION(); \
    1143             :     } while (0)
    1144             : 
    1145             : #define pgstat_begin_read_activity(beentry, before_changecount) \
    1146             :     do { \
    1147             :         (before_changecount) = (beentry)->st_changecount; \
    1148             :         pg_read_barrier(); \
    1149             :     } while (0)
    1150             : 
    1151             : #define pgstat_end_read_activity(beentry, after_changecount) \
    1152             :     do { \
    1153             :         pg_read_barrier(); \
    1154             :         (after_changecount) = (beentry)->st_changecount; \
    1155             :     } while (0)
    1156             : 
    1157             : #define pgstat_read_activity_complete(before_changecount, after_changecount) \
    1158             :     ((before_changecount) == (after_changecount) && \
    1159             :      ((before_changecount) & 1) == 0)
    1160             : 
    1161             : 
    1162             : /* ----------
    1163             :  * LocalPgBackendStatus
    1164             :  *
    1165             :  * When we build the backend status array, we use LocalPgBackendStatus to be
    1166             :  * able to add new values to the struct when needed without adding new fields
    1167             :  * to the shared memory. It contains the backend status as a first member.
    1168             :  * ----------
    1169             :  */
    1170             : typedef struct LocalPgBackendStatus
    1171             : {
    1172             :     /*
    1173             :      * Local version of the backend status entry.
    1174             :      */
    1175             :     PgBackendStatus backendStatus;
    1176             : 
    1177             :     /*
    1178             :      * The xid of the current transaction if available, InvalidTransactionId
    1179             :      * if not.
    1180             :      */
    1181             :     TransactionId backend_xid;
    1182             : 
    1183             :     /*
    1184             :      * The xmin of the current session if available, InvalidTransactionId if
    1185             :      * not.
    1186             :      */
    1187             :     TransactionId backend_xmin;
    1188             : } LocalPgBackendStatus;
    1189             : 
    1190             : /*
    1191             :  * Working state needed to accumulate per-function-call timing statistics.
    1192             :  */
    1193             : typedef struct PgStat_FunctionCallUsage
    1194             : {
    1195             :     /* Link to function's hashtable entry (must still be there at exit!) */
    1196             :     /* NULL means we are not tracking the current function call */
    1197             :     PgStat_FunctionCounts *fs;
    1198             :     /* Total time previously charged to function, as of function start */
    1199             :     instr_time  save_f_total_time;
    1200             :     /* Backend-wide total time as of function start */
    1201             :     instr_time  save_total;
    1202             :     /* system clock as of function start */
    1203             :     instr_time  f_start;
    1204             : } PgStat_FunctionCallUsage;
    1205             : 
    1206             : 
    1207             : /* ----------
    1208             :  * GUC parameters
    1209             :  * ----------
    1210             :  */
    1211             : extern bool pgstat_track_activities;
    1212             : extern bool pgstat_track_counts;
    1213             : extern int  pgstat_track_functions;
    1214             : extern PGDLLIMPORT int pgstat_track_activity_query_size;
    1215             : extern char *pgstat_stat_directory;
    1216             : extern char *pgstat_stat_tmpname;
    1217             : extern char *pgstat_stat_filename;
    1218             : 
    1219             : /*
    1220             :  * BgWriter statistics counters are updated directly by bgwriter and bufmgr
    1221             :  */
    1222             : extern PgStat_MsgBgWriter BgWriterStats;
    1223             : 
    1224             : /*
    1225             :  * Updated by pgstat_count_buffer_*_time macros
    1226             :  */
    1227             : extern PgStat_Counter pgStatBlockReadTime;
    1228             : extern PgStat_Counter pgStatBlockWriteTime;
    1229             : 
    1230             : /* ----------
    1231             :  * Functions called from postmaster
    1232             :  * ----------
    1233             :  */
    1234             : extern Size BackendStatusShmemSize(void);
    1235             : extern void CreateSharedBackendStatus(void);
    1236             : 
    1237             : extern void pgstat_init(void);
    1238             : extern int  pgstat_start(void);
    1239             : extern void pgstat_reset_all(void);
    1240             : extern void allow_immediate_pgstat_restart(void);
    1241             : 
    1242             : #ifdef EXEC_BACKEND
    1243             : extern void PgstatCollectorMain(int argc, char *argv[]) pg_attribute_noreturn();
    1244             : #endif
    1245             : 
    1246             : 
    1247             : /* ----------
    1248             :  * Functions called from backends
    1249             :  * ----------
    1250             :  */
    1251             : extern void pgstat_ping(void);
    1252             : 
    1253             : extern void pgstat_report_stat(bool force);
    1254             : extern void pgstat_vacuum_stat(void);
    1255             : extern void pgstat_drop_database(Oid databaseid);
    1256             : 
    1257             : extern void pgstat_clear_snapshot(void);
    1258             : extern void pgstat_reset_counters(void);
    1259             : extern void pgstat_reset_shared_counters(const char *);
    1260             : extern void pgstat_reset_single_counter(Oid objectid, PgStat_Single_Reset_Type type);
    1261             : 
    1262             : extern void pgstat_report_autovac(Oid dboid);
    1263             : extern void pgstat_report_vacuum(Oid tableoid, bool shared,
    1264             :                                  PgStat_Counter livetuples, PgStat_Counter deadtuples);
    1265             : extern void pgstat_report_analyze(Relation rel,
    1266             :                                   PgStat_Counter livetuples, PgStat_Counter deadtuples,
    1267             :                                   bool resetcounter);
    1268             : 
    1269             : extern void pgstat_report_recovery_conflict(int reason);
    1270             : extern void pgstat_report_deadlock(void);
    1271             : extern void pgstat_report_checksum_failures_in_db(Oid dboid, int failurecount);
    1272             : extern void pgstat_report_checksum_failure(void);
    1273             : 
    1274             : extern void pgstat_initialize(void);
    1275             : extern void pgstat_bestart(void);
    1276             : 
    1277             : extern void pgstat_report_activity(BackendState state, const char *cmd_str);
    1278             : extern void pgstat_report_tempfile(size_t filesize);
    1279             : extern void pgstat_report_appname(const char *appname);
    1280             : extern void pgstat_report_xact_timestamp(TimestampTz tstamp);
    1281             : extern const char *pgstat_get_wait_event(uint32 wait_event_info);
    1282             : extern const char *pgstat_get_wait_event_type(uint32 wait_event_info);
    1283             : extern const char *pgstat_get_backend_current_activity(int pid, bool checkUser);
    1284             : extern const char *pgstat_get_crashed_backend_activity(int pid, char *buffer,
    1285             :                                                        int buflen);
    1286             : extern const char *pgstat_get_backend_desc(BackendType backendType);
    1287             : 
    1288             : extern void pgstat_progress_start_command(ProgressCommandType cmdtype,
    1289             :                                           Oid relid);
    1290             : extern void pgstat_progress_update_param(int index, int64 val);
    1291             : extern void pgstat_progress_update_multi_param(int nparam, const int *index,
    1292             :                                                const int64 *val);
    1293             : extern void pgstat_progress_end_command(void);
    1294             : 
    1295             : extern PgStat_TableStatus *find_tabstat_entry(Oid rel_id);
    1296             : extern PgStat_BackendFunctionEntry *find_funcstat_entry(Oid func_id);
    1297             : 
    1298             : extern void pgstat_initstats(Relation rel);
    1299             : 
    1300             : extern char *pgstat_clip_activity(const char *raw_activity);
    1301             : 
    1302             : /* ----------
    1303             :  * pgstat_report_wait_start() -
    1304             :  *
    1305             :  *  Called from places where server process needs to wait.  This is called
    1306             :  *  to report wait event information.  The wait information is stored
    1307             :  *  as 4-bytes where first byte represents the wait event class (type of
    1308             :  *  wait, for different types of wait, refer WaitClass) and the next
    1309             :  *  3-bytes represent the actual wait event.  Currently 2-bytes are used
    1310             :  *  for wait event which is sufficient for current usage, 1-byte is
    1311             :  *  reserved for future usage.
    1312             :  *
    1313             :  * NB: this *must* be able to survive being called before MyProc has been
    1314             :  * initialized.
    1315             :  * ----------
    1316             :  */
    1317             : static inline void
    1318     7057120 : pgstat_report_wait_start(uint32 wait_event_info)
    1319             : {
    1320     7057120 :     volatile PGPROC *proc = MyProc;
    1321             : 
    1322     7057120 :     if (!pgstat_track_activities || !proc)
    1323       95720 :         return;
    1324             : 
    1325             :     /*
    1326             :      * Since this is a four-byte field which is always read and written as
    1327             :      * four-bytes, updates are atomic.
    1328             :      */
    1329     6961400 :     proc->wait_event_info = wait_event_info;
    1330             : }
    1331             : 
    1332             : /* ----------
    1333             :  * pgstat_report_wait_end() -
    1334             :  *
    1335             :  *  Called to report end of a wait.
    1336             :  *
    1337             :  * NB: this *must* be able to survive being called before MyProc has been
    1338             :  * initialized.
    1339             :  * ----------
    1340             :  */
    1341             : static inline void
    1342     7080982 : pgstat_report_wait_end(void)
    1343             : {
    1344     7080982 :     volatile PGPROC *proc = MyProc;
    1345             : 
    1346     7080982 :     if (!pgstat_track_activities || !proc)
    1347       95710 :         return;
    1348             : 
    1349             :     /*
    1350             :      * Since this is a four-byte field which is always read and written as
    1351             :      * four-bytes, updates are atomic.
    1352             :      */
    1353     6985272 :     proc->wait_event_info = 0;
    1354             : }
    1355             : 
    1356             : /* nontransactional event counts are simple enough to inline */
    1357             : 
    1358             : #define pgstat_count_heap_scan(rel)                                 \
    1359             :     do {                                                            \
    1360             :         if ((rel)->pgstat_info != NULL)                              \
    1361             :             (rel)->pgstat_info->t_counts.t_numscans++;                \
    1362             :     } while (0)
    1363             : #define pgstat_count_heap_getnext(rel)                              \
    1364             :     do {                                                            \
    1365             :         if ((rel)->pgstat_info != NULL)                              \
    1366             :             (rel)->pgstat_info->t_counts.t_tuples_returned++;     \
    1367             :     } while (0)
    1368             : #define pgstat_count_heap_fetch(rel)                                \
    1369             :     do {                                                            \
    1370             :         if ((rel)->pgstat_info != NULL)                              \
    1371             :             (rel)->pgstat_info->t_counts.t_tuples_fetched++;      \
    1372             :     } while (0)
    1373             : #define pgstat_count_index_scan(rel)                                \
    1374             :     do {                                                            \
    1375             :         if ((rel)->pgstat_info != NULL)                              \
    1376             :             (rel)->pgstat_info->t_counts.t_numscans++;                \
    1377             :     } while (0)
    1378             : #define pgstat_count_index_tuples(rel, n)                           \
    1379             :     do {                                                            \
    1380             :         if ((rel)->pgstat_info != NULL)                              \
    1381             :             (rel)->pgstat_info->t_counts.t_tuples_returned += (n);    \
    1382             :     } while (0)
    1383             : #define pgstat_count_buffer_read(rel)                               \
    1384             :     do {                                                            \
    1385             :         if ((rel)->pgstat_info != NULL)                              \
    1386             :             (rel)->pgstat_info->t_counts.t_blocks_fetched++;      \
    1387             :     } while (0)
    1388             : #define pgstat_count_buffer_hit(rel)                                \
    1389             :     do {                                                            \
    1390             :         if ((rel)->pgstat_info != NULL)                              \
    1391             :             (rel)->pgstat_info->t_counts.t_blocks_hit++;          \
    1392             :     } while (0)
    1393             : #define pgstat_count_buffer_read_time(n)                            \
    1394             :     (pgStatBlockReadTime += (n))
    1395             : #define pgstat_count_buffer_write_time(n)                           \
    1396             :     (pgStatBlockWriteTime += (n))
    1397             : 
    1398             : extern void pgstat_count_heap_insert(Relation rel, PgStat_Counter n);
    1399             : extern void pgstat_count_heap_update(Relation rel, bool hot);
    1400             : extern void pgstat_count_heap_delete(Relation rel);
    1401             : extern void pgstat_count_truncate(Relation rel);
    1402             : extern void pgstat_update_heap_dead_tuples(Relation rel, int delta);
    1403             : 
    1404             : struct FunctionCallInfoBaseData;
    1405             : extern void pgstat_init_function_usage(struct FunctionCallInfoBaseData *fcinfo,
    1406             :                                        PgStat_FunctionCallUsage *fcu);
    1407             : extern void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu,
    1408             :                                       bool finalize);
    1409             : 
    1410             : extern void AtEOXact_PgStat(bool isCommit, bool parallel);
    1411             : extern void AtEOSubXact_PgStat(bool isCommit, int nestDepth);
    1412             : 
    1413             : extern void AtPrepare_PgStat(void);
    1414             : extern void PostPrepare_PgStat(void);
    1415             : 
    1416             : extern void pgstat_twophase_postcommit(TransactionId xid, uint16 info,
    1417             :                                        void *recdata, uint32 len);
    1418             : extern void pgstat_twophase_postabort(TransactionId xid, uint16 info,
    1419             :                                       void *recdata, uint32 len);
    1420             : 
    1421             : extern void pgstat_send_archiver(const char *xlog, bool failed);
    1422             : extern void pgstat_send_bgwriter(void);
    1423             : 
    1424             : /* ----------
    1425             :  * Support functions for the SQL-callable functions to
    1426             :  * generate the pgstat* views.
    1427             :  * ----------
    1428             :  */
    1429             : extern PgStat_StatDBEntry *pgstat_fetch_stat_dbentry(Oid dbid);
    1430             : extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry(Oid relid);
    1431             : extern PgBackendStatus *pgstat_fetch_stat_beentry(int beid);
    1432             : extern LocalPgBackendStatus *pgstat_fetch_stat_local_beentry(int beid);
    1433             : extern PgStat_StatFuncEntry *pgstat_fetch_stat_funcentry(Oid funcid);
    1434             : extern int  pgstat_fetch_stat_numbackends(void);
    1435             : extern PgStat_ArchiverStats *pgstat_fetch_stat_archiver(void);
    1436             : extern PgStat_GlobalStats *pgstat_fetch_global(void);
    1437             : 
    1438             : #endif                          /* PGSTAT_H */

Generated by: LCOV version 1.13