PostgreSQL Source Code  git master
pgstat.c
Go to the documentation of this file.
1 /* ----------
2  * pgstat.c
3  * Infrastructure for the cumulative statistics system.
4  *
5  * The cumulative statistics system accumulates statistics for different kinds
6  * of objects. Some kinds of statistics are collected for a fixed number of
7  * objects (most commonly 1), e.g., checkpointer statistics. Other kinds of
8  * statistics are collected for a varying number of objects
9  * (e.g. relations). See PgStat_KindInfo for a list of currently handled
10  * statistics.
11  *
12  * Statistics are loaded from the filesystem during startup (by the startup
13  * process), unless preceded by a crash, in which case all stats are
14  * discarded. They are written out by the checkpointer process just before
15  * shutting down, except when shutting down in immediate mode.
16  *
17  * Fixed-numbered stats are stored in plain (non-dynamic) shared memory.
18  *
19  * Statistics for variable-numbered objects are stored in dynamic shared
20  * memory and can be found via a dshash hashtable. The statistics counters are
21  * not part of the dshash entry (PgStatShared_HashEntry) directly, but are
22  * separately allocated (PgStatShared_HashEntry->body). The separate
23  * allocation allows different kinds of statistics to be stored in the same
24  * hashtable without wasting space in PgStatShared_HashEntry.
25  *
26  * Variable-numbered stats are addressed by PgStat_HashKey while running. It
27  * is not possible to have statistics for an object that cannot be addressed
28  * that way at runtime. A wider identifier can be used when serializing to
29  * disk (used for replication slot stats).
30  *
31  * To avoid contention on the shared hashtable, each backend has a
32  * backend-local hashtable (pgStatEntryRefHash) in front of the shared
33  * hashtable, containing references (PgStat_EntryRef) to shared hashtable
34  * entries. The shared hashtable only needs to be accessed when no prior
35  * reference is found in the local hashtable. Besides pointing to the the
36  * shared hashtable entry (PgStatShared_HashEntry) PgStat_EntryRef also
37  * contains a pointer to the the shared statistics data, as a process-local
38  * address, to reduce access costs.
39  *
40  * The names for structs stored in shared memory are prefixed with
41  * PgStatShared instead of PgStat. Each stats entry in shared memory is
42  * protected by a dedicated lwlock.
43  *
44  * Most stats updates are first accumulated locally in each process as pending
45  * entries, then later flushed to shared memory (just after commit, or by
46  * idle-timeout). This practically eliminates contention on individual stats
47  * entries. For most kinds of variable-numbered pending stats data is stored
48  * in PgStat_EntryRef->pending. All entries with pending data are in the
49  * pgStatPending list. Pending statistics updates are flushed out by
50  * pgstat_report_stat().
51  *
52  * The behavior of different kinds of statistics is determined by the kind's
53  * entry in pgstat_kind_infos, see PgStat_KindInfo for details.
54  *
55  * The consistency of read accesses to statistics can be configured using the
56  * stats_fetch_consistency GUC (see config.sgml and monitoring.sgml for the
57  * settings). When using PGSTAT_FETCH_CONSISTENCY_CACHE or
58  * PGSTAT_FETCH_CONSISTENCY_SNAPSHOT statistics are stored in
59  * pgStatLocal.snapshot.
60  *
61  * To keep things manageable, stats handling is split across several
62  * files. Infrastructure pieces are in:
63  * - pgstat.c - this file, to tie it all together
64  * - pgstat_shmem.c - nearly everything dealing with shared memory, including
65  * the maintenance of hashtable entries
66  * - pgstat_xact.c - transactional integration, including the transactional
67  * creation and dropping of stats entries
68  *
69  * Each statistics kind is handled in a dedicated file:
70  * - pgstat_archiver.c
71  * - pgstat_bgwriter.c
72  * - pgstat_checkpointer.c
73  * - pgstat_database.c
74  * - pgstat_function.c
75  * - pgstat_relation.c
76  * - pgstat_slru.c
77  * - pgstat_subscription.c
78  * - pgstat_wal.c
79  *
80  * Whenever possible infrastructure files should not contain code related to
81  * specific kinds of stats.
82  *
83  *
84  * Copyright (c) 2001-2022, PostgreSQL Global Development Group
85  *
86  * IDENTIFICATION
87  * src/backend/utils/activity/pgstat.c
88  * ----------
89  */
90 #include "postgres.h"
91 
92 #include <unistd.h>
93 
94 #include "access/transam.h"
95 #include "access/xact.h"
96 #include "lib/dshash.h"
97 #include "pgstat.h"
98 #include "port/atomics.h"
99 #include "storage/fd.h"
100 #include "storage/ipc.h"
101 #include "storage/lwlock.h"
102 #include "storage/pg_shmem.h"
103 #include "storage/shmem.h"
104 #include "utils/guc.h"
105 #include "utils/memutils.h"
106 #include "utils/pgstat_internal.h"
107 #include "utils/timestamp.h"
108 
109 
110 /* ----------
111  * Timer definitions.
112  *
113  * In milliseconds.
114  * ----------
115  */
116 
117 /* minimum interval non-forced stats flushes.*/
118 #define PGSTAT_MIN_INTERVAL 1000
119 /* how long until to block flushing pending stats updates */
120 #define PGSTAT_MAX_INTERVAL 60000
121 /* when to call pgstat_report_stat() again, even when idle */
122 #define PGSTAT_IDLE_INTERVAL 10000
123 
124 /* ----------
125  * Initial size hints for the hash tables used in statistics.
126  * ----------
127  */
128 
129 #define PGSTAT_SNAPSHOT_HASH_SIZE 512
130 
131 
132 /* hash table for statistics snapshots entry */
133 typedef struct PgStat_SnapshotEntry
134 {
136  char status; /* for simplehash use */
137  void *data; /* the stats data itself */
139 
140 
141 /* ----------
142  * Backend-local Hash Table Definitions
143  * ----------
144  */
145 
146 /* for stats snapshot entries */
147 #define SH_PREFIX pgstat_snapshot
148 #define SH_ELEMENT_TYPE PgStat_SnapshotEntry
149 #define SH_KEY_TYPE PgStat_HashKey
150 #define SH_KEY key
151 #define SH_HASH_KEY(tb, key) \
152  pgstat_hash_hash_key(&key, sizeof(PgStat_HashKey), NULL)
153 #define SH_EQUAL(tb, a, b) \
154  pgstat_cmp_hash_key(&a, &b, sizeof(PgStat_HashKey), NULL) == 0
155 #define SH_SCOPE static inline
156 #define SH_DEFINE
157 #define SH_DECLARE
158 #include "lib/simplehash.h"
159 
160 
161 /* ----------
162  * Local function forward declarations
163  * ----------
164  */
165 
166 static void pgstat_write_statsfile(void);
167 static void pgstat_read_statsfile(void);
168 
169 static void pgstat_reset_after_failure(void);
170 
171 static bool pgstat_flush_pending_entries(bool nowait);
172 
173 static void pgstat_prep_snapshot(void);
174 static void pgstat_build_snapshot(void);
175 static void pgstat_build_snapshot_fixed(PgStat_Kind kind);
176 
177 static inline bool pgstat_is_kind_valid(int ikind);
178 
179 
180 /* ----------
181  * GUC parameters
182  * ----------
183  */
184 
185 bool pgstat_track_counts = false;
187 
188 
189 /* ----------
190  * state shared with pgstat_*.c
191  * ----------
192  */
193 
195 
196 
197 /* ----------
198  * Local data
199  *
200  * NB: There should be only variables related to stats infrastructure here,
201  * not for specific kinds of stats.
202  * ----------
203  */
204 
205 /*
206  * Memory contexts containing the pgStatEntryRefHash table, the
207  * pgStatSharedRef entries, and pending data respectively. Mostly to make it
208  * easier to track / attribute memory usage.
209  */
210 
212 
213 /*
214  * Backend local list of PgStat_EntryRef with unflushed pending stats.
215  *
216  * Newly pending entries should only ever be added to the end of the list,
217  * otherwise pgstat_flush_pending_entries() might not see them immediately.
218  */
220 
221 
222 /*
223  * Force the next stats flush to happen regardless of
224  * PGSTAT_MIN_INTERVAL. Useful in test scripts.
225  */
226 static bool pgStatForceNextFlush = false;
227 
228 /*
229  * For assertions that check pgstat is not used before initialization / after
230  * shutdown.
231  */
232 #ifdef USE_ASSERT_CHECKING
233 static bool pgstat_is_initialized = false;
234 static bool pgstat_is_shutdown = false;
235 #endif
236 
237 
238 /*
239  * The different kinds of statistics.
240  *
241  * If reasonably possible, handling specific to one kind of stats should go
242  * through this abstraction, rather than making more of pgstat.c aware.
243  *
244  * See comments for struct PgStat_KindInfo for details about the individual
245  * fields.
246  *
247  * XXX: It'd be nicer to define this outside of this file. But there doesn't
248  * seem to be a great way of doing that, given the split across multiple
249  * files.
250  */
252 
253  /* stats kinds for variable-numbered objects */
254 
256  .name = "database",
257 
258  .fixed_amount = false,
259  /* so pg_stat_database entries can be seen in all databases */
260  .accessed_across_databases = true,
261 
262  .shared_size = sizeof(PgStatShared_Database),
263  .shared_data_off = offsetof(PgStatShared_Database, stats),
264  .shared_data_len = sizeof(((PgStatShared_Database *) 0)->stats),
265  .pending_size = sizeof(PgStat_StatDBEntry),
266 
267  .flush_pending_cb = pgstat_database_flush_cb,
268  .reset_timestamp_cb = pgstat_database_reset_timestamp_cb,
269  },
270 
272  .name = "relation",
273 
274  .fixed_amount = false,
275 
276  .shared_size = sizeof(PgStatShared_Relation),
277  .shared_data_off = offsetof(PgStatShared_Relation, stats),
278  .shared_data_len = sizeof(((PgStatShared_Relation *) 0)->stats),
279  .pending_size = sizeof(PgStat_TableStatus),
280 
281  .flush_pending_cb = pgstat_relation_flush_cb,
282  .delete_pending_cb = pgstat_relation_delete_pending_cb,
283  },
284 
286  .name = "function",
287 
288  .fixed_amount = false,
289 
290  .shared_size = sizeof(PgStatShared_Function),
291  .shared_data_off = offsetof(PgStatShared_Function, stats),
292  .shared_data_len = sizeof(((PgStatShared_Function *) 0)->stats),
293  .pending_size = sizeof(PgStat_BackendFunctionEntry),
294 
295  .flush_pending_cb = pgstat_function_flush_cb,
296  },
297 
299  .name = "replslot",
300 
301  .fixed_amount = false,
302 
303  .accessed_across_databases = true,
304  .named_on_disk = true,
305 
306  .shared_size = sizeof(PgStatShared_ReplSlot),
307  .shared_data_off = offsetof(PgStatShared_ReplSlot, stats),
308  .shared_data_len = sizeof(((PgStatShared_ReplSlot *) 0)->stats),
309 
310  .reset_timestamp_cb = pgstat_replslot_reset_timestamp_cb,
311  .to_serialized_name = pgstat_replslot_to_serialized_name_cb,
312  .from_serialized_name = pgstat_replslot_from_serialized_name_cb,
313  },
314 
316  .name = "subscription",
317 
318  .fixed_amount = false,
319  /* so pg_stat_subscription_stats entries can be seen in all databases */
320  .accessed_across_databases = true,
321 
322  .shared_size = sizeof(PgStatShared_Subscription),
323  .shared_data_off = offsetof(PgStatShared_Subscription, stats),
324  .shared_data_len = sizeof(((PgStatShared_Subscription *) 0)->stats),
325  .pending_size = sizeof(PgStat_BackendSubEntry),
326 
327  .flush_pending_cb = pgstat_subscription_flush_cb,
328  .reset_timestamp_cb = pgstat_subscription_reset_timestamp_cb,
329  },
330 
331 
332  /* stats for fixed-numbered (mostly 1) objects */
333 
335  .name = "archiver",
336 
337  .fixed_amount = true,
338 
339  .reset_all_cb = pgstat_archiver_reset_all_cb,
340  .snapshot_cb = pgstat_archiver_snapshot_cb,
341  },
342 
344  .name = "bgwriter",
345 
346  .fixed_amount = true,
347 
348  .reset_all_cb = pgstat_bgwriter_reset_all_cb,
349  .snapshot_cb = pgstat_bgwriter_snapshot_cb,
350  },
351 
353  .name = "checkpointer",
354 
355  .fixed_amount = true,
356 
357  .reset_all_cb = pgstat_checkpointer_reset_all_cb,
358  .snapshot_cb = pgstat_checkpointer_snapshot_cb,
359  },
360 
361  [PGSTAT_KIND_SLRU] = {
362  .name = "slru",
363 
364  .fixed_amount = true,
365 
366  .reset_all_cb = pgstat_slru_reset_all_cb,
367  .snapshot_cb = pgstat_slru_snapshot_cb,
368  },
369 
370  [PGSTAT_KIND_WAL] = {
371  .name = "wal",
372 
373  .fixed_amount = true,
374 
375  .reset_all_cb = pgstat_wal_reset_all_cb,
376  .snapshot_cb = pgstat_wal_snapshot_cb,
377  },
378 };
379 
380 
381 /* ------------------------------------------------------------
382  * Functions managing the state of the stats system for all backends.
383  * ------------------------------------------------------------
384  */
385 
386 /*
387  * Read on-disk stats into memory at server start.
388  *
389  * Should only be called by the startup process or in single user mode.
390  */
391 void
393 {
395 }
396 
397 /*
398  * Remove the stats file. This is currently used only if WAL recovery is
399  * needed after a crash.
400  *
401  * Should only be called by the startup process or in single user mode.
402  */
403 void
405 {
406  int ret;
407 
408  /* NB: this needs to be done even in single user mode */
409 
410  ret = unlink(PGSTAT_STAT_PERMANENT_FILENAME);
411  if (ret != 0)
412  {
413  if (errno == ENOENT)
414  elog(DEBUG2,
415  "didn't need to unlink permanent stats file \"%s\" - didn't exist",
417  else
418  ereport(LOG,
420  errmsg("could not unlink permanent statistics file \"%s\": %m",
422  }
423  else
424  {
425  ereport(DEBUG2,
427  errmsg("unlinked permanent statistics file \"%s\"",
429  }
430 
431  /*
432  * Reset stats contents. This will set reset timestamps of fixed-numbered
433  * stats to the current time (no variable stats exist).
434  */
436 }
437 
438 /*
439  * pgstat_before_server_shutdown() needs to be called by exactly one process
440  * during regular server shutdowns. Otherwise all stats will be lost.
441  *
442  * We currently only write out stats for proc_exit(0). We might want to change
443  * that at some point... But right now pgstat_discard_stats() would be called
444  * during the start after a disorderly shutdown, anyway.
445  */
446 void
448 {
449  Assert(pgStatLocal.shmem != NULL);
451 
452  /*
453  * Stats should only be reported after pgstat_initialize() and before
454  * pgstat_shutdown(). This is a convenient point to catch most violations
455  * of this rule.
456  */
457  Assert(pgstat_is_initialized && !pgstat_is_shutdown);
458 
459  /* flush out our own pending changes before writing out */
460  pgstat_report_stat(true);
461 
462  /*
463  * Only write out file during normal shutdown. Don't even signal that
464  * we've shutdown during irregular shutdowns, because the shutdown
465  * sequence isn't coordinated to ensure this backend shuts down last.
466  */
467  if (code == 0)
468  {
469  pgStatLocal.shmem->is_shutdown = true;
471  }
472 }
473 
474 
475 /* ------------------------------------------------------------
476  * Backend initialization / shutdown functions
477  * ------------------------------------------------------------
478  */
479 
480 /*
481  * Shut down a single backend's statistics reporting at process exit.
482  *
483  * Flush out any remaining statistics counts. Without this, operations
484  * triggered during backend exit (such as temp table deletions) won't be
485  * counted.
486  */
487 static void
489 {
490  Assert(!pgstat_is_shutdown);
492 
493  /*
494  * If we got as far as discovering our own database ID, we can flush out
495  * what we did so far. Otherwise, we'd be reporting an invalid database
496  * ID, so forget it. (This means that accesses to pg_database during
497  * failed backend starts might never get counted.)
498  */
501 
502  pgstat_report_stat(true);
503 
504  /* there shouldn't be any pending changes left */
507 
509 
510 #ifdef USE_ASSERT_CHECKING
511  pgstat_is_shutdown = true;
512 #endif
513 }
514 
515 /*
516  * Initialize pgstats state, and set up our on-proc-exit hook. Called from
517  * BaseInit().
518  *
519  * NOTE: MyDatabaseId isn't set yet; so the shutdown hook has to be careful.
520  */
521 void
523 {
524  Assert(!pgstat_is_initialized);
525 
527 
528  pgstat_init_wal();
529 
530  /* Set up a process-exit hook to clean up */
532 
533 #ifdef USE_ASSERT_CHECKING
534  pgstat_is_initialized = true;
535 #endif
536 }
537 
538 
539 /* ------------------------------------------------------------
540  * Public functions used by backends follow
541  * ------------------------------------------------------------
542  */
543 
544 /*
545  * Must be called by processes that performs DML: tcop/postgres.c, logical
546  * receiver processes, SPI worker, etc. to flush pending statistics updates to
547  * shared memory.
548  *
549  * Unless called with 'force', pending stats updates are flushed happen once
550  * per PGSTAT_MIN_INTERVAL (1000ms). When not forced, stats flushes do not
551  * block on lock acquisition, except if stats updates have been pending for
552  * longer than PGSTAT_MAX_INTERVAL (60000ms).
553  *
554  * Whenever pending stats updates remain at the end of pgstat_report_stat() a
555  * suggested idle timeout is returned. Currently this is always
556  * PGSTAT_IDLE_INTERVAL (10000ms). Callers can use the returned time to set up
557  * a timeout after which to call pgstat_report_stat(true), but are not
558  * required to to do so.
559  *
560  * Note that this is called only when not within a transaction, so it is fair
561  * to use transaction stop time as an approximation of current time.
562  */
563 long
565 {
566  static TimestampTz pending_since = 0;
567  static TimestampTz last_flush = 0;
568  bool partial_flush;
570  bool nowait;
571 
574 
575  /* "absorb" the forced flush even if there's nothing to flush */
577  {
578  force = true;
579  pgStatForceNextFlush = false;
580  }
581 
582  /* Don't expend a clock check if nothing to do */
584  !have_slrustats &&
586  {
587  Assert(pending_since == 0);
588  return 0;
589  }
590 
591  /*
592  * There should never be stats to report once stats are shut down. Can't
593  * assert that before the checks above, as there is an unconditional
594  * pgstat_report_stat() call in pgstat_shutdown_hook() - which at least
595  * the process that ran pgstat_before_server_shutdown() will still call.
596  */
598 
600 
601  if (!force)
602  {
603  if (pending_since > 0 &&
605  {
606  /* don't keep pending updates longer than PGSTAT_MAX_INTERVAL */
607  force = true;
608  }
609  else if (last_flush > 0 &&
611  {
612  /* don't flush too frequently */
613  if (pending_since == 0)
614  pending_since = now;
615 
616  return PGSTAT_IDLE_INTERVAL;
617  }
618  }
619 
621 
622  /* don't wait for lock acquisition when !force */
623  nowait = !force;
624 
625  partial_flush = false;
626 
627  /* flush database / relation / function / ... stats */
628  partial_flush |= pgstat_flush_pending_entries(nowait);
629 
630  /* flush wal stats */
631  partial_flush |= pgstat_flush_wal(nowait);
632 
633  /* flush SLRU stats */
634  partial_flush |= pgstat_slru_flush(nowait);
635 
636  last_flush = now;
637 
638  /*
639  * If some of the pending stats could not be flushed due to lock
640  * contention, let the caller know when to retry.
641  */
642  if (partial_flush)
643  {
644  /* force should have prevented us from getting here */
645  Assert(!force);
646 
647  /* remember since when stats have been pending */
648  if (pending_since == 0)
649  pending_since = now;
650 
651  return PGSTAT_IDLE_INTERVAL;
652  }
653 
654  pending_since = 0;
655 
656  return 0;
657 }
658 
659 /*
660  * Force locally pending stats to be flushed during the next
661  * pgstat_report_stat() call. This is useful for writing tests.
662  */
663 void
665 {
666  pgStatForceNextFlush = true;
667 }
668 
669 /*
670  * Only for use by pgstat_reset_counters()
671  */
672 static bool
674 {
675  return entry->key.dboid == DatumGetObjectId(MyDatabaseId);
676 }
677 
678 /*
679  * Reset counters for our database.
680  *
681  * Permission checking for this function is managed through the normal
682  * GRANT system.
683  */
684 void
686 {
688 
691  ts);
692 }
693 
694 /*
695  * Reset a single variable-numbered entry.
696  *
697  * If the stats kind is within a database, also reset the database's
698  * stat_reset_timestamp.
699  *
700  * Permission checking for this function is managed through the normal
701  * GRANT system.
702  */
703 void
704 pgstat_reset(PgStat_Kind kind, Oid dboid, Oid objoid)
705 {
706  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
708 
709  /* not needed atm, and doesn't make sense with the current signature */
710  Assert(!pgstat_get_kind_info(kind)->fixed_amount);
711 
712  /* reset the "single counter" */
713  pgstat_reset_entry(kind, dboid, objoid, ts);
714 
715  if (!kind_info->accessed_across_databases)
717 }
718 
719 /*
720  * Reset stats for all entries of a kind.
721  *
722  * Permission checking for this function is managed through the normal
723  * GRANT system.
724  */
725 void
727 {
728  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
730 
731  if (kind_info->fixed_amount)
732  kind_info->reset_all_cb(ts);
733  else
735 }
736 
737 
738 /* ------------------------------------------------------------
739  * Fetching of stats
740  * ------------------------------------------------------------
741  */
742 
743 /*
744  * Discard any data collected in the current transaction. Any subsequent
745  * request will cause new snapshots to be read.
746  *
747  * This is also invoked during transaction commit or abort to discard
748  * the no-longer-wanted snapshot.
749  */
750 void
752 {
754 
755  memset(&pgStatLocal.snapshot.fixed_valid, 0,
757  pgStatLocal.snapshot.stats = NULL;
759 
760  /* Release memory, if any was allocated */
762  {
764 
765  /* Reset variables */
767  }
768 
769  /*
770  * Historically the backend_status.c facilities lived in this file, and
771  * were reset with the same function. For now keep it that way, and
772  * forward the reset request.
773  */
775 }
776 
777 void *
778 pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
779 {
781  PgStat_EntryRef *entry_ref;
782  void *stats_data;
783  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
784 
785  /* should be called from backends */
787  AssertArg(!kind_info->fixed_amount);
788 
790 
791  key.kind = kind;
792  key.dboid = dboid;
793  key.objoid = objoid;
794 
795  /* if we need to build a full snapshot, do so */
798 
799  /* if caching is desired, look up in cache */
801  {
802  PgStat_SnapshotEntry *entry = NULL;
803 
804  entry = pgstat_snapshot_lookup(pgStatLocal.snapshot.stats, key);
805 
806  if (entry)
807  return entry->data;
808 
809  /*
810  * If we built a full snapshot and the key is not in
811  * pgStatLocal.snapshot.stats, there are no matching stats.
812  */
814  return NULL;
815  }
816 
818 
819  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
820 
821  if (entry_ref == NULL || entry_ref->shared_entry->dropped)
822  {
823  /* create empty entry when using PGSTAT_FETCH_CONSISTENCY_CACHE */
825  {
826  PgStat_SnapshotEntry *entry = NULL;
827  bool found;
828 
829  entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
830  Assert(!found);
831  entry->data = NULL;
832  }
833  return NULL;
834  }
835 
836  /*
837  * Allocate in caller's context for PGSTAT_FETCH_CONSISTENCY_NONE,
838  * otherwise we could quickly end up with a fair bit of memory used due to
839  * repeated accesses.
840  */
842  stats_data = palloc(kind_info->shared_data_len);
843  else
845  kind_info->shared_data_len);
846  memcpy(stats_data,
847  pgstat_get_entry_data(kind, entry_ref->shared_stats),
848  kind_info->shared_data_len);
849 
851  {
852  PgStat_SnapshotEntry *entry = NULL;
853  bool found;
854 
855  entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
856  entry->data = stats_data;
857  }
858 
859  return stats_data;
860 }
861 
862 /*
863  * If a stats snapshot has been taken, return the timestamp at which that was
864  * done, and set *have_snapshot to true. Otherwise *have_snapshot is set to
865  * false.
866  */
869 {
871  {
872  *have_snapshot = true;
874  }
875 
876  *have_snapshot = false;
877 
878  return 0;
879 }
880 
881 bool
882 pgstat_have_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
883 {
884  /* fixed-numbered stats always exist */
885  if (pgstat_get_kind_info(kind)->fixed_amount)
886  return true;
887 
888  return pgstat_get_entry_ref(kind, dboid, objoid, false, NULL) != NULL;
889 }
890 
891 /*
892  * Ensure snapshot for fixed-numbered 'kind' exists.
893  *
894  * Typically used by the pgstat_fetch_* functions for a kind of stats, before
895  * massaging the data into the desired format.
896  */
897 void
899 {
901  AssertArg(pgstat_get_kind_info(kind)->fixed_amount);
902 
905  else
907 
909 }
910 
911 static void
913 {
915  pgStatLocal.snapshot.stats != NULL)
916  return;
917 
920  "PgStat Snapshot",
922 
924  pgstat_snapshot_create(pgStatLocal.snapshot.context,
926  NULL);
927 }
928 
929 static void
931 {
932  dshash_seq_status hstat;
934 
935  /* should only be called when we need a snapshot */
937 
938  /* snapshot already built */
940  return;
941 
943 
944  Assert(pgStatLocal.snapshot.stats->members == 0);
945 
947 
948  /*
949  * Snapshot all variable stats.
950  */
951  dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
952  while ((p = dshash_seq_next(&hstat)) != NULL)
953  {
954  PgStat_Kind kind = p->key.kind;
955  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
956  bool found;
957  PgStat_SnapshotEntry *entry;
958  PgStatShared_Common *stats_data;
959 
960  /*
961  * Check if the stats object should be included in the snapshot.
962  * Unless the stats kind can be accessed from all databases (e.g.,
963  * database stats themselves), we only include stats for the current
964  * database or objects not associated with a database (e.g. shared
965  * relations).
966  */
967  if (p->key.dboid != MyDatabaseId &&
968  p->key.dboid != InvalidOid &&
969  !kind_info->accessed_across_databases)
970  continue;
971 
972  if (p->dropped)
973  continue;
974 
976 
977  stats_data = dsa_get_address(pgStatLocal.dsa, p->body);
978  Assert(stats_data);
979 
980  entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, p->key, &found);
981  Assert(!found);
982 
984  kind_info->shared_size);
985  memcpy(entry->data,
986  pgstat_get_entry_data(kind, stats_data),
987  kind_info->shared_size);
988  }
989  dshash_seq_term(&hstat);
990 
991  /*
992  * Build snapshot of all fixed-numbered stats.
993  */
994  for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
995  {
996  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
997 
998  if (!kind_info->fixed_amount)
999  {
1000  Assert(kind_info->snapshot_cb == NULL);
1001  continue;
1002  }
1003 
1005  }
1006 
1008 }
1009 
1010 static void
1012 {
1013  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1014 
1015  Assert(kind_info->fixed_amount);
1016  Assert(kind_info->snapshot_cb != NULL);
1017 
1019  {
1020  /* rebuild every time */
1021  pgStatLocal.snapshot.fixed_valid[kind] = false;
1022  }
1023  else if (pgStatLocal.snapshot.fixed_valid[kind])
1024  {
1025  /* in snapshot mode we shouldn't get called again */
1027  return;
1028  }
1029 
1031 
1032  kind_info->snapshot_cb();
1033 
1035  pgStatLocal.snapshot.fixed_valid[kind] = true;
1036 }
1037 
1038 
1039 /* ------------------------------------------------------------
1040  * Backend-local pending stats infrastructure
1041  * ------------------------------------------------------------
1042  */
1043 
1044 /*
1045  * Returns the appropriate PgStat_EntryRef, preparing it to receive pending
1046  * stats if not already done.
1047  *
1048  * If created_entry is non-NULL, it'll be set to true if the entry is newly
1049  * created, false otherwise.
1050  */
1052 pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid, bool *created_entry)
1053 {
1054  PgStat_EntryRef *entry_ref;
1055 
1056  /* need to be able to flush out */
1057  Assert(pgstat_get_kind_info(kind)->flush_pending_cb != NULL);
1058 
1060  {
1063  "PgStat Pending",
1065  }
1066 
1067  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid,
1068  true, created_entry);
1069 
1070  if (entry_ref->pending == NULL)
1071  {
1072  size_t entrysize = pgstat_get_kind_info(kind)->pending_size;
1073 
1074  Assert(entrysize != (size_t) -1);
1075 
1076  entry_ref->pending = MemoryContextAllocZero(pgStatPendingContext, entrysize);
1078  }
1079 
1080  return entry_ref;
1081 }
1082 
1083 /*
1084  * Return an existing stats entry, or NULL.
1085  *
1086  * This should only be used for helper function for pgstatfuncs.c - outside of
1087  * that it shouldn't be needed.
1088  */
1091 {
1092  PgStat_EntryRef *entry_ref;
1093 
1094  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
1095 
1096  if (entry_ref == NULL || entry_ref->pending == NULL)
1097  return NULL;
1098 
1099  return entry_ref;
1100 }
1101 
1102 void
1104 {
1105  PgStat_Kind kind = entry_ref->shared_entry->key.kind;
1106  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1107  void *pending_data = entry_ref->pending;
1108 
1109  Assert(pending_data != NULL);
1110  /* !fixed_amount stats should be handled explicitly */
1111  Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1112 
1113  if (kind_info->delete_pending_cb)
1114  kind_info->delete_pending_cb(entry_ref);
1115 
1116  pfree(pending_data);
1117  entry_ref->pending = NULL;
1118 
1119  dlist_delete(&entry_ref->pending_node);
1120 }
1121 
1122 /*
1123  * Flush out pending stats for database objects (databases, relations,
1124  * functions).
1125  */
1126 static bool
1128 {
1129  bool have_pending = false;
1130  dlist_node *cur = NULL;
1131 
1132  /*
1133  * Need to be a bit careful iterating over the list of pending entries.
1134  * Processing a pending entry may queue further pending entries to the end
1135  * of the list that we want to process, so a simple iteration won't do.
1136  * Further complicating matters is that we want to delete the current
1137  * entry in each iteration from the list if we flushed successfully.
1138  *
1139  * So we just keep track of the next pointer in each loop iteration.
1140  */
1143 
1144  while (cur)
1145  {
1146  PgStat_EntryRef *entry_ref =
1147  dlist_container(PgStat_EntryRef, pending_node, cur);
1148  PgStat_HashKey key = entry_ref->shared_entry->key;
1149  PgStat_Kind kind = key.kind;
1150  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1151  bool did_flush;
1152  dlist_node *next;
1153 
1154  Assert(!kind_info->fixed_amount);
1155  Assert(kind_info->flush_pending_cb != NULL);
1156 
1157  /* flush the stats, if possible */
1158  did_flush = kind_info->flush_pending_cb(entry_ref, nowait);
1159 
1160  Assert(did_flush || nowait);
1161 
1162  /* determine next entry, before deleting the pending entry */
1165  else
1166  next = NULL;
1167 
1168  /* if successfully flushed, remove entry */
1169  if (did_flush)
1170  pgstat_delete_pending_entry(entry_ref);
1171  else
1172  have_pending = true;
1173 
1174  cur = next;
1175  }
1176 
1177  Assert(dlist_is_empty(&pgStatPending) == !have_pending);
1178 
1179  return have_pending;
1180 }
1181 
1182 
1183 /* ------------------------------------------------------------
1184  * Helper / infrastructure functions
1185  * ------------------------------------------------------------
1186  */
1187 
1190 {
1191  for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1192  {
1193  if (pg_strcasecmp(kind_str, pgstat_kind_infos[kind].name) == 0)
1194  return kind;
1195  }
1196 
1197  ereport(ERROR,
1198  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1199  errmsg("invalid statistics kind: \"%s\"", kind_str)));
1200  return PGSTAT_KIND_DATABASE; /* avoid compiler warnings */
1201 }
1202 
1203 static inline bool
1205 {
1206  return ikind >= PGSTAT_KIND_FIRST_VALID && ikind <= PGSTAT_KIND_LAST;
1207 }
1208 
1209 const PgStat_KindInfo *
1211 {
1213 
1214  return &pgstat_kind_infos[kind];
1215 }
1216 
1217 /*
1218  * Stats should only be reported after pgstat_initialize() and before
1219  * pgstat_shutdown(). This check is put in a few central places to catch
1220  * violations of this rule more easily.
1221  */
1222 #ifdef USE_ASSERT_CHECKING
1223 void
1224 pgstat_assert_is_up(void)
1225 {
1226  Assert(pgstat_is_initialized && !pgstat_is_shutdown);
1227 }
1228 #endif
1229 
1230 
1231 /* ------------------------------------------------------------
1232  * reading and writing of on-disk stats file
1233  * ------------------------------------------------------------
1234  */
1235 
1236 /* helpers for pgstat_write_statsfile() */
1237 static void
1238 write_chunk(FILE *fpout, void *ptr, size_t len)
1239 {
1240  int rc;
1241 
1242  rc = fwrite(ptr, len, 1, fpout);
1243 
1244  /* we'll check for errors with ferror once at the end */
1245  (void) rc;
1246 }
1247 
1248 #define write_chunk_s(fpout, ptr) write_chunk(fpout, ptr, sizeof(*ptr))
1249 
1250 /*
1251  * This function is called in the last process that is accessing the shared
1252  * stats so locking is not required.
1253  */
1254 static void
1256 {
1257  FILE *fpout;
1258  int32 format_id;
1259  const char *tmpfile = PGSTAT_STAT_PERMANENT_TMPFILE;
1260  const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
1261  dshash_seq_status hstat;
1263 
1265 
1266  /* we're shutting down, so it's ok to just override this */
1268 
1269  elog(DEBUG2, "writing stats file \"%s\"", statfile);
1270 
1271  /*
1272  * Open the statistics temp file to write out the current values.
1273  */
1274  fpout = AllocateFile(tmpfile, PG_BINARY_W);
1275  if (fpout == NULL)
1276  {
1277  ereport(LOG,
1279  errmsg("could not open temporary statistics file \"%s\": %m",
1280  tmpfile)));
1281  return;
1282  }
1283 
1284  /*
1285  * Write the file header --- currently just a format ID.
1286  */
1287  format_id = PGSTAT_FILE_FORMAT_ID;
1288  write_chunk_s(fpout, &format_id);
1289 
1290  /*
1291  * XXX: The following could now be generalized to just iterate over
1292  * pgstat_kind_infos instead of knowing about the different kinds of
1293  * stats.
1294  */
1295 
1296  /*
1297  * Write archiver stats struct
1298  */
1301 
1302  /*
1303  * Write bgwriter stats struct
1304  */
1307 
1308  /*
1309  * Write checkpointer stats struct
1310  */
1313 
1314  /*
1315  * Write SLRU stats struct
1316  */
1319 
1320  /*
1321  * Write WAL stats struct
1322  */
1325 
1326  /*
1327  * Walk through the stats entries
1328  */
1329  dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
1330  while ((ps = dshash_seq_next(&hstat)) != NULL)
1331  {
1332  PgStatShared_Common *shstats;
1333  const PgStat_KindInfo *kind_info = NULL;
1334 
1336 
1337  /* we may have some "dropped" entries not yet removed, skip them */
1338  Assert(!ps->dropped);
1339  if (ps->dropped)
1340  continue;
1341 
1343 
1344  kind_info = pgstat_get_kind_info(ps->key.kind);
1345 
1346  /* if not dropped the valid-entry refcount should exist */
1347  Assert(pg_atomic_read_u32(&ps->refcount) > 0);
1348 
1349  if (!kind_info->to_serialized_name)
1350  {
1351  /* normal stats entry, identified by PgStat_HashKey */
1352  fputc('S', fpout);
1353  write_chunk_s(fpout, &ps->key);
1354  }
1355  else
1356  {
1357  /* stats entry identified by name on disk (e.g. slots) */
1358  NameData name;
1359 
1360  kind_info->to_serialized_name(shstats, &name);
1361 
1362  fputc('N', fpout);
1363  write_chunk_s(fpout, &ps->key.kind);
1364  write_chunk_s(fpout, &name);
1365  }
1366 
1367  /* Write except the header part of the entry */
1368  write_chunk(fpout,
1369  pgstat_get_entry_data(ps->key.kind, shstats),
1371  }
1372  dshash_seq_term(&hstat);
1373 
1374  /*
1375  * No more output to be done. Close the temp file and replace the old
1376  * pgstat.stat with it. The ferror() check replaces testing for error
1377  * after each individual fputc or fwrite (in write_chunk()) above.
1378  */
1379  fputc('E', fpout);
1380 
1381  if (ferror(fpout))
1382  {
1383  ereport(LOG,
1385  errmsg("could not write temporary statistics file \"%s\": %m",
1386  tmpfile)));
1387  FreeFile(fpout);
1388  unlink(tmpfile);
1389  }
1390  else if (FreeFile(fpout) < 0)
1391  {
1392  ereport(LOG,
1394  errmsg("could not close temporary statistics file \"%s\": %m",
1395  tmpfile)));
1396  unlink(tmpfile);
1397  }
1398  else if (rename(tmpfile, statfile) < 0)
1399  {
1400  ereport(LOG,
1402  errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
1403  tmpfile, statfile)));
1404  unlink(tmpfile);
1405  }
1406 }
1407 
1408 /* helpers for pgstat_read_statsfile() */
1409 static bool
1410 read_chunk(FILE *fpin, void *ptr, size_t len)
1411 {
1412  return fread(ptr, 1, len, fpin) == len;
1413 }
1414 
1415 #define read_chunk_s(fpin, ptr) read_chunk(fpin, ptr, sizeof(*ptr))
1416 
1417 /*
1418  * Reads in existing statistics file into the shared stats hash.
1419  *
1420  * This function is called in the only process that is accessing the shared
1421  * stats so locking is not required.
1422  */
1423 static void
1425 {
1426  FILE *fpin;
1427  int32 format_id;
1428  bool found;
1429  const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
1431 
1432  /* shouldn't be called from postmaster */
1434 
1435  elog(DEBUG2, "reading stats file \"%s\"", statfile);
1436 
1437  /*
1438  * Try to open the stats file. If it doesn't exist, the backends simply
1439  * returns zero for anything and statistics simply starts from scratch
1440  * with empty counters.
1441  *
1442  * ENOENT is a possibility if stats collection was previously disabled or
1443  * has not yet written the stats file for the first time. Any other
1444  * failure condition is suspicious.
1445  */
1446  if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
1447  {
1448  if (errno != ENOENT)
1449  ereport(LOG,
1451  errmsg("could not open statistics file \"%s\": %m",
1452  statfile)));
1454  return;
1455  }
1456 
1457  /*
1458  * Verify it's of the expected format.
1459  */
1460  if (!read_chunk_s(fpin, &format_id) ||
1461  format_id != PGSTAT_FILE_FORMAT_ID)
1462  goto error;
1463 
1464  /*
1465  * XXX: The following could now be generalized to just iterate over
1466  * pgstat_kind_infos instead of knowing about the different kinds of
1467  * stats.
1468  */
1469 
1470  /*
1471  * Read archiver stats struct
1472  */
1473  if (!read_chunk_s(fpin, &shmem->archiver.stats))
1474  goto error;
1475 
1476  /*
1477  * Read bgwriter stats struct
1478  */
1479  if (!read_chunk_s(fpin, &shmem->bgwriter.stats))
1480  goto error;
1481 
1482  /*
1483  * Read checkpointer stats struct
1484  */
1485  if (!read_chunk_s(fpin, &shmem->checkpointer.stats))
1486  goto error;
1487 
1488  /*
1489  * Read SLRU stats struct
1490  */
1491  if (!read_chunk_s(fpin, &shmem->slru.stats))
1492  goto error;
1493 
1494  /*
1495  * Read WAL stats struct
1496  */
1497  if (!read_chunk_s(fpin, &shmem->wal.stats))
1498  goto error;
1499 
1500  /*
1501  * We found an existing statistics file. Read it and put all the hash
1502  * table entries into place.
1503  */
1504  for (;;)
1505  {
1506  int t = fgetc(fpin);
1507 
1508  switch (t)
1509  {
1510  case 'S':
1511  case 'N':
1512  {
1516 
1518 
1519  if (t == 'S')
1520  {
1521  /* normal stats entry, identified by PgStat_HashKey */
1522  if (!read_chunk_s(fpin, &key))
1523  goto error;
1524 
1525  if (!pgstat_is_kind_valid(key.kind))
1526  goto error;
1527  }
1528  else
1529  {
1530  /* stats entry identified by name on disk (e.g. slots) */
1531  const PgStat_KindInfo *kind_info = NULL;
1532  PgStat_Kind kind;
1533  NameData name;
1534 
1535  if (!read_chunk_s(fpin, &kind))
1536  goto error;
1537  if (!read_chunk_s(fpin, &name))
1538  goto error;
1539  if (!pgstat_is_kind_valid(kind))
1540  goto error;
1541 
1542  kind_info = pgstat_get_kind_info(kind);
1543 
1544  if (!kind_info->from_serialized_name)
1545  goto error;
1546 
1547  if (!kind_info->from_serialized_name(&name, &key))
1548  {
1549  /* skip over data for entry we don't care about */
1550  if (fseek(fpin, pgstat_get_entry_len(kind), SEEK_CUR) != 0)
1551  goto error;
1552 
1553  continue;
1554  }
1555 
1556  Assert(key.kind == kind);
1557  }
1558 
1559  /*
1560  * This intentionally doesn't use pgstat_get_entry_ref() -
1561  * putting all stats into checkpointer's
1562  * pgStatEntryRefHash would be wasted effort and memory.
1563  */
1565 
1566  /* don't allow duplicate entries */
1567  if (found)
1568  {
1570  elog(WARNING, "found duplicate stats entry %d/%u/%u",
1571  key.kind, key.dboid, key.objoid);
1572  goto error;
1573  }
1574 
1575  header = pgstat_init_entry(key.kind, p);
1577 
1578  if (!read_chunk(fpin,
1580  pgstat_get_entry_len(key.kind)))
1581  goto error;
1582 
1583  break;
1584  }
1585  case 'E':
1586  /* check that 'E' actually signals end of file */
1587  if (fgetc(fpin) != EOF)
1588  goto error;
1589 
1590  goto done;
1591 
1592  default:
1593  goto error;
1594  }
1595  }
1596 
1597 done:
1598  FreeFile(fpin);
1599 
1600  elog(DEBUG2, "removing permanent stats file \"%s\"", statfile);
1601  unlink(statfile);
1602 
1603  return;
1604 
1605 error:
1606  ereport(LOG,
1607  (errmsg("corrupted statistics file \"%s\"", statfile)));
1608 
1610 
1611  goto done;
1612 }
1613 
1614 /*
1615  * Helper to reset / drop stats after a crash or after restoring stats from
1616  * disk failed, potentially after already loading parts.
1617  */
1618 static void
1620 {
1622 
1623  /* reset fixed-numbered stats */
1624  for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1625  {
1626  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1627 
1628  if (!kind_info->fixed_amount)
1629  continue;
1630 
1631  kind_info->reset_all_cb(ts);
1632  }
1633 
1634  /* and drop variable-numbered ones */
1636 }
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:241
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1705
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1574
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1538
void pgstat_clear_backend_activity_snapshot(void)
static int32 next
Definition: blutils.c:219
#define PG_BINARY_R
Definition: c.h:1270
#define offsetof(type, field)
Definition: c.h:727
signed int int32
Definition: c.h:429
#define unlikely(x)
Definition: c.h:273
#define PG_BINARY_W
Definition: c.h:1271
#define OidIsValid(objectId)
Definition: c.h:710
#define AssertArg(condition)
Definition: c.h:806
int64 TimestampTz
Definition: timestamp.h:39
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
void dshash_release_lock(dshash_table *hash_table, void *entry)
Definition: dshash.c:569
void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table, bool exclusive)
Definition: dshash.c:612
void dshash_seq_term(dshash_seq_status *status)
Definition: dshash.c:723
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
Definition: dshash.c:438
void * dshash_seq_next(dshash_seq_status *status)
Definition: dshash.c:631
struct cursor * cur
Definition: ecpg.c:28
int errcode_for_file_access(void)
Definition: elog.c:716
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define LOG
Definition: elog.h:25
#define WARNING
Definition: elog.h:30
#define DEBUG2
Definition: elog.h:23
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
#define ereport(elevel,...)
Definition: elog.h:143
const char * name
Definition: encode.c:561
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2461
int FreeFile(FILE *file)
Definition: fd.c:2660
bool IsUnderPostmaster
Definition: globals.c:113
bool IsPostmasterEnvironment
Definition: globals.c:112
Oid MyDatabaseId
Definition: globals.c:89
static void dlist_init(dlist_head *head)
Definition: ilist.h:278
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
static dlist_node * dlist_next_node(dlist_head *head, dlist_node *node)
Definition: ilist.h:440
static bool dlist_has_next(dlist_head *head, dlist_node *node)
Definition: ilist.h:421
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:317
static dlist_node * dlist_head_node(dlist_head *head)
Definition: ilist.h:468
#define DLIST_STATIC_INIT(name)
Definition: ilist.h:248
#define dlist_container(type, membername, ptr)
Definition: ilist.h:496
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:333
Assert(fmt[strlen(fmt) - 1] !='\n')
void pfree(void *pointer)
Definition: mcxt.c:1175
MemoryContext TopMemoryContext
Definition: mcxt.c:48
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:906
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
MemoryContext CacheMemoryContext
Definition: mcxt.c:51
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
void * palloc(Size size)
Definition: mcxt.c:1068
#define AllocSetContextCreate
Definition: memutils.h:173
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:207
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
void * arg
const void size_t len
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:212
void pgstat_reset(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:704
static bool pgStatForceNextFlush
Definition: pgstat.c:226
int pgstat_fetch_consistency
Definition: pgstat.c:186
#define PGSTAT_MIN_INTERVAL
Definition: pgstat.c:118
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:898
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1210
static void pgstat_prep_snapshot(void)
Definition: pgstat.c:912
static void pgstat_build_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1011
#define PGSTAT_SNAPSHOT_HASH_SIZE
Definition: pgstat.c:129
static bool pgstat_is_kind_valid(int ikind)
Definition: pgstat.c:1204
void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat.c:1103
void pgstat_reset_counters(void)
Definition: pgstat.c:685
static void pgstat_build_snapshot(void)
Definition: pgstat.c:930
void pgstat_initialize(void)
Definition: pgstat.c:522
bool pgstat_track_counts
Definition: pgstat.c:185
bool pgstat_have_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:882
static void pgstat_write_statsfile(void)
Definition: pgstat.c:1255
long pgstat_report_stat(bool force)
Definition: pgstat.c:564
static void pgstat_read_statsfile(void)
Definition: pgstat.c:1424
void pgstat_reset_of_kind(PgStat_Kind kind)
Definition: pgstat.c:726
static bool pgstat_flush_pending_entries(bool nowait)
Definition: pgstat.c:1127
void pgstat_before_server_shutdown(int code, Datum arg)
Definition: pgstat.c:447
static void pgstat_reset_after_failure(void)
Definition: pgstat.c:1619
void pgstat_force_next_flush(void)
Definition: pgstat.c:664
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:778
void pgstat_clear_snapshot(void)
Definition: pgstat.c:751
TimestampTz pgstat_get_stat_snapshot_timestamp(bool *have_snapshot)
Definition: pgstat.c:868
static MemoryContext pgStatPendingContext
Definition: pgstat.c:211
static dlist_head pgStatPending
Definition: pgstat.c:219
static void write_chunk(FILE *fpout, void *ptr, size_t len)
Definition: pgstat.c:1238
void pgstat_restore_stats(void)
Definition: pgstat.c:392
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid, bool *created_entry)
Definition: pgstat.c:1052
#define write_chunk_s(fpout, ptr)
Definition: pgstat.c:1248
struct PgStat_SnapshotEntry PgStat_SnapshotEntry
PgStat_EntryRef * pgstat_fetch_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:1090
#define PGSTAT_MAX_INTERVAL
Definition: pgstat.c:120
static bool read_chunk(FILE *fpin, void *ptr, size_t len)
Definition: pgstat.c:1410
#define read_chunk_s(fpin, ptr)
Definition: pgstat.c:1415
static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS]
Definition: pgstat.c:251
#define PGSTAT_IDLE_INTERVAL
Definition: pgstat.c:122
PgStat_LocalState pgStatLocal
Definition: pgstat.c:194
void pgstat_discard_stats(void)
Definition: pgstat.c:404
static bool match_db_entries(PgStatShared_HashEntry *entry, Datum match_data)
Definition: pgstat.c:673
PgStat_Kind pgstat_get_kind_from_str(char *kind_str)
Definition: pgstat.c:1189
static void pgstat_shutdown_hook(int code, Datum arg)
Definition: pgstat.c:488
PgStat_Kind
Definition: pgstat.h:36
@ PGSTAT_KIND_DATABASE
Definition: pgstat.h:41
@ PGSTAT_KIND_REPLSLOT
Definition: pgstat.h:44
@ PGSTAT_KIND_ARCHIVER
Definition: pgstat.h:48
@ PGSTAT_KIND_SLRU
Definition: pgstat.h:51
@ PGSTAT_KIND_BGWRITER
Definition: pgstat.h:49
@ PGSTAT_KIND_SUBSCRIPTION
Definition: pgstat.h:45
@ PGSTAT_KIND_CHECKPOINTER
Definition: pgstat.h:50
@ PGSTAT_KIND_FUNCTION
Definition: pgstat.h:43
@ PGSTAT_KIND_WAL
Definition: pgstat.h:52
@ PGSTAT_KIND_RELATION
Definition: pgstat.h:42
@ PGSTAT_FETCH_CONSISTENCY_NONE
Definition: pgstat.h:69
@ PGSTAT_FETCH_CONSISTENCY_CACHE
Definition: pgstat.h:70
@ PGSTAT_FETCH_CONSISTENCY_SNAPSHOT
Definition: pgstat.h:71
#define PGSTAT_STAT_PERMANENT_FILENAME
Definition: pgstat.h:28
#define PGSTAT_STAT_PERMANENT_TMPFILE
Definition: pgstat.h:29
#define PGSTAT_NUM_KINDS
Definition: pgstat.h:57
#define PGSTAT_FILE_FORMAT_ID
Definition: pgstat.h:245
#define PGSTAT_KIND_LAST
Definition: pgstat.h:56
#define PGSTAT_KIND_FIRST_VALID
Definition: pgstat.h:55
void pgstat_archiver_reset_all_cb(TimestampTz ts)
void pgstat_archiver_snapshot_cb(void)
void pgstat_bgwriter_reset_all_cb(TimestampTz ts)
void pgstat_bgwriter_snapshot_cb(void)
void pgstat_checkpointer_snapshot_cb(void)
void pgstat_checkpointer_reset_all_cb(TimestampTz ts)
void pgstat_database_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_update_dbstats(TimestampTz ts)
void pgstat_reset_database_timestamp(Oid dboid, TimestampTz ts)
void pgstat_report_disconnect(Oid dboid)
bool pgstat_database_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
bool pgstat_function_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
struct PgStatShared_Function PgStatShared_Function
static void * pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry)
struct PgStatShared_Subscription PgStatShared_Subscription
#define pgstat_assert_is_up()
struct PgStatShared_Relation PgStatShared_Relation
struct PgStatShared_Database PgStatShared_Database
static size_t pgstat_get_entry_len(PgStat_Kind kind)
struct PgStatShared_ReplSlot PgStatShared_ReplSlot
bool pgstat_relation_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
void pgstat_relation_delete_pending_cb(PgStat_EntryRef *entry_ref)
bool pgstat_replslot_from_serialized_name_cb(const NameData *name, PgStat_HashKey *key)
void pgstat_replslot_to_serialized_name_cb(const PgStatShared_Common *header, NameData *name)
void pgstat_replslot_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts)
Definition: pgstat_shmem.c:969
PgStatShared_Common * pgstat_init_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
Definition: pgstat_shmem.c:254
void pgstat_attach_shmem(void)
Definition: pgstat_shmem.c:213
void pgstat_reset_entry(PgStat_Kind kind, Oid dboid, Oid objoid, TimestampTz ts)
Definition: pgstat_shmem.c:913
void pgstat_reset_matching_entries(bool(*do_reset)(PgStatShared_HashEntry *, Datum), Datum match_data, TimestampTz ts)
Definition: pgstat_shmem.c:933
void pgstat_drop_all_entries(void)
Definition: pgstat_shmem.c:875
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:393
void pgstat_detach_shmem(void)
Definition: pgstat_shmem.c:233
bool have_slrustats
Definition: pgstat_slru.c:35
void pgstat_slru_snapshot_cb(void)
Definition: pgstat_slru.c:205
bool pgstat_slru_flush(bool nowait)
Definition: pgstat_slru.c:158
void pgstat_slru_reset_all_cb(TimestampTz ts)
Definition: pgstat_slru.c:198
bool pgstat_subscription_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
void pgstat_subscription_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_wal_reset_all_cb(TimestampTz ts)
Definition: pgstat_wal.c:152
bool pgstat_have_pending_wal(void)
Definition: pgstat_wal.c:144
void pgstat_init_wal(void)
Definition: pgstat_wal.c:126
bool pgstat_flush_wal(bool nowait)
Definition: pgstat_wal.c:68
void pgstat_wal_snapshot_cb(void)
Definition: pgstat_wal.c:163
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define DatumGetObjectId(X)
Definition: postgres.h:544
uintptr_t Datum
Definition: postgres.h:411
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
static void error(void)
Definition: sql-dyntest.c:147
PgStat_ArchiverStats stats
PgStat_BgWriterStats stats
PgStat_CheckpointerStats stats
pg_atomic_uint32 refcount
PgStat_SLRUStats stats[SLRU_NUM_ELEMENTS]
PgStat_WalStats stats
PgStatShared_Common * shared_stats
PgStatShared_HashEntry * shared_entry
dlist_node pending_node
PgStat_Kind kind
bool accessed_across_databases
void(* to_serialized_name)(const PgStatShared_Common *header, NameData *name)
bool(* from_serialized_name)(const NameData *name, PgStat_HashKey *key)
bool(* flush_pending_cb)(PgStat_EntryRef *sr, bool nowait)
void(* reset_all_cb)(TimestampTz ts)
const char *const name
void(* delete_pending_cb)(PgStat_EntryRef *sr)
void(* snapshot_cb)(void)
PgStat_Snapshot snapshot
dshash_table * shared_hash
PgStat_ShmemControl * shmem
PgStatShared_SLRU slru
PgStatShared_Wal wal
PgStatShared_Archiver archiver
PgStatShared_Checkpointer checkpointer
PgStatShared_BgWriter bgwriter
PgStat_HashKey key
Definition: pgstat.c:135
PgStat_CheckpointerStats checkpointer
TimestampTz snapshot_timestamp
MemoryContext context
PgStat_WalStats wal
bool fixed_valid[PGSTAT_NUM_KINDS]
PgStat_ArchiverStats archiver
PgStat_FetchConsistency mode
PgStat_SLRUStats slru[SLRU_NUM_ELEMENTS]
struct pgstat_snapshot_hash * stats
PgStat_BgWriterStats bgwriter
Definition: c.h:676
bool IsTransactionBlock(void)
Definition: xact.c:4766
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition: xact.c:872