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

Generated by: LCOV version 1.14