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