LCOV - code coverage report
Current view: top level - src/backend/utils/activity - pgstat.c (source / functions) Hit Total Coverage
Test: PostgreSQL 16beta1 Lines: 359 395 90.9 %
Date: 2023-06-02 17:12:27 Functions: 32 32 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* ----------
       2             :  * pgstat.c
       3             :  *    Infrastructure for the cumulative statistics system.
       4             :  *
       5             :  * The cumulative statistics system accumulates statistics for different kinds
       6             :  * of objects. Some kinds of statistics are collected for a fixed number of
       7             :  * objects (most commonly 1), e.g., checkpointer statistics. Other kinds of
       8             :  * statistics are collected for a varying number of objects
       9             :  * (e.g. relations). See PgStat_KindInfo for a list of currently handled
      10             :  * statistics.
      11             :  *
      12             :  * Statistics are loaded from the filesystem during startup (by the startup
      13             :  * process), unless preceded by a crash, in which case all stats are
      14             :  * discarded. They are written out by the checkpointer process just before
      15             :  * shutting down, except when shutting down in immediate mode.
      16             :  *
      17             :  * Fixed-numbered stats are stored in plain (non-dynamic) shared memory.
      18             :  *
      19             :  * Statistics for variable-numbered objects are stored in dynamic shared
      20             :  * memory and can be found via a dshash hashtable. The statistics counters are
      21             :  * not part of the dshash entry (PgStatShared_HashEntry) directly, but are
      22             :  * separately allocated (PgStatShared_HashEntry->body). The separate
      23             :  * allocation allows different kinds of statistics to be stored in the same
      24             :  * hashtable without wasting space in PgStatShared_HashEntry.
      25             :  *
      26             :  * Variable-numbered stats are addressed by PgStat_HashKey while running.  It
      27             :  * is not possible to have statistics for an object that cannot be addressed
      28             :  * that way at runtime. A wider identifier can be used when serializing to
      29             :  * disk (used for replication slot stats).
      30             :  *
      31             :  * To avoid contention on the shared hashtable, each backend has a
      32             :  * backend-local hashtable (pgStatEntryRefHash) in front of the shared
      33             :  * hashtable, containing references (PgStat_EntryRef) to shared hashtable
      34             :  * entries. The shared hashtable only needs to be accessed when no prior
      35             :  * reference is found in the local hashtable. Besides pointing to the
      36             :  * shared hashtable entry (PgStatShared_HashEntry) PgStat_EntryRef also
      37             :  * contains a pointer to the shared statistics data, as a process-local
      38             :  * address, to reduce access costs.
      39             :  *
      40             :  * The names for structs stored in shared memory are prefixed with
      41             :  * PgStatShared instead of PgStat. Each stats entry in shared memory is
      42             :  * protected by a dedicated lwlock.
      43             :  *
      44             :  * Most stats updates are first accumulated locally in each process as pending
      45             :  * entries, then later flushed to shared memory (just after commit, or by
      46             :  * idle-timeout). This practically eliminates contention on individual stats
      47             :  * entries. For most kinds of variable-numbered pending stats data is stored
      48             :  * in PgStat_EntryRef->pending. All entries with pending data are in the
      49             :  * pgStatPending list. Pending statistics updates are flushed out by
      50             :  * pgstat_report_stat().
      51             :  *
      52             :  * The behavior of different kinds of statistics is determined by the kind's
      53             :  * entry in pgstat_kind_infos, see PgStat_KindInfo for details.
      54             :  *
      55             :  * The consistency of read accesses to statistics can be configured using the
      56             :  * stats_fetch_consistency GUC (see config.sgml and monitoring.sgml for the
      57             :  * settings). When using PGSTAT_FETCH_CONSISTENCY_CACHE or
      58             :  * PGSTAT_FETCH_CONSISTENCY_SNAPSHOT statistics are stored in
      59             :  * pgStatLocal.snapshot.
      60             :  *
      61             :  * To keep things manageable, stats handling is split across several
      62             :  * files. Infrastructure pieces are in:
      63             :  * - pgstat.c - this file, to tie it all together
      64             :  * - pgstat_shmem.c - nearly everything dealing with shared memory, including
      65             :  *   the maintenance of hashtable entries
      66             :  * - pgstat_xact.c - transactional integration, including the transactional
      67             :  *   creation and dropping of stats entries
      68             :  *
      69             :  * Each statistics kind is handled in a dedicated file:
      70             :  * - pgstat_archiver.c
      71             :  * - pgstat_bgwriter.c
      72             :  * - pgstat_checkpointer.c
      73             :  * - pgstat_database.c
      74             :  * - pgstat_function.c
      75             :  * - pgstat_io.c
      76             :  * - pgstat_relation.c
      77             :  * - pgstat_replslot.c
      78             :  * - pgstat_slru.c
      79             :  * - pgstat_subscription.c
      80             :  * - pgstat_wal.c
      81             :  *
      82             :  * Whenever possible infrastructure files should not contain code related to
      83             :  * specific kinds of stats.
      84             :  *
      85             :  *
      86             :  * Copyright (c) 2001-2023, PostgreSQL Global Development Group
      87             :  *
      88             :  * IDENTIFICATION
      89             :  *    src/backend/utils/activity/pgstat.c
      90             :  * ----------
      91             :  */
      92             : #include "postgres.h"
      93             : 
      94             : #include <unistd.h>
      95             : 
      96             : #include "access/transam.h"
      97             : #include "access/xact.h"
      98             : #include "lib/dshash.h"
      99             : #include "pgstat.h"
     100             : #include "port/atomics.h"
     101             : #include "storage/fd.h"
     102             : #include "storage/ipc.h"
     103             : #include "storage/lwlock.h"
     104             : #include "storage/pg_shmem.h"
     105             : #include "storage/shmem.h"
     106             : #include "utils/guc_hooks.h"
     107             : #include "utils/memutils.h"
     108             : #include "utils/pgstat_internal.h"
     109             : #include "utils/timestamp.h"
     110             : 
     111             : 
     112             : /* ----------
     113             :  * Timer definitions.
     114             :  *
     115             :  * In milliseconds.
     116             :  * ----------
     117             :  */
     118             : 
     119             : /* minimum interval non-forced stats flushes.*/
     120             : #define PGSTAT_MIN_INTERVAL         1000
     121             : /* how long until to block flushing pending stats updates */
     122             : #define PGSTAT_MAX_INTERVAL         60000
     123             : /* when to call pgstat_report_stat() again, even when idle */
     124             : #define PGSTAT_IDLE_INTERVAL        10000
     125             : 
     126             : /* ----------
     127             :  * Initial size hints for the hash tables used in statistics.
     128             :  * ----------
     129             :  */
     130             : 
     131             : #define PGSTAT_SNAPSHOT_HASH_SIZE   512
     132             : 
     133             : 
     134             : /* hash table for statistics snapshots entry */
     135             : typedef struct PgStat_SnapshotEntry
     136             : {
     137             :     PgStat_HashKey key;
     138             :     char        status;         /* for simplehash use */
     139             :     void       *data;           /* the stats data itself */
     140             : } PgStat_SnapshotEntry;
     141             : 
     142             : 
     143             : /* ----------
     144             :  * Backend-local Hash Table Definitions
     145             :  * ----------
     146             :  */
     147             : 
     148             : /* for stats snapshot entries */
     149             : #define SH_PREFIX pgstat_snapshot
     150             : #define SH_ELEMENT_TYPE PgStat_SnapshotEntry
     151             : #define SH_KEY_TYPE PgStat_HashKey
     152             : #define SH_KEY key
     153             : #define SH_HASH_KEY(tb, key) \
     154             :     pgstat_hash_hash_key(&key, sizeof(PgStat_HashKey), NULL)
     155             : #define SH_EQUAL(tb, a, b) \
     156             :     pgstat_cmp_hash_key(&a, &b, sizeof(PgStat_HashKey), NULL) == 0
     157             : #define SH_SCOPE static inline
     158             : #define SH_DEFINE
     159             : #define SH_DECLARE
     160             : #include "lib/simplehash.h"
     161             : 
     162             : 
     163             : /* ----------
     164             :  * Local function forward declarations
     165             :  * ----------
     166             :  */
     167             : 
     168             : static void pgstat_write_statsfile(void);
     169             : static void pgstat_read_statsfile(void);
     170             : 
     171             : static void pgstat_reset_after_failure(void);
     172             : 
     173             : static bool pgstat_flush_pending_entries(bool nowait);
     174             : 
     175             : static void pgstat_prep_snapshot(void);
     176             : static void pgstat_build_snapshot(void);
     177             : static void pgstat_build_snapshot_fixed(PgStat_Kind kind);
     178             : 
     179             : static inline bool pgstat_is_kind_valid(int ikind);
     180             : 
     181             : 
     182             : /* ----------
     183             :  * GUC parameters
     184             :  * ----------
     185             :  */
     186             : 
     187             : bool        pgstat_track_counts = false;
     188             : int         pgstat_fetch_consistency = PGSTAT_FETCH_CONSISTENCY_CACHE;
     189             : 
     190             : 
     191             : /* ----------
     192             :  * state shared with pgstat_*.c
     193             :  * ----------
     194             :  */
     195             : 
     196             : PgStat_LocalState pgStatLocal;
     197             : 
     198             : 
     199             : /* ----------
     200             :  * Local data
     201             :  *
     202             :  * NB: There should be only variables related to stats infrastructure here,
     203             :  * not for specific kinds of stats.
     204             :  * ----------
     205             :  */
     206             : 
     207             : /*
     208             :  * Memory contexts containing the pgStatEntryRefHash table, the
     209             :  * pgStatSharedRef entries, and pending data respectively. Mostly to make it
     210             :  * easier to track / attribute memory usage.
     211             :  */
     212             : 
     213             : static MemoryContext pgStatPendingContext = NULL;
     214             : 
     215             : /*
     216             :  * Backend local list of PgStat_EntryRef with unflushed pending stats.
     217             :  *
     218             :  * Newly pending entries should only ever be added to the end of the list,
     219             :  * otherwise pgstat_flush_pending_entries() might not see them immediately.
     220             :  */
     221             : static dlist_head pgStatPending = DLIST_STATIC_INIT(pgStatPending);
     222             : 
     223             : 
     224             : /*
     225             :  * Force the next stats flush to happen regardless of
     226             :  * PGSTAT_MIN_INTERVAL. Useful in test scripts.
     227             :  */
     228             : static bool pgStatForceNextFlush = false;
     229             : 
     230             : /*
     231             :  * Force-clear existing snapshot before next use when stats_fetch_consistency
     232             :  * is changed.
     233             :  */
     234             : static bool force_stats_snapshot_clear = false;
     235             : 
     236             : 
     237             : /*
     238             :  * For assertions that check pgstat is not used before initialization / after
     239             :  * shutdown.
     240             :  */
     241             : #ifdef USE_ASSERT_CHECKING
     242             : static bool pgstat_is_initialized = false;
     243             : static bool pgstat_is_shutdown = false;
     244             : #endif
     245             : 
     246             : 
     247             : /*
     248             :  * The different kinds of statistics.
     249             :  *
     250             :  * If reasonably possible, handling specific to one kind of stats should go
     251             :  * through this abstraction, rather than making more of pgstat.c aware.
     252             :  *
     253             :  * See comments for struct PgStat_KindInfo for details about the individual
     254             :  * fields.
     255             :  *
     256             :  * XXX: It'd be nicer to define this outside of this file. But there doesn't
     257             :  * seem to be a great way of doing that, given the split across multiple
     258             :  * files.
     259             :  */
     260             : static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
     261             : 
     262             :     /* stats kinds for variable-numbered objects */
     263             : 
     264             :     [PGSTAT_KIND_DATABASE] = {
     265             :         .name = "database",
     266             : 
     267             :         .fixed_amount = false,
     268             :         /* so pg_stat_database entries can be seen in all databases */
     269             :         .accessed_across_databases = true,
     270             : 
     271             :         .shared_size = sizeof(PgStatShared_Database),
     272             :         .shared_data_off = offsetof(PgStatShared_Database, stats),
     273             :         .shared_data_len = sizeof(((PgStatShared_Database *) 0)->stats),
     274             :         .pending_size = sizeof(PgStat_StatDBEntry),
     275             : 
     276             :         .flush_pending_cb = pgstat_database_flush_cb,
     277             :         .reset_timestamp_cb = pgstat_database_reset_timestamp_cb,
     278             :     },
     279             : 
     280             :     [PGSTAT_KIND_RELATION] = {
     281             :         .name = "relation",
     282             : 
     283             :         .fixed_amount = false,
     284             : 
     285             :         .shared_size = sizeof(PgStatShared_Relation),
     286             :         .shared_data_off = offsetof(PgStatShared_Relation, stats),
     287             :         .shared_data_len = sizeof(((PgStatShared_Relation *) 0)->stats),
     288             :         .pending_size = sizeof(PgStat_TableStatus),
     289             : 
     290             :         .flush_pending_cb = pgstat_relation_flush_cb,
     291             :         .delete_pending_cb = pgstat_relation_delete_pending_cb,
     292             :     },
     293             : 
     294             :     [PGSTAT_KIND_FUNCTION] = {
     295             :         .name = "function",
     296             : 
     297             :         .fixed_amount = false,
     298             : 
     299             :         .shared_size = sizeof(PgStatShared_Function),
     300             :         .shared_data_off = offsetof(PgStatShared_Function, stats),
     301             :         .shared_data_len = sizeof(((PgStatShared_Function *) 0)->stats),
     302             :         .pending_size = sizeof(PgStat_FunctionCounts),
     303             : 
     304             :         .flush_pending_cb = pgstat_function_flush_cb,
     305             :     },
     306             : 
     307             :     [PGSTAT_KIND_REPLSLOT] = {
     308             :         .name = "replslot",
     309             : 
     310             :         .fixed_amount = false,
     311             : 
     312             :         .accessed_across_databases = true,
     313             :         .named_on_disk = true,
     314             : 
     315             :         .shared_size = sizeof(PgStatShared_ReplSlot),
     316             :         .shared_data_off = offsetof(PgStatShared_ReplSlot, stats),
     317             :         .shared_data_len = sizeof(((PgStatShared_ReplSlot *) 0)->stats),
     318             : 
     319             :         .reset_timestamp_cb = pgstat_replslot_reset_timestamp_cb,
     320             :         .to_serialized_name = pgstat_replslot_to_serialized_name_cb,
     321             :         .from_serialized_name = pgstat_replslot_from_serialized_name_cb,
     322             :     },
     323             : 
     324             :     [PGSTAT_KIND_SUBSCRIPTION] = {
     325             :         .name = "subscription",
     326             : 
     327             :         .fixed_amount = false,
     328             :         /* so pg_stat_subscription_stats entries can be seen in all databases */
     329             :         .accessed_across_databases = true,
     330             : 
     331             :         .shared_size = sizeof(PgStatShared_Subscription),
     332             :         .shared_data_off = offsetof(PgStatShared_Subscription, stats),
     333             :         .shared_data_len = sizeof(((PgStatShared_Subscription *) 0)->stats),
     334             :         .pending_size = sizeof(PgStat_BackendSubEntry),
     335             : 
     336             :         .flush_pending_cb = pgstat_subscription_flush_cb,
     337             :         .reset_timestamp_cb = pgstat_subscription_reset_timestamp_cb,
     338             :     },
     339             : 
     340             : 
     341             :     /* stats for fixed-numbered (mostly 1) objects */
     342             : 
     343             :     [PGSTAT_KIND_ARCHIVER] = {
     344             :         .name = "archiver",
     345             : 
     346             :         .fixed_amount = true,
     347             : 
     348             :         .reset_all_cb = pgstat_archiver_reset_all_cb,
     349             :         .snapshot_cb = pgstat_archiver_snapshot_cb,
     350             :     },
     351             : 
     352             :     [PGSTAT_KIND_BGWRITER] = {
     353             :         .name = "bgwriter",
     354             : 
     355             :         .fixed_amount = true,
     356             : 
     357             :         .reset_all_cb = pgstat_bgwriter_reset_all_cb,
     358             :         .snapshot_cb = pgstat_bgwriter_snapshot_cb,
     359             :     },
     360             : 
     361             :     [PGSTAT_KIND_CHECKPOINTER] = {
     362             :         .name = "checkpointer",
     363             : 
     364             :         .fixed_amount = true,
     365             : 
     366             :         .reset_all_cb = pgstat_checkpointer_reset_all_cb,
     367             :         .snapshot_cb = pgstat_checkpointer_snapshot_cb,
     368             :     },
     369             : 
     370             :     [PGSTAT_KIND_IO] = {
     371             :         .name = "io",
     372             : 
     373             :         .fixed_amount = true,
     374             : 
     375             :         .reset_all_cb = pgstat_io_reset_all_cb,
     376             :         .snapshot_cb = pgstat_io_snapshot_cb,
     377             :     },
     378             : 
     379             :     [PGSTAT_KIND_SLRU] = {
     380             :         .name = "slru",
     381             : 
     382             :         .fixed_amount = true,
     383             : 
     384             :         .reset_all_cb = pgstat_slru_reset_all_cb,
     385             :         .snapshot_cb = pgstat_slru_snapshot_cb,
     386             :     },
     387             : 
     388             :     [PGSTAT_KIND_WAL] = {
     389             :         .name = "wal",
     390             : 
     391             :         .fixed_amount = true,
     392             : 
     393             :         .reset_all_cb = pgstat_wal_reset_all_cb,
     394             :         .snapshot_cb = pgstat_wal_snapshot_cb,
     395             :     },
     396             : };
     397             : 
     398             : 
     399             : /* ------------------------------------------------------------
     400             :  * Functions managing the state of the stats system for all backends.
     401             :  * ------------------------------------------------------------
     402             :  */
     403             : 
     404             : /*
     405             :  * Read on-disk stats into memory at server start.
     406             :  *
     407             :  * Should only be called by the startup process or in single user mode.
     408             :  */
     409             : void
     410        2066 : pgstat_restore_stats(void)
     411             : {
     412        2066 :     pgstat_read_statsfile();
     413        2066 : }
     414             : 
     415             : /*
     416             :  * Remove the stats file.  This is currently used only if WAL recovery is
     417             :  * needed after a crash.
     418             :  *
     419             :  * Should only be called by the startup process or in single user mode.
     420             :  */
     421             : void
     422         272 : pgstat_discard_stats(void)
     423             : {
     424             :     int         ret;
     425             : 
     426             :     /* NB: this needs to be done even in single user mode */
     427             : 
     428         272 :     ret = unlink(PGSTAT_STAT_PERMANENT_FILENAME);
     429         272 :     if (ret != 0)
     430             :     {
     431         270 :         if (errno == ENOENT)
     432         270 :             elog(DEBUG2,
     433             :                  "didn't need to unlink permanent stats file \"%s\" - didn't exist",
     434             :                  PGSTAT_STAT_PERMANENT_FILENAME);
     435             :         else
     436           0 :             ereport(LOG,
     437             :                     (errcode_for_file_access(),
     438             :                      errmsg("could not unlink permanent statistics file \"%s\": %m",
     439             :                             PGSTAT_STAT_PERMANENT_FILENAME)));
     440             :     }
     441             :     else
     442             :     {
     443           2 :         ereport(DEBUG2,
     444             :                 (errcode_for_file_access(),
     445             :                  errmsg_internal("unlinked permanent statistics file \"%s\"",
     446             :                                  PGSTAT_STAT_PERMANENT_FILENAME)));
     447             :     }
     448             : 
     449             :     /*
     450             :      * Reset stats contents. This will set reset timestamps of fixed-numbered
     451             :      * stats to the current time (no variable stats exist).
     452             :      */
     453         272 :     pgstat_reset_after_failure();
     454         272 : }
     455             : 
     456             : /*
     457             :  * pgstat_before_server_shutdown() needs to be called by exactly one process
     458             :  * during regular server shutdowns. Otherwise all stats will be lost.
     459             :  *
     460             :  * We currently only write out stats for proc_exit(0). We might want to change
     461             :  * that at some point... But right now pgstat_discard_stats() would be called
     462             :  * during the start after a disorderly shutdown, anyway.
     463             :  */
     464             : void
     465        1924 : pgstat_before_server_shutdown(int code, Datum arg)
     466             : {
     467             :     Assert(pgStatLocal.shmem != NULL);
     468             :     Assert(!pgStatLocal.shmem->is_shutdown);
     469             : 
     470             :     /*
     471             :      * Stats should only be reported after pgstat_initialize() and before
     472             :      * pgstat_shutdown(). This is a convenient point to catch most violations
     473             :      * of this rule.
     474             :      */
     475             :     Assert(pgstat_is_initialized && !pgstat_is_shutdown);
     476             : 
     477             :     /* flush out our own pending changes before writing out */
     478        1924 :     pgstat_report_stat(true);
     479             : 
     480             :     /*
     481             :      * Only write out file during normal shutdown. Don't even signal that
     482             :      * we've shutdown during irregular shutdowns, because the shutdown
     483             :      * sequence isn't coordinated to ensure this backend shuts down last.
     484             :      */
     485        1924 :     if (code == 0)
     486             :     {
     487        1914 :         pgStatLocal.shmem->is_shutdown = true;
     488        1914 :         pgstat_write_statsfile();
     489             :     }
     490        1924 : }
     491             : 
     492             : 
     493             : /* ------------------------------------------------------------
     494             :  * Backend initialization / shutdown functions
     495             :  * ------------------------------------------------------------
     496             :  */
     497             : 
     498             : /*
     499             :  * Shut down a single backend's statistics reporting at process exit.
     500             :  *
     501             :  * Flush out any remaining statistics counts.  Without this, operations
     502             :  * triggered during backend exit (such as temp table deletions) won't be
     503             :  * counted.
     504             :  */
     505             : static void
     506       27400 : pgstat_shutdown_hook(int code, Datum arg)
     507             : {
     508             :     Assert(!pgstat_is_shutdown);
     509             :     Assert(IsUnderPostmaster || !IsPostmasterEnvironment);
     510             : 
     511             :     /*
     512             :      * If we got as far as discovering our own database ID, we can flush out
     513             :      * what we did so far.  Otherwise, we'd be reporting an invalid database
     514             :      * ID, so forget it.  (This means that accesses to pg_database during
     515             :      * failed backend starts might never get counted.)
     516             :      */
     517       27400 :     if (OidIsValid(MyDatabaseId))
     518       21848 :         pgstat_report_disconnect(MyDatabaseId);
     519             : 
     520       27400 :     pgstat_report_stat(true);
     521             : 
     522             :     /* there shouldn't be any pending changes left */
     523             :     Assert(dlist_is_empty(&pgStatPending));
     524       27400 :     dlist_init(&pgStatPending);
     525             : 
     526       27400 :     pgstat_detach_shmem();
     527             : 
     528             : #ifdef USE_ASSERT_CHECKING
     529             :     pgstat_is_shutdown = true;
     530             : #endif
     531       27400 : }
     532             : 
     533             : /*
     534             :  * Initialize pgstats state, and set up our on-proc-exit hook. Called from
     535             :  * BaseInit().
     536             :  *
     537             :  * NOTE: MyDatabaseId isn't set yet; so the shutdown hook has to be careful.
     538             :  */
     539             : void
     540       27400 : pgstat_initialize(void)
     541             : {
     542             :     Assert(!pgstat_is_initialized);
     543             : 
     544       27400 :     pgstat_attach_shmem();
     545             : 
     546       27400 :     pgstat_init_wal();
     547             : 
     548             :     /* Set up a process-exit hook to clean up */
     549       27400 :     before_shmem_exit(pgstat_shutdown_hook, 0);
     550             : 
     551             : #ifdef USE_ASSERT_CHECKING
     552             :     pgstat_is_initialized = true;
     553             : #endif
     554       27400 : }
     555             : 
     556             : 
     557             : /* ------------------------------------------------------------
     558             :  * Public functions used by backends follow
     559             :  * ------------------------------------------------------------
     560             :  */
     561             : 
     562             : /*
     563             :  * Must be called by processes that performs DML: tcop/postgres.c, logical
     564             :  * receiver processes, SPI worker, etc. to flush pending statistics updates to
     565             :  * shared memory.
     566             :  *
     567             :  * Unless called with 'force', pending stats updates are flushed happen once
     568             :  * per PGSTAT_MIN_INTERVAL (1000ms). When not forced, stats flushes do not
     569             :  * block on lock acquisition, except if stats updates have been pending for
     570             :  * longer than PGSTAT_MAX_INTERVAL (60000ms).
     571             :  *
     572             :  * Whenever pending stats updates remain at the end of pgstat_report_stat() a
     573             :  * suggested idle timeout is returned. Currently this is always
     574             :  * PGSTAT_IDLE_INTERVAL (10000ms). Callers can use the returned time to set up
     575             :  * a timeout after which to call pgstat_report_stat(true), but are not
     576             :  * required to do so.
     577             :  *
     578             :  * Note that this is called only when not within a transaction, so it is fair
     579             :  * to use transaction stop time as an approximation of current time.
     580             :  */
     581             : long
     582      848572 : pgstat_report_stat(bool force)
     583             : {
     584             :     static TimestampTz pending_since = 0;
     585             :     static TimestampTz last_flush = 0;
     586             :     bool        partial_flush;
     587             :     TimestampTz now;
     588             :     bool        nowait;
     589             : 
     590             :     pgstat_assert_is_up();
     591             :     Assert(!IsTransactionOrTransactionBlock());
     592             : 
     593             :     /* "absorb" the forced flush even if there's nothing to flush */
     594      848572 :     if (pgStatForceNextFlush)
     595             :     {
     596         402 :         force = true;
     597         402 :         pgStatForceNextFlush = false;
     598             :     }
     599             : 
     600             :     /* Don't expend a clock check if nothing to do */
     601      848572 :     if (dlist_is_empty(&pgStatPending) &&
     602       13822 :         !have_iostats &&
     603       13582 :         !have_slrustats &&
     604       11994 :         !pgstat_have_pending_wal())
     605             :     {
     606             :         Assert(pending_since == 0);
     607       11908 :         return 0;
     608             :     }
     609             : 
     610             :     /*
     611             :      * There should never be stats to report once stats are shut down. Can't
     612             :      * assert that before the checks above, as there is an unconditional
     613             :      * pgstat_report_stat() call in pgstat_shutdown_hook() - which at least
     614             :      * the process that ran pgstat_before_server_shutdown() will still call.
     615             :      */
     616             :     Assert(!pgStatLocal.shmem->is_shutdown);
     617             : 
     618      836664 :     now = GetCurrentTransactionStopTimestamp();
     619             : 
     620      836664 :     if (!force)
     621             :     {
     622     1580030 :         if (pending_since > 0 &&
     623      770758 :             TimestampDifferenceExceeds(pending_since, now, PGSTAT_MAX_INTERVAL))
     624             :         {
     625             :             /* don't keep pending updates longer than PGSTAT_MAX_INTERVAL */
     626           0 :             force = true;
     627             :         }
     628      809272 :         else if (last_flush > 0 &&
     629      790602 :                  !TimestampDifferenceExceeds(last_flush, now, PGSTAT_MIN_INTERVAL))
     630             :         {
     631             :             /* don't flush too frequently */
     632      789112 :             if (pending_since == 0)
     633       19806 :                 pending_since = now;
     634             : 
     635      789112 :             return PGSTAT_IDLE_INTERVAL;
     636             :         }
     637             :     }
     638             : 
     639       47552 :     pgstat_update_dbstats(now);
     640             : 
     641             :     /* don't wait for lock acquisition when !force */
     642       47552 :     nowait = !force;
     643             : 
     644       47552 :     partial_flush = false;
     645             : 
     646             :     /* flush database / relation / function / ... stats */
     647       47552 :     partial_flush |= pgstat_flush_pending_entries(nowait);
     648             : 
     649             :     /* flush IO stats */
     650       47552 :     partial_flush |= pgstat_flush_io(nowait);
     651             : 
     652             :     /* flush wal stats */
     653       47552 :     partial_flush |= pgstat_flush_wal(nowait);
     654             : 
     655             :     /* flush SLRU stats */
     656       47552 :     partial_flush |= pgstat_slru_flush(nowait);
     657             : 
     658       47552 :     last_flush = now;
     659             : 
     660             :     /*
     661             :      * If some of the pending stats could not be flushed due to lock
     662             :      * contention, let the caller know when to retry.
     663             :      */
     664       47552 :     if (partial_flush)
     665             :     {
     666             :         /* force should have prevented us from getting here */
     667             :         Assert(!force);
     668             : 
     669             :         /* remember since when stats have been pending */
     670           0 :         if (pending_since == 0)
     671           0 :             pending_since = now;
     672             : 
     673           0 :         return PGSTAT_IDLE_INTERVAL;
     674             :     }
     675             : 
     676       47552 :     pending_since = 0;
     677             : 
     678       47552 :     return 0;
     679             : }
     680             : 
     681             : /*
     682             :  * Force locally pending stats to be flushed during the next
     683             :  * pgstat_report_stat() call. This is useful for writing tests.
     684             :  */
     685             : void
     686         402 : pgstat_force_next_flush(void)
     687             : {
     688         402 :     pgStatForceNextFlush = true;
     689         402 : }
     690             : 
     691             : /*
     692             :  * Only for use by pgstat_reset_counters()
     693             :  */
     694             : static bool
     695       21044 : match_db_entries(PgStatShared_HashEntry *entry, Datum match_data)
     696             : {
     697       21044 :     return entry->key.dboid == DatumGetObjectId(MyDatabaseId);
     698             : }
     699             : 
     700             : /*
     701             :  * Reset counters for our database.
     702             :  *
     703             :  * Permission checking for this function is managed through the normal
     704             :  * GRANT system.
     705             :  */
     706             : void
     707          26 : pgstat_reset_counters(void)
     708             : {
     709          26 :     TimestampTz ts = GetCurrentTimestamp();
     710             : 
     711          26 :     pgstat_reset_matching_entries(match_db_entries,
     712             :                                   ObjectIdGetDatum(MyDatabaseId),
     713             :                                   ts);
     714          26 : }
     715             : 
     716             : /*
     717             :  * Reset a single variable-numbered entry.
     718             :  *
     719             :  * If the stats kind is within a database, also reset the database's
     720             :  * stat_reset_timestamp.
     721             :  *
     722             :  * Permission checking for this function is managed through the normal
     723             :  * GRANT system.
     724             :  */
     725             : void
     726          32 : pgstat_reset(PgStat_Kind kind, Oid dboid, Oid objoid)
     727             : {
     728          32 :     const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
     729          32 :     TimestampTz ts = GetCurrentTimestamp();
     730             : 
     731             :     /* not needed atm, and doesn't make sense with the current signature */
     732             :     Assert(!pgstat_get_kind_info(kind)->fixed_amount);
     733             : 
     734             :     /* reset the "single counter" */
     735          32 :     pgstat_reset_entry(kind, dboid, objoid, ts);
     736             : 
     737          32 :     if (!kind_info->accessed_across_databases)
     738          10 :         pgstat_reset_database_timestamp(dboid, ts);
     739          32 : }
     740             : 
     741             : /*
     742             :  * Reset stats for all entries of a kind.
     743             :  *
     744             :  * Permission checking for this function is managed through the normal
     745             :  * GRANT system.
     746             :  */
     747             : void
     748          50 : pgstat_reset_of_kind(PgStat_Kind kind)
     749             : {
     750          50 :     const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
     751          50 :     TimestampTz ts = GetCurrentTimestamp();
     752             : 
     753          50 :     if (kind_info->fixed_amount)
     754          42 :         kind_info->reset_all_cb(ts);
     755             :     else
     756           8 :         pgstat_reset_entries_of_kind(kind, ts);
     757          50 : }
     758             : 
     759             : 
     760             : /* ------------------------------------------------------------
     761             :  * Fetching of stats
     762             :  * ------------------------------------------------------------
     763             :  */
     764             : 
     765             : /*
     766             :  * Discard any data collected in the current transaction.  Any subsequent
     767             :  * request will cause new snapshots to be read.
     768             :  *
     769             :  * This is also invoked during transaction commit or abort to discard
     770             :  * the no-longer-wanted snapshot.  Updates of stats_fetch_consistency can
     771             :  * cause this routine to be called.
     772             :  */
     773             : void
     774      976336 : pgstat_clear_snapshot(void)
     775             : {
     776             :     pgstat_assert_is_up();
     777             : 
     778      976336 :     memset(&pgStatLocal.snapshot.fixed_valid, 0,
     779             :            sizeof(pgStatLocal.snapshot.fixed_valid));
     780      976336 :     pgStatLocal.snapshot.stats = NULL;
     781      976336 :     pgStatLocal.snapshot.mode = PGSTAT_FETCH_CONSISTENCY_NONE;
     782             : 
     783             :     /* Release memory, if any was allocated */
     784      976336 :     if (pgStatLocal.snapshot.context)
     785             :     {
     786         804 :         MemoryContextDelete(pgStatLocal.snapshot.context);
     787             : 
     788             :         /* Reset variables */
     789         804 :         pgStatLocal.snapshot.context = NULL;
     790             :     }
     791             : 
     792             :     /*
     793             :      * Historically the backend_status.c facilities lived in this file, and
     794             :      * were reset with the same function. For now keep it that way, and
     795             :      * forward the reset request.
     796             :      */
     797      976336 :     pgstat_clear_backend_activity_snapshot();
     798             : 
     799             :     /* Reset this flag, as it may be possible that a cleanup was forced. */
     800      976336 :     force_stats_snapshot_clear = false;
     801      976336 : }
     802             : 
     803             : void *
     804       16422 : pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
     805             : {
     806             :     PgStat_HashKey key;
     807             :     PgStat_EntryRef *entry_ref;
     808             :     void       *stats_data;
     809       16422 :     const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
     810             : 
     811             :     /* should be called from backends */
     812             :     Assert(IsUnderPostmaster || !IsPostmasterEnvironment);
     813             :     Assert(!kind_info->fixed_amount);
     814             : 
     815       16422 :     pgstat_prep_snapshot();
     816             : 
     817       16422 :     key.kind = kind;
     818       16422 :     key.dboid = dboid;
     819       16422 :     key.objoid = objoid;
     820             : 
     821             :     /* if we need to build a full snapshot, do so */
     822       16422 :     if (pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_SNAPSHOT)
     823         460 :         pgstat_build_snapshot();
     824             : 
     825             :     /* if caching is desired, look up in cache */
     826       16422 :     if (pgstat_fetch_consistency > PGSTAT_FETCH_CONSISTENCY_NONE)
     827             :     {
     828        8988 :         PgStat_SnapshotEntry *entry = NULL;
     829             : 
     830        8988 :         entry = pgstat_snapshot_lookup(pgStatLocal.snapshot.stats, key);
     831             : 
     832        8988 :         if (entry)
     833         524 :             return entry->data;
     834             : 
     835             :         /*
     836             :          * If we built a full snapshot and the key is not in
     837             :          * pgStatLocal.snapshot.stats, there are no matching stats.
     838             :          */
     839        8464 :         if (pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_SNAPSHOT)
     840          28 :             return NULL;
     841             :     }
     842             : 
     843       15870 :     pgStatLocal.snapshot.mode = pgstat_fetch_consistency;
     844             : 
     845       15870 :     entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
     846             : 
     847       15870 :     if (entry_ref == NULL || entry_ref->shared_entry->dropped)
     848             :     {
     849             :         /* create empty entry when using PGSTAT_FETCH_CONSISTENCY_CACHE */
     850        4502 :         if (pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_CACHE)
     851             :         {
     852        1694 :             PgStat_SnapshotEntry *entry = NULL;
     853             :             bool        found;
     854             : 
     855        1694 :             entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
     856             :             Assert(!found);
     857        1694 :             entry->data = NULL;
     858             :         }
     859        4502 :         return NULL;
     860             :     }
     861             : 
     862             :     /*
     863             :      * Allocate in caller's context for PGSTAT_FETCH_CONSISTENCY_NONE,
     864             :      * otherwise we could quickly end up with a fair bit of memory used due to
     865             :      * repeated accesses.
     866             :      */
     867       11368 :     if (pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_NONE)
     868        4626 :         stats_data = palloc(kind_info->shared_data_len);
     869             :     else
     870        6742 :         stats_data = MemoryContextAlloc(pgStatLocal.snapshot.context,
     871        6742 :                                         kind_info->shared_data_len);
     872             : 
     873       11368 :     pgstat_lock_entry_shared(entry_ref, false);
     874       22736 :     memcpy(stats_data,
     875       11368 :            pgstat_get_entry_data(kind, entry_ref->shared_stats),
     876       11368 :            kind_info->shared_data_len);
     877       11368 :     pgstat_unlock_entry(entry_ref);
     878             : 
     879       11368 :     if (pgstat_fetch_consistency > PGSTAT_FETCH_CONSISTENCY_NONE)
     880             :     {
     881        6742 :         PgStat_SnapshotEntry *entry = NULL;
     882             :         bool        found;
     883             : 
     884        6742 :         entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
     885        6742 :         entry->data = stats_data;
     886             :     }
     887             : 
     888       11368 :     return stats_data;
     889             : }
     890             : 
     891             : /*
     892             :  * If a stats snapshot has been taken, return the timestamp at which that was
     893             :  * done, and set *have_snapshot to true. Otherwise *have_snapshot is set to
     894             :  * false.
     895             :  */
     896             : TimestampTz
     897          60 : pgstat_get_stat_snapshot_timestamp(bool *have_snapshot)
     898             : {
     899          60 :     if (force_stats_snapshot_clear)
     900          18 :         pgstat_clear_snapshot();
     901             : 
     902          60 :     if (pgStatLocal.snapshot.mode == PGSTAT_FETCH_CONSISTENCY_SNAPSHOT)
     903             :     {
     904          24 :         *have_snapshot = true;
     905          24 :         return pgStatLocal.snapshot.snapshot_timestamp;
     906             :     }
     907             : 
     908          36 :     *have_snapshot = false;
     909             : 
     910          36 :     return 0;
     911             : }
     912             : 
     913             : bool
     914         158 : pgstat_have_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
     915             : {
     916             :     /* fixed-numbered stats always exist */
     917         158 :     if (pgstat_get_kind_info(kind)->fixed_amount)
     918          12 :         return true;
     919             : 
     920         146 :     return pgstat_get_entry_ref(kind, dboid, objoid, false, NULL) != NULL;
     921             : }
     922             : 
     923             : /*
     924             :  * Ensure snapshot for fixed-numbered 'kind' exists.
     925             :  *
     926             :  * Typically used by the pgstat_fetch_* functions for a kind of stats, before
     927             :  * massaging the data into the desired format.
     928             :  */
     929             : void
     930         410 : pgstat_snapshot_fixed(PgStat_Kind kind)
     931             : {
     932             :     Assert(pgstat_is_kind_valid(kind));
     933             :     Assert(pgstat_get_kind_info(kind)->fixed_amount);
     934             : 
     935         410 :     if (pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_SNAPSHOT)
     936          24 :         pgstat_build_snapshot();
     937             :     else
     938         386 :         pgstat_build_snapshot_fixed(kind);
     939             : 
     940             :     Assert(pgStatLocal.snapshot.fixed_valid[kind]);
     941         410 : }
     942             : 
     943             : static void
     944       16474 : pgstat_prep_snapshot(void)
     945             : {
     946       16474 :     if (force_stats_snapshot_clear)
     947          18 :         pgstat_clear_snapshot();
     948             : 
     949       16474 :     if (pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_NONE ||
     950        9040 :         pgStatLocal.snapshot.stats != NULL)
     951       15670 :         return;
     952             : 
     953         804 :     if (!pgStatLocal.snapshot.context)
     954         804 :         pgStatLocal.snapshot.context = AllocSetContextCreate(TopMemoryContext,
     955             :                                                              "PgStat Snapshot",
     956             :                                                              ALLOCSET_SMALL_SIZES);
     957             : 
     958         804 :     pgStatLocal.snapshot.stats =
     959         804 :         pgstat_snapshot_create(pgStatLocal.snapshot.context,
     960             :                                PGSTAT_SNAPSHOT_HASH_SIZE,
     961             :                                NULL);
     962             : }
     963             : 
     964             : static void
     965         484 : pgstat_build_snapshot(void)
     966             : {
     967             :     dshash_seq_status hstat;
     968             :     PgStatShared_HashEntry *p;
     969             : 
     970             :     /* should only be called when we need a snapshot */
     971             :     Assert(pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_SNAPSHOT);
     972             : 
     973             :     /* snapshot already built */
     974         484 :     if (pgStatLocal.snapshot.mode == PGSTAT_FETCH_CONSISTENCY_SNAPSHOT)
     975         432 :         return;
     976             : 
     977          52 :     pgstat_prep_snapshot();
     978             : 
     979             :     Assert(pgStatLocal.snapshot.stats->members == 0);
     980             : 
     981          52 :     pgStatLocal.snapshot.snapshot_timestamp = GetCurrentTimestamp();
     982             : 
     983             :     /*
     984             :      * Snapshot all variable stats.
     985             :      */
     986          52 :     dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
     987       47286 :     while ((p = dshash_seq_next(&hstat)) != NULL)
     988             :     {
     989       47234 :         PgStat_Kind kind = p->key.kind;
     990       47234 :         const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
     991             :         bool        found;
     992             :         PgStat_SnapshotEntry *entry;
     993             :         PgStatShared_Common *stats_data;
     994             : 
     995             :         /*
     996             :          * Check if the stats object should be included in the snapshot.
     997             :          * Unless the stats kind can be accessed from all databases (e.g.,
     998             :          * database stats themselves), we only include stats for the current
     999             :          * database or objects not associated with a database (e.g. shared
    1000             :          * relations).
    1001             :          */
    1002       47234 :         if (p->key.dboid != MyDatabaseId &&
    1003       15540 :             p->key.dboid != InvalidOid &&
    1004       12624 :             !kind_info->accessed_across_databases)
    1005       12724 :             continue;
    1006             : 
    1007       34714 :         if (p->dropped)
    1008         204 :             continue;
    1009             : 
    1010             :         Assert(pg_atomic_read_u32(&p->refcount) > 0);
    1011             : 
    1012       34510 :         stats_data = dsa_get_address(pgStatLocal.dsa, p->body);
    1013             :         Assert(stats_data);
    1014             : 
    1015       34510 :         entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, p->key, &found);
    1016             :         Assert(!found);
    1017             : 
    1018       69020 :         entry->data = MemoryContextAlloc(pgStatLocal.snapshot.context,
    1019       34510 :                                          kind_info->shared_size);
    1020             : 
    1021             :         /*
    1022             :          * Acquire the LWLock directly instead of using
    1023             :          * pg_stat_lock_entry_shared() which requires a reference.
    1024             :          */
    1025       34510 :         LWLockAcquire(&stats_data->lock, LW_SHARED);
    1026       69020 :         memcpy(entry->data,
    1027       34510 :                pgstat_get_entry_data(kind, stats_data),
    1028       34510 :                kind_info->shared_size);
    1029       34510 :         LWLockRelease(&stats_data->lock);
    1030             :     }
    1031          52 :     dshash_seq_term(&hstat);
    1032             : 
    1033             :     /*
    1034             :      * Build snapshot of all fixed-numbered stats.
    1035             :      */
    1036         624 :     for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
    1037             :     {
    1038         572 :         const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
    1039             : 
    1040         572 :         if (!kind_info->fixed_amount)
    1041             :         {
    1042             :             Assert(kind_info->snapshot_cb == NULL);
    1043         260 :             continue;
    1044             :         }
    1045             : 
    1046         312 :         pgstat_build_snapshot_fixed(kind);
    1047             :     }
    1048             : 
    1049          52 :     pgStatLocal.snapshot.mode = PGSTAT_FETCH_CONSISTENCY_SNAPSHOT;
    1050             : }
    1051             : 
    1052             : static void
    1053       12182 : pgstat_build_snapshot_fixed(PgStat_Kind kind)
    1054             : {
    1055       12182 :     const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
    1056             : 
    1057             :     Assert(kind_info->fixed_amount);
    1058             :     Assert(kind_info->snapshot_cb != NULL);
    1059             : 
    1060       12182 :     if (pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_NONE)
    1061             :     {
    1062             :         /* rebuild every time */
    1063       11514 :         pgStatLocal.snapshot.fixed_valid[kind] = false;
    1064             :     }
    1065         668 :     else if (pgStatLocal.snapshot.fixed_valid[kind])
    1066             :     {
    1067             :         /* in snapshot mode we shouldn't get called again */
    1068             :         Assert(pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_CACHE);
    1069          12 :         return;
    1070             :     }
    1071             : 
    1072             :     Assert(!pgStatLocal.snapshot.fixed_valid[kind]);
    1073             : 
    1074       12170 :     kind_info->snapshot_cb();
    1075             : 
    1076             :     Assert(!pgStatLocal.snapshot.fixed_valid[kind]);
    1077       12170 :     pgStatLocal.snapshot.fixed_valid[kind] = true;
    1078             : }
    1079             : 
    1080             : 
    1081             : /* ------------------------------------------------------------
    1082             :  * Backend-local pending stats infrastructure
    1083             :  * ------------------------------------------------------------
    1084             :  */
    1085             : 
    1086             : /*
    1087             :  * Returns the appropriate PgStat_EntryRef, preparing it to receive pending
    1088             :  * stats if not already done.
    1089             :  *
    1090             :  * If created_entry is non-NULL, it'll be set to true if the entry is newly
    1091             :  * created, false otherwise.
    1092             :  */
    1093             : PgStat_EntryRef *
    1094     2862882 : pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid, bool *created_entry)
    1095             : {
    1096             :     PgStat_EntryRef *entry_ref;
    1097             : 
    1098             :     /* need to be able to flush out */
    1099             :     Assert(pgstat_get_kind_info(kind)->flush_pending_cb != NULL);
    1100             : 
    1101     2862882 :     if (unlikely(!pgStatPendingContext))
    1102             :     {
    1103       25720 :         pgStatPendingContext =
    1104       25720 :             AllocSetContextCreate(TopMemoryContext,
    1105             :                                   "PgStat Pending",
    1106             :                                   ALLOCSET_SMALL_SIZES);
    1107             :     }
    1108             : 
    1109     2862882 :     entry_ref = pgstat_get_entry_ref(kind, dboid, objoid,
    1110             :                                      true, created_entry);
    1111             : 
    1112     2862882 :     if (entry_ref->pending == NULL)
    1113             :     {
    1114     1449542 :         size_t      entrysize = pgstat_get_kind_info(kind)->pending_size;
    1115             : 
    1116             :         Assert(entrysize != (size_t) -1);
    1117             : 
    1118     1449542 :         entry_ref->pending = MemoryContextAllocZero(pgStatPendingContext, entrysize);
    1119     1449542 :         dlist_push_tail(&pgStatPending, &entry_ref->pending_node);
    1120             :     }
    1121             : 
    1122     2862882 :     return entry_ref;
    1123             : }
    1124             : 
    1125             : /*
    1126             :  * Return an existing stats entry, or NULL.
    1127             :  *
    1128             :  * This should only be used for helper function for pgstatfuncs.c - outside of
    1129             :  * that it shouldn't be needed.
    1130             :  */
    1131             : PgStat_EntryRef *
    1132          84 : pgstat_fetch_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
    1133             : {
    1134             :     PgStat_EntryRef *entry_ref;
    1135             : 
    1136          84 :     entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
    1137             : 
    1138          84 :     if (entry_ref == NULL || entry_ref->pending == NULL)
    1139          30 :         return NULL;
    1140             : 
    1141          54 :     return entry_ref;
    1142             : }
    1143             : 
    1144             : void
    1145     1449542 : pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref)
    1146             : {
    1147     1449542 :     PgStat_Kind kind = entry_ref->shared_entry->key.kind;
    1148     1449542 :     const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
    1149     1449542 :     void       *pending_data = entry_ref->pending;
    1150             : 
    1151             :     Assert(pending_data != NULL);
    1152             :     /* !fixed_amount stats should be handled explicitly */
    1153             :     Assert(!pgstat_get_kind_info(kind)->fixed_amount);
    1154             : 
    1155     1449542 :     if (kind_info->delete_pending_cb)
    1156     1368572 :         kind_info->delete_pending_cb(entry_ref);
    1157             : 
    1158     1449542 :     pfree(pending_data);
    1159     1449542 :     entry_ref->pending = NULL;
    1160             : 
    1161     1449542 :     dlist_delete(&entry_ref->pending_node);
    1162     1449542 : }
    1163             : 
    1164             : /*
    1165             :  * Flush out pending stats for database objects (databases, relations,
    1166             :  * functions).
    1167             :  */
    1168             : static bool
    1169       47552 : pgstat_flush_pending_entries(bool nowait)
    1170             : {
    1171       47552 :     bool        have_pending = false;
    1172       47552 :     dlist_node *cur = NULL;
    1173             : 
    1174             :     /*
    1175             :      * Need to be a bit careful iterating over the list of pending entries.
    1176             :      * Processing a pending entry may queue further pending entries to the end
    1177             :      * of the list that we want to process, so a simple iteration won't do.
    1178             :      * Further complicating matters is that we want to delete the current
    1179             :      * entry in each iteration from the list if we flushed successfully.
    1180             :      *
    1181             :      * So we just keep track of the next pointer in each loop iteration.
    1182             :      */
    1183       47552 :     if (!dlist_is_empty(&pgStatPending))
    1184       47552 :         cur = dlist_head_node(&pgStatPending);
    1185             : 
    1186     1441608 :     while (cur)
    1187             :     {
    1188     1394056 :         PgStat_EntryRef *entry_ref =
    1189     1394056 :             dlist_container(PgStat_EntryRef, pending_node, cur);
    1190     1394056 :         PgStat_HashKey key = entry_ref->shared_entry->key;
    1191     1394056 :         PgStat_Kind kind = key.kind;
    1192     1394056 :         const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
    1193             :         bool        did_flush;
    1194             :         dlist_node *next;
    1195             : 
    1196             :         Assert(!kind_info->fixed_amount);
    1197             :         Assert(kind_info->flush_pending_cb != NULL);
    1198             : 
    1199             :         /* flush the stats, if possible */
    1200     1394056 :         did_flush = kind_info->flush_pending_cb(entry_ref, nowait);
    1201             : 
    1202             :         Assert(did_flush || nowait);
    1203             : 
    1204             :         /* determine next entry, before deleting the pending entry */
    1205     1394056 :         if (dlist_has_next(&pgStatPending, cur))
    1206     1346504 :             next = dlist_next_node(&pgStatPending, cur);
    1207             :         else
    1208       47552 :             next = NULL;
    1209             : 
    1210             :         /* if successfully flushed, remove entry */
    1211     1394056 :         if (did_flush)
    1212     1394056 :             pgstat_delete_pending_entry(entry_ref);
    1213             :         else
    1214           0 :             have_pending = true;
    1215             : 
    1216     1394056 :         cur = next;
    1217             :     }
    1218             : 
    1219             :     Assert(dlist_is_empty(&pgStatPending) == !have_pending);
    1220             : 
    1221       47552 :     return have_pending;
    1222             : }
    1223             : 
    1224             : 
    1225             : /* ------------------------------------------------------------
    1226             :  * Helper / infrastructure functions
    1227             :  * ------------------------------------------------------------
    1228             :  */
    1229             : 
    1230             : PgStat_Kind
    1231         164 : pgstat_get_kind_from_str(char *kind_str)
    1232             : {
    1233         466 :     for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
    1234             :     {
    1235         460 :         if (pg_strcasecmp(kind_str, pgstat_kind_infos[kind].name) == 0)
    1236         158 :             return kind;
    1237             :     }
    1238             : 
    1239           6 :     ereport(ERROR,
    1240             :             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1241             :              errmsg("invalid statistics kind: \"%s\"", kind_str)));
    1242             :     return PGSTAT_KIND_DATABASE;    /* avoid compiler warnings */
    1243             : }
    1244             : 
    1245             : static inline bool
    1246      302814 : pgstat_is_kind_valid(int ikind)
    1247             : {
    1248      302814 :     return ikind >= PGSTAT_KIND_FIRST_VALID && ikind <= PGSTAT_KIND_LAST;
    1249             : }
    1250             : 
    1251             : const PgStat_KindInfo *
    1252     6995022 : pgstat_get_kind_info(PgStat_Kind kind)
    1253             : {
    1254             :     Assert(pgstat_is_kind_valid(kind));
    1255             : 
    1256     6995022 :     return &pgstat_kind_infos[kind];
    1257             : }
    1258             : 
    1259             : /*
    1260             :  * Stats should only be reported after pgstat_initialize() and before
    1261             :  * pgstat_shutdown(). This check is put in a few central places to catch
    1262             :  * violations of this rule more easily.
    1263             :  */
    1264             : #ifdef USE_ASSERT_CHECKING
    1265             : void
    1266             : pgstat_assert_is_up(void)
    1267             : {
    1268             :     Assert(pgstat_is_initialized && !pgstat_is_shutdown);
    1269             : }
    1270             : #endif
    1271             : 
    1272             : 
    1273             : /* ------------------------------------------------------------
    1274             :  * reading and writing of on-disk stats file
    1275             :  * ------------------------------------------------------------
    1276             :  */
    1277             : 
    1278             : /* helpers for pgstat_write_statsfile() */
    1279             : static void
    1280      893248 : write_chunk(FILE *fpout, void *ptr, size_t len)
    1281             : {
    1282             :     int         rc;
    1283             : 
    1284      893248 :     rc = fwrite(ptr, len, 1, fpout);
    1285             : 
    1286             :     /* we'll check for errors with ferror once at the end */
    1287             :     (void) rc;
    1288      893248 : }
    1289             : 
    1290             : #define write_chunk_s(fpout, ptr) write_chunk(fpout, ptr, sizeof(*ptr))
    1291             : 
    1292             : /*
    1293             :  * This function is called in the last process that is accessing the shared
    1294             :  * stats so locking is not required.
    1295             :  */
    1296             : static void
    1297        1914 : pgstat_write_statsfile(void)
    1298             : {
    1299             :     FILE       *fpout;
    1300             :     int32       format_id;
    1301        1914 :     const char *tmpfile = PGSTAT_STAT_PERMANENT_TMPFILE;
    1302        1914 :     const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
    1303             :     dshash_seq_status hstat;
    1304             :     PgStatShared_HashEntry *ps;
    1305             : 
    1306             :     pgstat_assert_is_up();
    1307             : 
    1308             :     /* we're shutting down, so it's ok to just override this */
    1309        1914 :     pgstat_fetch_consistency = PGSTAT_FETCH_CONSISTENCY_NONE;
    1310             : 
    1311        1914 :     elog(DEBUG2, "writing stats file \"%s\"", statfile);
    1312             : 
    1313             :     /*
    1314             :      * Open the statistics temp file to write out the current values.
    1315             :      */
    1316        1914 :     fpout = AllocateFile(tmpfile, PG_BINARY_W);
    1317        1914 :     if (fpout == NULL)
    1318             :     {
    1319           0 :         ereport(LOG,
    1320             :                 (errcode_for_file_access(),
    1321             :                  errmsg("could not open temporary statistics file \"%s\": %m",
    1322             :                         tmpfile)));
    1323           0 :         return;
    1324             :     }
    1325             : 
    1326             :     /*
    1327             :      * Write the file header --- currently just a format ID.
    1328             :      */
    1329        1914 :     format_id = PGSTAT_FILE_FORMAT_ID;
    1330        1914 :     write_chunk_s(fpout, &format_id);
    1331             : 
    1332             :     /*
    1333             :      * XXX: The following could now be generalized to just iterate over
    1334             :      * pgstat_kind_infos instead of knowing about the different kinds of
    1335             :      * stats.
    1336             :      */
    1337             : 
    1338             :     /*
    1339             :      * Write archiver stats struct
    1340             :      */
    1341        1914 :     pgstat_build_snapshot_fixed(PGSTAT_KIND_ARCHIVER);
    1342        1914 :     write_chunk_s(fpout, &pgStatLocal.snapshot.archiver);
    1343             : 
    1344             :     /*
    1345             :      * Write bgwriter stats struct
    1346             :      */
    1347        1914 :     pgstat_build_snapshot_fixed(PGSTAT_KIND_BGWRITER);
    1348        1914 :     write_chunk_s(fpout, &pgStatLocal.snapshot.bgwriter);
    1349             : 
    1350             :     /*
    1351             :      * Write checkpointer stats struct
    1352             :      */
    1353        1914 :     pgstat_build_snapshot_fixed(PGSTAT_KIND_CHECKPOINTER);
    1354        1914 :     write_chunk_s(fpout, &pgStatLocal.snapshot.checkpointer);
    1355             : 
    1356             :     /*
    1357             :      * Write IO stats struct
    1358             :      */
    1359        1914 :     pgstat_build_snapshot_fixed(PGSTAT_KIND_IO);
    1360        1914 :     write_chunk_s(fpout, &pgStatLocal.snapshot.io);
    1361             : 
    1362             :     /*
    1363             :      * Write SLRU stats struct
    1364             :      */
    1365        1914 :     pgstat_build_snapshot_fixed(PGSTAT_KIND_SLRU);
    1366        1914 :     write_chunk_s(fpout, &pgStatLocal.snapshot.slru);
    1367             : 
    1368             :     /*
    1369             :      * Write WAL stats struct
    1370             :      */
    1371        1914 :     pgstat_build_snapshot_fixed(PGSTAT_KIND_WAL);
    1372        1914 :     write_chunk_s(fpout, &pgStatLocal.snapshot.wal);
    1373             : 
    1374             :     /*
    1375             :      * Walk through the stats entries
    1376             :      */
    1377        1914 :     dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
    1378      441796 :     while ((ps = dshash_seq_next(&hstat)) != NULL)
    1379             :     {
    1380             :         PgStatShared_Common *shstats;
    1381      439882 :         const PgStat_KindInfo *kind_info = NULL;
    1382             : 
    1383      439882 :         CHECK_FOR_INTERRUPTS();
    1384             : 
    1385             :         /* we may have some "dropped" entries not yet removed, skip them */
    1386             :         Assert(!ps->dropped);
    1387      439882 :         if (ps->dropped)
    1388           0 :             continue;
    1389             : 
    1390      439882 :         shstats = (PgStatShared_Common *) dsa_get_address(pgStatLocal.dsa, ps->body);
    1391             : 
    1392      439882 :         kind_info = pgstat_get_kind_info(ps->key.kind);
    1393             : 
    1394             :         /* if not dropped the valid-entry refcount should exist */
    1395             :         Assert(pg_atomic_read_u32(&ps->refcount) > 0);
    1396             : 
    1397      439882 :         if (!kind_info->to_serialized_name)
    1398             :         {
    1399             :             /* normal stats entry, identified by PgStat_HashKey */
    1400      439796 :             fputc('S', fpout);
    1401      439796 :             write_chunk_s(fpout, &ps->key);
    1402             :         }
    1403             :         else
    1404             :         {
    1405             :             /* stats entry identified by name on disk (e.g. slots) */
    1406             :             NameData    name;
    1407             : 
    1408          86 :             kind_info->to_serialized_name(&ps->key, shstats, &name);
    1409             : 
    1410          86 :             fputc('N', fpout);
    1411          86 :             write_chunk_s(fpout, &ps->key.kind);
    1412          86 :             write_chunk_s(fpout, &name);
    1413             :         }
    1414             : 
    1415             :         /* Write except the header part of the entry */
    1416      439882 :         write_chunk(fpout,
    1417             :                     pgstat_get_entry_data(ps->key.kind, shstats),
    1418             :                     pgstat_get_entry_len(ps->key.kind));
    1419             :     }
    1420        1914 :     dshash_seq_term(&hstat);
    1421             : 
    1422             :     /*
    1423             :      * No more output to be done. Close the temp file and replace the old
    1424             :      * pgstat.stat with it.  The ferror() check replaces testing for error
    1425             :      * after each individual fputc or fwrite (in write_chunk()) above.
    1426             :      */
    1427        1914 :     fputc('E', fpout);
    1428             : 
    1429        1914 :     if (ferror(fpout))
    1430             :     {
    1431           0 :         ereport(LOG,
    1432             :                 (errcode_for_file_access(),
    1433             :                  errmsg("could not write temporary statistics file \"%s\": %m",
    1434             :                         tmpfile)));
    1435           0 :         FreeFile(fpout);
    1436           0 :         unlink(tmpfile);
    1437             :     }
    1438        1914 :     else if (FreeFile(fpout) < 0)
    1439             :     {
    1440           0 :         ereport(LOG,
    1441             :                 (errcode_for_file_access(),
    1442             :                  errmsg("could not close temporary statistics file \"%s\": %m",
    1443             :                         tmpfile)));
    1444           0 :         unlink(tmpfile);
    1445             :     }
    1446        1914 :     else if (rename(tmpfile, statfile) < 0)
    1447             :     {
    1448           0 :         ereport(LOG,
    1449             :                 (errcode_for_file_access(),
    1450             :                  errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
    1451             :                         tmpfile, statfile)));
    1452           0 :         unlink(tmpfile);
    1453             :     }
    1454             : }
    1455             : 
    1456             : /* helpers for pgstat_read_statsfile() */
    1457             : static bool
    1458      615868 : read_chunk(FILE *fpin, void *ptr, size_t len)
    1459             : {
    1460      615868 :     return fread(ptr, 1, len, fpin) == len;
    1461             : }
    1462             : 
    1463             : #define read_chunk_s(fpin, ptr) read_chunk(fpin, ptr, sizeof(*ptr))
    1464             : 
    1465             : /*
    1466             :  * Reads in existing statistics file into the shared stats hash.
    1467             :  *
    1468             :  * This function is called in the only process that is accessing the shared
    1469             :  * stats so locking is not required.
    1470             :  */
    1471             : static void
    1472        2066 : pgstat_read_statsfile(void)
    1473             : {
    1474             :     FILE       *fpin;
    1475             :     int32       format_id;
    1476             :     bool        found;
    1477        2066 :     const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
    1478        2066 :     PgStat_ShmemControl *shmem = pgStatLocal.shmem;
    1479             : 
    1480             :     /* shouldn't be called from postmaster */
    1481             :     Assert(IsUnderPostmaster || !IsPostmasterEnvironment);
    1482             : 
    1483        2066 :     elog(DEBUG2, "reading stats file \"%s\"", statfile);
    1484             : 
    1485             :     /*
    1486             :      * Try to open the stats file. If it doesn't exist, the backends simply
    1487             :      * returns zero for anything and statistics simply starts from scratch
    1488             :      * with empty counters.
    1489             :      *
    1490             :      * ENOENT is a possibility if stats collection was previously disabled or
    1491             :      * has not yet written the stats file for the first time.  Any other
    1492             :      * failure condition is suspicious.
    1493             :      */
    1494        2066 :     if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
    1495             :     {
    1496         606 :         if (errno != ENOENT)
    1497           0 :             ereport(LOG,
    1498             :                     (errcode_for_file_access(),
    1499             :                      errmsg("could not open statistics file \"%s\": %m",
    1500             :                             statfile)));
    1501         606 :         pgstat_reset_after_failure();
    1502         606 :         return;
    1503             :     }
    1504             : 
    1505             :     /*
    1506             :      * Verify it's of the expected format.
    1507             :      */
    1508        1460 :     if (!read_chunk_s(fpin, &format_id) ||
    1509        1460 :         format_id != PGSTAT_FILE_FORMAT_ID)
    1510           2 :         goto error;
    1511             : 
    1512             :     /*
    1513             :      * XXX: The following could now be generalized to just iterate over
    1514             :      * pgstat_kind_infos instead of knowing about the different kinds of
    1515             :      * stats.
    1516             :      */
    1517             : 
    1518             :     /*
    1519             :      * Read archiver stats struct
    1520             :      */
    1521        1458 :     if (!read_chunk_s(fpin, &shmem->archiver.stats))
    1522           0 :         goto error;
    1523             : 
    1524             :     /*
    1525             :      * Read bgwriter stats struct
    1526             :      */
    1527        1458 :     if (!read_chunk_s(fpin, &shmem->bgwriter.stats))
    1528           0 :         goto error;
    1529             : 
    1530             :     /*
    1531             :      * Read checkpointer stats struct
    1532             :      */
    1533        1458 :     if (!read_chunk_s(fpin, &shmem->checkpointer.stats))
    1534           0 :         goto error;
    1535             : 
    1536             :     /*
    1537             :      * Read IO stats struct
    1538             :      */
    1539        1458 :     if (!read_chunk_s(fpin, &shmem->io.stats))
    1540           0 :         goto error;
    1541             : 
    1542             :     /*
    1543             :      * Read SLRU stats struct
    1544             :      */
    1545        1458 :     if (!read_chunk_s(fpin, &shmem->slru.stats))
    1546           0 :         goto error;
    1547             : 
    1548             :     /*
    1549             :      * Read WAL stats struct
    1550             :      */
    1551        1458 :     if (!read_chunk_s(fpin, &shmem->wal.stats))
    1552           0 :         goto error;
    1553             : 
    1554             :     /*
    1555             :      * We found an existing statistics file. Read it and put all the hash
    1556             :      * table entries into place.
    1557             :      */
    1558             :     for (;;)
    1559      302814 :     {
    1560      304272 :         int         t = fgetc(fpin);
    1561             : 
    1562      304272 :         switch (t)
    1563             :         {
    1564      302814 :             case 'S':
    1565             :             case 'N':
    1566             :                 {
    1567             :                     PgStat_HashKey key;
    1568             :                     PgStatShared_HashEntry *p;
    1569             :                     PgStatShared_Common *header;
    1570             : 
    1571      302814 :                     CHECK_FOR_INTERRUPTS();
    1572             : 
    1573      302814 :                     if (t == 'S')
    1574             :                     {
    1575             :                         /* normal stats entry, identified by PgStat_HashKey */
    1576      302780 :                         if (!read_chunk_s(fpin, &key))
    1577           0 :                             goto error;
    1578             : 
    1579      302780 :                         if (!pgstat_is_kind_valid(key.kind))
    1580           0 :                             goto error;
    1581             :                     }
    1582             :                     else
    1583             :                     {
    1584             :                         /* stats entry identified by name on disk (e.g. slots) */
    1585          34 :                         const PgStat_KindInfo *kind_info = NULL;
    1586             :                         PgStat_Kind kind;
    1587             :                         NameData    name;
    1588             : 
    1589          34 :                         if (!read_chunk_s(fpin, &kind))
    1590           0 :                             goto error;
    1591          34 :                         if (!read_chunk_s(fpin, &name))
    1592           0 :                             goto error;
    1593          34 :                         if (!pgstat_is_kind_valid(kind))
    1594           0 :                             goto error;
    1595             : 
    1596          34 :                         kind_info = pgstat_get_kind_info(kind);
    1597             : 
    1598          34 :                         if (!kind_info->from_serialized_name)
    1599           0 :                             goto error;
    1600             : 
    1601          34 :                         if (!kind_info->from_serialized_name(&name, &key))
    1602             :                         {
    1603             :                             /* skip over data for entry we don't care about */
    1604           2 :                             if (fseek(fpin, pgstat_get_entry_len(kind), SEEK_CUR) != 0)
    1605           0 :                                 goto error;
    1606             : 
    1607           2 :                             continue;
    1608             :                         }
    1609             : 
    1610             :                         Assert(key.kind == kind);
    1611             :                     }
    1612             : 
    1613             :                     /*
    1614             :                      * This intentionally doesn't use pgstat_get_entry_ref() -
    1615             :                      * putting all stats into checkpointer's
    1616             :                      * pgStatEntryRefHash would be wasted effort and memory.
    1617             :                      */
    1618      302812 :                     p = dshash_find_or_insert(pgStatLocal.shared_hash, &key, &found);
    1619             : 
    1620             :                     /* don't allow duplicate entries */
    1621      302812 :                     if (found)
    1622             :                     {
    1623           0 :                         dshash_release_lock(pgStatLocal.shared_hash, p);
    1624           0 :                         elog(WARNING, "found duplicate stats entry %d/%u/%u",
    1625             :                              key.kind, key.dboid, key.objoid);
    1626           0 :                         goto error;
    1627             :                     }
    1628             : 
    1629      302812 :                     header = pgstat_init_entry(key.kind, p);
    1630      302812 :                     dshash_release_lock(pgStatLocal.shared_hash, p);
    1631             : 
    1632      302812 :                     if (!read_chunk(fpin,
    1633             :                                     pgstat_get_entry_data(key.kind, header),
    1634             :                                     pgstat_get_entry_len(key.kind)))
    1635           0 :                         goto error;
    1636             : 
    1637      302812 :                     break;
    1638             :                 }
    1639        1458 :             case 'E':
    1640             :                 /* check that 'E' actually signals end of file */
    1641        1458 :                 if (fgetc(fpin) != EOF)
    1642           2 :                     goto error;
    1643             : 
    1644        1456 :                 goto done;
    1645             : 
    1646           0 :             default:
    1647           0 :                 goto error;
    1648             :         }
    1649             :     }
    1650             : 
    1651        1460 : done:
    1652        1460 :     FreeFile(fpin);
    1653             : 
    1654        1460 :     elog(DEBUG2, "removing permanent stats file \"%s\"", statfile);
    1655        1460 :     unlink(statfile);
    1656             : 
    1657        1460 :     return;
    1658             : 
    1659           4 : error:
    1660           4 :     ereport(LOG,
    1661             :             (errmsg("corrupted statistics file \"%s\"", statfile)));
    1662             : 
    1663           4 :     pgstat_reset_after_failure();
    1664             : 
    1665           4 :     goto done;
    1666             : }
    1667             : 
    1668             : /*
    1669             :  * Helper to reset / drop stats after a crash or after restoring stats from
    1670             :  * disk failed, potentially after already loading parts.
    1671             :  */
    1672             : static void
    1673         882 : pgstat_reset_after_failure(void)
    1674             : {
    1675         882 :     TimestampTz ts = GetCurrentTimestamp();
    1676             : 
    1677             :     /* reset fixed-numbered stats */
    1678       10584 :     for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
    1679             :     {
    1680        9702 :         const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
    1681             : 
    1682        9702 :         if (!kind_info->fixed_amount)
    1683        4410 :             continue;
    1684             : 
    1685        5292 :         kind_info->reset_all_cb(ts);
    1686             :     }
    1687             : 
    1688             :     /* and drop variable-numbered ones */
    1689         882 :     pgstat_drop_all_entries();
    1690         882 : }
    1691             : 
    1692             : /*
    1693             :  * GUC assign_hook for stats_fetch_consistency.
    1694             :  */
    1695             : void
    1696        4646 : assign_stats_fetch_consistency(int newval, void *extra)
    1697             : {
    1698             :     /*
    1699             :      * Changing this value in a transaction may cause snapshot state
    1700             :      * inconsistencies, so force a clear of the current snapshot on the next
    1701             :      * snapshot build attempt.
    1702             :      */
    1703        4646 :     if (pgstat_fetch_consistency != newval)
    1704         734 :         force_stats_snapshot_clear = true;
    1705        4646 : }

Generated by: LCOV version 1.14