Debian Codegraph @

src/backend/utils/activity/pgstat_shmem.c

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