LCOV - code coverage report
Current view: top level - src/include/utils - pgstat_internal.h (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 41 42 97.6 %
Date: 2024-11-21 10:14:43 Functions: 11 11 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* ----------
       2             :  * pgstat_internal.h
       3             :  *
       4             :  * Definitions for the PostgreSQL cumulative statistics system that should
       5             :  * only be needed by files implementing statistics support (rather than ones
       6             :  * reporting / querying stats).
       7             :  *
       8             :  * Copyright (c) 2001-2024, PostgreSQL Global Development Group
       9             :  *
      10             :  * src/include/utils/pgstat_internal.h
      11             :  * ----------
      12             :  */
      13             : #ifndef PGSTAT_INTERNAL_H
      14             : #define PGSTAT_INTERNAL_H
      15             : 
      16             : 
      17             : #include "common/hashfn_unstable.h"
      18             : #include "lib/dshash.h"
      19             : #include "lib/ilist.h"
      20             : #include "pgstat.h"
      21             : #include "storage/lwlock.h"
      22             : #include "utils/dsa.h"
      23             : 
      24             : 
      25             : /*
      26             :  * Types related to shared memory storage of statistics.
      27             :  *
      28             :  * Per-object statistics are stored in the "shared stats" hashtable. That
      29             :  * table's entries (PgStatShared_HashEntry) contain a pointer to the actual stats
      30             :  * data for the object (the size of the stats data varies depending on the
      31             :  * kind of stats). The table is keyed by PgStat_HashKey.
      32             :  *
      33             :  * Once a backend has a reference to a shared stats entry, it increments the
      34             :  * entry's refcount. Even after stats data is dropped (e.g., due to a DROP
      35             :  * TABLE), the entry itself can only be deleted once all references have been
      36             :  * released.
      37             :  *
      38             :  * These refcounts, in combination with a backend local hashtable
      39             :  * (pgStatEntryRefHash, with entries pointing to PgStat_EntryRef) in front of
      40             :  * the shared hash table, mean that most stats work can happen without
      41             :  * touching the shared hash table, reducing contention.
      42             :  *
      43             :  * Once there are pending stats updates for a table PgStat_EntryRef->pending
      44             :  * is allocated to contain a working space for as-of-yet-unapplied stats
      45             :  * updates. Once the stats are flushed, PgStat_EntryRef->pending is freed.
      46             :  *
      47             :  * Each stat kind in the shared hash table has a fixed member
      48             :  * PgStatShared_Common as the first element.
      49             :  */
      50             : 
      51             : /* struct for shared statistics hash entry key. */
      52             : typedef struct PgStat_HashKey
      53             : {
      54             :     PgStat_Kind kind;           /* statistics entry kind */
      55             :     Oid         dboid;          /* database ID. InvalidOid for shared objects. */
      56             :     uint64      objid;          /* object ID (table, function, etc.), or
      57             :                                  * identifier. */
      58             : } PgStat_HashKey;
      59             : 
      60             : /*
      61             :  * Shared statistics hash entry. Doesn't itself contain any stats, but points
      62             :  * to them (with ->body). That allows the stats entries themselves to be of
      63             :  * variable size.
      64             :  */
      65             : typedef struct PgStatShared_HashEntry
      66             : {
      67             :     PgStat_HashKey key;         /* hash key */
      68             : 
      69             :     /*
      70             :      * If dropped is set, backends need to release their references so that
      71             :      * the memory for the entry can be freed. No new references may be made
      72             :      * once marked as dropped.
      73             :      */
      74             :     bool        dropped;
      75             : 
      76             :     /*
      77             :      * Refcount managing lifetime of the entry itself (as opposed to the
      78             :      * dshash entry pointing to it). The stats lifetime has to be separate
      79             :      * from the hash table entry lifetime because we allow backends to point
      80             :      * to a stats entry without holding a hash table lock (and some other
      81             :      * reasons).
      82             :      *
      83             :      * As long as the entry is not dropped, 1 is added to the refcount
      84             :      * representing that the entry should not be dropped. In addition each
      85             :      * backend that has a reference to the entry needs to increment the
      86             :      * refcount as long as it does.
      87             :      *
      88             :      * May only be incremented / decremented while holding at least a shared
      89             :      * lock on the dshash partition containing the entry. It needs to be an
      90             :      * atomic variable because multiple backends can increment the refcount
      91             :      * with just a shared lock.
      92             :      *
      93             :      * When the refcount reaches 0 the entry needs to be freed.
      94             :      */
      95             :     pg_atomic_uint32 refcount;
      96             : 
      97             :     /*
      98             :      * Counter tracking the number of times the entry has been reused.
      99             :      *
     100             :      * Set to 0 when the entry is created, and incremented by one each time
     101             :      * the shared entry is reinitialized with pgstat_reinit_entry().
     102             :      *
     103             :      * May only be incremented / decremented while holding at least a shared
     104             :      * lock on the dshash partition containing the entry. Like refcount, it
     105             :      * needs to be an atomic variable because multiple backends can increment
     106             :      * the generation with just a shared lock.
     107             :      */
     108             :     pg_atomic_uint32 generation;
     109             : 
     110             :     /*
     111             :      * Pointer to shared stats. The stats entry always starts with
     112             :      * PgStatShared_Common, embedded in a larger struct containing the
     113             :      * PgStat_Kind specific stats fields.
     114             :      */
     115             :     dsa_pointer body;
     116             : } PgStatShared_HashEntry;
     117             : 
     118             : /*
     119             :  * Common header struct for PgStatShared_*.
     120             :  */
     121             : typedef struct PgStatShared_Common
     122             : {
     123             :     uint32      magic;          /* just a validity cross-check */
     124             :     /* lock protecting stats contents (i.e. data following the header) */
     125             :     LWLock      lock;
     126             : } PgStatShared_Common;
     127             : 
     128             : /*
     129             :  * A backend local reference to a shared stats entry. As long as at least one
     130             :  * such reference exists, the shared stats entry will not be released.
     131             :  *
     132             :  * If there are pending stats update to the shared stats, these are stored in
     133             :  * ->pending.
     134             :  */
     135             : typedef struct PgStat_EntryRef
     136             : {
     137             :     /*
     138             :      * Pointer to the PgStatShared_HashEntry entry in the shared stats
     139             :      * hashtable.
     140             :      */
     141             :     PgStatShared_HashEntry *shared_entry;
     142             : 
     143             :     /*
     144             :      * Pointer to the stats data (i.e. PgStatShared_HashEntry->body), resolved
     145             :      * as a local pointer, to avoid repeated dsa_get_address() calls.
     146             :      */
     147             :     PgStatShared_Common *shared_stats;
     148             : 
     149             :     /*
     150             :      * Copy of PgStatShared_HashEntry->generation, keeping locally track of
     151             :      * the shared stats entry "generation" retrieved (number of times reused).
     152             :      */
     153             :     uint32      generation;
     154             : 
     155             :     /*
     156             :      * Pending statistics data that will need to be flushed to shared memory
     157             :      * stats eventually. Each stats kind utilizing pending data defines what
     158             :      * format its pending data has and needs to provide a
     159             :      * PgStat_KindInfo->flush_pending_cb callback to merge pending into shared
     160             :      * stats.
     161             :      */
     162             :     void       *pending;
     163             :     dlist_node  pending_node;   /* membership in pgStatPending list */
     164             : } PgStat_EntryRef;
     165             : 
     166             : 
     167             : /*
     168             :  * Some stats changes are transactional. To maintain those, a stack of
     169             :  * PgStat_SubXactStatus entries is maintained, which contain data pertaining
     170             :  * to the current transaction and its active subtransactions.
     171             :  */
     172             : typedef struct PgStat_SubXactStatus
     173             : {
     174             :     int         nest_level;     /* subtransaction nest level */
     175             : 
     176             :     struct PgStat_SubXactStatus *prev;  /* higher-level subxact if any */
     177             : 
     178             :     /*
     179             :      * Statistics for transactionally dropped objects need to be
     180             :      * transactionally dropped as well. Collect the stats dropped in the
     181             :      * current (sub-)transaction and only execute the stats drop when we know
     182             :      * if the transaction commits/aborts. To handle replicas and crashes,
     183             :      * stats drops are included in commit / abort records.
     184             :      */
     185             :     dclist_head pending_drops;
     186             : 
     187             :     /*
     188             :      * Tuple insertion/deletion counts for an open transaction can't be
     189             :      * propagated into PgStat_TableStatus counters until we know if it is
     190             :      * going to commit or abort.  Hence, we keep these counts in per-subxact
     191             :      * structs that live in TopTransactionContext.  This data structure is
     192             :      * designed on the assumption that subxacts won't usually modify very many
     193             :      * tables.
     194             :      */
     195             :     PgStat_TableXactStatus *first;  /* head of list for this subxact */
     196             : } PgStat_SubXactStatus;
     197             : 
     198             : 
     199             : /*
     200             :  * Metadata for a specific kind of statistics.
     201             :  */
     202             : typedef struct PgStat_KindInfo
     203             : {
     204             :     /*
     205             :      * Do a fixed number of stats objects exist for this kind of stats (e.g.
     206             :      * bgwriter stats) or not (e.g. tables).
     207             :      */
     208             :     bool        fixed_amount:1;
     209             : 
     210             :     /*
     211             :      * Can stats of this kind be accessed from another database? Determines
     212             :      * whether a stats object gets included in stats snapshots.
     213             :      */
     214             :     bool        accessed_across_databases:1;
     215             : 
     216             :     /*
     217             :      * The size of an entry in the shared stats hash table (pointed to by
     218             :      * PgStatShared_HashEntry->body).  For fixed-numbered statistics, this is
     219             :      * the size of an entry in PgStat_ShmemControl->custom_data.
     220             :      */
     221             :     uint32      shared_size;
     222             : 
     223             :     /*
     224             :      * The offset of the statistics struct in the cached statistics snapshot
     225             :      * PgStat_Snapshot, for fixed-numbered statistics.
     226             :      */
     227             :     uint32      snapshot_ctl_off;
     228             : 
     229             :     /*
     230             :      * The offset of the statistics struct in the containing shared memory
     231             :      * control structure PgStat_ShmemControl, for fixed-numbered statistics.
     232             :      */
     233             :     uint32      shared_ctl_off;
     234             : 
     235             :     /*
     236             :      * The offset/size of statistics inside the shared stats entry. Used when
     237             :      * [de-]serializing statistics to / from disk respectively. Separate from
     238             :      * shared_size because [de-]serialization may not include in-memory state
     239             :      * like lwlocks.
     240             :      */
     241             :     uint32      shared_data_off;
     242             :     uint32      shared_data_len;
     243             : 
     244             :     /*
     245             :      * The size of the pending data for this kind. E.g. how large
     246             :      * PgStat_EntryRef->pending is. Used for allocations.
     247             :      *
     248             :      * 0 signals that an entry of this kind should never have a pending entry.
     249             :      */
     250             :     uint32      pending_size;
     251             : 
     252             :     /*
     253             :      * Perform custom actions when initializing a backend (standalone or under
     254             :      * postmaster). Optional.
     255             :      */
     256             :     void        (*init_backend_cb) (void);
     257             : 
     258             :     /*
     259             :      * For variable-numbered stats: flush pending stats. Required if pending
     260             :      * data is used.  See flush_fixed_cb for fixed-numbered stats.
     261             :      */
     262             :     bool        (*flush_pending_cb) (PgStat_EntryRef *sr, bool nowait);
     263             : 
     264             :     /*
     265             :      * For variable-numbered stats: delete pending stats. Optional.
     266             :      */
     267             :     void        (*delete_pending_cb) (PgStat_EntryRef *sr);
     268             : 
     269             :     /*
     270             :      * For variable-numbered stats: reset the reset timestamp. Optional.
     271             :      */
     272             :     void        (*reset_timestamp_cb) (PgStatShared_Common *header, TimestampTz ts);
     273             : 
     274             :     /*
     275             :      * For variable-numbered stats. Optional.
     276             :      */
     277             :     void        (*to_serialized_name) (const PgStat_HashKey *key,
     278             :                                        const PgStatShared_Common *header, NameData *name);
     279             :     bool        (*from_serialized_name) (const NameData *name, PgStat_HashKey *key);
     280             : 
     281             :     /*
     282             :      * For fixed-numbered statistics: Initialize shared memory state.
     283             :      *
     284             :      * "stats" is the pointer to the allocated shared memory area.
     285             :      */
     286             :     void        (*init_shmem_cb) (void *stats);
     287             : 
     288             :     /*
     289             :      * For fixed-numbered statistics: Flush pending stats. Returns true if
     290             :      * some of the stats could not be flushed, due to lock contention for
     291             :      * example. Optional.
     292             :      */
     293             :     bool        (*flush_fixed_cb) (bool nowait);
     294             : 
     295             :     /*
     296             :      * For fixed-numbered statistics: Check for pending stats in need of
     297             :      * flush. Returns true if there are any stats pending for flush. Optional.
     298             :      */
     299             :     bool        (*have_fixed_pending_cb) (void);
     300             : 
     301             :     /*
     302             :      * For fixed-numbered statistics: Reset All.
     303             :      */
     304             :     void        (*reset_all_cb) (TimestampTz ts);
     305             : 
     306             :     /*
     307             :      * For fixed-numbered statistics: Build snapshot for entry
     308             :      */
     309             :     void        (*snapshot_cb) (void);
     310             : 
     311             :     /* name of the kind of stats */
     312             :     const char *const name;
     313             : } PgStat_KindInfo;
     314             : 
     315             : 
     316             : /*
     317             :  * List of SLRU names that we keep stats for.  There is no central registry of
     318             :  * SLRUs, so we use this fixed list instead.  The "other" entry is used for
     319             :  * all SLRUs without an explicit entry (e.g. SLRUs in extensions).
     320             :  *
     321             :  * This is only defined here so that SLRU_NUM_ELEMENTS is known for later type
     322             :  * definitions.
     323             :  */
     324             : static const char *const slru_names[] = {
     325             :     "commit_timestamp",
     326             :     "multixact_member",
     327             :     "multixact_offset",
     328             :     "notify",
     329             :     "serializable",
     330             :     "subtransaction",
     331             :     "transaction",
     332             :     "other"                       /* has to be last */
     333             : };
     334             : 
     335             : #define SLRU_NUM_ELEMENTS   lengthof(slru_names)
     336             : 
     337             : 
     338             : /* ----------
     339             :  * Types and definitions for different kinds of fixed-amount stats.
     340             :  *
     341             :  * Single-writer stats use the changecount mechanism to achieve low-overhead
     342             :  * writes - they're obviously more performance critical than reads. Check the
     343             :  * definition of struct PgBackendStatus for some explanation of the
     344             :  * changecount mechanism.
     345             :  *
     346             :  * Because the obvious implementation of resetting single-writer stats isn't
     347             :  * compatible with that (another backend needs to write), we don't scribble on
     348             :  * shared stats while resetting. Instead, just record the current counter
     349             :  * values in a copy of the stats data, which is protected by ->lock. See
     350             :  * pgstat_fetch_stat_(archiver|bgwriter|checkpointer) for the reader side.
     351             :  *
     352             :  * The only exception to that is the stat_reset_timestamp in these structs,
     353             :  * which is protected by ->lock, because it has to be written by another
     354             :  * backend while resetting.
     355             :  * ----------
     356             :  */
     357             : 
     358             : typedef struct PgStatShared_Archiver
     359             : {
     360             :     /* lock protects ->reset_offset as well as stats->stat_reset_timestamp */
     361             :     LWLock      lock;
     362             :     uint32      changecount;
     363             :     PgStat_ArchiverStats stats;
     364             :     PgStat_ArchiverStats reset_offset;
     365             : } PgStatShared_Archiver;
     366             : 
     367             : typedef struct PgStatShared_BgWriter
     368             : {
     369             :     /* lock protects ->reset_offset as well as stats->stat_reset_timestamp */
     370             :     LWLock      lock;
     371             :     uint32      changecount;
     372             :     PgStat_BgWriterStats stats;
     373             :     PgStat_BgWriterStats reset_offset;
     374             : } PgStatShared_BgWriter;
     375             : 
     376             : typedef struct PgStatShared_Checkpointer
     377             : {
     378             :     /* lock protects ->reset_offset as well as stats->stat_reset_timestamp */
     379             :     LWLock      lock;
     380             :     uint32      changecount;
     381             :     PgStat_CheckpointerStats stats;
     382             :     PgStat_CheckpointerStats reset_offset;
     383             : } PgStatShared_Checkpointer;
     384             : 
     385             : /* Shared-memory ready PgStat_IO */
     386             : typedef struct PgStatShared_IO
     387             : {
     388             :     /*
     389             :      * locks[i] protects stats.stats[i]. locks[0] also protects
     390             :      * stats.stat_reset_timestamp.
     391             :      */
     392             :     LWLock      locks[BACKEND_NUM_TYPES];
     393             :     PgStat_IO   stats;
     394             : } PgStatShared_IO;
     395             : 
     396             : typedef struct PgStatShared_SLRU
     397             : {
     398             :     /* lock protects ->stats */
     399             :     LWLock      lock;
     400             :     PgStat_SLRUStats stats[SLRU_NUM_ELEMENTS];
     401             : } PgStatShared_SLRU;
     402             : 
     403             : typedef struct PgStatShared_Wal
     404             : {
     405             :     /* lock protects ->stats */
     406             :     LWLock      lock;
     407             :     PgStat_WalStats stats;
     408             : } PgStatShared_Wal;
     409             : 
     410             : 
     411             : 
     412             : /* ----------
     413             :  * Types and definitions for different kinds of variable-amount stats.
     414             :  *
     415             :  * Each struct has to start with PgStatShared_Common, containing information
     416             :  * common across the different types of stats. Kind-specific data follows.
     417             :  * ----------
     418             :  */
     419             : 
     420             : typedef struct PgStatShared_Database
     421             : {
     422             :     PgStatShared_Common header;
     423             :     PgStat_StatDBEntry stats;
     424             : } PgStatShared_Database;
     425             : 
     426             : typedef struct PgStatShared_Relation
     427             : {
     428             :     PgStatShared_Common header;
     429             :     PgStat_StatTabEntry stats;
     430             : } PgStatShared_Relation;
     431             : 
     432             : typedef struct PgStatShared_Function
     433             : {
     434             :     PgStatShared_Common header;
     435             :     PgStat_StatFuncEntry stats;
     436             : } PgStatShared_Function;
     437             : 
     438             : typedef struct PgStatShared_Subscription
     439             : {
     440             :     PgStatShared_Common header;
     441             :     PgStat_StatSubEntry stats;
     442             : } PgStatShared_Subscription;
     443             : 
     444             : typedef struct PgStatShared_ReplSlot
     445             : {
     446             :     PgStatShared_Common header;
     447             :     PgStat_StatReplSlotEntry stats;
     448             : } PgStatShared_ReplSlot;
     449             : 
     450             : 
     451             : /*
     452             :  * Central shared memory entry for the cumulative stats system.
     453             :  *
     454             :  * Fixed amount stats, the dynamic shared memory hash table for
     455             :  * non-fixed-amount stats, as well as remaining bits and pieces are all
     456             :  * reached from here.
     457             :  */
     458             : typedef struct PgStat_ShmemControl
     459             : {
     460             :     void       *raw_dsa_area;
     461             : 
     462             :     /*
     463             :      * Stats for variable-numbered objects are kept in this shared hash table.
     464             :      * See comment above PgStat_Kind for details.
     465             :      */
     466             :     dshash_table_handle hash_handle;    /* shared dbstat hash */
     467             : 
     468             :     /* Has the stats system already been shut down? Just a debugging check. */
     469             :     bool        is_shutdown;
     470             : 
     471             :     /*
     472             :      * Whenever statistics for dropped objects could not be freed - because
     473             :      * backends still have references - the dropping backend calls
     474             :      * pgstat_request_entry_refs_gc() incrementing this counter. Eventually
     475             :      * that causes backends to run pgstat_gc_entry_refs(), allowing memory to
     476             :      * be reclaimed.
     477             :      */
     478             :     pg_atomic_uint64 gc_request_count;
     479             : 
     480             :     /*
     481             :      * Stats data for fixed-numbered objects.
     482             :      */
     483             :     PgStatShared_Archiver archiver;
     484             :     PgStatShared_BgWriter bgwriter;
     485             :     PgStatShared_Checkpointer checkpointer;
     486             :     PgStatShared_IO io;
     487             :     PgStatShared_SLRU slru;
     488             :     PgStatShared_Wal wal;
     489             : 
     490             :     /*
     491             :      * Custom stats data with fixed-numbered objects, indexed by (PgStat_Kind
     492             :      * - PGSTAT_KIND_CUSTOM_MIN).
     493             :      */
     494             :     void       *custom_data[PGSTAT_KIND_CUSTOM_SIZE];
     495             : 
     496             : } PgStat_ShmemControl;
     497             : 
     498             : 
     499             : /*
     500             :  * Cached statistics snapshot
     501             :  */
     502             : typedef struct PgStat_Snapshot
     503             : {
     504             :     PgStat_FetchConsistency mode;
     505             : 
     506             :     /* time at which snapshot was taken */
     507             :     TimestampTz snapshot_timestamp;
     508             : 
     509             :     bool        fixed_valid[PGSTAT_KIND_BUILTIN_SIZE];
     510             : 
     511             :     PgStat_ArchiverStats archiver;
     512             : 
     513             :     PgStat_BgWriterStats bgwriter;
     514             : 
     515             :     PgStat_CheckpointerStats checkpointer;
     516             : 
     517             :     PgStat_IO   io;
     518             : 
     519             :     PgStat_SLRUStats slru[SLRU_NUM_ELEMENTS];
     520             : 
     521             :     PgStat_WalStats wal;
     522             : 
     523             :     /*
     524             :      * Data in snapshot for custom fixed-numbered statistics, indexed by
     525             :      * (PgStat_Kind - PGSTAT_KIND_CUSTOM_MIN).  Each entry is allocated in
     526             :      * TopMemoryContext, for a size of PgStat_KindInfo->shared_data_len.
     527             :      */
     528             :     bool        custom_valid[PGSTAT_KIND_CUSTOM_SIZE];
     529             :     void       *custom_data[PGSTAT_KIND_CUSTOM_SIZE];
     530             : 
     531             :     /* to free snapshot in bulk */
     532             :     MemoryContext context;
     533             :     struct pgstat_snapshot_hash *stats;
     534             : } PgStat_Snapshot;
     535             : 
     536             : 
     537             : /*
     538             :  * Collection of backend-local stats state.
     539             :  */
     540             : typedef struct PgStat_LocalState
     541             : {
     542             :     PgStat_ShmemControl *shmem;
     543             :     dsa_area   *dsa;
     544             :     dshash_table *shared_hash;
     545             : 
     546             :     /* the current statistics snapshot */
     547             :     PgStat_Snapshot snapshot;
     548             : } PgStat_LocalState;
     549             : 
     550             : 
     551             : /*
     552             :  * Inline functions defined further below.
     553             :  */
     554             : 
     555             : static inline void pgstat_begin_changecount_write(uint32 *cc);
     556             : static inline void pgstat_end_changecount_write(uint32 *cc);
     557             : static inline uint32 pgstat_begin_changecount_read(uint32 *cc);
     558             : static inline bool pgstat_end_changecount_read(uint32 *cc, uint32 cc_before);
     559             : 
     560             : static inline void pgstat_copy_changecounted_stats(void *dst, void *src, size_t len,
     561             :                                                    uint32 *cc);
     562             : 
     563             : static inline int pgstat_cmp_hash_key(const void *a, const void *b, size_t size, void *arg);
     564             : static inline uint32 pgstat_hash_hash_key(const void *d, size_t size, void *arg);
     565             : static inline size_t pgstat_get_entry_len(PgStat_Kind kind);
     566             : static inline void *pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry);
     567             : static inline void *pgstat_get_custom_shmem_data(PgStat_Kind kind);
     568             : static inline void *pgstat_get_custom_snapshot_data(PgStat_Kind kind);
     569             : 
     570             : 
     571             : /*
     572             :  * Functions in pgstat.c
     573             :  */
     574             : 
     575             : extern const PgStat_KindInfo *pgstat_get_kind_info(PgStat_Kind kind);
     576             : extern void pgstat_register_kind(PgStat_Kind kind,
     577             :                                  const PgStat_KindInfo *kind_info);
     578             : 
     579             : #ifdef USE_ASSERT_CHECKING
     580             : extern void pgstat_assert_is_up(void);
     581             : #else
     582             : #define pgstat_assert_is_up() ((void)true)
     583             : #endif
     584             : 
     585             : extern void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref);
     586             : extern PgStat_EntryRef *pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid,
     587             :                                                   uint64 objid,
     588             :                                                   bool *created_entry);
     589             : extern PgStat_EntryRef *pgstat_fetch_pending_entry(PgStat_Kind kind,
     590             :                                                    Oid dboid, uint64 objid);
     591             : 
     592             : extern void *pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid);
     593             : extern void pgstat_snapshot_fixed(PgStat_Kind kind);
     594             : 
     595             : 
     596             : /*
     597             :  * Functions in pgstat_archiver.c
     598             :  */
     599             : 
     600             : extern void pgstat_archiver_init_shmem_cb(void *stats);
     601             : extern void pgstat_archiver_reset_all_cb(TimestampTz ts);
     602             : extern void pgstat_archiver_snapshot_cb(void);
     603             : 
     604             : 
     605             : /*
     606             :  * Functions in pgstat_bgwriter.c
     607             :  */
     608             : 
     609             : extern void pgstat_bgwriter_init_shmem_cb(void *stats);
     610             : extern void pgstat_bgwriter_reset_all_cb(TimestampTz ts);
     611             : extern void pgstat_bgwriter_snapshot_cb(void);
     612             : 
     613             : 
     614             : /*
     615             :  * Functions in pgstat_checkpointer.c
     616             :  */
     617             : 
     618             : extern void pgstat_checkpointer_init_shmem_cb(void *stats);
     619             : extern void pgstat_checkpointer_reset_all_cb(TimestampTz ts);
     620             : extern void pgstat_checkpointer_snapshot_cb(void);
     621             : 
     622             : 
     623             : /*
     624             :  * Functions in pgstat_database.c
     625             :  */
     626             : 
     627             : extern void pgstat_report_disconnect(Oid dboid);
     628             : extern void pgstat_update_dbstats(TimestampTz ts);
     629             : extern void AtEOXact_PgStat_Database(bool isCommit, bool parallel);
     630             : 
     631             : extern PgStat_StatDBEntry *pgstat_prep_database_pending(Oid dboid);
     632             : extern void pgstat_reset_database_timestamp(Oid dboid, TimestampTz ts);
     633             : extern bool pgstat_database_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
     634             : extern void pgstat_database_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts);
     635             : 
     636             : 
     637             : /*
     638             :  * Functions in pgstat_function.c
     639             :  */
     640             : 
     641             : extern bool pgstat_function_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
     642             : 
     643             : 
     644             : /*
     645             :  * Functions in pgstat_io.c
     646             :  */
     647             : 
     648             : extern void pgstat_flush_io(bool nowait);
     649             : 
     650             : extern bool pgstat_io_have_pending_cb(void);
     651             : extern bool pgstat_io_flush_cb(bool nowait);
     652             : extern void pgstat_io_init_shmem_cb(void *stats);
     653             : extern void pgstat_io_reset_all_cb(TimestampTz ts);
     654             : extern void pgstat_io_snapshot_cb(void);
     655             : 
     656             : 
     657             : /*
     658             :  * Functions in pgstat_relation.c
     659             :  */
     660             : 
     661             : extern void AtEOXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit);
     662             : extern void AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, int nestDepth);
     663             : extern void AtPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state);
     664             : extern void PostPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state);
     665             : 
     666             : extern bool pgstat_relation_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
     667             : extern void pgstat_relation_delete_pending_cb(PgStat_EntryRef *entry_ref);
     668             : 
     669             : 
     670             : /*
     671             :  * Functions in pgstat_replslot.c
     672             :  */
     673             : 
     674             : extern void pgstat_replslot_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts);
     675             : extern void pgstat_replslot_to_serialized_name_cb(const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name);
     676             : extern bool pgstat_replslot_from_serialized_name_cb(const NameData *name, PgStat_HashKey *key);
     677             : 
     678             : 
     679             : /*
     680             :  * Functions in pgstat_shmem.c
     681             :  */
     682             : 
     683             : extern void pgstat_attach_shmem(void);
     684             : extern void pgstat_detach_shmem(void);
     685             : 
     686             : extern PgStat_EntryRef *pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid,
     687             :                                              bool create, bool *created_entry);
     688             : extern bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait);
     689             : extern bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait);
     690             : extern void pgstat_unlock_entry(PgStat_EntryRef *entry_ref);
     691             : extern bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid);
     692             : extern void pgstat_drop_all_entries(void);
     693             : extern PgStat_EntryRef *pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid,
     694             :                                                     bool nowait);
     695             : extern void pgstat_reset_entry(PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts);
     696             : extern void pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts);
     697             : extern void pgstat_reset_matching_entries(bool (*do_reset) (PgStatShared_HashEntry *, Datum),
     698             :                                           Datum match_data,
     699             :                                           TimestampTz ts);
     700             : 
     701             : extern void pgstat_request_entry_refs_gc(void);
     702             : extern PgStatShared_Common *pgstat_init_entry(PgStat_Kind kind,
     703             :                                               PgStatShared_HashEntry *shhashent);
     704             : 
     705             : 
     706             : /*
     707             :  * Functions in pgstat_slru.c
     708             :  */
     709             : 
     710             : extern bool pgstat_slru_have_pending_cb(void);
     711             : extern bool pgstat_slru_flush_cb(bool nowait);
     712             : extern void pgstat_slru_init_shmem_cb(void *stats);
     713             : extern void pgstat_slru_reset_all_cb(TimestampTz ts);
     714             : extern void pgstat_slru_snapshot_cb(void);
     715             : 
     716             : 
     717             : /*
     718             :  * Functions in pgstat_wal.c
     719             :  */
     720             : 
     721             : extern void pgstat_flush_wal(bool nowait);
     722             : 
     723             : extern void pgstat_wal_init_backend_cb(void);
     724             : extern bool pgstat_wal_have_pending_cb(void);
     725             : extern bool pgstat_wal_flush_cb(bool nowait);
     726             : extern void pgstat_wal_init_shmem_cb(void *stats);
     727             : extern void pgstat_wal_reset_all_cb(TimestampTz ts);
     728             : extern void pgstat_wal_snapshot_cb(void);
     729             : 
     730             : 
     731             : /*
     732             :  * Functions in pgstat_subscription.c
     733             :  */
     734             : 
     735             : extern bool pgstat_subscription_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
     736             : extern void pgstat_subscription_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts);
     737             : 
     738             : 
     739             : /*
     740             :  * Functions in pgstat_xact.c
     741             :  */
     742             : 
     743             : extern PgStat_SubXactStatus *pgstat_get_xact_stack_level(int nest_level);
     744             : extern void pgstat_drop_transactional(PgStat_Kind kind, Oid dboid, uint64 objid);
     745             : extern void pgstat_create_transactional(PgStat_Kind kind, Oid dboid, uint64 objid);
     746             : 
     747             : 
     748             : /*
     749             :  * Variables in pgstat.c
     750             :  */
     751             : 
     752             : extern PGDLLIMPORT PgStat_LocalState pgStatLocal;
     753             : 
     754             : 
     755             : /*
     756             :  * Implementation of inline functions declared above.
     757             :  */
     758             : 
     759             : /*
     760             :  * Helpers for changecount manipulation. See comments around struct
     761             :  * PgBackendStatus for details.
     762             :  */
     763             : 
     764             : static inline void
     765       18452 : pgstat_begin_changecount_write(uint32 *cc)
     766             : {
     767             :     Assert((*cc & 1) == 0);
     768             : 
     769       18452 :     START_CRIT_SECTION();
     770       18452 :     (*cc)++;
     771       18452 :     pg_write_barrier();
     772       18452 : }
     773             : 
     774             : static inline void
     775       18452 : pgstat_end_changecount_write(uint32 *cc)
     776             : {
     777             :     Assert((*cc & 1) == 1);
     778             : 
     779       18452 :     pg_write_barrier();
     780             : 
     781       18452 :     (*cc)++;
     782             : 
     783       18452 :     END_CRIT_SECTION();
     784       18452 : }
     785             : 
     786             : static inline uint32
     787        4918 : pgstat_begin_changecount_read(uint32 *cc)
     788             : {
     789        4918 :     uint32      before_cc = *cc;
     790             : 
     791        4918 :     CHECK_FOR_INTERRUPTS();
     792             : 
     793        4918 :     pg_read_barrier();
     794             : 
     795        4918 :     return before_cc;
     796             : }
     797             : 
     798             : /*
     799             :  * Returns true if the read succeeded, false if it needs to be repeated.
     800             :  */
     801             : static inline bool
     802        4918 : pgstat_end_changecount_read(uint32 *cc, uint32 before_cc)
     803             : {
     804             :     uint32      after_cc;
     805             : 
     806        4918 :     pg_read_barrier();
     807             : 
     808        4918 :     after_cc = *cc;
     809             : 
     810             :     /* was a write in progress when we started? */
     811        4918 :     if (before_cc & 1)
     812           0 :         return false;
     813             : 
     814             :     /* did writes start and complete while we read? */
     815        4918 :     return before_cc == after_cc;
     816             : }
     817             : 
     818             : 
     819             : /*
     820             :  * helper function for PgStat_KindInfo->snapshot_cb
     821             :  * PgStat_KindInfo->reset_all_cb callbacks.
     822             :  *
     823             :  * Copies out the specified memory area following change-count protocol.
     824             :  */
     825             : static inline void
     826        4918 : pgstat_copy_changecounted_stats(void *dst, void *src, size_t len,
     827             :                                 uint32 *cc)
     828             : {
     829             :     uint32      cc_before;
     830             : 
     831             :     do
     832             :     {
     833        4918 :         cc_before = pgstat_begin_changecount_read(cc);
     834             : 
     835        4918 :         memcpy(dst, src, len);
     836             :     }
     837        4918 :     while (!pgstat_end_changecount_read(cc, cc_before));
     838        4918 : }
     839             : 
     840             : /* helpers for dshash / simplehash hashtables */
     841             : static inline int
     842     7677176 : pgstat_cmp_hash_key(const void *a, const void *b, size_t size, void *arg)
     843             : {
     844             :     Assert(size == sizeof(PgStat_HashKey) && arg == NULL);
     845     7677176 :     return memcmp(a, b, sizeof(PgStat_HashKey));
     846             : }
     847             : 
     848             : static inline uint32
     849     9818600 : pgstat_hash_hash_key(const void *d, size_t size, void *arg)
     850             : {
     851     9818600 :     const char *key = (const char *) d;
     852             : 
     853             :     Assert(size == sizeof(PgStat_HashKey) && arg == NULL);
     854     9818600 :     return fasthash32(key, size, 0);
     855             : }
     856             : 
     857             : /*
     858             :  * The length of the data portion of a shared memory stats entry (i.e. without
     859             :  * transient data such as refcounts, lwlocks, ...).
     860             :  */
     861             : static inline size_t
     862      646232 : pgstat_get_entry_len(PgStat_Kind kind)
     863             : {
     864      646232 :     return pgstat_get_kind_info(kind)->shared_data_len;
     865             : }
     866             : 
     867             : /*
     868             :  * Returns a pointer to the data portion of a shared memory stats entry.
     869             :  */
     870             : static inline void *
     871      880934 : pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry)
     872             : {
     873      880934 :     size_t      off = pgstat_get_kind_info(kind)->shared_data_off;
     874             : 
     875             :     Assert(off != 0 && off < PG_UINT32_MAX);
     876             : 
     877      880934 :     return ((char *) (entry)) + off;
     878             : }
     879             : 
     880             : /*
     881             :  * Returns a pointer to the shared memory area of custom stats for
     882             :  * fixed-numbered statistics.
     883             :  */
     884             : static inline void *
     885          24 : pgstat_get_custom_shmem_data(PgStat_Kind kind)
     886             : {
     887          24 :     int         idx = kind - PGSTAT_KIND_CUSTOM_MIN;
     888             : 
     889             :     Assert(pgstat_is_kind_custom(kind));
     890             :     Assert(pgstat_get_kind_info(kind)->fixed_amount);
     891             : 
     892          24 :     return pgStatLocal.shmem->custom_data[idx];
     893             : }
     894             : 
     895             : /*
     896             :  * Returns a pointer to the portion of custom data for fixed-numbered
     897             :  * statistics in the current snapshot.
     898             :  */
     899             : static inline void *
     900          18 : pgstat_get_custom_snapshot_data(PgStat_Kind kind)
     901             : {
     902          18 :     int         idx = kind - PGSTAT_KIND_CUSTOM_MIN;
     903             : 
     904             :     Assert(pgstat_is_kind_custom(kind));
     905             :     Assert(pgstat_get_kind_info(kind)->fixed_amount);
     906             : 
     907          18 :     return pgStatLocal.snapshot.custom_data[idx];
     908             : }
     909             : 
     910             : #endif                          /* PGSTAT_INTERNAL_H */

Generated by: LCOV version 1.14