Line data Source code
1 : /* ----------
2 : * pgstat.h
3 : *
4 : * Definitions for the PostgreSQL cumulative statistics system.
5 : *
6 : * Copyright (c) 2001-2024, PostgreSQL Global Development Group
7 : *
8 : * src/include/pgstat.h
9 : * ----------
10 : */
11 : #ifndef PGSTAT_H
12 : #define PGSTAT_H
13 :
14 : #include "access/xlogdefs.h"
15 : #include "datatype/timestamp.h"
16 : #include "portability/instr_time.h"
17 : #include "postmaster/pgarch.h" /* for MAX_XFN_CHARS */
18 : #include "replication/conflict.h"
19 : #include "utils/backend_progress.h" /* for backward compatibility */
20 : #include "utils/backend_status.h" /* for backward compatibility */
21 : #include "utils/relcache.h"
22 : #include "utils/wait_event.h" /* for backward compatibility */
23 :
24 :
25 : /* ----------
26 : * Paths for the statistics files (relative to installation's $PGDATA).
27 : * ----------
28 : */
29 : #define PGSTAT_STAT_PERMANENT_DIRECTORY "pg_stat"
30 : #define PGSTAT_STAT_PERMANENT_FILENAME "pg_stat/pgstat.stat"
31 : #define PGSTAT_STAT_PERMANENT_TMPFILE "pg_stat/pgstat.tmp"
32 :
33 : /* Default directory to store temporary statistics data in */
34 : #define PG_STAT_TMP_DIR "pg_stat_tmp"
35 :
36 : /* The types of statistics entries */
37 : #define PgStat_Kind uint32
38 :
39 : /* Range of IDs allowed, for built-in and custom kinds */
40 : #define PGSTAT_KIND_MIN 1 /* Minimum ID allowed */
41 : #define PGSTAT_KIND_MAX 256 /* Maximum ID allowed */
42 :
43 : /* use 0 for INVALID, to catch zero-initialized data */
44 : #define PGSTAT_KIND_INVALID 0
45 :
46 : /* stats for variable-numbered objects */
47 : #define PGSTAT_KIND_DATABASE 1 /* database-wide statistics */
48 : #define PGSTAT_KIND_RELATION 2 /* per-table statistics */
49 : #define PGSTAT_KIND_FUNCTION 3 /* per-function statistics */
50 : #define PGSTAT_KIND_REPLSLOT 4 /* per-slot statistics */
51 : #define PGSTAT_KIND_SUBSCRIPTION 5 /* per-subscription statistics */
52 :
53 : /* stats for fixed-numbered objects */
54 : #define PGSTAT_KIND_ARCHIVER 6
55 : #define PGSTAT_KIND_BGWRITER 7
56 : #define PGSTAT_KIND_CHECKPOINTER 8
57 : #define PGSTAT_KIND_IO 9
58 : #define PGSTAT_KIND_SLRU 10
59 : #define PGSTAT_KIND_WAL 11
60 :
61 : #define PGSTAT_KIND_BUILTIN_MIN PGSTAT_KIND_DATABASE
62 : #define PGSTAT_KIND_BUILTIN_MAX PGSTAT_KIND_WAL
63 : #define PGSTAT_KIND_BUILTIN_SIZE (PGSTAT_KIND_BUILTIN_MAX + 1)
64 :
65 : /* Custom stats kinds */
66 :
67 : /* Range of IDs allowed for custom stats kinds */
68 : #define PGSTAT_KIND_CUSTOM_MIN 128
69 : #define PGSTAT_KIND_CUSTOM_MAX PGSTAT_KIND_MAX
70 : #define PGSTAT_KIND_CUSTOM_SIZE (PGSTAT_KIND_CUSTOM_MAX - PGSTAT_KIND_CUSTOM_MIN + 1)
71 :
72 : /*
73 : * PgStat_Kind to use for extensions that require an ID, but are still in
74 : * development and have not reserved their own unique kind ID yet. See:
75 : * https://wiki.postgresql.org/wiki/CustomCumulativeStats
76 : */
77 : #define PGSTAT_KIND_EXPERIMENTAL 128
78 :
79 : static inline bool
80 42384780 : pgstat_is_kind_builtin(PgStat_Kind kind)
81 : {
82 42384780 : return kind >= PGSTAT_KIND_BUILTIN_MIN && kind <= PGSTAT_KIND_BUILTIN_MAX;
83 : }
84 :
85 : static inline bool
86 32627722 : pgstat_is_kind_custom(PgStat_Kind kind)
87 : {
88 32627722 : return kind >= PGSTAT_KIND_CUSTOM_MIN && kind <= PGSTAT_KIND_CUSTOM_MAX;
89 : }
90 :
91 : /* Values for track_functions GUC variable --- order is significant! */
92 : typedef enum TrackFunctionsLevel
93 : {
94 : TRACK_FUNC_OFF,
95 : TRACK_FUNC_PL,
96 : TRACK_FUNC_ALL,
97 : } TrackFunctionsLevel;
98 :
99 : typedef enum PgStat_FetchConsistency
100 : {
101 : PGSTAT_FETCH_CONSISTENCY_NONE,
102 : PGSTAT_FETCH_CONSISTENCY_CACHE,
103 : PGSTAT_FETCH_CONSISTENCY_SNAPSHOT,
104 : } PgStat_FetchConsistency;
105 :
106 : /* Values to track the cause of session termination */
107 : typedef enum SessionEndType
108 : {
109 : DISCONNECT_NOT_YET, /* still active */
110 : DISCONNECT_NORMAL,
111 : DISCONNECT_CLIENT_EOF,
112 : DISCONNECT_FATAL,
113 : DISCONNECT_KILLED,
114 : } SessionEndType;
115 :
116 : /* ----------
117 : * The data type used for counters.
118 : * ----------
119 : */
120 : typedef int64 PgStat_Counter;
121 :
122 :
123 : /* ------------------------------------------------------------
124 : * Structures kept in backend local memory while accumulating counts
125 : * ------------------------------------------------------------
126 : */
127 :
128 : /* ----------
129 : * PgStat_FunctionCounts The actual per-function counts kept by a backend
130 : *
131 : * This struct should contain only actual event counters, because we memcmp
132 : * it against zeroes to detect whether there are any pending stats.
133 : *
134 : * Note that the time counters are in instr_time format here. We convert to
135 : * microseconds in PgStat_Counter format when flushing out pending statistics.
136 : * ----------
137 : */
138 : typedef struct PgStat_FunctionCounts
139 : {
140 : PgStat_Counter numcalls;
141 : instr_time total_time;
142 : instr_time self_time;
143 : } PgStat_FunctionCounts;
144 :
145 : /*
146 : * Working state needed to accumulate per-function-call timing statistics.
147 : */
148 : typedef struct PgStat_FunctionCallUsage
149 : {
150 : /* Link to function's hashtable entry (must still be there at exit!) */
151 : /* NULL means we are not tracking the current function call */
152 : PgStat_FunctionCounts *fs;
153 : /* Total time previously charged to function, as of function start */
154 : instr_time save_f_total_time;
155 : /* Backend-wide total time as of function start */
156 : instr_time save_total;
157 : /* system clock as of function start */
158 : instr_time start;
159 : } PgStat_FunctionCallUsage;
160 :
161 : /* ----------
162 : * PgStat_BackendSubEntry Non-flushed subscription stats.
163 : * ----------
164 : */
165 : typedef struct PgStat_BackendSubEntry
166 : {
167 : PgStat_Counter apply_error_count;
168 : PgStat_Counter sync_error_count;
169 : PgStat_Counter conflict_count[CONFLICT_NUM_TYPES];
170 : } PgStat_BackendSubEntry;
171 :
172 : /* ----------
173 : * PgStat_TableCounts The actual per-table counts kept by a backend
174 : *
175 : * This struct should contain only actual event counters, because we memcmp
176 : * it against zeroes to detect whether there are any stats updates to apply.
177 : * It is a component of PgStat_TableStatus (within-backend state).
178 : *
179 : * Note: for a table, tuples_returned is the number of tuples successfully
180 : * fetched by heap_getnext, while tuples_fetched is the number of tuples
181 : * successfully fetched by heap_fetch under the control of bitmap indexscans.
182 : * For an index, tuples_returned is the number of index entries returned by
183 : * the index AM, while tuples_fetched is the number of tuples successfully
184 : * fetched by heap_fetch under the control of simple indexscans for this index.
185 : *
186 : * tuples_inserted/updated/deleted/hot_updated/newpage_updated count attempted
187 : * actions, regardless of whether the transaction committed. delta_live_tuples,
188 : * delta_dead_tuples, and changed_tuples are set depending on commit or abort.
189 : * Note that delta_live_tuples and delta_dead_tuples can be negative!
190 : * ----------
191 : */
192 : typedef struct PgStat_TableCounts
193 : {
194 : PgStat_Counter numscans;
195 :
196 : PgStat_Counter tuples_returned;
197 : PgStat_Counter tuples_fetched;
198 :
199 : PgStat_Counter tuples_inserted;
200 : PgStat_Counter tuples_updated;
201 : PgStat_Counter tuples_deleted;
202 : PgStat_Counter tuples_hot_updated;
203 : PgStat_Counter tuples_newpage_updated;
204 : bool truncdropped;
205 :
206 : PgStat_Counter delta_live_tuples;
207 : PgStat_Counter delta_dead_tuples;
208 : PgStat_Counter changed_tuples;
209 :
210 : PgStat_Counter blocks_fetched;
211 : PgStat_Counter blocks_hit;
212 : } PgStat_TableCounts;
213 :
214 : /* ----------
215 : * PgStat_TableStatus Per-table status within a backend
216 : *
217 : * Many of the event counters are nontransactional, ie, we count events
218 : * in committed and aborted transactions alike. For these, we just count
219 : * directly in the PgStat_TableStatus. However, delta_live_tuples,
220 : * delta_dead_tuples, and changed_tuples must be derived from event counts
221 : * with awareness of whether the transaction or subtransaction committed or
222 : * aborted. Hence, we also keep a stack of per-(sub)transaction status
223 : * records for every table modified in the current transaction. At commit
224 : * or abort, we propagate tuples_inserted/updated/deleted up to the
225 : * parent subtransaction level, or out to the parent PgStat_TableStatus,
226 : * as appropriate.
227 : * ----------
228 : */
229 : typedef struct PgStat_TableStatus
230 : {
231 : Oid id; /* table's OID */
232 : bool shared; /* is it a shared catalog? */
233 : struct PgStat_TableXactStatus *trans; /* lowest subxact's counts */
234 : PgStat_TableCounts counts; /* event counts to be sent */
235 : Relation relation; /* rel that is using this entry */
236 : } PgStat_TableStatus;
237 :
238 : /* ----------
239 : * PgStat_TableXactStatus Per-table, per-subtransaction status
240 : * ----------
241 : */
242 : typedef struct PgStat_TableXactStatus
243 : {
244 : PgStat_Counter tuples_inserted; /* tuples inserted in (sub)xact */
245 : PgStat_Counter tuples_updated; /* tuples updated in (sub)xact */
246 : PgStat_Counter tuples_deleted; /* tuples deleted in (sub)xact */
247 : bool truncdropped; /* relation truncated/dropped in this
248 : * (sub)xact */
249 : /* tuples i/u/d prior to truncate/drop */
250 : PgStat_Counter inserted_pre_truncdrop;
251 : PgStat_Counter updated_pre_truncdrop;
252 : PgStat_Counter deleted_pre_truncdrop;
253 : int nest_level; /* subtransaction nest level */
254 : /* links to other structs for same relation: */
255 : struct PgStat_TableXactStatus *upper; /* next higher subxact if any */
256 : PgStat_TableStatus *parent; /* per-table status */
257 : /* structs of same subxact level are linked here: */
258 : struct PgStat_TableXactStatus *next; /* next of same subxact */
259 : } PgStat_TableXactStatus;
260 :
261 :
262 : /* ------------------------------------------------------------
263 : * Data structures on disk and in shared memory follow
264 : *
265 : * PGSTAT_FILE_FORMAT_ID should be changed whenever any of these
266 : * data structures change.
267 : * ------------------------------------------------------------
268 : */
269 :
270 : #define PGSTAT_FILE_FORMAT_ID 0x01A5BCAF
271 :
272 : typedef struct PgStat_ArchiverStats
273 : {
274 : PgStat_Counter archived_count; /* archival successes */
275 : char last_archived_wal[MAX_XFN_CHARS + 1]; /* last WAL file
276 : * archived */
277 : TimestampTz last_archived_timestamp; /* last archival success time */
278 : PgStat_Counter failed_count; /* failed archival attempts */
279 : char last_failed_wal[MAX_XFN_CHARS + 1]; /* WAL file involved in
280 : * last failure */
281 : TimestampTz last_failed_timestamp; /* last archival failure time */
282 : TimestampTz stat_reset_timestamp;
283 : } PgStat_ArchiverStats;
284 :
285 : typedef struct PgStat_BgWriterStats
286 : {
287 : PgStat_Counter buf_written_clean;
288 : PgStat_Counter maxwritten_clean;
289 : PgStat_Counter buf_alloc;
290 : TimestampTz stat_reset_timestamp;
291 : } PgStat_BgWriterStats;
292 :
293 : typedef struct PgStat_CheckpointerStats
294 : {
295 : PgStat_Counter num_timed;
296 : PgStat_Counter num_requested;
297 : PgStat_Counter num_performed;
298 : PgStat_Counter restartpoints_timed;
299 : PgStat_Counter restartpoints_requested;
300 : PgStat_Counter restartpoints_performed;
301 : PgStat_Counter write_time; /* times in milliseconds */
302 : PgStat_Counter sync_time;
303 : PgStat_Counter buffers_written;
304 : PgStat_Counter slru_written;
305 : TimestampTz stat_reset_timestamp;
306 : } PgStat_CheckpointerStats;
307 :
308 :
309 : /*
310 : * Types related to counting IO operations
311 : */
312 : typedef enum IOObject
313 : {
314 : IOOBJECT_RELATION,
315 : IOOBJECT_TEMP_RELATION,
316 : } IOObject;
317 :
318 : #define IOOBJECT_NUM_TYPES (IOOBJECT_TEMP_RELATION + 1)
319 :
320 : typedef enum IOContext
321 : {
322 : IOCONTEXT_BULKREAD,
323 : IOCONTEXT_BULKWRITE,
324 : IOCONTEXT_NORMAL,
325 : IOCONTEXT_VACUUM,
326 : } IOContext;
327 :
328 : #define IOCONTEXT_NUM_TYPES (IOCONTEXT_VACUUM + 1)
329 :
330 : typedef enum IOOp
331 : {
332 : IOOP_EVICT,
333 : IOOP_EXTEND,
334 : IOOP_FSYNC,
335 : IOOP_HIT,
336 : IOOP_READ,
337 : IOOP_REUSE,
338 : IOOP_WRITE,
339 : IOOP_WRITEBACK,
340 : } IOOp;
341 :
342 : #define IOOP_NUM_TYPES (IOOP_WRITEBACK + 1)
343 :
344 : typedef struct PgStat_BktypeIO
345 : {
346 : PgStat_Counter counts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES];
347 : PgStat_Counter times[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES];
348 : } PgStat_BktypeIO;
349 :
350 : typedef struct PgStat_IO
351 : {
352 : TimestampTz stat_reset_timestamp;
353 : PgStat_BktypeIO stats[BACKEND_NUM_TYPES];
354 : } PgStat_IO;
355 :
356 :
357 : typedef struct PgStat_StatDBEntry
358 : {
359 : PgStat_Counter xact_commit;
360 : PgStat_Counter xact_rollback;
361 : PgStat_Counter blocks_fetched;
362 : PgStat_Counter blocks_hit;
363 : PgStat_Counter tuples_returned;
364 : PgStat_Counter tuples_fetched;
365 : PgStat_Counter tuples_inserted;
366 : PgStat_Counter tuples_updated;
367 : PgStat_Counter tuples_deleted;
368 : TimestampTz last_autovac_time;
369 : PgStat_Counter conflict_tablespace;
370 : PgStat_Counter conflict_lock;
371 : PgStat_Counter conflict_snapshot;
372 : PgStat_Counter conflict_logicalslot;
373 : PgStat_Counter conflict_bufferpin;
374 : PgStat_Counter conflict_startup_deadlock;
375 : PgStat_Counter temp_files;
376 : PgStat_Counter temp_bytes;
377 : PgStat_Counter deadlocks;
378 : PgStat_Counter checksum_failures;
379 : TimestampTz last_checksum_failure;
380 : PgStat_Counter blk_read_time; /* times in microseconds */
381 : PgStat_Counter blk_write_time;
382 : PgStat_Counter sessions;
383 : PgStat_Counter session_time;
384 : PgStat_Counter active_time;
385 : PgStat_Counter idle_in_transaction_time;
386 : PgStat_Counter sessions_abandoned;
387 : PgStat_Counter sessions_fatal;
388 : PgStat_Counter sessions_killed;
389 : PgStat_Counter parallel_workers_to_launch;
390 : PgStat_Counter parallel_workers_launched;
391 :
392 : TimestampTz stat_reset_timestamp;
393 : } PgStat_StatDBEntry;
394 :
395 : typedef struct PgStat_StatFuncEntry
396 : {
397 : PgStat_Counter numcalls;
398 :
399 : PgStat_Counter total_time; /* times in microseconds */
400 : PgStat_Counter self_time;
401 : } PgStat_StatFuncEntry;
402 :
403 : typedef struct PgStat_StatReplSlotEntry
404 : {
405 : PgStat_Counter spill_txns;
406 : PgStat_Counter spill_count;
407 : PgStat_Counter spill_bytes;
408 : PgStat_Counter stream_txns;
409 : PgStat_Counter stream_count;
410 : PgStat_Counter stream_bytes;
411 : PgStat_Counter total_txns;
412 : PgStat_Counter total_bytes;
413 : TimestampTz stat_reset_timestamp;
414 : } PgStat_StatReplSlotEntry;
415 :
416 : typedef struct PgStat_SLRUStats
417 : {
418 : PgStat_Counter blocks_zeroed;
419 : PgStat_Counter blocks_hit;
420 : PgStat_Counter blocks_read;
421 : PgStat_Counter blocks_written;
422 : PgStat_Counter blocks_exists;
423 : PgStat_Counter flush;
424 : PgStat_Counter truncate;
425 : TimestampTz stat_reset_timestamp;
426 : } PgStat_SLRUStats;
427 :
428 : typedef struct PgStat_StatSubEntry
429 : {
430 : PgStat_Counter apply_error_count;
431 : PgStat_Counter sync_error_count;
432 : PgStat_Counter conflict_count[CONFLICT_NUM_TYPES];
433 : TimestampTz stat_reset_timestamp;
434 : } PgStat_StatSubEntry;
435 :
436 : typedef struct PgStat_StatTabEntry
437 : {
438 : PgStat_Counter numscans;
439 : TimestampTz lastscan;
440 :
441 : PgStat_Counter tuples_returned;
442 : PgStat_Counter tuples_fetched;
443 :
444 : PgStat_Counter tuples_inserted;
445 : PgStat_Counter tuples_updated;
446 : PgStat_Counter tuples_deleted;
447 : PgStat_Counter tuples_hot_updated;
448 : PgStat_Counter tuples_newpage_updated;
449 :
450 : PgStat_Counter live_tuples;
451 : PgStat_Counter dead_tuples;
452 : PgStat_Counter mod_since_analyze;
453 : PgStat_Counter ins_since_vacuum;
454 :
455 : PgStat_Counter blocks_fetched;
456 : PgStat_Counter blocks_hit;
457 :
458 : TimestampTz last_vacuum_time; /* user initiated vacuum */
459 : PgStat_Counter vacuum_count;
460 : TimestampTz last_autovacuum_time; /* autovacuum initiated */
461 : PgStat_Counter autovacuum_count;
462 : TimestampTz last_analyze_time; /* user initiated */
463 : PgStat_Counter analyze_count;
464 : TimestampTz last_autoanalyze_time; /* autovacuum initiated */
465 : PgStat_Counter autoanalyze_count;
466 : } PgStat_StatTabEntry;
467 :
468 : typedef struct PgStat_WalStats
469 : {
470 : PgStat_Counter wal_records;
471 : PgStat_Counter wal_fpi;
472 : uint64 wal_bytes;
473 : PgStat_Counter wal_buffers_full;
474 : PgStat_Counter wal_write;
475 : PgStat_Counter wal_sync;
476 : PgStat_Counter wal_write_time;
477 : PgStat_Counter wal_sync_time;
478 : TimestampTz stat_reset_timestamp;
479 : } PgStat_WalStats;
480 :
481 : /*
482 : * This struct stores wal-related durations as instr_time, which makes it
483 : * cheaper and easier to accumulate them, by not requiring type
484 : * conversions. During stats flush instr_time will be converted into
485 : * microseconds.
486 : */
487 : typedef struct PgStat_PendingWalStats
488 : {
489 : PgStat_Counter wal_buffers_full;
490 : PgStat_Counter wal_write;
491 : PgStat_Counter wal_sync;
492 : instr_time wal_write_time;
493 : instr_time wal_sync_time;
494 : } PgStat_PendingWalStats;
495 :
496 :
497 : /*
498 : * Functions in pgstat.c
499 : */
500 :
501 : /* functions called from postmaster */
502 : extern Size StatsShmemSize(void);
503 : extern void StatsShmemInit(void);
504 :
505 : /* Functions called during server startup / shutdown */
506 : extern void pgstat_restore_stats(XLogRecPtr redo);
507 : extern void pgstat_discard_stats(void);
508 : extern void pgstat_before_server_shutdown(int code, Datum arg);
509 :
510 : /* Functions for backend initialization */
511 : extern void pgstat_initialize(void);
512 :
513 : /* Functions called from backends */
514 : extern long pgstat_report_stat(bool force);
515 : extern void pgstat_force_next_flush(void);
516 :
517 : extern void pgstat_reset_counters(void);
518 : extern void pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid);
519 : extern void pgstat_reset_of_kind(PgStat_Kind kind);
520 :
521 : /* stats accessors */
522 : extern void pgstat_clear_snapshot(void);
523 : extern TimestampTz pgstat_get_stat_snapshot_timestamp(bool *have_snapshot);
524 :
525 : /* helpers */
526 : extern PgStat_Kind pgstat_get_kind_from_str(char *kind_str);
527 : extern bool pgstat_have_entry(PgStat_Kind kind, Oid dboid, uint64 objid);
528 :
529 :
530 : /*
531 : * Functions in pgstat_archiver.c
532 : */
533 :
534 : extern void pgstat_report_archiver(const char *xlog, bool failed);
535 : extern PgStat_ArchiverStats *pgstat_fetch_stat_archiver(void);
536 :
537 :
538 : /*
539 : * Functions in pgstat_bgwriter.c
540 : */
541 :
542 : extern void pgstat_report_bgwriter(void);
543 : extern PgStat_BgWriterStats *pgstat_fetch_stat_bgwriter(void);
544 :
545 :
546 : /*
547 : * Functions in pgstat_checkpointer.c
548 : */
549 :
550 : extern void pgstat_report_checkpointer(void);
551 : extern PgStat_CheckpointerStats *pgstat_fetch_stat_checkpointer(void);
552 :
553 :
554 : /*
555 : * Functions in pgstat_io.c
556 : */
557 :
558 : extern bool pgstat_bktype_io_stats_valid(PgStat_BktypeIO *backend_io,
559 : BackendType bktype);
560 : extern void pgstat_count_io_op(IOObject io_object, IOContext io_context, IOOp io_op);
561 : extern void pgstat_count_io_op_n(IOObject io_object, IOContext io_context, IOOp io_op, uint32 cnt);
562 : extern instr_time pgstat_prepare_io_time(bool track_io_guc);
563 : extern void pgstat_count_io_op_time(IOObject io_object, IOContext io_context,
564 : IOOp io_op, instr_time start_time, uint32 cnt);
565 :
566 : extern PgStat_IO *pgstat_fetch_stat_io(void);
567 : extern const char *pgstat_get_io_context_name(IOContext io_context);
568 : extern const char *pgstat_get_io_object_name(IOObject io_object);
569 :
570 : extern bool pgstat_tracks_io_bktype(BackendType bktype);
571 : extern bool pgstat_tracks_io_object(BackendType bktype,
572 : IOObject io_object, IOContext io_context);
573 : extern bool pgstat_tracks_io_op(BackendType bktype, IOObject io_object,
574 : IOContext io_context, IOOp io_op);
575 :
576 :
577 : /*
578 : * Functions in pgstat_database.c
579 : */
580 :
581 : extern void pgstat_drop_database(Oid databaseid);
582 : extern void pgstat_report_autovac(Oid dboid);
583 : extern void pgstat_report_recovery_conflict(int reason);
584 : extern void pgstat_report_deadlock(void);
585 : extern void pgstat_report_checksum_failures_in_db(Oid dboid, int failurecount);
586 : extern void pgstat_report_checksum_failure(void);
587 : extern void pgstat_report_connect(Oid dboid);
588 : extern void pgstat_update_parallel_workers_stats(PgStat_Counter workers_to_launch,
589 : PgStat_Counter workers_launched);
590 :
591 : #define pgstat_count_buffer_read_time(n) \
592 : (pgStatBlockReadTime += (n))
593 : #define pgstat_count_buffer_write_time(n) \
594 : (pgStatBlockWriteTime += (n))
595 : #define pgstat_count_conn_active_time(n) \
596 : (pgStatActiveTime += (n))
597 : #define pgstat_count_conn_txn_idle_time(n) \
598 : (pgStatTransactionIdleTime += (n))
599 :
600 : extern PgStat_StatDBEntry *pgstat_fetch_stat_dbentry(Oid dboid);
601 :
602 :
603 : /*
604 : * Functions in pgstat_function.c
605 : */
606 :
607 : extern void pgstat_create_function(Oid proid);
608 : extern void pgstat_drop_function(Oid proid);
609 :
610 : struct FunctionCallInfoBaseData;
611 : extern void pgstat_init_function_usage(struct FunctionCallInfoBaseData *fcinfo,
612 : PgStat_FunctionCallUsage *fcu);
613 : extern void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu,
614 : bool finalize);
615 :
616 : extern PgStat_StatFuncEntry *pgstat_fetch_stat_funcentry(Oid func_id);
617 : extern PgStat_FunctionCounts *find_funcstat_entry(Oid func_id);
618 :
619 :
620 : /*
621 : * Functions in pgstat_relation.c
622 : */
623 :
624 : extern void pgstat_create_relation(Relation rel);
625 : extern void pgstat_drop_relation(Relation rel);
626 : extern void pgstat_copy_relation_stats(Relation dst, Relation src);
627 :
628 : extern void pgstat_init_relation(Relation rel);
629 : extern void pgstat_assoc_relation(Relation rel);
630 : extern void pgstat_unlink_relation(Relation rel);
631 :
632 : extern void pgstat_report_vacuum(Oid tableoid, bool shared,
633 : PgStat_Counter livetuples, PgStat_Counter deadtuples);
634 : extern void pgstat_report_analyze(Relation rel,
635 : PgStat_Counter livetuples, PgStat_Counter deadtuples,
636 : bool resetcounter);
637 :
638 : /*
639 : * If stats are enabled, but pending data hasn't been prepared yet, call
640 : * pgstat_assoc_relation() to do so. See its comment for why this is done
641 : * separately from pgstat_init_relation().
642 : */
643 : #define pgstat_should_count_relation(rel) \
644 : (likely((rel)->pgstat_info != NULL) ? true : \
645 : ((rel)->pgstat_enabled ? pgstat_assoc_relation(rel), true : false))
646 :
647 : /* nontransactional event counts are simple enough to inline */
648 :
649 : #define pgstat_count_heap_scan(rel) \
650 : do { \
651 : if (pgstat_should_count_relation(rel)) \
652 : (rel)->pgstat_info->counts.numscans++; \
653 : } while (0)
654 : #define pgstat_count_heap_getnext(rel) \
655 : do { \
656 : if (pgstat_should_count_relation(rel)) \
657 : (rel)->pgstat_info->counts.tuples_returned++; \
658 : } while (0)
659 : #define pgstat_count_heap_fetch(rel) \
660 : do { \
661 : if (pgstat_should_count_relation(rel)) \
662 : (rel)->pgstat_info->counts.tuples_fetched++; \
663 : } while (0)
664 : #define pgstat_count_index_scan(rel) \
665 : do { \
666 : if (pgstat_should_count_relation(rel)) \
667 : (rel)->pgstat_info->counts.numscans++; \
668 : } while (0)
669 : #define pgstat_count_index_tuples(rel, n) \
670 : do { \
671 : if (pgstat_should_count_relation(rel)) \
672 : (rel)->pgstat_info->counts.tuples_returned += (n); \
673 : } while (0)
674 : #define pgstat_count_buffer_read(rel) \
675 : do { \
676 : if (pgstat_should_count_relation(rel)) \
677 : (rel)->pgstat_info->counts.blocks_fetched++; \
678 : } while (0)
679 : #define pgstat_count_buffer_hit(rel) \
680 : do { \
681 : if (pgstat_should_count_relation(rel)) \
682 : (rel)->pgstat_info->counts.blocks_hit++; \
683 : } while (0)
684 :
685 : extern void pgstat_count_heap_insert(Relation rel, PgStat_Counter n);
686 : extern void pgstat_count_heap_update(Relation rel, bool hot, bool newpage);
687 : extern void pgstat_count_heap_delete(Relation rel);
688 : extern void pgstat_count_truncate(Relation rel);
689 : extern void pgstat_update_heap_dead_tuples(Relation rel, int delta);
690 :
691 : extern void pgstat_twophase_postcommit(TransactionId xid, uint16 info,
692 : void *recdata, uint32 len);
693 : extern void pgstat_twophase_postabort(TransactionId xid, uint16 info,
694 : void *recdata, uint32 len);
695 :
696 : extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry(Oid relid);
697 : extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry_ext(bool shared,
698 : Oid reloid);
699 : extern PgStat_TableStatus *find_tabstat_entry(Oid rel_id);
700 :
701 :
702 : /*
703 : * Functions in pgstat_replslot.c
704 : */
705 :
706 : extern void pgstat_reset_replslot(const char *name);
707 : struct ReplicationSlot;
708 : extern void pgstat_report_replslot(struct ReplicationSlot *slot, const PgStat_StatReplSlotEntry *repSlotStat);
709 : extern void pgstat_create_replslot(struct ReplicationSlot *slot);
710 : extern void pgstat_acquire_replslot(struct ReplicationSlot *slot);
711 : extern void pgstat_drop_replslot(struct ReplicationSlot *slot);
712 : extern PgStat_StatReplSlotEntry *pgstat_fetch_replslot(NameData slotname);
713 :
714 :
715 : /*
716 : * Functions in pgstat_slru.c
717 : */
718 :
719 : extern void pgstat_reset_slru(const char *);
720 : extern void pgstat_count_slru_page_zeroed(int slru_idx);
721 : extern void pgstat_count_slru_page_hit(int slru_idx);
722 : extern void pgstat_count_slru_page_read(int slru_idx);
723 : extern void pgstat_count_slru_page_written(int slru_idx);
724 : extern void pgstat_count_slru_page_exists(int slru_idx);
725 : extern void pgstat_count_slru_flush(int slru_idx);
726 : extern void pgstat_count_slru_truncate(int slru_idx);
727 : extern const char *pgstat_get_slru_name(int slru_idx);
728 : extern int pgstat_get_slru_index(const char *name);
729 : extern PgStat_SLRUStats *pgstat_fetch_slru(void);
730 :
731 :
732 : /*
733 : * Functions in pgstat_subscription.c
734 : */
735 :
736 : extern void pgstat_report_subscription_error(Oid subid, bool is_apply_error);
737 : extern void pgstat_report_subscription_conflict(Oid subid, ConflictType type);
738 : extern void pgstat_create_subscription(Oid subid);
739 : extern void pgstat_drop_subscription(Oid subid);
740 : extern PgStat_StatSubEntry *pgstat_fetch_stat_subscription(Oid subid);
741 :
742 :
743 : /*
744 : * Functions in pgstat_xact.c
745 : */
746 :
747 : extern void AtEOXact_PgStat(bool isCommit, bool parallel);
748 : extern void AtEOSubXact_PgStat(bool isCommit, int nestDepth);
749 : extern void AtPrepare_PgStat(void);
750 : extern void PostPrepare_PgStat(void);
751 : struct xl_xact_stats_item;
752 : extern int pgstat_get_transactional_drops(bool isCommit, struct xl_xact_stats_item **items);
753 : extern void pgstat_execute_transactional_drops(int ndrops, struct xl_xact_stats_item *items, bool is_redo);
754 :
755 :
756 : /*
757 : * Functions in pgstat_wal.c
758 : */
759 :
760 : extern void pgstat_report_wal(bool force);
761 : extern PgStat_WalStats *pgstat_fetch_stat_wal(void);
762 :
763 :
764 : /*
765 : * Variables in pgstat.c
766 : */
767 :
768 : /* GUC parameters */
769 : extern PGDLLIMPORT bool pgstat_track_counts;
770 : extern PGDLLIMPORT int pgstat_track_functions;
771 : extern PGDLLIMPORT int pgstat_fetch_consistency;
772 :
773 :
774 : /*
775 : * Variables in pgstat_bgwriter.c
776 : */
777 :
778 : /* updated directly by bgwriter and bufmgr */
779 : extern PGDLLIMPORT PgStat_BgWriterStats PendingBgWriterStats;
780 :
781 :
782 : /*
783 : * Variables in pgstat_checkpointer.c
784 : */
785 :
786 : /*
787 : * Checkpointer statistics counters are updated directly by checkpointer and
788 : * bufmgr.
789 : */
790 : extern PGDLLIMPORT PgStat_CheckpointerStats PendingCheckpointerStats;
791 :
792 :
793 : /*
794 : * Variables in pgstat_database.c
795 : */
796 :
797 : /* Updated by pgstat_count_buffer_*_time macros */
798 : extern PGDLLIMPORT PgStat_Counter pgStatBlockReadTime;
799 : extern PGDLLIMPORT PgStat_Counter pgStatBlockWriteTime;
800 :
801 : /*
802 : * Updated by pgstat_count_conn_*_time macros, called by
803 : * pgstat_report_activity().
804 : */
805 : extern PGDLLIMPORT PgStat_Counter pgStatActiveTime;
806 : extern PGDLLIMPORT PgStat_Counter pgStatTransactionIdleTime;
807 :
808 : /* updated by the traffic cop and in errfinish() */
809 : extern PGDLLIMPORT SessionEndType pgStatSessionEndCause;
810 :
811 :
812 : /*
813 : * Variables in pgstat_wal.c
814 : */
815 :
816 : /* updated directly by backends and background processes */
817 : extern PGDLLIMPORT PgStat_PendingWalStats PendingWalStats;
818 :
819 :
820 : #endif /* PGSTAT_H */
|