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