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