PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 (if the stats kind allows it), except when shutting down in
16 * immediate mode.
17 *
18 * Fixed-numbered stats are stored in plain (non-dynamic) shared memory.
19 *
20 * Statistics for variable-numbered objects are stored in dynamic shared
21 * memory and can be found via a dshash hashtable. The statistics counters are
22 * not part of the dshash entry (PgStatShared_HashEntry) directly, but are
23 * separately allocated (PgStatShared_HashEntry->body). The separate
24 * allocation allows different kinds of statistics to be stored in the same
25 * hashtable without wasting space in PgStatShared_HashEntry.
26 *
27 * Variable-numbered stats are addressed by PgStat_HashKey while running. It
28 * is not possible to have statistics for an object that cannot be addressed
29 * that way at runtime. A wider identifier can be used when serializing to
30 * disk (used for replication slot stats).
31 *
32 * To avoid contention on the shared hashtable, each backend has a
33 * backend-local hashtable (pgStatEntryRefHash) in front of the shared
34 * hashtable, containing references (PgStat_EntryRef) to shared hashtable
35 * entries. The shared hashtable only needs to be accessed when no prior
36 * reference is found in the local hashtable. Besides pointing to the
37 * shared hashtable entry (PgStatShared_HashEntry) PgStat_EntryRef also
38 * contains a pointer to the shared statistics data, as a process-local
39 * address, to reduce access costs.
40 *
41 * The names for structs stored in shared memory are prefixed with
42 * PgStatShared instead of PgStat. Each stats entry in shared memory is
43 * protected by a dedicated lwlock.
44 *
45 * Most stats updates are first accumulated locally in each process as pending
46 * entries, then later flushed to shared memory (just after commit, or by
47 * idle-timeout). This practically eliminates contention on individual stats
48 * entries. For most kinds of variable-numbered pending stats data is stored
49 * in PgStat_EntryRef->pending. All entries with pending data are in the
50 * pgStatPending list. Pending statistics updates are flushed out by
51 * pgstat_report_stat().
52 *
53 * It is possible for external modules to define custom statistics kinds,
54 * that can use the same properties as any built-in stats kinds. Each custom
55 * stats kind needs to assign a unique ID to ensure that it does not overlap
56 * with other extensions. In order to reserve a unique stats kind ID, refer
57 * to https://wiki.postgresql.org/wiki/CustomCumulativeStats.
58 *
59 * The behavior of different kinds of statistics is determined by the kind's
60 * entry in pgstat_kind_builtin_infos for all the built-in statistics kinds
61 * defined, and pgstat_kind_custom_infos for custom kinds registered at
62 * startup by pgstat_register_kind(). See PgStat_KindInfo for details.
63 *
64 * The consistency of read accesses to statistics can be configured using the
65 * stats_fetch_consistency GUC (see config.sgml and monitoring.sgml for the
66 * settings). When using PGSTAT_FETCH_CONSISTENCY_CACHE or
67 * PGSTAT_FETCH_CONSISTENCY_SNAPSHOT statistics are stored in
68 * pgStatLocal.snapshot.
69 *
70 * To keep things manageable, stats handling is split across several
71 * files. Infrastructure pieces are in:
72 * - pgstat.c - this file, to tie it all together
73 * - pgstat_shmem.c - nearly everything dealing with shared memory, including
74 * the maintenance of hashtable entries
75 * - pgstat_xact.c - transactional integration, including the transactional
76 * creation and dropping of stats entries
77 *
78 * Each statistics kind is handled in a dedicated file:
79 * - pgstat_archiver.c
80 * - pgstat_backend.c
81 * - pgstat_bgwriter.c
82 * - pgstat_checkpointer.c
83 * - pgstat_database.c
84 * - pgstat_function.c
85 * - pgstat_io.c
86 * - pgstat_relation.c
87 * - pgstat_replslot.c
88 * - pgstat_slru.c
89 * - pgstat_subscription.c
90 * - pgstat_wal.c
91 *
92 * Whenever possible infrastructure files should not contain code related to
93 * specific kinds of stats.
94 *
95 *
96 * Copyright (c) 2001-2025, PostgreSQL Global Development Group
97 *
98 * IDENTIFICATION
99 * src/backend/utils/activity/pgstat.c
100 * ----------
101 */
102#include "postgres.h"
103
104#include <unistd.h>
105
106#include "access/xact.h"
107#include "lib/dshash.h"
108#include "pgstat.h"
109#include "storage/fd.h"
110#include "storage/ipc.h"
111#include "storage/lwlock.h"
112#include "utils/guc_hooks.h"
113#include "utils/memutils.h"
115#include "utils/timestamp.h"
116
117
118/* ----------
119 * Timer definitions.
120 *
121 * In milliseconds.
122 * ----------
123 */
124
125/* minimum interval non-forced stats flushes.*/
126#define PGSTAT_MIN_INTERVAL 1000
127/* how long until to block flushing pending stats updates */
128#define PGSTAT_MAX_INTERVAL 60000
129/* when to call pgstat_report_stat() again, even when idle */
130#define PGSTAT_IDLE_INTERVAL 10000
131
132/* ----------
133 * Initial size hints for the hash tables used in statistics.
134 * ----------
135 */
136
137#define PGSTAT_SNAPSHOT_HASH_SIZE 512
138
139/* ---------
140 * Identifiers in stats file.
141 * ---------
142 */
143#define PGSTAT_FILE_ENTRY_END 'E' /* end of file */
144#define PGSTAT_FILE_ENTRY_FIXED 'F' /* fixed-numbered stats entry */
145#define PGSTAT_FILE_ENTRY_NAME 'N' /* stats entry identified by name */
146#define PGSTAT_FILE_ENTRY_HASH 'S' /* stats entry identified by
147 * PgStat_HashKey */
148
149/* hash table for statistics snapshots entry */
150typedef struct PgStat_SnapshotEntry
153 char status; /* for simplehash use */
154 void *data; /* the stats data itself */
156
157
158/* ----------
159 * Backend-local Hash Table Definitions
160 * ----------
161 */
162
163/* for stats snapshot entries */
164#define SH_PREFIX pgstat_snapshot
165#define SH_ELEMENT_TYPE PgStat_SnapshotEntry
166#define SH_KEY_TYPE PgStat_HashKey
167#define SH_KEY key
168#define SH_HASH_KEY(tb, key) \
169 pgstat_hash_hash_key(&key, sizeof(PgStat_HashKey), NULL)
170#define SH_EQUAL(tb, a, b) \
171 pgstat_cmp_hash_key(&a, &b, sizeof(PgStat_HashKey), NULL) == 0
172#define SH_SCOPE static inline
173#define SH_DEFINE
174#define SH_DECLARE
175#include "lib/simplehash.h"
176
177
178/* ----------
179 * Local function forward declarations
180 * ----------
181 */
182
183static void pgstat_write_statsfile(void);
184static void pgstat_read_statsfile(void);
185
186static void pgstat_init_snapshot_fixed(void);
187
188static void pgstat_reset_after_failure(void);
189
190static bool pgstat_flush_pending_entries(bool nowait);
191
192static void pgstat_prep_snapshot(void);
193static void pgstat_build_snapshot(void);
195
196static inline bool pgstat_is_kind_valid(PgStat_Kind kind);
197
198
199/* ----------
200 * GUC parameters
201 * ----------
202 */
206
207
208/* ----------
209 * state shared with pgstat_*.c
210 * ----------
211 */
214
215
216/* ----------
217 * Local data
218 *
219 * NB: There should be only variables related to stats infrastructure here,
220 * not for specific kinds of stats.
221 * ----------
222 */
223
224/*
225 * Memory contexts containing the pgStatEntryRefHash table, the
226 * pgStatSharedRef entries, and pending data respectively. Mostly to make it
227 * easier to track / attribute memory usage.
228 */
231
232/*
233 * Backend local list of PgStat_EntryRef with unflushed pending stats.
234 *
235 * Newly pending entries should only ever be added to the end of the list,
236 * otherwise pgstat_flush_pending_entries() might not see them immediately.
237 */
239
240
241/*
242 * Force the next stats flush to happen regardless of
243 * PGSTAT_MIN_INTERVAL. Useful in test scripts.
244 */
245static bool pgStatForceNextFlush = false;
246
247/*
248 * Force-clear existing snapshot before next use when stats_fetch_consistency
249 * is changed.
250 */
251static bool force_stats_snapshot_clear = false;
252
253
254/*
255 * For assertions that check pgstat is not used before initialization / after
256 * shutdown.
257 */
258#ifdef USE_ASSERT_CHECKING
259static bool pgstat_is_initialized = false;
260static bool pgstat_is_shutdown = false;
261#endif
262
263
264/*
265 * The different kinds of built-in statistics.
266 *
267 * If reasonably possible, handling specific to one kind of stats should go
268 * through this abstraction, rather than making more of pgstat.c aware.
269 *
270 * See comments for struct PgStat_KindInfo for details about the individual
271 * fields.
272 *
273 * XXX: It'd be nicer to define this outside of this file. But there doesn't
274 * seem to be a great way of doing that, given the split across multiple
275 * files.
276 */
278
279 /* stats kinds for variable-numbered objects */
280
282 .name = "database",
283
284 .fixed_amount = false,
285 .write_to_file = true,
286 /* so pg_stat_database entries can be seen in all databases */
287 .accessed_across_databases = true,
288
289 .shared_size = sizeof(PgStatShared_Database),
290 .shared_data_off = offsetof(PgStatShared_Database, stats),
291 .shared_data_len = sizeof(((PgStatShared_Database *) 0)->stats),
292 .pending_size = sizeof(PgStat_StatDBEntry),
293
294 .flush_pending_cb = pgstat_database_flush_cb,
295 .reset_timestamp_cb = pgstat_database_reset_timestamp_cb,
296 },
297
299 .name = "relation",
300
301 .fixed_amount = false,
302 .write_to_file = true,
303
304 .shared_size = sizeof(PgStatShared_Relation),
305 .shared_data_off = offsetof(PgStatShared_Relation, stats),
306 .shared_data_len = sizeof(((PgStatShared_Relation *) 0)->stats),
307 .pending_size = sizeof(PgStat_TableStatus),
308
309 .flush_pending_cb = pgstat_relation_flush_cb,
310 .delete_pending_cb = pgstat_relation_delete_pending_cb,
311 },
312
314 .name = "function",
315
316 .fixed_amount = false,
317 .write_to_file = true,
318
319 .shared_size = sizeof(PgStatShared_Function),
320 .shared_data_off = offsetof(PgStatShared_Function, stats),
321 .shared_data_len = sizeof(((PgStatShared_Function *) 0)->stats),
322 .pending_size = sizeof(PgStat_FunctionCounts),
323
324 .flush_pending_cb = pgstat_function_flush_cb,
325 },
326
328 .name = "replslot",
329
330 .fixed_amount = false,
331 .write_to_file = true,
332
333 .accessed_across_databases = true,
334
335 .shared_size = sizeof(PgStatShared_ReplSlot),
336 .shared_data_off = offsetof(PgStatShared_ReplSlot, stats),
337 .shared_data_len = sizeof(((PgStatShared_ReplSlot *) 0)->stats),
338
339 .reset_timestamp_cb = pgstat_replslot_reset_timestamp_cb,
340 .to_serialized_name = pgstat_replslot_to_serialized_name_cb,
341 .from_serialized_name = pgstat_replslot_from_serialized_name_cb,
342 },
343
345 .name = "subscription",
346
347 .fixed_amount = false,
348 .write_to_file = true,
349 /* so pg_stat_subscription_stats entries can be seen in all databases */
350 .accessed_across_databases = true,
351
352 .shared_size = sizeof(PgStatShared_Subscription),
353 .shared_data_off = offsetof(PgStatShared_Subscription, stats),
354 .shared_data_len = sizeof(((PgStatShared_Subscription *) 0)->stats),
355 .pending_size = sizeof(PgStat_BackendSubEntry),
356
357 .flush_pending_cb = pgstat_subscription_flush_cb,
358 .reset_timestamp_cb = pgstat_subscription_reset_timestamp_cb,
359 },
360
362 .name = "backend",
363
364 .fixed_amount = false,
365 .write_to_file = false,
366
367 .accessed_across_databases = true,
368
369 .shared_size = sizeof(PgStatShared_Backend),
370 .shared_data_off = offsetof(PgStatShared_Backend, stats),
371 .shared_data_len = sizeof(((PgStatShared_Backend *) 0)->stats),
372
373 .have_static_pending_cb = pgstat_backend_have_pending_cb,
374 .flush_static_cb = pgstat_backend_flush_cb,
375 .reset_timestamp_cb = pgstat_backend_reset_timestamp_cb,
376 },
377
378 /* stats for fixed-numbered (mostly 1) objects */
379
381 .name = "archiver",
382
383 .fixed_amount = true,
384 .write_to_file = true,
385
386 .snapshot_ctl_off = offsetof(PgStat_Snapshot, archiver),
387 .shared_ctl_off = offsetof(PgStat_ShmemControl, archiver),
388 .shared_data_off = offsetof(PgStatShared_Archiver, stats),
389 .shared_data_len = sizeof(((PgStatShared_Archiver *) 0)->stats),
390
391 .init_shmem_cb = pgstat_archiver_init_shmem_cb,
392 .reset_all_cb = pgstat_archiver_reset_all_cb,
393 .snapshot_cb = pgstat_archiver_snapshot_cb,
394 },
395
397 .name = "bgwriter",
398
399 .fixed_amount = true,
400 .write_to_file = true,
401
402 .snapshot_ctl_off = offsetof(PgStat_Snapshot, bgwriter),
403 .shared_ctl_off = offsetof(PgStat_ShmemControl, bgwriter),
404 .shared_data_off = offsetof(PgStatShared_BgWriter, stats),
405 .shared_data_len = sizeof(((PgStatShared_BgWriter *) 0)->stats),
406
407 .init_shmem_cb = pgstat_bgwriter_init_shmem_cb,
408 .reset_all_cb = pgstat_bgwriter_reset_all_cb,
409 .snapshot_cb = pgstat_bgwriter_snapshot_cb,
410 },
411
413 .name = "checkpointer",
414
415 .fixed_amount = true,
416 .write_to_file = true,
417
418 .snapshot_ctl_off = offsetof(PgStat_Snapshot, checkpointer),
419 .shared_ctl_off = offsetof(PgStat_ShmemControl, checkpointer),
420 .shared_data_off = offsetof(PgStatShared_Checkpointer, stats),
421 .shared_data_len = sizeof(((PgStatShared_Checkpointer *) 0)->stats),
422
423 .init_shmem_cb = pgstat_checkpointer_init_shmem_cb,
424 .reset_all_cb = pgstat_checkpointer_reset_all_cb,
425 .snapshot_cb = pgstat_checkpointer_snapshot_cb,
426 },
427
428 [PGSTAT_KIND_IO] = {
429 .name = "io",
430
431 .fixed_amount = true,
432 .write_to_file = true,
433
434 .snapshot_ctl_off = offsetof(PgStat_Snapshot, io),
435 .shared_ctl_off = offsetof(PgStat_ShmemControl, io),
436 .shared_data_off = offsetof(PgStatShared_IO, stats),
437 .shared_data_len = sizeof(((PgStatShared_IO *) 0)->stats),
438
439 .flush_static_cb = pgstat_io_flush_cb,
440 .have_static_pending_cb = pgstat_io_have_pending_cb,
441 .init_shmem_cb = pgstat_io_init_shmem_cb,
442 .reset_all_cb = pgstat_io_reset_all_cb,
443 .snapshot_cb = pgstat_io_snapshot_cb,
444 },
445
446 [PGSTAT_KIND_SLRU] = {
447 .name = "slru",
448
449 .fixed_amount = true,
450 .write_to_file = true,
451
452 .snapshot_ctl_off = offsetof(PgStat_Snapshot, slru),
453 .shared_ctl_off = offsetof(PgStat_ShmemControl, slru),
454 .shared_data_off = offsetof(PgStatShared_SLRU, stats),
455 .shared_data_len = sizeof(((PgStatShared_SLRU *) 0)->stats),
456
457 .flush_static_cb = pgstat_slru_flush_cb,
458 .have_static_pending_cb = pgstat_slru_have_pending_cb,
459 .init_shmem_cb = pgstat_slru_init_shmem_cb,
460 .reset_all_cb = pgstat_slru_reset_all_cb,
461 .snapshot_cb = pgstat_slru_snapshot_cb,
462 },
463
464 [PGSTAT_KIND_WAL] = {
465 .name = "wal",
466
467 .fixed_amount = true,
468 .write_to_file = true,
469
470 .snapshot_ctl_off = offsetof(PgStat_Snapshot, wal),
471 .shared_ctl_off = offsetof(PgStat_ShmemControl, wal),
472 .shared_data_off = offsetof(PgStatShared_Wal, stats),
473 .shared_data_len = sizeof(((PgStatShared_Wal *) 0)->stats),
474
475 .init_backend_cb = pgstat_wal_init_backend_cb,
476 .flush_static_cb = pgstat_wal_flush_cb,
477 .have_static_pending_cb = pgstat_wal_have_pending_cb,
478 .init_shmem_cb = pgstat_wal_init_shmem_cb,
479 .reset_all_cb = pgstat_wal_reset_all_cb,
480 .snapshot_cb = pgstat_wal_snapshot_cb,
481 },
482};
483
484/*
485 * Information about custom statistics kinds.
486 *
487 * These are saved in a different array than the built-in kinds to save
488 * in clarity with the initializations.
489 *
490 * Indexed by PGSTAT_KIND_CUSTOM_MIN, of size PGSTAT_KIND_CUSTOM_SIZE.
491 */
492static const PgStat_KindInfo **pgstat_kind_custom_infos = NULL;
493
494/* ------------------------------------------------------------
495 * Functions managing the state of the stats system for all backends.
496 * ------------------------------------------------------------
497 */
498
499/*
500 * Read on-disk stats into memory at server start.
501 *
502 * Should only be called by the startup process or in single user mode.
503 */
504void
506{
508}
509
510/*
511 * Remove the stats file. This is currently used only if WAL recovery is
512 * needed after a crash.
513 *
514 * Should only be called by the startup process or in single user mode.
515 */
516void
518{
519 int ret;
520
521 /* NB: this needs to be done even in single user mode */
522
523 ret = unlink(PGSTAT_STAT_PERMANENT_FILENAME);
524 if (ret != 0)
525 {
526 if (errno == ENOENT)
527 elog(DEBUG2,
528 "didn't need to unlink permanent stats file \"%s\" - didn't exist",
530 else
531 ereport(LOG,
533 errmsg("could not unlink permanent statistics file \"%s\": %m",
535 }
536 else
537 {
540 errmsg_internal("unlinked permanent statistics file \"%s\"",
542 }
543
544 /*
545 * Reset stats contents. This will set reset timestamps of fixed-numbered
546 * stats to the current time (no variable stats exist).
547 */
549}
550
551/*
552 * pgstat_before_server_shutdown() needs to be called by exactly one process
553 * during regular server shutdowns. Otherwise all stats will be lost.
554 *
555 * We currently only write out stats for proc_exit(0). We might want to change
556 * that at some point... But right now pgstat_discard_stats() would be called
557 * during the start after a disorderly shutdown, anyway.
558 */
559void
561{
562 Assert(pgStatLocal.shmem != NULL);
564
565 /*
566 * Stats should only be reported after pgstat_initialize() and before
567 * pgstat_shutdown(). This is a convenient point to catch most violations
568 * of this rule.
569 */
570 Assert(pgstat_is_initialized && !pgstat_is_shutdown);
571
572 /* flush out our own pending changes before writing out */
573 pgstat_report_stat(true);
574
575 /*
576 * Only write out file during normal shutdown. Don't even signal that
577 * we've shutdown during irregular shutdowns, because the shutdown
578 * sequence isn't coordinated to ensure this backend shuts down last.
579 */
580 if (code == 0)
581 {
584 }
585}
586
587
588/* ------------------------------------------------------------
589 * Backend initialization / shutdown functions
590 * ------------------------------------------------------------
591 */
592
593/*
594 * Shut down a single backend's statistics reporting at process exit.
595 *
596 * Flush out any remaining statistics counts. Without this, operations
597 * triggered during backend exit (such as temp table deletions) won't be
598 * counted.
599 */
600static void
602{
603 Assert(!pgstat_is_shutdown);
605
606 /*
607 * If we got as far as discovering our own database ID, we can flush out
608 * what we did so far. Otherwise, we'd be reporting an invalid database
609 * ID, so forget it. (This means that accesses to pg_database during
610 * failed backend starts might never get counted.)
611 */
614
615 pgstat_report_stat(true);
616
617 /* there shouldn't be any pending changes left */
620
621 /* drop the backend stats entry */
624
626
627#ifdef USE_ASSERT_CHECKING
628 pgstat_is_shutdown = true;
629#endif
630}
631
632/*
633 * Initialize pgstats state, and set up our on-proc-exit hook. Called from
634 * BaseInit().
635 *
636 * NOTE: MyDatabaseId isn't set yet; so the shutdown hook has to be careful.
637 */
638void
640{
641 Assert(!pgstat_is_initialized);
642
644
646
647 /* Backend initialization callbacks */
648 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
649 {
650 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
651
652 if (kind_info == NULL || kind_info->init_backend_cb == NULL)
653 continue;
654
655 kind_info->init_backend_cb();
656 }
657
658 /* Set up a process-exit hook to clean up */
660
661#ifdef USE_ASSERT_CHECKING
662 pgstat_is_initialized = true;
663#endif
664}
665
666
667/* ------------------------------------------------------------
668 * Public functions used by backends follow
669 * ------------------------------------------------------------
670 */
671
672/*
673 * Must be called by processes that performs DML: tcop/postgres.c, logical
674 * receiver processes, SPI worker, etc. to flush pending statistics updates to
675 * shared memory.
676 *
677 * Unless called with 'force', pending stats updates are flushed happen once
678 * per PGSTAT_MIN_INTERVAL (1000ms). When not forced, stats flushes do not
679 * block on lock acquisition, except if stats updates have been pending for
680 * longer than PGSTAT_MAX_INTERVAL (60000ms).
681 *
682 * Whenever pending stats updates remain at the end of pgstat_report_stat() a
683 * suggested idle timeout is returned. Currently this is always
684 * PGSTAT_IDLE_INTERVAL (10000ms). Callers can use the returned time to set up
685 * a timeout after which to call pgstat_report_stat(true), but are not
686 * required to do so.
687 *
688 * Note that this is called only when not within a transaction, so it is fair
689 * to use transaction stop time as an approximation of current time.
690 */
691long
692pgstat_report_stat(bool force)
693{
694 static TimestampTz pending_since = 0;
695 static TimestampTz last_flush = 0;
696 bool partial_flush;
698 bool nowait;
699
702
703 /* "absorb" the forced flush even if there's nothing to flush */
705 {
706 force = true;
707 pgStatForceNextFlush = false;
708 }
709
710 /* Don't expend a clock check if nothing to do */
712 {
713 bool do_flush = false;
714
715 /* Check for pending stats */
716 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
717 {
718 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
719
720 if (!kind_info)
721 continue;
722 if (!kind_info->have_static_pending_cb)
723 continue;
724
725 if (kind_info->have_static_pending_cb())
726 {
727 do_flush = true;
728 break;
729 }
730 }
731
732 if (!do_flush)
733 {
734 Assert(pending_since == 0);
735 return 0;
736 }
737 }
738
739 /*
740 * There should never be stats to report once stats are shut down. Can't
741 * assert that before the checks above, as there is an unconditional
742 * pgstat_report_stat() call in pgstat_shutdown_hook() - which at least
743 * the process that ran pgstat_before_server_shutdown() will still call.
744 */
746
747 if (force)
748 {
749 /*
750 * Stats reports are forced either when it's been too long since stats
751 * have been reported or in processes that force stats reporting to
752 * happen at specific points (including shutdown). In the former case
753 * the transaction stop time might be quite old, in the latter it
754 * would never get cleared.
755 */
757 }
758 else
759 {
761
762 if (pending_since > 0 &&
764 {
765 /* don't keep pending updates longer than PGSTAT_MAX_INTERVAL */
766 force = true;
767 }
768 else if (last_flush > 0 &&
770 {
771 /* don't flush too frequently */
772 if (pending_since == 0)
773 pending_since = now;
774
776 }
777 }
778
780
781 /* don't wait for lock acquisition when !force */
782 nowait = !force;
783
784 partial_flush = false;
785
786 /* flush of variable-numbered stats tracked in pending entries list */
787 partial_flush |= pgstat_flush_pending_entries(nowait);
788
789 /* flush of other stats kinds */
790 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
791 {
792 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
793
794 if (!kind_info)
795 continue;
796 if (!kind_info->flush_static_cb)
797 continue;
798
799 partial_flush |= kind_info->flush_static_cb(nowait);
800 }
801
802 last_flush = now;
803
804 /*
805 * If some of the pending stats could not be flushed due to lock
806 * contention, let the caller know when to retry.
807 */
808 if (partial_flush)
809 {
810 /* force should have prevented us from getting here */
811 Assert(!force);
812
813 /* remember since when stats have been pending */
814 if (pending_since == 0)
815 pending_since = now;
816
818 }
819
820 pending_since = 0;
821
822 return 0;
823}
824
825/*
826 * Force locally pending stats to be flushed during the next
827 * pgstat_report_stat() call. This is useful for writing tests.
828 */
829void
831{
833}
834
835/*
836 * Only for use by pgstat_reset_counters()
837 */
838static bool
840{
841 return entry->key.dboid == DatumGetObjectId(MyDatabaseId);
842}
843
844/*
845 * Reset counters for our database.
846 *
847 * Permission checking for this function is managed through the normal
848 * GRANT system.
849 */
850void
852{
854
857 ts);
858}
859
860/*
861 * Reset a single variable-numbered entry.
862 *
863 * If the stats kind is within a database, also reset the database's
864 * stat_reset_timestamp.
865 *
866 * Permission checking for this function is managed through the normal
867 * GRANT system.
868 */
869void
870pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
871{
872 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
874
875 /* not needed atm, and doesn't make sense with the current signature */
876 Assert(!pgstat_get_kind_info(kind)->fixed_amount);
877
878 /* reset the "single counter" */
879 pgstat_reset_entry(kind, dboid, objid, ts);
880
881 if (!kind_info->accessed_across_databases)
883}
884
885/*
886 * Reset stats for all entries of a kind.
887 *
888 * Permission checking for this function is managed through the normal
889 * GRANT system.
890 */
891void
893{
894 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
896
897 if (kind_info->fixed_amount)
898 kind_info->reset_all_cb(ts);
899 else
901}
902
903
904/* ------------------------------------------------------------
905 * Fetching of stats
906 * ------------------------------------------------------------
907 */
908
909/*
910 * Discard any data collected in the current transaction. Any subsequent
911 * request will cause new snapshots to be read.
912 *
913 * This is also invoked during transaction commit or abort to discard
914 * the no-longer-wanted snapshot. Updates of stats_fetch_consistency can
915 * cause this routine to be called.
916 */
917void
919{
921
928
929 /* Release memory, if any was allocated */
931 {
933
934 /* Reset variables */
936 }
937
938 /*
939 * Historically the backend_status.c facilities lived in this file, and
940 * were reset with the same function. For now keep it that way, and
941 * forward the reset request.
942 */
944
945 /* Reset this flag, as it may be possible that a cleanup was forced. */
947}
948
949void *
950pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
951{
953 PgStat_EntryRef *entry_ref;
954 void *stats_data;
955 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
956
957 /* should be called from backends */
959 Assert(!kind_info->fixed_amount);
960
962
963 /* clear padding */
964 memset(&key, 0, sizeof(struct PgStat_HashKey));
965
966 key.kind = kind;
967 key.dboid = dboid;
968 key.objid = objid;
969
970 /* if we need to build a full snapshot, do so */
973
974 /* if caching is desired, look up in cache */
976 {
977 PgStat_SnapshotEntry *entry = NULL;
978
979 entry = pgstat_snapshot_lookup(pgStatLocal.snapshot.stats, key);
980
981 if (entry)
982 return entry->data;
983
984 /*
985 * If we built a full snapshot and the key is not in
986 * pgStatLocal.snapshot.stats, there are no matching stats.
987 */
989 return NULL;
990 }
991
993
994 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
995
996 if (entry_ref == NULL || entry_ref->shared_entry->dropped)
997 {
998 /* create empty entry when using PGSTAT_FETCH_CONSISTENCY_CACHE */
1000 {
1001 PgStat_SnapshotEntry *entry = NULL;
1002 bool found;
1003
1004 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
1005 Assert(!found);
1006 entry->data = NULL;
1007 }
1008 return NULL;
1009 }
1010
1011 /*
1012 * Allocate in caller's context for PGSTAT_FETCH_CONSISTENCY_NONE,
1013 * otherwise we could quickly end up with a fair bit of memory used due to
1014 * repeated accesses.
1015 */
1017 stats_data = palloc(kind_info->shared_data_len);
1018 else
1020 kind_info->shared_data_len);
1021
1022 (void) pgstat_lock_entry_shared(entry_ref, false);
1023 memcpy(stats_data,
1024 pgstat_get_entry_data(kind, entry_ref->shared_stats),
1025 kind_info->shared_data_len);
1026 pgstat_unlock_entry(entry_ref);
1027
1029 {
1030 PgStat_SnapshotEntry *entry = NULL;
1031 bool found;
1032
1033 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
1034 entry->data = stats_data;
1035 }
1036
1037 return stats_data;
1038}
1039
1040/*
1041 * If a stats snapshot has been taken, return the timestamp at which that was
1042 * done, and set *have_snapshot to true. Otherwise *have_snapshot is set to
1043 * false.
1044 */
1046pgstat_get_stat_snapshot_timestamp(bool *have_snapshot)
1047{
1050
1052 {
1053 *have_snapshot = true;
1055 }
1056
1057 *have_snapshot = false;
1058
1059 return 0;
1060}
1061
1062bool
1063pgstat_have_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
1064{
1065 /* fixed-numbered stats always exist */
1066 if (pgstat_get_kind_info(kind)->fixed_amount)
1067 return true;
1068
1069 return pgstat_get_entry_ref(kind, dboid, objid, false, NULL) != NULL;
1070}
1071
1072/*
1073 * Ensure snapshot for fixed-numbered 'kind' exists.
1074 *
1075 * Typically used by the pgstat_fetch_* functions for a kind of stats, before
1076 * massaging the data into the desired format.
1077 */
1078void
1080{
1082 Assert(pgstat_get_kind_info(kind)->fixed_amount);
1083
1086
1089 else
1091
1092 if (pgstat_is_kind_builtin(kind))
1094 else if (pgstat_is_kind_custom(kind))
1096}
1097
1098static void
1100{
1101 /*
1102 * Initialize fixed-numbered statistics data in snapshots, only for custom
1103 * stats kinds.
1104 */
1105 for (PgStat_Kind kind = PGSTAT_KIND_CUSTOM_MIN; kind <= PGSTAT_KIND_CUSTOM_MAX; kind++)
1106 {
1107 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1108
1109 if (!kind_info || !kind_info->fixed_amount)
1110 continue;
1111
1114 }
1115}
1116
1117static void
1119{
1122
1124 pgStatLocal.snapshot.stats != NULL)
1125 return;
1126
1129 "PgStat Snapshot",
1131
1133 pgstat_snapshot_create(pgStatLocal.snapshot.context,
1135 NULL);
1136}
1137
1138static void
1140{
1141 dshash_seq_status hstat;
1143
1144 /* should only be called when we need a snapshot */
1146
1147 /* snapshot already built */
1149 return;
1150
1152
1153 Assert(pgStatLocal.snapshot.stats->members == 0);
1154
1156
1157 /*
1158 * Snapshot all variable stats.
1159 */
1160 dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
1161 while ((p = dshash_seq_next(&hstat)) != NULL)
1162 {
1163 PgStat_Kind kind = p->key.kind;
1164 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1165 bool found;
1166 PgStat_SnapshotEntry *entry;
1167 PgStatShared_Common *stats_data;
1168
1169 /*
1170 * Check if the stats object should be included in the snapshot.
1171 * Unless the stats kind can be accessed from all databases (e.g.,
1172 * database stats themselves), we only include stats for the current
1173 * database or objects not associated with a database (e.g. shared
1174 * relations).
1175 */
1176 if (p->key.dboid != MyDatabaseId &&
1177 p->key.dboid != InvalidOid &&
1178 !kind_info->accessed_across_databases)
1179 continue;
1180
1181 if (p->dropped)
1182 continue;
1183
1185
1186 stats_data = dsa_get_address(pgStatLocal.dsa, p->body);
1187 Assert(stats_data);
1188
1189 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, p->key, &found);
1190 Assert(!found);
1191
1193 kind_info->shared_size);
1194
1195 /*
1196 * Acquire the LWLock directly instead of using
1197 * pg_stat_lock_entry_shared() which requires a reference.
1198 */
1199 LWLockAcquire(&stats_data->lock, LW_SHARED);
1200 memcpy(entry->data,
1201 pgstat_get_entry_data(kind, stats_data),
1202 kind_info->shared_size);
1203 LWLockRelease(&stats_data->lock);
1204 }
1205 dshash_seq_term(&hstat);
1206
1207 /*
1208 * Build snapshot of all fixed-numbered stats.
1209 */
1210 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
1211 {
1212 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1213
1214 if (!kind_info)
1215 continue;
1216 if (!kind_info->fixed_amount)
1217 {
1218 Assert(kind_info->snapshot_cb == NULL);
1219 continue;
1220 }
1221
1223 }
1224
1226}
1227
1228static void
1230{
1231 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1232 int idx;
1233 bool *valid;
1234
1235 /* Position in fixed_valid or custom_valid */
1236 if (pgstat_is_kind_builtin(kind))
1237 {
1238 idx = kind;
1240 }
1241 else
1242 {
1243 idx = kind - PGSTAT_KIND_CUSTOM_MIN;
1245 }
1246
1247 Assert(kind_info->fixed_amount);
1248 Assert(kind_info->snapshot_cb != NULL);
1249
1251 {
1252 /* rebuild every time */
1253 valid[idx] = false;
1254 }
1255 else if (valid[idx])
1256 {
1257 /* in snapshot mode we shouldn't get called again */
1259 return;
1260 }
1261
1262 Assert(!valid[idx]);
1263
1264 kind_info->snapshot_cb();
1265
1266 Assert(!valid[idx]);
1267 valid[idx] = true;
1268}
1269
1270
1271/* ------------------------------------------------------------
1272 * Backend-local pending stats infrastructure
1273 * ------------------------------------------------------------
1274 */
1275
1276/*
1277 * Returns the appropriate PgStat_EntryRef, preparing it to receive pending
1278 * stats if not already done.
1279 *
1280 * If created_entry is non-NULL, it'll be set to true if the entry is newly
1281 * created, false otherwise.
1282 */
1284pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *created_entry)
1285{
1286 PgStat_EntryRef *entry_ref;
1287
1288 /* need to be able to flush out */
1289 Assert(pgstat_get_kind_info(kind)->flush_pending_cb != NULL);
1290
1292 {
1295 "PgStat Pending",
1297 }
1298
1299 entry_ref = pgstat_get_entry_ref(kind, dboid, objid,
1300 true, created_entry);
1301
1302 if (entry_ref->pending == NULL)
1303 {
1304 size_t entrysize = pgstat_get_kind_info(kind)->pending_size;
1305
1306 Assert(entrysize != (size_t) -1);
1307
1308 entry_ref->pending = MemoryContextAllocZero(pgStatPendingContext, entrysize);
1310 }
1311
1312 return entry_ref;
1313}
1314
1315/*
1316 * Return an existing stats entry, or NULL.
1317 *
1318 * This should only be used for helper function for pgstatfuncs.c - outside of
1319 * that it shouldn't be needed.
1320 */
1323{
1324 PgStat_EntryRef *entry_ref;
1325
1326 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
1327
1328 if (entry_ref == NULL || entry_ref->pending == NULL)
1329 return NULL;
1330
1331 return entry_ref;
1332}
1333
1334void
1336{
1337 PgStat_Kind kind = entry_ref->shared_entry->key.kind;
1338 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1339 void *pending_data = entry_ref->pending;
1340
1341 Assert(pending_data != NULL);
1342 /* !fixed_amount stats should be handled explicitly */
1343 Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1344
1345 if (kind_info->delete_pending_cb)
1346 kind_info->delete_pending_cb(entry_ref);
1347
1348 pfree(pending_data);
1349 entry_ref->pending = NULL;
1350
1351 dlist_delete(&entry_ref->pending_node);
1352}
1353
1354/*
1355 * Flush out pending variable-numbered stats.
1356 */
1357static bool
1359{
1360 bool have_pending = false;
1361 dlist_node *cur = NULL;
1362
1363 /*
1364 * Need to be a bit careful iterating over the list of pending entries.
1365 * Processing a pending entry may queue further pending entries to the end
1366 * of the list that we want to process, so a simple iteration won't do.
1367 * Further complicating matters is that we want to delete the current
1368 * entry in each iteration from the list if we flushed successfully.
1369 *
1370 * So we just keep track of the next pointer in each loop iteration.
1371 */
1374
1375 while (cur)
1376 {
1377 PgStat_EntryRef *entry_ref =
1378 dlist_container(PgStat_EntryRef, pending_node, cur);
1379 PgStat_HashKey key = entry_ref->shared_entry->key;
1380 PgStat_Kind kind = key.kind;
1381 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1382 bool did_flush;
1384
1385 Assert(!kind_info->fixed_amount);
1386 Assert(kind_info->flush_pending_cb != NULL);
1387
1388 /* flush the stats, if possible */
1389 did_flush = kind_info->flush_pending_cb(entry_ref, nowait);
1390
1391 Assert(did_flush || nowait);
1392
1393 /* determine next entry, before deleting the pending entry */
1396 else
1397 next = NULL;
1398
1399 /* if successfully flushed, remove entry */
1400 if (did_flush)
1401 pgstat_delete_pending_entry(entry_ref);
1402 else
1403 have_pending = true;
1404
1405 cur = next;
1406 }
1407
1408 Assert(dlist_is_empty(&pgStatPending) == !have_pending);
1409
1410 return have_pending;
1411}
1412
1413
1414/* ------------------------------------------------------------
1415 * Helper / infrastructure functions
1416 * ------------------------------------------------------------
1417 */
1418
1420pgstat_get_kind_from_str(char *kind_str)
1421{
1422 for (PgStat_Kind kind = PGSTAT_KIND_BUILTIN_MIN; kind <= PGSTAT_KIND_BUILTIN_MAX; kind++)
1423 {
1424 if (pg_strcasecmp(kind_str, pgstat_kind_builtin_infos[kind].name) == 0)
1425 return kind;
1426 }
1427
1428 /* Check the custom set of cumulative stats */
1430 {
1431 for (PgStat_Kind kind = PGSTAT_KIND_CUSTOM_MIN; kind <= PGSTAT_KIND_CUSTOM_MAX; kind++)
1432 {
1434
1437 return kind;
1438 }
1439 }
1440
1441 ereport(ERROR,
1442 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1443 errmsg("invalid statistics kind: \"%s\"", kind_str)));
1444 return PGSTAT_KIND_INVALID; /* avoid compiler warnings */
1445}
1446
1447static inline bool
1449{
1450 return pgstat_is_kind_builtin(kind) || pgstat_is_kind_custom(kind);
1451}
1452
1455{
1456 if (pgstat_is_kind_builtin(kind))
1457 return &pgstat_kind_builtin_infos[kind];
1458
1459 if (pgstat_is_kind_custom(kind))
1460 {
1462
1463 if (pgstat_kind_custom_infos == NULL ||
1465 return NULL;
1467 }
1468
1469 return NULL;
1470}
1471
1472/*
1473 * Register a new stats kind.
1474 *
1475 * PgStat_Kinds must be globally unique across all extensions. Refer
1476 * to https://wiki.postgresql.org/wiki/CustomCumulativeStats to reserve a
1477 * unique ID for your extension, to avoid conflicts with other extension
1478 * developers. During development, use PGSTAT_KIND_EXPERIMENTAL to avoid
1479 * needlessly reserving a new ID.
1480 */
1481void
1482pgstat_register_kind(PgStat_Kind kind, const PgStat_KindInfo *kind_info)
1483{
1485
1486 if (kind_info->name == NULL || strlen(kind_info->name) == 0)
1487 ereport(ERROR,
1488 (errmsg("custom cumulative statistics name is invalid"),
1489 errhint("Provide a non-empty name for the custom cumulative statistics.")));
1490
1491 if (!pgstat_is_kind_custom(kind))
1492 ereport(ERROR, (errmsg("custom cumulative statistics ID %u is out of range", kind),
1493 errhint("Provide a custom cumulative statistics ID between %u and %u.",
1495
1497 ereport(ERROR,
1498 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1499 errdetail("Custom cumulative statistics must be registered while initializing modules in \"shared_preload_libraries\".")));
1500
1501 /*
1502 * Check some data for fixed-numbered stats.
1503 */
1504 if (kind_info->fixed_amount)
1505 {
1506 if (kind_info->shared_size == 0)
1507 ereport(ERROR,
1508 (errmsg("custom cumulative statistics property is invalid"),
1509 errhint("Custom cumulative statistics require a shared memory size for fixed-numbered objects.")));
1510 }
1511
1512 /*
1513 * If pgstat_kind_custom_infos is not available yet, allocate it.
1514 */
1515 if (pgstat_kind_custom_infos == NULL)
1516 {
1520 }
1521
1522 if (pgstat_kind_custom_infos[idx] != NULL &&
1524 ereport(ERROR,
1525 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1526 errdetail("Custom cumulative statistics \"%s\" already registered with the same ID.",
1528
1529 /* check for existing custom stats with the same name */
1530 for (PgStat_Kind existing_kind = PGSTAT_KIND_CUSTOM_MIN; existing_kind <= PGSTAT_KIND_CUSTOM_MAX; existing_kind++)
1531 {
1532 uint32 existing_idx = existing_kind - PGSTAT_KIND_CUSTOM_MIN;
1533
1534 if (pgstat_kind_custom_infos[existing_idx] == NULL)
1535 continue;
1536 if (!pg_strcasecmp(pgstat_kind_custom_infos[existing_idx]->name, kind_info->name))
1537 ereport(ERROR,
1538 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1539 errdetail("Existing cumulative statistics with ID %u has the same name.", existing_kind)));
1540 }
1541
1542 /* Register it */
1543 pgstat_kind_custom_infos[idx] = kind_info;
1544 ereport(LOG,
1545 (errmsg("registered custom cumulative statistics \"%s\" with ID %u",
1546 kind_info->name, kind)));
1547}
1548
1549/*
1550 * Stats should only be reported after pgstat_initialize() and before
1551 * pgstat_shutdown(). This check is put in a few central places to catch
1552 * violations of this rule more easily.
1553 */
1554#ifdef USE_ASSERT_CHECKING
1555void
1557{
1558 Assert(pgstat_is_initialized && !pgstat_is_shutdown);
1559}
1560#endif
1561
1562
1563/* ------------------------------------------------------------
1564 * reading and writing of on-disk stats file
1565 * ------------------------------------------------------------
1566 */
1567
1568/* helpers for pgstat_write_statsfile() */
1569static void
1570write_chunk(FILE *fpout, void *ptr, size_t len)
1571{
1572 int rc;
1573
1574 rc = fwrite(ptr, len, 1, fpout);
1575
1576 /* we'll check for errors with ferror once at the end */
1577 (void) rc;
1578}
1580#define write_chunk_s(fpout, ptr) write_chunk(fpout, ptr, sizeof(*ptr))
1581
1582/*
1583 * This function is called in the last process that is accessing the shared
1584 * stats so locking is not required.
1585 */
1586static void
1588{
1589 FILE *fpout;
1590 int32 format_id;
1591 const char *tmpfile = PGSTAT_STAT_PERMANENT_TMPFILE;
1592 const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
1593 dshash_seq_status hstat;
1595
1597
1598 /* should be called only by the checkpointer or single user mode */
1600
1601 /* we're shutting down, so it's ok to just override this */
1603
1604 elog(DEBUG2, "writing stats file \"%s\"", statfile);
1605
1606 /*
1607 * Open the statistics temp file to write out the current values.
1608 */
1609 fpout = AllocateFile(tmpfile, PG_BINARY_W);
1610 if (fpout == NULL)
1611 {
1612 ereport(LOG,
1614 errmsg("could not open temporary statistics file \"%s\": %m",
1615 tmpfile)));
1616 return;
1617 }
1618
1619 /*
1620 * Write the file header --- currently just a format ID.
1621 */
1622 format_id = PGSTAT_FILE_FORMAT_ID;
1623 write_chunk_s(fpout, &format_id);
1624
1625 /* Write various stats structs for fixed number of objects */
1626 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
1627 {
1628 char *ptr;
1629 const PgStat_KindInfo *info = pgstat_get_kind_info(kind);
1630
1631 if (!info || !info->fixed_amount)
1632 continue;
1633
1634 if (pgstat_is_kind_builtin(kind))
1635 Assert(info->snapshot_ctl_off != 0);
1636
1637 /* skip if no need to write to file */
1638 if (!info->write_to_file)
1639 continue;
1640
1642 if (pgstat_is_kind_builtin(kind))
1643 ptr = ((char *) &pgStatLocal.snapshot) + info->snapshot_ctl_off;
1644 else
1646
1647 fputc(PGSTAT_FILE_ENTRY_FIXED, fpout);
1648 write_chunk_s(fpout, &kind);
1649 write_chunk(fpout, ptr, info->shared_data_len);
1650 }
1651
1652 /*
1653 * Walk through the stats entries
1654 */
1655 dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
1656 while ((ps = dshash_seq_next(&hstat)) != NULL)
1657 {
1658 PgStatShared_Common *shstats;
1659 const PgStat_KindInfo *kind_info = NULL;
1660
1662
1663 /*
1664 * We should not see any "dropped" entries when writing the stats
1665 * file, as all backends and auxiliary processes should have cleaned
1666 * up their references before they terminated.
1667 *
1668 * However, since we are already shutting down, it is not worth
1669 * crashing the server over any potential cleanup issues, so we simply
1670 * skip such entries if encountered.
1671 */
1672 Assert(!ps->dropped);
1673 if (ps->dropped)
1674 continue;
1675
1676 /*
1677 * This discards data related to custom stats kinds that are unknown
1678 * to this process.
1679 */
1680 if (!pgstat_is_kind_valid(ps->key.kind))
1681 {
1682 elog(WARNING, "found unknown stats entry %u/%u/%" PRIu64,
1683 ps->key.kind, ps->key.dboid,
1684 ps->key.objid);
1685 continue;
1686 }
1687
1689
1690 kind_info = pgstat_get_kind_info(ps->key.kind);
1691
1692 /* if not dropped the valid-entry refcount should exist */
1693 Assert(pg_atomic_read_u32(&ps->refcount) > 0);
1694
1695 /* skip if no need to write to file */
1696 if (!kind_info->write_to_file)
1697 continue;
1698
1699 if (!kind_info->to_serialized_name)
1700 {
1701 /* normal stats entry, identified by PgStat_HashKey */
1702 fputc(PGSTAT_FILE_ENTRY_HASH, fpout);
1703 write_chunk_s(fpout, &ps->key);
1704 }
1705 else
1706 {
1707 /* stats entry identified by name on disk (e.g. slots) */
1708 NameData name;
1709
1710 kind_info->to_serialized_name(&ps->key, shstats, &name);
1711
1712 fputc(PGSTAT_FILE_ENTRY_NAME, fpout);
1713 write_chunk_s(fpout, &ps->key.kind);
1714 write_chunk_s(fpout, &name);
1715 }
1716
1717 /* Write except the header part of the entry */
1718 write_chunk(fpout,
1719 pgstat_get_entry_data(ps->key.kind, shstats),
1720 pgstat_get_entry_len(ps->key.kind));
1721 }
1722 dshash_seq_term(&hstat);
1723
1724 /*
1725 * No more output to be done. Close the temp file and replace the old
1726 * pgstat.stat with it. The ferror() check replaces testing for error
1727 * after each individual fputc or fwrite (in write_chunk()) above.
1728 */
1729 fputc(PGSTAT_FILE_ENTRY_END, fpout);
1730
1731 if (ferror(fpout))
1732 {
1733 ereport(LOG,
1735 errmsg("could not write temporary statistics file \"%s\": %m",
1736 tmpfile)));
1737 FreeFile(fpout);
1738 unlink(tmpfile);
1739 }
1740 else if (FreeFile(fpout) < 0)
1741 {
1742 ereport(LOG,
1744 errmsg("could not close temporary statistics file \"%s\": %m",
1745 tmpfile)));
1746 unlink(tmpfile);
1747 }
1748 else if (durable_rename(tmpfile, statfile, LOG) < 0)
1749 {
1750 /* durable_rename already emitted log message */
1751 unlink(tmpfile);
1752 }
1753}
1754
1755/* helpers for pgstat_read_statsfile() */
1756static bool
1757read_chunk(FILE *fpin, void *ptr, size_t len)
1758{
1759 return fread(ptr, 1, len, fpin) == len;
1760}
1762#define read_chunk_s(fpin, ptr) read_chunk(fpin, ptr, sizeof(*ptr))
1763
1764/*
1765 * Reads in existing statistics file into memory.
1766 *
1767 * This function is called in the only process that is accessing the shared
1768 * stats so locking is not required.
1769 */
1770static void
1772{
1773 FILE *fpin;
1774 int32 format_id;
1775 bool found;
1776 const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
1778
1779 /* shouldn't be called from postmaster */
1781
1782 elog(DEBUG2, "reading stats file \"%s\"", statfile);
1783
1784 /*
1785 * Try to open the stats file. If it doesn't exist, the backends simply
1786 * returns zero for anything and statistics simply starts from scratch
1787 * with empty counters.
1788 *
1789 * ENOENT is a possibility if stats collection was previously disabled or
1790 * has not yet written the stats file for the first time. Any other
1791 * failure condition is suspicious.
1792 */
1793 if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
1794 {
1795 if (errno != ENOENT)
1796 ereport(LOG,
1798 errmsg("could not open statistics file \"%s\": %m",
1799 statfile)));
1801 return;
1802 }
1803
1804 /*
1805 * Verify it's of the expected format.
1806 */
1807 if (!read_chunk_s(fpin, &format_id))
1808 {
1809 elog(WARNING, "could not read format ID");
1810 goto error;
1811 }
1812
1813 if (format_id != PGSTAT_FILE_FORMAT_ID)
1814 {
1815 elog(WARNING, "found incorrect format ID %d (expected %d)",
1816 format_id, PGSTAT_FILE_FORMAT_ID);
1817 goto error;
1818 }
1819
1820 /*
1821 * We found an existing statistics file. Read it and put all the stats
1822 * data into place.
1823 */
1824 for (;;)
1825 {
1826 int t = fgetc(fpin);
1827
1828 switch (t)
1829 {
1831 {
1832 PgStat_Kind kind;
1833 const PgStat_KindInfo *info;
1834 char *ptr;
1835
1836 /* entry for fixed-numbered stats */
1837 if (!read_chunk_s(fpin, &kind))
1838 {
1839 elog(WARNING, "could not read stats kind for entry of type %c", t);
1840 goto error;
1841 }
1842
1843 if (!pgstat_is_kind_valid(kind))
1844 {
1845 elog(WARNING, "invalid stats kind %u for entry of type %c",
1846 kind, t);
1847 goto error;
1848 }
1849
1850 info = pgstat_get_kind_info(kind);
1851 if (!info)
1852 {
1853 elog(WARNING, "could not find information of kind %u for entry of type %c",
1854 kind, t);
1855 goto error;
1856 }
1857
1858 if (!info->fixed_amount)
1859 {
1860 elog(WARNING, "invalid fixed_amount in stats kind %u for entry of type %c",
1861 kind, t);
1862 goto error;
1863 }
1864
1865 /* Load back stats into shared memory */
1866 if (pgstat_is_kind_builtin(kind))
1867 ptr = ((char *) shmem) + info->shared_ctl_off +
1868 info->shared_data_off;
1869 else
1870 {
1871 int idx = kind - PGSTAT_KIND_CUSTOM_MIN;
1872
1873 ptr = ((char *) shmem->custom_data[idx]) +
1874 info->shared_data_off;
1875 }
1876
1877 if (!read_chunk(fpin, ptr, info->shared_data_len))
1878 {
1879 elog(WARNING, "could not read data of stats kind %u for entry of type %c with size %u",
1880 kind, t, info->shared_data_len);
1881 goto error;
1882 }
1883
1884 break;
1885 }
1888 {
1891 PgStatShared_Common *header;
1892
1894
1895 if (t == PGSTAT_FILE_ENTRY_HASH)
1896 {
1897 /* normal stats entry, identified by PgStat_HashKey */
1898 if (!read_chunk_s(fpin, &key))
1899 {
1900 elog(WARNING, "could not read key for entry of type %c", t);
1901 goto error;
1902 }
1903
1904 if (!pgstat_is_kind_valid(key.kind))
1905 {
1906 elog(WARNING, "invalid stats kind for entry %u/%u/%" PRIu64 " of type %c",
1907 key.kind, key.dboid,
1908 key.objid, t);
1909 goto error;
1910 }
1911 }
1912 else
1913 {
1914 /* stats entry identified by name on disk (e.g. slots) */
1915 const PgStat_KindInfo *kind_info = NULL;
1916 PgStat_Kind kind;
1917 NameData name;
1918
1919 if (!read_chunk_s(fpin, &kind))
1920 {
1921 elog(WARNING, "could not read stats kind for entry of type %c", t);
1922 goto error;
1923 }
1924 if (!read_chunk_s(fpin, &name))
1925 {
1926 elog(WARNING, "could not read name of stats kind %u for entry of type %c",
1927 kind, t);
1928 goto error;
1929 }
1930 if (!pgstat_is_kind_valid(kind))
1931 {
1932 elog(WARNING, "invalid stats kind %u for entry of type %c",
1933 kind, t);
1934 goto error;
1935 }
1936
1937 kind_info = pgstat_get_kind_info(kind);
1938 if (!kind_info)
1939 {
1940 elog(WARNING, "could not find information of kind %u for entry of type %c",
1941 kind, t);
1942 goto error;
1943 }
1944
1945 if (!kind_info->from_serialized_name)
1946 {
1947 elog(WARNING, "invalid from_serialized_name in stats kind %u for entry of type %c",
1948 kind, t);
1949 goto error;
1950 }
1951
1952 if (!kind_info->from_serialized_name(&name, &key))
1953 {
1954 /* skip over data for entry we don't care about */
1955 if (fseek(fpin, pgstat_get_entry_len(kind), SEEK_CUR) != 0)
1956 {
1957 elog(WARNING, "could not seek \"%s\" of stats kind %u for entry of type %c",
1958 NameStr(name), kind, t);
1959 goto error;
1960 }
1961
1962 continue;
1963 }
1964
1965 Assert(key.kind == kind);
1966 }
1967
1968 /*
1969 * This intentionally doesn't use pgstat_get_entry_ref() -
1970 * putting all stats into checkpointer's
1971 * pgStatEntryRefHash would be wasted effort and memory.
1972 */
1974
1975 /* don't allow duplicate entries */
1976 if (found)
1977 {
1979 elog(WARNING, "found duplicate stats entry %u/%u/%" PRIu64 " of type %c",
1980 key.kind, key.dboid,
1981 key.objid, t);
1982 goto error;
1983 }
1984
1985 header = pgstat_init_entry(key.kind, p);
1987
1988 if (!read_chunk(fpin,
1989 pgstat_get_entry_data(key.kind, header),
1991 {
1992 elog(WARNING, "could not read data for entry %u/%u/%" PRIu64 " of type %c",
1993 key.kind, key.dboid,
1994 key.objid, t);
1995 goto error;
1996 }
1997
1998 break;
1999 }
2001
2002 /*
2003 * check that PGSTAT_FILE_ENTRY_END actually signals end of
2004 * file
2005 */
2006 if (fgetc(fpin) != EOF)
2007 {
2008 elog(WARNING, "could not read end-of-file");
2009 goto error;
2010 }
2011
2012 goto done;
2013
2014 default:
2015 elog(WARNING, "could not read entry of type %c", t);
2016 goto error;
2017 }
2018 }
2019
2020done:
2021 FreeFile(fpin);
2022
2023 elog(DEBUG2, "removing permanent stats file \"%s\"", statfile);
2024 unlink(statfile);
2025
2026 return;
2027
2028error:
2029 ereport(LOG,
2030 (errmsg("corrupted statistics file \"%s\"", statfile)));
2031
2033
2034 goto done;
2035}
2036
2037/*
2038 * Helper to reset / drop stats after a crash or after restoring stats from
2039 * disk failed, potentially after already loading parts.
2040 */
2041static void
2043{
2045
2046 /* reset fixed-numbered stats */
2047 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
2048 {
2049 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
2050
2051 if (!kind_info || !kind_info->fixed_amount)
2052 continue;
2053
2054 kind_info->reset_all_cb(ts);
2055 }
2056
2057 /* and drop variable-numbered ones */
2059}
2060
2061/*
2062 * GUC assign_hook for stats_fetch_consistency.
2063 */
2064void
2065assign_stats_fetch_consistency(int newval, void *extra)
2066{
2067 /*
2068 * Changing this value in a transaction may cause snapshot state
2069 * inconsistencies, so force a clear of the current snapshot on the next
2070 * snapshot build attempt.
2071 */
2074}
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:262
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:239
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1781
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1645
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1609
void pgstat_clear_backend_activity_snapshot(void)
static int32 next
Definition: blutils.c:224
#define NameStr(name)
Definition: c.h:717
#define PG_BINARY_R
Definition: c.h:1246
int32_t int32
Definition: c.h:498
uint64_t uint64
Definition: c.h:503
#define unlikely(x)
Definition: c.h:347
uint32_t uint32
Definition: c.h:502
#define PG_BINARY_W
Definition: c.h:1247
#define OidIsValid(objectId)
Definition: c.h:746
int64 TimestampTz
Definition: timestamp.h:39
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:942
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:638
void dshash_seq_term(dshash_seq_status *status)
Definition: dshash.c:747
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:657
struct cursor * cur
Definition: ecpg.c:29
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1158
int errcode_for_file_access(void)
Definition: elog.c:877
int errdetail(const char *fmt,...)
Definition: elog.c:1204
int errhint(const char *fmt,...)
Definition: elog.c:1318
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#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 elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:149
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:782
int FreeFile(FILE *file)
Definition: fd.c:2843
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2644
ProcNumber MyProcNumber
Definition: globals.c:91
bool IsUnderPostmaster
Definition: globals.c:121
bool IsPostmasterEnvironment
Definition: globals.c:120
Oid MyDatabaseId
Definition: globals.c:95
#define newval
Assert(PointerIsAligned(start, uint64))
static void dlist_init(dlist_head *head)
Definition: ilist.h:314
static bool dlist_has_next(const dlist_head *head, const dlist_node *node)
Definition: ilist.h:503
static dlist_node * dlist_next_node(dlist_head *head, dlist_node *node)
Definition: ilist.h:537
static void dlist_delete(dlist_node *node)
Definition: ilist.h:405
static dlist_node * dlist_head_node(dlist_head *head)
Definition: ilist.h:565
static bool dlist_is_empty(const dlist_head *head)
Definition: ilist.h:336
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:364
#define DLIST_STATIC_INIT(name)
Definition: ilist.h:281
#define dlist_container(type, membername, ptr)
Definition: ilist.h:593
struct parser_state ps
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:337
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1182
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1902
@ LW_SHARED
Definition: lwlock.h:115
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1260
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1294
void pfree(void *pointer)
Definition: mcxt.c:2150
MemoryContext TopMemoryContext
Definition: mcxt.c:165
void * palloc(Size size)
Definition: mcxt.c:1943
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:485
#define AllocSetContextCreate
Definition: memutils.h:149
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:190
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
@ B_CHECKPOINTER
Definition: miscadmin.h:363
BackendType MyBackendType
Definition: miscinit.c:64
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1837
void * arg
const void size_t len
void pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:869
static bool pgStatForceNextFlush
Definition: pgstat.c:244
int pgstat_fetch_consistency
Definition: pgstat.c:204
#define PGSTAT_MIN_INTERVAL
Definition: pgstat.c:126
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1078
static void pgstat_prep_snapshot(void)
Definition: pgstat.c:1117
static const PgStat_KindInfo ** pgstat_kind_custom_infos
Definition: pgstat.c:491
static void pgstat_build_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1228
#define PGSTAT_SNAPSHOT_HASH_SIZE
Definition: pgstat.c:137
#define PGSTAT_FILE_ENTRY_END
Definition: pgstat.c:143
void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat.c:1334
void pgstat_reset_counters(void)
Definition: pgstat.c:850
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *created_entry)
Definition: pgstat.c:1283
static void pgstat_build_snapshot(void)
Definition: pgstat.c:1138
void pgstat_initialize(void)
Definition: pgstat.c:638
bool pgstat_track_counts
Definition: pgstat.c:203
static void pgstat_write_statsfile(void)
Definition: pgstat.c:1586
long pgstat_report_stat(bool force)
Definition: pgstat.c:691
static void pgstat_read_statsfile(void)
Definition: pgstat.c:1770
static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
Definition: pgstat.c:276
void pgstat_reset_of_kind(PgStat_Kind kind)
Definition: pgstat.c:891
static bool pgstat_flush_pending_entries(bool nowait)
Definition: pgstat.c:1357
void pgstat_before_server_shutdown(int code, Datum arg)
Definition: pgstat.c:559
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1453
static void pgstat_reset_after_failure(void)
Definition: pgstat.c:2041
void assign_stats_fetch_consistency(int newval, void *extra)
Definition: pgstat.c:2064
void pgstat_force_next_flush(void)
Definition: pgstat.c:829
static bool pgstat_is_kind_valid(PgStat_Kind kind)
Definition: pgstat.c:1447
void pgstat_clear_snapshot(void)
Definition: pgstat.c:917
TimestampTz pgstat_get_stat_snapshot_timestamp(bool *have_snapshot)
Definition: pgstat.c:1045
bool pgstat_have_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:1062
static MemoryContext pgStatPendingContext
Definition: pgstat.c:229
static dlist_head pgStatPending
Definition: pgstat.c:237
#define PGSTAT_FILE_ENTRY_HASH
Definition: pgstat.c:146
static void pgstat_init_snapshot_fixed(void)
Definition: pgstat.c:1098
PgStat_EntryRef * pgstat_fetch_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:1321
static void write_chunk(FILE *fpout, void *ptr, size_t len)
Definition: pgstat.c:1569
void pgstat_restore_stats(void)
Definition: pgstat.c:504
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:949
#define write_chunk_s(fpout, ptr)
Definition: pgstat.c:1579
struct PgStat_SnapshotEntry PgStat_SnapshotEntry
#define PGSTAT_MAX_INTERVAL
Definition: pgstat.c:128
#define PGSTAT_FILE_ENTRY_FIXED
Definition: pgstat.c:144
static bool read_chunk(FILE *fpin, void *ptr, size_t len)
Definition: pgstat.c:1756
#define read_chunk_s(fpin, ptr)
Definition: pgstat.c:1761
#define PGSTAT_IDLE_INTERVAL
Definition: pgstat.c:130
#define PGSTAT_FILE_ENTRY_NAME
Definition: pgstat.c:145
void pgstat_register_kind(PgStat_Kind kind, const PgStat_KindInfo *kind_info)
Definition: pgstat.c:1481
PgStat_LocalState pgStatLocal
Definition: pgstat.c:212
void pgstat_discard_stats(void)
Definition: pgstat.c:516
static bool match_db_entries(PgStatShared_HashEntry *entry, Datum match_data)
Definition: pgstat.c:838
static bool force_stats_snapshot_clear
Definition: pgstat.c:250
PgStat_Kind pgstat_get_kind_from_str(char *kind_str)
Definition: pgstat.c:1419
static void pgstat_shutdown_hook(int code, Datum arg)
Definition: pgstat.c:600
@ PGSTAT_FETCH_CONSISTENCY_NONE
Definition: pgstat.h:46
@ PGSTAT_FETCH_CONSISTENCY_CACHE
Definition: pgstat.h:47
@ PGSTAT_FETCH_CONSISTENCY_SNAPSHOT
Definition: pgstat.h:48
#define PGSTAT_STAT_PERMANENT_FILENAME
Definition: pgstat.h:30
#define PGSTAT_STAT_PERMANENT_TMPFILE
Definition: pgstat.h:31
#define PGSTAT_FILE_FORMAT_ID
Definition: pgstat.h:214
void pgstat_archiver_init_shmem_cb(void *stats)
void pgstat_archiver_reset_all_cb(TimestampTz ts)
void pgstat_archiver_snapshot_cb(void)
bool pgstat_backend_have_pending_cb(void)
void pgstat_backend_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
bool pgstat_backend_flush_cb(bool nowait)
void pgstat_bgwriter_reset_all_cb(TimestampTz ts)
void pgstat_bgwriter_init_shmem_cb(void *stats)
void pgstat_bgwriter_snapshot_cb(void)
void pgstat_checkpointer_snapshot_cb(void)
void pgstat_checkpointer_init_shmem_cb(void *stats)
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_Backend PgStatShared_Backend
struct PgStatShared_ReplSlot PgStatShared_ReplSlot
bool pgstat_io_have_pending_cb(void)
Definition: pgstat_io.c:174
bool pgstat_io_flush_cb(bool nowait)
Definition: pgstat_io.c:197
void pgstat_io_reset_all_cb(TimestampTz ts)
Definition: pgstat_io.c:295
void pgstat_io_snapshot_cb(void)
Definition: pgstat_io.c:317
void pgstat_io_init_shmem_cb(void *stats)
Definition: pgstat_io.c:286
#define PGSTAT_KIND_CUSTOM_MAX
Definition: pgstat_kind.h:50
#define PGSTAT_KIND_ARCHIVER
Definition: pgstat_kind.h:35
static bool pgstat_is_kind_builtin(PgStat_Kind kind)
Definition: pgstat_kind.h:61
#define PGSTAT_KIND_WAL
Definition: pgstat_kind.h:40
static bool pgstat_is_kind_custom(PgStat_Kind kind)
Definition: pgstat_kind.h:67
#define PgStat_Kind
Definition: pgstat_kind.h:17
#define PGSTAT_KIND_MAX
Definition: pgstat_kind.h:21
#define PGSTAT_KIND_BGWRITER
Definition: pgstat_kind.h:36
#define PGSTAT_KIND_CUSTOM_MIN
Definition: pgstat_kind.h:49
#define PGSTAT_KIND_REPLSLOT
Definition: pgstat_kind.h:30
#define PGSTAT_KIND_FUNCTION
Definition: pgstat_kind.h:29
#define PGSTAT_KIND_BUILTIN_MAX
Definition: pgstat_kind.h:43
#define PGSTAT_KIND_CUSTOM_SIZE
Definition: pgstat_kind.h:51
#define PGSTAT_KIND_DATABASE
Definition: pgstat_kind.h:27
#define PGSTAT_KIND_INVALID
Definition: pgstat_kind.h:24
#define PGSTAT_KIND_SLRU
Definition: pgstat_kind.h:39
#define PGSTAT_KIND_RELATION
Definition: pgstat_kind.h:28
#define PGSTAT_KIND_CHECKPOINTER
Definition: pgstat_kind.h:37
#define PGSTAT_KIND_MIN
Definition: pgstat_kind.h:20
#define PGSTAT_KIND_BUILTIN_SIZE
Definition: pgstat_kind.h:44
#define PGSTAT_KIND_IO
Definition: pgstat_kind.h:38
#define PGSTAT_KIND_SUBSCRIPTION
Definition: pgstat_kind.h:31
#define PGSTAT_KIND_BACKEND
Definition: pgstat_kind.h:32
#define PGSTAT_KIND_BUILTIN_MIN
Definition: pgstat_kind.h:42
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)
void pgstat_request_entry_refs_gc(void)
Definition: pgstat_shmem.c:709
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:444
bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:672
void pgstat_attach_shmem(void)
Definition: pgstat_shmem.c:244
bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat_shmem.c:962
void pgstat_reset_entry(PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts)
PgStatShared_Common * pgstat_init_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
Definition: pgstat_shmem.c:293
void pgstat_reset_matching_entries(bool(*do_reset)(PgStatShared_HashEntry *, Datum), Datum match_data, TimestampTz ts)
void pgstat_drop_all_entries(void)
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:684
void pgstat_detach_shmem(void)
Definition: pgstat_shmem.c:264
void pgstat_slru_snapshot_cb(void)
Definition: pgstat_slru.c:220
bool pgstat_slru_flush_cb(bool nowait)
Definition: pgstat_slru.c:165
void pgstat_slru_reset_all_cb(TimestampTz ts)
Definition: pgstat_slru.c:213
void pgstat_slru_init_shmem_cb(void *stats)
Definition: pgstat_slru.c:205
bool pgstat_slru_have_pending_cb(void)
Definition: pgstat_slru.c:150
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:157
void pgstat_wal_init_shmem_cb(void *stats)
Definition: pgstat_wal.c:149
void pgstat_wal_init_backend_cb(void)
Definition: pgstat_wal.c:129
bool pgstat_wal_flush_cb(bool nowait)
Definition: pgstat_wal.c:82
bool pgstat_wal_have_pending_cb(void)
Definition: pgstat_wal.c:143
void pgstat_wal_snapshot_cb(void)
Definition: pgstat_wal.c:168
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
uintptr_t Datum
Definition: postgres.h:69
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:247
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
#define InvalidOid
Definition: postgres_ext.h:35
unsigned int Oid
Definition: postgres_ext.h:30
static void error(void)
Definition: sql-dyntest.c:147
pg_atomic_uint32 refcount
PgStatShared_Common * shared_stats
PgStatShared_HashEntry * shared_entry
dlist_node pending_node
PgStat_Kind kind
bool accessed_across_databases
bool(* flush_static_cb)(bool nowait)
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(* have_static_pending_cb)(void)
bool(* flush_pending_cb)(PgStat_EntryRef *sr, bool nowait)
void(* reset_all_cb)(TimestampTz ts)
void(* init_backend_cb)(void)
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
void * custom_data[PGSTAT_KIND_CUSTOM_SIZE]
PgStat_HashKey key
Definition: pgstat.c:151
void * custom_data[PGSTAT_KIND_CUSTOM_SIZE]
TimestampTz snapshot_timestamp
MemoryContext context
bool custom_valid[PGSTAT_KIND_CUSTOM_SIZE]
PgStat_FetchConsistency mode
struct pgstat_snapshot_hash * stats
bool fixed_valid[PGSTAT_KIND_BUILTIN_SIZE]
Definition: c.h:712
const char * name
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4989
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition: xact.c:891