LCOV - differential code coverage report
Current view: top level - src/backend/utils/activity - pgstat.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 82.3 % 554 456 98 1 455 1
Current Date: 2025-09-06 07:49:51 +0900 Functions: 97.1 % 35 34 1 1 33
Baseline: lcov-20250906-005545-baseline Branches: 57.8 % 464 268 196 268
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 1 1 1
(30,360] days: 82.2 % 45 37 8 37
(360..) days: 82.3 % 508 418 90 418
Function coverage date bins:
(30,360] days: 100.0 % 8 8 8
(360..) days: 96.3 % 27 26 1 1 25
Branch coverage date bins:
(30,360] days: 61.8 % 34 21 13 21
(360..) days: 57.4 % 430 247 183 247

 Age         Owner                    Branch data    TLA  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 (if the stats kind allows it), except when shutting down in
                                 16                 :                :  * immediate mode.
                                 17                 :                :  *
                                 18                 :                :  * Fixed-numbered stats are stored in plain (non-dynamic) shared memory.
                                 19                 :                :  *
                                 20                 :                :  * Statistics for variable-numbered objects are stored in dynamic shared
                                 21                 :                :  * memory and can be found via a dshash hashtable. The statistics counters are
                                 22                 :                :  * not part of the dshash entry (PgStatShared_HashEntry) directly, but are
                                 23                 :                :  * separately allocated (PgStatShared_HashEntry->body). The separate
                                 24                 :                :  * allocation allows different kinds of statistics to be stored in the same
                                 25                 :                :  * hashtable without wasting space in PgStatShared_HashEntry.
                                 26                 :                :  *
                                 27                 :                :  * Variable-numbered stats are addressed by PgStat_HashKey while running.  It
                                 28                 :                :  * is not possible to have statistics for an object that cannot be addressed
                                 29                 :                :  * that way at runtime. A wider identifier can be used when serializing to
                                 30                 :                :  * disk (used for replication slot stats).
                                 31                 :                :  *
                                 32                 :                :  * To avoid contention on the shared hashtable, each backend has a
                                 33                 :                :  * backend-local hashtable (pgStatEntryRefHash) in front of the shared
                                 34                 :                :  * hashtable, containing references (PgStat_EntryRef) to shared hashtable
                                 35                 :                :  * entries. The shared hashtable only needs to be accessed when no prior
                                 36                 :                :  * reference is found in the local hashtable. Besides pointing to the
                                 37                 :                :  * shared hashtable entry (PgStatShared_HashEntry) PgStat_EntryRef also
                                 38                 :                :  * contains a pointer to the shared statistics data, as a process-local
                                 39                 :                :  * address, to reduce access costs.
                                 40                 :                :  *
                                 41                 :                :  * The names for structs stored in shared memory are prefixed with
                                 42                 :                :  * PgStatShared instead of PgStat. Each stats entry in shared memory is
                                 43                 :                :  * protected by a dedicated lwlock.
                                 44                 :                :  *
                                 45                 :                :  * Most stats updates are first accumulated locally in each process as pending
                                 46                 :                :  * entries, then later flushed to shared memory (just after commit, or by
                                 47                 :                :  * idle-timeout). This practically eliminates contention on individual stats
                                 48                 :                :  * entries. For most kinds of variable-numbered pending stats data is stored
                                 49                 :                :  * in PgStat_EntryRef->pending. All entries with pending data are in the
                                 50                 :                :  * pgStatPending list. Pending statistics updates are flushed out by
                                 51                 :                :  * pgstat_report_stat().
                                 52                 :                :  *
                                 53                 :                :  * It is possible for external modules to define custom statistics kinds,
                                 54                 :                :  * that can use the same properties as any built-in stats kinds.  Each custom
                                 55                 :                :  * stats kind needs to assign a unique ID to ensure that it does not overlap
                                 56                 :                :  * with other extensions.  In order to reserve a unique stats kind ID, refer
                                 57                 :                :  * to https://wiki.postgresql.org/wiki/CustomCumulativeStats.
                                 58                 :                :  *
                                 59                 :                :  * The behavior of different kinds of statistics is determined by the kind's
                                 60                 :                :  * entry in pgstat_kind_builtin_infos for all the built-in statistics kinds
                                 61                 :                :  * defined, and pgstat_kind_custom_infos for custom kinds registered at
                                 62                 :                :  * startup by pgstat_register_kind().  See PgStat_KindInfo for details.
                                 63                 :                :  *
                                 64                 :                :  * The consistency of read accesses to statistics can be configured using the
                                 65                 :                :  * stats_fetch_consistency GUC (see config.sgml and monitoring.sgml for the
                                 66                 :                :  * settings). When using PGSTAT_FETCH_CONSISTENCY_CACHE or
                                 67                 :                :  * PGSTAT_FETCH_CONSISTENCY_SNAPSHOT statistics are stored in
                                 68                 :                :  * pgStatLocal.snapshot.
                                 69                 :                :  *
                                 70                 :                :  * To keep things manageable, stats handling is split across several
                                 71                 :                :  * files. Infrastructure pieces are in:
                                 72                 :                :  * - pgstat.c - this file, to tie it all together
                                 73                 :                :  * - pgstat_shmem.c - nearly everything dealing with shared memory, including
                                 74                 :                :  *   the maintenance of hashtable entries
                                 75                 :                :  * - pgstat_xact.c - transactional integration, including the transactional
                                 76                 :                :  *   creation and dropping of stats entries
                                 77                 :                :  *
                                 78                 :                :  * Each statistics kind is handled in a dedicated file:
                                 79                 :                :  * - pgstat_archiver.c
                                 80                 :                :  * - pgstat_backend.c
                                 81                 :                :  * - pgstat_bgwriter.c
                                 82                 :                :  * - pgstat_checkpointer.c
                                 83                 :                :  * - pgstat_database.c
                                 84                 :                :  * - pgstat_function.c
                                 85                 :                :  * - pgstat_io.c
                                 86                 :                :  * - pgstat_relation.c
                                 87                 :                :  * - pgstat_replslot.c
                                 88                 :                :  * - pgstat_slru.c
                                 89                 :                :  * - pgstat_subscription.c
                                 90                 :                :  * - pgstat_wal.c
                                 91                 :                :  *
                                 92                 :                :  * Whenever possible infrastructure files should not contain code related to
                                 93                 :                :  * specific kinds of stats.
                                 94                 :                :  *
                                 95                 :                :  *
                                 96                 :                :  * Copyright (c) 2001-2025, PostgreSQL Global Development Group
                                 97                 :                :  *
                                 98                 :                :  * IDENTIFICATION
                                 99                 :                :  *    src/backend/utils/activity/pgstat.c
                                100                 :                :  * ----------
                                101                 :                :  */
                                102                 :                : #include "postgres.h"
                                103                 :                : 
                                104                 :                : #include <unistd.h>
                                105                 :                : 
                                106                 :                : #include "access/xact.h"
                                107                 :                : #include "lib/dshash.h"
                                108                 :                : #include "pgstat.h"
                                109                 :                : #include "storage/fd.h"
                                110                 :                : #include "storage/ipc.h"
                                111                 :                : #include "storage/lwlock.h"
                                112                 :                : #include "utils/guc_hooks.h"
                                113                 :                : #include "utils/memutils.h"
                                114                 :                : #include "utils/pgstat_internal.h"
                                115                 :                : #include "utils/timestamp.h"
                                116                 :                : 
                                117                 :                : 
                                118                 :                : /* ----------
                                119                 :                :  * Timer definitions.
                                120                 :                :  *
                                121                 :                :  * In milliseconds.
                                122                 :                :  * ----------
                                123                 :                :  */
                                124                 :                : 
                                125                 :                : /* minimum interval non-forced stats flushes.*/
                                126                 :                : #define PGSTAT_MIN_INTERVAL         1000
                                127                 :                : /* how long until to block flushing pending stats updates */
                                128                 :                : #define PGSTAT_MAX_INTERVAL         60000
                                129                 :                : /* when to call pgstat_report_stat() again, even when idle */
                                130                 :                : #define PGSTAT_IDLE_INTERVAL        10000
                                131                 :                : 
                                132                 :                : /* ----------
                                133                 :                :  * Initial size hints for the hash tables used in statistics.
                                134                 :                :  * ----------
                                135                 :                :  */
                                136                 :                : 
                                137                 :                : #define PGSTAT_SNAPSHOT_HASH_SIZE   512
                                138                 :                : 
                                139                 :                : /* ---------
                                140                 :                :  * Identifiers in stats file.
                                141                 :                :  * ---------
                                142                 :                :  */
                                143                 :                : #define PGSTAT_FILE_ENTRY_END   'E' /* end of file */
                                144                 :                : #define PGSTAT_FILE_ENTRY_FIXED 'F' /* fixed-numbered stats entry */
                                145                 :                : #define PGSTAT_FILE_ENTRY_NAME  'N' /* stats entry identified by name */
                                146                 :                : #define PGSTAT_FILE_ENTRY_HASH  'S' /* stats entry identified by
                                147                 :                :                                      * PgStat_HashKey */
                                148                 :                : 
                                149                 :                : /* hash table for statistics snapshots entry */
                                150                 :                : typedef struct PgStat_SnapshotEntry
                                151                 :                : {
                                152                 :                :     PgStat_HashKey key;
                                153                 :                :     char        status;         /* for simplehash use */
                                154                 :                :     void       *data;           /* the stats data itself */
                                155                 :                : } PgStat_SnapshotEntry;
                                156                 :                : 
                                157                 :                : 
                                158                 :                : /* ----------
                                159                 :                :  * Backend-local Hash Table Definitions
                                160                 :                :  * ----------
                                161                 :                :  */
                                162                 :                : 
                                163                 :                : /* for stats snapshot entries */
                                164                 :                : #define SH_PREFIX pgstat_snapshot
                                165                 :                : #define SH_ELEMENT_TYPE PgStat_SnapshotEntry
                                166                 :                : #define SH_KEY_TYPE PgStat_HashKey
                                167                 :                : #define SH_KEY key
                                168                 :                : #define SH_HASH_KEY(tb, key) \
                                169                 :                :     pgstat_hash_hash_key(&key, sizeof(PgStat_HashKey), NULL)
                                170                 :                : #define SH_EQUAL(tb, a, b) \
                                171                 :                :     pgstat_cmp_hash_key(&a, &b, sizeof(PgStat_HashKey), NULL) == 0
                                172                 :                : #define SH_SCOPE static inline
                                173                 :                : #define SH_DEFINE
                                174                 :                : #define SH_DECLARE
                                175                 :                : #include "lib/simplehash.h"
                                176                 :                : 
                                177                 :                : 
                                178                 :                : /* ----------
                                179                 :                :  * Local function forward declarations
                                180                 :                :  * ----------
                                181                 :                :  */
                                182                 :                : 
                                183                 :                : static void pgstat_write_statsfile(void);
                                184                 :                : static void pgstat_read_statsfile(void);
                                185                 :                : 
                                186                 :                : static void pgstat_init_snapshot_fixed(void);
                                187                 :                : 
                                188                 :                : static void pgstat_reset_after_failure(void);
                                189                 :                : 
                                190                 :                : static bool pgstat_flush_pending_entries(bool nowait);
                                191                 :                : 
                                192                 :                : static void pgstat_prep_snapshot(void);
                                193                 :                : static void pgstat_build_snapshot(void);
                                194                 :                : static void pgstat_build_snapshot_fixed(PgStat_Kind kind);
                                195                 :                : 
                                196                 :                : static inline bool pgstat_is_kind_valid(PgStat_Kind kind);
                                197                 :                : 
                                198                 :                : 
                                199                 :                : /* ----------
                                200                 :                :  * GUC parameters
                                201                 :                :  * ----------
                                202                 :                :  */
                                203                 :                : 
                                204                 :                : bool        pgstat_track_counts = false;
                                205                 :                : int         pgstat_fetch_consistency = PGSTAT_FETCH_CONSISTENCY_CACHE;
                                206                 :                : 
                                207                 :                : 
                                208                 :                : /* ----------
                                209                 :                :  * state shared with pgstat_*.c
                                210                 :                :  * ----------
                                211                 :                :  */
                                212                 :                : 
                                213                 :                : PgStat_LocalState pgStatLocal;
                                214                 :                : 
                                215                 :                : /*
                                216                 :                :  * Track pending reports for fixed-numbered stats, used by
                                217                 :                :  * pgstat_report_stat().
                                218                 :                :  */
                                219                 :                : bool        pgstat_report_fixed = false;
                                220                 :                : 
                                221                 :                : /* ----------
                                222                 :                :  * Local data
                                223                 :                :  *
                                224                 :                :  * NB: There should be only variables related to stats infrastructure here,
                                225                 :                :  * not for specific kinds of stats.
                                226                 :                :  * ----------
                                227                 :                :  */
                                228                 :                : 
                                229                 :                : /*
                                230                 :                :  * Memory contexts containing the pgStatEntryRefHash table, the
                                231                 :                :  * pgStatSharedRef entries, and pending data respectively. Mostly to make it
                                232                 :                :  * easier to track / attribute memory usage.
                                233                 :                :  */
                                234                 :                : 
                                235                 :                : static MemoryContext pgStatPendingContext = NULL;
                                236                 :                : 
                                237                 :                : /*
                                238                 :                :  * Backend local list of PgStat_EntryRef with unflushed pending stats.
                                239                 :                :  *
                                240                 :                :  * Newly pending entries should only ever be added to the end of the list,
                                241                 :                :  * otherwise pgstat_flush_pending_entries() might not see them immediately.
                                242                 :                :  */
                                243                 :                : static dlist_head pgStatPending = DLIST_STATIC_INIT(pgStatPending);
                                244                 :                : 
                                245                 :                : 
                                246                 :                : /*
                                247                 :                :  * Force the next stats flush to happen regardless of
                                248                 :                :  * PGSTAT_MIN_INTERVAL. Useful in test scripts.
                                249                 :                :  */
                                250                 :                : static bool pgStatForceNextFlush = false;
                                251                 :                : 
                                252                 :                : /*
                                253                 :                :  * Force-clear existing snapshot before next use when stats_fetch_consistency
                                254                 :                :  * is changed.
                                255                 :                :  */
                                256                 :                : static bool force_stats_snapshot_clear = false;
                                257                 :                : 
                                258                 :                : 
                                259                 :                : /*
                                260                 :                :  * For assertions that check pgstat is not used before initialization / after
                                261                 :                :  * shutdown.
                                262                 :                :  */
                                263                 :                : #ifdef USE_ASSERT_CHECKING
                                264                 :                : static bool pgstat_is_initialized = false;
                                265                 :                : static bool pgstat_is_shutdown = false;
                                266                 :                : #endif
                                267                 :                : 
                                268                 :                : 
                                269                 :                : /*
                                270                 :                :  * The different kinds of built-in statistics.
                                271                 :                :  *
                                272                 :                :  * If reasonably possible, handling specific to one kind of stats should go
                                273                 :                :  * through this abstraction, rather than making more of pgstat.c aware.
                                274                 :                :  *
                                275                 :                :  * See comments for struct PgStat_KindInfo for details about the individual
                                276                 :                :  * fields.
                                277                 :                :  *
                                278                 :                :  * XXX: It'd be nicer to define this outside of this file. But there doesn't
                                279                 :                :  * seem to be a great way of doing that, given the split across multiple
                                280                 :                :  * files.
                                281                 :                :  */
                                282                 :                : static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE] = {
                                283                 :                : 
                                284                 :                :     /* stats kinds for variable-numbered objects */
                                285                 :                : 
                                286                 :                :     [PGSTAT_KIND_DATABASE] = {
                                287                 :                :         .name = "database",
                                288                 :                : 
                                289                 :                :         .fixed_amount = false,
                                290                 :                :         .write_to_file = true,
                                291                 :                :         /* so pg_stat_database entries can be seen in all databases */
                                292                 :                :         .accessed_across_databases = true,
                                293                 :                : 
                                294                 :                :         .shared_size = sizeof(PgStatShared_Database),
                                295                 :                :         .shared_data_off = offsetof(PgStatShared_Database, stats),
                                296                 :                :         .shared_data_len = sizeof(((PgStatShared_Database *) 0)->stats),
                                297                 :                :         .pending_size = sizeof(PgStat_StatDBEntry),
                                298                 :                : 
                                299                 :                :         .flush_pending_cb = pgstat_database_flush_cb,
                                300                 :                :         .reset_timestamp_cb = pgstat_database_reset_timestamp_cb,
                                301                 :                :     },
                                302                 :                : 
                                303                 :                :     [PGSTAT_KIND_RELATION] = {
                                304                 :                :         .name = "relation",
                                305                 :                : 
                                306                 :                :         .fixed_amount = false,
                                307                 :                :         .write_to_file = true,
                                308                 :                : 
                                309                 :                :         .shared_size = sizeof(PgStatShared_Relation),
                                310                 :                :         .shared_data_off = offsetof(PgStatShared_Relation, stats),
                                311                 :                :         .shared_data_len = sizeof(((PgStatShared_Relation *) 0)->stats),
                                312                 :                :         .pending_size = sizeof(PgStat_TableStatus),
                                313                 :                : 
                                314                 :                :         .flush_pending_cb = pgstat_relation_flush_cb,
                                315                 :                :         .delete_pending_cb = pgstat_relation_delete_pending_cb,
                                316                 :                :     },
                                317                 :                : 
                                318                 :                :     [PGSTAT_KIND_FUNCTION] = {
                                319                 :                :         .name = "function",
                                320                 :                : 
                                321                 :                :         .fixed_amount = false,
                                322                 :                :         .write_to_file = true,
                                323                 :                : 
                                324                 :                :         .shared_size = sizeof(PgStatShared_Function),
                                325                 :                :         .shared_data_off = offsetof(PgStatShared_Function, stats),
                                326                 :                :         .shared_data_len = sizeof(((PgStatShared_Function *) 0)->stats),
                                327                 :                :         .pending_size = sizeof(PgStat_FunctionCounts),
                                328                 :                : 
                                329                 :                :         .flush_pending_cb = pgstat_function_flush_cb,
                                330                 :                :     },
                                331                 :                : 
                                332                 :                :     [PGSTAT_KIND_REPLSLOT] = {
                                333                 :                :         .name = "replslot",
                                334                 :                : 
                                335                 :                :         .fixed_amount = false,
                                336                 :                :         .write_to_file = true,
                                337                 :                : 
                                338                 :                :         .accessed_across_databases = true,
                                339                 :                : 
                                340                 :                :         .shared_size = sizeof(PgStatShared_ReplSlot),
                                341                 :                :         .shared_data_off = offsetof(PgStatShared_ReplSlot, stats),
                                342                 :                :         .shared_data_len = sizeof(((PgStatShared_ReplSlot *) 0)->stats),
                                343                 :                : 
                                344                 :                :         .reset_timestamp_cb = pgstat_replslot_reset_timestamp_cb,
                                345                 :                :         .to_serialized_name = pgstat_replslot_to_serialized_name_cb,
                                346                 :                :         .from_serialized_name = pgstat_replslot_from_serialized_name_cb,
                                347                 :                :     },
                                348                 :                : 
                                349                 :                :     [PGSTAT_KIND_SUBSCRIPTION] = {
                                350                 :                :         .name = "subscription",
                                351                 :                : 
                                352                 :                :         .fixed_amount = false,
                                353                 :                :         .write_to_file = true,
                                354                 :                :         /* so pg_stat_subscription_stats entries can be seen in all databases */
                                355                 :                :         .accessed_across_databases = true,
                                356                 :                : 
                                357                 :                :         .shared_size = sizeof(PgStatShared_Subscription),
                                358                 :                :         .shared_data_off = offsetof(PgStatShared_Subscription, stats),
                                359                 :                :         .shared_data_len = sizeof(((PgStatShared_Subscription *) 0)->stats),
                                360                 :                :         .pending_size = sizeof(PgStat_BackendSubEntry),
                                361                 :                : 
                                362                 :                :         .flush_pending_cb = pgstat_subscription_flush_cb,
                                363                 :                :         .reset_timestamp_cb = pgstat_subscription_reset_timestamp_cb,
                                364                 :                :     },
                                365                 :                : 
                                366                 :                :     [PGSTAT_KIND_BACKEND] = {
                                367                 :                :         .name = "backend",
                                368                 :                : 
                                369                 :                :         .fixed_amount = false,
                                370                 :                :         .write_to_file = false,
                                371                 :                : 
                                372                 :                :         .accessed_across_databases = true,
                                373                 :                : 
                                374                 :                :         .shared_size = sizeof(PgStatShared_Backend),
                                375                 :                :         .shared_data_off = offsetof(PgStatShared_Backend, stats),
                                376                 :                :         .shared_data_len = sizeof(((PgStatShared_Backend *) 0)->stats),
                                377                 :                : 
                                378                 :                :         .flush_static_cb = pgstat_backend_flush_cb,
                                379                 :                :         .reset_timestamp_cb = pgstat_backend_reset_timestamp_cb,
                                380                 :                :     },
                                381                 :                : 
                                382                 :                :     /* stats for fixed-numbered (mostly 1) objects */
                                383                 :                : 
                                384                 :                :     [PGSTAT_KIND_ARCHIVER] = {
                                385                 :                :         .name = "archiver",
                                386                 :                : 
                                387                 :                :         .fixed_amount = true,
                                388                 :                :         .write_to_file = true,
                                389                 :                : 
                                390                 :                :         .snapshot_ctl_off = offsetof(PgStat_Snapshot, archiver),
                                391                 :                :         .shared_ctl_off = offsetof(PgStat_ShmemControl, archiver),
                                392                 :                :         .shared_data_off = offsetof(PgStatShared_Archiver, stats),
                                393                 :                :         .shared_data_len = sizeof(((PgStatShared_Archiver *) 0)->stats),
                                394                 :                : 
                                395                 :                :         .init_shmem_cb = pgstat_archiver_init_shmem_cb,
                                396                 :                :         .reset_all_cb = pgstat_archiver_reset_all_cb,
                                397                 :                :         .snapshot_cb = pgstat_archiver_snapshot_cb,
                                398                 :                :     },
                                399                 :                : 
                                400                 :                :     [PGSTAT_KIND_BGWRITER] = {
                                401                 :                :         .name = "bgwriter",
                                402                 :                : 
                                403                 :                :         .fixed_amount = true,
                                404                 :                :         .write_to_file = true,
                                405                 :                : 
                                406                 :                :         .snapshot_ctl_off = offsetof(PgStat_Snapshot, bgwriter),
                                407                 :                :         .shared_ctl_off = offsetof(PgStat_ShmemControl, bgwriter),
                                408                 :                :         .shared_data_off = offsetof(PgStatShared_BgWriter, stats),
                                409                 :                :         .shared_data_len = sizeof(((PgStatShared_BgWriter *) 0)->stats),
                                410                 :                : 
                                411                 :                :         .init_shmem_cb = pgstat_bgwriter_init_shmem_cb,
                                412                 :                :         .reset_all_cb = pgstat_bgwriter_reset_all_cb,
                                413                 :                :         .snapshot_cb = pgstat_bgwriter_snapshot_cb,
                                414                 :                :     },
                                415                 :                : 
                                416                 :                :     [PGSTAT_KIND_CHECKPOINTER] = {
                                417                 :                :         .name = "checkpointer",
                                418                 :                : 
                                419                 :                :         .fixed_amount = true,
                                420                 :                :         .write_to_file = true,
                                421                 :                : 
                                422                 :                :         .snapshot_ctl_off = offsetof(PgStat_Snapshot, checkpointer),
                                423                 :                :         .shared_ctl_off = offsetof(PgStat_ShmemControl, checkpointer),
                                424                 :                :         .shared_data_off = offsetof(PgStatShared_Checkpointer, stats),
                                425                 :                :         .shared_data_len = sizeof(((PgStatShared_Checkpointer *) 0)->stats),
                                426                 :                : 
                                427                 :                :         .init_shmem_cb = pgstat_checkpointer_init_shmem_cb,
                                428                 :                :         .reset_all_cb = pgstat_checkpointer_reset_all_cb,
                                429                 :                :         .snapshot_cb = pgstat_checkpointer_snapshot_cb,
                                430                 :                :     },
                                431                 :                : 
                                432                 :                :     [PGSTAT_KIND_IO] = {
                                433                 :                :         .name = "io",
                                434                 :                : 
                                435                 :                :         .fixed_amount = true,
                                436                 :                :         .write_to_file = true,
                                437                 :                : 
                                438                 :                :         .snapshot_ctl_off = offsetof(PgStat_Snapshot, io),
                                439                 :                :         .shared_ctl_off = offsetof(PgStat_ShmemControl, io),
                                440                 :                :         .shared_data_off = offsetof(PgStatShared_IO, stats),
                                441                 :                :         .shared_data_len = sizeof(((PgStatShared_IO *) 0)->stats),
                                442                 :                : 
                                443                 :                :         .flush_static_cb = pgstat_io_flush_cb,
                                444                 :                :         .init_shmem_cb = pgstat_io_init_shmem_cb,
                                445                 :                :         .reset_all_cb = pgstat_io_reset_all_cb,
                                446                 :                :         .snapshot_cb = pgstat_io_snapshot_cb,
                                447                 :                :     },
                                448                 :                : 
                                449                 :                :     [PGSTAT_KIND_SLRU] = {
                                450                 :                :         .name = "slru",
                                451                 :                : 
                                452                 :                :         .fixed_amount = true,
                                453                 :                :         .write_to_file = true,
                                454                 :                : 
                                455                 :                :         .snapshot_ctl_off = offsetof(PgStat_Snapshot, slru),
                                456                 :                :         .shared_ctl_off = offsetof(PgStat_ShmemControl, slru),
                                457                 :                :         .shared_data_off = offsetof(PgStatShared_SLRU, stats),
                                458                 :                :         .shared_data_len = sizeof(((PgStatShared_SLRU *) 0)->stats),
                                459                 :                : 
                                460                 :                :         .flush_static_cb = pgstat_slru_flush_cb,
                                461                 :                :         .init_shmem_cb = pgstat_slru_init_shmem_cb,
                                462                 :                :         .reset_all_cb = pgstat_slru_reset_all_cb,
                                463                 :                :         .snapshot_cb = pgstat_slru_snapshot_cb,
                                464                 :                :     },
                                465                 :                : 
                                466                 :                :     [PGSTAT_KIND_WAL] = {
                                467                 :                :         .name = "wal",
                                468                 :                : 
                                469                 :                :         .fixed_amount = true,
                                470                 :                :         .write_to_file = true,
                                471                 :                : 
                                472                 :                :         .snapshot_ctl_off = offsetof(PgStat_Snapshot, wal),
                                473                 :                :         .shared_ctl_off = offsetof(PgStat_ShmemControl, wal),
                                474                 :                :         .shared_data_off = offsetof(PgStatShared_Wal, stats),
                                475                 :                :         .shared_data_len = sizeof(((PgStatShared_Wal *) 0)->stats),
                                476                 :                : 
                                477                 :                :         .init_backend_cb = pgstat_wal_init_backend_cb,
                                478                 :                :         .flush_static_cb = pgstat_wal_flush_cb,
                                479                 :                :         .init_shmem_cb = pgstat_wal_init_shmem_cb,
                                480                 :                :         .reset_all_cb = pgstat_wal_reset_all_cb,
                                481                 :                :         .snapshot_cb = pgstat_wal_snapshot_cb,
                                482                 :                :     },
                                483                 :                : };
                                484                 :                : 
                                485                 :                : /*
                                486                 :                :  * Information about custom statistics kinds.
                                487                 :                :  *
                                488                 :                :  * These are saved in a different array than the built-in kinds to save
                                489                 :                :  * in clarity with the initializations.
                                490                 :                :  *
                                491                 :                :  * Indexed by PGSTAT_KIND_CUSTOM_MIN, of size PGSTAT_KIND_CUSTOM_SIZE.
                                492                 :                :  */
                                493                 :                : static const PgStat_KindInfo **pgstat_kind_custom_infos = NULL;
                                494                 :                : 
                                495                 :                : /* ------------------------------------------------------------
                                496                 :                :  * Functions managing the state of the stats system for all backends.
                                497                 :                :  * ------------------------------------------------------------
                                498                 :                :  */
                                499                 :                : 
                                500                 :                : /*
                                501                 :                :  * Read on-disk stats into memory at server start.
                                502                 :                :  *
                                503                 :                :  * Should only be called by the startup process or in single user mode.
                                504                 :                :  */
                                505                 :                : void
  173 michael@paquier.xyz       506                 :CBC         718 : pgstat_restore_stats(void)
                                507                 :                : {
                                508                 :            718 :     pgstat_read_statsfile();
 7331 tgl@sss.pgh.pa.us         509                 :            718 : }
                                510                 :                : 
                                511                 :                : /*
                                512                 :                :  * Remove the stats file.  This is currently used only if WAL recovery is
                                513                 :                :  * needed after a crash.
                                514                 :                :  *
                                515                 :                :  * Should only be called by the startup process or in single user mode.
                                516                 :                :  */
                                517                 :                : void
 1249 andres@anarazel.de        518                 :            169 : pgstat_discard_stats(void)
                                519                 :                : {
                                520                 :                :     int         ret;
                                521                 :                : 
                                522                 :                :     /* NB: this needs to be done even in single user mode */
                                523                 :                : 
                                524                 :            169 :     ret = unlink(PGSTAT_STAT_PERMANENT_FILENAME);
                                525         [ +  + ]:            169 :     if (ret != 0)
                                526                 :                :     {
                                527         [ +  - ]:            168 :         if (errno == ENOENT)
                                528         [ +  + ]:            168 :             elog(DEBUG2,
                                529                 :                :                  "didn't need to unlink permanent stats file \"%s\" - didn't exist",
                                530                 :                :                  PGSTAT_STAT_PERMANENT_FILENAME);
                                531                 :                :         else
 1249 andres@anarazel.de        532         [ #  # ]:UBC           0 :             ereport(LOG,
                                533                 :                :                     (errcode_for_file_access(),
                                534                 :                :                      errmsg("could not unlink permanent statistics file \"%s\": %m",
                                535                 :                :                             PGSTAT_STAT_PERMANENT_FILENAME)));
                                536                 :                :     }
                                537                 :                :     else
                                538                 :                :     {
 1249 andres@anarazel.de        539         [ -  + ]:CBC           1 :         ereport(DEBUG2,
                                540                 :                :                 (errcode_for_file_access(),
                                541                 :                :                  errmsg_internal("unlinked permanent statistics file \"%s\"",
                                542                 :                :                                  PGSTAT_STAT_PERMANENT_FILENAME)));
                                543                 :                :     }
                                544                 :                : 
                                545                 :                :     /*
                                546                 :                :      * Reset stats contents. This will set reset timestamps of fixed-numbered
                                547                 :                :      * stats to the current time (no variable stats exist).
                                548                 :                :      */
 1241                           549                 :            169 :     pgstat_reset_after_failure();
 7926 bruce@momjian.us          550                 :            169 : }
                                551                 :                : 
                                552                 :                : /*
                                553                 :                :  * pgstat_before_server_shutdown() needs to be called by exactly one process
                                554                 :                :  * during regular server shutdowns. Otherwise all stats will be lost.
                                555                 :                :  *
                                556                 :                :  * We currently only write out stats for proc_exit(0). We might want to change
                                557                 :                :  * that at some point... But right now pgstat_discard_stats() would be called
                                558                 :                :  * during the start after a disorderly shutdown, anyway.
                                559                 :                :  */
                                560                 :                : void
 1249 andres@anarazel.de        561                 :            610 : pgstat_before_server_shutdown(int code, Datum arg)
                                562                 :                : {
                                563         [ -  + ]:            610 :     Assert(pgStatLocal.shmem != NULL);
                                564         [ -  + ]:            610 :     Assert(!pgStatLocal.shmem->is_shutdown);
                                565                 :                : 
                                566                 :                :     /*
                                567                 :                :      * Stats should only be reported after pgstat_initialize() and before
                                568                 :                :      * pgstat_shutdown(). This is a convenient point to catch most violations
                                569                 :                :      * of this rule.
                                570                 :                :      */
                                571   [ +  -  -  + ]:            610 :     Assert(pgstat_is_initialized && !pgstat_is_shutdown);
                                572                 :                : 
                                573                 :                :     /* flush out our own pending changes before writing out */
                                574                 :            610 :     pgstat_report_stat(true);
                                575                 :                : 
                                576                 :                :     /*
                                577                 :                :      * Only write out file during normal shutdown. Don't even signal that
                                578                 :                :      * we've shutdown during irregular shutdowns, because the shutdown
                                579                 :                :      * sequence isn't coordinated to ensure this backend shuts down last.
                                580                 :                :      */
                                581         [ +  + ]:            610 :     if (code == 0)
                                582                 :                :     {
                                583                 :            605 :         pgStatLocal.shmem->is_shutdown = true;
  173 michael@paquier.xyz       584                 :            605 :         pgstat_write_statsfile();
                                585                 :                :     }
 6743 bruce@momjian.us          586                 :            610 : }
                                587                 :                : 
                                588                 :                : 
                                589                 :                : /* ------------------------------------------------------------
                                590                 :                :  * Backend initialization / shutdown functions
                                591                 :                :  * ------------------------------------------------------------
                                592                 :                :  */
                                593                 :                : 
                                594                 :                : /*
                                595                 :                :  * Shut down a single backend's statistics reporting at process exit.
                                596                 :                :  *
                                597                 :                :  * Flush out any remaining statistics counts.  Without this, operations
                                598                 :                :  * triggered during backend exit (such as temp table deletions) won't be
                                599                 :                :  * counted.
                                600                 :                :  */
                                601                 :                : static void
 1265 andres@anarazel.de        602                 :          18771 : pgstat_shutdown_hook(int code, Datum arg)
                                603                 :                : {
                                604         [ -  + ]:          18771 :     Assert(!pgstat_is_shutdown);
 1249                           605   [ +  +  -  + ]:          18771 :     Assert(IsUnderPostmaster || !IsPostmasterEnvironment);
                                606                 :                : 
                                607                 :                :     /*
                                608                 :                :      * If we got as far as discovering our own database ID, we can flush out
                                609                 :                :      * what we did so far.  Otherwise, we'd be reporting an invalid database
                                610                 :                :      * ID, so forget it.  (This means that accesses to pg_database during
                                611                 :                :      * failed backend starts might never get counted.)
                                612                 :                :      */
 1265                           613         [ +  + ]:          18771 :     if (OidIsValid(MyDatabaseId))
 1249                           614                 :          13454 :         pgstat_report_disconnect(MyDatabaseId);
                                615                 :                : 
                                616                 :          18771 :     pgstat_report_stat(true);
                                617                 :                : 
                                618                 :                :     /* there shouldn't be any pending changes left */
                                619         [ -  + ]:          18771 :     Assert(dlist_is_empty(&pgStatPending));
                                620                 :          18771 :     dlist_init(&pgStatPending);
                                621                 :                : 
                                622                 :                :     /* drop the backend stats entry */
  261 michael@paquier.xyz       623         [ -  + ]:          18771 :     if (!pgstat_drop_entry(PGSTAT_KIND_BACKEND, InvalidOid, MyProcNumber))
  261 michael@paquier.xyz       624                 :UBC           0 :         pgstat_request_entry_refs_gc();
                                625                 :                : 
 1249 andres@anarazel.de        626                 :CBC       18771 :     pgstat_detach_shmem();
                                627                 :                : 
                                628                 :                : #ifdef USE_ASSERT_CHECKING
 1265                           629                 :          18771 :     pgstat_is_shutdown = true;
                                630                 :                : #endif
                                631                 :          18771 : }
                                632                 :                : 
                                633                 :                : /*
                                634                 :                :  * Initialize pgstats state, and set up our on-proc-exit hook. Called from
                                635                 :                :  * BaseInit().
                                636                 :                :  *
                                637                 :                :  * NOTE: MyDatabaseId isn't set yet; so the shutdown hook has to be careful.
                                638                 :                :  */
                                639                 :                : void
                                640                 :          18771 : pgstat_initialize(void)
                                641                 :                : {
                                642         [ -  + ]:          18771 :     Assert(!pgstat_is_initialized);
                                643                 :                : 
 1249                           644                 :          18771 :     pgstat_attach_shmem();
                                645                 :                : 
  398 michael@paquier.xyz       646                 :          18771 :     pgstat_init_snapshot_fixed();
                                647                 :                : 
                                648                 :                :     /* Backend initialization callbacks */
  366                           649         [ +  + ]:         619443 :     for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
                                650                 :                :     {
                                651                 :         600672 :         const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                                652                 :                : 
                                653   [ +  +  +  + ]:         600672 :         if (kind_info == NULL || kind_info->init_backend_cb == NULL)
                                654                 :         581901 :             continue;
                                655                 :                : 
                                656                 :          18771 :         kind_info->init_backend_cb();
                                657                 :                :     }
                                658                 :                : 
                                659                 :                :     /* Set up a process-exit hook to clean up */
 1265 andres@anarazel.de        660                 :          18771 :     before_shmem_exit(pgstat_shutdown_hook, 0);
                                661                 :                : 
                                662                 :                : #ifdef USE_ASSERT_CHECKING
                                663                 :          18771 :     pgstat_is_initialized = true;
                                664                 :                : #endif
                                665                 :          18771 : }
                                666                 :                : 
                                667                 :                : 
                                668                 :                : /* ------------------------------------------------------------
                                669                 :                :  * Public functions used by backends follow
                                670                 :                :  * ------------------------------------------------------------
                                671                 :                :  */
                                672                 :                : 
                                673                 :                : /*
                                674                 :                :  * Must be called by processes that performs DML: tcop/postgres.c, logical
                                675                 :                :  * receiver processes, SPI worker, etc. to flush pending statistics updates to
                                676                 :                :  * shared memory.
                                677                 :                :  *
                                678                 :                :  * Unless called with 'force', pending stats updates are flushed happen once
                                679                 :                :  * per PGSTAT_MIN_INTERVAL (1000ms). When not forced, stats flushes do not
                                680                 :                :  * block on lock acquisition, except if stats updates have been pending for
                                681                 :                :  * longer than PGSTAT_MAX_INTERVAL (60000ms).
                                682                 :                :  *
                                683                 :                :  * Whenever pending stats updates remain at the end of pgstat_report_stat() a
                                684                 :                :  * suggested idle timeout is returned. Currently this is always
                                685                 :                :  * PGSTAT_IDLE_INTERVAL (10000ms). Callers can use the returned time to set up
                                686                 :                :  * a timeout after which to call pgstat_report_stat(true), but are not
                                687                 :                :  * required to do so.
                                688                 :                :  *
                                689                 :                :  * Note that this is called only when not within a transaction, so it is fair
                                690                 :                :  * to use transaction stop time as an approximation of current time.
                                691                 :                :  */
                                692                 :                : long
 1249                           693                 :         296012 : pgstat_report_stat(bool force)
                                694                 :                : {
                                695                 :                :     static TimestampTz pending_since = 0;
                                696                 :                :     static TimestampTz last_flush = 0;
                                697                 :                :     bool        partial_flush;
                                698                 :                :     TimestampTz now;
                                699                 :                :     bool        nowait;
                                700                 :                : 
 1492                           701                 :         296012 :     pgstat_assert_is_up();
 1177                           702         [ -  + ]:         296012 :     Assert(!IsTransactionOrTransactionBlock());
                                703                 :                : 
                                704                 :                :     /* "absorb" the forced flush even if there's nothing to flush */
 1249                           705         [ +  + ]:         296012 :     if (pgStatForceNextFlush)
                                706                 :                :     {
                                707                 :            242 :         force = true;
                                708                 :            242 :         pgStatForceNextFlush = false;
                                709                 :                :     }
                                710                 :                : 
                                711                 :                :     /* Don't expend a clock check if nothing to do */
   40 michael@paquier.xyz       712         [ +  + ]:         296012 :     if (dlist_is_empty(&pgStatPending) &&
                                713         [ +  + ]:           8928 :         !pgstat_report_fixed)
                                714                 :                :     {
                                715                 :           6477 :         return 0;
                                716                 :                :     }
                                717                 :                : 
                                718                 :                :     /*
                                719                 :                :      * There should never be stats to report once stats are shut down. Can't
                                720                 :                :      * assert that before the checks above, as there is an unconditional
                                721                 :                :      * pgstat_report_stat() call in pgstat_shutdown_hook() - which at least
                                722                 :                :      * the process that ran pgstat_before_server_shutdown() will still call.
                                723                 :                :      */
 1249 andres@anarazel.de        724         [ -  + ]:         289535 :     Assert(!pgStatLocal.shmem->is_shutdown);
                                725                 :                : 
  817                           726         [ +  + ]:         289535 :     if (force)
                                727                 :                :     {
                                728                 :                :         /*
                                729                 :                :          * Stats reports are forced either when it's been too long since stats
                                730                 :                :          * have been reported or in processes that force stats reporting to
                                731                 :                :          * happen at specific points (including shutdown). In the former case
                                732                 :                :          * the transaction stop time might be quite old, in the latter it
                                733                 :                :          * would never get cleared.
                                734                 :                :          */
                                735                 :          18199 :         now = GetCurrentTimestamp();
                                736                 :                :     }
                                737                 :                :     else
                                738                 :                :     {
                                739                 :         271336 :         now = GetCurrentTransactionStopTimestamp();
                                740                 :                : 
 1249                           741   [ +  +  -  + ]:         516960 :         if (pending_since > 0 &&
                                742                 :         245624 :             TimestampDifferenceExceeds(pending_since, now, PGSTAT_MAX_INTERVAL))
                                743                 :                :         {
                                744                 :                :             /* don't keep pending updates longer than PGSTAT_MAX_INTERVAL */
 1249 andres@anarazel.de        745                 :UBC           0 :             force = true;
                                746                 :                :         }
 1249 andres@anarazel.de        747         [ +  + ]:CBC      271336 :         else if (last_flush > 0 &&
                                748         [ +  + ]:         259407 :                  !TimestampDifferenceExceeds(last_flush, now, PGSTAT_MIN_INTERVAL))
                                749                 :                :         {
                                750                 :                :             /* don't flush too frequently */
                                751         [ +  + ]:         257681 :             if (pending_since == 0)
                                752                 :          13697 :                 pending_since = now;
                                753                 :                : 
                                754                 :         257681 :             return PGSTAT_IDLE_INTERVAL;
                                755                 :                :         }
                                756                 :                :     }
                                757                 :                : 
                                758                 :          31854 :     pgstat_update_dbstats(now);
                                759                 :                : 
                                760                 :                :     /* don't wait for lock acquisition when !force */
                                761                 :          31854 :     nowait = !force;
                                762                 :                : 
                                763                 :          31854 :     partial_flush = false;
                                764                 :                : 
                                765                 :                :     /* flush of variable-numbered stats tracked in pending entries list */
                                766                 :          31854 :     partial_flush |= pgstat_flush_pending_entries(nowait);
                                767                 :                : 
                                768                 :                :     /* flush of other stats kinds */
   40 michael@paquier.xyz       769         [ +  + ]:          31854 :     if (pgstat_report_fixed)
                                770                 :                :     {
                                771         [ +  + ]:        1022835 :         for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
                                772                 :                :         {
                                773                 :         991840 :             const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                                774                 :                : 
                                775         [ +  + ]:         991840 :             if (!kind_info)
                                776                 :         619900 :                 continue;
                                777         [ +  + ]:         371940 :             if (!kind_info->flush_static_cb)
                                778                 :         247960 :                 continue;
                                779                 :                : 
                                780                 :         123980 :             partial_flush |= kind_info->flush_static_cb(nowait);
                                781                 :                :         }
                                782                 :                :     }
                                783                 :                : 
 1249 andres@anarazel.de        784                 :          31854 :     last_flush = now;
                                785                 :                : 
                                786                 :                :     /*
                                787                 :                :      * If some of the pending stats could not be flushed due to lock
                                788                 :                :      * contention, let the caller know when to retry.
                                789                 :                :      */
                                790         [ +  + ]:          31854 :     if (partial_flush)
                                791                 :                :     {
                                792                 :                :         /* force should have prevented us from getting here */
                                793         [ -  + ]:              5 :         Assert(!force);
                                794                 :                : 
                                795                 :                :         /* remember since when stats have been pending */
                                796         [ +  + ]:              5 :         if (pending_since == 0)
                                797                 :              4 :             pending_since = now;
                                798                 :                : 
                                799                 :              5 :         return PGSTAT_IDLE_INTERVAL;
                                800                 :                :     }
                                801                 :                : 
                                802                 :          31849 :     pending_since = 0;
   40 michael@paquier.xyz       803                 :          31849 :     pgstat_report_fixed = false;
                                804                 :                : 
 1249 andres@anarazel.de        805                 :          31849 :     return 0;
                                806                 :                : }
                                807                 :                : 
                                808                 :                : /*
                                809                 :                :  * Force locally pending stats to be flushed during the next
                                810                 :                :  * pgstat_report_stat() call. This is useful for writing tests.
                                811                 :                :  */
                                812                 :                : void
                                813                 :            242 : pgstat_force_next_flush(void)
                                814                 :                : {
                                815                 :            242 :     pgStatForceNextFlush = true;
                                816                 :            242 : }
                                817                 :                : 
                                818                 :                : /*
                                819                 :                :  * Only for use by pgstat_reset_counters()
                                820                 :                :  */
                                821                 :                : static bool
                                822                 :          10675 : match_db_entries(PgStatShared_HashEntry *entry, Datum match_data)
                                823                 :                : {
   29 peter@eisentraut.org      824                 :GNC       10675 :     return entry->key.dboid == MyDatabaseId;
                                825                 :                : }
                                826                 :                : 
                                827                 :                : /*
                                828                 :                :  * Reset counters for our database.
                                829                 :                :  *
                                830                 :                :  * Permission checking for this function is managed through the normal
                                831                 :                :  * GRANT system.
                                832                 :                :  */
                                833                 :                : void
 1249 andres@anarazel.de        834                 :CBC           7 : pgstat_reset_counters(void)
                                835                 :                : {
                                836                 :              7 :     TimestampTz ts = GetCurrentTimestamp();
                                837                 :                : 
                                838                 :              7 :     pgstat_reset_matching_entries(match_db_entries,
                                839                 :                :                                   ObjectIdGetDatum(MyDatabaseId),
                                840                 :                :                                   ts);
                                841                 :              7 : }
                                842                 :                : 
                                843                 :                : /*
                                844                 :                :  * Reset a single variable-numbered entry.
                                845                 :                :  *
                                846                 :                :  * If the stats kind is within a database, also reset the database's
                                847                 :                :  * stat_reset_timestamp.
                                848                 :                :  *
                                849                 :                :  * Permission checking for this function is managed through the normal
                                850                 :                :  * GRANT system.
                                851                 :                :  */
                                852                 :                : void
  353 michael@paquier.xyz       853                 :             22 : pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
                                854                 :                : {
 1249 andres@anarazel.de        855                 :             22 :     const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                                856                 :             22 :     TimestampTz ts = GetCurrentTimestamp();
                                857                 :                : 
                                858                 :                :     /* not needed atm, and doesn't make sense with the current signature */
                                859         [ -  + ]:             22 :     Assert(!pgstat_get_kind_info(kind)->fixed_amount);
                                860                 :                : 
                                861                 :                :     /* reset the "single counter" */
  353 michael@paquier.xyz       862                 :             22 :     pgstat_reset_entry(kind, dboid, objid, ts);
                                863                 :                : 
 1249 andres@anarazel.de        864         [ +  + ]:             22 :     if (!kind_info->accessed_across_databases)
                                865                 :              8 :         pgstat_reset_database_timestamp(dboid, ts);
                                866                 :             22 : }
                                867                 :                : 
                                868                 :                : /*
                                869                 :                :  * Reset stats for all entries of a kind.
                                870                 :                :  *
                                871                 :                :  * Permission checking for this function is managed through the normal
                                872                 :                :  * GRANT system.
                                873                 :                :  */
                                874                 :                : void
                                875                 :             29 : pgstat_reset_of_kind(PgStat_Kind kind)
                                876                 :                : {
                                877                 :             29 :     const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                                878                 :             29 :     TimestampTz ts = GetCurrentTimestamp();
                                879                 :                : 
                                880         [ +  + ]:             29 :     if (kind_info->fixed_amount)
                                881                 :             25 :         kind_info->reset_all_cb(ts);
                                882                 :                :     else
                                883                 :              4 :         pgstat_reset_entries_of_kind(kind, ts);
                                884                 :             29 : }
                                885                 :                : 
                                886                 :                : 
                                887                 :                : /* ------------------------------------------------------------
                                888                 :                :  * Fetching of stats
                                889                 :                :  * ------------------------------------------------------------
                                890                 :                :  */
                                891                 :                : 
                                892                 :                : /*
                                893                 :                :  * Discard any data collected in the current transaction.  Any subsequent
                                894                 :                :  * request will cause new snapshots to be read.
                                895                 :                :  *
                                896                 :                :  * This is also invoked during transaction commit or abort to discard
                                897                 :                :  * the no-longer-wanted snapshot.  Updates of stats_fetch_consistency can
                                898                 :                :  * cause this routine to be called.
                                899                 :                :  */
                                900                 :                : void
                                901                 :         318945 : pgstat_clear_snapshot(void)
                                902                 :                : {
                                903                 :         318945 :     pgstat_assert_is_up();
                                904                 :                : 
                                905                 :         318945 :     memset(&pgStatLocal.snapshot.fixed_valid, 0,
                                906                 :                :            sizeof(pgStatLocal.snapshot.fixed_valid));
  398 michael@paquier.xyz       907                 :         318945 :     memset(&pgStatLocal.snapshot.custom_valid, 0,
                                908                 :                :            sizeof(pgStatLocal.snapshot.custom_valid));
 1249 andres@anarazel.de        909                 :         318945 :     pgStatLocal.snapshot.stats = NULL;
                                910                 :         318945 :     pgStatLocal.snapshot.mode = PGSTAT_FETCH_CONSISTENCY_NONE;
                                911                 :                : 
                                912                 :                :     /* Release memory, if any was allocated */
                                913         [ +  + ]:         318945 :     if (pgStatLocal.snapshot.context)
                                914                 :                :     {
                                915                 :            567 :         MemoryContextDelete(pgStatLocal.snapshot.context);
                                916                 :                : 
                                917                 :                :         /* Reset variables */
                                918                 :            567 :         pgStatLocal.snapshot.context = NULL;
                                919                 :                :     }
                                920                 :                : 
                                921                 :                :     /*
                                922                 :                :      * Historically the backend_status.c facilities lived in this file, and
                                923                 :                :      * were reset with the same function. For now keep it that way, and
                                924                 :                :      * forward the reset request.
                                925                 :                :      */
                                926                 :         318945 :     pgstat_clear_backend_activity_snapshot();
                                927                 :                : 
                                928                 :                :     /* Reset this flag, as it may be possible that a cleanup was forced. */
  850 michael@paquier.xyz       929                 :         318945 :     force_stats_snapshot_clear = false;
 1249 andres@anarazel.de        930                 :         318945 : }
                                931                 :                : 
                                932                 :                : void *
  353 michael@paquier.xyz       933                 :          12882 : pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
                                934                 :                : {
                                935                 :                :     PgStat_HashKey key;
                                936                 :                :     PgStat_EntryRef *entry_ref;
                                937                 :                :     void       *stats_data;
 1249 andres@anarazel.de        938                 :          12882 :     const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                                939                 :                : 
                                940                 :                :     /* should be called from backends */
                                941   [ -  +  -  - ]:          12882 :     Assert(IsUnderPostmaster || !IsPostmasterEnvironment);
 1044 peter@eisentraut.org      942         [ -  + ]:          12882 :     Assert(!kind_info->fixed_amount);
                                943                 :                : 
 1249 andres@anarazel.de        944                 :          12882 :     pgstat_prep_snapshot();
                                945                 :                : 
                                946                 :                :     /* clear padding */
  305 michael@paquier.xyz       947                 :          12882 :     memset(&key, 0, sizeof(struct PgStat_HashKey));
                                948                 :                : 
 1249 andres@anarazel.de        949                 :          12882 :     key.kind = kind;
                                950                 :          12882 :     key.dboid = dboid;
  353 michael@paquier.xyz       951                 :          12882 :     key.objid = objid;
                                952                 :                : 
                                953                 :                :     /* if we need to build a full snapshot, do so */
 1249 andres@anarazel.de        954         [ +  + ]:          12882 :     if (pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_SNAPSHOT)
                                955                 :            230 :         pgstat_build_snapshot();
                                956                 :                : 
                                957                 :                :     /* if caching is desired, look up in cache */
                                958         [ +  + ]:          12882 :     if (pgstat_fetch_consistency > PGSTAT_FETCH_CONSISTENCY_NONE)
                                959                 :                :     {
                                960                 :           4933 :         PgStat_SnapshotEntry *entry = NULL;
                                961                 :                : 
                                962                 :           4933 :         entry = pgstat_snapshot_lookup(pgStatLocal.snapshot.stats, key);
                                963                 :                : 
                                964         [ +  + ]:           4933 :         if (entry)
                                965                 :            382 :             return entry->data;
                                966                 :                : 
                                967                 :                :         /*
                                968                 :                :          * If we built a full snapshot and the key is not in
                                969                 :                :          * pgStatLocal.snapshot.stats, there are no matching stats.
                                970                 :                :          */
                                971         [ +  + ]:           4551 :         if (pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_SNAPSHOT)
                                972                 :             14 :             return NULL;
                                973                 :                :     }
                                974                 :                : 
                                975                 :          12486 :     pgStatLocal.snapshot.mode = pgstat_fetch_consistency;
                                976                 :                : 
  353 michael@paquier.xyz       977                 :          12486 :     entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
                                978                 :                : 
 1249 andres@anarazel.de        979   [ +  +  +  + ]:          12486 :     if (entry_ref == NULL || entry_ref->shared_entry->dropped)
                                980                 :                :     {
                                981                 :                :         /* create empty entry when using PGSTAT_FETCH_CONSISTENCY_CACHE */
                                982         [ +  + ]:           3524 :         if (pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_CACHE)
                                983                 :                :         {
                                984                 :            896 :             PgStat_SnapshotEntry *entry = NULL;
                                985                 :                :             bool        found;
                                986                 :                : 
                                987                 :            896 :             entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
                                988         [ -  + ]:            896 :             Assert(!found);
                                989                 :            896 :             entry->data = NULL;
                                990                 :                :         }
                                991                 :           3524 :         return NULL;
                                992                 :                :     }
                                993                 :                : 
                                994                 :                :     /*
                                995                 :                :      * Allocate in caller's context for PGSTAT_FETCH_CONSISTENCY_NONE,
                                996                 :                :      * otherwise we could quickly end up with a fair bit of memory used due to
                                997                 :                :      * repeated accesses.
                                998                 :                :      */
                                999         [ +  + ]:           8962 :     if (pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_NONE)
                               1000                 :           5321 :         stats_data = palloc(kind_info->shared_data_len);
                               1001                 :                :     else
                               1002                 :           3641 :         stats_data = MemoryContextAlloc(pgStatLocal.snapshot.context,
                               1003                 :           3641 :                                         kind_info->shared_data_len);
                               1004                 :                : 
  153 tgl@sss.pgh.pa.us        1005                 :           8962 :     (void) pgstat_lock_entry_shared(entry_ref, false);
 1249 andres@anarazel.de       1006                 :          17924 :     memcpy(stats_data,
                               1007                 :           8962 :            pgstat_get_entry_data(kind, entry_ref->shared_stats),
                               1008                 :           8962 :            kind_info->shared_data_len);
 1111                          1009                 :           8962 :     pgstat_unlock_entry(entry_ref);
                               1010                 :                : 
 1249                          1011         [ +  + ]:           8962 :     if (pgstat_fetch_consistency > PGSTAT_FETCH_CONSISTENCY_NONE)
                               1012                 :                :     {
                               1013                 :           3641 :         PgStat_SnapshotEntry *entry = NULL;
                               1014                 :                :         bool        found;
                               1015                 :                : 
                               1016                 :           3641 :         entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
                               1017                 :           3641 :         entry->data = stats_data;
                               1018                 :                :     }
                               1019                 :                : 
                               1020                 :           8962 :     return stats_data;
                               1021                 :                : }
                               1022                 :                : 
                               1023                 :                : /*
                               1024                 :                :  * If a stats snapshot has been taken, return the timestamp at which that was
                               1025                 :                :  * done, and set *have_snapshot to true. Otherwise *have_snapshot is set to
                               1026                 :                :  * false.
                               1027                 :                :  */
                               1028                 :                : TimestampTz
                               1029                 :             30 : pgstat_get_stat_snapshot_timestamp(bool *have_snapshot)
                               1030                 :                : {
  850 michael@paquier.xyz      1031         [ +  + ]:             30 :     if (force_stats_snapshot_clear)
                               1032                 :              9 :         pgstat_clear_snapshot();
                               1033                 :                : 
 1249 andres@anarazel.de       1034         [ +  + ]:             30 :     if (pgStatLocal.snapshot.mode == PGSTAT_FETCH_CONSISTENCY_SNAPSHOT)
                               1035                 :                :     {
                               1036                 :             12 :         *have_snapshot = true;
                               1037                 :             12 :         return pgStatLocal.snapshot.snapshot_timestamp;
                               1038                 :                :     }
                               1039                 :                : 
                               1040                 :             18 :     *have_snapshot = false;
                               1041                 :                : 
                               1042                 :             18 :     return 0;
                               1043                 :                : }
                               1044                 :                : 
                               1045                 :                : bool
  353 michael@paquier.xyz      1046                 :             80 : pgstat_have_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
                               1047                 :                : {
                               1048                 :                :     /* fixed-numbered stats always exist */
 1248 andres@anarazel.de       1049         [ +  + ]:             80 :     if (pgstat_get_kind_info(kind)->fixed_amount)
                               1050                 :              6 :         return true;
                               1051                 :                : 
  353 michael@paquier.xyz      1052                 :             74 :     return pgstat_get_entry_ref(kind, dboid, objid, false, NULL) != NULL;
                               1053                 :                : }
                               1054                 :                : 
                               1055                 :                : /*
                               1056                 :                :  * Ensure snapshot for fixed-numbered 'kind' exists.
                               1057                 :                :  *
                               1058                 :                :  * Typically used by the pgstat_fetch_* functions for a kind of stats, before
                               1059                 :                :  * massaging the data into the desired format.
                               1060                 :                :  */
                               1061                 :                : void
 1249 andres@anarazel.de       1062                 :            219 : pgstat_snapshot_fixed(PgStat_Kind kind)
                               1063                 :                : {
 1044 peter@eisentraut.org     1064         [ -  + ]:            219 :     Assert(pgstat_is_kind_valid(kind));
                               1065         [ -  + ]:            219 :     Assert(pgstat_get_kind_info(kind)->fixed_amount);
                               1066                 :                : 
  583 michael@paquier.xyz      1067         [ -  + ]:            219 :     if (force_stats_snapshot_clear)
  583 michael@paquier.xyz      1068                 :UBC           0 :         pgstat_clear_snapshot();
                               1069                 :                : 
 1249 andres@anarazel.de       1070         [ +  + ]:CBC         219 :     if (pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_SNAPSHOT)
                               1071                 :             12 :         pgstat_build_snapshot();
                               1072                 :                :     else
                               1073                 :            207 :         pgstat_build_snapshot_fixed(kind);
                               1074                 :                : 
  398 michael@paquier.xyz      1075         [ +  - ]:            219 :     if (pgstat_is_kind_builtin(kind))
                               1076         [ -  + ]:            219 :         Assert(pgStatLocal.snapshot.fixed_valid[kind]);
  398 michael@paquier.xyz      1077         [ #  # ]:UBC           0 :     else if (pgstat_is_kind_custom(kind))
                               1078         [ #  # ]:              0 :         Assert(pgStatLocal.snapshot.custom_valid[kind - PGSTAT_KIND_CUSTOM_MIN]);
  398 michael@paquier.xyz      1079                 :CBC         219 : }
                               1080                 :                : 
                               1081                 :                : static void
                               1082                 :          18771 : pgstat_init_snapshot_fixed(void)
                               1083                 :                : {
                               1084                 :                :     /*
                               1085                 :                :      * Initialize fixed-numbered statistics data in snapshots, only for custom
                               1086                 :                :      * stats kinds.
                               1087                 :                :      */
                               1088         [ +  + ]:         187710 :     for (PgStat_Kind kind = PGSTAT_KIND_CUSTOM_MIN; kind <= PGSTAT_KIND_CUSTOM_MAX; kind++)
                               1089                 :                :     {
                               1090                 :         168939 :         const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                               1091                 :                : 
                               1092   [ -  +  -  - ]:         168939 :         if (!kind_info || !kind_info->fixed_amount)
                               1093                 :         168939 :             continue;
                               1094                 :                : 
  398 michael@paquier.xyz      1095                 :UBC           0 :         pgStatLocal.snapshot.custom_data[kind - PGSTAT_KIND_CUSTOM_MIN] =
                               1096                 :              0 :             MemoryContextAlloc(TopMemoryContext, kind_info->shared_data_len);
                               1097                 :                :     }
 8842 JanWieck@Yahoo.com       1098                 :CBC       18771 : }
                               1099                 :                : 
                               1100                 :                : static void
 1249 andres@anarazel.de       1101                 :          12908 : pgstat_prep_snapshot(void)
                               1102                 :                : {
  850 michael@paquier.xyz      1103         [ +  + ]:          12908 :     if (force_stats_snapshot_clear)
                               1104                 :              9 :         pgstat_clear_snapshot();
                               1105                 :                : 
 1249 andres@anarazel.de       1106         [ +  + ]:          12908 :     if (pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_NONE ||
                               1107         [ +  + ]:           4959 :         pgStatLocal.snapshot.stats != NULL)
 5709 magnus@hagander.net      1108                 :          12341 :         return;
                               1109                 :                : 
 1249 andres@anarazel.de       1110         [ +  - ]:            567 :     if (!pgStatLocal.snapshot.context)
                               1111                 :            567 :         pgStatLocal.snapshot.context = AllocSetContextCreate(TopMemoryContext,
                               1112                 :                :                                                              "PgStat Snapshot",
                               1113                 :                :                                                              ALLOCSET_SMALL_SIZES);
                               1114                 :                : 
                               1115                 :            567 :     pgStatLocal.snapshot.stats =
                               1116                 :            567 :         pgstat_snapshot_create(pgStatLocal.snapshot.context,
                               1117                 :                :                                PGSTAT_SNAPSHOT_HASH_SIZE,
                               1118                 :                :                                NULL);
                               1119                 :                : }
                               1120                 :                : 
                               1121                 :                : static void
                               1122                 :            242 : pgstat_build_snapshot(void)
                               1123                 :                : {
                               1124                 :                :     dshash_seq_status hstat;
                               1125                 :                :     PgStatShared_HashEntry *p;
                               1126                 :                : 
                               1127                 :                :     /* should only be called when we need a snapshot */
                               1128         [ -  + ]:            242 :     Assert(pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_SNAPSHOT);
                               1129                 :                : 
                               1130                 :                :     /* snapshot already built */
                               1131         [ +  + ]:            242 :     if (pgStatLocal.snapshot.mode == PGSTAT_FETCH_CONSISTENCY_SNAPSHOT)
                               1132                 :            216 :         return;
                               1133                 :                : 
                               1134                 :             26 :     pgstat_prep_snapshot();
                               1135                 :                : 
                               1136         [ -  + ]:             26 :     Assert(pgStatLocal.snapshot.stats->members == 0);
                               1137                 :                : 
                               1138                 :             26 :     pgStatLocal.snapshot.snapshot_timestamp = GetCurrentTimestamp();
                               1139                 :                : 
                               1140                 :                :     /*
                               1141                 :                :      * Snapshot all variable stats.
                               1142                 :                :      */
                               1143                 :             26 :     dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
                               1144         [ +  + ]:          27353 :     while ((p = dshash_seq_next(&hstat)) != NULL)
                               1145                 :                :     {
                               1146                 :          27327 :         PgStat_Kind kind = p->key.kind;
                               1147                 :          27327 :         const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                               1148                 :                :         bool        found;
                               1149                 :                :         PgStat_SnapshotEntry *entry;
                               1150                 :                :         PgStatShared_Common *stats_data;
                               1151                 :                : 
                               1152                 :                :         /*
                               1153                 :                :          * Check if the stats object should be included in the snapshot.
                               1154                 :                :          * Unless the stats kind can be accessed from all databases (e.g.,
                               1155                 :                :          * database stats themselves), we only include stats for the current
                               1156                 :                :          * database or objects not associated with a database (e.g. shared
                               1157                 :                :          * relations).
                               1158                 :                :          */
                               1159         [ +  + ]:          27327 :         if (p->key.dboid != MyDatabaseId &&
                               1160         [ +  + ]:           8362 :             p->key.dboid != InvalidOid &&
                               1161         [ +  + ]:           6924 :             !kind_info->accessed_across_databases)
                               1162                 :           6948 :             continue;
                               1163                 :                : 
                               1164         [ +  + ]:          20481 :         if (p->dropped)
                               1165                 :            102 :             continue;
                               1166                 :                : 
                               1167         [ -  + ]:          20379 :         Assert(pg_atomic_read_u32(&p->refcount) > 0);
                               1168                 :                : 
                               1169                 :          20379 :         stats_data = dsa_get_address(pgStatLocal.dsa, p->body);
                               1170         [ -  + ]:          20379 :         Assert(stats_data);
                               1171                 :                : 
                               1172                 :          20379 :         entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, p->key, &found);
                               1173         [ -  + ]:          20379 :         Assert(!found);
                               1174                 :                : 
                               1175                 :          20379 :         entry->data = MemoryContextAlloc(pgStatLocal.snapshot.context,
                               1176                 :                :                                          pgstat_get_entry_len(kind));
                               1177                 :                : 
                               1178                 :                :         /*
                               1179                 :                :          * Acquire the LWLock directly instead of using
                               1180                 :                :          * pg_stat_lock_entry_shared() which requires a reference.
                               1181                 :                :          */
 1111                          1182                 :          20379 :         LWLockAcquire(&stats_data->lock, LW_SHARED);
 1249                          1183                 :          20379 :         memcpy(entry->data,
                               1184                 :          20379 :                pgstat_get_entry_data(kind, stats_data),
                               1185                 :                :                pgstat_get_entry_len(kind));
 1111                          1186                 :          20379 :         LWLockRelease(&stats_data->lock);
                               1187                 :                :     }
 1249                          1188                 :             26 :     dshash_seq_term(&hstat);
                               1189                 :                : 
                               1190                 :                :     /*
                               1191                 :                :      * Build snapshot of all fixed-numbered stats.
                               1192                 :                :      */
  398 michael@paquier.xyz      1193         [ +  + ]:            858 :     for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
                               1194                 :                :     {
 1249 andres@anarazel.de       1195                 :            832 :         const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                               1196                 :                : 
  398 michael@paquier.xyz      1197         [ +  + ]:            832 :         if (!kind_info)
                               1198                 :            520 :             continue;
 1249 andres@anarazel.de       1199         [ +  + ]:            312 :         if (!kind_info->fixed_amount)
                               1200                 :                :         {
                               1201         [ -  + ]:            156 :             Assert(kind_info->snapshot_cb == NULL);
                               1202                 :            156 :             continue;
                               1203                 :                :         }
                               1204                 :                : 
                               1205                 :            156 :         pgstat_build_snapshot_fixed(kind);
                               1206                 :                :     }
                               1207                 :                : 
                               1208                 :             26 :     pgStatLocal.snapshot.mode = PGSTAT_FETCH_CONSISTENCY_SNAPSHOT;
                               1209                 :                : }
                               1210                 :                : 
                               1211                 :                : static void
                               1212                 :           3993 : pgstat_build_snapshot_fixed(PgStat_Kind kind)
                               1213                 :                : {
                               1214                 :           3993 :     const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                               1215                 :                :     int         idx;
                               1216                 :                :     bool       *valid;
                               1217                 :                : 
                               1218                 :                :     /* Position in fixed_valid or custom_valid */
  398 michael@paquier.xyz      1219         [ +  - ]:           3993 :     if (pgstat_is_kind_builtin(kind))
                               1220                 :                :     {
                               1221                 :           3993 :         idx = kind;
                               1222                 :           3993 :         valid = pgStatLocal.snapshot.fixed_valid;
                               1223                 :                :     }
                               1224                 :                :     else
                               1225                 :                :     {
  398 michael@paquier.xyz      1226                 :UBC           0 :         idx = kind - PGSTAT_KIND_CUSTOM_MIN;
                               1227                 :              0 :         valid = pgStatLocal.snapshot.custom_valid;
                               1228                 :                :     }
                               1229                 :                : 
 1249 andres@anarazel.de       1230         [ -  + ]:CBC        3993 :     Assert(kind_info->fixed_amount);
                               1231         [ -  + ]:           3993 :     Assert(kind_info->snapshot_cb != NULL);
                               1232                 :                : 
                               1233         [ +  + ]:           3993 :     if (pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_NONE)
                               1234                 :                :     {
                               1235                 :                :         /* rebuild every time */
  398 michael@paquier.xyz      1236                 :           3645 :         valid[idx] = false;
                               1237                 :                :     }
                               1238         [ +  + ]:            348 :     else if (valid[idx])
                               1239                 :                :     {
                               1240                 :                :         /* in snapshot mode we shouldn't get called again */
 1249 andres@anarazel.de       1241         [ -  + ]:              6 :         Assert(pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_CACHE);
                               1242                 :              6 :         return;
                               1243                 :                :     }
                               1244                 :                : 
  398 michael@paquier.xyz      1245         [ -  + ]:           3987 :     Assert(!valid[idx]);
                               1246                 :                : 
 1249 andres@anarazel.de       1247                 :           3987 :     kind_info->snapshot_cb();
                               1248                 :                : 
  398 michael@paquier.xyz      1249         [ -  + ]:           3987 :     Assert(!valid[idx]);
                               1250                 :           3987 :     valid[idx] = true;
                               1251                 :                : }
                               1252                 :                : 
                               1253                 :                : 
                               1254                 :                : /* ------------------------------------------------------------
                               1255                 :                :  * Backend-local pending stats infrastructure
                               1256                 :                :  * ------------------------------------------------------------
                               1257                 :                :  */
                               1258                 :                : 
                               1259                 :                : /*
                               1260                 :                :  * Returns the appropriate PgStat_EntryRef, preparing it to receive pending
                               1261                 :                :  * stats if not already done.
                               1262                 :                :  *
                               1263                 :                :  * If created_entry is non-NULL, it'll be set to true if the entry is newly
                               1264                 :                :  * created, false otherwise.
                               1265                 :                :  */
                               1266                 :                : PgStat_EntryRef *
  353                          1267                 :        1672175 : pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *created_entry)
                               1268                 :                : {
                               1269                 :                :     PgStat_EntryRef *entry_ref;
                               1270                 :                : 
                               1271                 :                :     /* need to be able to flush out */
 1249 andres@anarazel.de       1272         [ -  + ]:        1672175 :     Assert(pgstat_get_kind_info(kind)->flush_pending_cb != NULL);
                               1273                 :                : 
                               1274         [ +  + ]:        1672175 :     if (unlikely(!pgStatPendingContext))
                               1275                 :                :     {
                               1276                 :          14842 :         pgStatPendingContext =
 1086                          1277                 :          14842 :             AllocSetContextCreate(TopMemoryContext,
                               1278                 :                :                                   "PgStat Pending",
                               1279                 :                :                                   ALLOCSET_SMALL_SIZES);
                               1280                 :                :     }
                               1281                 :                : 
  353 michael@paquier.xyz      1282                 :        1672175 :     entry_ref = pgstat_get_entry_ref(kind, dboid, objid,
                               1283                 :                :                                      true, created_entry);
                               1284                 :                : 
 1249 andres@anarazel.de       1285         [ +  + ]:        1672175 :     if (entry_ref->pending == NULL)
                               1286                 :                :     {
                               1287                 :         858185 :         size_t      entrysize = pgstat_get_kind_info(kind)->pending_size;
                               1288                 :                : 
                               1289         [ -  + ]:         858185 :         Assert(entrysize != (size_t) -1);
                               1290                 :                : 
                               1291                 :         858185 :         entry_ref->pending = MemoryContextAllocZero(pgStatPendingContext, entrysize);
                               1292                 :         858185 :         dlist_push_tail(&pgStatPending, &entry_ref->pending_node);
                               1293                 :                :     }
                               1294                 :                : 
                               1295                 :        1672175 :     return entry_ref;
                               1296                 :                : }
                               1297                 :                : 
                               1298                 :                : /*
                               1299                 :                :  * Return an existing stats entry, or NULL.
                               1300                 :                :  *
                               1301                 :                :  * This should only be used for helper function for pgstatfuncs.c - outside of
                               1302                 :                :  * that it shouldn't be needed.
                               1303                 :                :  */
                               1304                 :                : PgStat_EntryRef *
  353 michael@paquier.xyz      1305                 :             42 : pgstat_fetch_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
                               1306                 :                : {
                               1307                 :                :     PgStat_EntryRef *entry_ref;
                               1308                 :                : 
                               1309                 :             42 :     entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
                               1310                 :                : 
 1249 andres@anarazel.de       1311   [ +  +  +  + ]:             42 :     if (entry_ref == NULL || entry_ref->pending == NULL)
                               1312                 :             15 :         return NULL;
                               1313                 :                : 
                               1314                 :             27 :     return entry_ref;
                               1315                 :                : }
                               1316                 :                : 
                               1317                 :                : void
                               1318                 :         858185 : pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref)
                               1319                 :                : {
                               1320                 :         858185 :     PgStat_Kind kind = entry_ref->shared_entry->key.kind;
                               1321                 :         858185 :     const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                               1322                 :         858185 :     void       *pending_data = entry_ref->pending;
                               1323                 :                : 
                               1324         [ -  + ]:         858185 :     Assert(pending_data != NULL);
                               1325                 :                :     /* !fixed_amount stats should be handled explicitly */
                               1326         [ -  + ]:         858185 :     Assert(!pgstat_get_kind_info(kind)->fixed_amount);
                               1327                 :                : 
                               1328         [ +  + ]:         858185 :     if (kind_info->delete_pending_cb)
                               1329                 :         806375 :         kind_info->delete_pending_cb(entry_ref);
                               1330                 :                : 
                               1331                 :         858185 :     pfree(pending_data);
                               1332                 :         858185 :     entry_ref->pending = NULL;
                               1333                 :                : 
                               1334                 :         858185 :     dlist_delete(&entry_ref->pending_node);
 1794 akapila@postgresql.o     1335                 :         858185 : }
                               1336                 :                : 
                               1337                 :                : /*
                               1338                 :                :  * Flush out pending variable-numbered stats.
                               1339                 :                :  */
                               1340                 :                : static bool
 1249 andres@anarazel.de       1341                 :          31854 : pgstat_flush_pending_entries(bool nowait)
                               1342                 :                : {
                               1343                 :          31854 :     bool        have_pending = false;
                               1344                 :          31854 :     dlist_node *cur = NULL;
                               1345                 :                : 
                               1346                 :                :     /*
                               1347                 :                :      * Need to be a bit careful iterating over the list of pending entries.
                               1348                 :                :      * Processing a pending entry may queue further pending entries to the end
                               1349                 :                :      * of the list that we want to process, so a simple iteration won't do.
                               1350                 :                :      * Further complicating matters is that we want to delete the current
                               1351                 :                :      * entry in each iteration from the list if we flushed successfully.
                               1352                 :                :      *
                               1353                 :                :      * So we just keep track of the next pointer in each loop iteration.
                               1354                 :                :      */
                               1355         [ +  + ]:          31854 :     if (!dlist_is_empty(&pgStatPending))
                               1356                 :          29582 :         cur = dlist_head_node(&pgStatPending);
                               1357                 :                : 
                               1358         [ +  + ]:         857759 :     while (cur)
                               1359                 :                :     {
                               1360                 :         825905 :         PgStat_EntryRef *entry_ref =
  841 tgl@sss.pgh.pa.us        1361                 :         825905 :             dlist_container(PgStat_EntryRef, pending_node, cur);
 1249 andres@anarazel.de       1362                 :         825905 :         PgStat_HashKey key = entry_ref->shared_entry->key;
                               1363                 :         825905 :         PgStat_Kind kind = key.kind;
                               1364                 :         825905 :         const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                               1365                 :                :         bool        did_flush;
                               1366                 :                :         dlist_node *next;
                               1367                 :                : 
                               1368         [ -  + ]:         825905 :         Assert(!kind_info->fixed_amount);
                               1369         [ -  + ]:         825905 :         Assert(kind_info->flush_pending_cb != NULL);
                               1370                 :                : 
                               1371                 :                :         /* flush the stats, if possible */
                               1372                 :         825905 :         did_flush = kind_info->flush_pending_cb(entry_ref, nowait);
                               1373                 :                : 
                               1374   [ +  +  -  + ]:         825905 :         Assert(did_flush || nowait);
                               1375                 :                : 
                               1376                 :                :         /* determine next entry, before deleting the pending entry */
                               1377         [ +  + ]:         825905 :         if (dlist_has_next(&pgStatPending, cur))
                               1378                 :         796323 :             next = dlist_next_node(&pgStatPending, cur);
                               1379                 :                :         else
                               1380                 :          29582 :             next = NULL;
                               1381                 :                : 
                               1382                 :                :         /* if successfully flushed, remove entry */
                               1383         [ +  + ]:         825905 :         if (did_flush)
                               1384                 :         825902 :             pgstat_delete_pending_entry(entry_ref);
                               1385                 :                :         else
                               1386                 :              3 :             have_pending = true;
                               1387                 :                : 
                               1388                 :         825905 :         cur = next;
                               1389                 :                :     }
                               1390                 :                : 
                               1391         [ -  + ]:          31854 :     Assert(dlist_is_empty(&pgStatPending) == !have_pending);
                               1392                 :                : 
                               1393                 :          31854 :     return have_pending;
                               1394                 :                : }
                               1395                 :                : 
                               1396                 :                : 
                               1397                 :                : /* ------------------------------------------------------------
                               1398                 :                :  * Helper / infrastructure functions
                               1399                 :                :  * ------------------------------------------------------------
                               1400                 :                :  */
                               1401                 :                : 
                               1402                 :                : PgStat_Kind
                               1403                 :             83 : pgstat_get_kind_from_str(char *kind_str)
                               1404                 :                : {
  398 michael@paquier.xyz      1405         [ +  + ]:            247 :     for (PgStat_Kind kind = PGSTAT_KIND_BUILTIN_MIN; kind <= PGSTAT_KIND_BUILTIN_MAX; kind++)
                               1406                 :                :     {
                               1407         [ +  + ]:            244 :         if (pg_strcasecmp(kind_str, pgstat_kind_builtin_infos[kind].name) == 0)
 1249 andres@anarazel.de       1408                 :             80 :             return kind;
                               1409                 :                :     }
                               1410                 :                : 
                               1411                 :                :     /* Check the custom set of cumulative stats */
  398 michael@paquier.xyz      1412         [ -  + ]:              3 :     if (pgstat_kind_custom_infos)
                               1413                 :                :     {
  398 michael@paquier.xyz      1414         [ #  # ]:UBC           0 :         for (PgStat_Kind kind = PGSTAT_KIND_CUSTOM_MIN; kind <= PGSTAT_KIND_CUSTOM_MAX; kind++)
                               1415                 :                :         {
                               1416                 :              0 :             uint32      idx = kind - PGSTAT_KIND_CUSTOM_MIN;
                               1417                 :                : 
                               1418   [ #  #  #  # ]:              0 :             if (pgstat_kind_custom_infos[idx] &&
                               1419                 :              0 :                 pg_strcasecmp(kind_str, pgstat_kind_custom_infos[idx]->name) == 0)
                               1420                 :              0 :                 return kind;
                               1421                 :                :         }
                               1422                 :                :     }
                               1423                 :                : 
 1249 andres@anarazel.de       1424         [ +  - ]:CBC           3 :     ereport(ERROR,
                               1425                 :                :             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1426                 :                :              errmsg("invalid statistics kind: \"%s\"", kind_str)));
                               1427                 :                :     return PGSTAT_KIND_INVALID; /* avoid compiler warnings */
                               1428                 :                : }
                               1429                 :                : 
                               1430                 :                : static inline bool
  400 michael@paquier.xyz      1431                 :         371617 : pgstat_is_kind_valid(PgStat_Kind kind)
                               1432                 :                : {
  398                          1433   [ -  +  -  - ]:         371617 :     return pgstat_is_kind_builtin(kind) || pgstat_is_kind_custom(kind);
                               1434                 :                : }
                               1435                 :                : 
                               1436                 :                : const PgStat_KindInfo *
 1249 andres@anarazel.de       1437                 :        8465986 : pgstat_get_kind_info(PgStat_Kind kind)
                               1438                 :                : {
  398 michael@paquier.xyz      1439         [ +  + ]:        8465986 :     if (pgstat_is_kind_builtin(kind))
                               1440                 :        7237645 :         return &pgstat_kind_builtin_infos[kind];
                               1441                 :                : 
                               1442         [ +  + ]:        1228341 :     if (pgstat_is_kind_custom(kind))
                               1443                 :                :     {
                               1444                 :         660213 :         uint32      idx = kind - PGSTAT_KIND_CUSTOM_MIN;
                               1445                 :                : 
                               1446         [ -  + ]:         660213 :         if (pgstat_kind_custom_infos == NULL ||
  398 michael@paquier.xyz      1447         [ #  # ]:UBC           0 :             pgstat_kind_custom_infos[idx] == NULL)
  398 michael@paquier.xyz      1448                 :CBC      660213 :             return NULL;
  398 michael@paquier.xyz      1449                 :UBC           0 :         return pgstat_kind_custom_infos[idx];
                               1450                 :                :     }
                               1451                 :                : 
  398 michael@paquier.xyz      1452                 :CBC      568128 :     return NULL;
                               1453                 :                : }
                               1454                 :                : 
                               1455                 :                : /*
                               1456                 :                :  * Register a new stats kind.
                               1457                 :                :  *
                               1458                 :                :  * PgStat_Kinds must be globally unique across all extensions. Refer
                               1459                 :                :  * to https://wiki.postgresql.org/wiki/CustomCumulativeStats to reserve a
                               1460                 :                :  * unique ID for your extension, to avoid conflicts with other extension
                               1461                 :                :  * developers. During development, use PGSTAT_KIND_EXPERIMENTAL to avoid
                               1462                 :                :  * needlessly reserving a new ID.
                               1463                 :                :  */
                               1464                 :                : void
  398 michael@paquier.xyz      1465                 :UBC           0 : pgstat_register_kind(PgStat_Kind kind, const PgStat_KindInfo *kind_info)
                               1466                 :                : {
                               1467                 :              0 :     uint32      idx = kind - PGSTAT_KIND_CUSTOM_MIN;
                               1468                 :                : 
                               1469   [ #  #  #  # ]:              0 :     if (kind_info->name == NULL || strlen(kind_info->name) == 0)
                               1470         [ #  # ]:              0 :         ereport(ERROR,
                               1471                 :                :                 (errmsg("custom cumulative statistics name is invalid"),
                               1472                 :                :                  errhint("Provide a non-empty name for the custom cumulative statistics.")));
                               1473                 :                : 
                               1474         [ #  # ]:              0 :     if (!pgstat_is_kind_custom(kind))
                               1475         [ #  # ]:              0 :         ereport(ERROR, (errmsg("custom cumulative statistics ID %u is out of range", kind),
                               1476                 :                :                         errhint("Provide a custom cumulative statistics ID between %u and %u.",
                               1477                 :                :                                 PGSTAT_KIND_CUSTOM_MIN, PGSTAT_KIND_CUSTOM_MAX)));
                               1478                 :                : 
                               1479         [ #  # ]:              0 :     if (!process_shared_preload_libraries_in_progress)
                               1480         [ #  # ]:              0 :         ereport(ERROR,
                               1481                 :                :                 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
                               1482                 :                :                  errdetail("Custom cumulative statistics must be registered while initializing modules in \"shared_preload_libraries\".")));
                               1483                 :                : 
                               1484                 :                :     /*
                               1485                 :                :      * Check some data for fixed-numbered stats.
                               1486                 :                :      */
                               1487         [ #  # ]:              0 :     if (kind_info->fixed_amount)
                               1488                 :                :     {
                               1489         [ #  # ]:              0 :         if (kind_info->shared_size == 0)
                               1490         [ #  # ]:              0 :             ereport(ERROR,
                               1491                 :                :                     (errmsg("custom cumulative statistics property is invalid"),
                               1492                 :                :                      errhint("Custom cumulative statistics require a shared memory size for fixed-numbered objects.")));
                               1493                 :                :     }
                               1494                 :                : 
                               1495                 :                :     /*
                               1496                 :                :      * If pgstat_kind_custom_infos is not available yet, allocate it.
                               1497                 :                :      */
                               1498         [ #  # ]:              0 :     if (pgstat_kind_custom_infos == NULL)
                               1499                 :                :     {
                               1500                 :              0 :         pgstat_kind_custom_infos = (const PgStat_KindInfo **)
                               1501                 :              0 :             MemoryContextAllocZero(TopMemoryContext,
                               1502                 :                :                                    sizeof(PgStat_KindInfo *) * PGSTAT_KIND_CUSTOM_SIZE);
                               1503                 :                :     }
                               1504                 :                : 
                               1505         [ #  # ]:              0 :     if (pgstat_kind_custom_infos[idx] != NULL &&
                               1506         [ #  # ]:              0 :         pgstat_kind_custom_infos[idx]->name != NULL)
                               1507         [ #  # ]:              0 :         ereport(ERROR,
                               1508                 :                :                 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
                               1509                 :                :                  errdetail("Custom cumulative statistics \"%s\" already registered with the same ID.",
                               1510                 :                :                            pgstat_kind_custom_infos[idx]->name)));
                               1511                 :                : 
                               1512                 :                :     /* check for existing custom stats with the same name */
                               1513         [ #  # ]:              0 :     for (PgStat_Kind existing_kind = PGSTAT_KIND_CUSTOM_MIN; existing_kind <= PGSTAT_KIND_CUSTOM_MAX; existing_kind++)
                               1514                 :                :     {
                               1515                 :              0 :         uint32      existing_idx = existing_kind - PGSTAT_KIND_CUSTOM_MIN;
                               1516                 :                : 
                               1517         [ #  # ]:              0 :         if (pgstat_kind_custom_infos[existing_idx] == NULL)
                               1518                 :              0 :             continue;
                               1519         [ #  # ]:              0 :         if (!pg_strcasecmp(pgstat_kind_custom_infos[existing_idx]->name, kind_info->name))
                               1520         [ #  # ]:              0 :             ereport(ERROR,
                               1521                 :                :                     (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
                               1522                 :                :                      errdetail("Existing cumulative statistics with ID %u has the same name.", existing_kind)));
                               1523                 :                :     }
                               1524                 :                : 
                               1525                 :                :     /* Register it */
                               1526                 :              0 :     pgstat_kind_custom_infos[idx] = kind_info;
                               1527         [ #  # ]:              0 :     ereport(LOG,
                               1528                 :                :             (errmsg("registered custom cumulative statistics \"%s\" with ID %u",
                               1529                 :                :                     kind_info->name, kind)));
 5069 rhaas@postgresql.org     1530                 :              0 : }
                               1531                 :                : 
                               1532                 :                : /*
                               1533                 :                :  * Stats should only be reported after pgstat_initialize() and before
                               1534                 :                :  * pgstat_shutdown(). This check is put in a few central places to catch
                               1535                 :                :  * violations of this rule more easily.
                               1536                 :                :  */
                               1537                 :                : #ifdef USE_ASSERT_CHECKING
                               1538                 :                : void
 1265 andres@anarazel.de       1539                 :CBC     4615678 : pgstat_assert_is_up(void)
                               1540                 :                : {
                               1541   [ +  -  -  + ]:        4615678 :     Assert(pgstat_is_initialized && !pgstat_is_shutdown);
                               1542                 :        4615678 : }
                               1543                 :                : #endif
                               1544                 :                : 
                               1545                 :                : 
                               1546                 :                : /* ------------------------------------------------------------
                               1547                 :                :  * reading and writing of on-disk stats file
                               1548                 :                :  * ------------------------------------------------------------
                               1549                 :                :  */
                               1550                 :                : 
                               1551                 :                : /* helpers for pgstat_write_statsfile() */
                               1552                 :                : static void
 1249                          1553                 :         368722 : write_chunk(FILE *fpout, void *ptr, size_t len)
                               1554                 :                : {
                               1555                 :                :     int         rc;
                               1556                 :                : 
                               1557                 :         368722 :     rc = fwrite(ptr, len, 1, fpout);
                               1558                 :                : 
                               1559                 :                :     /* we'll check for errors with ferror once at the end */
                               1560                 :                :     (void) rc;
 1265                          1561                 :         368722 : }
                               1562                 :                : 
                               1563                 :                : #define write_chunk_s(fpout, ptr) write_chunk(fpout, ptr, sizeof(*ptr))
                               1564                 :                : 
                               1565                 :                : /*
                               1566                 :                :  * This function is called in the last process that is accessing the shared
                               1567                 :                :  * stats so locking is not required.
                               1568                 :                :  */
                               1569                 :                : static void
  173 michael@paquier.xyz      1570                 :            605 : pgstat_write_statsfile(void)
                               1571                 :                : {
                               1572                 :                :     FILE       *fpout;
                               1573                 :                :     int32       format_id;
 1249 andres@anarazel.de       1574                 :            605 :     const char *tmpfile = PGSTAT_STAT_PERMANENT_TMPFILE;
                               1575                 :            605 :     const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
                               1576                 :                :     dshash_seq_status hstat;
                               1577                 :                :     PgStatShared_HashEntry *ps;
                               1578                 :                : 
                               1579                 :            605 :     pgstat_assert_is_up();
                               1580                 :                : 
                               1581                 :                :     /* should be called only by the checkpointer or single user mode */
  421 michael@paquier.xyz      1582   [ +  +  -  + ]:            605 :     Assert(!IsUnderPostmaster || MyBackendType == B_CHECKPOINTER);
                               1583                 :                : 
                               1584                 :                :     /* we're shutting down, so it's ok to just override this */
 1249 andres@anarazel.de       1585                 :            605 :     pgstat_fetch_consistency = PGSTAT_FETCH_CONSISTENCY_NONE;
                               1586                 :                : 
  173 michael@paquier.xyz      1587         [ +  + ]:            605 :     elog(DEBUG2, "writing stats file \"%s\"", statfile);
                               1588                 :                : 
                               1589                 :                :     /*
                               1590                 :                :      * Open the statistics temp file to write out the current values.
                               1591                 :                :      */
 6151 tgl@sss.pgh.pa.us        1592                 :            605 :     fpout = AllocateFile(tmpfile, PG_BINARY_W);
 8842 JanWieck@Yahoo.com       1593         [ -  + ]:            605 :     if (fpout == NULL)
                               1594                 :                :     {
 8082 tgl@sss.pgh.pa.us        1595         [ #  # ]:UBC           0 :         ereport(LOG,
                               1596                 :                :                 (errcode_for_file_access(),
                               1597                 :                :                  errmsg("could not open temporary statistics file \"%s\": %m",
                               1598                 :                :                         tmpfile)));
 8842 JanWieck@Yahoo.com       1599                 :              0 :         return;
                               1600                 :                :     }
                               1601                 :                : 
                               1602                 :                :     /*
                               1603                 :                :      * Write the file header --- currently just a format ID.
                               1604                 :                :      */
 7359 tgl@sss.pgh.pa.us        1605                 :CBC         605 :     format_id = PGSTAT_FILE_FORMAT_ID;
 1249 andres@anarazel.de       1606                 :            605 :     write_chunk_s(fpout, &format_id);
                               1607                 :                : 
                               1608                 :                :     /* Write various stats structs for fixed number of objects */
  398 michael@paquier.xyz      1609         [ +  + ]:          19965 :     for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
                               1610                 :                :     {
                               1611                 :                :         char       *ptr;
  424                          1612                 :          19360 :         const PgStat_KindInfo *info = pgstat_get_kind_info(kind);
                               1613                 :                : 
  398                          1614   [ +  +  +  + ]:          19360 :         if (!info || !info->fixed_amount)
  424                          1615                 :          15730 :             continue;
                               1616                 :                : 
  398                          1617         [ +  - ]:           3630 :         if (pgstat_is_kind_builtin(kind))
                               1618         [ -  + ]:           3630 :             Assert(info->snapshot_ctl_off != 0);
                               1619                 :                : 
                               1620                 :                :         /* skip if no need to write to file */
  288                          1621         [ -  + ]:           3630 :         if (!info->write_to_file)
  288 michael@paquier.xyz      1622                 :UBC           0 :             continue;
                               1623                 :                : 
  424 michael@paquier.xyz      1624                 :CBC        3630 :         pgstat_build_snapshot_fixed(kind);
  398                          1625         [ +  - ]:           3630 :         if (pgstat_is_kind_builtin(kind))
                               1626                 :           3630 :             ptr = ((char *) &pgStatLocal.snapshot) + info->snapshot_ctl_off;
                               1627                 :                :         else
  398 michael@paquier.xyz      1628                 :UBC           0 :             ptr = pgStatLocal.snapshot.custom_data[kind - PGSTAT_KIND_CUSTOM_MIN];
                               1629                 :                : 
  422 michael@paquier.xyz      1630                 :CBC        3630 :         fputc(PGSTAT_FILE_ENTRY_FIXED, fpout);
                               1631                 :           3630 :         write_chunk_s(fpout, &kind);
  424                          1632                 :           3630 :         write_chunk(fpout, ptr, info->shared_data_len);
                               1633                 :                :     }
                               1634                 :                : 
                               1635                 :                :     /*
                               1636                 :                :      * Walk through the stats entries
                               1637                 :                :      */
 1249 andres@anarazel.de       1638                 :            605 :     dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
                               1639         [ +  + ]:         181106 :     while ((ps = dshash_seq_next(&hstat)) != NULL)
                               1640                 :                :     {
                               1641                 :                :         PgStatShared_Common *shstats;
                               1642                 :         180501 :         const PgStat_KindInfo *kind_info = NULL;
                               1643                 :                : 
                               1644         [ -  + ]:         180501 :         CHECK_FOR_INTERRUPTS();
                               1645                 :                : 
                               1646                 :                :         /*
                               1647                 :                :          * We should not see any "dropped" entries when writing the stats
                               1648                 :                :          * file, as all backends and auxiliary processes should have cleaned
                               1649                 :                :          * up their references before they terminated.
                               1650                 :                :          *
                               1651                 :                :          * However, since we are already shutting down, it is not worth
                               1652                 :                :          * crashing the server over any potential cleanup issues, so we simply
                               1653                 :                :          * skip such entries if encountered.
                               1654                 :                :          */
                               1655         [ -  + ]:         180501 :         Assert(!ps->dropped);
                               1656         [ -  + ]:         180501 :         if (ps->dropped)
 1249 andres@anarazel.de       1657                 :UBC           0 :             continue;
                               1658                 :                : 
                               1659                 :                :         /*
                               1660                 :                :          * This discards data related to custom stats kinds that are unknown
                               1661                 :                :          * to this process.
                               1662                 :                :          */
  398 michael@paquier.xyz      1663         [ -  + ]:CBC      180501 :         if (!pgstat_is_kind_valid(ps->key.kind))
                               1664                 :                :         {
  161 peter@eisentraut.org     1665         [ #  # ]:UBC           0 :             elog(WARNING, "found unknown stats entry %u/%u/%" PRIu64,
                               1666                 :                :                  ps->key.kind, ps->key.dboid,
                               1667                 :                :                  ps->key.objid);
  398 michael@paquier.xyz      1668                 :              0 :             continue;
                               1669                 :                :         }
                               1670                 :                : 
 1249 andres@anarazel.de       1671                 :CBC      180501 :         shstats = (PgStatShared_Common *) dsa_get_address(pgStatLocal.dsa, ps->body);
                               1672                 :                : 
                               1673                 :         180501 :         kind_info = pgstat_get_kind_info(ps->key.kind);
                               1674                 :                : 
                               1675                 :                :         /* if not dropped the valid-entry refcount should exist */
                               1676         [ -  + ]:         180501 :         Assert(pg_atomic_read_u32(&ps->refcount) > 0);
                               1677                 :                : 
                               1678                 :                :         /* skip if no need to write to file */
  288 michael@paquier.xyz      1679         [ +  + ]:         180501 :         if (!kind_info->write_to_file)
                               1680                 :            117 :             continue;
                               1681                 :                : 
 1249 andres@anarazel.de       1682         [ +  + ]:         180384 :         if (!kind_info->to_serialized_name)
                               1683                 :                :         {
                               1684                 :                :             /* normal stats entry, identified by PgStat_HashKey */
  430 michael@paquier.xyz      1685                 :         180295 :             fputc(PGSTAT_FILE_ENTRY_HASH, fpout);
 1249 andres@anarazel.de       1686                 :         180295 :             write_chunk_s(fpout, &ps->key);
                               1687                 :                :         }
                               1688                 :                :         else
                               1689                 :                :         {
                               1690                 :                :             /* stats entry identified by name on disk (e.g. slots) */
                               1691                 :                :             NameData    name;
                               1692                 :                : 
 1064                          1693                 :             89 :             kind_info->to_serialized_name(&ps->key, shstats, &name);
                               1694                 :                : 
  430 michael@paquier.xyz      1695                 :             89 :             fputc(PGSTAT_FILE_ENTRY_NAME, fpout);
 1249 andres@anarazel.de       1696                 :             89 :             write_chunk_s(fpout, &ps->key.kind);
                               1697                 :             89 :             write_chunk_s(fpout, &name);
                               1698                 :                :         }
                               1699                 :                : 
                               1700                 :                :         /* Write except the header part of the entry */
                               1701                 :         180384 :         write_chunk(fpout,
                               1702                 :                :                     pgstat_get_entry_data(ps->key.kind, shstats),
                               1703                 :                :                     pgstat_get_entry_len(ps->key.kind));
                               1704                 :                :     }
                               1705                 :            605 :     dshash_seq_term(&hstat);
                               1706                 :                : 
                               1707                 :                :     /*
                               1708                 :                :      * No more output to be done. Close the temp file and replace the old
                               1709                 :                :      * pgstat.stat with it.  The ferror() check replaces testing for error
                               1710                 :                :      * after each individual fputc or fwrite (in write_chunk()) above.
                               1711                 :                :      */
  430 michael@paquier.xyz      1712                 :            605 :     fputc(PGSTAT_FILE_ENTRY_END, fpout);
                               1713                 :                : 
 7171 tgl@sss.pgh.pa.us        1714         [ -  + ]:            605 :     if (ferror(fpout))
                               1715                 :                :     {
 7171 tgl@sss.pgh.pa.us        1716         [ #  # ]:UBC           0 :         ereport(LOG,
                               1717                 :                :                 (errcode_for_file_access(),
                               1718                 :                :                  errmsg("could not write temporary statistics file \"%s\": %m",
                               1719                 :                :                         tmpfile)));
 6151                          1720                 :              0 :         FreeFile(fpout);
 6241 magnus@hagander.net      1721                 :              0 :         unlink(tmpfile);
                               1722                 :                :     }
 6151 tgl@sss.pgh.pa.us        1723         [ -  + ]:CBC         605 :     else if (FreeFile(fpout) < 0)
                               1724                 :                :     {
 8082 tgl@sss.pgh.pa.us        1725         [ #  # ]:UBC           0 :         ereport(LOG,
                               1726                 :                :                 (errcode_for_file_access(),
                               1727                 :                :                  errmsg("could not close temporary statistics file \"%s\": %m",
                               1728                 :                :                         tmpfile)));
 6241 magnus@hagander.net      1729                 :              0 :         unlink(tmpfile);
                               1730                 :                :     }
  416 michael@paquier.xyz      1731         [ -  + ]:CBC         605 :     else if (durable_rename(tmpfile, statfile, LOG) < 0)
                               1732                 :                :     {
                               1733                 :                :         /* durable_rename already emitted log message */
 6241 magnus@hagander.net      1734                 :UBC           0 :         unlink(tmpfile);
                               1735                 :                :     }
                               1736                 :                : }
                               1737                 :                : 
                               1738                 :                : /* helpers for pgstat_read_statsfile() */
                               1739                 :                : static bool
 1249 andres@anarazel.de       1740                 :CBC      382521 : read_chunk(FILE *fpin, void *ptr, size_t len)
                               1741                 :                : {
                               1742                 :         382521 :     return fread(ptr, 1, len, fpin) == len;
                               1743                 :                : }
                               1744                 :                : 
                               1745                 :                : #define read_chunk_s(fpin, ptr) read_chunk(fpin, ptr, sizeof(*ptr))
                               1746                 :                : 
                               1747                 :                : /*
                               1748                 :                :  * Reads in existing statistics file into memory.
                               1749                 :                :  *
                               1750                 :                :  * This function is called in the only process that is accessing the shared
                               1751                 :                :  * stats so locking is not required.
                               1752                 :                :  */
                               1753                 :                : static void
  173 michael@paquier.xyz      1754                 :            718 : pgstat_read_statsfile(void)
                               1755                 :                : {
                               1756                 :                :     FILE       *fpin;
                               1757                 :                :     int32       format_id;
                               1758                 :                :     bool        found;
 1249 andres@anarazel.de       1759                 :            718 :     const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
                               1760                 :            718 :     PgStat_ShmemControl *shmem = pgStatLocal.shmem;
                               1761                 :                : 
                               1762                 :                :     /* shouldn't be called from postmaster */
                               1763   [ +  +  -  + ]:            718 :     Assert(IsUnderPostmaster || !IsPostmasterEnvironment);
                               1764                 :                : 
  173 michael@paquier.xyz      1765         [ +  + ]:            718 :     elog(DEBUG2, "reading stats file \"%s\"", statfile);
                               1766                 :                : 
                               1767                 :                :     /*
                               1768                 :                :      * Try to open the stats file. If it doesn't exist, the backends simply
                               1769                 :                :      * returns zero for anything and statistics simply starts from scratch
                               1770                 :                :      * with empty counters.
                               1771                 :                :      *
                               1772                 :                :      * ENOENT is a possibility if stats collection was previously disabled or
                               1773                 :                :      * has not yet written the stats file for the first time.  Any other
                               1774                 :                :      * failure condition is suspicious.
                               1775                 :                :      */
 6241 magnus@hagander.net      1776         [ +  + ]:            718 :     if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
                               1777                 :                :     {
 5657 tgl@sss.pgh.pa.us        1778         [ -  + ]:             51 :         if (errno != ENOENT)
 1249 andres@anarazel.de       1779         [ #  # ]:UBC           0 :             ereport(LOG,
                               1780                 :                :                     (errcode_for_file_access(),
                               1781                 :                :                      errmsg("could not open statistics file \"%s\": %m",
                               1782                 :                :                             statfile)));
 1241 andres@anarazel.de       1783                 :CBC          51 :         pgstat_reset_after_failure();
 1249                          1784                 :             51 :         return;
                               1785                 :                :     }
                               1786                 :                : 
                               1787                 :                :     /*
                               1788                 :                :      * Verify it's of the expected format.
                               1789                 :                :      */
  403 michael@paquier.xyz      1790         [ -  + ]:            667 :     if (!read_chunk_s(fpin, &format_id))
                               1791                 :                :     {
  403 michael@paquier.xyz      1792         [ #  # ]:UBC           0 :         elog(WARNING, "could not read format ID");
                               1793                 :              0 :         goto error;
                               1794                 :                :     }
                               1795                 :                : 
  403 michael@paquier.xyz      1796         [ +  + ]:CBC         667 :     if (format_id != PGSTAT_FILE_FORMAT_ID)
                               1797                 :                :     {
                               1798         [ +  - ]:              1 :         elog(WARNING, "found incorrect format ID %d (expected %d)",
                               1799                 :                :              format_id, PGSTAT_FILE_FORMAT_ID);
 1249 andres@anarazel.de       1800                 :              1 :         goto error;
                               1801                 :                :     }
                               1802                 :                : 
                               1803                 :                :     /*
                               1804                 :                :      * We found an existing statistics file. Read it and put all the stats
                               1805                 :                :      * data into place.
                               1806                 :                :      */
                               1807                 :                :     for (;;)
 8842 JanWieck@Yahoo.com       1808                 :         190897 :     {
 1244 tgl@sss.pgh.pa.us        1809                 :         191563 :         int         t = fgetc(fpin);
                               1810                 :                : 
 1249 andres@anarazel.de       1811   [ +  +  +  - ]:         191563 :         switch (t)
                               1812                 :                :         {
  422 michael@paquier.xyz      1813                 :           3996 :             case PGSTAT_FILE_ENTRY_FIXED:
                               1814                 :                :                 {
                               1815                 :                :                     PgStat_Kind kind;
                               1816                 :                :                     const PgStat_KindInfo *info;
                               1817                 :                :                     char       *ptr;
                               1818                 :                : 
                               1819                 :                :                     /* entry for fixed-numbered stats */
                               1820         [ -  + ]:           3996 :                     if (!read_chunk_s(fpin, &kind))
                               1821                 :                :                     {
  403 michael@paquier.xyz      1822         [ #  # ]:UBC           0 :                         elog(WARNING, "could not read stats kind for entry of type %c", t);
  422                          1823                 :              0 :                         goto error;
                               1824                 :                :                     }
                               1825                 :                : 
  422 michael@paquier.xyz      1826         [ -  + ]:CBC        3996 :                     if (!pgstat_is_kind_valid(kind))
                               1827                 :                :                     {
  398 michael@paquier.xyz      1828         [ #  # ]:UBC           0 :                         elog(WARNING, "invalid stats kind %u for entry of type %c",
                               1829                 :                :                              kind, t);
  422                          1830                 :              0 :                         goto error;
                               1831                 :                :                     }
                               1832                 :                : 
  422 michael@paquier.xyz      1833                 :CBC        3996 :                     info = pgstat_get_kind_info(kind);
  366                          1834         [ -  + ]:           3996 :                     if (!info)
                               1835                 :                :                     {
  366 michael@paquier.xyz      1836         [ #  # ]:UBC           0 :                         elog(WARNING, "could not find information of kind %u for entry of type %c",
                               1837                 :                :                              kind, t);
                               1838                 :              0 :                         goto error;
                               1839                 :                :                     }
                               1840                 :                : 
  422 michael@paquier.xyz      1841         [ -  + ]:CBC        3996 :                     if (!info->fixed_amount)
                               1842                 :                :                     {
  398 michael@paquier.xyz      1843         [ #  # ]:UBC           0 :                         elog(WARNING, "invalid fixed_amount in stats kind %u for entry of type %c",
                               1844                 :                :                              kind, t);
  422                          1845                 :              0 :                         goto error;
                               1846                 :                :                     }
                               1847                 :                : 
                               1848                 :                :                     /* Load back stats into shared memory */
  398 michael@paquier.xyz      1849         [ +  - ]:CBC        3996 :                     if (pgstat_is_kind_builtin(kind))
                               1850                 :           3996 :                         ptr = ((char *) shmem) + info->shared_ctl_off +
                               1851                 :           3996 :                             info->shared_data_off;
                               1852                 :                :                     else
                               1853                 :                :                     {
  398 michael@paquier.xyz      1854                 :UBC           0 :                         int         idx = kind - PGSTAT_KIND_CUSTOM_MIN;
                               1855                 :                : 
                               1856                 :              0 :                         ptr = ((char *) shmem->custom_data[idx]) +
                               1857                 :              0 :                             info->shared_data_off;
                               1858                 :                :                     }
                               1859                 :                : 
  422 michael@paquier.xyz      1860         [ -  + ]:CBC        3996 :                     if (!read_chunk(fpin, ptr, info->shared_data_len))
                               1861                 :                :                     {
  398 michael@paquier.xyz      1862         [ #  # ]:UBC           0 :                         elog(WARNING, "could not read data of stats kind %u for entry of type %c with size %u",
                               1863                 :                :                              kind, t, info->shared_data_len);
  422                          1864                 :              0 :                         goto error;
                               1865                 :                :                     }
                               1866                 :                : 
  422 michael@paquier.xyz      1867                 :CBC        3996 :                     break;
                               1868                 :                :                 }
  430                          1869                 :         186901 :             case PGSTAT_FILE_ENTRY_HASH:
                               1870                 :                :             case PGSTAT_FILE_ENTRY_NAME:
                               1871                 :                :                 {
                               1872                 :                :                     PgStat_HashKey key;
                               1873                 :                :                     PgStatShared_HashEntry *p;
                               1874                 :                :                     PgStatShared_Common *header;
                               1875                 :                : 
 1249 andres@anarazel.de       1876         [ -  + ]:         186901 :                     CHECK_FOR_INTERRUPTS();
                               1877                 :                : 
  430 michael@paquier.xyz      1878         [ +  + ]:         186901 :                     if (t == PGSTAT_FILE_ENTRY_HASH)
                               1879                 :                :                     {
                               1880                 :                :                         /* normal stats entry, identified by PgStat_HashKey */
 1249 andres@anarazel.de       1881         [ -  + ]:         186840 :                         if (!read_chunk_s(fpin, &key))
                               1882                 :                :                         {
  403 michael@paquier.xyz      1883         [ #  # ]:UBC           0 :                             elog(WARNING, "could not read key for entry of type %c", t);
 1249 andres@anarazel.de       1884                 :              0 :                             goto error;
                               1885                 :                :                         }
                               1886                 :                : 
 1249 andres@anarazel.de       1887         [ -  + ]:CBC      186840 :                         if (!pgstat_is_kind_valid(key.kind))
                               1888                 :                :                         {
  161 peter@eisentraut.org     1889         [ #  # ]:UBC           0 :                             elog(WARNING, "invalid stats kind for entry %u/%u/%" PRIu64 " of type %c",
                               1890                 :                :                                  key.kind, key.dboid,
                               1891                 :                :                                  key.objid, t);
 1249 andres@anarazel.de       1892                 :              0 :                             goto error;
                               1893                 :                :                         }
                               1894                 :                : 
  135 michael@paquier.xyz      1895         [ -  + ]:CBC      186840 :                         if (!pgstat_get_kind_info(key.kind))
                               1896                 :                :                         {
  135 michael@paquier.xyz      1897         [ #  # ]:UBC           0 :                             elog(WARNING, "could not find information of kind for entry %u/%u/%" PRIu64 " of type %c",
                               1898                 :                :                                  key.kind, key.dboid,
                               1899                 :                :                                  key.objid, t);
                               1900                 :              0 :                             goto error;
                               1901                 :                :                         }
                               1902                 :                :                     }
                               1903                 :                :                     else
                               1904                 :                :                     {
                               1905                 :                :                         /* stats entry identified by name on disk (e.g. slots) */
 1249 andres@anarazel.de       1906                 :CBC          61 :                         const PgStat_KindInfo *kind_info = NULL;
                               1907                 :                :                         PgStat_Kind kind;
                               1908                 :                :                         NameData    name;
                               1909                 :                : 
                               1910         [ -  + ]:             61 :                         if (!read_chunk_s(fpin, &kind))
                               1911                 :                :                         {
  403 michael@paquier.xyz      1912         [ #  # ]:UBC           0 :                             elog(WARNING, "could not read stats kind for entry of type %c", t);
 1249 andres@anarazel.de       1913                 :              0 :                             goto error;
                               1914                 :                :                         }
 1249 andres@anarazel.de       1915         [ -  + ]:CBC          61 :                         if (!read_chunk_s(fpin, &name))
                               1916                 :                :                         {
  398 michael@paquier.xyz      1917         [ #  # ]:UBC           0 :                             elog(WARNING, "could not read name of stats kind %u for entry of type %c",
                               1918                 :                :                                  kind, t);
 1249 andres@anarazel.de       1919                 :              0 :                             goto error;
                               1920                 :                :                         }
 1249 andres@anarazel.de       1921         [ -  + ]:CBC          61 :                         if (!pgstat_is_kind_valid(kind))
                               1922                 :                :                         {
  398 michael@paquier.xyz      1923         [ #  # ]:UBC           0 :                             elog(WARNING, "invalid stats kind %u for entry of type %c",
                               1924                 :                :                                  kind, t);
 1249 andres@anarazel.de       1925                 :              0 :                             goto error;
                               1926                 :                :                         }
                               1927                 :                : 
 1249 andres@anarazel.de       1928                 :CBC          61 :                         kind_info = pgstat_get_kind_info(kind);
  366 michael@paquier.xyz      1929         [ -  + ]:             61 :                         if (!kind_info)
                               1930                 :                :                         {
  366 michael@paquier.xyz      1931         [ #  # ]:UBC           0 :                             elog(WARNING, "could not find information of kind %u for entry of type %c",
                               1932                 :                :                                  kind, t);
                               1933                 :              0 :                             goto error;
                               1934                 :                :                         }
                               1935                 :                : 
 1249 andres@anarazel.de       1936         [ -  + ]:CBC          61 :                         if (!kind_info->from_serialized_name)
                               1937                 :                :                         {
  398 michael@paquier.xyz      1938         [ #  # ]:UBC           0 :                             elog(WARNING, "invalid from_serialized_name in stats kind %u for entry of type %c",
                               1939                 :                :                                  kind, t);
 1249 andres@anarazel.de       1940                 :              0 :                             goto error;
                               1941                 :                :                         }
                               1942                 :                : 
 1249 andres@anarazel.de       1943         [ +  + ]:CBC          61 :                         if (!kind_info->from_serialized_name(&name, &key))
                               1944                 :                :                         {
                               1945                 :                :                             /* skip over data for entry we don't care about */
                               1946         [ -  + ]:              1 :                             if (fseek(fpin, pgstat_get_entry_len(kind), SEEK_CUR) != 0)
                               1947                 :                :                             {
  398 michael@paquier.xyz      1948         [ #  # ]:UBC           0 :                                 elog(WARNING, "could not seek \"%s\" of stats kind %u for entry of type %c",
                               1949                 :                :                                      NameStr(name), kind, t);
 1249 andres@anarazel.de       1950                 :              0 :                                 goto error;
                               1951                 :                :                             }
                               1952                 :                : 
 1249 andres@anarazel.de       1953                 :CBC           1 :                             continue;
                               1954                 :                :                         }
                               1955                 :                : 
                               1956         [ -  + ]:             60 :                         Assert(key.kind == kind);
                               1957                 :                :                     }
                               1958                 :                : 
                               1959                 :                :                     /*
                               1960                 :                :                      * This intentionally doesn't use pgstat_get_entry_ref() -
                               1961                 :                :                      * putting all stats into checkpointer's
                               1962                 :                :                      * pgStatEntryRefHash would be wasted effort and memory.
                               1963                 :                :                      */
                               1964                 :         186900 :                     p = dshash_find_or_insert(pgStatLocal.shared_hash, &key, &found);
                               1965                 :                : 
                               1966                 :                :                     /* don't allow duplicate entries */
                               1967         [ -  + ]:         186900 :                     if (found)
                               1968                 :                :                     {
 1249 andres@anarazel.de       1969                 :UBC           0 :                         dshash_release_lock(pgStatLocal.shared_hash, p);
  161 peter@eisentraut.org     1970         [ #  # ]:              0 :                         elog(WARNING, "found duplicate stats entry %u/%u/%" PRIu64 " of type %c",
                               1971                 :                :                              key.kind, key.dboid,
                               1972                 :                :                              key.objid, t);
 1249 andres@anarazel.de       1973                 :              0 :                         goto error;
                               1974                 :                :                     }
                               1975                 :                : 
 1249 andres@anarazel.de       1976                 :CBC      186900 :                     header = pgstat_init_entry(key.kind, p);
                               1977                 :         186900 :                     dshash_release_lock(pgStatLocal.shared_hash, p);
                               1978                 :                : 
                               1979         [ -  + ]:         186900 :                     if (!read_chunk(fpin,
                               1980                 :                :                                     pgstat_get_entry_data(key.kind, header),
                               1981                 :                :                                     pgstat_get_entry_len(key.kind)))
                               1982                 :                :                     {
  161 peter@eisentraut.org     1983         [ #  # ]:UBC           0 :                         elog(WARNING, "could not read data for entry %u/%u/%" PRIu64 " of type %c",
                               1984                 :                :                              key.kind, key.dboid,
                               1985                 :                :                              key.objid, t);
 1249 andres@anarazel.de       1986                 :              0 :                         goto error;
                               1987                 :                :                     }
                               1988                 :                : 
 1285 akapila@postgresql.o     1989                 :CBC      186900 :                     break;
                               1990                 :                :                 }
  430 michael@paquier.xyz      1991                 :            666 :             case PGSTAT_FILE_ENTRY_END:
                               1992                 :                : 
                               1993                 :                :                 /*
                               1994                 :                :                  * check that PGSTAT_FILE_ENTRY_END actually signals end of
                               1995                 :                :                  * file
                               1996                 :                :                  */
 1239 andres@anarazel.de       1997         [ +  + ]:            666 :                 if (fgetc(fpin) != EOF)
                               1998                 :                :                 {
  403 michael@paquier.xyz      1999         [ +  - ]:              1 :                     elog(WARNING, "could not read end-of-file");
 1239 andres@anarazel.de       2000                 :              1 :                     goto error;
                               2001                 :                :                 }
                               2002                 :                : 
 4583 alvherre@alvh.no-ip.     2003                 :            665 :                 goto done;
                               2004                 :                : 
 4583 alvherre@alvh.no-ip.     2005                 :UBC           0 :             default:
  403 michael@paquier.xyz      2006         [ #  # ]:              0 :                 elog(WARNING, "could not read entry of type %c", t);
 1249 andres@anarazel.de       2007                 :              0 :                 goto error;
                               2008                 :                :         }
                               2009                 :                :     }
                               2010                 :                : 
 4583 alvherre@alvh.no-ip.     2011                 :CBC         667 : done:
                               2012                 :            667 :     FreeFile(fpin);
                               2013                 :                : 
 1249 andres@anarazel.de       2014         [ +  + ]:            667 :     elog(DEBUG2, "removing permanent stats file \"%s\"", statfile);
                               2015                 :            667 :     unlink(statfile);
                               2016                 :                : 
                               2017                 :            667 :     return;
                               2018                 :                : 
                               2019                 :              2 : error:
                               2020         [ +  - ]:              2 :     ereport(LOG,
                               2021                 :                :             (errmsg("corrupted statistics file \"%s\"", statfile)));
                               2022                 :                : 
 1241                          2023                 :              2 :     pgstat_reset_after_failure();
                               2024                 :                : 
 1249                          2025                 :              2 :     goto done;
                               2026                 :                : }
                               2027                 :                : 
                               2028                 :                : /*
                               2029                 :                :  * Helper to reset / drop stats after a crash or after restoring stats from
                               2030                 :                :  * disk failed, potentially after already loading parts.
                               2031                 :                :  */
                               2032                 :                : static void
 1241                          2033                 :            222 : pgstat_reset_after_failure(void)
                               2034                 :                : {
                               2035                 :            222 :     TimestampTz ts = GetCurrentTimestamp();
                               2036                 :                : 
                               2037                 :                :     /* reset fixed-numbered stats */
  398 michael@paquier.xyz      2038         [ +  + ]:           7326 :     for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
                               2039                 :                :     {
 1249 andres@anarazel.de       2040                 :           7104 :         const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                               2041                 :                : 
  398 michael@paquier.xyz      2042   [ +  +  +  + ]:           7104 :         if (!kind_info || !kind_info->fixed_amount)
 1249 andres@anarazel.de       2043                 :           5772 :             continue;
                               2044                 :                : 
                               2045                 :           1332 :         kind_info->reset_all_cb(ts);
                               2046                 :                :     }
                               2047                 :                : 
                               2048                 :                :     /* and drop variable-numbered ones */
                               2049                 :            222 :     pgstat_drop_all_entries();
 1376 akapila@postgresql.o     2050                 :            222 : }
                               2051                 :                : 
                               2052                 :                : /*
                               2053                 :                :  * GUC assign_hook for stats_fetch_consistency.
                               2054                 :                :  */
                               2055                 :                : void
  850 michael@paquier.xyz      2056                 :           1621 : assign_stats_fetch_consistency(int newval, void *extra)
                               2057                 :                : {
                               2058                 :                :     /*
                               2059                 :                :      * Changing this value in a transaction may cause snapshot state
                               2060                 :                :      * inconsistencies, so force a clear of the current snapshot on the next
                               2061                 :                :      * snapshot build attempt.
                               2062                 :                :      */
                               2063         [ +  + ]:           1621 :     if (pgstat_fetch_consistency != newval)
                               2064                 :            447 :         force_stats_snapshot_clear = true;
                               2065                 :           1621 : }
        

Generated by: LCOV version 2.4-beta