Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

PostgreSQL Source Code git master
pgstat_internal.h
Go to the documentation of this file.
1/* ----------
2 * pgstat_internal.h
3 *
4 * Definitions for the PostgreSQL cumulative statistics system that should
5 * only be needed by files implementing statistics support (rather than ones
6 * reporting / querying stats).
7 *
8 * Copyright (c) 2001-2025, PostgreSQL Global Development Group
9 *
10 * src/include/utils/pgstat_internal.h
11 * ----------
12 */
13#ifndef PGSTAT_INTERNAL_H
14#define PGSTAT_INTERNAL_H
15
16
18#include "lib/dshash.h"
19#include "lib/ilist.h"
20#include "pgstat.h"
21#include "storage/lwlock.h"
22#include "utils/dsa.h"
23
24
25/*
26 * Types related to shared memory storage of statistics.
27 *
28 * Per-object statistics are stored in the "shared stats" hashtable. That
29 * table's entries (PgStatShared_HashEntry) contain a pointer to the actual stats
30 * data for the object (the size of the stats data varies depending on the
31 * kind of stats). The table is keyed by PgStat_HashKey.
32 *
33 * Once a backend has a reference to a shared stats entry, it increments the
34 * entry's refcount. Even after stats data is dropped (e.g., due to a DROP
35 * TABLE), the entry itself can only be deleted once all references have been
36 * released.
37 *
38 * These refcounts, in combination with a backend local hashtable
39 * (pgStatEntryRefHash, with entries pointing to PgStat_EntryRef) in front of
40 * the shared hash table, mean that most stats work can happen without
41 * touching the shared hash table, reducing contention.
42 *
43 * Once there are pending stats updates for a table PgStat_EntryRef->pending
44 * is allocated to contain a working space for as-of-yet-unapplied stats
45 * updates. Once the stats are flushed, PgStat_EntryRef->pending is freed.
46 *
47 * Each stat kind in the shared hash table has a fixed member
48 * PgStatShared_Common as the first element.
49 */
50
51/*
52 * Struct for shared statistics hash entry key.
53 *
54 * NB: We assume that this struct contains no padding. Also, 8 bytes
55 * allocated for the object ID are good enough to ensure the uniqueness
56 * of the hash key, hence the addition of new fields is not recommended.
57 */
58typedef struct PgStat_HashKey
59{
60 PgStat_Kind kind; /* statistics entry kind */
61 Oid dboid; /* database ID. InvalidOid for shared objects. */
62 uint64 objid; /* object ID (table, function, etc.), or
63 * identifier. */
65
66/*
67 * PgStat_HashKey should not have any padding. Checking that the structure
68 * size matches with the sum of each field is a check simple enough to
69 * enforce this policy.
70 */
71StaticAssertDecl((sizeof(PgStat_Kind) + sizeof(uint64) + sizeof(Oid)) ==
72 sizeof(PgStat_HashKey),
73 "PgStat_HashKey should have no padding");
74
75/*
76 * Shared statistics hash entry. Doesn't itself contain any stats, but points
77 * to them (with ->body). That allows the stats entries themselves to be of
78 * variable size.
79 */
81{
82 PgStat_HashKey key; /* hash key */
83
84 /*
85 * If dropped is set, backends need to release their references so that
86 * the memory for the entry can be freed. No new references may be made
87 * once marked as dropped.
88 */
89 bool dropped;
90
91 /*
92 * Refcount managing lifetime of the entry itself (as opposed to the
93 * dshash entry pointing to it). The stats lifetime has to be separate
94 * from the hash table entry lifetime because we allow backends to point
95 * to a stats entry without holding a hash table lock (and some other
96 * reasons).
97 *
98 * As long as the entry is not dropped, 1 is added to the refcount
99 * representing that the entry should not be dropped. In addition each
100 * backend that has a reference to the entry needs to increment the
101 * refcount as long as it does.
102 *
103 * May only be incremented / decremented while holding at least a shared
104 * lock on the dshash partition containing the entry. It needs to be an
105 * atomic variable because multiple backends can increment the refcount
106 * with just a shared lock.
107 *
108 * When the refcount reaches 0 the entry needs to be freed.
109 */
111
112 /*
113 * Counter tracking the number of times the entry has been reused.
114 *
115 * Set to 0 when the entry is created, and incremented by one each time
116 * the shared entry is reinitialized with pgstat_reinit_entry().
117 *
118 * May only be incremented / decremented while holding at least a shared
119 * lock on the dshash partition containing the entry. Like refcount, it
120 * needs to be an atomic variable because multiple backends can increment
121 * the generation with just a shared lock.
122 */
124
125 /*
126 * Pointer to shared stats. The stats entry always starts with
127 * PgStatShared_Common, embedded in a larger struct containing the
128 * PgStat_Kind specific stats fields.
129 */
132
133/*
134 * Common header struct for PgStatShared_*.
135 */
137{
138 uint32 magic; /* just a validity cross-check */
139 /* lock protecting stats contents (i.e. data following the header) */
142
143/*
144 * A backend local reference to a shared stats entry. As long as at least one
145 * such reference exists, the shared stats entry will not be released.
146 *
147 * If there are pending stats update to the shared stats, these are stored in
148 * ->pending.
149 */
150typedef struct PgStat_EntryRef
151{
152 /*
153 * Pointer to the PgStatShared_HashEntry entry in the shared stats
154 * hashtable.
155 */
157
158 /*
159 * Pointer to the stats data (i.e. PgStatShared_HashEntry->body), resolved
160 * as a local pointer, to avoid repeated dsa_get_address() calls.
161 */
163
164 /*
165 * Copy of PgStatShared_HashEntry->generation, keeping locally track of
166 * the shared stats entry "generation" retrieved (number of times reused).
167 */
169
170 /*
171 * Pending statistics data that will need to be flushed to shared memory
172 * stats eventually. Each stats kind utilizing pending data defines what
173 * format its pending data has and needs to provide a
174 * PgStat_KindInfo->flush_pending_cb callback to merge pending entries
175 * into the shared stats hash table.
176 */
177 void *pending;
178 dlist_node pending_node; /* membership in pgStatPending list */
180
181
182/*
183 * Some stats changes are transactional. To maintain those, a stack of
184 * PgStat_SubXactStatus entries is maintained, which contain data pertaining
185 * to the current transaction and its active subtransactions.
186 */
188{
189 int nest_level; /* subtransaction nest level */
190
191 struct PgStat_SubXactStatus *prev; /* higher-level subxact if any */
192
193 /*
194 * Statistics for transactionally dropped objects need to be
195 * transactionally dropped as well. Collect the stats dropped in the
196 * current (sub-)transaction and only execute the stats drop when we know
197 * if the transaction commits/aborts. To handle replicas and crashes,
198 * stats drops are included in commit / abort records.
199 */
201
202 /*
203 * Tuple insertion/deletion counts for an open transaction can't be
204 * propagated into PgStat_TableStatus counters until we know if it is
205 * going to commit or abort. Hence, we keep these counts in per-subxact
206 * structs that live in TopTransactionContext. This data structure is
207 * designed on the assumption that subxacts won't usually modify very many
208 * tables.
209 */
210 PgStat_TableXactStatus *first; /* head of list for this subxact */
212
213
214/*
215 * Metadata for a specific kind of statistics.
216 */
217typedef struct PgStat_KindInfo
218{
219 /*
220 * Do a fixed number of stats objects exist for this kind of stats (e.g.
221 * bgwriter stats) or not (e.g. tables).
222 */
224
225 /*
226 * Can stats of this kind be accessed from another database? Determines
227 * whether a stats object gets included in stats snapshots.
228 */
230
231 /* Should stats be written to the on-disk stats file? */
233
234 /*
235 * The size of an entry in the shared stats hash table (pointed to by
236 * PgStatShared_HashEntry->body). For fixed-numbered statistics, this is
237 * the size of an entry in PgStat_ShmemControl->custom_data.
238 */
240
241 /*
242 * The offset of the statistics struct in the cached statistics snapshot
243 * PgStat_Snapshot, for fixed-numbered statistics.
244 */
246
247 /*
248 * The offset of the statistics struct in the containing shared memory
249 * control structure PgStat_ShmemControl, for fixed-numbered statistics.
250 */
252
253 /*
254 * The offset/size of statistics inside the shared stats entry. Used when
255 * [de-]serializing statistics to / from disk respectively. Separate from
256 * shared_size because [de-]serialization may not include in-memory state
257 * like lwlocks.
258 */
261
262 /*
263 * The size of the pending data for this kind. E.g. how large
264 * PgStat_EntryRef->pending is. Used for allocations.
265 *
266 * 0 signals that an entry of this kind should never have a pending entry.
267 */
269
270 /*
271 * Perform custom actions when initializing a backend (standalone or under
272 * postmaster). Optional.
273 */
274 void (*init_backend_cb) (void);
275
276 /*
277 * For variable-numbered stats: flush pending stats. Required if pending
278 * data is used. See flush_static_cb when dealing with stats data that
279 * that cannot use PgStat_EntryRef->pending.
280 */
281 bool (*flush_pending_cb) (PgStat_EntryRef *sr, bool nowait);
282
283 /*
284 * For variable-numbered stats: delete pending stats. Optional.
285 */
287
288 /*
289 * For variable-numbered stats: reset the reset timestamp. Optional.
290 */
292
293 /*
294 * For variable-numbered stats. Optional.
295 */
297 const PgStatShared_Common *header, NameData *name);
299
300 /*
301 * For fixed-numbered statistics: Initialize shared memory state.
302 *
303 * "stats" is the pointer to the allocated shared memory area.
304 */
305 void (*init_shmem_cb) (void *stats);
306
307 /*
308 * For fixed-numbered or variable-numbered statistics: Flush pending stats
309 * entries, for stats kinds that do not use PgStat_EntryRef->pending.
310 *
311 * Returns true if some of the stats could not be flushed, due to lock
312 * contention for example. Optional.
313 *
314 * "pgstat_report_fixed" needs to be set to trigger the flush of pending
315 * stats.
316 */
317 bool (*flush_static_cb) (bool nowait);
318
319 /*
320 * For fixed-numbered statistics: Reset All.
321 */
323
324 /*
325 * For fixed-numbered statistics: Build snapshot for entry
326 */
327 void (*snapshot_cb) (void);
328
329 /* name of the kind of stats */
330 const char *const name;
332
333
334/*
335 * List of SLRU names that we keep stats for. There is no central registry of
336 * SLRUs, so we use this fixed list instead. The "other" entry is used for
337 * all SLRUs without an explicit entry (e.g. SLRUs in extensions).
338 *
339 * This is only defined here so that SLRU_NUM_ELEMENTS is known for later type
340 * definitions.
341 */
342static const char *const slru_names[] = {
343 "commit_timestamp",
344 "multixact_member",
345 "multixact_offset",
346 "notify",
347 "serializable",
348 "subtransaction",
349 "transaction",
350 "other" /* has to be last */
351};
352
353#define SLRU_NUM_ELEMENTS lengthof(slru_names)
354
355
356/* ----------
357 * Types and definitions for different kinds of fixed-amount stats.
358 *
359 * Single-writer stats use the changecount mechanism to achieve low-overhead
360 * writes - they're obviously more performance critical than reads. Check the
361 * definition of struct PgBackendStatus for some explanation of the
362 * changecount mechanism.
363 *
364 * Because the obvious implementation of resetting single-writer stats isn't
365 * compatible with that (another backend needs to write), we don't scribble on
366 * shared stats while resetting. Instead, just record the current counter
367 * values in a copy of the stats data, which is protected by ->lock. See
368 * pgstat_fetch_stat_(archiver|bgwriter|checkpointer) for the reader side.
369 *
370 * The only exception to that is the stat_reset_timestamp in these structs,
371 * which is protected by ->lock, because it has to be written by another
372 * backend while resetting.
373 * ----------
374 */
375
377{
378 /* lock protects ->reset_offset as well as stats->stat_reset_timestamp */
384
386{
387 /* lock protects ->reset_offset as well as stats->stat_reset_timestamp */
393
395{
396 /* lock protects ->reset_offset as well as stats->stat_reset_timestamp */
402
403/* Shared-memory ready PgStat_IO */
404typedef struct PgStatShared_IO
405{
406 /*
407 * locks[i] protects stats.stats[i]. locks[0] also protects
408 * stats.stat_reset_timestamp.
409 */
413
414typedef struct PgStatShared_SLRU
415{
416 /* lock protects ->stats */
420
421typedef struct PgStatShared_Wal
422{
423 /* lock protects ->stats */
427
428
429
430/* ----------
431 * Types and definitions for different kinds of variable-amount stats.
432 *
433 * Each struct has to start with PgStatShared_Common, containing information
434 * common across the different types of stats. Kind-specific data follows.
435 * ----------
436 */
437
439{
443
445{
449
451{
455
457{
461
463{
467
469{
473
474/*
475 * Central shared memory entry for the cumulative stats system.
476 *
477 * Fixed amount stats, the dynamic shared memory hash table for
478 * non-fixed-amount stats, as well as remaining bits and pieces are all
479 * reached from here.
480 */
482{
484
485 /*
486 * Stats for variable-numbered objects are kept in this shared hash table.
487 * See comment above PgStat_Kind for details.
488 */
489 dshash_table_handle hash_handle; /* shared dbstat hash */
490
491 /* Has the stats system already been shut down? Just a debugging check. */
493
494 /*
495 * Whenever statistics for dropped objects could not be freed - because
496 * backends still have references - the dropping backend calls
497 * pgstat_request_entry_refs_gc() incrementing this counter. Eventually
498 * that causes backends to run pgstat_gc_entry_refs(), allowing memory to
499 * be reclaimed.
500 */
502
503 /*
504 * Stats data for fixed-numbered objects.
505 */
512
513 /*
514 * Custom stats data with fixed-numbered objects, indexed by (PgStat_Kind
515 * - PGSTAT_KIND_CUSTOM_MIN).
516 */
518
520
521
522/*
523 * Cached statistics snapshot
524 */
525typedef struct PgStat_Snapshot
526{
528
529 /* time at which snapshot was taken */
531
533
535
537
539
541
543
545
546 /*
547 * Data in snapshot for custom fixed-numbered statistics, indexed by
548 * (PgStat_Kind - PGSTAT_KIND_CUSTOM_MIN). Each entry is allocated in
549 * TopMemoryContext, for a size of PgStat_KindInfo->shared_data_len.
550 */
553
554 /* to free snapshot in bulk */
556 struct pgstat_snapshot_hash *stats;
558
559
560/*
561 * Collection of backend-local stats state.
562 */
563typedef struct PgStat_LocalState
564{
568
569 /* the current statistics snapshot */
572
573
574/*
575 * Inline functions defined further below.
576 */
577
578static inline void pgstat_begin_changecount_write(uint32 *cc);
579static inline void pgstat_end_changecount_write(uint32 *cc);
581static inline bool pgstat_end_changecount_read(uint32 *cc, uint32 cc_before);
582
583static inline void pgstat_copy_changecounted_stats(void *dst, void *src, size_t len,
584 uint32 *cc);
585
586static inline int pgstat_cmp_hash_key(const void *a, const void *b, size_t size, void *arg);
587static inline uint32 pgstat_hash_hash_key(const void *d, size_t size, void *arg);
588static inline size_t pgstat_get_entry_len(PgStat_Kind kind);
589static inline void *pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry);
590static inline void *pgstat_get_custom_shmem_data(PgStat_Kind kind);
591static inline void *pgstat_get_custom_snapshot_data(PgStat_Kind kind);
592
593
594/*
595 * Functions in pgstat.c
596 */
597
599extern void pgstat_register_kind(PgStat_Kind kind,
600 const PgStat_KindInfo *kind_info);
601
602#ifdef USE_ASSERT_CHECKING
603extern void pgstat_assert_is_up(void);
604#else
605#define pgstat_assert_is_up() ((void)true)
606#endif
607
608extern void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref);
610 uint64 objid,
611 bool *created_entry);
613 Oid dboid, uint64 objid);
614
615extern void *pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid);
616extern void pgstat_snapshot_fixed(PgStat_Kind kind);
617
618
619/*
620 * Functions in pgstat_archiver.c
621 */
622
623extern void pgstat_archiver_init_shmem_cb(void *stats);
625extern void pgstat_archiver_snapshot_cb(void);
626
627/*
628 * Functions in pgstat_backend.c
629 */
630
631/* flags for pgstat_flush_backend() */
632#define PGSTAT_BACKEND_FLUSH_IO (1 << 0) /* Flush I/O statistics */
633#define PGSTAT_BACKEND_FLUSH_WAL (1 << 1) /* Flush WAL statistics */
634#define PGSTAT_BACKEND_FLUSH_ALL (PGSTAT_BACKEND_FLUSH_IO | PGSTAT_BACKEND_FLUSH_WAL)
635
636extern bool pgstat_flush_backend(bool nowait, bits32 flags);
637extern bool pgstat_backend_flush_cb(bool nowait);
639 TimestampTz ts);
640
641/*
642 * Functions in pgstat_bgwriter.c
643 */
644
645extern void pgstat_bgwriter_init_shmem_cb(void *stats);
647extern void pgstat_bgwriter_snapshot_cb(void);
648
649
650/*
651 * Functions in pgstat_checkpointer.c
652 */
653
654extern void pgstat_checkpointer_init_shmem_cb(void *stats);
656extern void pgstat_checkpointer_snapshot_cb(void);
657
658
659/*
660 * Functions in pgstat_database.c
661 */
662
663extern void pgstat_report_disconnect(Oid dboid);
664extern void pgstat_update_dbstats(TimestampTz ts);
665extern void AtEOXact_PgStat_Database(bool isCommit, bool parallel);
666
668extern void pgstat_reset_database_timestamp(Oid dboid, TimestampTz ts);
669extern bool pgstat_database_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
671
672
673/*
674 * Functions in pgstat_function.c
675 */
676
677extern bool pgstat_function_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
678
679
680/*
681 * Functions in pgstat_io.c
682 */
683
684extern void pgstat_flush_io(bool nowait);
685
686extern bool pgstat_io_flush_cb(bool nowait);
687extern void pgstat_io_init_shmem_cb(void *stats);
688extern void pgstat_io_reset_all_cb(TimestampTz ts);
689extern void pgstat_io_snapshot_cb(void);
690
691
692/*
693 * Functions in pgstat_relation.c
694 */
695
696extern void AtEOXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit);
697extern void AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, int nestDepth);
698extern void AtPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state);
700
701extern bool pgstat_relation_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
703
704
705/*
706 * Functions in pgstat_replslot.c
707 */
708
712
713
714/*
715 * Functions in pgstat_shmem.c
716 */
717
718extern void pgstat_attach_shmem(void);
719extern void pgstat_detach_shmem(void);
720
722 bool create, bool *created_entry);
723extern bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait);
724extern bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait);
725extern void pgstat_unlock_entry(PgStat_EntryRef *entry_ref);
726extern bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid);
727extern void pgstat_drop_all_entries(void);
728extern void pgstat_drop_matching_entries(bool (*do_drop) (PgStatShared_HashEntry *, Datum),
729 Datum match_data);
731 bool nowait);
732extern void pgstat_reset_entry(PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts);
734extern void pgstat_reset_matching_entries(bool (*do_reset) (PgStatShared_HashEntry *, Datum),
735 Datum match_data,
736 TimestampTz ts);
737
738extern void pgstat_request_entry_refs_gc(void);
740 PgStatShared_HashEntry *shhashent);
741
742
743/*
744 * Functions in pgstat_slru.c
745 */
746
747extern bool pgstat_slru_flush_cb(bool nowait);
748extern void pgstat_slru_init_shmem_cb(void *stats);
750extern void pgstat_slru_snapshot_cb(void);
751
752
753/*
754 * Functions in pgstat_wal.c
755 */
756
757extern void pgstat_wal_init_backend_cb(void);
758extern bool pgstat_wal_flush_cb(bool nowait);
759extern void pgstat_wal_init_shmem_cb(void *stats);
761extern void pgstat_wal_snapshot_cb(void);
762
763
764/*
765 * Functions in pgstat_subscription.c
766 */
767
768extern bool pgstat_subscription_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
770
771
772/*
773 * Functions in pgstat_xact.c
774 */
775
777extern void pgstat_drop_transactional(PgStat_Kind kind, Oid dboid, uint64 objid);
778extern void pgstat_create_transactional(PgStat_Kind kind, Oid dboid, uint64 objid);
779
780
781/*
782 * Variables in pgstat.c
783 */
784
785/*
786 * Track if *any* pending fixed-numbered statistics should be flushed to
787 * shared memory.
788 *
789 * This flag can be switched to true by fixed-numbered statistics to let
790 * pgstat_report_stat() know if it needs to go through one round of
791 * reports, calling flush_static_cb for each fixed-numbered statistics
792 * kind. When this flag is not set, pgstat_report_stat() is able to do
793 * a fast exit, knowing that there are no pending fixed-numbered statistics.
794 *
795 * Statistics callbacks should never reset this flag; pgstat_report_stat()
796 * is in charge of doing that.
797 */
799
800/* Backend-local stats state */
802
803/*
804 * Implementation of inline functions declared above.
805 */
806
807/*
808 * Helpers for changecount manipulation. See comments around struct
809 * PgBackendStatus for details.
810 */
811
812static inline void
814{
815 Assert((*cc & 1) == 0);
816
818 (*cc)++;
820}
821
822static inline void
824{
825 Assert((*cc & 1) == 1);
826
828
829 (*cc)++;
830
832}
833
834static inline uint32
836{
837 uint32 before_cc = *cc;
838
840
842
843 return before_cc;
844}
845
846/*
847 * Returns true if the read succeeded, false if it needs to be repeated.
848 */
849static inline bool
851{
852 uint32 after_cc;
853
855
856 after_cc = *cc;
857
858 /* was a write in progress when we started? */
859 if (before_cc & 1)
860 return false;
861
862 /* did writes start and complete while we read? */
863 return before_cc == after_cc;
864}
865
866
867/*
868 * helper function for PgStat_KindInfo->snapshot_cb
869 * PgStat_KindInfo->reset_all_cb callbacks.
870 *
871 * Copies out the specified memory area following change-count protocol.
872 */
873static inline void
874pgstat_copy_changecounted_stats(void *dst, void *src, size_t len,
875 uint32 *cc)
876{
877 uint32 cc_before;
878
879 do
880 {
881 cc_before = pgstat_begin_changecount_read(cc);
882
883 memcpy(dst, src, len);
884 }
885 while (!pgstat_end_changecount_read(cc, cc_before));
886}
887
888/* helpers for dshash / simplehash hashtables */
889static inline int
890pgstat_cmp_hash_key(const void *a, const void *b, size_t size, void *arg)
891{
892 Assert(size == sizeof(PgStat_HashKey) && arg == NULL);
893 return memcmp(a, b, sizeof(PgStat_HashKey));
894}
895
896static inline uint32
897pgstat_hash_hash_key(const void *d, size_t size, void *arg)
898{
899 const char *key = (const char *) d;
900
901 Assert(size == sizeof(PgStat_HashKey) && arg == NULL);
902 return fasthash32(key, size, 0);
903}
904
905/*
906 * The length of the data portion of a shared memory stats entry (i.e. without
907 * transient data such as refcounts, lwlocks, ...).
908 */
909static inline size_t
911{
913}
914
915/*
916 * Returns a pointer to the data portion of a shared memory stats entry.
917 */
918static inline void *
920{
921 size_t off = pgstat_get_kind_info(kind)->shared_data_off;
922
923 Assert(off != 0 && off < PG_UINT32_MAX);
924
925 return ((char *) (entry)) + off;
926}
927
928/*
929 * Returns a pointer to the shared memory area of custom stats for
930 * fixed-numbered statistics.
931 */
932static inline void *
934{
935 int idx = kind - PGSTAT_KIND_CUSTOM_MIN;
936
938 Assert(pgstat_get_kind_info(kind)->fixed_amount);
939
941}
942
943/*
944 * Returns a pointer to the portion of custom data for fixed-numbered
945 * statistics in the current snapshot.
946 */
947static inline void *
949{
950 int idx = kind - PGSTAT_KIND_CUSTOM_MIN;
951
953 Assert(pgstat_get_kind_info(kind)->fixed_amount);
954
956}
957
958#endif /* PGSTAT_INTERNAL_H */
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:262
#define pg_read_barrier()
Definition: atomics.h:154
#define pg_write_barrier()
Definition: atomics.h:155
#define PGDLLIMPORT
Definition: c.h:1320
#define PG_UINT32_MAX
Definition: c.h:596
uint32 bits32
Definition: c.h:548
uint64_t uint64
Definition: c.h:540
uint32_t uint32
Definition: c.h:539
int64 TimestampTz
Definition: timestamp.h:39
uint64 dsa_pointer
Definition: dsa.h:62
dsa_pointer dshash_table_handle
Definition: dshash.h:24
Assert(PointerIsAligned(start, uint64))
static uint32 fasthash32(const char *k, size_t len, uint64 seed)
int b
Definition: isn.c:74
int a
Definition: isn.c:73
#define BACKEND_NUM_TYPES
Definition: miscadmin.h:376
#define START_CRIT_SECTION()
Definition: miscadmin.h:149
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define END_CRIT_SECTION()
Definition: miscadmin.h:151
void * arg
const void size_t len
PgStat_FetchConsistency
Definition: pgstat.h:46
bool pgstat_replslot_from_serialized_name_cb(const NameData *name, PgStat_HashKey *key)
void pgstat_slru_snapshot_cb(void)
Definition: pgstat_slru.c:197
void pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts)
struct PgStatShared_Wal PgStatShared_Wal
void pgstat_archiver_init_shmem_cb(void *stats)
void pgstat_request_entry_refs_gc(void)
Definition: pgstat_shmem.c:732
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1058
struct PgStat_EntryRef PgStat_EntryRef
static void * pgstat_get_custom_snapshot_data(PgStat_Kind kind)
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:456
void pgstat_flush_io(bool nowait)
Definition: pgstat_io.c:175
static void * pgstat_get_custom_shmem_data(PgStat_Kind kind)
static uint32 pgstat_hash_hash_key(const void *d, size_t size, void *arg)
struct PgStatShared_SLRU PgStatShared_SLRU
void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat.c:1314
bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:695
void pgstat_attach_shmem(void)
Definition: pgstat_shmem.c:243
static void pgstat_end_changecount_write(uint32 *cc)
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *created_entry)
Definition: pgstat.c:1263
static const char *const slru_names[]
static int pgstat_cmp_hash_key(const void *a, const void *b, size_t size, void *arg)
struct PgStat_KindInfo PgStat_KindInfo
void pgstat_database_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
struct PgStatShared_Function PgStatShared_Function
void pgstat_checkpointer_snapshot_cb(void)
bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat_shmem.c:986
static void * pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry)
void pgstat_reset_entry(PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts)
bool pgstat_slru_flush_cb(bool nowait)
Definition: pgstat_slru.c:142
void pgstat_wal_reset_all_cb(TimestampTz ts)
Definition: pgstat_wal.c:157
struct PgStatShared_Subscription PgStatShared_Subscription
bool pgstat_io_flush_cb(bool nowait)
Definition: pgstat_io.c:189
void AtPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
struct PgStatShared_IO PgStatShared_IO
void AtEOXact_PgStat_Database(bool isCommit, bool parallel)
PgStat_StatDBEntry * pgstat_prep_database_pending(Oid dboid)
#define pgstat_assert_is_up()
void pgstat_drop_matching_entries(bool(*do_drop)(PgStatShared_HashEntry *, Datum), Datum match_data)
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1433
PGDLLIMPORT PgStat_LocalState pgStatLocal
Definition: pgstat.c:212
PGDLLIMPORT bool pgstat_report_fixed
Definition: pgstat.c:218
void pgstat_archiver_reset_all_cb(TimestampTz ts)
void pgstat_checkpointer_init_shmem_cb(void *stats)
void pgstat_bgwriter_reset_all_cb(TimestampTz ts)
struct PgStat_HashKey PgStat_HashKey
struct PgStatShared_Archiver PgStatShared_Archiver
void pgstat_drop_transactional(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat_xact.c:384
void pgstat_replslot_to_serialized_name_cb(const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name)
bool pgstat_subscription_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
struct PgStatShared_HashEntry PgStatShared_HashEntry
struct PgStat_LocalState PgStat_LocalState
void pgstat_create_transactional(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat_xact.c:361
bool pgstat_relation_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
void pgstat_wal_init_shmem_cb(void *stats)
Definition: pgstat_wal.c:149
void pgstat_io_reset_all_cb(TimestampTz ts)
Definition: pgstat_io.c:287
static uint32 pgstat_begin_changecount_read(uint32 *cc)
void pgstat_update_dbstats(TimestampTz ts)
PgStat_SubXactStatus * pgstat_get_xact_stack_level(int nest_level)
Definition: pgstat_xact.c:238
void pgstat_replslot_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_bgwriter_init_shmem_cb(void *stats)
PgStatShared_Common * pgstat_init_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
Definition: pgstat_shmem.c:300
void pgstat_reset_matching_entries(bool(*do_reset)(PgStatShared_HashEntry *, Datum), Datum match_data, TimestampTz ts)
PgStat_EntryRef * pgstat_fetch_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:1301
void pgstat_slru_reset_all_cb(TimestampTz ts)
Definition: pgstat_slru.c:190
void pgstat_backend_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:932
struct PgStat_SubXactStatus PgStat_SubXactStatus
void AtEOXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit)
void pgstat_wal_init_backend_cb(void)
Definition: pgstat_wal.c:138
struct PgStatShared_Relation PgStatShared_Relation
StaticAssertDecl((sizeof(PgStat_Kind)+sizeof(uint64)+sizeof(Oid))==sizeof(PgStat_HashKey), "PgStat_HashKey should have no padding")
bool pgstat_wal_flush_cb(bool nowait)
Definition: pgstat_wal.c:91
void pgstat_drop_all_entries(void)
static bool pgstat_end_changecount_read(uint32 *cc, uint32 cc_before)
void pgstat_bgwriter_snapshot_cb(void)
struct PgStatShared_Checkpointer PgStatShared_Checkpointer
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:707
void pgstat_checkpointer_reset_all_cb(TimestampTz ts)
void pgstat_reset_database_timestamp(Oid dboid, TimestampTz ts)
static void pgstat_begin_changecount_write(uint32 *cc)
bool pgstat_function_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
void pgstat_report_disconnect(Oid dboid)
void pgstat_slru_init_shmem_cb(void *stats)
Definition: pgstat_slru.c:182
void PostPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
bool pgstat_flush_backend(bool nowait, bits32 flags)
bool pgstat_database_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
struct PgStat_ShmemControl PgStat_ShmemControl
#define SLRU_NUM_ELEMENTS
struct PgStatShared_Database PgStatShared_Database
void pgstat_wal_snapshot_cb(void)
Definition: pgstat_wal.c:168
static void pgstat_copy_changecounted_stats(void *dst, void *src, size_t len, uint32 *cc)
struct PgStatShared_BgWriter PgStatShared_BgWriter
void AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, int nestDepth)
void pgstat_register_kind(PgStat_Kind kind, const PgStat_KindInfo *kind_info)
Definition: pgstat.c:1461
void pgstat_io_snapshot_cb(void)
Definition: pgstat_io.c:309
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:677
bool pgstat_backend_flush_cb(bool nowait)
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid, bool nowait)
Definition: pgstat_shmem.c:716
static size_t pgstat_get_entry_len(PgStat_Kind kind)
void pgstat_io_init_shmem_cb(void *stats)
Definition: pgstat_io.c:278
struct PgStatShared_Common PgStatShared_Common
struct PgStatShared_Backend PgStatShared_Backend
void pgstat_subscription_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
struct PgStatShared_ReplSlot PgStatShared_ReplSlot
void pgstat_archiver_snapshot_cb(void)
void pgstat_detach_shmem(void)
Definition: pgstat_shmem.c:264
struct PgStat_Snapshot PgStat_Snapshot
void pgstat_relation_delete_pending_cb(PgStat_EntryRef *entry_ref)
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_CUSTOM_MIN
Definition: pgstat_kind.h:49
#define PGSTAT_KIND_CUSTOM_SIZE
Definition: pgstat_kind.h:51
#define PGSTAT_KIND_BUILTIN_SIZE
Definition: pgstat_kind.h:44
uint64_t Datum
Definition: postgres.h:70
unsigned int Oid
Definition: postgres_ext.h:32
Definition: lwlock.h:42
PgStat_ArchiverStats reset_offset
PgStat_ArchiverStats stats
PgStat_Backend stats
PgStatShared_Common header
PgStat_BgWriterStats reset_offset
PgStat_BgWriterStats stats
PgStat_CheckpointerStats reset_offset
PgStat_CheckpointerStats stats
PgStat_StatDBEntry stats
PgStatShared_Common header
PgStatShared_Common header
PgStat_StatFuncEntry stats
pg_atomic_uint32 refcount
pg_atomic_uint32 generation
LWLock locks[BACKEND_NUM_TYPES]
PgStatShared_Common header
PgStat_StatTabEntry stats
PgStatShared_Common header
PgStat_StatReplSlotEntry stats
PgStat_SLRUStats stats[SLRU_NUM_ELEMENTS]
PgStat_StatSubEntry stats
PgStatShared_Common header
PgStat_WalStats stats
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(* flush_pending_cb)(PgStat_EntryRef *sr, bool nowait)
void(* reset_all_cb)(TimestampTz ts)
void(* init_backend_cb)(void)
void(* reset_timestamp_cb)(PgStatShared_Common *header, TimestampTz ts)
const char *const name
void(* init_shmem_cb)(void *stats)
void(* delete_pending_cb)(PgStat_EntryRef *sr)
void(* snapshot_cb)(void)
PgStat_Snapshot snapshot
dshash_table * shared_hash
PgStat_ShmemControl * shmem
dshash_table_handle hash_handle
PgStatShared_SLRU slru
PgStatShared_IO io
void * custom_data[PGSTAT_KIND_CUSTOM_SIZE]
pg_atomic_uint64 gc_request_count
PgStatShared_Wal wal
PgStatShared_Archiver archiver
PgStatShared_Checkpointer checkpointer
PgStatShared_BgWriter bgwriter
PgStat_CheckpointerStats checkpointer
void * custom_data[PGSTAT_KIND_CUSTOM_SIZE]
TimestampTz snapshot_timestamp
MemoryContext context
PgStat_WalStats wal
bool custom_valid[PGSTAT_KIND_CUSTOM_SIZE]
PgStat_ArchiverStats archiver
PgStat_FetchConsistency mode
PgStat_SLRUStats slru[SLRU_NUM_ELEMENTS]
struct pgstat_snapshot_hash * stats
bool fixed_valid[PGSTAT_KIND_BUILTIN_SIZE]
PgStat_BgWriterStats bgwriter
PgStat_TableXactStatus * first
struct PgStat_SubXactStatus * prev
Definition: dsa.c:348
Definition: c.h:747
const char * name