PostgreSQL Source Code  git master
pgstat_shmem.c
Go to the documentation of this file.
1 /* -------------------------------------------------------------------------
2  *
3  * pgstat_shmem.c
4  * Storage of stats entries in shared memory
5  *
6  * Copyright (c) 2001-2022, 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 */
25 {
26  PgStat_HashKey key; /* hash key */
27  char status; /* for simplehash use */
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 
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);
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),
68 };
69 
70 
71 /*
72  * Backend local references to shared stats entries. If there are pending
73  * updates to a stats entry, the PgStat_EntryRef is added to the pgStatPending
74  * list.
75  *
76  * When a stats entry is dropped each backend needs to release its reference
77  * to it before the memory can be released. To trigger that
78  * pgStatLocal.shmem->gc_request_count is incremented - which each backend
79  * compares to their copy of pgStatSharedRefAge on a regular basis.
80  */
81 static pgstat_entry_ref_hash_hash *pgStatEntryRefHash = NULL;
82 static int pgStatSharedRefAge = 0; /* cache age of pgStatShmLookupCache */
83 
84 /*
85  * Memory contexts containing the pgStatEntryRefHash table and the
86  * pgStatSharedRef entries respectively. Kept separate to make it easier to
87  * track / attribute memory usage.
88  */
91 
92 
93 /* ------------------------------------------------------------
94  * Public functions called from postmaster follow
95  * ------------------------------------------------------------
96  */
97 
98 /*
99  * The size of the shared memory allocation for stats stored in the shared
100  * stats hash table. This allocation will be done as part of the main shared
101  * memory, rather than dynamic shared memory, allowing it to be initialized in
102  * postmaster.
103  */
104 static Size
106 {
107  Size sz;
108 
109  /*
110  * The dshash header / initial buckets array needs to fit into "plain"
111  * shared memory, but it's beneficial to not need dsm segments
112  * immediately. A size of 256kB seems works well and is not
113  * disproportional compared to other constant sized shared memory
114  * allocations. NB: To avoid DSMs further, the user can configure
115  * min_dynamic_shared_memory.
116  */
117  sz = 256 * 1024;
118  Assert(dsa_minimum_size() <= sz);
119  return MAXALIGN(sz);
120 }
121 
122 /*
123  * Compute shared memory space needed for cumulative statistics
124  */
125 Size
127 {
128  Size sz;
129 
130  sz = MAXALIGN(sizeof(PgStat_ShmemControl));
131  sz = add_size(sz, pgstat_dsa_init_size());
132 
133  return sz;
134 }
135 
136 /*
137  * Initialize cumulative statistics system during startup
138  */
139 void
141 {
142  bool found;
143  Size sz;
144 
145  sz = StatsShmemSize();
147  ShmemInitStruct("Shared Memory Stats", sz, &found);
148 
149  if (!IsUnderPostmaster)
150  {
151  dsa_area *dsa;
152  dshash_table *dsh;
154  char *p = (char *) ctl;
155 
156  Assert(!found);
157 
158  /* the allocation of pgStatLocal.shmem itself */
159  p += MAXALIGN(sizeof(PgStat_ShmemControl));
160 
161  /*
162  * Create a small dsa allocation in plain shared memory. This is
163  * required because postmaster cannot use dsm segments. It also
164  * provides a small efficiency win.
165  */
166  ctl->raw_dsa_area = p;
171  dsa_pin(dsa);
172 
173  /*
174  * To ensure dshash is created in "plain" shared memory, temporarily
175  * limit size of dsa to the initial size of the dsa.
176  */
178 
179  /*
180  * With the limit in place, create the dshash table. XXX: It'd be nice
181  * if there were dshash_create_in_place().
182  */
183  dsh = dshash_create(dsa, &dsh_params, 0);
185 
186  /* lift limit set above */
187  dsa_set_size_limit(dsa, -1);
188 
189  /*
190  * Postmaster will never access these again, thus free the local
191  * dsa/dshash references.
192  */
193  dshash_detach(dsh);
194  dsa_detach(dsa);
195 
197 
198 
199  /* initialize fixed-numbered stats */
205  }
206  else
207  {
208  Assert(found);
209  }
210 }
211 
212 void
214 {
215  MemoryContext oldcontext;
216 
217  Assert(pgStatLocal.dsa == NULL);
218 
219  /* stats shared memory persists for the backend lifetime */
221 
223  NULL);
225 
228 
229  MemoryContextSwitchTo(oldcontext);
230 }
231 
232 void
234 {
236 
237  /* we shouldn't leave references to shared stats */
239 
241  pgStatLocal.shared_hash = NULL;
242 
244  pgStatLocal.dsa = NULL;
245 }
246 
247 
248 /* ------------------------------------------------------------
249  * Maintenance of shared memory stats entries
250  * ------------------------------------------------------------
251  */
252 
255  PgStatShared_HashEntry *shhashent)
256 {
257  /* Create new stats entry. */
258  dsa_pointer chunk;
259  PgStatShared_Common *shheader;
260 
261  /*
262  * Initialize refcount to 1, marking it as valid / not dropped. The entry
263  * can't be freed before the initialization because it can't be found as
264  * long as we hold the dshash partition lock. Caller needs to increase
265  * further if a longer lived reference is needed.
266  */
267  pg_atomic_init_u32(&shhashent->refcount, 1);
268  shhashent->dropped = false;
269 
270  chunk = dsa_allocate0(pgStatLocal.dsa, pgstat_get_kind_info(kind)->shared_size);
271  shheader = dsa_get_address(pgStatLocal.dsa, chunk);
272  shheader->magic = 0xdeadbeef;
273 
274  /* Link the new entry from the hash entry. */
275  shhashent->body = chunk;
276 
278 
279  return shheader;
280 }
281 
282 static PgStatShared_Common *
284 {
285  PgStatShared_Common *shheader;
286 
287  shheader = dsa_get_address(pgStatLocal.dsa, shhashent->body);
288 
289  /* mark as not dropped anymore */
290  pg_atomic_fetch_add_u32(&shhashent->refcount, 1);
291  shhashent->dropped = false;
292 
293  /* reinitialize content */
294  Assert(shheader->magic == 0xdeadbeef);
295  memset(pgstat_get_entry_data(kind, shheader), 0,
296  pgstat_get_entry_len(kind));
297 
298  return shheader;
299 }
300 
301 static void
303 {
304  if (likely(pgStatEntryRefHash != NULL))
305  return;
306 
308  pgstat_entry_ref_hash_create(pgStatEntryRefHashContext,
312 }
313 
314 /*
315  * Helper function for pgstat_get_entry_ref().
316  */
317 static void
319  PgStatShared_HashEntry *shhashent,
320  PgStatShared_Common *shheader)
321 {
322  Assert(shheader->magic == 0xdeadbeef);
323  Assert(pg_atomic_read_u32(&shhashent->refcount) > 0);
324 
325  pg_atomic_fetch_add_u32(&shhashent->refcount, 1);
326 
328 
329  entry_ref->shared_stats = shheader;
330  entry_ref->shared_entry = shhashent;
331 }
332 
333 /*
334  * Helper function for pgstat_get_entry_ref().
335  */
336 static bool
338 {
339  bool found;
340  PgStat_EntryRefHashEntry *cache_entry;
341 
342  /*
343  * We immediately insert a cache entry, because it avoids 1) multiple
344  * hashtable lookups in case of a cache miss 2) having to deal with
345  * out-of-memory errors after incrementing PgStatShared_Common->refcount.
346  */
347 
348  cache_entry = pgstat_entry_ref_hash_insert(pgStatEntryRefHash, key, &found);
349 
350  if (!found || !cache_entry->entry_ref)
351  {
352  PgStat_EntryRef *entry_ref;
353 
354  cache_entry->entry_ref = entry_ref =
356  sizeof(PgStat_EntryRef));
357  entry_ref->shared_stats = NULL;
358  entry_ref->shared_entry = NULL;
359  entry_ref->pending = NULL;
360 
361  found = false;
362  }
363  else if (cache_entry->entry_ref->shared_stats == NULL)
364  {
365  Assert(cache_entry->entry_ref->pending == NULL);
366  found = false;
367  }
368  else
369  {
371 
372  entry_ref = cache_entry->entry_ref;
373  Assert(entry_ref->shared_entry != NULL);
374  Assert(entry_ref->shared_stats != NULL);
375 
376  Assert(entry_ref->shared_stats->magic == 0xdeadbeef);
377  /* should have at least our reference */
378  Assert(pg_atomic_read_u32(&entry_ref->shared_entry->refcount) > 0);
379  }
380 
381  *entry_ref_p = cache_entry->entry_ref;
382  return found;
383 }
384 
385 /*
386  * Get a shared stats reference. If create is true, the shared stats object is
387  * created if it does not exist.
388  *
389  * When create is true, and created_entry is non-NULL, it'll be set to true
390  * if the entry is newly created, false otherwise.
391  */
393 pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create,
394  bool *created_entry)
395 {
396  PgStat_HashKey key = {.kind = kind,.dboid = dboid,.objoid = objoid};
397  PgStatShared_HashEntry *shhashent;
398  PgStatShared_Common *shheader = NULL;
399  PgStat_EntryRef *entry_ref;
400 
401  /*
402  * passing in created_entry only makes sense if we possibly could create
403  * entry.
404  */
405  Assert(create || created_entry == NULL);
407  Assert(pgStatLocal.shared_hash != NULL);
409 
412 
413  if (created_entry != NULL)
414  *created_entry = false;
415 
416  /*
417  * Check if other backends dropped stats that could not be deleted because
418  * somebody held references to it. If so, check this backend's references.
419  * This is not expected to happen often. The location of the check is a
420  * bit random, but this is a relatively frequently called path, so better
421  * than most.
422  */
425 
426  /*
427  * First check the lookup cache hashtable in local memory. If we find a
428  * match here we can avoid taking locks / causing contention.
429  */
430  if (pgstat_get_entry_ref_cached(key, &entry_ref))
431  return entry_ref;
432 
433  Assert(entry_ref != NULL);
434 
435  /*
436  * Do a lookup in the hash table first - it's quite likely that the entry
437  * already exists, and that way we only need a shared lock.
438  */
439  shhashent = dshash_find(pgStatLocal.shared_hash, &key, false);
440 
441  if (create && !shhashent)
442  {
443  bool shfound;
444 
445  /*
446  * It's possible that somebody created the entry since the above
447  * lookup. If so, fall through to the same path as if we'd have if it
448  * already had been created before the dshash_find() calls.
449  */
450  shhashent = dshash_find_or_insert(pgStatLocal.shared_hash, &key, &shfound);
451  if (!shfound)
452  {
453  shheader = pgstat_init_entry(kind, shhashent);
454  pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
455 
456  if (created_entry != NULL)
457  *created_entry = true;
458 
459  return entry_ref;
460  }
461  }
462 
463  if (!shhashent)
464  {
465  /*
466  * If we're not creating, delete the reference again. In all
467  * likelihood it's just a stats lookup - no point wasting memory for a
468  * shared ref to nothing...
469  */
470  pgstat_release_entry_ref(key, entry_ref, false);
471 
472  return NULL;
473  }
474  else
475  {
476  /*
477  * Can get here either because dshash_find() found a match, or if
478  * dshash_find_or_insert() found a concurrently inserted entry.
479  */
480 
481  if (shhashent->dropped && create)
482  {
483  /*
484  * There are legitimate cases where the old stats entry might not
485  * yet have been dropped by the time it's reused. The most obvious
486  * case are replication slot stats, where a new slot can be
487  * created with the same index just after dropping. But oid
488  * wraparound can lead to other cases as well. We just reset the
489  * stats to their plain state.
490  */
491  shheader = pgstat_reinit_entry(kind, shhashent);
492  pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
493 
494  if (created_entry != NULL)
495  *created_entry = true;
496 
497  return entry_ref;
498  }
499  else if (shhashent->dropped)
500  {
502  pgstat_release_entry_ref(key, entry_ref, false);
503 
504  return NULL;
505  }
506  else
507  {
508  shheader = dsa_get_address(pgStatLocal.dsa, shhashent->body);
509  pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
510 
511  return entry_ref;
512  }
513  }
514 }
515 
516 static void
518  bool discard_pending)
519 {
520  if (entry_ref && entry_ref->pending)
521  {
522  if (discard_pending)
523  pgstat_delete_pending_entry(entry_ref);
524  else
525  elog(ERROR, "releasing ref with pending data");
526  }
527 
528  if (entry_ref && entry_ref->shared_stats)
529  {
530  Assert(entry_ref->shared_stats->magic == 0xdeadbeef);
531  Assert(entry_ref->pending == NULL);
532 
533  /*
534  * This can't race with another backend looking up the stats entry and
535  * increasing the refcount because it is not "legal" to create
536  * additional references to dropped entries.
537  */
538  if (pg_atomic_fetch_sub_u32(&entry_ref->shared_entry->refcount, 1) == 1)
539  {
540  PgStatShared_HashEntry *shent;
541 
542  /*
543  * We're the last referrer to this entry, try to drop the shared
544  * entry.
545  */
546 
547  /* only dropped entries can reach a 0 refcount */
548  Assert(entry_ref->shared_entry->dropped);
549 
551  &entry_ref->shared_entry->key,
552  true);
553  if (!shent)
554  elog(ERROR, "could not find just referenced shared stats entry");
555 
556  Assert(pg_atomic_read_u32(&entry_ref->shared_entry->refcount) == 0);
557  Assert(entry_ref->shared_entry == shent);
558 
559  pgstat_free_entry(shent, NULL);
560  }
561  }
562 
563  if (!pgstat_entry_ref_hash_delete(pgStatEntryRefHash, key))
564  elog(ERROR, "entry ref vanished before deletion");
565 
566  if (entry_ref)
567  pfree(entry_ref);
568 }
569 
570 bool
571 pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
572 {
573  LWLock *lock = &entry_ref->shared_stats->lock;
574 
575  if (nowait)
577 
579  return true;
580 }
581 
582 /*
583  * Separate from pgstat_lock_entry() as most callers will need to lock
584  * exclusively.
585  */
586 bool
588 {
589  LWLock *lock = &entry_ref->shared_stats->lock;
590 
591  if (nowait)
592  return LWLockConditionalAcquire(lock, LW_SHARED);
593 
594  LWLockAcquire(lock, LW_SHARED);
595  return true;
596 }
597 
598 void
600 {
601  LWLockRelease(&entry_ref->shared_stats->lock);
602 }
603 
604 /*
605  * Helper function to fetch and lock shared stats.
606  */
609  bool nowait)
610 {
611  PgStat_EntryRef *entry_ref;
612 
613  /* find shared table stats entry corresponding to the local entry */
614  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, true, NULL);
615 
616  /* lock the shared entry to protect the content, skip if failed */
617  if (!pgstat_lock_entry(entry_ref, nowait))
618  return NULL;
619 
620  return entry_ref;
621 }
622 
623 void
625 {
627 }
628 
629 static bool
631 {
632  uint64 curage;
633 
634  if (!pgStatEntryRefHash)
635  return false;
636 
637  /* should have been initialized when creating pgStatEntryRefHash */
639 
641 
642  return pgStatSharedRefAge != curage;
643 }
644 
645 static void
647 {
648  pgstat_entry_ref_hash_iterator i;
650  uint64 curage;
651 
653  Assert(curage != 0);
654 
655  /*
656  * Some entries have been dropped. Invalidate cache pointer to them.
657  */
658  pgstat_entry_ref_hash_start_iterate(pgStatEntryRefHash, &i);
659  while ((ent = pgstat_entry_ref_hash_iterate(pgStatEntryRefHash, &i)) != NULL)
660  {
661  PgStat_EntryRef *entry_ref = ent->entry_ref;
662 
663  Assert(!entry_ref->shared_stats ||
664  entry_ref->shared_stats->magic == 0xdeadbeef);
665 
666  if (!entry_ref->shared_entry->dropped)
667  continue;
668 
669  /* cannot gc shared ref that has pending data */
670  if (entry_ref->pending != NULL)
671  continue;
672 
673  pgstat_release_entry_ref(ent->key, entry_ref, false);
674  }
675 
676  pgStatSharedRefAge = curage;
677 }
678 
679 static void
681  Datum match_data)
682 {
683  pgstat_entry_ref_hash_iterator i;
685 
686  if (pgStatEntryRefHash == NULL)
687  return;
688 
689  pgstat_entry_ref_hash_start_iterate(pgStatEntryRefHash, &i);
690 
691  while ((ent = pgstat_entry_ref_hash_iterate(pgStatEntryRefHash, &i))
692  != NULL)
693  {
694  Assert(ent->entry_ref != NULL);
695 
696  if (match && !match(ent, match_data))
697  continue;
698 
699  pgstat_release_entry_ref(ent->key, ent->entry_ref, discard_pending);
700  }
701 }
702 
703 /*
704  * Release all local references to shared stats entries.
705  *
706  * When a process exits it cannot do so while still holding references onto
707  * stats entries, otherwise the shared stats entries could never be freed.
708  */
709 static void
710 pgstat_release_all_entry_refs(bool discard_pending)
711 {
712  if (pgStatEntryRefHash == NULL)
713  return;
714 
715  pgstat_release_matching_entry_refs(discard_pending, NULL, 0);
716  Assert(pgStatEntryRefHash->members == 0);
717  pgstat_entry_ref_hash_destroy(pgStatEntryRefHash);
718  pgStatEntryRefHash = NULL;
719 }
720 
721 static bool
723 {
724  Oid dboid = DatumGetObjectId(match_data);
725 
726  return ent->key.dboid == dboid;
727 }
728 
729 static void
731 {
732  pgstat_release_matching_entry_refs( /* discard pending = */ true,
733  match_db,
734  ObjectIdGetDatum(dboid));
735 }
736 
737 
738 /* ------------------------------------------------------------
739  * Dropping and resetting of stats entries
740  * ------------------------------------------------------------
741  */
742 
743 static void
745 {
746  dsa_pointer pdsa;
747 
748  /*
749  * Fetch dsa pointer before deleting entry - that way we can free the
750  * memory after releasing the lock.
751  */
752  pdsa = shent->body;
753 
754  if (!hstat)
756  else
757  dshash_delete_current(hstat);
758 
759  dsa_free(pgStatLocal.dsa, pdsa);
760 }
761 
762 /*
763  * Helper for both pgstat_drop_database_and_contents() and
764  * pgstat_drop_entry(). If hstat is non-null delete the shared entry using
765  * dshash_delete_current(), otherwise use dshash_delete_entry(). In either
766  * case the entry needs to be already locked.
767  */
768 static bool
770  dshash_seq_status *hstat)
771 {
772  Assert(shent->body != InvalidDsaPointer);
773 
774  /* should already have released local reference */
775  if (pgStatEntryRefHash)
776  Assert(!pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, shent->key));
777 
778  /*
779  * Signal that the entry is dropped - this will eventually cause other
780  * backends to release their references.
781  */
782  if (shent->dropped)
783  elog(ERROR, "can only drop stats once");
784  shent->dropped = true;
785 
786  /* release refcount marking entry as not dropped */
787  if (pg_atomic_sub_fetch_u32(&shent->refcount, 1) == 0)
788  {
789  pgstat_free_entry(shent, hstat);
790  return true;
791  }
792  else
793  {
794  if (!hstat)
796  return false;
797  }
798 }
799 
800 /*
801  * Drop stats for the database and all the objects inside that database.
802  */
803 static void
805 {
806  dshash_seq_status hstat;
808  uint64 not_freed_count = 0;
809 
810  Assert(OidIsValid(dboid));
811 
812  Assert(pgStatLocal.shared_hash != NULL);
813 
814  /*
815  * This backend might very well be the only backend holding a reference to
816  * about-to-be-dropped entries. Ensure that we're not preventing it from
817  * being cleaned up till later.
818  *
819  * Doing this separately from the dshash iteration below avoids having to
820  * do so while holding a partition lock on the shared hashtable.
821  */
823 
824  /* some of the dshash entries are to be removed, take exclusive lock. */
825  dshash_seq_init(&hstat, pgStatLocal.shared_hash, true);
826  while ((p = dshash_seq_next(&hstat)) != NULL)
827  {
828  if (p->dropped)
829  continue;
830 
831  if (p->key.dboid != dboid)
832  continue;
833 
834  if (!pgstat_drop_entry_internal(p, &hstat))
835  {
836  /*
837  * Even statistics for a dropped database might currently be
838  * accessed (consider e.g. database stats for pg_stat_database).
839  */
840  not_freed_count++;
841  }
842  }
843  dshash_seq_term(&hstat);
844 
845  /*
846  * If some of the stats data could not be freed, signal the reference
847  * holders to run garbage collection of their cached pgStatShmLookupCache.
848  */
849  if (not_freed_count > 0)
851 }
852 
853 bool
854 pgstat_drop_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
855 {
856  PgStat_HashKey key = {.kind = kind,.dboid = dboid,.objoid = objoid};
857  PgStatShared_HashEntry *shent;
858  bool freed = true;
859 
860  /* delete local reference */
861  if (pgStatEntryRefHash)
862  {
863  PgStat_EntryRefHashEntry *lohashent =
864  pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, key);
865 
866  if (lohashent)
867  pgstat_release_entry_ref(lohashent->key, lohashent->entry_ref,
868  true);
869  }
870 
871  /* mark entry in shared hashtable as deleted, drop if possible */
872  shent = dshash_find(pgStatLocal.shared_hash, &key, true);
873  if (shent)
874  {
875  freed = pgstat_drop_entry_internal(shent, NULL);
876 
877  /*
878  * Database stats contain other stats. Drop those as well when
879  * dropping the database. XXX: Perhaps this should be done in a
880  * slightly more principled way? But not obvious what that'd look
881  * like, and so far this is the only case...
882  */
883  if (key.kind == PGSTAT_KIND_DATABASE)
885  }
886 
887  return freed;
888 }
889 
890 void
892 {
893  dshash_seq_status hstat;
895  uint64 not_freed_count = 0;
896 
897  dshash_seq_init(&hstat, pgStatLocal.shared_hash, true);
898  while ((ps = dshash_seq_next(&hstat)) != NULL)
899  {
900  if (ps->dropped)
901  continue;
902 
903  if (!pgstat_drop_entry_internal(ps, &hstat))
904  not_freed_count++;
905  }
906  dshash_seq_term(&hstat);
907 
908  if (not_freed_count > 0)
910 }
911 
912 static void
914  TimestampTz ts)
915 {
916  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
917 
918  memset(pgstat_get_entry_data(kind, header), 0,
919  pgstat_get_entry_len(kind));
920 
921  if (kind_info->reset_timestamp_cb)
922  kind_info->reset_timestamp_cb(header, ts);
923 }
924 
925 /*
926  * Reset one variable-numbered stats entry.
927  */
928 void
930 {
931  PgStat_EntryRef *entry_ref;
932 
933  Assert(!pgstat_get_kind_info(kind)->fixed_amount);
934 
935  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
936  if (!entry_ref || entry_ref->shared_entry->dropped)
937  return;
938 
939  (void) pgstat_lock_entry(entry_ref, false);
940  shared_stat_reset_contents(kind, entry_ref->shared_stats, ts);
941  pgstat_unlock_entry(entry_ref);
942 }
943 
944 /*
945  * Scan through the shared hashtable of stats, resetting statistics if
946  * approved by the provided do_reset() function.
947  */
948 void
950  Datum match_data, TimestampTz ts)
951 {
952  dshash_seq_status hstat;
954 
955  /* dshash entry is not modified, take shared lock */
956  dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
957  while ((p = dshash_seq_next(&hstat)) != NULL)
958  {
960 
961  if (p->dropped)
962  continue;
963 
964  if (!do_reset(p, match_data))
965  continue;
966 
968 
970 
972 
973  LWLockRelease(&header->lock);
974  }
975  dshash_seq_term(&hstat);
976 }
977 
978 static bool
980 {
981  return p->key.kind == DatumGetInt32(match_data);
982 }
983 
984 void
986 {
988 }
989 
990 static void
992 {
996  "PgStat Shared Ref",
1001  "PgStat Shared Ref Hash",
1003 }
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:396
static uint32 pg_atomic_fetch_sub_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:338
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:218
static uint32 pg_atomic_fetch_add_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:323
static uint64 pg_atomic_fetch_add_u64(volatile pg_atomic_uint64 *ptr, int64 add_)
Definition: atomics.h:462
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:236
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:410
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:424
#define likely(x)
Definition: c.h:294
#define MAXALIGN(LEN)
Definition: c.h:747
unsigned char bool
Definition: c.h:392
#define unlikely(x)
Definition: c.h:295
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:166
#define OidIsValid(objectId)
Definition: c.h:711
size_t Size
Definition: c.h:541
int64 TimestampTz
Definition: timestamp.h:39
dsa_area * dsa_create_in_place(void *place, size_t size, int tranche_id, dsm_segment *segment)
Definition: dsa.c:481
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:944
void dsa_set_size_limit(dsa_area *area, size_t limit)
Definition: dsa.c:1020
void dsa_pin_mapping(dsa_area *area)
Definition: dsa.c:643
void dsa_detach(dsa_area *area)
Definition: dsa.c:1922
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition: dsa.c:553
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:832
size_t dsa_minimum_size(void)
Definition: dsa.c:1181
void dsa_pin(dsa_area *area)
Definition: dsa.c:977
#define dsa_allocate0(area, size)
Definition: dsa.h:88
uint64 dsa_pointer
Definition: dsa.h:62
#define InvalidDsaPointer
Definition: dsa.h:78
void dshash_delete_entry(dshash_table *hash_table, void *entry)
Definition: dshash.c:541
void dshash_release_lock(dshash_table *hash_table, void *entry)
Definition: dshash.c:558
void dshash_detach(dshash_table *hash_table)
Definition: dshash.c:307
void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table, bool exclusive)
Definition: dshash.c:595
void * dshash_find(dshash_table *hash_table, const void *key, bool exclusive)
Definition: dshash.c:390
dshash_table_handle dshash_get_hash_table_handle(dshash_table *hash_table)
Definition: dshash.c:367
void dshash_seq_term(dshash_seq_status *status)
Definition: dshash.c:704
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
Definition: dshash.c:433
dshash_table * dshash_attach(dsa_area *area, const dshash_parameters *params, dshash_table_handle handle, void *arg)
Definition: dshash.c:270
dshash_table * dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
Definition: dshash.c:206
void * dshash_seq_next(dshash_seq_status *status)
Definition: dshash.c:614
void dshash_delete_current(dshash_seq_status *status)
Definition: dshash.c:714
#define ERROR
Definition: elog.h:35
bool IsUnderPostmaster
Definition: globals.c:113
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1194
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1802
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:729
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1365
@ LWTRANCHE_PGSTATS_HASH
Definition: lwlock.h:202
@ LWTRANCHE_PGSTATS_DSA
Definition: lwlock.h:201
@ LWTRANCHE_PGSTATS_DATA
Definition: lwlock.h:203
@ LW_SHARED
Definition: lwlock.h:113
@ LW_EXCLUSIVE
Definition: lwlock.h:112
void pfree(void *pointer)
Definition: mcxt.c:1306
MemoryContext TopMemoryContext
Definition: mcxt.c:130
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:994
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:163
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:135
const void * data
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:208
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1220
void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat.c:1113
PgStat_LocalState pgStatLocal
Definition: pgstat.c:195
PgStat_Kind
Definition: pgstat.h:36
@ PGSTAT_KIND_DATABASE
Definition: pgstat.h:41
static uint32 pgstat_hash_hash_key(const void *d, size_t size, void *arg)
static int pgstat_cmp_hash_key(const void *a, const void *b, size_t size, void *arg)
static void * pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry)
#define pgstat_assert_is_up()
struct PgStat_HashKey PgStat_HashKey
static size_t pgstat_get_entry_len(PgStat_Kind kind)
static bool match_db(PgStat_EntryRefHashEntry *ent, Datum match_data)
Definition: pgstat_shmem.c:722
static void pgstat_setup_memcxt(void)
Definition: pgstat_shmem.c:991
void pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts)
Definition: pgstat_shmem.c:985
void pgstat_request_entry_refs_gc(void)
Definition: pgstat_shmem.c:624
static void pgstat_release_db_entry_refs(Oid dboid)
Definition: pgstat_shmem.c:730
PgStatShared_Common * pgstat_init_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
Definition: pgstat_shmem.c:254
static Size pgstat_dsa_init_size(void)
Definition: pgstat_shmem.c:105
static bool match_kind(PgStatShared_HashEntry *p, Datum match_data)
Definition: pgstat_shmem.c:979
static MemoryContext pgStatEntryRefHashContext
Definition: pgstat_shmem.c:90
void StatsShmemInit(void)
Definition: pgstat_shmem.c:140
bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:587
void pgstat_attach_shmem(void)
Definition: pgstat_shmem.c:213
static void pgstat_free_entry(PgStatShared_HashEntry *shent, dshash_seq_status *hstat)
Definition: pgstat_shmem.c:744
static int pgStatSharedRefAge
Definition: pgstat_shmem.c:82
#define PGSTAT_ENTRY_REF_HASH_SIZE
Definition: pgstat_shmem.c:21
static void shared_stat_reset_contents(PgStat_Kind kind, PgStatShared_Common *header, TimestampTz ts)
Definition: pgstat_shmem.c:913
static void pgstat_setup_shared_refs(void)
Definition: pgstat_shmem.c:302
static void pgstat_release_entry_ref(PgStat_HashKey key, PgStat_EntryRef *entry_ref, bool discard_pending)
Definition: pgstat_shmem.c:517
static void pgstat_drop_database_and_contents(Oid dboid)
Definition: pgstat_shmem.c:804
static PgStatShared_Common * pgstat_reinit_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
Definition: pgstat_shmem.c:283
static pgstat_entry_ref_hash_hash * pgStatEntryRefHash
Definition: pgstat_shmem.c:81
void pgstat_reset_entry(PgStat_Kind kind, Oid dboid, Oid objoid, TimestampTz ts)
Definition: pgstat_shmem.c:929
Size StatsShmemSize(void)
Definition: pgstat_shmem.c:126
static void pgstat_acquire_entry_ref(PgStat_EntryRef *entry_ref, PgStatShared_HashEntry *shhashent, PgStatShared_Common *shheader)
Definition: pgstat_shmem.c:318
static MemoryContext pgStatSharedRefContext
Definition: pgstat_shmem.c:89
void pgstat_reset_matching_entries(bool(*do_reset)(PgStatShared_HashEntry *, Datum), Datum match_data, TimestampTz ts)
Definition: pgstat_shmem.c:949
void pgstat_drop_all_entries(void)
Definition: pgstat_shmem.c:891
static const dshash_parameters dsh_params
Definition: pgstat_shmem.c:62
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:393
static bool pgstat_get_entry_ref_cached(PgStat_HashKey key, PgStat_EntryRef **entry_ref_p)
Definition: pgstat_shmem.c:337
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:599
static void pgstat_release_all_entry_refs(bool discard_pending)
Definition: pgstat_shmem.c:710
struct PgStat_EntryRefHashEntry PgStat_EntryRefHashEntry
static void pgstat_release_matching_entry_refs(bool discard_pending, ReleaseMatchCB match, Datum match_data)
Definition: pgstat_shmem.c:680
bool(* ReleaseMatchCB)(PgStat_EntryRefHashEntry *, Datum data)
Definition: pgstat_shmem.c:55
bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat_shmem.c:854
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:571
static void pgstat_gc_entry_refs(void)
Definition: pgstat_shmem.c:646
static bool pgstat_need_entry_refs_gc(void)
Definition: pgstat_shmem.c:630
static bool pgstat_drop_entry_internal(PgStatShared_HashEntry *shent, dshash_seq_status *hstat)
Definition: pgstat_shmem.c:769
void pgstat_detach_shmem(void)
Definition: pgstat_shmem.c:233
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, Oid objoid, bool nowait)
Definition: pgstat_shmem.c:608
uintptr_t Datum
Definition: postgres.h:412
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:590
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:600
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:560
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:550
unsigned int Oid
Definition: postgres_ext.h:31
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
Definition: lwlock.h:40
pg_atomic_uint32 refcount
PgStat_EntryRef * entry_ref
Definition: pgstat_shmem.c:28
PgStatShared_Common * shared_stats
PgStatShared_HashEntry * shared_entry
PgStat_Kind kind
void(* reset_timestamp_cb)(PgStatShared_Common *header, TimestampTz ts)
dshash_table * shared_hash
PgStat_ShmemControl * shmem
dshash_table_handle hash_handle
PgStatShared_SLRU slru
pg_atomic_uint64 gc_request_count
PgStatShared_Wal wal
PgStatShared_Archiver archiver
PgStatShared_Checkpointer checkpointer
PgStatShared_BgWriter bgwriter
Definition: dsa.c:367