LCOV - code coverage report
Current view: top level - src/include/utils - pgstat_internal.h (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 97.7 % 44 43
Test Date: 2026-10-14 03:15:50 Functions: 100.0 % 12 12
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 50.0 % 6 3

             Branch data     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-2026, 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 shared stats hashtables. Those tables'
      29                 :             :  * entries (PgStatShared_HashEntry) contain a pointer to the actual stats data
      30                 :             :  * for the object (the size of the stats data varies depending on the kind of
      31                 :             :  * stats). The tables are 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) that caches
      40                 :             :  * references to shared stats hashtable entries, mean that most stats work can
      41                 :             :  * happen without touching the shared stats hashtables, 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 variable-numbered shared stats struct has PgStatShared_Common as the
      48                 :             :  * first element.
      49                 :             :  */
      50                 :             : 
      51                 :             : /*
      52                 :             :  * Struct for shared statistics hash entry key.
      53                 :             :  *
      54                 :             :  * NB: We assume that this struct contains no padding.  Also, 8 bytes
      55                 :             :  * allocated for the object ID are good enough to ensure the uniqueness
      56                 :             :  * of the hash key, hence the addition of new fields is not recommended.
      57                 :             :  */
      58                 :             : typedef struct PgStat_HashKey
      59                 :             : {
      60                 :             :     PgStat_Kind kind;           /* statistics entry kind */
      61                 :             :     Oid         dboid;          /* database ID. InvalidOid for shared objects. */
      62                 :             :     uint64      objid;          /* object ID (table, function, etc.), or
      63                 :             :                                  * identifier. */
      64                 :             : } PgStat_HashKey;
      65                 :             : 
      66                 :             : /*
      67                 :             :  * Tracks if the stats file is being read, written or discarded, used in
      68                 :             :  * combination with the finish callback.
      69                 :             :  *
      70                 :             :  * These states allow plugins that create auxiliary data files to determine
      71                 :             :  * the current operation and perform any necessary file cleanup.
      72                 :             :  */
      73                 :             : typedef enum PgStat_StatsFileOp
      74                 :             : {
      75                 :             :     STATS_WRITE,
      76                 :             :     STATS_READ,
      77                 :             :     STATS_DISCARD,
      78                 :             : } PgStat_StatsFileOp;
      79                 :             : 
      80                 :             : /*
      81                 :             :  * PgStat_HashKey should not have any padding.  Checking that the structure
      82                 :             :  * size matches with the sum of each field is a check simple enough to
      83                 :             :  * enforce this policy.
      84                 :             :  */
      85                 :             : StaticAssertDecl((sizeof(PgStat_Kind) + sizeof(uint64) + sizeof(Oid)) ==
      86                 :             :                  sizeof(PgStat_HashKey),
      87                 :             :                  "PgStat_HashKey should have no padding");
      88                 :             : 
      89                 :             : /*
      90                 :             :  * Shared statistics hash entry. Doesn't itself contain any stats, but points
      91                 :             :  * to them (with ->body). That allows the stats entries themselves to be of
      92                 :             :  * variable size.
      93                 :             :  */
      94                 :             : typedef struct PgStatShared_HashEntry
      95                 :             : {
      96                 :             :     PgStat_HashKey key;         /* hash key */
      97                 :             : 
      98                 :             :     /*
      99                 :             :      * If dropped is set, backends need to release their references so that
     100                 :             :      * the memory for the entry can be freed. No new references may be made
     101                 :             :      * once marked as dropped.
     102                 :             :      */
     103                 :             :     bool        dropped;
     104                 :             : 
     105                 :             :     /*
     106                 :             :      * Refcount managing lifetime of the entry itself (as opposed to the
     107                 :             :      * dshash entry pointing to it). The stats lifetime has to be separate
     108                 :             :      * from the hash table entry lifetime because we allow backends to point
     109                 :             :      * to a stats entry without holding a hash table lock (and some other
     110                 :             :      * reasons).
     111                 :             :      *
     112                 :             :      * As long as the entry is not dropped, 1 is added to the refcount
     113                 :             :      * representing that the entry should not be dropped. In addition each
     114                 :             :      * backend that has a reference to the entry needs to increment the
     115                 :             :      * refcount as long as it does.
     116                 :             :      *
     117                 :             :      * May only be incremented / decremented while holding at least a shared
     118                 :             :      * lock on the dshash partition containing the entry. It needs to be an
     119                 :             :      * atomic variable because multiple backends can increment the refcount
     120                 :             :      * with just a shared lock.
     121                 :             :      *
     122                 :             :      * When the refcount reaches 0 the entry needs to be freed.
     123                 :             :      */
     124                 :             :     pg_atomic_uint32 refcount;
     125                 :             : 
     126                 :             :     /*
     127                 :             :      * Counter tracking the number of times the entry has been reused.
     128                 :             :      *
     129                 :             :      * Set to 0 when the entry is created, and incremented by one each time
     130                 :             :      * the shared entry is reinitialized with pgstat_reinit_entry().
     131                 :             :      *
     132                 :             :      * May only be incremented / decremented while holding at least a shared
     133                 :             :      * lock on the dshash partition containing the entry. Like refcount, it
     134                 :             :      * needs to be an atomic variable because multiple backends can increment
     135                 :             :      * the generation with just a shared lock.
     136                 :             :      */
     137                 :             :     pg_atomic_uint32 generation;
     138                 :             : 
     139                 :             :     /*
     140                 :             :      * Pointer to shared stats. The stats entry always starts with
     141                 :             :      * PgStatShared_Common, embedded in a larger struct containing the
     142                 :             :      * PgStat_Kind specific stats fields.
     143                 :             :      */
     144                 :             :     dsa_pointer body;
     145                 :             : } PgStatShared_HashEntry;
     146                 :             : 
     147                 :             : /*
     148                 :             :  * Common header struct for PgStatShared_*.
     149                 :             :  */
     150                 :             : typedef struct PgStatShared_Common
     151                 :             : {
     152                 :             :     uint32      magic;          /* just a validity cross-check */
     153                 :             :     /* lock protecting stats contents (i.e. data following the header) */
     154                 :             :     LWLock      lock;
     155                 :             : } PgStatShared_Common;
     156                 :             : 
     157                 :             : /*
     158                 :             :  * A backend local reference to a shared stats entry. As long as at least one
     159                 :             :  * such reference exists, the shared stats entry will not be released.
     160                 :             :  *
     161                 :             :  * If there are pending stats update to the shared stats, these are stored in
     162                 :             :  * ->pending.
     163                 :             :  */
     164                 :             : typedef struct PgStat_EntryRef
     165                 :             : {
     166                 :             :     /*
     167                 :             :      * Pointer to the PgStatShared_HashEntry entry in the shared stats
     168                 :             :      * hashtable.
     169                 :             :      */
     170                 :             :     PgStatShared_HashEntry *shared_entry;
     171                 :             : 
     172                 :             :     /*
     173                 :             :      * Pointer to the stats data (i.e. PgStatShared_HashEntry->body), resolved
     174                 :             :      * as a local pointer, to avoid repeated dsa_get_address() calls.
     175                 :             :      */
     176                 :             :     PgStatShared_Common *shared_stats;
     177                 :             : 
     178                 :             :     /*
     179                 :             :      * Copy of PgStatShared_HashEntry->generation, keeping locally track of
     180                 :             :      * the shared stats entry "generation" retrieved (number of times reused).
     181                 :             :      */
     182                 :             :     uint32      generation;
     183                 :             : 
     184                 :             :     /*
     185                 :             :      * Pending statistics data that will need to be flushed to shared memory
     186                 :             :      * stats eventually. Each stats kind utilizing pending data defines what
     187                 :             :      * format its pending data has and needs to provide a
     188                 :             :      * PgStat_KindInfo->flush_pending_cb callback to merge pending entries
     189                 :             :      * into shared stats hashtable entries.
     190                 :             :      */
     191                 :             :     void       *pending;
     192                 :             :     dlist_node  pending_node;   /* membership in pgStatPending list */
     193                 :             : } PgStat_EntryRef;
     194                 :             : 
     195                 :             : 
     196                 :             : /*
     197                 :             :  * Some stats changes are transactional. To maintain those, a stack of
     198                 :             :  * PgStat_SubXactStatus entries is maintained, which contain data pertaining
     199                 :             :  * to the current transaction and its active subtransactions.
     200                 :             :  */
     201                 :             : typedef struct PgStat_SubXactStatus
     202                 :             : {
     203                 :             :     int         nest_level;     /* subtransaction nest level */
     204                 :             : 
     205                 :             :     struct PgStat_SubXactStatus *prev;  /* higher-level subxact if any */
     206                 :             : 
     207                 :             :     /*
     208                 :             :      * Statistics for transactionally dropped objects need to be
     209                 :             :      * transactionally dropped as well. Collect the stats dropped in the
     210                 :             :      * current (sub-)transaction and only execute the stats drop when we know
     211                 :             :      * if the transaction commits/aborts. To handle replicas and crashes,
     212                 :             :      * stats drops are included in commit / abort records.
     213                 :             :      */
     214                 :             :     dclist_head pending_drops;
     215                 :             : 
     216                 :             :     /*
     217                 :             :      * Tuple insertion/deletion counts for an open transaction can't be
     218                 :             :      * propagated into PgStat_RelationStatus counters until we know if it is
     219                 :             :      * going to commit or abort.  Hence, we keep these counts in per-subxact
     220                 :             :      * structs that live in TopTransactionContext.  This data structure is
     221                 :             :      * designed on the assumption that subxacts won't usually modify very many
     222                 :             :      * tables.
     223                 :             :      */
     224                 :             :     PgStat_TableXactStatus *first;  /* head of list for this subxact */
     225                 :             : } PgStat_SubXactStatus;
     226                 :             : 
     227                 :             : 
     228                 :             : /*
     229                 :             :  * Metadata for a specific kind of statistics.
     230                 :             :  */
     231                 :             : typedef struct PgStat_KindInfo
     232                 :             : {
     233                 :             :     /*
     234                 :             :      * Do a fixed number of stats objects exist for this kind of stats (e.g.
     235                 :             :      * bgwriter stats) or not (e.g. tables).
     236                 :             :      */
     237                 :             :     bool        fixed_amount:1;
     238                 :             : 
     239                 :             :     /*
     240                 :             :      * Can stats of this kind be accessed from another database? Determines
     241                 :             :      * whether a stats object gets included in stats snapshots.
     242                 :             :      */
     243                 :             :     bool        accessed_across_databases:1;
     244                 :             : 
     245                 :             :     /* Should stats be written to the on-disk stats file? */
     246                 :             :     bool        write_to_file:1;
     247                 :             : 
     248                 :             :     /*
     249                 :             :      * Should the number of entries be tracked?  For variable-numbered stats,
     250                 :             :      * to update its PgStat_ShmemControl.entry_counts.
     251                 :             :      */
     252                 :             :     bool        track_entry_count:1;
     253                 :             : 
     254                 :             :     /*
     255                 :             :      * Should entries of this kind be stored in a dedicated dshash table
     256                 :             :      * rather than the shared hash table? For variable-numbered stats only.
     257                 :             :      *
     258                 :             :      * A kind setting this option creates its own DSA area in the main shared
     259                 :             :      * memory.  This is worth only for kinds expected to hold a large number
     260                 :             :      * of entries.
     261                 :             :      */
     262                 :             :     bool        own_hash:1;
     263                 :             : 
     264                 :             :     /*
     265                 :             :      * The size of an entry pointed to by PgStatShared_HashEntry->body.  For
     266                 :             :      * fixed-numbered statistics, this is the size of an entry in
     267                 :             :      * PgStat_ShmemControl->custom_data.
     268                 :             :      */
     269                 :             :     uint32      shared_size;
     270                 :             : 
     271                 :             :     /*
     272                 :             :      * The offset of the statistics struct in the cached statistics snapshot
     273                 :             :      * PgStat_Snapshot, for fixed-numbered statistics.
     274                 :             :      */
     275                 :             :     uint32      snapshot_ctl_off;
     276                 :             : 
     277                 :             :     /*
     278                 :             :      * The offset of the statistics struct in the containing shared memory
     279                 :             :      * control structure PgStat_ShmemControl, for fixed-numbered statistics.
     280                 :             :      */
     281                 :             :     uint32      shared_ctl_off;
     282                 :             : 
     283                 :             :     /*
     284                 :             :      * The offset/size of statistics inside the shared stats entry. Used when
     285                 :             :      * [de-]serializing statistics to / from disk respectively. Separate from
     286                 :             :      * shared_size because [de-]serialization may not include in-memory state
     287                 :             :      * like lwlocks.
     288                 :             :      */
     289                 :             :     uint32      shared_data_off;
     290                 :             :     uint32      shared_data_len;
     291                 :             : 
     292                 :             :     /*
     293                 :             :      * The size of the pending data for this kind. E.g. how large
     294                 :             :      * PgStat_EntryRef->pending is. Used for allocations.
     295                 :             :      *
     296                 :             :      * 0 signals that an entry of this kind should never have a pending entry.
     297                 :             :      */
     298                 :             :     uint32      pending_size;
     299                 :             : 
     300                 :             :     /*
     301                 :             :      * Perform custom actions when initializing a backend (standalone or under
     302                 :             :      * postmaster). Optional.
     303                 :             :      */
     304                 :             :     void        (*init_backend_cb) (void);
     305                 :             : 
     306                 :             :     /*
     307                 :             :      * For variable-numbered stats: flush pending stats. Required if pending
     308                 :             :      * data is used. See flush_static_cb when dealing with stats data that
     309                 :             :      * that cannot use PgStat_EntryRef->pending.
     310                 :             :      */
     311                 :             :     bool        (*flush_pending_cb) (PgStat_EntryRef *sr, bool nowait);
     312                 :             : 
     313                 :             :     /*
     314                 :             :      * For variable-numbered stats: delete pending stats. Optional.
     315                 :             :      */
     316                 :             :     void        (*delete_pending_cb) (PgStat_EntryRef *sr);
     317                 :             : 
     318                 :             :     /*
     319                 :             :      * For variable-numbered stats: reset the reset timestamp. Optional.
     320                 :             :      */
     321                 :             :     void        (*reset_timestamp_cb) (PgStatShared_Common *header, TimestampTz ts);
     322                 :             : 
     323                 :             :     /*
     324                 :             :      * For variable-numbered stats. Optional.
     325                 :             :      */
     326                 :             :     void        (*to_serialized_name) (const PgStat_HashKey *key,
     327                 :             :                                        const PgStatShared_Common *header, NameData *name);
     328                 :             :     bool        (*from_serialized_name) (const NameData *name, PgStat_HashKey *key);
     329                 :             : 
     330                 :             :     /*
     331                 :             :      * For variable-numbered stats: read or write additional data related to
     332                 :             :      * an entry, in the stats file or optionally in a different file.
     333                 :             :      * Optional.
     334                 :             :      *
     335                 :             :      * to_serialized_data: write auxiliary data for an entry.  Returns true on
     336                 :             :      * success, false on write error.
     337                 :             :      *
     338                 :             :      * from_serialized_data: read auxiliary data for an entry.  Returns true
     339                 :             :      * on success, false on read error.
     340                 :             :      *
     341                 :             :      * "statfile" is a pointer to the on-disk stats file, named
     342                 :             :      * PGSTAT_STAT_PERMANENT_FILENAME.  "key" is the hash key of the entry
     343                 :             :      * just written or read.  "header" is a pointer to the stats data; it may
     344                 :             :      * be modified only in from_serialized_data to reconstruct an entry.
     345                 :             :      */
     346                 :             :     bool        (*to_serialized_data) (const PgStat_HashKey *key,
     347                 :             :                                        const PgStatShared_Common *header,
     348                 :             :                                        FILE *statfile);
     349                 :             :     bool        (*from_serialized_data) (const PgStat_HashKey *key,
     350                 :             :                                          PgStatShared_Common *header,
     351                 :             :                                          FILE *statfile);
     352                 :             : 
     353                 :             :     /*
     354                 :             :      * For fixed-numbered or variable-numbered statistics.
     355                 :             :      *
     356                 :             :      * Perform custom actions when done processing the on-disk stats file
     357                 :             :      * after all the stats entries have been processed.  Optional.
     358                 :             :      *
     359                 :             :      * "status" tracks the operation done for the on-disk stats file (read,
     360                 :             :      * write, discard).
     361                 :             :      */
     362                 :             :     void        (*finish) (PgStat_StatsFileOp status);
     363                 :             : 
     364                 :             :     /*
     365                 :             :      * For fixed-numbered statistics: Initialize shared memory state.
     366                 :             :      *
     367                 :             :      * "stats" is the pointer to the allocated shared memory area.
     368                 :             :      */
     369                 :             :     void        (*init_shmem_cb) (void *stats);
     370                 :             : 
     371                 :             :     /*
     372                 :             :      * For fixed-numbered or variable-numbered statistics: Flush pending stats
     373                 :             :      * entries, for stats kinds that do not use PgStat_EntryRef->pending.
     374                 :             :      *
     375                 :             :      * Returns true if some of the stats could not be flushed, due to lock
     376                 :             :      * contention for example. Optional.
     377                 :             :      *
     378                 :             :      * "pgstat_report_fixed" needs to be set to trigger the flush of pending
     379                 :             :      * stats.
     380                 :             :      */
     381                 :             :     bool        (*flush_static_cb) (bool nowait);
     382                 :             : 
     383                 :             :     /*
     384                 :             :      * For fixed-numbered statistics: Reset All.
     385                 :             :      */
     386                 :             :     void        (*reset_all_cb) (TimestampTz ts);
     387                 :             : 
     388                 :             :     /*
     389                 :             :      * For fixed-numbered statistics: Build snapshot for entry
     390                 :             :      */
     391                 :             :     void        (*snapshot_cb) (void);
     392                 :             : 
     393                 :             :     /* name of the kind of stats */
     394                 :             :     const char *const name;
     395                 :             : } PgStat_KindInfo;
     396                 :             : 
     397                 :             : 
     398                 :             : /*
     399                 :             :  * List of SLRU names that we keep stats for.  There is no central registry of
     400                 :             :  * SLRUs, so we use this fixed list instead.  The "other" entry is used for
     401                 :             :  * all SLRUs without an explicit entry (e.g. SLRUs in extensions).
     402                 :             :  *
     403                 :             :  * This is only defined here so that SLRU_NUM_ELEMENTS is known for later type
     404                 :             :  * definitions.
     405                 :             :  */
     406                 :             : static const char *const slru_names[] = {
     407                 :             :     "commit_timestamp",
     408                 :             :     "multixact_member",
     409                 :             :     "multixact_offset",
     410                 :             :     "notify",
     411                 :             :     "serializable",
     412                 :             :     "subtransaction",
     413                 :             :     "transaction",
     414                 :             :     "other"                       /* has to be last */
     415                 :             : };
     416                 :             : 
     417                 :             : #define SLRU_NUM_ELEMENTS   lengthof(slru_names)
     418                 :             : 
     419                 :             : 
     420                 :             : /* ----------
     421                 :             :  * Types and definitions for different kinds of fixed-amount stats.
     422                 :             :  *
     423                 :             :  * Single-writer stats use the changecount mechanism to achieve low-overhead
     424                 :             :  * writes - they're obviously more performance critical than reads. Check the
     425                 :             :  * definition of struct PgBackendStatus for some explanation of the
     426                 :             :  * changecount mechanism.
     427                 :             :  *
     428                 :             :  * Because the obvious implementation of resetting single-writer stats isn't
     429                 :             :  * compatible with that (another backend needs to write), we don't scribble on
     430                 :             :  * shared stats while resetting. Instead, just record the current counter
     431                 :             :  * values in a copy of the stats data, which is protected by ->lock. See
     432                 :             :  * pgstat_fetch_stat_(archiver|bgwriter|checkpointer) for the reader side.
     433                 :             :  *
     434                 :             :  * The only exception to that is the stat_reset_timestamp in these structs,
     435                 :             :  * which is protected by ->lock, because it has to be written by another
     436                 :             :  * backend while resetting.
     437                 :             :  * ----------
     438                 :             :  */
     439                 :             : 
     440                 :             : typedef struct PgStatShared_Archiver
     441                 :             : {
     442                 :             :     /* lock protects ->reset_offset as well as stats->stat_reset_timestamp */
     443                 :             :     LWLock      lock;
     444                 :             :     uint32      changecount;
     445                 :             :     PgStat_ArchiverStats stats;
     446                 :             :     PgStat_ArchiverStats reset_offset;
     447                 :             : } PgStatShared_Archiver;
     448                 :             : 
     449                 :             : typedef struct PgStatShared_BgWriter
     450                 :             : {
     451                 :             :     /* lock protects ->reset_offset as well as stats->stat_reset_timestamp */
     452                 :             :     LWLock      lock;
     453                 :             :     uint32      changecount;
     454                 :             :     PgStat_BgWriterStats stats;
     455                 :             :     PgStat_BgWriterStats reset_offset;
     456                 :             : } PgStatShared_BgWriter;
     457                 :             : 
     458                 :             : typedef struct PgStatShared_Checkpointer
     459                 :             : {
     460                 :             :     /* lock protects ->reset_offset as well as stats->stat_reset_timestamp */
     461                 :             :     LWLock      lock;
     462                 :             :     uint32      changecount;
     463                 :             :     PgStat_CheckpointerStats stats;
     464                 :             :     PgStat_CheckpointerStats reset_offset;
     465                 :             : } PgStatShared_Checkpointer;
     466                 :             : 
     467                 :             : /* Shared-memory ready PgStat_IO */
     468                 :             : typedef struct PgStatShared_IO
     469                 :             : {
     470                 :             :     /*
     471                 :             :      * locks[i] protects stats.stats[i]. locks[0] also protects
     472                 :             :      * stats.stat_reset_timestamp.
     473                 :             :      */
     474                 :             :     LWLock      locks[BACKEND_NUM_TYPES];
     475                 :             :     PgStat_IO   stats;
     476                 :             : } PgStatShared_IO;
     477                 :             : 
     478                 :             : typedef struct PgStatShared_Lock
     479                 :             : {
     480                 :             :     /* lock protects ->stats */
     481                 :             :     LWLock      lock;
     482                 :             :     PgStat_Lock stats;
     483                 :             : } PgStatShared_Lock;
     484                 :             : 
     485                 :             : typedef struct PgStatShared_SLRU
     486                 :             : {
     487                 :             :     /* lock protects ->stats */
     488                 :             :     LWLock      lock;
     489                 :             :     PgStat_SLRUStats stats[SLRU_NUM_ELEMENTS];
     490                 :             : } PgStatShared_SLRU;
     491                 :             : 
     492                 :             : typedef struct PgStatShared_Wal
     493                 :             : {
     494                 :             :     /* lock protects ->stats */
     495                 :             :     LWLock      lock;
     496                 :             :     PgStat_WalStats stats;
     497                 :             : } PgStatShared_Wal;
     498                 :             : 
     499                 :             : 
     500                 :             : 
     501                 :             : /* ----------
     502                 :             :  * Types and definitions for different kinds of variable-amount stats.
     503                 :             :  *
     504                 :             :  * Each struct has to start with PgStatShared_Common, containing information
     505                 :             :  * common across the different types of stats. Kind-specific data follows.
     506                 :             :  * ----------
     507                 :             :  */
     508                 :             : 
     509                 :             : typedef struct PgStatShared_Database
     510                 :             : {
     511                 :             :     PgStatShared_Common header;
     512                 :             :     PgStat_StatDBEntry stats;
     513                 :             : } PgStatShared_Database;
     514                 :             : 
     515                 :             : typedef struct PgStatShared_Relation
     516                 :             : {
     517                 :             :     PgStatShared_Common header;
     518                 :             :     PgStat_StatTabEntry stats;
     519                 :             : } PgStatShared_Relation;
     520                 :             : 
     521                 :             : typedef struct PgStatShared_Index
     522                 :             : {
     523                 :             :     PgStatShared_Common header;
     524                 :             :     PgStat_StatIdxEntry stats;
     525                 :             : } PgStatShared_Index;
     526                 :             : 
     527                 :             : typedef struct PgStatShared_Function
     528                 :             : {
     529                 :             :     PgStatShared_Common header;
     530                 :             :     PgStat_StatFuncEntry stats;
     531                 :             : } PgStatShared_Function;
     532                 :             : 
     533                 :             : typedef struct PgStatShared_Subscription
     534                 :             : {
     535                 :             :     PgStatShared_Common header;
     536                 :             :     PgStat_StatSubEntry stats;
     537                 :             : } PgStatShared_Subscription;
     538                 :             : 
     539                 :             : typedef struct PgStatShared_ReplSlot
     540                 :             : {
     541                 :             :     PgStatShared_Common header;
     542                 :             :     PgStat_StatReplSlotEntry stats;
     543                 :             : } PgStatShared_ReplSlot;
     544                 :             : 
     545                 :             : typedef struct PgStatShared_Backend
     546                 :             : {
     547                 :             :     PgStatShared_Common header;
     548                 :             :     PgStat_Backend stats;
     549                 :             : } PgStatShared_Backend;
     550                 :             : 
     551                 :             : /*
     552                 :             :  * Central shared memory entry for the cumulative stats system.
     553                 :             :  *
     554                 :             :  * Fixed amount stats, the dynamic shared memory hashtables for
     555                 :             :  * non-fixed-amount stats, as well as remaining bits and pieces are all
     556                 :             :  * reached from here.
     557                 :             :  */
     558                 :             : typedef struct PgStat_ShmemControl
     559                 :             : {
     560                 :             :     void       *raw_dsa_area;
     561                 :             : 
     562                 :             :     /* Shared hash for variable-numbered stats kinds without own_hash set. */
     563                 :             :     dshash_table_handle hash_handle;    /* shared stats hash */
     564                 :             : 
     565                 :             :     /* Dedicated hash and DSA for variable-numbered kinds with own_hash set. */
     566                 :             :     dshash_table_handle kind_hash_handles[PGSTAT_KIND_MAX + 1];
     567                 :             :     void       *raw_kind_dsa_area[PGSTAT_KIND_MAX + 1];
     568                 :             : 
     569                 :             :     /* Has the stats system already been shut down? Just a debugging check. */
     570                 :             :     bool        is_shutdown;
     571                 :             : 
     572                 :             :     /*
     573                 :             :      * Whenever statistics for dropped objects could not be freed - because
     574                 :             :      * backends still have references - the dropping backend calls
     575                 :             :      * pgstat_request_entry_refs_gc() incrementing this counter. Eventually
     576                 :             :      * that causes backends to run pgstat_gc_entry_refs(), allowing memory to
     577                 :             :      * be reclaimed.
     578                 :             :      */
     579                 :             :     pg_atomic_uint64 gc_request_count;
     580                 :             : 
     581                 :             :     /*
     582                 :             :      * Counters for the number of entries associated to a single
     583                 :             :      * variable-numbered stats kind.  These counters can be enabled on a
     584                 :             :      * per-kind basis, when track_entry_count is set.  A counter is
     585                 :             :      * incremented each time a new entry is created (not reused) in a stats
     586                 :             :      * hashtable, and is decremented each time an entry is freed from a stats
     587                 :             :      * hashtable.
     588                 :             :      */
     589                 :             :     pg_atomic_uint64 entry_counts[PGSTAT_KIND_MAX];
     590                 :             : 
     591                 :             :     /*
     592                 :             :      * Stats data for fixed-numbered objects.
     593                 :             :      */
     594                 :             :     PgStatShared_Archiver archiver;
     595                 :             :     PgStatShared_BgWriter bgwriter;
     596                 :             :     PgStatShared_Checkpointer checkpointer;
     597                 :             :     PgStatShared_IO io;
     598                 :             :     PgStatShared_Lock lock;
     599                 :             :     PgStatShared_SLRU slru;
     600                 :             :     PgStatShared_Wal wal;
     601                 :             : 
     602                 :             :     /*
     603                 :             :      * Custom stats data with fixed-numbered objects, indexed by (PgStat_Kind
     604                 :             :      * - PGSTAT_KIND_CUSTOM_MIN).
     605                 :             :      */
     606                 :             :     void       *custom_data[PGSTAT_KIND_CUSTOM_SIZE];
     607                 :             : 
     608                 :             : } PgStat_ShmemControl;
     609                 :             : 
     610                 :             : 
     611                 :             : /*
     612                 :             :  * Cached statistics snapshot
     613                 :             :  */
     614                 :             : typedef struct PgStat_Snapshot
     615                 :             : {
     616                 :             :     PgStat_FetchConsistency mode;
     617                 :             : 
     618                 :             :     /* time at which snapshot was taken */
     619                 :             :     TimestampTz snapshot_timestamp;
     620                 :             : 
     621                 :             :     bool        fixed_valid[PGSTAT_KIND_BUILTIN_SIZE];
     622                 :             : 
     623                 :             :     PgStat_ArchiverStats archiver;
     624                 :             : 
     625                 :             :     PgStat_BgWriterStats bgwriter;
     626                 :             : 
     627                 :             :     PgStat_CheckpointerStats checkpointer;
     628                 :             : 
     629                 :             :     PgStat_IO   io;
     630                 :             : 
     631                 :             :     PgStat_Lock lock;
     632                 :             : 
     633                 :             :     PgStat_SLRUStats slru[SLRU_NUM_ELEMENTS];
     634                 :             : 
     635                 :             :     PgStat_WalStats wal;
     636                 :             : 
     637                 :             :     /*
     638                 :             :      * Data in snapshot for custom fixed-numbered statistics, indexed by
     639                 :             :      * (PgStat_Kind - PGSTAT_KIND_CUSTOM_MIN).  Each entry is allocated in
     640                 :             :      * TopMemoryContext, for a size of PgStat_KindInfo->shared_data_len.
     641                 :             :      */
     642                 :             :     bool        custom_valid[PGSTAT_KIND_CUSTOM_SIZE];
     643                 :             :     void       *custom_data[PGSTAT_KIND_CUSTOM_SIZE];
     644                 :             : 
     645                 :             :     /* to free snapshot in bulk */
     646                 :             :     MemoryContext context;
     647                 :             :     struct pgstat_snapshot_hash *stats;
     648                 :             : } PgStat_Snapshot;
     649                 :             : 
     650                 :             : 
     651                 :             : /*
     652                 :             :  * Collection of backend-local stats state.
     653                 :             :  */
     654                 :             : typedef struct PgStat_LocalState
     655                 :             : {
     656                 :             :     PgStat_ShmemControl *shmem;
     657                 :             :     dsa_area   *kind_dsa[PGSTAT_KIND_MAX + 1];
     658                 :             :     dshash_table *kind_hash[PGSTAT_KIND_MAX + 1];
     659                 :             : 
     660                 :             :     /* Variable-numbered stats hash tables to iterate, built at attach time. */
     661                 :             :     dshash_table *var_hashes[PGSTAT_KIND_MAX + 1];
     662                 :             :     int         num_var_hashes;
     663                 :             : 
     664                 :             :     /* the current statistics snapshot */
     665                 :             :     PgStat_Snapshot snapshot;
     666                 :             : } PgStat_LocalState;
     667                 :             : 
     668                 :             : 
     669                 :             : /*
     670                 :             :  * Inline functions defined further below.
     671                 :             :  */
     672                 :             : 
     673                 :             : static inline void pgstat_begin_changecount_write(uint32 *cc);
     674                 :             : static inline void pgstat_end_changecount_write(uint32 *cc);
     675                 :             : static inline uint32 pgstat_begin_changecount_read(uint32 *cc);
     676                 :             : static inline bool pgstat_end_changecount_read(uint32 *cc, uint32 cc_before);
     677                 :             : 
     678                 :             : static inline void pgstat_copy_changecounted_stats(void *dst, void *src, size_t len,
     679                 :             :                                                    uint32 *cc);
     680                 :             : 
     681                 :             : static inline int pgstat_cmp_hash_key(const void *a, const void *b, size_t size, void *arg);
     682                 :             : static inline uint32 pgstat_hash_hash_key(const void *d, size_t size, void *arg);
     683                 :             : static inline size_t pgstat_get_entry_len(PgStat_Kind kind);
     684                 :             : static inline void *pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry);
     685                 :             : static inline void *pgstat_get_custom_shmem_data(PgStat_Kind kind);
     686                 :             : static inline void *pgstat_get_custom_snapshot_data(PgStat_Kind kind);
     687                 :             : 
     688                 :             : 
     689                 :             : /*
     690                 :             :  * Functions in pgstat.c
     691                 :             :  */
     692                 :             : 
     693                 :             : extern const PgStat_KindInfo *pgstat_get_kind_info(PgStat_Kind kind);
     694                 :             : extern void pgstat_register_kind(PgStat_Kind kind,
     695                 :             :                                  const PgStat_KindInfo *kind_info);
     696                 :             : 
     697                 :             : #ifdef USE_ASSERT_CHECKING
     698                 :             : extern void pgstat_assert_is_up(void);
     699                 :             : #else
     700                 :             : #define pgstat_assert_is_up() ((void)true)
     701                 :             : #endif
     702                 :             : 
     703                 :             : extern void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref);
     704                 :             : extern void pgstat_prep_pending_from_entry_ref(PgStat_EntryRef *entry_ref);
     705                 :             : extern PgStat_EntryRef *pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid,
     706                 :             :                                                   uint64 objid,
     707                 :             :                                                   bool *created_entry);
     708                 :             : extern PgStat_EntryRef *pgstat_fetch_pending_entry(PgStat_Kind kind,
     709                 :             :                                                    Oid dboid, uint64 objid);
     710                 :             : 
     711                 :             : extern void *pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid,
     712                 :             :                                 bool *may_free);
     713                 :             : extern void pgstat_snapshot_fixed(PgStat_Kind kind);
     714                 :             : 
     715                 :             : 
     716                 :             : /*
     717                 :             :  * Functions in pgstat_archiver.c
     718                 :             :  */
     719                 :             : 
     720                 :             : extern void pgstat_archiver_init_shmem_cb(void *stats);
     721                 :             : extern void pgstat_archiver_reset_all_cb(TimestampTz ts);
     722                 :             : extern void pgstat_archiver_snapshot_cb(void);
     723                 :             : 
     724                 :             : /*
     725                 :             :  * Functions in pgstat_backend.c
     726                 :             :  */
     727                 :             : 
     728                 :             : /* flags for pgstat_flush_backend() */
     729                 :             : #define PGSTAT_BACKEND_FLUSH_IO     (1 << 0)  /* Flush I/O statistics */
     730                 :             : #define PGSTAT_BACKEND_FLUSH_WAL   (1 << 1) /* Flush WAL statistics */
     731                 :             : #define PGSTAT_BACKEND_FLUSH_LOCK  (1 << 2) /* Flush lock statistics */
     732                 :             : #define PGSTAT_BACKEND_FLUSH_ALL   (PGSTAT_BACKEND_FLUSH_IO | PGSTAT_BACKEND_FLUSH_WAL | PGSTAT_BACKEND_FLUSH_LOCK)
     733                 :             : 
     734                 :             : extern bool pgstat_flush_backend(bool nowait, uint32 flags);
     735                 :             : extern bool pgstat_backend_flush_cb(bool nowait);
     736                 :             : extern void pgstat_backend_reset_timestamp_cb(PgStatShared_Common *header,
     737                 :             :                                               TimestampTz ts);
     738                 :             : 
     739                 :             : /*
     740                 :             :  * Functions in pgstat_bgwriter.c
     741                 :             :  */
     742                 :             : 
     743                 :             : extern void pgstat_bgwriter_init_shmem_cb(void *stats);
     744                 :             : extern void pgstat_bgwriter_reset_all_cb(TimestampTz ts);
     745                 :             : extern void pgstat_bgwriter_snapshot_cb(void);
     746                 :             : 
     747                 :             : 
     748                 :             : /*
     749                 :             :  * Functions in pgstat_checkpointer.c
     750                 :             :  */
     751                 :             : 
     752                 :             : extern void pgstat_checkpointer_init_shmem_cb(void *stats);
     753                 :             : extern void pgstat_checkpointer_reset_all_cb(TimestampTz ts);
     754                 :             : extern void pgstat_checkpointer_snapshot_cb(void);
     755                 :             : 
     756                 :             : 
     757                 :             : /*
     758                 :             :  * Functions in pgstat_database.c
     759                 :             :  */
     760                 :             : 
     761                 :             : extern void pgstat_report_disconnect(Oid dboid);
     762                 :             : extern void pgstat_update_dbstats(TimestampTz ts);
     763                 :             : extern void AtEOXact_PgStat_Database(bool isCommit, bool parallel);
     764                 :             : 
     765                 :             : extern PgStat_StatDBEntry *pgstat_prep_database_pending(Oid dboid);
     766                 :             : extern void pgstat_reset_database_timestamp(Oid dboid, TimestampTz ts);
     767                 :             : extern bool pgstat_database_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
     768                 :             : extern void pgstat_database_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts);
     769                 :             : 
     770                 :             : 
     771                 :             : /*
     772                 :             :  * Functions in pgstat_function.c
     773                 :             :  */
     774                 :             : 
     775                 :             : extern bool pgstat_function_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
     776                 :             : extern void pgstat_function_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts);
     777                 :             : 
     778                 :             : 
     779                 :             : /*
     780                 :             :  * Functions in pgstat_io.c
     781                 :             :  */
     782                 :             : 
     783                 :             : extern void pgstat_flush_io(bool nowait);
     784                 :             : 
     785                 :             : extern bool pgstat_io_flush_cb(bool nowait);
     786                 :             : extern void pgstat_io_init_shmem_cb(void *stats);
     787                 :             : extern void pgstat_io_reset_all_cb(TimestampTz ts);
     788                 :             : extern void pgstat_io_snapshot_cb(void);
     789                 :             : 
     790                 :             : /*
     791                 :             :  * Functions in pgstat_lock.c
     792                 :             :  */
     793                 :             : 
     794                 :             : extern bool pgstat_lock_flush_cb(bool nowait);
     795                 :             : extern void pgstat_lock_init_shmem_cb(void *stats);
     796                 :             : extern void pgstat_lock_reset_all_cb(TimestampTz ts);
     797                 :             : extern void pgstat_lock_snapshot_cb(void);
     798                 :             : 
     799                 :             : /*
     800                 :             :  * Functions in pgstat_relation.c
     801                 :             :  */
     802                 :             : 
     803                 :             : extern void AtEOXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit);
     804                 :             : extern void AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, int nestDepth);
     805                 :             : extern void AtPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state);
     806                 :             : extern void PostPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state);
     807                 :             : 
     808                 :             : extern bool pgstat_relation_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
     809                 :             : extern void pgstat_relation_delete_pending_cb(PgStat_EntryRef *entry_ref);
     810                 :             : extern void pgstat_relation_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts);
     811                 :             : 
     812                 :             : 
     813                 :             : /*
     814                 :             :  * Functions in pgstat_index.c
     815                 :             :  */
     816                 :             : 
     817                 :             : extern bool pgstat_index_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
     818                 :             : extern void pgstat_index_delete_pending_cb(PgStat_EntryRef *entry_ref);
     819                 :             : extern void pgstat_index_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts);
     820                 :             : 
     821                 :             : 
     822                 :             : /*
     823                 :             :  * Functions in pgstat_replslot.c
     824                 :             :  */
     825                 :             : 
     826                 :             : extern void pgstat_replslot_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts);
     827                 :             : extern void pgstat_replslot_to_serialized_name_cb(const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name);
     828                 :             : extern bool pgstat_replslot_from_serialized_name_cb(const NameData *name, PgStat_HashKey *key);
     829                 :             : 
     830                 :             : 
     831                 :             : /*
     832                 :             :  * Functions in pgstat_shmem.c
     833                 :             :  */
     834                 :             : 
     835                 :             : extern void pgstat_attach_shmem(void);
     836                 :             : extern void pgstat_detach_shmem(void);
     837                 :             : 
     838                 :             : extern PgStat_EntryRef *pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid,
     839                 :             :                                              bool create, bool *created_entry);
     840                 :             : extern bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait);
     841                 :             : extern bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait);
     842                 :             : extern void pgstat_unlock_entry(PgStat_EntryRef *entry_ref);
     843                 :             : extern bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid,
     844                 :             :                               bool missing_ok);
     845                 :             : extern void pgstat_drop_all_entries(void);
     846                 :             : extern void pgstat_drop_matching_entries(bool (*do_drop) (PgStatShared_HashEntry *, Datum),
     847                 :             :                                          Datum match_data);
     848                 :             : extern PgStat_EntryRef *pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid,
     849                 :             :                                                     bool nowait);
     850                 :             : extern void pgstat_reset_entry(PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts);
     851                 :             : extern void pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts);
     852                 :             : extern void pgstat_reset_matching_entries(bool (*do_reset) (PgStatShared_HashEntry *, Datum),
     853                 :             :                                           Datum match_data,
     854                 :             :                                           TimestampTz ts);
     855                 :             : 
     856                 :             : extern void pgstat_request_entry_refs_gc(void);
     857                 :             : extern dsa_pointer pgstat_alloc_entry_body(PgStat_Kind kind);
     858                 :             : extern PgStatShared_Common *pgstat_init_entry(PgStat_Kind kind,
     859                 :             :                                               PgStatShared_HashEntry *shhashent,
     860                 :             :                                               dsa_pointer chunk);
     861                 :             : 
     862                 :             : 
     863                 :             : /*
     864                 :             :  * Functions in pgstat_slru.c
     865                 :             :  */
     866                 :             : 
     867                 :             : extern bool pgstat_slru_flush_cb(bool nowait);
     868                 :             : extern void pgstat_slru_init_shmem_cb(void *stats);
     869                 :             : extern void pgstat_slru_reset_all_cb(TimestampTz ts);
     870                 :             : extern void pgstat_slru_snapshot_cb(void);
     871                 :             : 
     872                 :             : 
     873                 :             : /*
     874                 :             :  * Functions in pgstat_wal.c
     875                 :             :  */
     876                 :             : 
     877                 :             : extern void pgstat_wal_init_backend_cb(void);
     878                 :             : extern bool pgstat_wal_flush_cb(bool nowait);
     879                 :             : extern void pgstat_wal_init_shmem_cb(void *stats);
     880                 :             : extern void pgstat_wal_reset_all_cb(TimestampTz ts);
     881                 :             : extern void pgstat_wal_snapshot_cb(void);
     882                 :             : 
     883                 :             : 
     884                 :             : /*
     885                 :             :  * Functions in pgstat_subscription.c
     886                 :             :  */
     887                 :             : 
     888                 :             : extern bool pgstat_subscription_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
     889                 :             : extern void pgstat_subscription_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts);
     890                 :             : 
     891                 :             : 
     892                 :             : /*
     893                 :             :  * Functions in pgstat_xact.c
     894                 :             :  */
     895                 :             : 
     896                 :             : extern PgStat_SubXactStatus *pgstat_get_xact_stack_level(int nest_level);
     897                 :             : extern void pgstat_drop_transactional(PgStat_Kind kind, Oid dboid, uint64 objid);
     898                 :             : extern void pgstat_create_transactional(PgStat_Kind kind, Oid dboid, uint64 objid);
     899                 :             : 
     900                 :             : 
     901                 :             : /*
     902                 :             :  * Variables in pgstat.c
     903                 :             :  */
     904                 :             : 
     905                 :             : /*
     906                 :             :  * Track if *any* pending fixed-numbered statistics should be flushed to
     907                 :             :  * shared memory.
     908                 :             :  *
     909                 :             :  * This flag can be switched to true by fixed-numbered statistics to let
     910                 :             :  * pgstat_report_stat() know if it needs to go through one round of
     911                 :             :  * reports, calling flush_static_cb for each fixed-numbered statistics
     912                 :             :  * kind.  When this flag is not set, pgstat_report_stat() is able to do
     913                 :             :  * a fast exit, knowing that there are no pending fixed-numbered statistics.
     914                 :             :  *
     915                 :             :  * Statistics callbacks should never reset this flag; pgstat_report_stat()
     916                 :             :  * is in charge of doing that.
     917                 :             :  */
     918                 :             : extern PGDLLIMPORT bool pgstat_report_fixed;
     919                 :             : 
     920                 :             : /* Backend-local stats state */
     921                 :             : extern PGDLLIMPORT PgStat_LocalState pgStatLocal;
     922                 :             : 
     923                 :             : 
     924                 :             : /*
     925                 :             :  * Implementation of inline functions declared above.
     926                 :             :  */
     927                 :             : 
     928                 :             : /*
     929                 :             :  * Helpers for changecount manipulation. See comments around struct
     930                 :             :  * PgBackendStatus for details.
     931                 :             :  */
     932                 :             : 
     933                 :             : static inline void
     934                 :       14273 : pgstat_begin_changecount_write(uint32 *cc)
     935                 :             : {
     936                 :             :     Assert((*cc & 1) == 0);
     937                 :             : 
     938                 :       14273 :     START_CRIT_SECTION();
     939                 :       14273 :     (*cc)++;
     940                 :       14273 :     pg_write_barrier();
     941                 :       14273 : }
     942                 :             : 
     943                 :             : static inline void
     944                 :       14273 : pgstat_end_changecount_write(uint32 *cc)
     945                 :             : {
     946                 :             :     Assert((*cc & 1) == 1);
     947                 :             : 
     948                 :       14273 :     pg_write_barrier();
     949                 :             : 
     950                 :       14273 :     (*cc)++;
     951                 :             : 
     952                 :       14273 :     END_CRIT_SECTION();
     953                 :       14273 : }
     954                 :             : 
     955                 :             : static inline uint32
     956                 :        3429 : pgstat_begin_changecount_read(uint32 *cc)
     957                 :             : {
     958                 :        3429 :     uint32      before_cc = *cc;
     959                 :             : 
     960         [ -  + ]:        3429 :     CHECK_FOR_INTERRUPTS();
     961                 :             : 
     962                 :        3429 :     pg_read_barrier();
     963                 :             : 
     964                 :        3429 :     return before_cc;
     965                 :             : }
     966                 :             : 
     967                 :             : /*
     968                 :             :  * Returns true if the read succeeded, false if it needs to be repeated.
     969                 :             :  */
     970                 :             : static inline bool
     971                 :        3429 : pgstat_end_changecount_read(uint32 *cc, uint32 before_cc)
     972                 :             : {
     973                 :             :     uint32      after_cc;
     974                 :             : 
     975                 :        3429 :     pg_read_barrier();
     976                 :             : 
     977                 :        3429 :     after_cc = *cc;
     978                 :             : 
     979                 :             :     /* was a write in progress when we started? */
     980         [ -  + ]:        3429 :     if (before_cc & 1)
     981                 :           0 :         return false;
     982                 :             : 
     983                 :             :     /* did writes start and complete while we read? */
     984                 :        3429 :     return before_cc == after_cc;
     985                 :             : }
     986                 :             : 
     987                 :             : 
     988                 :             : /*
     989                 :             :  * helper function for PgStat_KindInfo->snapshot_cb
     990                 :             :  * PgStat_KindInfo->reset_all_cb callbacks.
     991                 :             :  *
     992                 :             :  * Copies out the specified memory area following change-count protocol.
     993                 :             :  */
     994                 :             : static inline void
     995                 :        3429 : pgstat_copy_changecounted_stats(void *dst, void *src, size_t len,
     996                 :             :                                 uint32 *cc)
     997                 :             : {
     998                 :             :     uint32      cc_before;
     999                 :             : 
    1000                 :             :     do
    1001                 :             :     {
    1002                 :        3429 :         cc_before = pgstat_begin_changecount_read(cc);
    1003                 :             : 
    1004                 :        3429 :         memcpy(dst, src, len);
    1005                 :             :     }
    1006         [ -  + ]:        3429 :     while (!pgstat_end_changecount_read(cc, cc_before));
    1007                 :        3429 : }
    1008                 :             : 
    1009                 :             : /* helpers for dshash / simplehash hashtables */
    1010                 :             : static inline int
    1011                 :     7756176 : pgstat_cmp_hash_key(const void *a, const void *b, size_t size, void *arg)
    1012                 :             : {
    1013                 :             :     Assert(size == sizeof(PgStat_HashKey) && arg == NULL);
    1014                 :     7756176 :     return memcmp(a, b, sizeof(PgStat_HashKey));
    1015                 :             : }
    1016                 :             : 
    1017                 :             : static inline uint32
    1018                 :     9392653 : pgstat_hash_hash_key(const void *d, size_t size, void *arg)
    1019                 :             : {
    1020                 :     9392653 :     const char *key = (const char *) d;
    1021                 :             : 
    1022                 :             :     Assert(size == sizeof(PgStat_HashKey) && arg == NULL);
    1023                 :     9392653 :     return fasthash32(key, size, 0);
    1024                 :             : }
    1025                 :             : 
    1026                 :             : /*
    1027                 :             :  * The length of the data portion of a shared memory stats entry (i.e. without
    1028                 :             :  * transient data such as refcounts, lwlocks, ...).
    1029                 :             :  */
    1030                 :             : static inline size_t
    1031                 :      548347 : pgstat_get_entry_len(PgStat_Kind kind)
    1032                 :             : {
    1033                 :      548347 :     return pgstat_get_kind_info(kind)->shared_data_len;
    1034                 :             : }
    1035                 :             : 
    1036                 :             : /*
    1037                 :             :  * Returns a pointer to the data portion of a shared memory stats entry.
    1038                 :             :  */
    1039                 :             : static inline void *
    1040                 :      809054 : pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry)
    1041                 :             : {
    1042                 :      809054 :     size_t      off = pgstat_get_kind_info(kind)->shared_data_off;
    1043                 :             : 
    1044                 :             :     Assert(off != 0 && off < PG_UINT32_MAX);
    1045                 :             : 
    1046                 :      809054 :     return ((char *) (entry)) + off;
    1047                 :             : }
    1048                 :             : 
    1049                 :             : /*
    1050                 :             :  * Returns the number of entries counted for a stats kind.
    1051                 :             :  */
    1052                 :             : static inline uint64
    1053                 :           3 : pgstat_get_entry_count(PgStat_Kind kind)
    1054                 :             : {
    1055                 :             :     Assert(pgstat_get_kind_info(kind)->track_entry_count);
    1056                 :             : 
    1057                 :           3 :     return pg_atomic_read_u64(&pgStatLocal.shmem->entry_counts[kind - 1]);
    1058                 :             : }
    1059                 :             : 
    1060                 :             : /*
    1061                 :             :  * Returns a pointer to the shared memory area of custom stats for
    1062                 :             :  * fixed-numbered statistics.
    1063                 :             :  */
    1064                 :             : static inline void *
    1065                 :          19 : pgstat_get_custom_shmem_data(PgStat_Kind kind)
    1066                 :             : {
    1067                 :          19 :     int         idx = kind - PGSTAT_KIND_CUSTOM_MIN;
    1068                 :             : 
    1069                 :             :     Assert(pgstat_is_kind_custom(kind));
    1070                 :             :     Assert(pgstat_get_kind_info(kind)->fixed_amount);
    1071                 :             : 
    1072                 :          19 :     return pgStatLocal.shmem->custom_data[idx];
    1073                 :             : }
    1074                 :             : 
    1075                 :             : /*
    1076                 :             :  * Returns a pointer to the portion of custom data for fixed-numbered
    1077                 :             :  * statistics in the current snapshot.
    1078                 :             :  */
    1079                 :             : static inline void *
    1080                 :          13 : pgstat_get_custom_snapshot_data(PgStat_Kind kind)
    1081                 :             : {
    1082                 :          13 :     int         idx = kind - PGSTAT_KIND_CUSTOM_MIN;
    1083                 :             : 
    1084                 :             :     Assert(pgstat_is_kind_custom(kind));
    1085                 :             :     Assert(pgstat_get_kind_info(kind)->fixed_amount);
    1086                 :             : 
    1087                 :          13 :     return pgStatLocal.snapshot.custom_data[idx];
    1088                 :             : }
    1089                 :             : 
    1090                 :             : #endif                          /* PGSTAT_INTERNAL_H */
        

Generated by: LCOV version 2.0-1