LCOV - differential code coverage report
Current view: top level - src/backend/utils/activity - pgstat_shmem.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB DCB
Current: 1a1248818fb8b8b0353c292c632d6a6af77e8209 vs 8e82d6610e7280d7870c0c36ff1c797e3cfb3e09 Lines: 91.3 % 404 369 8 27 14 355 4 9
Current Date: 2026-10-12 11:56:10 +0200 Functions: 100.0 % 37 37 5 32
Baseline: lcov-20261012-baseline Branches: 65.0 % 260 169 10 81 4 165
Baseline Date: 2026-10-12 10:36:06 +0200 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 52.9 % 17 9 4 4 8 1
(30,360] days: 84.3 % 51 43 4 4 6 37
(360..) days: 94.3 % 336 317 19 317
Function coverage date bins:
(7,30] days: 100.0 % 1 1 1
(30,360] days: 100.0 % 3 3 3
(360..) days: 100.0 % 33 33 4 29
Branch coverage date bins:
(7,30] days: 25.0 % 12 3 4 5 2 1
(30,360] days: 53.6 % 28 15 6 7 2 13
(360..) days: 68.6 % 220 151 69 151

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /* -------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * pgstat_shmem.c
                                  4                 :                :  *    Storage of stats entries in shared memory
                                  5                 :                :  *
                                  6                 :                :  * Copyright (c) 2001-2026, PostgreSQL Global Development Group
                                  7                 :                :  *
                                  8                 :                :  * IDENTIFICATION
                                  9                 :                :  *    src/backend/utils/activity/pgstat_shmem.c
                                 10                 :                :  * -------------------------------------------------------------------------
                                 11                 :                :  */
                                 12                 :                : 
                                 13                 :                : #include "postgres.h"
                                 14                 :                : 
                                 15                 :                : #include "pgstat.h"
                                 16                 :                : #include "storage/shmem.h"
                                 17                 :                : #include "storage/subsystems.h"
                                 18                 :                : #include "utils/memutils.h"
                                 19                 :                : #include "utils/pgstat_internal.h"
                                 20                 :                : 
                                 21                 :                : 
                                 22                 :                : #define PGSTAT_ENTRY_REF_HASH_SIZE  128
                                 23                 :                : 
                                 24                 :                : /* hash table entry for finding the PgStat_EntryRef for a key */
                                 25                 :                : typedef struct PgStat_EntryRefHashEntry
                                 26                 :                : {
                                 27                 :                :     PgStat_HashKey key;         /* hash key */
                                 28                 :                :     char        status;         /* for simplehash use */
                                 29                 :                :     PgStat_EntryRef *entry_ref;
                                 30                 :                : } PgStat_EntryRefHashEntry;
                                 31                 :                : 
                                 32                 :                : 
                                 33                 :                : /* for references to shared statistics entries */
                                 34                 :                : #define SH_PREFIX pgstat_entry_ref_hash
                                 35                 :                : #define SH_ELEMENT_TYPE PgStat_EntryRefHashEntry
                                 36                 :                : #define SH_KEY_TYPE PgStat_HashKey
                                 37                 :                : #define SH_KEY key
                                 38                 :                : #define SH_HASH_KEY(tb, key) \
                                 39                 :                :     pgstat_hash_hash_key(&key, sizeof(PgStat_HashKey), NULL)
                                 40                 :                : #define SH_EQUAL(tb, a, b) \
                                 41                 :                :     pgstat_cmp_hash_key(&a, &b, sizeof(PgStat_HashKey), NULL) == 0
                                 42                 :                : #define SH_SCOPE static inline
                                 43                 :                : #define SH_DEFINE
                                 44                 :                : #define SH_DECLARE
                                 45                 :                : #include "lib/simplehash.h"
                                 46                 :                : 
                                 47                 :                : 
                                 48                 :                : static void pgstat_drop_database_and_contents(Oid dboid);
                                 49                 :                : 
                                 50                 :                : static void pgstat_free_entry(PgStatShared_HashEntry *shent, dshash_seq_status *hstat);
                                 51                 :                : 
                                 52                 :                : static void pgstat_release_entry_ref(PgStat_HashKey key, PgStat_EntryRef *entry_ref, bool discard_pending);
                                 53                 :                : static bool pgstat_need_entry_refs_gc(void);
                                 54                 :                : static void pgstat_gc_entry_refs(void);
                                 55                 :                : static void pgstat_release_all_entry_refs(bool discard_pending);
                                 56                 :                : typedef bool (*ReleaseMatchCB) (PgStat_EntryRefHashEntry *, Datum data);
                                 57                 :                : static void pgstat_release_matching_entry_refs(bool discard_pending, ReleaseMatchCB match, Datum match_data);
                                 58                 :                : 
                                 59                 :                : static void pgstat_setup_memcxt(void);
                                 60                 :                : 
                                 61                 :                : static void StatsShmemRequest(void *arg);
                                 62                 :                : static void StatsShmemInit(void *arg);
                                 63                 :                : 
                                 64                 :                : const ShmemCallbacks StatsShmemCallbacks = {
                                 65                 :                :     .request_fn = StatsShmemRequest,
                                 66                 :                :     .init_fn = StatsShmemInit,
                                 67                 :                : };
                                 68                 :                : 
                                 69                 :                : /* parameter for the shared hash */
                                 70                 :                : static const dshash_parameters dsh_params = {
                                 71                 :                :     sizeof(PgStat_HashKey),
                                 72                 :                :     sizeof(PgStatShared_HashEntry),
                                 73                 :                :     pgstat_cmp_hash_key,
                                 74                 :                :     pgstat_hash_hash_key,
                                 75                 :                :     dshash_memcpy,
                                 76                 :                :     LWTRANCHE_PGSTATS_HASH
                                 77                 :                : };
                                 78                 :                : 
                                 79                 :                : 
                                 80                 :                : /*
                                 81                 :                :  * Backend local references to shared stats entries. If there are pending
                                 82                 :                :  * updates to a stats entry, the PgStat_EntryRef is added to the pgStatPending
                                 83                 :                :  * list.
                                 84                 :                :  *
                                 85                 :                :  * When a stats entry is dropped each backend needs to release its reference
                                 86                 :                :  * to it before the memory can be released. To trigger that
                                 87                 :                :  * pgStatLocal.shmem->gc_request_count is incremented - which each backend
                                 88                 :                :  * compares to their copy of pgStatSharedRefAge on a regular basis.
                                 89                 :                :  */
                                 90                 :                : static pgstat_entry_ref_hash_hash *pgStatEntryRefHash = NULL;
                                 91                 :                : static int  pgStatSharedRefAge = 0; /* cache age of pgStatLocal.shmem */
                                 92                 :                : 
                                 93                 :                : /*
                                 94                 :                :  * Memory contexts containing the pgStatEntryRefHash table and the
                                 95                 :                :  * pgStatSharedRef entries respectively. Kept separate to make it easier to
                                 96                 :                :  * track / attribute memory usage.
                                 97                 :                :  */
                                 98                 :                : static MemoryContext pgStatSharedRefContext = NULL;
                                 99                 :                : static MemoryContext pgStatEntryRefHashContext = NULL;
                                100                 :                : 
                                101                 :                : 
                                102                 :                : /* ------------------------------------------------------------
                                103                 :                :  * Public functions called from postmaster follow
                                104                 :                :  * ------------------------------------------------------------
                                105                 :                :  */
                                106                 :                : 
                                107                 :                : /*
                                108                 :                :  * The size of the shared memory allocation for stats stored in the shared
                                109                 :                :  * stats hash table. This allocation will be done as part of the main shared
                                110                 :                :  * memory, rather than dynamic shared memory, allowing it to be initialized in
                                111                 :                :  * postmaster.
                                112                 :                :  */
                                113                 :                : static Size
 1650 andres@anarazel.de        114                 :CBC        5136 : pgstat_dsa_init_size(void)
                                115                 :                : {
                                116                 :                :     Size        sz;
                                117                 :                : 
                                118                 :                :     /*
                                119                 :                :      * The dshash header / initial buckets array needs to fit into "plain"
                                120                 :                :      * shared memory, but it's beneficial to not need dsm segments
                                121                 :                :      * immediately. A size of 256kB seems works well and is not
                                122                 :                :      * disproportional compared to other constant sized shared memory
                                123                 :                :      * allocations. NB: To avoid DSMs further, the user can configure
                                124                 :                :      * min_dynamic_shared_memory.
                                125                 :                :      */
                                126                 :           5136 :     sz = 256 * 1024;
                                127         [ -  + ]:           5136 :     Assert(dsa_minimum_size() <= sz);
                                128                 :           5136 :     return MAXALIGN(sz);
                                129                 :                : }
                                130                 :                : 
                                131                 :                : /*
                                132                 :                :  * Compute shared memory space needed for cumulative statistics
                                133                 :                :  */
                                134                 :                : static Size
                                135                 :           1287 : StatsShmemSize(void)
                                136                 :                : {
                                137                 :                :     Size        sz;
                                138                 :                : 
                                139                 :           1287 :     sz = MAXALIGN(sizeof(PgStat_ShmemControl));
                                140                 :           1287 :     sz = add_size(sz, pgstat_dsa_init_size());
                                141                 :                : 
                                142                 :                :     /* Add shared memory for all the custom fixed-numbered statistics */
  799 michael@paquier.xyz       143         [ +  + ]:          12870 :     for (PgStat_Kind kind = PGSTAT_KIND_CUSTOM_MIN; kind <= PGSTAT_KIND_CUSTOM_MAX; kind++)
                                144                 :                :     {
                                145                 :          11583 :         const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                                146                 :                : 
                                147         [ +  + ]:          11583 :         if (!kind_info)
                                148                 :          11577 :             continue;
                                149         [ +  + ]:              6 :         if (!kind_info->fixed_amount)
                                150                 :              3 :             continue;
                                151                 :                : 
                                152         [ -  + ]:              3 :         Assert(kind_info->shared_size != 0);
  188                           153                 :              3 :         sz = add_size(sz, MAXALIGN(kind_info->shared_size));
                                154                 :                :     }
                                155                 :                : 
 1650 andres@anarazel.de        156                 :           1287 :     return sz;
                                157                 :                : }
                                158                 :                : 
                                159                 :                : /*
                                160                 :                :  * Register shared memory area for cumulative statistics
                                161                 :                :  */
                                162                 :                : static void
  189 heikki.linnakangas@i      163                 :           1287 : StatsShmemRequest(void *arg)
                                164                 :                : {
                                165                 :           1287 :     ShmemRequestStruct(.name = "Shared Memory Stats",
                                166                 :                :                        .size = StatsShmemSize(),
                                167                 :                :                        .ptr = (void **) &pgStatLocal.shmem,
                                168                 :                :         );
                                169                 :           1287 : }
                                170                 :                : 
                                171                 :                : /*
                                172                 :                :  * Initialize cumulative statistics system during startup
                                173                 :                :  */
                                174                 :                : static void
                                175                 :           1283 : StatsShmemInit(void *arg)
                                176                 :                : {
                                177                 :                :     dsa_area   *dsa;
                                178                 :                :     dshash_table *dsh;
                                179                 :           1283 :     PgStat_ShmemControl *ctl = pgStatLocal.shmem;
                                180                 :           1283 :     char       *p = (char *) ctl;
                                181                 :                : 
                                182                 :                :     /* the allocation of pgStatLocal.shmem itself */
                                183                 :           1283 :     p += MAXALIGN(sizeof(PgStat_ShmemControl));
                                184                 :                : 
                                185                 :                :     /*
                                186                 :                :      * Create a small dsa allocation in plain shared memory. This is required
                                187                 :                :      * because postmaster cannot use dsm segments. It also provides a small
                                188                 :                :      * efficiency win.
                                189                 :                :      */
                                190                 :           1283 :     ctl->raw_dsa_area = p;
  188 michael@paquier.xyz       191                 :           1283 :     p += pgstat_dsa_init_size();
  189 heikki.linnakangas@i      192                 :           1283 :     dsa = dsa_create_in_place(ctl->raw_dsa_area,
                                193                 :                :                               pgstat_dsa_init_size(),
                                194                 :                :                               LWTRANCHE_PGSTATS_DSA, NULL);
                                195                 :           1283 :     dsa_pin(dsa);
                                196                 :                : 
                                197                 :                :     /*
                                198                 :                :      * To ensure dshash is created in "plain" shared memory, temporarily limit
                                199                 :                :      * size of dsa to the initial size of the dsa.
                                200                 :                :      */
                                201                 :           1283 :     dsa_set_size_limit(dsa, pgstat_dsa_init_size());
                                202                 :                : 
                                203                 :                :     /*
                                204                 :                :      * With the limit in place, create the dshash table. XXX: It'd be nice if
                                205                 :                :      * there were dshash_create_in_place().
                                206                 :                :      */
                                207                 :           1283 :     dsh = dshash_create(dsa, &dsh_params, NULL);
                                208                 :           1283 :     ctl->hash_handle = dshash_get_hash_table_handle(dsh);
                                209                 :                : 
                                210                 :                :     /* lift limit set above */
                                211                 :           1283 :     dsa_set_size_limit(dsa, -1);
                                212                 :                : 
                                213                 :                :     /*
                                214                 :                :      * Postmaster will never access these again, thus free the local
                                215                 :                :      * dsa/dshash references.
                                216                 :                :      */
                                217                 :           1283 :     dshash_detach(dsh);
                                218                 :           1283 :     dsa_detach(dsa);
                                219                 :                : 
                                220                 :           1283 :     pg_atomic_init_u64(&ctl->gc_request_count, 1);
                                221                 :                : 
                                222                 :                :     /* Do the per-kind initialization */
                                223         [ +  + ]:          42339 :     for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
                                224                 :                :     {
                                225                 :          41056 :         const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                                226                 :                :         char       *ptr;
                                227                 :                : 
                                228         [ +  + ]:          41056 :         if (!kind_info)
                                229                 :          23088 :             continue;
                                230                 :                : 
                                231                 :                :         /* initialize entry count tracking */
                                232         [ +  + ]:          17968 :         if (kind_info->track_entry_count)
                                233                 :              3 :             pg_atomic_init_u64(&ctl->entry_counts[kind - 1], 0);
                                234                 :                : 
                                235                 :                :         /* initialize fixed-numbered stats */
                                236         [ +  + ]:          17968 :         if (kind_info->fixed_amount)
                                237                 :                :         {
                                238         [ +  + ]:           8984 :             if (pgstat_is_kind_builtin(kind))
                                239                 :           8981 :                 ptr = ((char *) ctl) + kind_info->shared_ctl_off;
                                240                 :                :             else
                                241                 :                :             {
                                242                 :              3 :                 int         idx = kind - PGSTAT_KIND_CUSTOM_MIN;
                                243                 :                : 
                                244         [ -  + ]:              3 :                 Assert(kind_info->shared_size != 0);
  188 michael@paquier.xyz       245                 :              3 :                 ctl->custom_data[idx] = p;
                                246                 :              3 :                 p += MAXALIGN(kind_info->shared_size);
  189 heikki.linnakangas@i      247                 :              3 :                 ptr = ctl->custom_data[idx];
                                248                 :                :             }
                                249                 :                : 
                                250                 :           8984 :             kind_info->init_shmem_cb(ptr);
                                251                 :                :         }
                                252                 :                :     }
 1650 andres@anarazel.de        253                 :           1283 : }
                                254                 :                : 
                                255                 :                : void
                                256                 :          23947 : pgstat_attach_shmem(void)
                                257                 :                : {
                                258                 :                :     MemoryContext oldcontext;
                                259                 :                : 
                                260         [ -  + ]:          23947 :     Assert(pgStatLocal.dsa == NULL);
                                261                 :                : 
                                262                 :                :     /* stats shared memory persists for the backend lifetime */
                                263                 :          23947 :     oldcontext = MemoryContextSwitchTo(TopMemoryContext);
                                264                 :                : 
                                265                 :          23947 :     pgStatLocal.dsa = dsa_attach_in_place(pgStatLocal.shmem->raw_dsa_area,
                                266                 :                :                                           NULL);
                                267                 :          23947 :     dsa_pin_mapping(pgStatLocal.dsa);
                                268                 :                : 
                                269                 :          47894 :     pgStatLocal.shared_hash = dshash_attach(pgStatLocal.dsa, &dsh_params,
  493 nathan@postgresql.or      270                 :          23947 :                                             pgStatLocal.shmem->hash_handle,
                                271                 :                :                                             NULL);
                                272                 :                : 
 1650 andres@anarazel.de        273                 :          23947 :     MemoryContextSwitchTo(oldcontext);
                                274                 :          23947 : }
                                275                 :                : 
                                276                 :                : void
                                277                 :          23947 : pgstat_detach_shmem(void)
                                278                 :                : {
                                279         [ -  + ]:          23947 :     Assert(pgStatLocal.dsa);
                                280                 :                : 
                                281                 :                :     /* we shouldn't leave references to shared stats */
                                282                 :          23947 :     pgstat_release_all_entry_refs(false);
                                283                 :                : 
                                284                 :          23947 :     dshash_detach(pgStatLocal.shared_hash);
                                285                 :          23947 :     pgStatLocal.shared_hash = NULL;
                                286                 :                : 
                                287                 :          23947 :     dsa_detach(pgStatLocal.dsa);
                                288                 :                : 
                                289                 :                :     /*
                                290                 :                :      * dsa_detach() does not decrement the DSA reference count as no segment
                                291                 :                :      * was provided to dsa_attach_in_place(), causing no cleanup callbacks to
                                292                 :                :      * be registered.  Hence, release it manually now.
                                293                 :                :      */
  837 michael@paquier.xyz       294                 :          23947 :     dsa_release_in_place(pgStatLocal.shmem->raw_dsa_area);
                                295                 :                : 
 1650 andres@anarazel.de        296                 :          23947 :     pgStatLocal.dsa = NULL;
                                297                 :          23947 : }
                                298                 :                : 
                                299                 :                : 
                                300                 :                : /* ------------------------------------------------------------
                                301                 :                :  * Maintenance of shared memory stats entries
                                302                 :                :  * ------------------------------------------------------------
                                303                 :                :  */
                                304                 :                : 
                                305                 :                : /*
                                306                 :                :  * Allocate the DSA body for a new variable-numbered pgstats entry.
                                307                 :                :  *
                                308                 :                :  * Returns InvalidDsaPointer in the event of an allocation failure.
                                309                 :                :  */
                                310                 :                : dsa_pointer
   18 michael@paquier.xyz       311                 :GNC      378074 : pgstat_alloc_entry_body(PgStat_Kind kind)
                                312                 :                : {
                                313                 :         378074 :     const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                                314                 :                : 
                                315                 :         756148 :     return dsa_allocate_extended(pgStatLocal.dsa,
                                316                 :         378074 :                                  kind_info->shared_size,
                                317                 :                :                                  DSA_ALLOC_ZERO | DSA_ALLOC_NO_OOM);
                                318                 :                : }
                                319                 :                : 
                                320                 :                : /*
                                321                 :                :  * Initialize variable-numbered pgstats entry.
                                322                 :                :  *
                                323                 :                :  * "chunk" must be a valid pointer, allocated previously by
                                324                 :                :  * pgstat_alloc_entry_body().
                                325                 :                :  */
                                326                 :                : PgStatShared_Common *
 1650 andres@anarazel.de        327                 :CBC      378074 : pgstat_init_entry(PgStat_Kind kind,
                                328                 :                :                   PgStatShared_HashEntry *shhashent,
                                329                 :                :                   dsa_pointer chunk)
                                330                 :                : {
                                331                 :                :     PgStatShared_Common *shheader;
  378 michael@paquier.xyz       332                 :         378074 :     const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                                333                 :                : 
   18 michael@paquier.xyz       334         [ -  + ]:GNC      378074 :     Assert(DsaPointerIsValid(chunk));
                                335                 :                : 
                                336                 :                :     /*
                                337                 :                :      * Initialize refcount to 1, marking it as valid / not dropped. The entry
                                338                 :                :      * can't be freed before the initialization because it can't be found as
                                339                 :                :      * long as we hold the dshash partition lock. Caller needs to increase
                                340                 :                :      * further if a longer lived reference is needed.
                                341                 :                :      */
 1650 andres@anarazel.de        342                 :CBC      378074 :     pg_atomic_init_u32(&shhashent->refcount, 1);
                                343                 :                : 
                                344                 :                :     /*
                                345                 :                :      * Initialize "generation" to 0, as freshly created.
                                346                 :                :      */
  696 michael@paquier.xyz       347                 :         378074 :     pg_atomic_init_u32(&shhashent->generation, 0);
 1650 andres@anarazel.de        348                 :         378074 :     shhashent->dropped = false;
                                349                 :                : 
                                350                 :         378074 :     shheader = dsa_get_address(pgStatLocal.dsa, chunk);
                                351                 :         378074 :     shheader->magic = 0xdeadbeef;
                                352                 :                : 
                                353                 :                :     /* Link the new entry from the hash entry. */
                                354                 :         378074 :     shhashent->body = chunk;
                                355                 :                : 
                                356                 :                :     /* Increment entry count, if required. */
  378 michael@paquier.xyz       357         [ +  + ]:         378074 :     if (kind_info->track_entry_count)
                                358                 :              6 :         pg_atomic_fetch_add_u64(&pgStatLocal.shmem->entry_counts[kind - 1], 1);
                                359                 :                : 
 1650 andres@anarazel.de        360                 :         378074 :     LWLockInitialize(&shheader->lock, LWTRANCHE_PGSTATS_DATA);
                                361                 :                : 
                                362                 :         378074 :     return shheader;
                                363                 :                : }
                                364                 :                : 
                                365                 :                : static PgStatShared_Common *
                                366                 :             30 : pgstat_reinit_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
                                367                 :                : {
                                368                 :                :     PgStatShared_Common *shheader;
                                369                 :                : 
                                370                 :             30 :     shheader = dsa_get_address(pgStatLocal.dsa, shhashent->body);
                                371                 :                : 
                                372                 :                :     /* mark as not dropped anymore */
                                373                 :             30 :     pg_atomic_fetch_add_u32(&shhashent->refcount, 1);
                                374                 :                : 
                                375                 :                :     /*
                                376                 :                :      * Increment "generation", to let any backend with local references know
                                377                 :                :      * that what they point to is outdated.
                                378                 :                :      */
  696 michael@paquier.xyz       379                 :             30 :     pg_atomic_fetch_add_u32(&shhashent->generation, 1);
 1650 andres@anarazel.de        380                 :             30 :     shhashent->dropped = false;
                                381                 :                : 
                                382                 :                :     /* reinitialize content */
                                383         [ -  + ]:             30 :     Assert(shheader->magic == 0xdeadbeef);
                                384                 :             30 :     memset(pgstat_get_entry_data(kind, shheader), 0,
                                385                 :                :            pgstat_get_entry_len(kind));
                                386                 :                : 
                                387                 :             30 :     return shheader;
                                388                 :                : }
                                389                 :                : 
                                390                 :                : static void
                                391                 :        3938320 : pgstat_setup_shared_refs(void)
                                392                 :                : {
                                393         [ +  + ]:        3938320 :     if (likely(pgStatEntryRefHash != NULL))
                                394                 :        3917947 :         return;
                                395                 :                : 
                                396                 :          20373 :     pgStatEntryRefHash =
                                397                 :          20373 :         pgstat_entry_ref_hash_create(pgStatEntryRefHashContext,
                                398                 :                :                                      PGSTAT_ENTRY_REF_HASH_SIZE, NULL);
                                399                 :          20373 :     pgStatSharedRefAge = pg_atomic_read_u64(&pgStatLocal.shmem->gc_request_count);
                                400         [ -  + ]:          20373 :     Assert(pgStatSharedRefAge != 0);
                                401                 :                : }
                                402                 :                : 
                                403                 :                : /*
                                404                 :                :  * Helper function for pgstat_get_entry_ref().
                                405                 :                :  */
                                406                 :                : static void
                                407                 :         886319 : pgstat_acquire_entry_ref(PgStat_EntryRef *entry_ref,
                                408                 :                :                          PgStatShared_HashEntry *shhashent,
                                409                 :                :                          PgStatShared_Common *shheader)
                                410                 :                : {
                                411         [ -  + ]:         886319 :     Assert(shheader->magic == 0xdeadbeef);
                                412         [ -  + ]:         886319 :     Assert(pg_atomic_read_u32(&shhashent->refcount) > 0);
                                413                 :                : 
                                414                 :         886319 :     pg_atomic_fetch_add_u32(&shhashent->refcount, 1);
                                415                 :                : 
                                416                 :         886319 :     entry_ref->shared_stats = shheader;
                                417                 :         886319 :     entry_ref->shared_entry = shhashent;
  696 michael@paquier.xyz       418                 :         886319 :     entry_ref->generation = pg_atomic_read_u32(&shhashent->generation);
                                419                 :                : 
                                420                 :                :     /*
                                421                 :                :      * Complete the local reference before releasing the lock.  Releasing an
                                422                 :                :      * LWLock can process a pending interrupt, and callers may catch the
                                423                 :                :      * resulting error and continue using the backend-local cache.
                                424                 :                :      */
   66 michael@paquier.xyz       425                 :GNC      886319 :     dshash_release_lock(pgStatLocal.shared_hash, shhashent);
 1650 andres@anarazel.de        426                 :CBC      886319 : }
                                427                 :                : 
                                428                 :                : /*
                                429                 :                :  * Helper function for pgstat_get_entry_ref().
                                430                 :                :  */
                                431                 :                : static bool
                                432                 :        3938320 : pgstat_get_entry_ref_cached(PgStat_HashKey key, PgStat_EntryRef **entry_ref_p)
                                433                 :                : {
                                434                 :                :     bool        found;
                                435                 :                :     PgStat_EntryRefHashEntry *cache_entry;
                                436                 :                : 
                                437                 :                :     /*
                                438                 :                :      * We immediately insert a cache entry, because it avoids 1) multiple
                                439                 :                :      * hashtable lookups in case of a cache miss 2) having to deal with
                                440                 :                :      * out-of-memory errors after incrementing PgStatShared_Common->refcount.
                                441                 :                :      */
                                442                 :                : 
                                443                 :        3938320 :     cache_entry = pgstat_entry_ref_hash_insert(pgStatEntryRefHash, key, &found);
                                444                 :                : 
                                445   [ +  +  -  + ]:        3938320 :     if (!found || !cache_entry->entry_ref)
                                446                 :         994425 :     {
                                447                 :                :         PgStat_EntryRef *entry_ref;
                                448                 :                : 
   84 michael@paquier.xyz       449                 :GNC      994425 :         entry_ref = MemoryContextAllocExtended(pgStatSharedRefContext,
                                450                 :                :                                                sizeof(PgStat_EntryRef),
                                451                 :                :                                                MCXT_ALLOC_NO_OOM);
                                452         [ -  + ]:         994425 :         if (unlikely(entry_ref == NULL))
                                453                 :                :         {
                                454                 :                :             /*
                                455                 :                :              * Clean the hash entry to keep the table consistent in the
                                456                 :                :              * backend.
                                457                 :                :              */
   84 michael@paquier.xyz       458                 :UNC           0 :             pgstat_entry_ref_hash_delete(pgStatEntryRefHash, key);
                                459                 :                : 
                                460         [ #  # ]:              0 :             ereport(ERROR,
                                461                 :                :                     (errcode(ERRCODE_OUT_OF_MEMORY),
                                462                 :                :                      errmsg("out of memory")));
                                463                 :                :         }
                                464                 :                : 
   84 michael@paquier.xyz       465                 :GNC      994425 :         cache_entry->entry_ref = entry_ref;
 1650 andres@anarazel.de        466                 :CBC      994425 :         entry_ref->shared_stats = NULL;
                                467                 :         994425 :         entry_ref->shared_entry = NULL;
                                468                 :         994425 :         entry_ref->pending = NULL;
                                469                 :                : 
                                470                 :         994425 :         found = false;
                                471                 :                :     }
                                472         [ -  + ]:        2943895 :     else if (cache_entry->entry_ref->shared_stats == NULL)
                                473                 :                :     {
 1650 andres@anarazel.de        474         [ #  # ]:UBC           0 :         Assert(cache_entry->entry_ref->pending == NULL);
                                475                 :              0 :         found = false;
                                476                 :                :     }
                                477                 :                :     else
                                478                 :                :     {
                                479                 :                :         PgStat_EntryRef *entry_ref PG_USED_FOR_ASSERTS_ONLY;
                                480                 :                : 
 1650 andres@anarazel.de        481                 :CBC     2943895 :         entry_ref = cache_entry->entry_ref;
                                482         [ -  + ]:        2943895 :         Assert(entry_ref->shared_entry != NULL);
                                483         [ -  + ]:        2943895 :         Assert(entry_ref->shared_stats != NULL);
                                484                 :                : 
                                485         [ -  + ]:        2943895 :         Assert(entry_ref->shared_stats->magic == 0xdeadbeef);
                                486                 :                :         /* should have at least our reference */
                                487         [ -  + ]:        2943895 :         Assert(pg_atomic_read_u32(&entry_ref->shared_entry->refcount) > 0);
                                488                 :                :     }
                                489                 :                : 
                                490                 :        3938320 :     *entry_ref_p = cache_entry->entry_ref;
                                491                 :        3938320 :     return found;
                                492                 :                : }
                                493                 :                : 
                                494                 :                : /*
                                495                 :                :  * Get a shared stats reference. If create is true, the shared stats object is
                                496                 :                :  * created if it does not exist.
                                497                 :                :  *
                                498                 :                :  * When create is true, and created_entry is non-NULL, it'll be set to true
                                499                 :                :  * if the entry is newly created, false otherwise.
                                500                 :                :  */
                                501                 :                : PgStat_EntryRef *
  754 michael@paquier.xyz       502                 :        3938320 : pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create,
                                503                 :                :                      bool *created_entry)
                                504                 :                : {
  388                           505                 :        3938320 :     PgStat_HashKey key = {0};
                                506                 :                :     PgStatShared_HashEntry *shhashent;
 1650 andres@anarazel.de        507                 :        3938320 :     PgStatShared_Common *shheader = NULL;
                                508                 :                :     PgStat_EntryRef *entry_ref;
                                509                 :                : 
  706 michael@paquier.xyz       510                 :        3938320 :     key.kind = kind;
                                511                 :        3938320 :     key.dboid = dboid;
                                512                 :        3938320 :     key.objid = objid;
                                513                 :                : 
                                514                 :                :     /*
                                515                 :                :      * passing in created_entry only makes sense if we possibly could create
                                516                 :                :      * entry.
                                517                 :                :      */
 1445 peter@eisentraut.org      518   [ +  +  -  + ]:        3938320 :     Assert(create || created_entry == NULL);
 1650 andres@anarazel.de        519                 :        3938320 :     pgstat_assert_is_up();
                                520         [ -  + ]:        3938320 :     Assert(pgStatLocal.shared_hash != NULL);
                                521         [ -  + ]:        3938320 :     Assert(!pgStatLocal.shmem->is_shutdown);
                                522                 :                : 
                                523                 :        3938320 :     pgstat_setup_memcxt();
                                524                 :        3938320 :     pgstat_setup_shared_refs();
                                525                 :                : 
                                526         [ +  + ]:        3938320 :     if (created_entry != NULL)
                                527                 :            116 :         *created_entry = false;
                                528                 :                : 
                                529                 :                :     /*
                                530                 :                :      * Check if other backends dropped stats that could not be deleted because
                                531                 :                :      * somebody held references to it. If so, check this backend's references.
                                532                 :                :      * This is not expected to happen often. The location of the check is a
                                533                 :                :      * bit random, but this is a relatively frequently called path, so better
                                534                 :                :      * than most.
                                535                 :                :      */
                                536         [ +  + ]:        3938320 :     if (pgstat_need_entry_refs_gc())
                                537                 :           7259 :         pgstat_gc_entry_refs();
                                538                 :                : 
                                539                 :                :     /*
                                540                 :                :      * First check the lookup cache hashtable in local memory. If we find a
                                541                 :                :      * match here we can avoid taking locks / causing contention.
                                542                 :                :      */
                                543         [ +  + ]:        3938320 :     if (pgstat_get_entry_ref_cached(key, &entry_ref))
                                544                 :        2943895 :         return entry_ref;
                                545                 :                : 
                                546         [ -  + ]:         994425 :     Assert(entry_ref != NULL);
                                547                 :                : 
                                548                 :                :     /*
                                549                 :                :      * Do a lookup in the hash table first - it's quite likely that the entry
                                550                 :                :      * already exists, and that way we only need a shared lock.
                                551                 :                :      */
                                552                 :         994425 :     shhashent = dshash_find(pgStatLocal.shared_hash, &key, false);
                                553                 :                : 
                                554   [ +  +  +  + ]:         994425 :     if (create && !shhashent)
                                555                 :                :     {
                                556                 :                :         bool        shfound;
                                557                 :                :         dsa_pointer chunk;
                                558                 :                : 
                                559                 :                :         /* Allocate the stats body before inserting a hash entry. */
   18 michael@paquier.xyz       560                 :GNC      142384 :         chunk = pgstat_alloc_entry_body(kind);
                                561         [ -  + ]:         142384 :         if (chunk == InvalidDsaPointer)
                                562                 :                :         {
   18 michael@paquier.xyz       563                 :UNC           0 :             pgstat_release_entry_ref(key, entry_ref, false);
                                564         [ #  # ]:              0 :             ereport(ERROR,
                                565                 :                :                     (errcode(ERRCODE_OUT_OF_MEMORY),
                                566                 :                :                      errmsg("out of memory"),
                                567                 :                :                      errdetail("Failed while allocating entry %u/%u/%" PRIu64 ".",
                                568                 :                :                                key.kind, key.dboid, key.objid)));
                                569                 :                :         }
                                570                 :                : 
                                571                 :                :         /*
                                572                 :                :          * It's possible that somebody created the entry since the above
                                573                 :                :          * lookup. If so, fall through to the same path as if we'd have if it
                                574                 :                :          * already had been created before the dshash_find() calls.
                                575                 :                :          */
   63 michael@paquier.xyz       576                 :GNC      142384 :         shhashent = dshash_find_or_insert_extended(pgStatLocal.shared_hash,
                                577                 :                :                                                    &key, &shfound,
                                578                 :                :                                                    DSHASH_INSERT_NO_OOM);
                                579         [ -  + ]:         142384 :         if (!shhashent)
                                580                 :                :         {
   18 michael@paquier.xyz       581                 :UNC           0 :             dsa_free(pgStatLocal.dsa, chunk);
                                582                 :                : 
                                583                 :                :             /*
                                584                 :                :              * Clean up the local reference when failing insert into the
                                585                 :                :              * shared hashtable.
                                586                 :                :              */
   63                           587                 :              0 :             pgstat_release_entry_ref(key, entry_ref, false);
                                588         [ #  # ]:              0 :             ereport(ERROR,
                                589                 :                :                     (errcode(ERRCODE_OUT_OF_MEMORY),
                                590                 :                :                      errmsg("out of memory"),
                                591                 :                :                      errdetail("Failed while inserting entry %u/%u/%" PRIu64 ".",
                                592                 :                :                                key.kind, key.dboid, key.objid)));
                                593                 :                :         }
                                594                 :                : 
 1650 andres@anarazel.de        595         [ +  - ]:CBC      142384 :         if (!shfound)
                                596                 :                :         {
   18 michael@paquier.xyz       597                 :GNC      142384 :             shheader = pgstat_init_entry(kind, shhashent, chunk);
 1650 andres@anarazel.de        598                 :CBC      142384 :             pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
                                599                 :                : 
                                600         [ +  + ]:         142384 :             if (created_entry != NULL)
                                601                 :             51 :                 *created_entry = true;
                                602                 :                : 
                                603                 :         142384 :             return entry_ref;
                                604                 :                :         }
                                605                 :                : 
                                606                 :                :         /* Concurrent insert won; drop the unused body. */
   18 michael@paquier.xyz       607                 :UNC           0 :         dsa_free(pgStatLocal.dsa, chunk);
                                608                 :                :     }
                                609                 :                : 
 1650 andres@anarazel.de        610         [ +  + ]:CBC      852041 :     if (!shhashent)
                                611                 :                :     {
                                612                 :                :         /*
                                613                 :                :          * If we're not creating, delete the reference again. In all
                                614                 :                :          * likelihood it's just a stats lookup - no point wasting memory for a
                                615                 :                :          * shared ref to nothing...
                                616                 :                :          */
                                617                 :         108069 :         pgstat_release_entry_ref(key, entry_ref, false);
                                618                 :                : 
                                619                 :         108069 :         return NULL;
                                620                 :                :     }
                                621                 :                :     else
                                622                 :                :     {
                                623                 :                :         /*
                                624                 :                :          * Can get here either because dshash_find() found a match, or if
                                625                 :                :          * dshash_find_or_insert() found a concurrently inserted entry.
                                626                 :                :          */
                                627                 :                : 
                                628   [ +  +  +  + ]:         743972 :         if (shhashent->dropped && create)
                                629                 :                :         {
                                630                 :                :             /*
                                631                 :                :              * There are legitimate cases where the old stats entry might not
                                632                 :                :              * yet have been dropped by the time it's reused. The most obvious
                                633                 :                :              * case are replication slot stats, where a new slot can be
                                634                 :                :              * created with the same index just after dropping. But oid
                                635                 :                :              * wraparound can lead to other cases as well. We just reset the
                                636                 :                :              * stats to their plain state, while incrementing its "generation"
                                637                 :                :              * in the shared entry for any remaining local references.
                                638                 :                :              */
                                639                 :             30 :             shheader = pgstat_reinit_entry(kind, shhashent);
                                640                 :             30 :             pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
                                641                 :                : 
                                642         [ -  + ]:             30 :             if (created_entry != NULL)
 1650 andres@anarazel.de        643                 :UBC           0 :                 *created_entry = true;
                                644                 :                : 
 1650 andres@anarazel.de        645                 :CBC          30 :             return entry_ref;
                                646                 :                :         }
                                647         [ +  + ]:         743942 :         else if (shhashent->dropped)
                                648                 :                :         {
                                649                 :             37 :             dshash_release_lock(pgStatLocal.shared_hash, shhashent);
                                650                 :             37 :             pgstat_release_entry_ref(key, entry_ref, false);
                                651                 :                : 
                                652                 :             37 :             return NULL;
                                653                 :                :         }
                                654                 :                :         else
                                655                 :                :         {
                                656                 :         743905 :             shheader = dsa_get_address(pgStatLocal.dsa, shhashent->body);
                                657                 :         743905 :             pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
                                658                 :                : 
                                659                 :         743905 :             return entry_ref;
                                660                 :                :         }
                                661                 :                :     }
                                662                 :                : }
                                663                 :                : 
                                664                 :                : static void
                                665                 :         994425 : pgstat_release_entry_ref(PgStat_HashKey key, PgStat_EntryRef *entry_ref,
                                666                 :                :                          bool discard_pending)
                                667                 :                : {
                                668   [ +  -  +  + ]:         994425 :     if (entry_ref && entry_ref->pending)
                                669                 :                :     {
                                670         [ +  - ]:          44025 :         if (discard_pending)
                                671                 :          44025 :             pgstat_delete_pending_entry(entry_ref);
                                672                 :                :         else
 1650 andres@anarazel.de        673         [ #  # ]:UBC           0 :             elog(ERROR, "releasing ref with pending data");
                                674                 :                :     }
                                675                 :                : 
 1650 andres@anarazel.de        676   [ +  -  +  + ]:CBC      994425 :     if (entry_ref && entry_ref->shared_stats)
                                677                 :                :     {
                                678         [ -  + ]:         886319 :         Assert(entry_ref->shared_stats->magic == 0xdeadbeef);
                                679         [ -  + ]:         886319 :         Assert(entry_ref->pending == NULL);
                                680                 :                : 
                                681                 :                :         /*
                                682                 :                :          * This can't race with another backend looking up the stats entry and
                                683                 :                :          * increasing the refcount because it is not "legal" to create
                                684                 :                :          * additional references to dropped entries.
                                685                 :                :          */
                                686         [ +  + ]:         886319 :         if (pg_atomic_fetch_sub_u32(&entry_ref->shared_entry->refcount, 1) == 1)
                                687                 :                :         {
                                688                 :                :             PgStatShared_HashEntry *shent;
                                689                 :                : 
                                690                 :                :             /*
                                691                 :                :              * We're the last referrer to this entry, try to drop the shared
                                692                 :                :              * entry.
                                693                 :                :              */
                                694                 :                : 
                                695                 :                :             /* only dropped entries can reach a 0 refcount */
                                696         [ -  + ]:           5255 :             Assert(entry_ref->shared_entry->dropped);
                                697                 :                : 
                                698                 :           5255 :             shent = dshash_find(pgStatLocal.shared_hash,
                                699                 :           5255 :                                 &entry_ref->shared_entry->key,
                                700                 :                :                                 true);
                                701         [ -  + ]:           5255 :             if (!shent)
 1650 andres@anarazel.de        702         [ #  # ]:UBC           0 :                 elog(ERROR, "could not find just referenced shared stats entry");
                                703                 :                : 
                                704                 :                :             /*
                                705                 :                :              * This entry may have been reinitialized while trying to release
                                706                 :                :              * it, so double-check that it has not been reused while holding a
                                707                 :                :              * lock on its shared entry.
                                708                 :                :              */
  696 michael@paquier.xyz       709                 :CBC        5255 :             if (pg_atomic_read_u32(&entry_ref->shared_entry->generation) ==
                                710         [ +  - ]:           5255 :                 entry_ref->generation)
                                711                 :                :             {
                                712                 :                :                 /* Same "generation", so we're OK with the removal */
                                713         [ -  + ]:           5255 :                 Assert(pg_atomic_read_u32(&entry_ref->shared_entry->refcount) == 0);
                                714         [ -  + ]:           5255 :                 Assert(entry_ref->shared_entry == shent);
                                715                 :           5255 :                 pgstat_free_entry(shent, NULL);
                                716                 :                :             }
                                717                 :                :             else
                                718                 :                :             {
                                719                 :                :                 /*
                                720                 :                :                  * Shared stats entry has been reinitialized, so do not drop
                                721                 :                :                  * its shared entry, only release its lock.
                                722                 :                :                  */
  696 michael@paquier.xyz       723                 :UBC           0 :                 dshash_release_lock(pgStatLocal.shared_hash, shent);
                                724                 :                :             }
                                725                 :                :         }
                                726                 :                :     }
                                727                 :                : 
 1650 andres@anarazel.de        728         [ -  + ]:CBC      994425 :     if (!pgstat_entry_ref_hash_delete(pgStatEntryRefHash, key))
 1650 andres@anarazel.de        729         [ #  # ]:UBC           0 :         elog(ERROR, "entry ref vanished before deletion");
                                730                 :                : 
 1650 andres@anarazel.de        731         [ +  - ]:CBC      994425 :     if (entry_ref)
                                732                 :         994425 :         pfree(entry_ref);
                                733                 :         994425 : }
                                734                 :                : 
                                735                 :                : /*
                                736                 :                :  * Acquire exclusive lock on the entry.
                                737                 :                :  *
                                738                 :                :  * If nowait is true, it's just a conditional acquire, and the result
                                739                 :                :  * *must* be checked to verify success.
                                740                 :                :  * If nowait is false, waits as necessary, always returning true.
                                741                 :                :  */
                                742                 :                : bool
                                743                 :        1252998 : pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
                                744                 :                : {
                                745                 :        1252998 :     LWLock     *lock = &entry_ref->shared_stats->lock;
                                746                 :                : 
                                747         [ +  + ]:        1252998 :     if (nowait)
                                748                 :         564326 :         return LWLockConditionalAcquire(lock, LW_EXCLUSIVE);
                                749                 :                : 
                                750                 :         688672 :     LWLockAcquire(lock, LW_EXCLUSIVE);
                                751                 :         688672 :     return true;
                                752                 :                : }
                                753                 :                : 
                                754                 :                : /*
                                755                 :                :  * Acquire shared lock on the entry.
                                756                 :                :  *
                                757                 :                :  * Separate from pgstat_lock_entry() as most callers will need to lock
                                758                 :                :  * exclusively.  The wait semantics are identical.
                                759                 :                :  */
                                760                 :                : bool
 1512                           761                 :          28568 : pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
                                762                 :                : {
                                763                 :          28568 :     LWLock     *lock = &entry_ref->shared_stats->lock;
                                764                 :                : 
                                765         [ -  + ]:          28568 :     if (nowait)
 1512 andres@anarazel.de        766                 :UBC           0 :         return LWLockConditionalAcquire(lock, LW_SHARED);
                                767                 :                : 
 1512 andres@anarazel.de        768                 :CBC       28568 :     LWLockAcquire(lock, LW_SHARED);
                                769                 :          28568 :     return true;
                                770                 :                : }
                                771                 :                : 
                                772                 :                : void
 1650                           773                 :        1281564 : pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
                                774                 :                : {
                                775                 :        1281564 :     LWLockRelease(&entry_ref->shared_stats->lock);
                                776                 :        1281564 : }
                                777                 :                : 
                                778                 :                : /*
                                779                 :                :  * Helper function to fetch and lock shared stats.
                                780                 :                :  */
                                781                 :                : PgStat_EntryRef *
  754 michael@paquier.xyz       782                 :         113515 : pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid,
                                783                 :                :                             bool nowait)
                                784                 :                : {
                                785                 :                :     PgStat_EntryRef *entry_ref;
                                786                 :                : 
                                787                 :                :     /* find shared table stats entry corresponding to the local entry */
                                788                 :         113515 :     entry_ref = pgstat_get_entry_ref(kind, dboid, objid, true, NULL);
                                789                 :                : 
                                790                 :                :     /* lock the shared entry to protect the content, skip if failed */
 1650 andres@anarazel.de        791         [ -  + ]:         113515 :     if (!pgstat_lock_entry(entry_ref, nowait))
 1650 andres@anarazel.de        792                 :UBC           0 :         return NULL;
                                793                 :                : 
 1650 andres@anarazel.de        794                 :CBC      113515 :     return entry_ref;
                                795                 :                : }
                                796                 :                : 
                                797                 :                : void
                                798                 :           2069 : pgstat_request_entry_refs_gc(void)
                                799                 :                : {
                                800                 :           2069 :     pg_atomic_fetch_add_u64(&pgStatLocal.shmem->gc_request_count, 1);
                                801                 :           2069 : }
                                802                 :                : 
                                803                 :                : static bool
                                804                 :        3938320 : pgstat_need_entry_refs_gc(void)
                                805                 :                : {
                                806                 :                :     uint64      curage;
                                807                 :                : 
                                808         [ -  + ]:        3938320 :     if (!pgStatEntryRefHash)
 1650 andres@anarazel.de        809                 :UBC           0 :         return false;
                                810                 :                : 
                                811                 :                :     /* should have been initialized when creating pgStatEntryRefHash */
 1650 andres@anarazel.de        812         [ -  + ]:CBC     3938320 :     Assert(pgStatSharedRefAge != 0);
                                813                 :                : 
                                814                 :        3938320 :     curage = pg_atomic_read_u64(&pgStatLocal.shmem->gc_request_count);
                                815                 :                : 
                                816                 :        3938320 :     return pgStatSharedRefAge != curage;
                                817                 :                : }
                                818                 :                : 
                                819                 :                : static void
                                820                 :           7259 : pgstat_gc_entry_refs(void)
                                821                 :                : {
                                822                 :                :     pgstat_entry_ref_hash_iterator i;
                                823                 :                :     PgStat_EntryRefHashEntry *ent;
                                824                 :                :     uint64      curage;
                                825                 :                : 
                                826                 :           7259 :     curage = pg_atomic_read_u64(&pgStatLocal.shmem->gc_request_count);
                                827         [ -  + ]:           7259 :     Assert(curage != 0);
                                828                 :                : 
                                829                 :                :     /*
                                830                 :                :      * Some entries have been dropped or reinitialized.  Invalidate cache
                                831                 :                :      * pointer to them.
                                832                 :                :      */
                                833                 :           7259 :     pgstat_entry_ref_hash_start_iterate(pgStatEntryRefHash, &i);
                                834         [ +  + ]:         457544 :     while ((ent = pgstat_entry_ref_hash_iterate(pgStatEntryRefHash, &i)) != NULL)
                                835                 :                :     {
                                836                 :         450285 :         PgStat_EntryRef *entry_ref = ent->entry_ref;
                                837                 :                : 
                                838   [ +  -  -  + ]:         450285 :         Assert(!entry_ref->shared_stats ||
                                839                 :                :                entry_ref->shared_stats->magic == 0xdeadbeef);
                                840                 :                : 
                                841                 :                :         /* A NULL shared_entry marks a partial reference. */
   25 michael@paquier.xyz       842         [ -  + ]:         450285 :         if (entry_ref->shared_entry == NULL)
                                843                 :                :         {
   25 michael@paquier.xyz       844         [ #  # ]:UBC           0 :             Assert(entry_ref->shared_stats == NULL);
                                845         [ #  # ]:              0 :             Assert(entry_ref->pending == NULL);
                                846                 :              0 :             pgstat_release_entry_ref(ent->key, entry_ref, false);
                                847                 :              0 :             continue;
                                848                 :                :         }
                                849                 :                : 
                                850                 :                :         /*
                                851                 :                :          * "generation" checks for the case of entries being reinitialized,
                                852                 :                :          * and "dropped" for the case where these are..  dropped.
                                853                 :                :          */
  672 michael@paquier.xyz       854         [ +  + ]:CBC      450285 :         if (!entry_ref->shared_entry->dropped &&
                                855                 :         404774 :             pg_atomic_read_u32(&entry_ref->shared_entry->generation) ==
                                856         [ +  + ]:         404774 :             entry_ref->generation)
 1650 andres@anarazel.de        857                 :         404744 :             continue;
                                858                 :                : 
                                859                 :                :         /* cannot gc shared ref that has pending data */
                                860         [ +  + ]:          45541 :         if (entry_ref->pending != NULL)
                                861                 :          38029 :             continue;
                                862                 :                : 
                                863                 :           7512 :         pgstat_release_entry_ref(ent->key, entry_ref, false);
                                864                 :                :     }
                                865                 :                : 
                                866                 :           7259 :     pgStatSharedRefAge = curage;
                                867                 :           7259 : }
                                868                 :                : 
                                869                 :                : static void
                                870                 :          20420 : pgstat_release_matching_entry_refs(bool discard_pending, ReleaseMatchCB match,
                                871                 :                :                                    Datum match_data)
                                872                 :                : {
                                873                 :                :     pgstat_entry_ref_hash_iterator i;
                                874                 :                :     PgStat_EntryRefHashEntry *ent;
                                875                 :                : 
                                876         [ +  + ]:          20420 :     if (pgStatEntryRefHash == NULL)
                                877                 :              1 :         return;
                                878                 :                : 
                                879                 :          20419 :     pgstat_entry_ref_hash_start_iterate(pgStatEntryRefHash, &i);
                                880                 :                : 
                                881                 :         836282 :     while ((ent = pgstat_entry_ref_hash_iterate(pgStatEntryRefHash, &i))
                                882         [ +  + ]:         836282 :            != NULL)
                                883                 :                :     {
                                884         [ -  + ]:         815863 :         Assert(ent->entry_ref != NULL);
                                885                 :                : 
                                886   [ +  +  +  - ]:         815863 :         if (match && !match(ent, match_data))
                                887                 :           1404 :             continue;
                                888                 :                : 
                                889                 :         814459 :         pgstat_release_entry_ref(ent->key, ent->entry_ref, discard_pending);
                                890                 :                :     }
                                891                 :                : }
                                892                 :                : 
                                893                 :                : /*
                                894                 :                :  * Release all local references to shared stats entries.
                                895                 :                :  *
                                896                 :                :  * When a process exits it cannot do so while still holding references onto
                                897                 :                :  * stats entries, otherwise the shared stats entries could never be freed.
                                898                 :                :  */
                                899                 :                : static void
                                900                 :          23947 : pgstat_release_all_entry_refs(bool discard_pending)
                                901                 :                : {
                                902         [ +  + ]:          23947 :     if (pgStatEntryRefHash == NULL)
                                903                 :           3574 :         return;
                                904                 :                : 
                                905                 :          20373 :     pgstat_release_matching_entry_refs(discard_pending, NULL, 0);
                                906         [ -  + ]:          20373 :     Assert(pgStatEntryRefHash->members == 0);
                                907                 :          20373 :     pgstat_entry_ref_hash_destroy(pgStatEntryRefHash);
                                908                 :          20373 :     pgStatEntryRefHash = NULL;
                                909                 :                : }
                                910                 :                : 
                                911                 :                : static bool
                                912                 :           1404 : match_db(PgStat_EntryRefHashEntry *ent, Datum match_data)
                                913                 :                : {
                                914                 :           1404 :     Oid         dboid = DatumGetObjectId(match_data);
                                915                 :                : 
                                916                 :           1404 :     return ent->key.dboid == dboid;
                                917                 :                : }
                                918                 :                : 
                                919                 :                : static void
                                920                 :             47 : pgstat_release_db_entry_refs(Oid dboid)
                                921                 :                : {
                                922                 :             47 :     pgstat_release_matching_entry_refs( /* discard pending = */ true,
                                923                 :                :                                        match_db,
                                924                 :                :                                        ObjectIdGetDatum(dboid));
                                925                 :             47 : }
                                926                 :                : 
                                927                 :                : 
                                928                 :                : /* ------------------------------------------------------------
                                929                 :                :  * Dropping and resetting of stats entries
                                930                 :                :  * ------------------------------------------------------------
                                931                 :                :  */
                                932                 :                : 
                                933                 :                : static void
                                934                 :          70208 : pgstat_free_entry(PgStatShared_HashEntry *shent, dshash_seq_status *hstat)
                                935                 :                : {
                                936                 :                :     dsa_pointer pdsa;
  378 michael@paquier.xyz       937                 :          70208 :     PgStat_Kind kind = shent->key.kind;
                                938                 :                : 
                                939                 :                :     /*
                                940                 :                :      * Fetch dsa pointer before deleting entry - that way we can free the
                                941                 :                :      * memory after releasing the lock.
                                942                 :                :      */
 1650 andres@anarazel.de        943                 :          70208 :     pdsa = shent->body;
                                944                 :                : 
                                945         [ +  + ]:          70208 :     if (!hstat)
                                946                 :          64484 :         dshash_delete_entry(pgStatLocal.shared_hash, shent);
                                947                 :                :     else
                                948                 :           5724 :         dshash_delete_current(hstat);
                                949                 :                : 
                                950                 :          70208 :     dsa_free(pgStatLocal.dsa, pdsa);
                                951                 :                : 
                                952                 :                :     /* Decrement entry count, if required. */
  378 michael@paquier.xyz       953         [ +  + ]:          70208 :     if (pgstat_get_kind_info(kind)->track_entry_count)
                                954                 :              2 :         pg_atomic_sub_fetch_u64(&pgStatLocal.shmem->entry_counts[kind - 1], 1);
 1650 andres@anarazel.de        955                 :          70208 : }
                                956                 :                : 
                                957                 :                : /*
                                958                 :                :  * Helper for both pgstat_drop_database_and_contents() and
                                959                 :                :  * pgstat_drop_entry(). If hstat is non-null delete the shared entry using
                                960                 :                :  * dshash_delete_current(), otherwise use dshash_delete_entry(). In either
                                961                 :                :  * case the entry needs to be already locked.
                                962                 :                :  */
                                963                 :                : static bool
                                964                 :          70238 : pgstat_drop_entry_internal(PgStatShared_HashEntry *shent,
                                965                 :                :                            dshash_seq_status *hstat)
                                966                 :                : {
                                967         [ -  + ]:          70238 :     Assert(shent->body != InvalidDsaPointer);
                                968                 :                : 
                                969                 :                :     /* should already have released local reference */
                                970         [ +  + ]:          70238 :     if (pgStatEntryRefHash)
                                971         [ -  + ]:          70162 :         Assert(!pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, shent->key));
                                972                 :                : 
                                973                 :                :     /*
                                974                 :                :      * Signal that the entry is dropped - this will eventually cause other
                                975                 :                :      * backends to release their references.
                                976                 :                :      */
  116 michael@paquier.xyz       977         [ -  + ]:          70238 :     Assert(!shent->dropped);
 1650 andres@anarazel.de        978                 :          70238 :     shent->dropped = true;
                                979                 :                : 
                                980                 :                :     /* release refcount marking entry as not dropped */
                                981         [ +  + ]:          70238 :     if (pg_atomic_sub_fetch_u32(&shent->refcount, 1) == 0)
                                982                 :                :     {
                                983                 :          64953 :         pgstat_free_entry(shent, hstat);
                                984                 :          64953 :         return true;
                                985                 :                :     }
                                986                 :                :     else
                                987                 :                :     {
                                988         [ +  - ]:           5285 :         if (!hstat)
                                989                 :           5285 :             dshash_release_lock(pgStatLocal.shared_hash, shent);
                                990                 :           5285 :         return false;
                                991                 :                :     }
                                992                 :                : }
                                993                 :                : 
                                994                 :                : /*
                                995                 :                :  * Drop stats for the database and all the objects inside that database.
                                996                 :                :  */
                                997                 :                : static void
                                998                 :             47 : pgstat_drop_database_and_contents(Oid dboid)
                                999                 :                : {
                               1000                 :                :     dshash_seq_status hstat;
                               1001                 :                :     PgStatShared_HashEntry *p;
                               1002                 :             47 :     uint64      not_freed_count = 0;
                               1003                 :                : 
                               1004         [ -  + ]:             47 :     Assert(OidIsValid(dboid));
                               1005                 :                : 
                               1006         [ -  + ]:             47 :     Assert(pgStatLocal.shared_hash != NULL);
                               1007                 :                : 
                               1008                 :                :     /*
                               1009                 :                :      * This backend might very well be the only backend holding a reference to
                               1010                 :                :      * about-to-be-dropped entries. Ensure that we're not preventing it from
                               1011                 :                :      * being cleaned up till later.
                               1012                 :                :      *
                               1013                 :                :      * Doing this separately from the dshash iteration below avoids having to
                               1014                 :                :      * do so while holding a partition lock on the shared hashtable.
                               1015                 :                :      */
                               1016                 :             47 :     pgstat_release_db_entry_refs(dboid);
                               1017                 :                : 
                               1018                 :                :     /* some of the dshash entries are to be removed, take exclusive lock. */
                               1019                 :             47 :     dshash_seq_init(&hstat, pgStatLocal.shared_hash, true);
                               1020         [ +  + ]:          18939 :     while ((p = dshash_seq_next(&hstat)) != NULL)
                               1021                 :                :     {
                               1022         [ +  + ]:          18892 :         if (p->dropped)
                               1023                 :              1 :             continue;
                               1024                 :                : 
                               1025         [ +  + ]:          18891 :         if (p->key.dboid != dboid)
                               1026                 :          13223 :             continue;
                               1027                 :                : 
                               1028         [ -  + ]:           5668 :         if (!pgstat_drop_entry_internal(p, &hstat))
                               1029                 :                :         {
                               1030                 :                :             /*
                               1031                 :                :              * Even statistics for a dropped database might currently be
                               1032                 :                :              * accessed (consider e.g. database stats for pg_stat_database).
                               1033                 :                :              */
 1650 andres@anarazel.de       1034                 :UBC           0 :             not_freed_count++;
                               1035                 :                :         }
                               1036                 :                :     }
 1650 andres@anarazel.de       1037                 :CBC          47 :     dshash_seq_term(&hstat);
                               1038                 :                : 
                               1039                 :                :     /*
                               1040                 :                :      * If some of the stats data could not be freed, signal the reference
                               1041                 :                :      * holders to run garbage collection of their cached pgStatLocal.shmem.
                               1042                 :                :      */
                               1043         [ -  + ]:             47 :     if (not_freed_count > 0)
 1650 andres@anarazel.de       1044                 :UBC           0 :         pgstat_request_entry_refs_gc();
 1650 andres@anarazel.de       1045                 :CBC          47 : }
                               1046                 :                : 
                               1047                 :                : /*
                               1048                 :                :  * Drop a single stats entry.
                               1049                 :                :  *
                               1050                 :                :  * This routine returns false if the stats entry of the dropped object could
                               1051                 :                :  * not be freed, true otherwise.
                               1052                 :                :  *
                               1053                 :                :  * If missing_ok is true, skip entries that have been concurrently dropped.
                               1054                 :                :  *
                               1055                 :                :  * The callers of this function should call pgstat_request_entry_refs_gc()
                               1056                 :                :  * if the stats entry could not be freed, to ensure that this entry's memory
                               1057                 :                :  * can be reclaimed later by a different backend calling
                               1058                 :                :  * pgstat_gc_entry_refs().
                               1059                 :                :  */
                               1060                 :                : bool
  116 michael@paquier.xyz      1061                 :          91451 : pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid,
                               1062                 :                :                   bool missing_ok)
                               1063                 :                : {
  388                          1064                 :          91451 :     PgStat_HashKey key = {0};
                               1065                 :                :     PgStatShared_HashEntry *shent;
 1650 andres@anarazel.de       1066                 :          91451 :     bool        freed = true;
                               1067                 :                : 
  706 michael@paquier.xyz      1068                 :          91451 :     key.kind = kind;
                               1069                 :          91451 :     key.dboid = dboid;
                               1070                 :          91451 :     key.objid = objid;
                               1071                 :                : 
                               1072                 :                :     /* delete local reference */
 1650 andres@anarazel.de       1073         [ +  + ]:          91451 :     if (pgStatEntryRefHash)
                               1074                 :                :     {
                               1075                 :                :         PgStat_EntryRefHashEntry *lohashent =
 1242 tgl@sss.pgh.pa.us        1076                 :          87870 :             pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, key);
                               1077                 :                : 
 1650 andres@anarazel.de       1078         [ +  + ]:          87870 :         if (lohashent)
                               1079                 :          64348 :             pgstat_release_entry_ref(lohashent->key, lohashent->entry_ref,
                               1080                 :                :                                      true);
                               1081                 :                :     }
                               1082                 :                : 
                               1083                 :                :     /* mark entry in shared hashtable as deleted, drop if possible */
                               1084                 :          91451 :     shent = dshash_find(pgStatLocal.shared_hash, &key, true);
                               1085         [ +  + ]:          91451 :     if (shent)
                               1086                 :                :     {
  116 michael@paquier.xyz      1087         [ -  + ]:          64514 :         if (shent->dropped)
                               1088                 :                :         {
  116 michael@paquier.xyz      1089         [ #  # ]:UBC           0 :             if (!missing_ok)
                               1090         [ #  # ]:              0 :                 elog(ERROR,
                               1091                 :                :                      "trying to drop stats entry already dropped: kind=%s dboid=%u objid=%" PRIu64 " refcount=%u generation=%u",
                               1092                 :                :                      pgstat_get_kind_info(shent->key.kind)->name,
                               1093                 :                :                      shent->key.dboid,
                               1094                 :                :                      shent->key.objid,
                               1095                 :                :                      pg_atomic_read_u32(&shent->refcount),
                               1096                 :                :                      pg_atomic_read_u32(&shent->generation));
                               1097                 :              0 :             dshash_release_lock(pgStatLocal.shared_hash, shent);
                               1098                 :              0 :             return true;
                               1099                 :                :         }
                               1100                 :                : 
 1650 andres@anarazel.de       1101                 :CBC       64514 :         freed = pgstat_drop_entry_internal(shent, NULL);
                               1102                 :                : 
                               1103                 :                :         /*
                               1104                 :                :          * Database stats contain other stats. Drop those as well when
                               1105                 :                :          * dropping the database. XXX: Perhaps this should be done in a
                               1106                 :                :          * slightly more principled way? But not obvious what that'd look
                               1107                 :                :          * like, and so far this is the only case...
                               1108                 :                :          */
                               1109         [ +  + ]:          64514 :         if (key.kind == PGSTAT_KIND_DATABASE)
                               1110                 :             47 :             pgstat_drop_database_and_contents(key.dboid);
                               1111                 :                :     }
                               1112                 :                : 
                               1113                 :          91451 :     return freed;
                               1114                 :                : }
                               1115                 :                : 
                               1116                 :                : /*
                               1117                 :                :  * Scan through the shared hashtable of stats, dropping statistics if
                               1118                 :                :  * approved by the optional do_drop() function.
                               1119                 :                :  */
                               1120                 :                : void
  619 michael@paquier.xyz      1121                 :            266 : pgstat_drop_matching_entries(bool (*do_drop) (PgStatShared_HashEntry *, Datum),
                               1122                 :                :                              Datum match_data)
                               1123                 :                : {
                               1124                 :                :     dshash_seq_status hstat;
                               1125                 :                :     PgStatShared_HashEntry *ps;
 1650 andres@anarazel.de       1126                 :            266 :     uint64      not_freed_count = 0;
                               1127                 :                : 
                               1128                 :                :     /* entries are removed, take an exclusive lock */
 1640                          1129                 :            266 :     dshash_seq_init(&hstat, pgStatLocal.shared_hash, true);
 1650                          1130         [ +  + ]:            322 :     while ((ps = dshash_seq_next(&hstat)) != NULL)
                               1131                 :                :     {
                               1132         [ -  + ]:             56 :         if (ps->dropped)
 1650 andres@anarazel.de       1133                 :UBC           0 :             continue;
                               1134                 :                : 
  619 michael@paquier.xyz      1135   [ -  +  -  - ]:CBC          56 :         if (do_drop != NULL && !do_drop(ps, match_data))
  619 michael@paquier.xyz      1136                 :UBC           0 :             continue;
                               1137                 :                : 
                               1138                 :                :         /* delete local reference */
  619 michael@paquier.xyz      1139         [ -  + ]:CBC          56 :         if (pgStatEntryRefHash)
                               1140                 :                :         {
                               1141                 :                :             PgStat_EntryRefHashEntry *lohashent =
  619 michael@paquier.xyz      1142                 :UBC           0 :                 pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, ps->key);
                               1143                 :                : 
                               1144         [ #  # ]:              0 :             if (lohashent)
                               1145                 :              0 :                 pgstat_release_entry_ref(lohashent->key, lohashent->entry_ref,
                               1146                 :                :                                          true);
                               1147                 :                :         }
                               1148                 :                : 
 1650 andres@anarazel.de       1149         [ -  + ]:CBC          56 :         if (!pgstat_drop_entry_internal(ps, &hstat))
 1650 andres@anarazel.de       1150                 :UBC           0 :             not_freed_count++;
                               1151                 :                :     }
 1650 andres@anarazel.de       1152                 :CBC         266 :     dshash_seq_term(&hstat);
                               1153                 :                : 
                               1154         [ -  + ]:            266 :     if (not_freed_count > 0)
 1650 andres@anarazel.de       1155                 :UBC           0 :         pgstat_request_entry_refs_gc();
 1650 andres@anarazel.de       1156                 :CBC         266 : }
                               1157                 :                : 
                               1158                 :                : /*
                               1159                 :                :  * Scan through the shared hashtable of stats and drop all entries.
                               1160                 :                :  */
                               1161                 :                : void
  619 michael@paquier.xyz      1162                 :            266 : pgstat_drop_all_entries(void)
                               1163                 :                : {
                               1164                 :            266 :     pgstat_drop_matching_entries(NULL, 0);
                               1165                 :            266 : }
                               1166                 :                : 
                               1167                 :                : static void
 1650 andres@anarazel.de       1168                 :          11490 : shared_stat_reset_contents(PgStat_Kind kind, PgStatShared_Common *header,
                               1169                 :                :                            TimestampTz ts)
                               1170                 :                : {
                               1171                 :          11490 :     const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                               1172                 :                : 
                               1173                 :          11490 :     memset(pgstat_get_entry_data(kind, header), 0,
                               1174                 :                :            pgstat_get_entry_len(kind));
                               1175                 :                : 
                               1176         [ +  - ]:          11490 :     if (kind_info->reset_timestamp_cb)
                               1177                 :          11490 :         kind_info->reset_timestamp_cb(header, ts);
                               1178                 :          11490 : }
                               1179                 :                : 
                               1180                 :                : /*
                               1181                 :                :  * Reset one variable-numbered stats entry.
                               1182                 :                :  */
                               1183                 :                : void
  754 michael@paquier.xyz      1184                 :            262 : pgstat_reset_entry(PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts)
                               1185                 :                : {
                               1186                 :                :     PgStat_EntryRef *entry_ref;
                               1187                 :                : 
 1650 andres@anarazel.de       1188         [ -  + ]:            262 :     Assert(!pgstat_get_kind_info(kind)->fixed_amount);
                               1189                 :                : 
  754 michael@paquier.xyz      1190                 :            262 :     entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
 1650 andres@anarazel.de       1191   [ +  +  -  + ]:            262 :     if (!entry_ref || entry_ref->shared_entry->dropped)
                               1192                 :              1 :         return;
                               1193                 :                : 
 1645 tgl@sss.pgh.pa.us        1194                 :            261 :     (void) pgstat_lock_entry(entry_ref, false);
 1650 andres@anarazel.de       1195                 :            261 :     shared_stat_reset_contents(kind, entry_ref->shared_stats, ts);
                               1196                 :            261 :     pgstat_unlock_entry(entry_ref);
                               1197                 :                : }
                               1198                 :                : 
                               1199                 :                : /*
                               1200                 :                :  * Scan through the shared hashtable of stats, resetting statistics if
                               1201                 :                :  * approved by the provided do_reset() function.
                               1202                 :                :  */
                               1203                 :                : void
                               1204                 :             19 : pgstat_reset_matching_entries(bool (*do_reset) (PgStatShared_HashEntry *, Datum),
                               1205                 :                :                               Datum match_data, TimestampTz ts)
                               1206                 :                : {
                               1207                 :                :     dshash_seq_status hstat;
                               1208                 :                :     PgStatShared_HashEntry *p;
                               1209                 :                : 
                               1210                 :                :     /* dshash entry is not modified, take shared lock */
                               1211                 :             19 :     dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
                               1212         [ +  + ]:          16337 :     while ((p = dshash_seq_next(&hstat)) != NULL)
                               1213                 :                :     {
                               1214                 :                :         PgStatShared_Common *header;
                               1215                 :                : 
                               1216         [ +  + ]:          16318 :         if (p->dropped)
                               1217                 :              1 :             continue;
                               1218                 :                : 
                               1219         [ +  + ]:          16317 :         if (!do_reset(p, match_data))
                               1220                 :           5088 :             continue;
                               1221                 :                : 
                               1222                 :          11229 :         header = dsa_get_address(pgStatLocal.dsa, p->body);
                               1223                 :                : 
                               1224                 :          11229 :         LWLockAcquire(&header->lock, LW_EXCLUSIVE);
                               1225                 :                : 
                               1226                 :          11229 :         shared_stat_reset_contents(p->key.kind, header, ts);
                               1227                 :                : 
                               1228                 :          11229 :         LWLockRelease(&header->lock);
                               1229                 :                :     }
                               1230                 :             19 :     dshash_seq_term(&hstat);
                               1231                 :             19 : }
                               1232                 :                : 
                               1233                 :                : static bool
                               1234                 :           1500 : match_kind(PgStatShared_HashEntry *p, Datum match_data)
                               1235                 :                : {
                               1236                 :           1500 :     return p->key.kind == DatumGetInt32(match_data);
                               1237                 :                : }
                               1238                 :                : 
                               1239                 :                : void
                               1240                 :              4 : pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts)
                               1241                 :                : {
                               1242                 :              4 :     pgstat_reset_matching_entries(match_kind, Int32GetDatum(kind), ts);
                               1243                 :              4 : }
                               1244                 :                : 
                               1245                 :                : static void
                               1246                 :        3938320 : pgstat_setup_memcxt(void)
                               1247                 :                : {
                               1248         [ +  + ]:        3938320 :     if (unlikely(!pgStatSharedRefContext))
                               1249                 :          20373 :         pgStatSharedRefContext =
 1487                          1250                 :          20373 :             AllocSetContextCreate(TopMemoryContext,
                               1251                 :                :                                   "PgStat Shared Ref",
                               1252                 :                :                                   ALLOCSET_SMALL_SIZES);
 1650                          1253         [ +  + ]:        3938320 :     if (unlikely(!pgStatEntryRefHashContext))
                               1254                 :          20373 :         pgStatEntryRefHashContext =
 1487                          1255                 :          20373 :             AllocSetContextCreate(TopMemoryContext,
                               1256                 :                :                                   "PgStat Shared Ref Hash",
                               1257                 :                :                                   ALLOCSET_SMALL_SIZES);
 1650                          1258                 :        3938320 : }
        

Generated by: LCOV version 2.0-1