LCOV - differential code coverage report
Current view: top level - src/backend/utils/activity - pgstat_shmem.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DUB DCB
Current: c38de3ae6098ccf1904109f2e550eb4777f66024 vs e352ac8c5c90ed6ce792f044c22145578542a330 Lines: 92.9 % 468 435 16 17 138 297 13 63
Current Date: 2026-11-05 14:14:01 +0100 Functions: 100.0 % 38 38 20 18 1
Baseline: lcov-20261105-baseline Branches: 71.2 % 316 225 29 62 2 83 140
Baseline Date: 2026-11-05 14:14:01 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
[..1] days: 100.0 % 14 14 14
(7,30] days: 92.1 % 127 117 10 113 4
(30,360] days: 79.0 % 62 49 6 7 11 38
(360..) days: 96.2 % 265 255 10 255
Function coverage date bins:
[..1] days: 100.0 % 1 1 1
(7,30] days: 100.0 % 1 1 1
(30,360] days: 100.0 % 4 4 3 1
(360..) days: 100.0 % 32 32 15 17
Branch coverage date bins:
[..1] days: 78.6 % 14 11 3 11
(7,30] days: 78.9 % 90 71 16 3 68 3
(30,360] days: 45.0 % 40 18 10 12 4 14
(360..) days: 72.7 % 172 125 47 2 123

 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                 :                : /* Parameters for shared hashtables. */
                                 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 a stats
                                109                 :                :  * hashtable. 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
 1674 andres@anarazel.de        114                 :CBC        5228 : 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                 :           5228 :     sz = 256 * 1024;
                                127         [ -  + ]:           5228 :     Assert(dsa_minimum_size() <= sz);
                                128                 :           5228 :     return MAXALIGN(sz);
                                129                 :                : }
                                130                 :                : 
                                131                 :                : /*
                                132                 :                :  * Compute shared memory space needed for cumulative statistics
                                133                 :                :  */
                                134                 :                : static Size
                                135                 :           1307 : StatsShmemSize(void)
                                136                 :                : {
                                137                 :                :     Size        sz;
                                138                 :                : 
                                139                 :           1307 :     sz = MAXALIGN(sizeof(PgStat_ShmemControl));
                                140                 :           1307 :     sz = add_size(sz, pgstat_dsa_init_size());
                                141                 :                : 
                                142                 :                :     /* Add per-kind DSA space for variable-numbered own_hash kinds. */
   23 michael@paquier.xyz       143         [ +  + ]:GNC       43131 :     for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
                                144                 :                :     {
                                145                 :          41824 :         const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                                146                 :                : 
                                147   [ +  +  +  +  :          41824 :         if (!kind_info || kind_info->fixed_amount || !kind_info->own_hash)
                                              +  + ]
                                148                 :          41821 :             continue;
                                149                 :                : 
                                150                 :              3 :         sz = add_size(sz, pgstat_dsa_init_size());
                                151                 :                :     }
                                152                 :                : 
                                153                 :                :     /* Add shared memory for all the custom fixed-numbered statistics */
  823 michael@paquier.xyz       154         [ +  + ]:CBC       13070 :     for (PgStat_Kind kind = PGSTAT_KIND_CUSTOM_MIN; kind <= PGSTAT_KIND_CUSTOM_MAX; kind++)
                                155                 :                :     {
                                156                 :          11763 :         const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                                157                 :                : 
                                158         [ +  + ]:          11763 :         if (!kind_info)
                                159                 :          11745 :             continue;
                                160         [ +  + ]:             18 :         if (!kind_info->fixed_amount)
                                161                 :              9 :             continue;
                                162                 :                : 
                                163         [ -  + ]:              9 :         Assert(kind_info->shared_size != 0);
  212                           164                 :              9 :         sz = add_size(sz, MAXALIGN(kind_info->shared_size));
                                165                 :                :     }
                                166                 :                : 
 1674 andres@anarazel.de        167                 :           1307 :     return sz;
                                168                 :                : }
                                169                 :                : 
                                170                 :                : /*
                                171                 :                :  * Register shared memory area for cumulative statistics
                                172                 :                :  */
                                173                 :                : static void
  213 heikki.linnakangas@i      174                 :           1307 : StatsShmemRequest(void *arg)
                                175                 :                : {
                                176                 :           1307 :     ShmemRequestStruct(.name = "Shared Memory Stats",
                                177                 :                :                        .size = StatsShmemSize(),
                                178                 :                :                        .ptr = (void **) &pgStatLocal.shmem,
                                179                 :                :         );
                                180                 :           1307 : }
                                181                 :                : 
                                182                 :                : /*
                                183                 :                :  * Initialize cumulative statistics system during startup
                                184                 :                :  */
                                185                 :                : static void
                                186                 :           1303 : StatsShmemInit(void *arg)
                                187                 :                : {
                                188                 :                :     dsa_area   *dsa;
                                189                 :                :     dshash_table *dsh;
                                190                 :           1303 :     PgStat_ShmemControl *ctl = pgStatLocal.shmem;
                                191                 :           1303 :     char       *p = (char *) ctl;
                                192                 :                : 
                                193                 :                :     /* the allocation of pgStatLocal.shmem itself */
                                194                 :           1303 :     p += MAXALIGN(sizeof(PgStat_ShmemControl));
                                195                 :                : 
                                196                 :                :     /*
                                197                 :                :      * Create a small dsa allocation in plain shared memory. This is required
                                198                 :                :      * because postmaster cannot use dsm segments. It also provides a small
                                199                 :                :      * efficiency win.
                                200                 :                :      */
                                201                 :           1303 :     ctl->raw_dsa_area = p;
  212 michael@paquier.xyz       202                 :           1303 :     p += pgstat_dsa_init_size();
  213 heikki.linnakangas@i      203                 :           1303 :     dsa = dsa_create_in_place(ctl->raw_dsa_area,
                                204                 :                :                               pgstat_dsa_init_size(),
                                205                 :                :                               LWTRANCHE_PGSTATS_DSA, NULL);
                                206                 :           1303 :     dsa_pin(dsa);
                                207                 :                : 
                                208                 :                :     /*
                                209                 :                :      * To ensure dshash is created in "plain" shared memory, temporarily limit
                                210                 :                :      * size of dsa to the initial size of the dsa.
                                211                 :                :      */
                                212                 :           1303 :     dsa_set_size_limit(dsa, pgstat_dsa_init_size());
                                213                 :                : 
                                214                 :                :     /*
                                215                 :                :      * With the limit in place, create the dshash table. XXX: It'd be nice if
                                216                 :                :      * there were dshash_create_in_place().
                                217                 :                :      */
                                218                 :           1303 :     dsh = dshash_create(dsa, &dsh_params, NULL);
                                219                 :           1303 :     ctl->hash_handle = dshash_get_hash_table_handle(dsh);
                                220                 :                : 
                                221                 :                :     /* lift limit set above */
                                222                 :           1303 :     dsa_set_size_limit(dsa, -1);
                                223                 :                : 
                                224                 :                :     /*
                                225                 :                :      * Create dedicated DSA areas and hash tables for kinds that have
                                226                 :                :      * variable-numbered stats and set own_hash to true.
                                227                 :                :      */
   23 michael@paquier.xyz       228                 :GNC        1303 :     memset(ctl->raw_kind_dsa_area, 0, sizeof(ctl->raw_kind_dsa_area));
                                229         [ +  + ]:          42999 :     for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
                                230                 :                :     {
                                231                 :          41696 :         const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                                232                 :                :         dsa_area   *kind_dsa;
                                233                 :                :         dshash_table *kind_dsh;
                                234                 :                : 
                                235   [ +  +  +  +  :          41696 :         if (!kind_info || kind_info->fixed_amount || !kind_info->own_hash)
                                              +  + ]
                                236                 :          41693 :             continue;
                                237                 :                : 
                                238                 :                :         /* Create a per-kind DSA in pre-allocated shared memory */
                                239                 :              3 :         ctl->raw_kind_dsa_area[kind] = p;
                                240                 :              3 :         p += pgstat_dsa_init_size();
                                241                 :              3 :         kind_dsa = dsa_create_in_place(ctl->raw_kind_dsa_area[kind],
                                242                 :                :                                        pgstat_dsa_init_size(),
                                243                 :                :                                        LWTRANCHE_PGSTATS_DSA, NULL);
                                244                 :              3 :         dsa_pin(kind_dsa);
                                245                 :                : 
                                246                 :                :         /* Temporarily limit to keep dshash in the in-place area */
                                247                 :              3 :         dsa_set_size_limit(kind_dsa, pgstat_dsa_init_size());
                                248                 :                : 
                                249                 :              3 :         kind_dsh = dshash_create(kind_dsa, &dsh_params, NULL);
                                250                 :              3 :         ctl->kind_hash_handles[kind] = dshash_get_hash_table_handle(kind_dsh);
                                251                 :                : 
                                252                 :              3 :         dsa_set_size_limit(kind_dsa, -1);
                                253                 :                : 
                                254                 :                :         /* detach, as postmaster never accesses them again. */
                                255                 :              3 :         dshash_detach(kind_dsh);
                                256                 :              3 :         dsa_detach(kind_dsa);
                                257                 :                :     }
                                258                 :                : 
                                259                 :                :     /*
                                260                 :                :      * Postmaster will never access these again, thus free the local
                                261                 :                :      * dsa/dshash references.
                                262                 :                :      */
  213 heikki.linnakangas@i      263                 :CBC        1303 :     dshash_detach(dsh);
                                264                 :           1303 :     dsa_detach(dsa);
                                265                 :                : 
                                266                 :           1303 :     pg_atomic_init_u64(&ctl->gc_request_count, 1);
                                267                 :                : 
                                268                 :                :     /* Do the per-kind initialization */
                                269         [ +  + ]:          42999 :     for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
                                270                 :                :     {
                                271                 :          41696 :         const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                                272                 :                :         char       *ptr;
                                273                 :                : 
                                274         [ +  + ]:          41696 :         if (!kind_info)
                                275                 :          23436 :             continue;
                                276                 :                : 
                                277                 :                :         /* initialize entry count tracking */
                                278         [ +  + ]:          18260 :         if (kind_info->track_entry_count)
                                279                 :              9 :             pg_atomic_init_u64(&ctl->entry_counts[kind - 1], 0);
                                280                 :                : 
                                281                 :                :         /* initialize fixed-numbered stats */
                                282         [ +  + ]:          18260 :         if (kind_info->fixed_amount)
                                283                 :                :         {
                                284         [ +  + ]:           9130 :             if (pgstat_is_kind_builtin(kind))
                                285                 :           9121 :                 ptr = ((char *) ctl) + kind_info->shared_ctl_off;
                                286                 :                :             else
                                287                 :                :             {
                                288                 :              9 :                 int         idx = kind - PGSTAT_KIND_CUSTOM_MIN;
                                289                 :                : 
                                290         [ -  + ]:              9 :                 Assert(kind_info->shared_size != 0);
  212 michael@paquier.xyz       291                 :              9 :                 ctl->custom_data[idx] = p;
                                292                 :              9 :                 p += MAXALIGN(kind_info->shared_size);
  213 heikki.linnakangas@i      293                 :              9 :                 ptr = ctl->custom_data[idx];
                                294                 :                :             }
                                295                 :                : 
                                296                 :           9130 :             kind_info->init_shmem_cb(ptr);
                                297                 :                :         }
                                298                 :                :     }
 1674 andres@anarazel.de        299                 :           1303 : }
                                300                 :                : 
                                301                 :                : void
                                302                 :          24261 : pgstat_attach_shmem(void)
                                303                 :                : {
                                304                 :                :     dsa_area   *shared_dsa;
                                305                 :                :     dshash_table *shared_hash;
                                306                 :                :     MemoryContext oldcontext;
                                307                 :                : 
   23 michael@paquier.xyz       308         [ -  + ]:GNC       24261 :     Assert(pgStatLocal.num_var_hashes == 0);
                                309                 :                : 
                                310                 :                :     /* stats shared memory persists for the backend lifetime */
 1674 andres@anarazel.de        311                 :CBC       24261 :     oldcontext = MemoryContextSwitchTo(TopMemoryContext);
                                312                 :                : 
   23 michael@paquier.xyz       313                 :GNC       24261 :     shared_dsa = dsa_attach_in_place(pgStatLocal.shmem->raw_dsa_area,
                                314                 :                :                                      NULL);
                                315                 :          24261 :     dsa_pin_mapping(shared_dsa);
                                316                 :                : 
                                317                 :          24261 :     shared_hash = dshash_attach(shared_dsa, &dsh_params,
                                318                 :          24261 :                                 pgStatLocal.shmem->hash_handle,
                                319                 :                :                                 NULL);
                                320                 :                : 
                                321                 :                :     /* Build per-kind mappings for the hash tables. */
                                322                 :          24261 :     pgStatLocal.var_hashes[pgStatLocal.num_var_hashes++] = shared_hash;
                                323                 :                : 
                                324         [ +  + ]:         800613 :     for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
                                325                 :                :     {
                                326                 :         776352 :         const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                                327                 :                : 
                                328         [ +  + ]:         776352 :         if (pgStatLocal.shmem->raw_kind_dsa_area[kind] != NULL)
                                329                 :                :         {
                                330                 :                :             dsa_area   *kind_dsa;
                                331                 :                : 
                                332                 :                :             /* per-kind hashtable */
                                333                 :                :             kind_dsa =
                                334                 :             41 :                 dsa_attach_in_place(pgStatLocal.shmem->raw_kind_dsa_area[kind],
                                335                 :                :                                     NULL);
                                336                 :             41 :             dsa_pin_mapping(kind_dsa);
                                337                 :                : 
                                338                 :             41 :             pgStatLocal.kind_hash[kind] =
                                339                 :             41 :                 dshash_attach(kind_dsa, &dsh_params,
                                340                 :             41 :                               pgStatLocal.shmem->kind_hash_handles[kind],
                                341                 :                :                               NULL);
                                342                 :             41 :             pgStatLocal.kind_dsa[kind] = kind_dsa;
                                343                 :             41 :             pgStatLocal.var_hashes[pgStatLocal.num_var_hashes++] =
                                344                 :             41 :                 pgStatLocal.kind_hash[kind];
                                345                 :                :         }
                                346   [ +  +  +  + ]:         776311 :         else if (kind_info && !kind_info->fixed_amount)
                                347                 :                :         {
                                348                 :                :             /* main shared hashtable */
                                349                 :         169893 :             pgStatLocal.kind_hash[kind] = shared_hash;
                                350                 :         169893 :             pgStatLocal.kind_dsa[kind] = shared_dsa;
                                351                 :                :         }
                                352                 :                :         else
                                353                 :                :         {
                                354                 :                :             /* unassigned kind ID */
                                355                 :         606418 :             pgStatLocal.kind_hash[kind] = NULL;
                                356                 :         606418 :             pgStatLocal.kind_dsa[kind] = NULL;
                                357                 :                :         }
                                358                 :                :     }
                                359                 :                : 
 1674 andres@anarazel.de        360                 :CBC       24261 :     MemoryContextSwitchTo(oldcontext);
                                361                 :          24261 : }
                                362                 :                : 
                                363                 :                : void
                                364                 :          24261 : pgstat_detach_shmem(void)
                                365                 :                : {
   23 michael@paquier.xyz       366         [ -  + ]:GNC       24261 :     Assert(pgStatLocal.num_var_hashes > 0);
                                367                 :                : 
                                368                 :                :     /* we shouldn't leave references to shared stats */
 1674 andres@anarazel.de        369                 :CBC       24261 :     pgstat_release_all_entry_refs(false);
                                370                 :                : 
   23 michael@paquier.xyz       371         [ +  + ]:GNC       48563 :     for (int h = 0; h < pgStatLocal.num_var_hashes; h++)
                                372                 :                :     {
                                373                 :          24302 :         dsa_area   *dsa = dshash_get_dsa_area(pgStatLocal.var_hashes[h]);
                                374                 :                : 
                                375                 :          24302 :         dshash_detach(pgStatLocal.var_hashes[h]);
                                376                 :          24302 :         dsa_detach(dsa);
                                377                 :          24302 :         pgStatLocal.var_hashes[h] = NULL;
                                378                 :                :     }
                                379                 :                : 
                                380                 :          24261 :     pgStatLocal.num_var_hashes = 0;
                                381                 :                : 
                                382                 :                :     /* Reset per-kind mappings. */
                                383         [ +  + ]:         800613 :     for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
                                384                 :                :     {
                                385                 :         776352 :         pgStatLocal.kind_hash[kind] = NULL;
                                386                 :         776352 :         pgStatLocal.kind_dsa[kind] = NULL;
                                387                 :                :     }
                                388                 :                : 
                                389                 :                :     /*
                                390                 :                :      * dsa_detach() does not decrement the DSA reference count as no segment
                                391                 :                :      * was provided to dsa_attach_in_place(), causing no cleanup callbacks to
                                392                 :                :      * be registered.  Hence, release it manually now.
                                393                 :                :      */
  861 michael@paquier.xyz       394                 :CBC       24261 :     dsa_release_in_place(pgStatLocal.shmem->raw_dsa_area);
                                395                 :                : 
   23 michael@paquier.xyz       396         [ +  + ]:GNC      800613 :     for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
                                397                 :                :     {
                                398         [ +  + ]:         776352 :         if (pgStatLocal.shmem->raw_kind_dsa_area[kind] != NULL)
                                399                 :             41 :             dsa_release_in_place(pgStatLocal.shmem->raw_kind_dsa_area[kind]);
                                400                 :                :     }
 1674 andres@anarazel.de        401                 :CBC       24261 : }
                                402                 :                : 
                                403                 :                : 
                                404                 :                : /* ------------------------------------------------------------
                                405                 :                :  * Maintenance of shared memory stats entries
                                406                 :                :  * ------------------------------------------------------------
                                407                 :                :  */
                                408                 :                : 
                                409                 :                : /*
                                410                 :                :  * Allocate the DSA body for a new variable-numbered pgstats entry.
                                411                 :                :  *
                                412                 :                :  * Returns InvalidDsaPointer in the event of an allocation failure.
                                413                 :                :  */
                                414                 :                : dsa_pointer
   42 michael@paquier.xyz       415                 :GNC      381751 : pgstat_alloc_entry_body(PgStat_Kind kind)
                                416                 :                : {
                                417                 :         381751 :     const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                                418                 :                : 
   23                           419                 :         763502 :     return dsa_allocate_extended(pgStatLocal.kind_dsa[kind],
   42                           420                 :         381751 :                                  kind_info->shared_size,
                                421                 :                :                                  DSA_ALLOC_ZERO | DSA_ALLOC_NO_OOM);
                                422                 :                : }
                                423                 :                : 
                                424                 :                : /*
                                425                 :                :  * Initialize variable-numbered pgstats entry.
                                426                 :                :  *
                                427                 :                :  * "chunk" must be a valid pointer, allocated previously by
                                428                 :                :  * pgstat_alloc_entry_body().
                                429                 :                :  */
                                430                 :                : PgStatShared_Common *
 1674 andres@anarazel.de        431                 :CBC      381749 : pgstat_init_entry(PgStat_Kind kind,
                                432                 :                :                   PgStatShared_HashEntry *shhashent,
                                433                 :                :                   dsa_pointer chunk)
                                434                 :                : {
                                435                 :                :     PgStatShared_Common *shheader;
  402 michael@paquier.xyz       436                 :         381749 :     const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                                437                 :                : 
   42 michael@paquier.xyz       438         [ -  + ]:GNC      381749 :     Assert(DsaPointerIsValid(chunk));
                                439                 :                : 
                                440                 :                :     /*
                                441                 :                :      * Initialize refcount to 1, marking it as valid / not dropped. The entry
                                442                 :                :      * can't be freed before the initialization because it can't be found as
                                443                 :                :      * long as we hold the dshash partition lock. Caller needs to increase
                                444                 :                :      * further if a longer lived reference is needed.
                                445                 :                :      */
 1674 andres@anarazel.de        446                 :CBC      381749 :     pg_atomic_init_u32(&shhashent->refcount, 1);
                                447                 :                : 
                                448                 :                :     /*
                                449                 :                :      * Initialize "generation" to 0, as freshly created.
                                450                 :                :      */
  720 michael@paquier.xyz       451                 :         381749 :     pg_atomic_init_u32(&shhashent->generation, 0);
 1674 andres@anarazel.de        452                 :         381749 :     shhashent->dropped = false;
                                453                 :                : 
   23 michael@paquier.xyz       454                 :GNC      381749 :     shheader = dsa_get_address(pgStatLocal.kind_dsa[kind], chunk);
 1674 andres@anarazel.de        455                 :CBC      381749 :     shheader->magic = 0xdeadbeef;
                                456                 :                : 
                                457                 :                :     /* Link the new entry from the hash entry. */
                                458                 :         381749 :     shhashent->body = chunk;
                                459                 :                : 
                                460                 :                :     /* Increment entry count, if required. */
  402 michael@paquier.xyz       461         [ +  + ]:         381749 :     if (kind_info->track_entry_count)
                                462                 :             14 :         pg_atomic_fetch_add_u64(&pgStatLocal.shmem->entry_counts[kind - 1], 1);
                                463                 :                : 
 1674 andres@anarazel.de        464                 :         381749 :     LWLockInitialize(&shheader->lock, LWTRANCHE_PGSTATS_DATA);
                                465                 :                : 
                                466                 :         381749 :     return shheader;
                                467                 :                : }
                                468                 :                : 
                                469                 :                : static PgStatShared_Common *
                                470                 :             32 : pgstat_reinit_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
                                471                 :                : {
                                472                 :                :     PgStatShared_Common *shheader;
                                473                 :                : 
   23 michael@paquier.xyz       474                 :GNC          32 :     shheader = dsa_get_address(pgStatLocal.kind_dsa[kind],
                                475                 :                :                                shhashent->body);
                                476                 :                : 
                                477                 :                :     /* mark as not dropped anymore */
 1674 andres@anarazel.de        478                 :CBC          32 :     pg_atomic_fetch_add_u32(&shhashent->refcount, 1);
                                479                 :                : 
                                480                 :                :     /*
                                481                 :                :      * Increment "generation", to let any backend with local references know
                                482                 :                :      * that what they point to is outdated.
                                483                 :                :      */
  720 michael@paquier.xyz       484                 :             32 :     pg_atomic_fetch_add_u32(&shhashent->generation, 1);
 1674 andres@anarazel.de        485                 :             32 :     shhashent->dropped = false;
                                486                 :                : 
                                487                 :                :     /* reinitialize content */
                                488         [ -  + ]:             32 :     Assert(shheader->magic == 0xdeadbeef);
                                489                 :             32 :     memset(pgstat_get_entry_data(kind, shheader), 0,
                                490                 :                :            pgstat_get_entry_len(kind));
                                491                 :                : 
                                492                 :             32 :     return shheader;
                                493                 :                : }
                                494                 :                : 
                                495                 :                : static void
                                496                 :        3965205 : pgstat_setup_shared_refs(void)
                                497                 :                : {
                                498         [ +  + ]:        3965205 :     if (likely(pgStatEntryRefHash != NULL))
                                499                 :        3944570 :         return;
                                500                 :                : 
                                501                 :          20635 :     pgStatEntryRefHash =
                                502                 :          20635 :         pgstat_entry_ref_hash_create(pgStatEntryRefHashContext,
                                503                 :                :                                      PGSTAT_ENTRY_REF_HASH_SIZE, NULL);
                                504                 :          20635 :     pgStatSharedRefAge = pg_atomic_read_u64(&pgStatLocal.shmem->gc_request_count);
                                505         [ -  + ]:          20635 :     Assert(pgStatSharedRefAge != 0);
                                506                 :                : }
                                507                 :                : 
                                508                 :                : /*
                                509                 :                :  * Helper function for pgstat_get_entry_ref().
                                510                 :                :  */
                                511                 :                : static void
                                512                 :         894261 : pgstat_acquire_entry_ref(PgStat_EntryRef *entry_ref,
                                513                 :                :                          dshash_table *hash,
                                514                 :                :                          PgStatShared_HashEntry *shhashent,
                                515                 :                :                          PgStatShared_Common *shheader)
                                516                 :                : {
                                517         [ -  + ]:         894261 :     Assert(shheader->magic == 0xdeadbeef);
                                518         [ -  + ]:         894261 :     Assert(pg_atomic_read_u32(&shhashent->refcount) > 0);
                                519                 :                : 
                                520                 :         894261 :     pg_atomic_fetch_add_u32(&shhashent->refcount, 1);
                                521                 :                : 
                                522                 :         894261 :     entry_ref->shared_stats = shheader;
                                523                 :         894261 :     entry_ref->shared_entry = shhashent;
  720 michael@paquier.xyz       524                 :         894261 :     entry_ref->generation = pg_atomic_read_u32(&shhashent->generation);
                                525                 :                : 
                                526                 :                :     /*
                                527                 :                :      * Complete the local reference before releasing the lock.  Releasing an
                                528                 :                :      * LWLock can process a pending interrupt, and callers may catch the
                                529                 :                :      * resulting error and continue using the backend-local cache.
                                530                 :                :      */
   23 michael@paquier.xyz       531                 :GNC      894261 :     dshash_release_lock(hash, shhashent);
 1674 andres@anarazel.de        532                 :CBC      894261 : }
                                533                 :                : 
                                534                 :                : /*
                                535                 :                :  * Helper function for pgstat_get_entry_ref().
                                536                 :                :  */
                                537                 :                : static bool
                                538                 :        3965205 : pgstat_get_entry_ref_cached(PgStat_HashKey key, PgStat_EntryRef **entry_ref_p)
                                539                 :                : {
                                540                 :                :     bool        found;
                                541                 :                :     PgStat_EntryRefHashEntry *cache_entry;
                                542                 :                : 
                                543                 :                :     /*
                                544                 :                :      * We immediately insert a cache entry, because it avoids 1) multiple
                                545                 :                :      * hashtable lookups in case of a cache miss 2) having to deal with
                                546                 :                :      * out-of-memory errors after incrementing PgStatShared_Common->refcount.
                                547                 :                :      */
                                548                 :                : 
                                549                 :        3965205 :     cache_entry = pgstat_entry_ref_hash_insert(pgStatEntryRefHash, key, &found);
                                550                 :                : 
                                551   [ +  +  -  + ]:        3965205 :     if (!found || !cache_entry->entry_ref)
                                552                 :        1003594 :     {
                                553                 :                :         PgStat_EntryRef *entry_ref;
                                554                 :                : 
  108 michael@paquier.xyz       555                 :GNC     1003594 :         entry_ref = MemoryContextAllocExtended(pgStatSharedRefContext,
                                556                 :                :                                                sizeof(PgStat_EntryRef),
                                557                 :                :                                                MCXT_ALLOC_NO_OOM);
                                558         [ -  + ]:        1003594 :         if (unlikely(entry_ref == NULL))
                                559                 :                :         {
                                560                 :                :             /*
                                561                 :                :              * Clean the hash entry to keep the table consistent in the
                                562                 :                :              * backend.
                                563                 :                :              */
  108 michael@paquier.xyz       564                 :UNC           0 :             pgstat_entry_ref_hash_delete(pgStatEntryRefHash, key);
                                565                 :                : 
                                566         [ #  # ]:              0 :             ereport(ERROR,
                                567                 :                :                     (errcode(ERRCODE_OUT_OF_MEMORY),
                                568                 :                :                      errmsg("out of memory")));
                                569                 :                :         }
                                570                 :                : 
  108 michael@paquier.xyz       571                 :GNC     1003594 :         cache_entry->entry_ref = entry_ref;
 1674 andres@anarazel.de        572                 :CBC     1003594 :         entry_ref->shared_stats = NULL;
                                573                 :        1003594 :         entry_ref->shared_entry = NULL;
                                574                 :        1003594 :         entry_ref->pending = NULL;
                                575                 :                : 
                                576                 :        1003594 :         found = false;
                                577                 :                :     }
                                578         [ -  + ]:        2961611 :     else if (cache_entry->entry_ref->shared_stats == NULL)
                                579                 :                :     {
 1674 andres@anarazel.de        580         [ #  # ]:UBC           0 :         Assert(cache_entry->entry_ref->pending == NULL);
                                581                 :              0 :         found = false;
                                582                 :                :     }
                                583                 :                :     else
                                584                 :                :     {
                                585                 :                :         PgStat_EntryRef *entry_ref PG_USED_FOR_ASSERTS_ONLY;
                                586                 :                : 
 1674 andres@anarazel.de        587                 :CBC     2961611 :         entry_ref = cache_entry->entry_ref;
                                588         [ -  + ]:        2961611 :         Assert(entry_ref->shared_entry != NULL);
                                589         [ -  + ]:        2961611 :         Assert(entry_ref->shared_stats != NULL);
                                590                 :                : 
                                591         [ -  + ]:        2961611 :         Assert(entry_ref->shared_stats->magic == 0xdeadbeef);
                                592                 :                :         /* should have at least our reference */
                                593         [ -  + ]:        2961611 :         Assert(pg_atomic_read_u32(&entry_ref->shared_entry->refcount) > 0);
                                594                 :                :     }
                                595                 :                : 
                                596                 :        3965205 :     *entry_ref_p = cache_entry->entry_ref;
                                597                 :        3965205 :     return found;
                                598                 :                : }
                                599                 :                : 
                                600                 :                : /*
                                601                 :                :  * Get a shared stats reference. If create is true, the shared stats object is
                                602                 :                :  * created if it does not exist.
                                603                 :                :  *
                                604                 :                :  * When create is true, and created_entry is non-NULL, it'll be set to true
                                605                 :                :  * if the entry is newly created, false otherwise.
                                606                 :                :  */
                                607                 :                : PgStat_EntryRef *
  778 michael@paquier.xyz       608                 :        3965205 : pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create,
                                609                 :                :                      bool *created_entry)
                                610                 :                : {
  412                           611                 :        3965205 :     PgStat_HashKey key = {0};
                                612                 :                :     PgStatShared_HashEntry *shhashent;
 1674 andres@anarazel.de        613                 :        3965205 :     PgStatShared_Common *shheader = NULL;
                                614                 :                :     PgStat_EntryRef *entry_ref;
                                615                 :                :     dshash_table *hash;
                                616                 :                :     dsa_area   *dsa;
                                617                 :                : 
  730 michael@paquier.xyz       618                 :        3965205 :     key.kind = kind;
                                619                 :        3965205 :     key.dboid = dboid;
                                620                 :        3965205 :     key.objid = objid;
                                621                 :                : 
                                622                 :                :     /*
                                623                 :                :      * passing in created_entry only makes sense if we possibly could create
                                624                 :                :      * entry.
                                625                 :                :      */
 1469 peter@eisentraut.org      626   [ +  +  -  + ]:        3965205 :     Assert(create || created_entry == NULL);
 1674 andres@anarazel.de        627                 :        3965205 :     pgstat_assert_is_up();
   23 michael@paquier.xyz       628         [ -  + ]:GNC     3965205 :     Assert(pgStatLocal.num_var_hashes > 0);
 1674 andres@anarazel.de        629         [ -  + ]:CBC     3965205 :     Assert(!pgStatLocal.shmem->is_shutdown);
                                630                 :                : 
                                631                 :        3965205 :     pgstat_setup_memcxt();
                                632                 :        3965205 :     pgstat_setup_shared_refs();
                                633                 :                : 
                                634         [ +  + ]:        3965205 :     if (created_entry != NULL)
                                635                 :            116 :         *created_entry = false;
                                636                 :                : 
                                637                 :                :     /*
                                638                 :                :      * Check if other backends dropped stats that could not be deleted because
                                639                 :                :      * somebody held references to it. If so, check this backend's references.
                                640                 :                :      * This is not expected to happen often. The location of the check is a
                                641                 :                :      * bit random, but this is a relatively frequently called path, so better
                                642                 :                :      * than most.
                                643                 :                :      */
                                644         [ +  + ]:        3965205 :     if (pgstat_need_entry_refs_gc())
                                645                 :           7066 :         pgstat_gc_entry_refs();
                                646                 :                : 
                                647                 :                :     /*
                                648                 :                :      * First check the lookup cache hashtable in local memory. If we find a
                                649                 :                :      * match here we can avoid taking locks / causing contention.
                                650                 :                :      */
                                651         [ +  + ]:        3965205 :     if (pgstat_get_entry_ref_cached(key, &entry_ref))
                                652                 :        2961611 :         return entry_ref;
                                653                 :                : 
                                654         [ -  + ]:        1003594 :     Assert(entry_ref != NULL);
                                655                 :                : 
                                656                 :                :     /* Per-kind hashtable and DSA area */
   23 michael@paquier.xyz       657                 :GNC     1003594 :     hash = pgStatLocal.kind_hash[kind];
                                658                 :        1003594 :     dsa = pgStatLocal.kind_dsa[kind];
                                659                 :                : 
                                660                 :                :     /*
                                661                 :                :      * Do a lookup in the hash table first - it's quite likely that the entry
                                662                 :                :      * already exists, and that way we only need a shared lock.
                                663                 :                :      */
                                664                 :        1003594 :     shhashent = dshash_find(hash, &key, false);
                                665                 :                : 
 1674 andres@anarazel.de        666   [ +  +  +  + ]:CBC     1003594 :     if (create && !shhashent)
                                667                 :                :     {
                                668                 :                :         bool        shfound;
                                669                 :                :         dsa_pointer chunk;
                                670                 :                : 
                                671                 :                :         /* Allocate the stats body before inserting a hash entry. */
   42 michael@paquier.xyz       672                 :GNC      143699 :         chunk = pgstat_alloc_entry_body(kind);
                                673         [ -  + ]:         143699 :         if (chunk == InvalidDsaPointer)
                                674                 :                :         {
   42 michael@paquier.xyz       675                 :UNC           0 :             pgstat_release_entry_ref(key, entry_ref, false);
                                676         [ #  # ]:              0 :             ereport(ERROR,
                                677                 :                :                     (errcode(ERRCODE_OUT_OF_MEMORY),
                                678                 :                :                      errmsg("out of memory"),
                                679                 :                :                      errdetail("Failed while allocating entry %u/%u/%" PRIu64 ".",
                                680                 :                :                                key.kind, key.dboid, key.objid)));
                                681                 :                :         }
                                682                 :                : 
                                683                 :                :         /*
                                684                 :                :          * It's possible that somebody created the entry since the above
                                685                 :                :          * lookup. If so, fall through to the same path as if we'd have if it
                                686                 :                :          * already had been created before the dshash_find() calls.
                                687                 :                :          */
   23 michael@paquier.xyz       688                 :GNC      143699 :         shhashent = dshash_find_or_insert_extended(hash,
                                689                 :                :                                                    &key, &shfound,
                                690                 :                :                                                    DSHASH_INSERT_NO_OOM);
   87                           691         [ -  + ]:         143699 :         if (!shhashent)
                                692                 :                :         {
   23 michael@paquier.xyz       693                 :UNC           0 :             dsa_free(dsa, chunk);
                                694                 :                : 
                                695                 :                :             /*
                                696                 :                :              * Clean up the local reference when failing insert into the stats
                                697                 :                :              * hashtable.
                                698                 :                :              */
   87                           699                 :              0 :             pgstat_release_entry_ref(key, entry_ref, false);
                                700         [ #  # ]:              0 :             ereport(ERROR,
                                701                 :                :                     (errcode(ERRCODE_OUT_OF_MEMORY),
                                702                 :                :                      errmsg("out of memory"),
                                703                 :                :                      errdetail("Failed while inserting entry %u/%u/%" PRIu64 ".",
                                704                 :                :                                key.kind, key.dboid, key.objid)));
                                705                 :                :         }
                                706                 :                : 
 1674 andres@anarazel.de        707         [ +  + ]:CBC      143699 :         if (!shfound)
                                708                 :                :         {
   42 michael@paquier.xyz       709                 :GNC      143697 :             shheader = pgstat_init_entry(kind, shhashent, chunk);
   23                           710                 :         143697 :             pgstat_acquire_entry_ref(entry_ref, hash, shhashent, shheader);
                                711                 :                : 
 1674 andres@anarazel.de        712         [ +  + ]:CBC      143697 :             if (created_entry != NULL)
                                713                 :             51 :                 *created_entry = true;
                                714                 :                : 
                                715                 :         143697 :             return entry_ref;
                                716                 :                :         }
                                717                 :                : 
                                718                 :                :         /* Concurrent insert won; drop the unused body. */
   23 michael@paquier.xyz       719                 :GNC           2 :         dsa_free(dsa, chunk);
                                720                 :                :     }
                                721                 :                : 
 1674 andres@anarazel.de        722         [ +  + ]:CBC      859897 :     if (!shhashent)
                                723                 :                :     {
                                724                 :                :         /*
                                725                 :                :          * If we're not creating, delete the reference again. In all
                                726                 :                :          * likelihood it's just a stats lookup - no point wasting memory for a
                                727                 :                :          * shared ref to nothing...
                                728                 :                :          */
                                729                 :         109296 :         pgstat_release_entry_ref(key, entry_ref, false);
                                730                 :                : 
                                731                 :         109296 :         return NULL;
                                732                 :                :     }
                                733                 :                :     else
                                734                 :                :     {
                                735                 :                :         /*
                                736                 :                :          * Can get here either because dshash_find() found a match, or if
                                737                 :                :          * dshash_find_or_insert() found a concurrently inserted entry.
                                738                 :                :          */
                                739                 :                : 
                                740   [ +  +  +  + ]:         750601 :         if (shhashent->dropped && create)
                                741                 :                :         {
                                742                 :                :             /*
                                743                 :                :              * There are legitimate cases where the old stats entry might not
                                744                 :                :              * yet have been dropped by the time it's reused. The most obvious
                                745                 :                :              * case are replication slot stats, where a new slot can be
                                746                 :                :              * created with the same index just after dropping. But oid
                                747                 :                :              * wraparound can lead to other cases as well. We just reset the
                                748                 :                :              * stats to their plain state, while incrementing its "generation"
                                749                 :                :              * in the shared entry for any remaining local references.
                                750                 :                :              */
                                751                 :             32 :             shheader = pgstat_reinit_entry(kind, shhashent);
   23 michael@paquier.xyz       752                 :GNC          32 :             pgstat_acquire_entry_ref(entry_ref, hash, shhashent, shheader);
                                753                 :                : 
 1674 andres@anarazel.de        754         [ -  + ]:CBC          32 :             if (created_entry != NULL)
 1674 andres@anarazel.de        755                 :UBC           0 :                 *created_entry = true;
                                756                 :                : 
 1674 andres@anarazel.de        757                 :CBC          32 :             return entry_ref;
                                758                 :                :         }
                                759         [ +  + ]:         750569 :         else if (shhashent->dropped)
                                760                 :                :         {
   23 michael@paquier.xyz       761                 :GNC          37 :             dshash_release_lock(hash, shhashent);
 1674 andres@anarazel.de        762                 :CBC          37 :             pgstat_release_entry_ref(key, entry_ref, false);
                                763                 :                : 
                                764                 :             37 :             return NULL;
                                765                 :                :         }
                                766                 :                :         else
                                767                 :                :         {
   23 michael@paquier.xyz       768                 :GNC      750532 :             shheader = dsa_get_address(dsa, shhashent->body);
                                769                 :         750532 :             pgstat_acquire_entry_ref(entry_ref, hash, shhashent, shheader);
                                770                 :                : 
 1674 andres@anarazel.de        771                 :CBC      750532 :             return entry_ref;
                                772                 :                :         }
                                773                 :                :     }
                                774                 :                : }
                                775                 :                : 
                                776                 :                : static void
                                777                 :        1003594 : pgstat_release_entry_ref(PgStat_HashKey key, PgStat_EntryRef *entry_ref,
                                778                 :                :                          bool discard_pending)
                                779                 :                : {
                                780   [ +  -  +  + ]:        1003594 :     if (entry_ref && entry_ref->pending)
                                781                 :                :     {
                                782         [ +  - ]:          44326 :         if (discard_pending)
                                783                 :          44326 :             pgstat_delete_pending_entry(entry_ref);
                                784                 :                :         else
 1674 andres@anarazel.de        785         [ #  # ]:UBC           0 :             elog(ERROR, "releasing ref with pending data");
                                786                 :                :     }
                                787                 :                : 
 1674 andres@anarazel.de        788   [ +  -  +  + ]:CBC     1003594 :     if (entry_ref && entry_ref->shared_stats)
                                789                 :                :     {
                                790         [ -  + ]:         894261 :         Assert(entry_ref->shared_stats->magic == 0xdeadbeef);
                                791         [ -  + ]:         894261 :         Assert(entry_ref->pending == NULL);
                                792                 :                : 
                                793                 :                :         /*
                                794                 :                :          * This can't race with another backend looking up the stats entry and
                                795                 :                :          * increasing the refcount because it is not "legal" to create
                                796                 :                :          * additional references to dropped entries.
                                797                 :                :          */
                                798         [ +  + ]:         894261 :         if (pg_atomic_fetch_sub_u32(&entry_ref->shared_entry->refcount, 1) == 1)
                                799                 :                :         {
                                800                 :                :             PgStatShared_HashEntry *shent;
                                801                 :                : 
                                802                 :                :             /*
                                803                 :                :              * We're the last referrer.  Before we get the lock, the entry may
                                804                 :                :              * have been reinitialized, or freed and created again, so look it
                                805                 :                :              * up by key.  If it is gone, it got already dropped and freed.
                                806                 :                :              */
   15 michael@paquier.xyz       807                 :GNC        5194 :             shent = dshash_find(pgStatLocal.kind_hash[key.kind], &key, true);
                                808                 :                : 
   15 michael@paquier.xyz       809         [ +  - ]:CBC        5194 :             if (shent)
                                810                 :                :             {
                                811   [ +  -  +  - ]:          10388 :                 if (shent->dropped &&
                                812                 :           5194 :                     pg_atomic_read_u32(&shent->refcount) == 0)
                                813                 :                :                 {
                                814                 :                :                     /* Entry dropped and unreferenced, so free it. */
                                815                 :           5194 :                     pgstat_free_entry(shent, NULL);
                                816                 :                :                 }
                                817                 :                :                 else
                                818                 :                :                 {
                                819                 :                :                     /* Entry reinitialized or created again, still in use. */
   15 michael@paquier.xyz       820                 :UNC           0 :                     dshash_release_lock(pgStatLocal.kind_hash[key.kind], shent);
                                821                 :                :                 }
                                822                 :                :             }
                                823                 :                :         }
                                824                 :                :     }
                                825                 :                : 
 1674 andres@anarazel.de        826         [ -  + ]:CBC     1003594 :     if (!pgstat_entry_ref_hash_delete(pgStatEntryRefHash, key))
 1674 andres@anarazel.de        827         [ #  # ]:UBC           0 :         elog(ERROR, "entry ref vanished before deletion");
                                828                 :                : 
 1674 andres@anarazel.de        829         [ +  - ]:CBC     1003594 :     if (entry_ref)
                                830                 :        1003594 :         pfree(entry_ref);
                                831                 :        1003594 : }
                                832                 :                : 
                                833                 :                : /*
                                834                 :                :  * Acquire exclusive lock on the entry.
                                835                 :                :  *
                                836                 :                :  * If nowait is true, it's just a conditional acquire, and the result
                                837                 :                :  * *must* be checked to verify success.
                                838                 :                :  * If nowait is false, waits as necessary, always returning true.
                                839                 :                :  */
                                840                 :                : bool
                                841                 :        1264806 : pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
                                842                 :                : {
                                843                 :        1264806 :     LWLock     *lock = &entry_ref->shared_stats->lock;
                                844                 :                : 
                                845         [ +  + ]:        1264806 :     if (nowait)
                                846                 :         568200 :         return LWLockConditionalAcquire(lock, LW_EXCLUSIVE);
                                847                 :                : 
                                848                 :         696606 :     LWLockAcquire(lock, LW_EXCLUSIVE);
                                849                 :         696606 :     return true;
                                850                 :                : }
                                851                 :                : 
                                852                 :                : /*
                                853                 :                :  * Acquire shared lock on the entry.
                                854                 :                :  *
                                855                 :                :  * Separate from pgstat_lock_entry() as most callers will need to lock
                                856                 :                :  * exclusively.  The wait semantics are identical.
                                857                 :                :  */
                                858                 :                : bool
 1536                           859                 :          28127 : pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
                                860                 :                : {
                                861                 :          28127 :     LWLock     *lock = &entry_ref->shared_stats->lock;
                                862                 :                : 
                                863         [ -  + ]:          28127 :     if (nowait)
 1536 andres@anarazel.de        864                 :UBC           0 :         return LWLockConditionalAcquire(lock, LW_SHARED);
                                865                 :                : 
 1536 andres@anarazel.de        866                 :CBC       28127 :     LWLockAcquire(lock, LW_SHARED);
                                867                 :          28127 :     return true;
                                868                 :                : }
                                869                 :                : 
                                870                 :                : void
 1674                           871                 :        1292929 : pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
                                872                 :                : {
                                873                 :        1292929 :     LWLockRelease(&entry_ref->shared_stats->lock);
                                874                 :        1292929 : }
                                875                 :                : 
                                876                 :                : /*
                                877                 :                :  * Helper function to fetch and lock shared stats.
                                878                 :                :  */
                                879                 :                : PgStat_EntryRef *
  778 michael@paquier.xyz       880                 :         114909 : pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid,
                                881                 :                :                             bool nowait)
                                882                 :                : {
                                883                 :                :     PgStat_EntryRef *entry_ref;
                                884                 :                : 
                                885                 :                :     /* find shared table stats entry corresponding to the local entry */
                                886                 :         114909 :     entry_ref = pgstat_get_entry_ref(kind, dboid, objid, true, NULL);
                                887                 :                : 
                                888                 :                :     /* lock the shared entry to protect the content, skip if failed */
 1674 andres@anarazel.de        889         [ -  + ]:         114909 :     if (!pgstat_lock_entry(entry_ref, nowait))
 1674 andres@anarazel.de        890                 :UBC           0 :         return NULL;
                                891                 :                : 
 1674 andres@anarazel.de        892                 :CBC      114909 :     return entry_ref;
                                893                 :                : }
                                894                 :                : 
                                895                 :                : void
                                896                 :           2068 : pgstat_request_entry_refs_gc(void)
                                897                 :                : {
                                898                 :           2068 :     pg_atomic_fetch_add_u64(&pgStatLocal.shmem->gc_request_count, 1);
                                899                 :           2068 : }
                                900                 :                : 
                                901                 :                : static bool
                                902                 :        3965205 : pgstat_need_entry_refs_gc(void)
                                903                 :                : {
                                904                 :                :     uint64      curage;
                                905                 :                : 
                                906         [ -  + ]:        3965205 :     if (!pgStatEntryRefHash)
 1674 andres@anarazel.de        907                 :UBC           0 :         return false;
                                908                 :                : 
                                909                 :                :     /* should have been initialized when creating pgStatEntryRefHash */
 1674 andres@anarazel.de        910         [ -  + ]:CBC     3965205 :     Assert(pgStatSharedRefAge != 0);
                                911                 :                : 
                                912                 :        3965205 :     curage = pg_atomic_read_u64(&pgStatLocal.shmem->gc_request_count);
                                913                 :                : 
                                914                 :        3965205 :     return pgStatSharedRefAge != curage;
                                915                 :                : }
                                916                 :                : 
                                917                 :                : static void
                                918                 :           7066 : pgstat_gc_entry_refs(void)
                                919                 :                : {
                                920                 :                :     pgstat_entry_ref_hash_iterator i;
                                921                 :                :     PgStat_EntryRefHashEntry *ent;
                                922                 :                :     uint64      curage;
                                923                 :                : 
                                924                 :           7066 :     curage = pg_atomic_read_u64(&pgStatLocal.shmem->gc_request_count);
                                925         [ -  + ]:           7066 :     Assert(curage != 0);
                                926                 :                : 
                                927                 :                :     /*
                                928                 :                :      * Some entries have been dropped or reinitialized.  Invalidate cache
                                929                 :                :      * pointer to them.
                                930                 :                :      */
                                931                 :           7066 :     pgstat_entry_ref_hash_start_iterate(pgStatEntryRefHash, &i);
                                932         [ +  + ]:         430573 :     while ((ent = pgstat_entry_ref_hash_iterate(pgStatEntryRefHash, &i)) != NULL)
                                933                 :                :     {
                                934                 :         423507 :         PgStat_EntryRef *entry_ref = ent->entry_ref;
                                935                 :                : 
                                936   [ +  -  -  + ]:         423507 :         Assert(!entry_ref->shared_stats ||
                                937                 :                :                entry_ref->shared_stats->magic == 0xdeadbeef);
                                938                 :                : 
                                939                 :                :         /* A NULL shared_entry marks a partial reference. */
   49 michael@paquier.xyz       940         [ -  + ]:         423507 :         if (entry_ref->shared_entry == NULL)
                                941                 :                :         {
   49 michael@paquier.xyz       942         [ #  # ]:UBC           0 :             Assert(entry_ref->shared_stats == NULL);
                                943         [ #  # ]:              0 :             Assert(entry_ref->pending == NULL);
                                944                 :              0 :             pgstat_release_entry_ref(ent->key, entry_ref, false);
                                945                 :              0 :             continue;
                                946                 :                :         }
                                947                 :                : 
                                948                 :                :         /*
                                949                 :                :          * "generation" checks for the case of entries being reinitialized,
                                950                 :                :          * and "dropped" for the case where these are..  dropped.
                                951                 :                :          */
  696 michael@paquier.xyz       952         [ +  + ]:CBC      423507 :         if (!entry_ref->shared_entry->dropped &&
                                953                 :         375551 :             pg_atomic_read_u32(&entry_ref->shared_entry->generation) ==
                                954         [ +  + ]:         375551 :             entry_ref->generation)
 1674 andres@anarazel.de        955                 :         375520 :             continue;
                                956                 :                : 
                                957                 :                :         /* cannot gc shared ref that has pending data */
                                958         [ +  + ]:          47987 :         if (entry_ref->pending != NULL)
                                959                 :          40725 :             continue;
                                960                 :                : 
                                961                 :           7262 :         pgstat_release_entry_ref(ent->key, entry_ref, false);
                                962                 :                :     }
                                963                 :                : 
                                964                 :           7066 :     pgStatSharedRefAge = curage;
                                965                 :           7066 : }
                                966                 :                : 
                                967                 :                : static void
                                968                 :          20683 : pgstat_release_matching_entry_refs(bool discard_pending, ReleaseMatchCB match,
                                969                 :                :                                    Datum match_data)
                                970                 :                : {
                                971                 :                :     pgstat_entry_ref_hash_iterator i;
                                972                 :                :     PgStat_EntryRefHashEntry *ent;
                                973                 :                : 
                                974         [ +  + ]:          20683 :     if (pgStatEntryRefHash == NULL)
                                975                 :              1 :         return;
                                976                 :                : 
                                977                 :          20682 :     pgstat_entry_ref_hash_start_iterate(pgStatEntryRefHash, &i);
                                978                 :                : 
                                979                 :         844206 :     while ((ent = pgstat_entry_ref_hash_iterate(pgStatEntryRefHash, &i))
                                980         [ +  + ]:         844206 :            != NULL)
                                981                 :                :     {
                                982         [ -  + ]:         823524 :         Assert(ent->entry_ref != NULL);
                                983                 :                : 
                                984   [ +  +  +  - ]:         823524 :         if (match && !match(ent, match_data))
                                985                 :           1434 :             continue;
                                986                 :                : 
                                987                 :         822090 :         pgstat_release_entry_ref(ent->key, ent->entry_ref, discard_pending);
                                988                 :                :     }
                                989                 :                : }
                                990                 :                : 
                                991                 :                : /*
                                992                 :                :  * Release all local references to shared stats entries.
                                993                 :                :  *
                                994                 :                :  * When a process exits it cannot do so while still holding references onto
                                995                 :                :  * stats entries, otherwise the shared stats entries could never be freed.
                                996                 :                :  */
                                997                 :                : static void
                                998                 :          24261 : pgstat_release_all_entry_refs(bool discard_pending)
                                999                 :                : {
                               1000         [ +  + ]:          24261 :     if (pgStatEntryRefHash == NULL)
                               1001                 :           3626 :         return;
                               1002                 :                : 
                               1003                 :          20635 :     pgstat_release_matching_entry_refs(discard_pending, NULL, 0);
                               1004         [ -  + ]:          20635 :     Assert(pgStatEntryRefHash->members == 0);
                               1005                 :          20635 :     pgstat_entry_ref_hash_destroy(pgStatEntryRefHash);
                               1006                 :          20635 :     pgStatEntryRefHash = NULL;
                               1007                 :                : }
                               1008                 :                : 
                               1009                 :                : static bool
                               1010                 :           1434 : match_db(PgStat_EntryRefHashEntry *ent, Datum match_data)
                               1011                 :                : {
                               1012                 :           1434 :     Oid         dboid = DatumGetObjectId(match_data);
                               1013                 :                : 
                               1014                 :           1434 :     return ent->key.dboid == dboid;
                               1015                 :                : }
                               1016                 :                : 
                               1017                 :                : static void
                               1018                 :             48 : pgstat_release_db_entry_refs(Oid dboid)
                               1019                 :                : {
                               1020                 :             48 :     pgstat_release_matching_entry_refs( /* discard pending = */ true,
                               1021                 :                :                                        match_db,
                               1022                 :                :                                        ObjectIdGetDatum(dboid));
                               1023                 :             48 : }
                               1024                 :                : 
                               1025                 :                : 
                               1026                 :                : /* ------------------------------------------------------------
                               1027                 :                :  * Dropping and resetting of stats entries
                               1028                 :                :  * ------------------------------------------------------------
                               1029                 :                :  */
                               1030                 :                : 
                               1031                 :                : static void
                               1032                 :          70820 : pgstat_free_entry(PgStatShared_HashEntry *shent, dshash_seq_status *hstat)
                               1033                 :                : {
                               1034                 :                :     dsa_pointer pdsa;
  402 michael@paquier.xyz      1035                 :          70820 :     PgStat_Kind kind = shent->key.kind;
                               1036                 :                : 
                               1037                 :                :     /*
                               1038                 :                :      * Fetch dsa pointer before deleting entry - that way we can free the
                               1039                 :                :      * memory after releasing the lock.
                               1040                 :                :      */
 1674 andres@anarazel.de       1041                 :          70820 :     pdsa = shent->body;
                               1042                 :                : 
                               1043         [ +  + ]:          70820 :     if (!hstat)
   23 michael@paquier.xyz      1044                 :GNC       65045 :         dshash_delete_entry(pgStatLocal.kind_hash[kind], shent);
                               1045                 :                :     else
 1674 andres@anarazel.de       1046                 :CBC        5775 :         dshash_delete_current(hstat);
                               1047                 :                : 
   23 michael@paquier.xyz      1048                 :GNC       70820 :     dsa_free(pgStatLocal.kind_dsa[kind], pdsa);
                               1049                 :                : 
                               1050                 :                :     /* Decrement entry count, if required. */
  402 michael@paquier.xyz      1051         [ +  + ]:CBC       70820 :     if (pgstat_get_kind_info(kind)->track_entry_count)
                               1052                 :              6 :         pg_atomic_sub_fetch_u64(&pgStatLocal.shmem->entry_counts[kind - 1], 1);
 1674 andres@anarazel.de       1053                 :          70820 : }
                               1054                 :                : 
                               1055                 :                : /*
                               1056                 :                :  * Helper for both pgstat_drop_database_and_contents() and
                               1057                 :                :  * pgstat_drop_entry(). If hstat is non-null delete the shared entry using
                               1058                 :                :  * dshash_delete_current(), otherwise use dshash_delete_entry(). In either
                               1059                 :                :  * case the entry needs to be already locked.
                               1060                 :                :  */
                               1061                 :                : static bool
                               1062                 :          70852 : pgstat_drop_entry_internal(PgStatShared_HashEntry *shent,
                               1063                 :                :                            dshash_seq_status *hstat)
                               1064                 :                : {
                               1065         [ -  + ]:          70852 :     Assert(shent->body != InvalidDsaPointer);
                               1066                 :                : 
                               1067                 :                :     /* should already have released local reference */
                               1068         [ +  + ]:          70852 :     if (pgStatEntryRefHash)
                               1069         [ -  + ]:          70776 :         Assert(!pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, shent->key));
                               1070                 :                : 
                               1071                 :                :     /*
                               1072                 :                :      * Signal that the entry is dropped - this will eventually cause other
                               1073                 :                :      * backends to release their references.
                               1074                 :                :      */
  140 michael@paquier.xyz      1075         [ -  + ]:          70852 :     Assert(!shent->dropped);
 1674 andres@anarazel.de       1076                 :          70852 :     shent->dropped = true;
                               1077                 :                : 
                               1078                 :                :     /* release refcount marking entry as not dropped */
                               1079         [ +  + ]:          70852 :     if (pg_atomic_sub_fetch_u32(&shent->refcount, 1) == 0)
                               1080                 :                :     {
                               1081                 :          65626 :         pgstat_free_entry(shent, hstat);
                               1082                 :          65626 :         return true;
                               1083                 :                :     }
                               1084                 :                :     else
                               1085                 :                :     {
                               1086         [ +  - ]:           5226 :         if (!hstat)
   23 michael@paquier.xyz      1087                 :GNC        5226 :             dshash_release_lock(pgStatLocal.kind_hash[shent->key.kind], shent);
 1674 andres@anarazel.de       1088                 :CBC        5226 :         return false;
                               1089                 :                :     }
                               1090                 :                : }
                               1091                 :                : 
                               1092                 :                : /*
                               1093                 :                :  * Drop stats for the database and all the objects inside that database.
                               1094                 :                :  */
                               1095                 :                : static void
                               1096                 :             48 : pgstat_drop_database_and_contents(Oid dboid)
                               1097                 :                : {
                               1098                 :                :     dshash_seq_status hstat;
                               1099                 :                :     PgStatShared_HashEntry *p;
                               1100                 :             48 :     uint64      not_freed_count = 0;
                               1101                 :                : 
                               1102         [ -  + ]:             48 :     Assert(OidIsValid(dboid));
                               1103                 :                : 
   23 michael@paquier.xyz      1104         [ -  + ]:GNC          48 :     Assert(pgStatLocal.num_var_hashes > 0);
                               1105                 :                : 
                               1106                 :                :     /*
                               1107                 :                :      * This backend might very well be the only backend holding a reference to
                               1108                 :                :      * about-to-be-dropped entries. Ensure that we're not preventing it from
                               1109                 :                :      * being cleaned up till later.
                               1110                 :                :      *
                               1111                 :                :      * Doing this separately from the dshash iteration below avoids having to
                               1112                 :                :      * do so while holding a partition lock on any of the shared hashtables.
                               1113                 :                :      */
 1674 andres@anarazel.de       1114                 :CBC          48 :     pgstat_release_db_entry_refs(dboid);
                               1115                 :                : 
   23 michael@paquier.xyz      1116         [ +  + ]:GNC          96 :     for (int h = 0; h < pgStatLocal.num_var_hashes; h++)
                               1117                 :                :     {
                               1118                 :                :         /* some of the dshash entries are to be removed, take exclusive lock. */
                               1119                 :             48 :         dshash_seq_init(&hstat, pgStatLocal.var_hashes[h], true);
                               1120         [ +  + ]:          19342 :         while ((p = dshash_seq_next(&hstat)) != NULL)
                               1121                 :                :         {
                               1122         [ +  + ]:          19294 :             if (p->dropped)
                               1123                 :              2 :                 continue;
                               1124                 :                : 
                               1125         [ +  + ]:          19292 :             if (p->key.dboid != dboid)
                               1126                 :          13577 :                 continue;
                               1127                 :                : 
                               1128         [ -  + ]:           5715 :             if (!pgstat_drop_entry_internal(p, &hstat))
                               1129                 :                :             {
                               1130                 :                :                 /*
                               1131                 :                :                  * Even statistics for a dropped database might currently be
                               1132                 :                :                  * accessed (consider e.g. database stats for
                               1133                 :                :                  * pg_stat_database).
                               1134                 :                :                  */
   23 michael@paquier.xyz      1135                 :UNC           0 :                 not_freed_count++;
                               1136                 :                :             }
                               1137                 :                :         }
   23 michael@paquier.xyz      1138                 :GNC          48 :         dshash_seq_term(&hstat);
                               1139                 :                :     }
                               1140                 :                : 
                               1141                 :                :     /*
                               1142                 :                :      * If some of the stats data could not be freed, signal the reference
                               1143                 :                :      * holders to run garbage collection of their cached pgStatLocal.shmem.
                               1144                 :                :      */
 1674 andres@anarazel.de       1145         [ -  + ]:CBC          48 :     if (not_freed_count > 0)
 1674 andres@anarazel.de       1146                 :UBC           0 :         pgstat_request_entry_refs_gc();
 1674 andres@anarazel.de       1147                 :CBC          48 : }
                               1148                 :                : 
                               1149                 :                : /*
                               1150                 :                :  * Drop a single stats entry.
                               1151                 :                :  *
                               1152                 :                :  * This routine returns false if the stats entry of the dropped object could
                               1153                 :                :  * not be freed, true otherwise.
                               1154                 :                :  *
                               1155                 :                :  * If missing_ok is true, skip entries that have been concurrently dropped.
                               1156                 :                :  *
                               1157                 :                :  * The callers of this function should call pgstat_request_entry_refs_gc()
                               1158                 :                :  * if the stats entry could not be freed, to ensure that this entry's memory
                               1159                 :                :  * can be reclaimed later by a different backend calling
                               1160                 :                :  * pgstat_gc_entry_refs().
                               1161                 :                :  */
                               1162                 :                : bool
  140 michael@paquier.xyz      1163                 :          92311 : pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid,
                               1164                 :                :                   bool missing_ok)
                               1165                 :                : {
  412                          1166                 :          92311 :     PgStat_HashKey key = {0};
                               1167                 :                :     PgStatShared_HashEntry *shent;
 1674 andres@anarazel.de       1168                 :          92311 :     bool        freed = true;
                               1169                 :                : 
   23 michael@paquier.xyz      1170   [ +  -  -  + ]:GNC       92311 :     Assert(kind >= PGSTAT_KIND_MIN && kind <= PGSTAT_KIND_MAX);
                               1171                 :                : 
                               1172                 :                :     /* Check case of unassigned kind ID */
                               1173         [ -  + ]:          92311 :     if (pgStatLocal.kind_hash[kind] == NULL)
                               1174                 :                :     {
   23 michael@paquier.xyz      1175         [ #  # ]:UNC           0 :         if (missing_ok)
                               1176                 :              0 :             return true;
                               1177                 :                : 
                               1178         [ #  # ]:              0 :         elog(ERROR, "invalid stats kind for entry %u/%u/%" PRIu64,
                               1179                 :                :              kind, dboid, objid);
                               1180                 :                :     }
                               1181                 :                : 
  730 michael@paquier.xyz      1182                 :CBC       92311 :     key.kind = kind;
                               1183                 :          92311 :     key.dboid = dboid;
                               1184                 :          92311 :     key.objid = objid;
                               1185                 :                : 
                               1186                 :                :     /* delete local reference */
 1674 andres@anarazel.de       1187         [ +  + ]:          92311 :     if (pgStatEntryRefHash)
                               1188                 :                :     {
                               1189                 :                :         PgStat_EntryRefHashEntry *lohashent =
 1266 tgl@sss.pgh.pa.us        1190                 :          88678 :             pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, key);
                               1191                 :                : 
 1674 andres@anarazel.de       1192         [ +  + ]:          88678 :         if (lohashent)
                               1193                 :          64909 :             pgstat_release_entry_ref(lohashent->key, lohashent->entry_ref,
                               1194                 :                :                                      true);
                               1195                 :                :     }
                               1196                 :                : 
                               1197                 :                :     /* mark entry in the stats hashtable as deleted, drop if possible */
   23 michael@paquier.xyz      1198                 :GNC       92311 :     shent = dshash_find(pgStatLocal.kind_hash[kind], &key, true);
 1674 andres@anarazel.de       1199         [ +  + ]:CBC       92311 :     if (shent)
                               1200                 :                :     {
  140 michael@paquier.xyz      1201         [ -  + ]:          65077 :         if (shent->dropped)
                               1202                 :                :         {
  140 michael@paquier.xyz      1203         [ #  # ]:UBC           0 :             if (!missing_ok)
                               1204         [ #  # ]:              0 :                 elog(ERROR,
                               1205                 :                :                      "trying to drop stats entry already dropped: kind=%s dboid=%u objid=%" PRIu64 " refcount=%u generation=%u",
                               1206                 :                :                      pgstat_get_kind_info(shent->key.kind)->name,
                               1207                 :                :                      shent->key.dboid,
                               1208                 :                :                      shent->key.objid,
                               1209                 :                :                      pg_atomic_read_u32(&shent->refcount),
                               1210                 :                :                      pg_atomic_read_u32(&shent->generation));
   23 michael@paquier.xyz      1211                 :UNC           0 :             dshash_release_lock(pgStatLocal.kind_hash[kind], shent);
  140 michael@paquier.xyz      1212                 :UBC           0 :             return true;
                               1213                 :                :         }
                               1214                 :                : 
 1674 andres@anarazel.de       1215                 :CBC       65077 :         freed = pgstat_drop_entry_internal(shent, NULL);
                               1216                 :                : 
                               1217                 :                :         /*
                               1218                 :                :          * Database stats contain other stats. Drop those as well when
                               1219                 :                :          * dropping the database. XXX: Perhaps this should be done in a
                               1220                 :                :          * slightly more principled way? But not obvious what that'd look
                               1221                 :                :          * like, and so far this is the only case...
                               1222                 :                :          */
                               1223         [ +  + ]:          65077 :         if (key.kind == PGSTAT_KIND_DATABASE)
                               1224                 :             48 :             pgstat_drop_database_and_contents(key.dboid);
                               1225                 :                :     }
                               1226                 :                : 
                               1227                 :          92311 :     return freed;
                               1228                 :                : }
                               1229                 :                : 
                               1230                 :                : /*
                               1231                 :                :  * Scan through the stats hashtables, dropping statistics if approved by the
                               1232                 :                :  * optional do_drop() function, or all entries if do_drop is NULL.
                               1233                 :                :  *
                               1234                 :                :  * If kind is PGSTAT_KIND_INVALID, all stats hashtables are scanned.  Otherwise,
                               1235                 :                :  * only the hashtable of the specified kind is scanned.
                               1236                 :                :  */
                               1237                 :                : void
    0 michael@paquier.xyz      1238                 :GNC         277 : pgstat_drop_matching_entries(PgStat_Kind kind,
                               1239                 :                :                              bool (*do_drop) (PgStatShared_HashEntry *, Datum),
                               1240                 :                :                              Datum match_data)
                               1241                 :                : {
                               1242                 :            277 :     bool        filter_by_kind = (kind != PGSTAT_KIND_INVALID);
                               1243                 :                :     dshash_table **hashes;
                               1244                 :                :     int         num_hashes;
                               1245                 :                :     dshash_seq_status hstat;
                               1246                 :                :     PgStatShared_HashEntry *ps;
 1674 andres@anarazel.de       1247                 :CBC         277 :     uint64      not_freed_count = 0;
                               1248                 :                : 
    0 michael@paquier.xyz      1249         [ +  + ]:GNC         277 :     if (filter_by_kind)
                               1250                 :                :     {
                               1251   [ +  -  -  + ]:              2 :         Assert(kind >= PGSTAT_KIND_MIN && kind <= PGSTAT_KIND_MAX);
                               1252         [ -  + ]:              2 :         Assert(pgStatLocal.kind_hash[kind] != NULL);
                               1253                 :                : 
                               1254                 :              2 :         hashes = &pgStatLocal.kind_hash[kind];
                               1255                 :              2 :         num_hashes = 1;
                               1256                 :                :     }
                               1257                 :                :     else
                               1258                 :                :     {
                               1259                 :            275 :         hashes = pgStatLocal.var_hashes;
                               1260                 :            275 :         num_hashes = pgStatLocal.num_var_hashes;
                               1261                 :                :     }
                               1262                 :                : 
                               1263         [ +  + ]:            556 :     for (int h = 0; h < num_hashes; h++)
                               1264                 :                :     {
                               1265                 :                :         /* entries are removed, take an exclusive lock */
                               1266                 :            279 :         dshash_seq_init(&hstat, hashes[h], true);
   23                          1267         [ +  + ]:            490 :         while ((ps = dshash_seq_next(&hstat)) != NULL)
                               1268                 :                :         {
                               1269         [ -  + ]:            211 :             if (ps->dropped)
   23 michael@paquier.xyz      1270                 :UNC           0 :                 continue;
                               1271                 :                : 
                               1272                 :                :             /*
                               1273                 :                :              * A kind-specific scan may still use the shared hash, which can
                               1274                 :                :              * contain entries for other kinds.
                               1275                 :                :              */
    0 michael@paquier.xyz      1276   [ +  +  +  + ]:GNC         211 :             if (filter_by_kind && ps->key.kind != kind)
                               1277                 :             68 :                 continue;
                               1278                 :                : 
   23                          1279   [ +  +  +  + ]:            143 :             if (do_drop != NULL && !do_drop(ps, match_data))
                               1280                 :             83 :                 continue;
                               1281                 :                : 
                               1282                 :                :             /* delete local reference */
                               1283         [ +  + ]:             60 :             if (pgStatEntryRefHash)
                               1284                 :                :             {
                               1285                 :                :                 PgStat_EntryRefHashEntry *lohashent =
                               1286                 :              4 :                     pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, ps->key);
                               1287                 :                : 
                               1288         [ -  + ]:              4 :                 if (lohashent)
   23 michael@paquier.xyz      1289                 :UNC           0 :                     pgstat_release_entry_ref(lohashent->key, lohashent->entry_ref,
                               1290                 :                :                                              true);
                               1291                 :                :             }
                               1292                 :                : 
   23 michael@paquier.xyz      1293         [ -  + ]:GNC          60 :             if (!pgstat_drop_entry_internal(ps, &hstat))
   23 michael@paquier.xyz      1294                 :UNC           0 :                 not_freed_count++;
                               1295                 :                :         }
   23 michael@paquier.xyz      1296                 :GNC         279 :         dshash_seq_term(&hstat);
                               1297                 :                :     }
                               1298                 :                : 
 1674 andres@anarazel.de       1299         [ -  + ]:CBC         277 :     if (not_freed_count > 0)
 1674 andres@anarazel.de       1300                 :UBC           0 :         pgstat_request_entry_refs_gc();
 1674 andres@anarazel.de       1301                 :CBC         277 : }
                               1302                 :                : 
                               1303                 :                : /*
                               1304                 :                :  * Scan through the stats hashtables and drop all entries.
                               1305                 :                :  */
                               1306                 :                : void
  643 michael@paquier.xyz      1307                 :            273 : pgstat_drop_all_entries(void)
                               1308                 :                : {
    0 michael@paquier.xyz      1309                 :GNC         273 :     pgstat_drop_matching_entries(PGSTAT_KIND_INVALID, NULL, 0);
  643 michael@paquier.xyz      1310                 :CBC         273 : }
                               1311                 :                : 
                               1312                 :                : static void
 1674 andres@anarazel.de       1313                 :          11382 : shared_stat_reset_contents(PgStat_Kind kind, PgStatShared_Common *header,
                               1314                 :                :                            TimestampTz ts)
                               1315                 :                : {
                               1316                 :          11382 :     const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
                               1317                 :                : 
                               1318                 :          11382 :     memset(pgstat_get_entry_data(kind, header), 0,
                               1319                 :                :            pgstat_get_entry_len(kind));
                               1320                 :                : 
                               1321         [ +  + ]:          11382 :     if (kind_info->reset_timestamp_cb)
                               1322                 :          11378 :         kind_info->reset_timestamp_cb(header, ts);
                               1323                 :          11382 : }
                               1324                 :                : 
                               1325                 :                : static void
   23 michael@paquier.xyz      1326                 :GNC          21 : pgstat_reset_matching_entries_in_hash(dshash_table *hash,
                               1327                 :                :                                       bool (*do_reset) (PgStatShared_HashEntry *, Datum),
                               1328                 :                :                                       Datum match_data,
                               1329                 :                :                                       TimestampTz ts)
                               1330                 :                : {
                               1331                 :                :     dshash_seq_status hstat;
                               1332                 :                :     PgStatShared_HashEntry *p;
                               1333                 :                : 
                               1334                 :                :     /* dshash entry is not modified, take shared lock */
                               1335                 :             21 :     dshash_seq_init(&hstat, hash, false);
                               1336         [ +  + ]:          16301 :     while ((p = dshash_seq_next(&hstat)) != NULL)
                               1337                 :                :     {
                               1338                 :                :         PgStatShared_Common *header;
                               1339                 :                : 
                               1340         [ +  + ]:          16280 :         if (p->dropped)
                               1341                 :              1 :             continue;
                               1342                 :                : 
                               1343         [ +  + ]:          16279 :         if (!do_reset(p, match_data))
                               1344                 :           5159 :             continue;
                               1345                 :                : 
                               1346                 :          11120 :         header = dsa_get_address(pgStatLocal.kind_dsa[p->key.kind], p->body);
                               1347                 :                : 
                               1348                 :          11120 :         LWLockAcquire(&header->lock, LW_EXCLUSIVE);
                               1349                 :                : 
                               1350                 :          11120 :         shared_stat_reset_contents(p->key.kind, header, ts);
                               1351                 :                : 
                               1352                 :          11120 :         LWLockRelease(&header->lock);
                               1353                 :                :     }
                               1354                 :             21 :     dshash_seq_term(&hstat);
                               1355                 :             21 : }
                               1356                 :                : 
                               1357                 :                : /*
                               1358                 :                :  * Reset one variable-numbered stats entry.
                               1359                 :                :  */
                               1360                 :                : void
  778 michael@paquier.xyz      1361                 :CBC         263 : pgstat_reset_entry(PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts)
                               1362                 :                : {
                               1363                 :                :     PgStat_EntryRef *entry_ref;
                               1364                 :                : 
 1674 andres@anarazel.de       1365         [ -  + ]:            263 :     Assert(!pgstat_get_kind_info(kind)->fixed_amount);
                               1366                 :                : 
  778 michael@paquier.xyz      1367                 :            263 :     entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
 1674 andres@anarazel.de       1368   [ +  +  -  + ]:            263 :     if (!entry_ref || entry_ref->shared_entry->dropped)
                               1369                 :              1 :         return;
                               1370                 :                : 
 1669 tgl@sss.pgh.pa.us        1371                 :            262 :     (void) pgstat_lock_entry(entry_ref, false);
 1674 andres@anarazel.de       1372                 :            262 :     shared_stat_reset_contents(kind, entry_ref->shared_stats, ts);
                               1373                 :            262 :     pgstat_unlock_entry(entry_ref);
                               1374                 :                : }
                               1375                 :                : 
                               1376                 :                : /*
                               1377                 :                :  * Scan through the stats hashtables, resetting statistics if approved by the
                               1378                 :                :  * provided do_reset() function.
                               1379                 :                :  */
                               1380                 :                : void
                               1381                 :             15 : pgstat_reset_matching_entries(bool (*do_reset) (PgStatShared_HashEntry *, Datum),
                               1382                 :                :                               Datum match_data, TimestampTz ts)
                               1383                 :                : {
   23 michael@paquier.xyz      1384         [ +  + ]:GNC          30 :     for (int h = 0; h < pgStatLocal.num_var_hashes; h++)
                               1385                 :                :     {
                               1386                 :             15 :         pgstat_reset_matching_entries_in_hash(pgStatLocal.var_hashes[h],
                               1387                 :                :                                               do_reset,
                               1388                 :                :                                               match_data,
                               1389                 :                :                                               ts);
                               1390                 :                :     }
 1674 andres@anarazel.de       1391                 :CBC          15 : }
                               1392                 :                : 
                               1393                 :                : static bool
                               1394                 :           1572 : match_kind(PgStatShared_HashEntry *p, Datum match_data)
                               1395                 :                : {
                               1396                 :           1572 :     return p->key.kind == DatumGetInt32(match_data);
                               1397                 :                : }
                               1398                 :                : 
                               1399                 :                : void
                               1400                 :              6 : pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts)
                               1401                 :                : {
   23 michael@paquier.xyz      1402         [ -  + ]:GNC           6 :     Assert(pgStatLocal.kind_hash[kind] != NULL);
                               1403                 :                : 
                               1404                 :              6 :     pgstat_reset_matching_entries_in_hash(pgStatLocal.kind_hash[kind],
                               1405                 :                :                                           match_kind,
                               1406                 :                :                                           Int32GetDatum(kind),
                               1407                 :                :                                           ts);
 1674 andres@anarazel.de       1408                 :CBC           6 : }
                               1409                 :                : 
                               1410                 :                : static void
                               1411                 :        3965205 : pgstat_setup_memcxt(void)
                               1412                 :                : {
                               1413         [ +  + ]:        3965205 :     if (unlikely(!pgStatSharedRefContext))
                               1414                 :          20635 :         pgStatSharedRefContext =
 1511                          1415                 :          20635 :             AllocSetContextCreate(TopMemoryContext,
                               1416                 :                :                                   "PgStat Shared Ref",
                               1417                 :                :                                   ALLOCSET_SMALL_SIZES);
 1674                          1418         [ +  + ]:        3965205 :     if (unlikely(!pgStatEntryRefHashContext))
                               1419                 :          20635 :         pgStatEntryRefHashContext =
 1511                          1420                 :          20635 :             AllocSetContextCreate(TopMemoryContext,
                               1421                 :                :                                   "PgStat Shared Ref Hash",
                               1422                 :                :                                   ALLOCSET_SMALL_SIZES);
 1674                          1423                 :        3965205 : }
        

Generated by: LCOV version 2.0-1