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 : 5067 : 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 : 5067 : sz = 256 * 1024;
127 : : Assert(dsa_minimum_size() <= sz);
128 : 5067 : return MAXALIGN(sz);
129 : : }
130 : :
131 : : /*
132 : : * Compute shared memory space needed for cumulative statistics
133 : : */
134 : : static Size
135 : 1269 : StatsShmemSize(void)
136 : : {
137 : : Size sz;
138 : :
139 : 1269 : sz = MAXALIGN(sizeof(PgStat_ShmemControl));
140 : 1269 : sz = add_size(sz, pgstat_dsa_init_size());
141 : :
142 : : /* Add shared memory for all the custom fixed-numbered statistics */
143 [ + + ]: 12690 : for (PgStat_Kind kind = PGSTAT_KIND_CUSTOM_MIN; kind <= PGSTAT_KIND_CUSTOM_MAX; kind++)
144 : : {
145 : 11421 : const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
146 : :
147 [ + + ]: 11421 : if (!kind_info)
148 : 11415 : 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 : 1269 : return sz;
157 : : }
158 : :
159 : : /*
160 : : * Register shared memory area for cumulative statistics
161 : : */
162 : : static void
163 : 1269 : StatsShmemRequest(void *arg)
164 : : {
165 : 1269 : ShmemRequestStruct(.name = "Shared Memory Stats",
166 : : .size = StatsShmemSize(),
167 : : .ptr = (void **) &pgStatLocal.shmem,
168 : : );
169 : 1269 : }
170 : :
171 : : /*
172 : : * Initialize cumulative statistics system during startup
173 : : */
174 : : static void
175 : 1266 : StatsShmemInit(void *arg)
176 : : {
177 : : dsa_area *dsa;
178 : : dshash_table *dsh;
179 : 1266 : PgStat_ShmemControl *ctl = pgStatLocal.shmem;
180 : 1266 : char *p = (char *) ctl;
181 : :
182 : : /* the allocation of pgStatLocal.shmem itself */
183 : 1266 : 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 : 1266 : ctl->raw_dsa_area = p;
191 : 1266 : p += pgstat_dsa_init_size();
192 : 1266 : dsa = dsa_create_in_place(ctl->raw_dsa_area,
193 : : pgstat_dsa_init_size(),
194 : : LWTRANCHE_PGSTATS_DSA, NULL);
195 : 1266 : 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 : 1266 : 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 : 1266 : dsh = dshash_create(dsa, &dsh_params, NULL);
208 : 1266 : ctl->hash_handle = dshash_get_hash_table_handle(dsh);
209 : :
210 : : /* lift limit set above */
211 : 1266 : 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 : 1266 : dshash_detach(dsh);
218 : 1266 : dsa_detach(dsa);
219 : :
220 : 1266 : pg_atomic_init_u64(&ctl->gc_request_count, 1);
221 : :
222 : : /* Do the per-kind initialization */
223 [ + + ]: 41778 : for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
224 : : {
225 : 40512 : const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
226 : : char *ptr;
227 : :
228 [ + + ]: 40512 : if (!kind_info)
229 : 24048 : continue;
230 : :
231 : : /* initialize entry count tracking */
232 [ + + ]: 16464 : 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 [ + + ]: 16464 : if (kind_info->fixed_amount)
237 : : {
238 [ + + ]: 8865 : if (pgstat_is_kind_builtin(kind))
239 : 8862 : 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 : 8865 : kind_info->init_shmem_cb(ptr);
251 : : }
252 : : }
253 : 1266 : }
254 : :
255 : : void
256 : 25174 : 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 : 25174 : oldcontext = MemoryContextSwitchTo(TopMemoryContext);
264 : :
265 : 25174 : pgStatLocal.dsa = dsa_attach_in_place(pgStatLocal.shmem->raw_dsa_area,
266 : : NULL);
267 : 25174 : dsa_pin_mapping(pgStatLocal.dsa);
268 : :
269 : 50348 : pgStatLocal.shared_hash = dshash_attach(pgStatLocal.dsa, &dsh_params,
270 : 25174 : pgStatLocal.shmem->hash_handle,
271 : : NULL);
272 : :
273 : 25174 : MemoryContextSwitchTo(oldcontext);
274 : 25174 : }
275 : :
276 : : void
277 : 25174 : pgstat_detach_shmem(void)
278 : : {
279 : : Assert(pgStatLocal.dsa);
280 : :
281 : : /* we shouldn't leave references to shared stats */
282 : 25174 : pgstat_release_all_entry_refs(false);
283 : :
284 : 25174 : dshash_detach(pgStatLocal.shared_hash);
285 : 25174 : pgStatLocal.shared_hash = NULL;
286 : :
287 : 25174 : 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 : 25174 : dsa_release_in_place(pgStatLocal.shmem->raw_dsa_area);
295 : :
296 : 25174 : pgStatLocal.dsa = NULL;
297 : 25174 : }
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 : 400497 : 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 : 400497 : 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 : 400497 : pg_atomic_init_u32(&shhashent->refcount, 1);
328 : :
329 : : /*
330 : : * Initialize "generation" to 0, as freshly created.
331 : : */
332 : 400497 : pg_atomic_init_u32(&shhashent->generation, 0);
333 : 400497 : shhashent->dropped = false;
334 : :
335 : 400497 : chunk = dsa_allocate_extended(pgStatLocal.dsa,
336 : 400497 : kind_info->shared_size,
337 : : DSA_ALLOC_ZERO | DSA_ALLOC_NO_OOM);
338 [ - + ]: 400497 : if (chunk == InvalidDsaPointer)
339 : 0 : return NULL;
340 : :
341 : 400497 : shheader = dsa_get_address(pgStatLocal.dsa, chunk);
342 : 400497 : shheader->magic = 0xdeadbeef;
343 : :
344 : : /* Link the new entry from the hash entry. */
345 : 400497 : shhashent->body = chunk;
346 : :
347 : : /* Increment entry count, if required. */
348 [ + + ]: 400497 : if (kind_info->track_entry_count)
349 : 6 : pg_atomic_fetch_add_u64(&pgStatLocal.shmem->entry_counts[kind - 1], 1);
350 : :
351 : 400497 : LWLockInitialize(&shheader->lock, LWTRANCHE_PGSTATS_DATA);
352 : :
353 : 400497 : 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 : 4615962 : pgstat_setup_shared_refs(void)
383 : : {
384 [ + + ]: 4615962 : if (likely(pgStatEntryRefHash != NULL))
385 : 4594332 : return;
386 : :
387 : 21630 : pgStatEntryRefHash =
388 : 21630 : pgstat_entry_ref_hash_create(pgStatEntryRefHashContext,
389 : : PGSTAT_ENTRY_REF_HASH_SIZE, NULL);
390 : 21630 : 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 : 1190358 : 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 : 1190358 : pg_atomic_fetch_add_u32(&shhashent->refcount, 1);
406 : :
407 : 1190358 : entry_ref->shared_stats = shheader;
408 : 1190358 : entry_ref->shared_entry = shhashent;
409 : 1190358 : entry_ref->generation = pg_atomic_read_u32(&shhashent->generation);
410 : :
411 : : /*
412 : : * Complete the local reference before releasing the lock. Releasing an
413 : : * LWLock can process a pending interrupt, and callers may catch the
414 : : * resulting error and continue using the backend-local cache.
415 : : */
416 : 1190358 : dshash_release_lock(pgStatLocal.shared_hash, shhashent);
417 : 1190358 : }
418 : :
419 : : /*
420 : : * Helper function for pgstat_get_entry_ref().
421 : : */
422 : : static bool
423 : 4615962 : pgstat_get_entry_ref_cached(PgStat_HashKey key, PgStat_EntryRef **entry_ref_p)
424 : : {
425 : : bool found;
426 : : PgStat_EntryRefHashEntry *cache_entry;
427 : :
428 : : /*
429 : : * We immediately insert a cache entry, because it avoids 1) multiple
430 : : * hashtable lookups in case of a cache miss 2) having to deal with
431 : : * out-of-memory errors after incrementing PgStatShared_Common->refcount.
432 : : */
433 : :
434 : 4615962 : cache_entry = pgstat_entry_ref_hash_insert(pgStatEntryRefHash, key, &found);
435 : :
436 [ + + - + ]: 4615962 : if (!found || !cache_entry->entry_ref)
437 : 1300149 : {
438 : : PgStat_EntryRef *entry_ref;
439 : :
440 : 1300149 : entry_ref = MemoryContextAllocExtended(pgStatSharedRefContext,
441 : : sizeof(PgStat_EntryRef),
442 : : MCXT_ALLOC_NO_OOM);
443 [ - + ]: 1300149 : if (unlikely(entry_ref == NULL))
444 : : {
445 : : /*
446 : : * Clean the hash entry to keep the table consistent in the
447 : : * backend.
448 : : */
449 : 0 : pgstat_entry_ref_hash_delete(pgStatEntryRefHash, key);
450 : :
451 [ # # ]: 0 : ereport(ERROR,
452 : : (errcode(ERRCODE_OUT_OF_MEMORY),
453 : : errmsg("out of memory")));
454 : : }
455 : :
456 : 1300149 : cache_entry->entry_ref = entry_ref;
457 : 1300149 : entry_ref->shared_stats = NULL;
458 : 1300149 : entry_ref->shared_entry = NULL;
459 : 1300149 : entry_ref->pending = NULL;
460 : :
461 : 1300149 : found = false;
462 : : }
463 [ - + ]: 3315813 : else if (cache_entry->entry_ref->shared_stats == NULL)
464 : : {
465 : : Assert(cache_entry->entry_ref->pending == NULL);
466 : 0 : found = false;
467 : : }
468 : : else
469 : : {
470 : : PgStat_EntryRef *entry_ref PG_USED_FOR_ASSERTS_ONLY;
471 : :
472 : 3315813 : entry_ref = cache_entry->entry_ref;
473 : : Assert(entry_ref->shared_entry != NULL);
474 : : Assert(entry_ref->shared_stats != NULL);
475 : :
476 : : Assert(entry_ref->shared_stats->magic == 0xdeadbeef);
477 : : /* should have at least our reference */
478 : : Assert(pg_atomic_read_u32(&entry_ref->shared_entry->refcount) > 0);
479 : : }
480 : :
481 : 4615962 : *entry_ref_p = cache_entry->entry_ref;
482 : 4615962 : return found;
483 : : }
484 : :
485 : : /*
486 : : * Get a shared stats reference. If create is true, the shared stats object is
487 : : * created if it does not exist.
488 : : *
489 : : * When create is true, and created_entry is non-NULL, it'll be set to true
490 : : * if the entry is newly created, false otherwise.
491 : : */
492 : : PgStat_EntryRef *
493 : 4615962 : pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create,
494 : : bool *created_entry)
495 : : {
496 : 4615962 : PgStat_HashKey key = {0};
497 : : PgStatShared_HashEntry *shhashent;
498 : 4615962 : PgStatShared_Common *shheader = NULL;
499 : : PgStat_EntryRef *entry_ref;
500 : :
501 : 4615962 : key.kind = kind;
502 : 4615962 : key.dboid = dboid;
503 : 4615962 : key.objid = objid;
504 : :
505 : : /*
506 : : * passing in created_entry only makes sense if we possibly could create
507 : : * entry.
508 : : */
509 : : Assert(create || created_entry == NULL);
510 : : pgstat_assert_is_up();
511 : : Assert(pgStatLocal.shared_hash != NULL);
512 : : Assert(!pgStatLocal.shmem->is_shutdown);
513 : :
514 : 4615962 : pgstat_setup_memcxt();
515 : 4615962 : pgstat_setup_shared_refs();
516 : :
517 [ + + ]: 4615962 : if (created_entry != NULL)
518 : 116 : *created_entry = false;
519 : :
520 : : /*
521 : : * Check if other backends dropped stats that could not be deleted because
522 : : * somebody held references to it. If so, check this backend's references.
523 : : * This is not expected to happen often. The location of the check is a
524 : : * bit random, but this is a relatively frequently called path, so better
525 : : * than most.
526 : : */
527 [ + + ]: 4615962 : if (pgstat_need_entry_refs_gc())
528 : 6740 : pgstat_gc_entry_refs();
529 : :
530 : : /*
531 : : * First check the lookup cache hashtable in local memory. If we find a
532 : : * match here we can avoid taking locks / causing contention.
533 : : */
534 [ + + ]: 4615962 : if (pgstat_get_entry_ref_cached(key, &entry_ref))
535 : 3315813 : return entry_ref;
536 : :
537 : : Assert(entry_ref != NULL);
538 : :
539 : : /*
540 : : * Do a lookup in the hash table first - it's quite likely that the entry
541 : : * already exists, and that way we only need a shared lock.
542 : : */
543 : 1300149 : shhashent = dshash_find(pgStatLocal.shared_hash, &key, false);
544 : :
545 [ + + + + ]: 1300149 : if (create && !shhashent)
546 : : {
547 : : bool shfound;
548 : :
549 : : /*
550 : : * It's possible that somebody created the entry since the above
551 : : * lookup. If so, fall through to the same path as if we'd have if it
552 : : * already had been created before the dshash_find() calls.
553 : : */
554 : 149711 : shhashent = dshash_find_or_insert(pgStatLocal.shared_hash, &key, &shfound);
555 [ + - ]: 149711 : if (!shfound)
556 : : {
557 : 149711 : shheader = pgstat_init_entry(kind, shhashent);
558 [ - + ]: 149711 : if (shheader == NULL)
559 : : {
560 : : /*
561 : : * Failed the allocation of a new entry, so clean up both the
562 : : * local reference and the shared hashtable before giving up.
563 : : * Clean the local state first, since releasing the dshash
564 : : * lock can process a pending interrupt.
565 : : */
566 : 0 : pgstat_release_entry_ref(key, entry_ref, false);
567 : 0 : dshash_delete_entry(pgStatLocal.shared_hash, shhashent);
568 : :
569 [ # # ]: 0 : ereport(ERROR,
570 : : (errcode(ERRCODE_OUT_OF_MEMORY),
571 : : errmsg("out of memory"),
572 : : errdetail("Failed while allocating entry %u/%u/%" PRIu64 ".",
573 : : key.kind, key.dboid, key.objid)));
574 : : }
575 : 149711 : pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
576 : :
577 [ + + ]: 149711 : if (created_entry != NULL)
578 : 51 : *created_entry = true;
579 : :
580 : 149711 : return entry_ref;
581 : : }
582 : : }
583 : :
584 [ + + ]: 1150438 : if (!shhashent)
585 : : {
586 : : /*
587 : : * If we're not creating, delete the reference again. In all
588 : : * likelihood it's just a stats lookup - no point wasting memory for a
589 : : * shared ref to nothing...
590 : : */
591 : 109754 : pgstat_release_entry_ref(key, entry_ref, false);
592 : :
593 : 109754 : return NULL;
594 : : }
595 : : else
596 : : {
597 : : /*
598 : : * Can get here either because dshash_find() found a match, or if
599 : : * dshash_find_or_insert() found a concurrently inserted entry.
600 : : */
601 : :
602 [ + + + + ]: 1040684 : if (shhashent->dropped && create)
603 : : {
604 : : /*
605 : : * There are legitimate cases where the old stats entry might not
606 : : * yet have been dropped by the time it's reused. The most obvious
607 : : * case are replication slot stats, where a new slot can be
608 : : * created with the same index just after dropping. But oid
609 : : * wraparound can lead to other cases as well. We just reset the
610 : : * stats to their plain state, while incrementing its "generation"
611 : : * in the shared entry for any remaining local references.
612 : : */
613 : 30 : shheader = pgstat_reinit_entry(kind, shhashent);
614 : 30 : pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
615 : :
616 [ - + ]: 30 : if (created_entry != NULL)
617 : 0 : *created_entry = true;
618 : :
619 : 30 : return entry_ref;
620 : : }
621 [ + + ]: 1040654 : else if (shhashent->dropped)
622 : : {
623 : 37 : dshash_release_lock(pgStatLocal.shared_hash, shhashent);
624 : 37 : pgstat_release_entry_ref(key, entry_ref, false);
625 : :
626 : 37 : return NULL;
627 : : }
628 : : else
629 : : {
630 : 1040617 : shheader = dsa_get_address(pgStatLocal.dsa, shhashent->body);
631 : 1040617 : pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
632 : :
633 : 1040617 : return entry_ref;
634 : : }
635 : : }
636 : : }
637 : :
638 : : static void
639 : 1300149 : pgstat_release_entry_ref(PgStat_HashKey key, PgStat_EntryRef *entry_ref,
640 : : bool discard_pending)
641 : : {
642 [ + - + + ]: 1300149 : if (entry_ref && entry_ref->pending)
643 : : {
644 [ + - ]: 46450 : if (discard_pending)
645 : 46450 : pgstat_delete_pending_entry(entry_ref);
646 : : else
647 [ # # ]: 0 : elog(ERROR, "releasing ref with pending data");
648 : : }
649 : :
650 [ + - + + ]: 1300149 : if (entry_ref && entry_ref->shared_stats)
651 : : {
652 : : Assert(entry_ref->shared_stats->magic == 0xdeadbeef);
653 : : Assert(entry_ref->pending == NULL);
654 : :
655 : : /*
656 : : * This can't race with another backend looking up the stats entry and
657 : : * increasing the refcount because it is not "legal" to create
658 : : * additional references to dropped entries.
659 : : */
660 [ + + ]: 1190358 : if (pg_atomic_fetch_sub_u32(&entry_ref->shared_entry->refcount, 1) == 1)
661 : : {
662 : : PgStatShared_HashEntry *shent;
663 : :
664 : : /*
665 : : * We're the last referrer to this entry, try to drop the shared
666 : : * entry.
667 : : */
668 : :
669 : : /* only dropped entries can reach a 0 refcount */
670 : : Assert(entry_ref->shared_entry->dropped);
671 : :
672 : 5423 : shent = dshash_find(pgStatLocal.shared_hash,
673 : 5423 : &entry_ref->shared_entry->key,
674 : : true);
675 [ - + ]: 5423 : if (!shent)
676 [ # # ]: 0 : elog(ERROR, "could not find just referenced shared stats entry");
677 : :
678 : : /*
679 : : * This entry may have been reinitialized while trying to release
680 : : * it, so double-check that it has not been reused while holding a
681 : : * lock on its shared entry.
682 : : */
683 : 5423 : if (pg_atomic_read_u32(&entry_ref->shared_entry->generation) ==
684 [ + - ]: 5423 : entry_ref->generation)
685 : : {
686 : : /* Same "generation", so we're OK with the removal */
687 : : Assert(pg_atomic_read_u32(&entry_ref->shared_entry->refcount) == 0);
688 : : Assert(entry_ref->shared_entry == shent);
689 : 5423 : pgstat_free_entry(shent, NULL);
690 : : }
691 : : else
692 : : {
693 : : /*
694 : : * Shared stats entry has been reinitialized, so do not drop
695 : : * its shared entry, only release its lock.
696 : : */
697 : 0 : dshash_release_lock(pgStatLocal.shared_hash, shent);
698 : : }
699 : : }
700 : : }
701 : :
702 [ - + ]: 1300149 : if (!pgstat_entry_ref_hash_delete(pgStatEntryRefHash, key))
703 [ # # ]: 0 : elog(ERROR, "entry ref vanished before deletion");
704 : :
705 [ + - ]: 1300149 : if (entry_ref)
706 : 1300149 : pfree(entry_ref);
707 : 1300149 : }
708 : :
709 : : /*
710 : : * Acquire exclusive lock on the entry.
711 : : *
712 : : * If nowait is true, it's just a conditional acquire, and the result
713 : : * *must* be checked to verify success.
714 : : * If nowait is false, waits as necessary, always returning true.
715 : : */
716 : : bool
717 : 1533730 : pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
718 : : {
719 : 1533730 : LWLock *lock = &entry_ref->shared_stats->lock;
720 : :
721 [ + + ]: 1533730 : if (nowait)
722 : 417364 : return LWLockConditionalAcquire(lock, LW_EXCLUSIVE);
723 : :
724 : 1116366 : LWLockAcquire(lock, LW_EXCLUSIVE);
725 : 1116366 : return true;
726 : : }
727 : :
728 : : /*
729 : : * Acquire shared lock on the entry.
730 : : *
731 : : * Separate from pgstat_lock_entry() as most callers will need to lock
732 : : * exclusively. The wait semantics are identical.
733 : : */
734 : : bool
735 : 308275 : pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
736 : : {
737 : 308275 : LWLock *lock = &entry_ref->shared_stats->lock;
738 : :
739 [ - + ]: 308275 : if (nowait)
740 : 0 : return LWLockConditionalAcquire(lock, LW_SHARED);
741 : :
742 : 308275 : LWLockAcquire(lock, LW_SHARED);
743 : 308275 : return true;
744 : : }
745 : :
746 : : void
747 : 1841993 : pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
748 : : {
749 : 1841993 : LWLockRelease(&entry_ref->shared_stats->lock);
750 : 1841993 : }
751 : :
752 : : /*
753 : : * Helper function to fetch and lock shared stats.
754 : : */
755 : : PgStat_EntryRef *
756 : 335361 : pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid,
757 : : bool nowait)
758 : : {
759 : : PgStat_EntryRef *entry_ref;
760 : :
761 : : /* find shared table stats entry corresponding to the local entry */
762 : 335361 : entry_ref = pgstat_get_entry_ref(kind, dboid, objid, true, NULL);
763 : :
764 : : /* lock the shared entry to protect the content, skip if failed */
765 [ - + ]: 335361 : if (!pgstat_lock_entry(entry_ref, nowait))
766 : 0 : return NULL;
767 : :
768 : 335361 : return entry_ref;
769 : : }
770 : :
771 : : void
772 : 2158 : pgstat_request_entry_refs_gc(void)
773 : : {
774 : 2158 : pg_atomic_fetch_add_u64(&pgStatLocal.shmem->gc_request_count, 1);
775 : 2158 : }
776 : :
777 : : static bool
778 : 4615962 : pgstat_need_entry_refs_gc(void)
779 : : {
780 : : uint64 curage;
781 : :
782 [ - + ]: 4615962 : if (!pgStatEntryRefHash)
783 : 0 : return false;
784 : :
785 : : /* should have been initialized when creating pgStatEntryRefHash */
786 : : Assert(pgStatSharedRefAge != 0);
787 : :
788 : 4615962 : curage = pg_atomic_read_u64(&pgStatLocal.shmem->gc_request_count);
789 : :
790 : 4615962 : return pgStatSharedRefAge != curage;
791 : : }
792 : :
793 : : static void
794 : 6740 : pgstat_gc_entry_refs(void)
795 : : {
796 : : pgstat_entry_ref_hash_iterator i;
797 : : PgStat_EntryRefHashEntry *ent;
798 : : uint64 curage;
799 : :
800 : 6740 : curage = pg_atomic_read_u64(&pgStatLocal.shmem->gc_request_count);
801 : : Assert(curage != 0);
802 : :
803 : : /*
804 : : * Some entries have been dropped or reinitialized. Invalidate cache
805 : : * pointer to them.
806 : : */
807 : 6740 : pgstat_entry_ref_hash_start_iterate(pgStatEntryRefHash, &i);
808 [ + + ]: 509626 : while ((ent = pgstat_entry_ref_hash_iterate(pgStatEntryRefHash, &i)) != NULL)
809 : : {
810 : 502886 : PgStat_EntryRef *entry_ref = ent->entry_ref;
811 : :
812 : : Assert(!entry_ref->shared_stats ||
813 : : entry_ref->shared_stats->magic == 0xdeadbeef);
814 : :
815 : : /*
816 : : * "generation" checks for the case of entries being reinitialized,
817 : : * and "dropped" for the case where these are.. dropped.
818 : : */
819 [ + + ]: 502886 : if (!entry_ref->shared_entry->dropped &&
820 : 369793 : pg_atomic_read_u32(&entry_ref->shared_entry->generation) ==
821 [ + + ]: 369793 : entry_ref->generation)
822 : 369763 : continue;
823 : :
824 : : /* cannot gc shared ref that has pending data */
825 [ + + ]: 133123 : if (entry_ref->pending != NULL)
826 : 127852 : continue;
827 : :
828 : 5271 : pgstat_release_entry_ref(ent->key, entry_ref, false);
829 : : }
830 : :
831 : 6740 : pgStatSharedRefAge = curage;
832 : 6740 : }
833 : :
834 : : static void
835 : 21676 : pgstat_release_matching_entry_refs(bool discard_pending, ReleaseMatchCB match,
836 : : Datum match_data)
837 : : {
838 : : pgstat_entry_ref_hash_iterator i;
839 : : PgStat_EntryRefHashEntry *ent;
840 : :
841 [ + + ]: 21676 : if (pgStatEntryRefHash == NULL)
842 : 1 : return;
843 : :
844 : 21675 : pgstat_entry_ref_hash_start_iterate(pgStatEntryRefHash, &i);
845 : :
846 : 1140131 : while ((ent = pgstat_entry_ref_hash_iterate(pgStatEntryRefHash, &i))
847 [ + + ]: 1140131 : != NULL)
848 : : {
849 : : Assert(ent->entry_ref != NULL);
850 : :
851 [ + + + - ]: 1118456 : if (match && !match(ent, match_data))
852 : 1376 : continue;
853 : :
854 : 1117080 : pgstat_release_entry_ref(ent->key, ent->entry_ref, discard_pending);
855 : : }
856 : : }
857 : :
858 : : /*
859 : : * Release all local references to shared stats entries.
860 : : *
861 : : * When a process exits it cannot do so while still holding references onto
862 : : * stats entries, otherwise the shared stats entries could never be freed.
863 : : */
864 : : static void
865 : 25174 : pgstat_release_all_entry_refs(bool discard_pending)
866 : : {
867 [ + + ]: 25174 : if (pgStatEntryRefHash == NULL)
868 : 3544 : return;
869 : :
870 : 21630 : pgstat_release_matching_entry_refs(discard_pending, NULL, 0);
871 : : Assert(pgStatEntryRefHash->members == 0);
872 : 21630 : pgstat_entry_ref_hash_destroy(pgStatEntryRefHash);
873 : 21630 : pgStatEntryRefHash = NULL;
874 : : }
875 : :
876 : : static bool
877 : 1376 : match_db(PgStat_EntryRefHashEntry *ent, Datum match_data)
878 : : {
879 : 1376 : Oid dboid = DatumGetObjectId(match_data);
880 : :
881 : 1376 : return ent->key.dboid == dboid;
882 : : }
883 : :
884 : : static void
885 : 46 : pgstat_release_db_entry_refs(Oid dboid)
886 : : {
887 : 46 : pgstat_release_matching_entry_refs( /* discard pending = */ true,
888 : : match_db,
889 : : ObjectIdGetDatum(dboid));
890 : 46 : }
891 : :
892 : :
893 : : /* ------------------------------------------------------------
894 : : * Dropping and resetting of stats entries
895 : : * ------------------------------------------------------------
896 : : */
897 : :
898 : : static void
899 : 74225 : pgstat_free_entry(PgStatShared_HashEntry *shent, dshash_seq_status *hstat)
900 : : {
901 : : dsa_pointer pdsa;
902 : 74225 : PgStat_Kind kind = shent->key.kind;
903 : :
904 : : /*
905 : : * Fetch dsa pointer before deleting entry - that way we can free the
906 : : * memory after releasing the lock.
907 : : */
908 : 74225 : pdsa = shent->body;
909 : :
910 [ + + ]: 74225 : if (!hstat)
911 : 68138 : dshash_delete_entry(pgStatLocal.shared_hash, shent);
912 : : else
913 : 6087 : dshash_delete_current(hstat);
914 : :
915 : 74225 : dsa_free(pgStatLocal.dsa, pdsa);
916 : :
917 : : /* Decrement entry count, if required. */
918 [ + + ]: 74225 : if (pgstat_get_kind_info(kind)->track_entry_count)
919 : 2 : pg_atomic_sub_fetch_u64(&pgStatLocal.shmem->entry_counts[kind - 1], 1);
920 : 74225 : }
921 : :
922 : : /*
923 : : * Helper for both pgstat_drop_database_and_contents() and
924 : : * pgstat_drop_entry(). If hstat is non-null delete the shared entry using
925 : : * dshash_delete_current(), otherwise use dshash_delete_entry(). In either
926 : : * case the entry needs to be already locked.
927 : : */
928 : : static bool
929 : 74255 : pgstat_drop_entry_internal(PgStatShared_HashEntry *shent,
930 : : dshash_seq_status *hstat)
931 : : {
932 : : Assert(shent->body != InvalidDsaPointer);
933 : :
934 : : /* should already have released local reference */
935 : 74255 : if (pgStatEntryRefHash)
936 : : Assert(!pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, shent->key));
937 : :
938 : : /*
939 : : * Signal that the entry is dropped - this will eventually cause other
940 : : * backends to release their references.
941 : : */
942 : : Assert(!shent->dropped);
943 : 74255 : shent->dropped = true;
944 : :
945 : : /* release refcount marking entry as not dropped */
946 [ + + ]: 74255 : if (pg_atomic_sub_fetch_u32(&shent->refcount, 1) == 0)
947 : : {
948 : 68802 : pgstat_free_entry(shent, hstat);
949 : 68802 : return true;
950 : : }
951 : : else
952 : : {
953 [ + - ]: 5453 : if (!hstat)
954 : 5453 : dshash_release_lock(pgStatLocal.shared_hash, shent);
955 : 5453 : return false;
956 : : }
957 : : }
958 : :
959 : : /*
960 : : * Drop stats for the database and all the objects inside that database.
961 : : */
962 : : static void
963 : 46 : pgstat_drop_database_and_contents(Oid dboid)
964 : : {
965 : : dshash_seq_status hstat;
966 : : PgStatShared_HashEntry *p;
967 : 46 : uint64 not_freed_count = 0;
968 : :
969 : : Assert(OidIsValid(dboid));
970 : :
971 : : Assert(pgStatLocal.shared_hash != NULL);
972 : :
973 : : /*
974 : : * This backend might very well be the only backend holding a reference to
975 : : * about-to-be-dropped entries. Ensure that we're not preventing it from
976 : : * being cleaned up till later.
977 : : *
978 : : * Doing this separately from the dshash iteration below avoids having to
979 : : * do so while holding a partition lock on the shared hashtable.
980 : : */
981 : 46 : pgstat_release_db_entry_refs(dboid);
982 : :
983 : : /* some of the dshash entries are to be removed, take exclusive lock. */
984 : 46 : dshash_seq_init(&hstat, pgStatLocal.shared_hash, true);
985 [ + + ]: 19665 : while ((p = dshash_seq_next(&hstat)) != NULL)
986 : : {
987 [ + + ]: 19619 : if (p->dropped)
988 : 1 : continue;
989 : :
990 [ + + ]: 19618 : if (p->key.dboid != dboid)
991 : 13587 : continue;
992 : :
993 [ - + ]: 6031 : if (!pgstat_drop_entry_internal(p, &hstat))
994 : : {
995 : : /*
996 : : * Even statistics for a dropped database might currently be
997 : : * accessed (consider e.g. database stats for pg_stat_database).
998 : : */
999 : 0 : not_freed_count++;
1000 : : }
1001 : : }
1002 : 46 : dshash_seq_term(&hstat);
1003 : :
1004 : : /*
1005 : : * If some of the stats data could not be freed, signal the reference
1006 : : * holders to run garbage collection of their cached pgStatLocal.shmem.
1007 : : */
1008 [ - + ]: 46 : if (not_freed_count > 0)
1009 : 0 : pgstat_request_entry_refs_gc();
1010 : 46 : }
1011 : :
1012 : : /*
1013 : : * Drop a single stats entry.
1014 : : *
1015 : : * This routine returns false if the stats entry of the dropped object could
1016 : : * not be freed, true otherwise.
1017 : : *
1018 : : * If missing_ok is true, skip entries that have been concurrently dropped.
1019 : : *
1020 : : * The callers of this function should call pgstat_request_entry_refs_gc()
1021 : : * if the stats entry could not be freed, to ensure that this entry's memory
1022 : : * can be reclaimed later by a different backend calling
1023 : : * pgstat_gc_entry_refs().
1024 : : */
1025 : : bool
1026 : 95613 : pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid,
1027 : : bool missing_ok)
1028 : : {
1029 : 95613 : PgStat_HashKey key = {0};
1030 : : PgStatShared_HashEntry *shent;
1031 : 95613 : bool freed = true;
1032 : :
1033 : 95613 : key.kind = kind;
1034 : 95613 : key.dboid = dboid;
1035 : 95613 : key.objid = objid;
1036 : :
1037 : : /* delete local reference */
1038 [ + + ]: 95613 : if (pgStatEntryRefHash)
1039 : : {
1040 : : PgStat_EntryRefHashEntry *lohashent =
1041 : 92065 : pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, key);
1042 : :
1043 [ + + ]: 92065 : if (lohashent)
1044 : 68007 : pgstat_release_entry_ref(lohashent->key, lohashent->entry_ref,
1045 : : true);
1046 : : }
1047 : :
1048 : : /* mark entry in shared hashtable as deleted, drop if possible */
1049 : 95613 : shent = dshash_find(pgStatLocal.shared_hash, &key, true);
1050 [ + + ]: 95613 : if (shent)
1051 : : {
1052 [ - + ]: 68168 : if (shent->dropped)
1053 : : {
1054 [ # # ]: 0 : if (!missing_ok)
1055 [ # # ]: 0 : elog(ERROR,
1056 : : "trying to drop stats entry already dropped: kind=%s dboid=%u objid=%" PRIu64 " refcount=%u generation=%u",
1057 : : pgstat_get_kind_info(shent->key.kind)->name,
1058 : : shent->key.dboid,
1059 : : shent->key.objid,
1060 : : pg_atomic_read_u32(&shent->refcount),
1061 : : pg_atomic_read_u32(&shent->generation));
1062 : 0 : dshash_release_lock(pgStatLocal.shared_hash, shent);
1063 : 0 : return true;
1064 : : }
1065 : :
1066 : 68168 : freed = pgstat_drop_entry_internal(shent, NULL);
1067 : :
1068 : : /*
1069 : : * Database stats contain other stats. Drop those as well when
1070 : : * dropping the database. XXX: Perhaps this should be done in a
1071 : : * slightly more principled way? But not obvious what that'd look
1072 : : * like, and so far this is the only case...
1073 : : */
1074 [ + + ]: 68168 : if (key.kind == PGSTAT_KIND_DATABASE)
1075 : 46 : pgstat_drop_database_and_contents(key.dboid);
1076 : : }
1077 : :
1078 : 95613 : return freed;
1079 : : }
1080 : :
1081 : : /*
1082 : : * Scan through the shared hashtable of stats, dropping statistics if
1083 : : * approved by the optional do_drop() function.
1084 : : */
1085 : : void
1086 : 258 : pgstat_drop_matching_entries(bool (*do_drop) (PgStatShared_HashEntry *, Datum),
1087 : : Datum match_data)
1088 : : {
1089 : : dshash_seq_status hstat;
1090 : : PgStatShared_HashEntry *ps;
1091 : 258 : uint64 not_freed_count = 0;
1092 : :
1093 : : /* entries are removed, take an exclusive lock */
1094 : 258 : dshash_seq_init(&hstat, pgStatLocal.shared_hash, true);
1095 [ + + ]: 314 : while ((ps = dshash_seq_next(&hstat)) != NULL)
1096 : : {
1097 [ - + ]: 56 : if (ps->dropped)
1098 : 0 : continue;
1099 : :
1100 [ - + - - ]: 56 : if (do_drop != NULL && !do_drop(ps, match_data))
1101 : 0 : continue;
1102 : :
1103 : : /* delete local reference */
1104 [ - + ]: 56 : if (pgStatEntryRefHash)
1105 : : {
1106 : : PgStat_EntryRefHashEntry *lohashent =
1107 : 0 : pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, ps->key);
1108 : :
1109 [ # # ]: 0 : if (lohashent)
1110 : 0 : pgstat_release_entry_ref(lohashent->key, lohashent->entry_ref,
1111 : : true);
1112 : : }
1113 : :
1114 [ - + ]: 56 : if (!pgstat_drop_entry_internal(ps, &hstat))
1115 : 0 : not_freed_count++;
1116 : : }
1117 : 258 : dshash_seq_term(&hstat);
1118 : :
1119 [ - + ]: 258 : if (not_freed_count > 0)
1120 : 0 : pgstat_request_entry_refs_gc();
1121 : 258 : }
1122 : :
1123 : : /*
1124 : : * Scan through the shared hashtable of stats and drop all entries.
1125 : : */
1126 : : void
1127 : 258 : pgstat_drop_all_entries(void)
1128 : : {
1129 : 258 : pgstat_drop_matching_entries(NULL, 0);
1130 : 258 : }
1131 : :
1132 : : static void
1133 : 12611 : shared_stat_reset_contents(PgStat_Kind kind, PgStatShared_Common *header,
1134 : : TimestampTz ts)
1135 : : {
1136 : 12611 : const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1137 : :
1138 : 12611 : memset(pgstat_get_entry_data(kind, header), 0,
1139 : : pgstat_get_entry_len(kind));
1140 : :
1141 [ + - ]: 12611 : if (kind_info->reset_timestamp_cb)
1142 : 12611 : kind_info->reset_timestamp_cb(header, ts);
1143 : 12611 : }
1144 : :
1145 : : /*
1146 : : * Reset one variable-numbered stats entry.
1147 : : */
1148 : : void
1149 : 257 : pgstat_reset_entry(PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts)
1150 : : {
1151 : : PgStat_EntryRef *entry_ref;
1152 : :
1153 : : Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1154 : :
1155 : 257 : entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
1156 [ + + - + ]: 257 : if (!entry_ref || entry_ref->shared_entry->dropped)
1157 : 1 : return;
1158 : :
1159 : 256 : (void) pgstat_lock_entry(entry_ref, false);
1160 : 256 : shared_stat_reset_contents(kind, entry_ref->shared_stats, ts);
1161 : 256 : pgstat_unlock_entry(entry_ref);
1162 : : }
1163 : :
1164 : : /*
1165 : : * Scan through the shared hashtable of stats, resetting statistics if
1166 : : * approved by the provided do_reset() function.
1167 : : */
1168 : : void
1169 : 19 : pgstat_reset_matching_entries(bool (*do_reset) (PgStatShared_HashEntry *, Datum),
1170 : : Datum match_data, TimestampTz ts)
1171 : : {
1172 : : dshash_seq_status hstat;
1173 : : PgStatShared_HashEntry *p;
1174 : :
1175 : : /* dshash entry is not modified, take shared lock */
1176 : 19 : dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
1177 [ + + ]: 17674 : while ((p = dshash_seq_next(&hstat)) != NULL)
1178 : : {
1179 : : PgStatShared_Common *header;
1180 : :
1181 [ + + ]: 17655 : if (p->dropped)
1182 : 1 : continue;
1183 : :
1184 [ + + ]: 17654 : if (!do_reset(p, match_data))
1185 : 5299 : continue;
1186 : :
1187 : 12355 : header = dsa_get_address(pgStatLocal.dsa, p->body);
1188 : :
1189 : 12355 : LWLockAcquire(&header->lock, LW_EXCLUSIVE);
1190 : :
1191 : 12355 : shared_stat_reset_contents(p->key.kind, header, ts);
1192 : :
1193 : 12355 : LWLockRelease(&header->lock);
1194 : : }
1195 : 19 : dshash_seq_term(&hstat);
1196 : 19 : }
1197 : :
1198 : : static bool
1199 : 1581 : match_kind(PgStatShared_HashEntry *p, Datum match_data)
1200 : : {
1201 : 1581 : return p->key.kind == DatumGetInt32(match_data);
1202 : : }
1203 : :
1204 : : void
1205 : 4 : pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts)
1206 : : {
1207 : 4 : pgstat_reset_matching_entries(match_kind, Int32GetDatum(kind), ts);
1208 : 4 : }
1209 : :
1210 : : static void
1211 : 4615962 : pgstat_setup_memcxt(void)
1212 : : {
1213 [ + + ]: 4615962 : if (unlikely(!pgStatSharedRefContext))
1214 : 21630 : pgStatSharedRefContext =
1215 : 21630 : AllocSetContextCreate(TopMemoryContext,
1216 : : "PgStat Shared Ref",
1217 : : ALLOCSET_SMALL_SIZES);
1218 [ + + ]: 4615962 : if (unlikely(!pgStatEntryRefHashContext))
1219 : 21630 : pgStatEntryRefHashContext =
1220 : 21630 : AllocSetContextCreate(TopMemoryContext,
1221 : : "PgStat Shared Ref Hash",
1222 : : ALLOCSET_SMALL_SIZES);
1223 : 4615962 : }
|