PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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-2026, 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 * Tracks if the stats file is being read, written or discarded, used in
68 * combination with the finish callback.
69 *
70 * These states allow plugins that create auxiliary data files to determine
71 * the current operation and perform any necessary file cleanup.
72 */
79
80/*
81 * PgStat_HashKey should not have any padding. Checking that the structure
82 * size matches with the sum of each field is a check simple enough to
83 * enforce this policy.
84 */
85StaticAssertDecl((sizeof(PgStat_Kind) + sizeof(uint64) + sizeof(Oid)) ==
86 sizeof(PgStat_HashKey),
87 "PgStat_HashKey should have no padding");
88
89/*
90 * Shared statistics hash entry. Doesn't itself contain any stats, but points
91 * to them (with ->body). That allows the stats entries themselves to be of
92 * variable size.
93 */
95{
96 PgStat_HashKey key; /* hash key */
97
98 /*
99 * If dropped is set, backends need to release their references so that
100 * the memory for the entry can be freed. No new references may be made
101 * once marked as dropped.
102 */
104
105 /*
106 * Refcount managing lifetime of the entry itself (as opposed to the
107 * dshash entry pointing to it). The stats lifetime has to be separate
108 * from the hash table entry lifetime because we allow backends to point
109 * to a stats entry without holding a hash table lock (and some other
110 * reasons).
111 *
112 * As long as the entry is not dropped, 1 is added to the refcount
113 * representing that the entry should not be dropped. In addition each
114 * backend that has a reference to the entry needs to increment the
115 * refcount as long as it does.
116 *
117 * May only be incremented / decremented while holding at least a shared
118 * lock on the dshash partition containing the entry. It needs to be an
119 * atomic variable because multiple backends can increment the refcount
120 * with just a shared lock.
121 *
122 * When the refcount reaches 0 the entry needs to be freed.
123 */
125
126 /*
127 * Counter tracking the number of times the entry has been reused.
128 *
129 * Set to 0 when the entry is created, and incremented by one each time
130 * the shared entry is reinitialized with pgstat_reinit_entry().
131 *
132 * May only be incremented / decremented while holding at least a shared
133 * lock on the dshash partition containing the entry. Like refcount, it
134 * needs to be an atomic variable because multiple backends can increment
135 * the generation with just a shared lock.
136 */
138
139 /*
140 * Pointer to shared stats. The stats entry always starts with
141 * PgStatShared_Common, embedded in a larger struct containing the
142 * PgStat_Kind specific stats fields.
143 */
146
147/*
148 * Common header struct for PgStatShared_*.
149 */
151{
152 uint32 magic; /* just a validity cross-check */
153 /* lock protecting stats contents (i.e. data following the header) */
156
157/*
158 * A backend local reference to a shared stats entry. As long as at least one
159 * such reference exists, the shared stats entry will not be released.
160 *
161 * If there are pending stats update to the shared stats, these are stored in
162 * ->pending.
163 */
164typedef struct PgStat_EntryRef
165{
166 /*
167 * Pointer to the PgStatShared_HashEntry entry in the shared stats
168 * hashtable.
169 */
171
172 /*
173 * Pointer to the stats data (i.e. PgStatShared_HashEntry->body), resolved
174 * as a local pointer, to avoid repeated dsa_get_address() calls.
175 */
177
178 /*
179 * Copy of PgStatShared_HashEntry->generation, keeping locally track of
180 * the shared stats entry "generation" retrieved (number of times reused).
181 */
183
184 /*
185 * Pending statistics data that will need to be flushed to shared memory
186 * stats eventually. Each stats kind utilizing pending data defines what
187 * format its pending data has and needs to provide a
188 * PgStat_KindInfo->flush_pending_cb callback to merge pending entries
189 * into the shared stats hash table.
190 */
191 void *pending;
192 dlist_node pending_node; /* membership in pgStatPending list */
194
195
196/*
197 * Some stats changes are transactional. To maintain those, a stack of
198 * PgStat_SubXactStatus entries is maintained, which contain data pertaining
199 * to the current transaction and its active subtransactions.
200 */
202{
203 int nest_level; /* subtransaction nest level */
204
205 struct PgStat_SubXactStatus *prev; /* higher-level subxact if any */
206
207 /*
208 * Statistics for transactionally dropped objects need to be
209 * transactionally dropped as well. Collect the stats dropped in the
210 * current (sub-)transaction and only execute the stats drop when we know
211 * if the transaction commits/aborts. To handle replicas and crashes,
212 * stats drops are included in commit / abort records.
213 */
215
216 /*
217 * Tuple insertion/deletion counts for an open transaction can't be
218 * propagated into PgStat_TableStatus counters until we know if it is
219 * going to commit or abort. Hence, we keep these counts in per-subxact
220 * structs that live in TopTransactionContext. This data structure is
221 * designed on the assumption that subxacts won't usually modify very many
222 * tables.
223 */
224 PgStat_TableXactStatus *first; /* head of list for this subxact */
226
227
228/*
229 * Metadata for a specific kind of statistics.
230 */
231typedef struct PgStat_KindInfo
232{
233 /*
234 * Do a fixed number of stats objects exist for this kind of stats (e.g.
235 * bgwriter stats) or not (e.g. tables).
236 */
238
239 /*
240 * Can stats of this kind be accessed from another database? Determines
241 * whether a stats object gets included in stats snapshots.
242 */
244
245 /* Should stats be written to the on-disk stats file? */
247
248 /*
249 * Should the number of entries be tracked? For variable-numbered stats,
250 * to update its PgStat_ShmemControl.entry_counts.
251 */
253
254 /*
255 * The size of an entry in the shared stats hash table (pointed to by
256 * PgStatShared_HashEntry->body). For fixed-numbered statistics, this is
257 * the size of an entry in PgStat_ShmemControl->custom_data.
258 */
260
261 /*
262 * The offset of the statistics struct in the cached statistics snapshot
263 * PgStat_Snapshot, for fixed-numbered statistics.
264 */
266
267 /*
268 * The offset of the statistics struct in the containing shared memory
269 * control structure PgStat_ShmemControl, for fixed-numbered statistics.
270 */
272
273 /*
274 * The offset/size of statistics inside the shared stats entry. Used when
275 * [de-]serializing statistics to / from disk respectively. Separate from
276 * shared_size because [de-]serialization may not include in-memory state
277 * like lwlocks.
278 */
281
282 /*
283 * The size of the pending data for this kind. E.g. how large
284 * PgStat_EntryRef->pending is. Used for allocations.
285 *
286 * 0 signals that an entry of this kind should never have a pending entry.
287 */
289
290 /*
291 * Perform custom actions when initializing a backend (standalone or under
292 * postmaster). Optional.
293 */
295
296 /*
297 * For variable-numbered stats: flush pending stats. Required if pending
298 * data is used. See flush_static_cb when dealing with stats data that
299 * that cannot use PgStat_EntryRef->pending.
300 */
302
303 /*
304 * For variable-numbered stats: delete pending stats. Optional.
305 */
307
308 /*
309 * For variable-numbered stats: reset the reset timestamp. Optional.
310 */
312
313 /*
314 * For variable-numbered stats. Optional.
315 */
317 const PgStatShared_Common *header, NameData *name);
319
320 /*
321 * For variable-numbered stats: read or write additional data related to
322 * an entry, in the stats file or optionally in a different file.
323 * Optional.
324 *
325 * to_serialized_data: write auxiliary data for an entry.
326 *
327 * from_serialized_data: read auxiliary data for an entry. Returns true
328 * on success, false on read error.
329 *
330 * "statfile" is a pointer to the on-disk stats file, named
331 * PGSTAT_STAT_PERMANENT_FILENAME. "key" is the hash key of the entry
332 * just written or read. "header" is a pointer to the stats data; it may
333 * be modified only in from_serialized_data to reconstruct an entry.
334 */
336 const PgStatShared_Common *header,
337 FILE *statfile);
339 PgStatShared_Common *header,
340 FILE *statfile);
341
342 /*
343 * For fixed-numbered or variable-numbered statistics.
344 *
345 * Perform custom actions when done processing the on-disk stats file
346 * after all the stats entries have been processed. Optional.
347 *
348 * "status" tracks the operation done for the on-disk stats file (read,
349 * write, discard).
350 */
352
353 /*
354 * For fixed-numbered statistics: Initialize shared memory state.
355 *
356 * "stats" is the pointer to the allocated shared memory area.
357 */
358 void (*init_shmem_cb) (void *stats);
359
360 /*
361 * For fixed-numbered or variable-numbered statistics: Flush pending stats
362 * entries, for stats kinds that do not use PgStat_EntryRef->pending.
363 *
364 * Returns true if some of the stats could not be flushed, due to lock
365 * contention for example. Optional.
366 *
367 * "pgstat_report_fixed" needs to be set to trigger the flush of pending
368 * stats.
369 */
370 bool (*flush_static_cb) (bool nowait);
371
372 /*
373 * For fixed-numbered statistics: Reset All.
374 */
376
377 /*
378 * For fixed-numbered statistics: Build snapshot for entry
379 */
381
382 /* name of the kind of stats */
383 const char *const name;
385
386
387/*
388 * List of SLRU names that we keep stats for. There is no central registry of
389 * SLRUs, so we use this fixed list instead. The "other" entry is used for
390 * all SLRUs without an explicit entry (e.g. SLRUs in extensions).
391 *
392 * This is only defined here so that SLRU_NUM_ELEMENTS is known for later type
393 * definitions.
394 */
395static const char *const slru_names[] = {
396 "commit_timestamp",
397 "multixact_member",
398 "multixact_offset",
399 "notify",
400 "serializable",
401 "subtransaction",
402 "transaction",
403 "other" /* has to be last */
404};
405
406#define SLRU_NUM_ELEMENTS lengthof(slru_names)
407
408
409/* ----------
410 * Types and definitions for different kinds of fixed-amount stats.
411 *
412 * Single-writer stats use the changecount mechanism to achieve low-overhead
413 * writes - they're obviously more performance critical than reads. Check the
414 * definition of struct PgBackendStatus for some explanation of the
415 * changecount mechanism.
416 *
417 * Because the obvious implementation of resetting single-writer stats isn't
418 * compatible with that (another backend needs to write), we don't scribble on
419 * shared stats while resetting. Instead, just record the current counter
420 * values in a copy of the stats data, which is protected by ->lock. See
421 * pgstat_fetch_stat_(archiver|bgwriter|checkpointer) for the reader side.
422 *
423 * The only exception to that is the stat_reset_timestamp in these structs,
424 * which is protected by ->lock, because it has to be written by another
425 * backend while resetting.
426 * ----------
427 */
428
430{
431 /* lock protects ->reset_offset as well as stats->stat_reset_timestamp */
437
439{
440 /* lock protects ->reset_offset as well as stats->stat_reset_timestamp */
446
448{
449 /* lock protects ->reset_offset as well as stats->stat_reset_timestamp */
455
456/* Shared-memory ready PgStat_IO */
457typedef struct PgStatShared_IO
458{
459 /*
460 * locks[i] protects stats.stats[i]. locks[0] also protects
461 * stats.stat_reset_timestamp.
462 */
466
467typedef struct PgStatShared_Lock
468{
469 /* lock protects ->stats */
473
480
481typedef struct PgStatShared_Wal
482{
483 /* lock protects ->stats */
487
488
489
490/* ----------
491 * Types and definitions for different kinds of variable-amount stats.
492 *
493 * Each struct has to start with PgStatShared_Common, containing information
494 * common across the different types of stats. Kind-specific data follows.
495 * ----------
496 */
497
503
509
515
521
527
533
534/*
535 * Central shared memory entry for the cumulative stats system.
536 *
537 * Fixed amount stats, the dynamic shared memory hash table for
538 * non-fixed-amount stats, as well as remaining bits and pieces are all
539 * reached from here.
540 */
542{
544
545 /*
546 * Stats for variable-numbered objects are kept in this shared hash table.
547 * See comment above PgStat_Kind for details.
548 */
549 dshash_table_handle hash_handle; /* shared dbstat hash */
550
551 /* Has the stats system already been shut down? Just a debugging check. */
553
554 /*
555 * Whenever statistics for dropped objects could not be freed - because
556 * backends still have references - the dropping backend calls
557 * pgstat_request_entry_refs_gc() incrementing this counter. Eventually
558 * that causes backends to run pgstat_gc_entry_refs(), allowing memory to
559 * be reclaimed.
560 */
562
563 /*
564 * Counters for the number of entries associated to a single stats kind
565 * that uses variable-numbered objects stored in the shared hash table.
566 * These counters can be enabled on a per-kind basis, when
567 * track_entry_count is set. This counter is incremented each time a new
568 * entry is created (not reused) in the shared hash table, and is
569 * decremented each time an entry is freed from the shared hash table.
570 */
572
573 /*
574 * Stats data for fixed-numbered objects.
575 */
583
584 /*
585 * Custom stats data with fixed-numbered objects, indexed by (PgStat_Kind
586 * - PGSTAT_KIND_CUSTOM_MIN).
587 */
589
591
592
593/*
594 * Cached statistics snapshot
595 */
596typedef struct PgStat_Snapshot
597{
599
600 /* time at which snapshot was taken */
602
604
606
608
610
612
614
616
618
619 /*
620 * Data in snapshot for custom fixed-numbered statistics, indexed by
621 * (PgStat_Kind - PGSTAT_KIND_CUSTOM_MIN). Each entry is allocated in
622 * TopMemoryContext, for a size of PgStat_KindInfo->shared_data_len.
623 */
626
627 /* to free snapshot in bulk */
631
632
633/*
634 * Collection of backend-local stats state.
635 */
645
646
647/*
648 * Inline functions defined further below.
649 */
650
651static inline void pgstat_begin_changecount_write(uint32 *cc);
652static inline void pgstat_end_changecount_write(uint32 *cc);
654static inline bool pgstat_end_changecount_read(uint32 *cc, uint32 cc_before);
655
656static inline void pgstat_copy_changecounted_stats(void *dst, void *src, size_t len,
657 uint32 *cc);
658
659static inline int pgstat_cmp_hash_key(const void *a, const void *b, size_t size, void *arg);
660static inline uint32 pgstat_hash_hash_key(const void *d, size_t size, void *arg);
661static inline size_t pgstat_get_entry_len(PgStat_Kind kind);
662static inline void *pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry);
663static inline void *pgstat_get_custom_shmem_data(PgStat_Kind kind);
664static inline void *pgstat_get_custom_snapshot_data(PgStat_Kind kind);
665
666
667/*
668 * Functions in pgstat.c
669 */
670
672extern void pgstat_register_kind(PgStat_Kind kind,
674
675#ifdef USE_ASSERT_CHECKING
676extern void pgstat_assert_is_up(void);
677#else
678#define pgstat_assert_is_up() ((void)true)
679#endif
680
681extern void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref);
683 uint64 objid,
684 bool *created_entry);
686 Oid dboid, uint64 objid);
687
688extern void *pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid,
689 bool *may_free);
690extern void pgstat_snapshot_fixed(PgStat_Kind kind);
691
692
693/*
694 * Functions in pgstat_archiver.c
695 */
696
697extern void pgstat_archiver_init_shmem_cb(void *stats);
699extern void pgstat_archiver_snapshot_cb(void);
700
701/*
702 * Functions in pgstat_backend.c
703 */
704
705/* flags for pgstat_flush_backend() */
706#define PGSTAT_BACKEND_FLUSH_IO (1 << 0) /* Flush I/O statistics */
707#define PGSTAT_BACKEND_FLUSH_WAL (1 << 1) /* Flush WAL statistics */
708#define PGSTAT_BACKEND_FLUSH_ALL (PGSTAT_BACKEND_FLUSH_IO | PGSTAT_BACKEND_FLUSH_WAL)
709
710extern bool pgstat_flush_backend(bool nowait, uint32 flags);
711extern bool pgstat_backend_flush_cb(bool nowait);
713 TimestampTz ts);
714
715/*
716 * Functions in pgstat_bgwriter.c
717 */
718
719extern void pgstat_bgwriter_init_shmem_cb(void *stats);
721extern void pgstat_bgwriter_snapshot_cb(void);
722
723
724/*
725 * Functions in pgstat_checkpointer.c
726 */
727
728extern void pgstat_checkpointer_init_shmem_cb(void *stats);
730extern void pgstat_checkpointer_snapshot_cb(void);
731
732
733/*
734 * Functions in pgstat_database.c
735 */
736
737extern void pgstat_report_disconnect(Oid dboid);
738extern void pgstat_update_dbstats(TimestampTz ts);
739extern void AtEOXact_PgStat_Database(bool isCommit, bool parallel);
740
742extern void pgstat_reset_database_timestamp(Oid dboid, TimestampTz ts);
743extern bool pgstat_database_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
745
746
747/*
748 * Functions in pgstat_function.c
749 */
750
751extern bool pgstat_function_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
753
754
755/*
756 * Functions in pgstat_io.c
757 */
758
759extern void pgstat_flush_io(bool nowait);
760
761extern bool pgstat_io_flush_cb(bool nowait);
762extern void pgstat_io_init_shmem_cb(void *stats);
763extern void pgstat_io_reset_all_cb(TimestampTz ts);
764extern void pgstat_io_snapshot_cb(void);
765
766/*
767 * Functions in pgstat_lock.c
768 */
769
770extern bool pgstat_lock_flush_cb(bool nowait);
771extern void pgstat_lock_init_shmem_cb(void *stats);
773extern void pgstat_lock_snapshot_cb(void);
774
775/*
776 * Functions in pgstat_relation.c
777 */
778
779extern void AtEOXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit);
780extern void AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, int nestDepth);
781extern void AtPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state);
783
784extern bool pgstat_relation_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
787
788
789/*
790 * Functions in pgstat_replslot.c
791 */
792
796
797
798/*
799 * Functions in pgstat_shmem.c
800 */
801
802extern void pgstat_attach_shmem(void);
803extern void pgstat_detach_shmem(void);
804
806 bool create, bool *created_entry);
807extern bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait);
808extern bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait);
809extern void pgstat_unlock_entry(PgStat_EntryRef *entry_ref);
810extern bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid);
811extern void pgstat_drop_all_entries(void);
815 bool nowait);
816extern void pgstat_reset_entry(PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts);
820 TimestampTz ts);
821
822extern void pgstat_request_entry_refs_gc(void);
825
826
827/*
828 * Functions in pgstat_slru.c
829 */
830
831extern bool pgstat_slru_flush_cb(bool nowait);
832extern void pgstat_slru_init_shmem_cb(void *stats);
834extern void pgstat_slru_snapshot_cb(void);
835
836
837/*
838 * Functions in pgstat_wal.c
839 */
840
841extern void pgstat_wal_init_backend_cb(void);
842extern bool pgstat_wal_flush_cb(bool nowait);
843extern void pgstat_wal_init_shmem_cb(void *stats);
845extern void pgstat_wal_snapshot_cb(void);
846
847
848/*
849 * Functions in pgstat_subscription.c
850 */
851
852extern bool pgstat_subscription_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
854
855
856/*
857 * Functions in pgstat_xact.c
858 */
859
861extern void pgstat_drop_transactional(PgStat_Kind kind, Oid dboid, uint64 objid);
862extern void pgstat_create_transactional(PgStat_Kind kind, Oid dboid, uint64 objid);
863
864
865/*
866 * Variables in pgstat.c
867 */
868
869/*
870 * Track if *any* pending fixed-numbered statistics should be flushed to
871 * shared memory.
872 *
873 * This flag can be switched to true by fixed-numbered statistics to let
874 * pgstat_report_stat() know if it needs to go through one round of
875 * reports, calling flush_static_cb for each fixed-numbered statistics
876 * kind. When this flag is not set, pgstat_report_stat() is able to do
877 * a fast exit, knowing that there are no pending fixed-numbered statistics.
878 *
879 * Statistics callbacks should never reset this flag; pgstat_report_stat()
880 * is in charge of doing that.
881 */
883
884/* Backend-local stats state */
886
887/* Helper functions for reading and writing of on-disk stats file */
888extern void pgstat_write_chunk(FILE *fpout, void *ptr, size_t len);
889extern bool pgstat_read_chunk(FILE *fpin, void *ptr, size_t len);
890#define pgstat_read_chunk_s(fpin, ptr) pgstat_read_chunk(fpin, ptr, sizeof(*ptr))
891#define pgstat_write_chunk_s(fpout, ptr) pgstat_write_chunk(fpout, ptr, sizeof(*ptr))
892
893/*
894 * Implementation of inline functions declared above.
895 */
896
897/*
898 * Helpers for changecount manipulation. See comments around struct
899 * PgBackendStatus for details.
900 */
901
902static inline void
904{
905 Assert((*cc & 1) == 0);
906
908 (*cc)++;
910}
911
912static inline void
914{
915 Assert((*cc & 1) == 1);
916
918
919 (*cc)++;
920
922}
923
924static inline uint32
926{
927 uint32 before_cc = *cc;
928
930
932
933 return before_cc;
934}
935
936/*
937 * Returns true if the read succeeded, false if it needs to be repeated.
938 */
939static inline bool
941{
943
945
946 after_cc = *cc;
947
948 /* was a write in progress when we started? */
949 if (before_cc & 1)
950 return false;
951
952 /* did writes start and complete while we read? */
953 return before_cc == after_cc;
954}
955
956
957/*
958 * helper function for PgStat_KindInfo->snapshot_cb
959 * PgStat_KindInfo->reset_all_cb callbacks.
960 *
961 * Copies out the specified memory area following change-count protocol.
962 */
963static inline void
964pgstat_copy_changecounted_stats(void *dst, void *src, size_t len,
965 uint32 *cc)
966{
968
969 do
970 {
972
973 memcpy(dst, src, len);
974 }
976}
977
978/* helpers for dshash / simplehash hashtables */
979static inline int
980pgstat_cmp_hash_key(const void *a, const void *b, size_t size, void *arg)
981{
982 Assert(size == sizeof(PgStat_HashKey) && arg == NULL);
983 return memcmp(a, b, sizeof(PgStat_HashKey));
984}
985
986static inline uint32
987pgstat_hash_hash_key(const void *d, size_t size, void *arg)
988{
989 const char *key = (const char *) d;
990
991 Assert(size == sizeof(PgStat_HashKey) && arg == NULL);
992 return fasthash32(key, size, 0);
993}
994
995/*
996 * The length of the data portion of a shared memory stats entry (i.e. without
997 * transient data such as refcounts, lwlocks, ...).
998 */
999static inline size_t
1004
1005/*
1006 * Returns a pointer to the data portion of a shared memory stats entry.
1007 */
1008static inline void *
1010{
1011 size_t off = pgstat_get_kind_info(kind)->shared_data_off;
1012
1013 Assert(off != 0 && off < PG_UINT32_MAX);
1014
1015 return ((char *) (entry)) + off;
1016}
1017
1018/*
1019 * Returns the number of entries counted for a stats kind.
1020 */
1021static inline uint64
1023{
1024 Assert(pgstat_get_kind_info(kind)->track_entry_count);
1025
1027}
1028
1029/*
1030 * Returns a pointer to the shared memory area of custom stats for
1031 * fixed-numbered statistics.
1032 */
1033static inline void *
1035{
1036 int idx = kind - PGSTAT_KIND_CUSTOM_MIN;
1037
1039 Assert(pgstat_get_kind_info(kind)->fixed_amount);
1040
1042}
1043
1044/*
1045 * Returns a pointer to the portion of custom data for fixed-numbered
1046 * statistics in the current snapshot.
1047 */
1048static inline void *
1050{
1051 int idx = kind - PGSTAT_KIND_CUSTOM_MIN;
1052
1054 Assert(pgstat_get_kind_info(kind)->fixed_amount);
1055
1057}
1058
1059#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
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition atomics.h:467
#define PGDLLIMPORT
Definition c.h:1421
#define PG_UINT32_MAX
Definition c.h:674
#define Assert(condition)
Definition c.h:943
uint64_t uint64
Definition c.h:625
uint32_t uint32
Definition c.h:624
#define StaticAssertDecl(condition, errmessage)
Definition c.h:1008
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
int64 TimestampTz
Definition timestamp.h:39
uint64 dsa_pointer
Definition dsa.h:62
dsa_pointer dshash_table_handle
Definition dshash.h:24
Datum arg
Definition elog.c:1322
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:392
#define START_CRIT_SECTION()
Definition miscadmin.h:152
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:125
#define END_CRIT_SECTION()
Definition miscadmin.h:154
const void size_t len
PgStat_FetchConsistency
Definition pgstat.h:51
bool pgstat_replslot_from_serialized_name_cb(const NameData *name, PgStat_HashKey *key)
void pgstat_slru_snapshot_cb(void)
void pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts)
void pgstat_archiver_init_shmem_cb(void *stats)
void pgstat_function_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_request_entry_refs_gc(void)
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition pgstat.c:1104
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)
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)
void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref)
Definition pgstat.c:1360
bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
void pgstat_attach_shmem(void)
static uint64 pgstat_get_entry_count(PgStat_Kind kind)
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:1309
static const char *const slru_names[]
static int pgstat_cmp_hash_key(const void *a, const void *b, size_t size, void *arg)
void pgstat_database_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_checkpointer_snapshot_cb(void)
bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *may_free)
Definition pgstat.c:962
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)
void pgstat_wal_reset_all_cb(TimestampTz ts)
Definition pgstat_wal.c:158
bool pgstat_io_flush_cb(bool nowait)
Definition pgstat_io.c:189
void AtPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
PgStat_StatsFileOp
@ STATS_WRITE
@ STATS_READ
@ STATS_DISCARD
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:1479
PGDLLIMPORT PgStat_LocalState pgStatLocal
Definition pgstat.c:213
PGDLLIMPORT bool pgstat_report_fixed
Definition pgstat.c:219
bool pgstat_flush_backend(bool nowait, uint32 flags)
void pgstat_archiver_reset_all_cb(TimestampTz ts)
void pgstat_checkpointer_init_shmem_cb(void *stats)
void pgstat_bgwriter_reset_all_cb(TimestampTz ts)
void pgstat_drop_transactional(PgStat_Kind kind, Oid dboid, uint64 objid)
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)
void pgstat_create_transactional(PgStat_Kind kind, Oid dboid, uint64 objid)
bool pgstat_relation_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
bool pgstat_read_chunk(FILE *fpin, void *ptr, size_t len)
Definition pgstat.c:1797
void pgstat_wal_init_shmem_cb(void *stats)
Definition pgstat_wal.c:150
bool pgstat_lock_flush_cb(bool nowait)
Definition pgstat_lock.c:51
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)
void pgstat_replslot_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_lock_init_shmem_cb(void *stats)
Definition pgstat_lock.c:86
void pgstat_bgwriter_init_shmem_cb(void *stats)
PgStatShared_Common * pgstat_init_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
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:1347
void pgstat_slru_reset_all_cb(TimestampTz ts)
void pgstat_backend_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void AtEOXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit)
void pgstat_wal_init_backend_cb(void)
Definition pgstat_wal.c:139
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_relation_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_bgwriter_snapshot_cb(void)
void pgstat_lock_reset_all_cb(TimestampTz ts)
Definition pgstat_lock.c:94
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
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)
void PostPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
bool pgstat_database_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
#define SLRU_NUM_ELEMENTS
void pgstat_wal_snapshot_cb(void)
Definition pgstat_wal.c:169
static void pgstat_copy_changecounted_stats(void *dst, void *src, size_t len, uint32 *cc)
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:1507
void pgstat_io_snapshot_cb(void)
Definition pgstat_io.c:309
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
bool pgstat_backend_flush_cb(bool nowait)
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid, bool nowait)
static size_t pgstat_get_entry_len(PgStat_Kind kind)
void pgstat_io_init_shmem_cb(void *stats)
Definition pgstat_io.c:278
void pgstat_subscription_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_write_chunk(FILE *fpout, void *ptr, size_t len)
Definition pgstat.c:1599
void pgstat_archiver_snapshot_cb(void)
void pgstat_lock_snapshot_cb(void)
void pgstat_detach_shmem(void)
void pgstat_relation_delete_pending_cb(PgStat_EntryRef *entry_ref)
static bool pgstat_is_kind_custom(PgStat_Kind kind)
Definition pgstat_kind.h:68
#define PgStat_Kind
Definition pgstat_kind.h:17
#define PGSTAT_KIND_MAX
Definition pgstat_kind.h:21
#define PGSTAT_KIND_CUSTOM_MIN
Definition pgstat_kind.h:50
#define PGSTAT_KIND_CUSTOM_SIZE
Definition pgstat_kind.h:52
#define PGSTAT_KIND_BUILTIN_SIZE
Definition pgstat_kind.h:45
uint64_t Datum
Definition postgres.h:70
unsigned int Oid
static int fb(int x)
PgStat_ArchiverStats reset_offset
PgStat_ArchiverStats 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(* flush_static_cb)(bool nowait)
void(* finish)(PgStat_StatsFileOp status)
void(* to_serialized_data)(const PgStat_HashKey *key, const PgStatShared_Common *header, FILE *statfile)
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(* from_serialized_data)(const PgStat_HashKey *key, PgStatShared_Common *header, FILE *statfile)
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
void * custom_data[PGSTAT_KIND_CUSTOM_SIZE]
PgStatShared_Lock lock
pg_atomic_uint64 gc_request_count
PgStatShared_Wal wal
PgStatShared_Archiver archiver
PgStatShared_Checkpointer checkpointer
PgStatShared_BgWriter bgwriter
pg_atomic_uint64 entry_counts[PGSTAT_KIND_MAX]
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 c.h:830
const char * name