PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pgstat.c File Reference
#include "postgres.h"
#include <unistd.h>
#include "access/xact.h"
#include "lib/dshash.h"
#include "pgstat.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/lwlock.h"
#include "utils/guc_hooks.h"
#include "utils/memutils.h"
#include "utils/pgstat_internal.h"
#include "utils/timestamp.h"
#include "lib/simplehash.h"
Include dependency graph for pgstat.c:

Go to the source code of this file.

Data Structures

struct  PgStat_SnapshotEntry
 

Macros

#define PGSTAT_MIN_INTERVAL   1000
 
#define PGSTAT_MAX_INTERVAL   60000
 
#define PGSTAT_IDLE_INTERVAL   10000
 
#define PGSTAT_SNAPSHOT_HASH_SIZE   512
 
#define PGSTAT_FILE_ENTRY_END   'E' /* end of file */
 
#define PGSTAT_FILE_ENTRY_FIXED   'F' /* fixed-numbered stats entry */
 
#define PGSTAT_FILE_ENTRY_NAME   'N' /* stats entry identified by name */
 
#define PGSTAT_FILE_ENTRY_HASH
 
#define SH_PREFIX   pgstat_snapshot
 
#define SH_ELEMENT_TYPE   PgStat_SnapshotEntry
 
#define SH_KEY_TYPE   PgStat_HashKey
 
#define SH_KEY   key
 
#define SH_HASH_KEY(tb, key)    pgstat_hash_hash_key(&key, sizeof(PgStat_HashKey), NULL)
 
#define SH_EQUAL(tb, a, b)    pgstat_cmp_hash_key(&a, &b, sizeof(PgStat_HashKey), NULL) == 0
 
#define SH_SCOPE   static inline
 
#define SH_DEFINE
 
#define SH_DECLARE
 

Typedefs

typedef struct PgStat_SnapshotEntry PgStat_SnapshotEntry
 

Functions

static void pgstat_write_statsfile (void)
 
static void pgstat_read_statsfile (void)
 
static void pgstat_init_snapshot_fixed (void)
 
static void pgstat_reset_after_failure (void)
 
static bool pgstat_flush_pending_entries (bool nowait)
 
static void pgstat_prep_snapshot (void)
 
static void pgstat_build_snapshot (void)
 
static void pgstat_build_snapshot_fixed (PgStat_Kind kind)
 
static bool pgstat_is_kind_valid (PgStat_Kind kind)
 
void pgstat_restore_stats (void)
 
void pgstat_discard_stats (void)
 
void pgstat_before_server_shutdown (int code, Datum arg)
 
static void pgstat_shutdown_hook (int code, Datum arg)
 
void pgstat_initialize (void)
 
long pgstat_report_stat (bool force)
 
void pgstat_force_next_flush (void)
 
static bool match_db_entries (PgStatShared_HashEntry *entry, Datum match_data)
 
void pgstat_reset_counters (void)
 
void pgstat_reset (PgStat_Kind kind, Oid dboid, uint64 objid)
 
void pgstat_reset_of_kind (PgStat_Kind kind)
 
void pgstat_clear_snapshot (void)
 
voidpgstat_fetch_entry (PgStat_Kind kind, Oid dboid, uint64 objid)
 
TimestampTz pgstat_get_stat_snapshot_timestamp (bool *have_snapshot)
 
bool pgstat_have_entry (PgStat_Kind kind, Oid dboid, uint64 objid)
 
void pgstat_snapshot_fixed (PgStat_Kind kind)
 
PgStat_EntryRefpgstat_prep_pending_entry (PgStat_Kind kind, Oid dboid, uint64 objid, bool *created_entry)
 
PgStat_EntryRefpgstat_fetch_pending_entry (PgStat_Kind kind, Oid dboid, uint64 objid)
 
void pgstat_delete_pending_entry (PgStat_EntryRef *entry_ref)
 
PgStat_Kind pgstat_get_kind_from_str (char *kind_str)
 
const PgStat_KindInfopgstat_get_kind_info (PgStat_Kind kind)
 
void pgstat_register_kind (PgStat_Kind kind, const PgStat_KindInfo *kind_info)
 
void pgstat_write_chunk (FILE *fpout, void *ptr, size_t len)
 
bool pgstat_read_chunk (FILE *fpin, void *ptr, size_t len)
 
void assign_stats_fetch_consistency (int newval, void *extra)
 

Variables

bool pgstat_track_counts = false
 
int pgstat_fetch_consistency = PGSTAT_FETCH_CONSISTENCY_CACHE
 
PgStat_LocalState pgStatLocal
 
bool pgstat_report_fixed = false
 
static MemoryContext pgStatPendingContext = NULL
 
static dlist_head pgStatPending = DLIST_STATIC_INIT(pgStatPending)
 
static bool pgStatForceNextFlush = false
 
static bool force_stats_snapshot_clear = false
 
static const PgStat_KindInfo pgstat_kind_builtin_infos [PGSTAT_KIND_BUILTIN_SIZE]
 
static const PgStat_KindInfo ** pgstat_kind_custom_infos = NULL
 

Macro Definition Documentation

◆ PGSTAT_FILE_ENTRY_END

#define PGSTAT_FILE_ENTRY_END   'E' /* end of file */

Definition at line 143 of file pgstat.c.

◆ PGSTAT_FILE_ENTRY_FIXED

#define PGSTAT_FILE_ENTRY_FIXED   'F' /* fixed-numbered stats entry */

Definition at line 144 of file pgstat.c.

◆ PGSTAT_FILE_ENTRY_HASH

#define PGSTAT_FILE_ENTRY_HASH
Value:
'S' /* stats entry identified by
* PgStat_HashKey */

Definition at line 146 of file pgstat.c.

151{
153 char status; /* for simplehash use */
154 void *data; /* the stats data itself */
156
157
158/* ----------
159 * Backend-local Hash Table Definitions
160 * ----------
161 */
162
163/* for stats snapshot entries */
164#define SH_PREFIX pgstat_snapshot
165#define SH_ELEMENT_TYPE PgStat_SnapshotEntry
166#define SH_KEY_TYPE PgStat_HashKey
167#define SH_KEY key
168#define SH_HASH_KEY(tb, key) \
169 pgstat_hash_hash_key(&key, sizeof(PgStat_HashKey), NULL)
170#define SH_EQUAL(tb, a, b) \
171 pgstat_cmp_hash_key(&a, &b, sizeof(PgStat_HashKey), NULL) == 0
172#define SH_SCOPE static inline
173#define SH_DEFINE
174#define SH_DECLARE
175#include "lib/simplehash.h"
176
177
178/* ----------
179 * Local function forward declarations
180 * ----------
181 */
182
183static void pgstat_write_statsfile(void);
184static void pgstat_read_statsfile(void);
185
186static void pgstat_init_snapshot_fixed(void);
187
188static void pgstat_reset_after_failure(void);
189
190static bool pgstat_flush_pending_entries(bool nowait);
191
192static void pgstat_prep_snapshot(void);
193static void pgstat_build_snapshot(void);
195
196static inline bool pgstat_is_kind_valid(PgStat_Kind kind);
197
198
199/* ----------
200 * GUC parameters
201 * ----------
202 */
203
204bool pgstat_track_counts = false;
206
207
208/* ----------
209 * state shared with pgstat_*.c
210 * ----------
211 */
212
214
215/*
216 * Track pending reports for fixed-numbered stats, used by
217 * pgstat_report_stat().
218 */
219bool pgstat_report_fixed = false;
220
221/* ----------
222 * Local data
223 *
224 * NB: There should be only variables related to stats infrastructure here,
225 * not for specific kinds of stats.
226 * ----------
227 */
228
229/*
230 * Memory contexts containing the pgStatEntryRefHash table, the
231 * pgStatSharedRef entries, and pending data respectively. Mostly to make it
232 * easier to track / attribute memory usage.
233 */
234
236
237/*
238 * Backend local list of PgStat_EntryRef with unflushed pending stats.
239 *
240 * Newly pending entries should only ever be added to the end of the list,
241 * otherwise pgstat_flush_pending_entries() might not see them immediately.
242 */
244
245
246/*
247 * Force the next stats flush to happen regardless of
248 * PGSTAT_MIN_INTERVAL. Useful in test scripts.
249 */
250static bool pgStatForceNextFlush = false;
251
252/*
253 * Force-clear existing snapshot before next use when stats_fetch_consistency
254 * is changed.
255 */
256static bool force_stats_snapshot_clear = false;
257
258
259/*
260 * For assertions that check pgstat is not used before initialization / after
261 * shutdown.
262 */
263#ifdef USE_ASSERT_CHECKING
264static bool pgstat_is_initialized = false;
265static bool pgstat_is_shutdown = false;
266#endif
267
268
269/*
270 * The different kinds of built-in statistics.
271 *
272 * If reasonably possible, handling specific to one kind of stats should go
273 * through this abstraction, rather than making more of pgstat.c aware.
274 *
275 * See comments for struct PgStat_KindInfo for details about the individual
276 * fields.
277 *
278 * XXX: It'd be nicer to define this outside of this file. But there doesn't
279 * seem to be a great way of doing that, given the split across multiple
280 * files.
281 */
283
284 /* stats kinds for variable-numbered objects */
285
287 .name = "database",
288
289 .fixed_amount = false,
290 .write_to_file = true,
291 /* so pg_stat_database entries can be seen in all databases */
292 .accessed_across_databases = true,
293
294 .shared_size = sizeof(PgStatShared_Database),
295 .shared_data_off = offsetof(PgStatShared_Database, stats),
296 .shared_data_len = sizeof(((PgStatShared_Database *) 0)->stats),
297 .pending_size = sizeof(PgStat_StatDBEntry),
298
299 .flush_pending_cb = pgstat_database_flush_cb,
300 .reset_timestamp_cb = pgstat_database_reset_timestamp_cb,
301 },
302
304 .name = "relation",
305
306 .fixed_amount = false,
307 .write_to_file = true,
308
309 .shared_size = sizeof(PgStatShared_Relation),
310 .shared_data_off = offsetof(PgStatShared_Relation, stats),
311 .shared_data_len = sizeof(((PgStatShared_Relation *) 0)->stats),
312 .pending_size = sizeof(PgStat_TableStatus),
313
314 .flush_pending_cb = pgstat_relation_flush_cb,
315 .delete_pending_cb = pgstat_relation_delete_pending_cb,
316 .reset_timestamp_cb = pgstat_relation_reset_timestamp_cb,
317 },
318
320 .name = "function",
321
322 .fixed_amount = false,
323 .write_to_file = true,
324
325 .shared_size = sizeof(PgStatShared_Function),
326 .shared_data_off = offsetof(PgStatShared_Function, stats),
327 .shared_data_len = sizeof(((PgStatShared_Function *) 0)->stats),
328 .pending_size = sizeof(PgStat_FunctionCounts),
329
330 .flush_pending_cb = pgstat_function_flush_cb,
331 .reset_timestamp_cb = pgstat_function_reset_timestamp_cb,
332 },
333
335 .name = "replslot",
336
337 .fixed_amount = false,
338 .write_to_file = true,
339
340 .accessed_across_databases = true,
341
342 .shared_size = sizeof(PgStatShared_ReplSlot),
343 .shared_data_off = offsetof(PgStatShared_ReplSlot, stats),
344 .shared_data_len = sizeof(((PgStatShared_ReplSlot *) 0)->stats),
345
346 .reset_timestamp_cb = pgstat_replslot_reset_timestamp_cb,
347 .to_serialized_name = pgstat_replslot_to_serialized_name_cb,
348 .from_serialized_name = pgstat_replslot_from_serialized_name_cb,
349 },
350
352 .name = "subscription",
353
354 .fixed_amount = false,
355 .write_to_file = true,
356 /* so pg_stat_subscription_stats entries can be seen in all databases */
357 .accessed_across_databases = true,
358
359 .shared_size = sizeof(PgStatShared_Subscription),
360 .shared_data_off = offsetof(PgStatShared_Subscription, stats),
361 .shared_data_len = sizeof(((PgStatShared_Subscription *) 0)->stats),
362 .pending_size = sizeof(PgStat_BackendSubEntry),
363
364 .flush_pending_cb = pgstat_subscription_flush_cb,
365 .reset_timestamp_cb = pgstat_subscription_reset_timestamp_cb,
366 },
367
369 .name = "backend",
370
371 .fixed_amount = false,
372 .write_to_file = false,
373
374 .accessed_across_databases = true,
375
376 .shared_size = sizeof(PgStatShared_Backend),
377 .shared_data_off = offsetof(PgStatShared_Backend, stats),
378 .shared_data_len = sizeof(((PgStatShared_Backend *) 0)->stats),
379
380 .flush_static_cb = pgstat_backend_flush_cb,
381 .reset_timestamp_cb = pgstat_backend_reset_timestamp_cb,
382 },
383
384 /* stats for fixed-numbered (mostly 1) objects */
385
387 .name = "archiver",
388
389 .fixed_amount = true,
390 .write_to_file = true,
391
392 .snapshot_ctl_off = offsetof(PgStat_Snapshot, archiver),
393 .shared_ctl_off = offsetof(PgStat_ShmemControl, archiver),
394 .shared_data_off = offsetof(PgStatShared_Archiver, stats),
395 .shared_data_len = sizeof(((PgStatShared_Archiver *) 0)->stats),
396
397 .init_shmem_cb = pgstat_archiver_init_shmem_cb,
398 .reset_all_cb = pgstat_archiver_reset_all_cb,
399 .snapshot_cb = pgstat_archiver_snapshot_cb,
400 },
401
403 .name = "bgwriter",
404
405 .fixed_amount = true,
406 .write_to_file = true,
407
408 .snapshot_ctl_off = offsetof(PgStat_Snapshot, bgwriter),
409 .shared_ctl_off = offsetof(PgStat_ShmemControl, bgwriter),
410 .shared_data_off = offsetof(PgStatShared_BgWriter, stats),
411 .shared_data_len = sizeof(((PgStatShared_BgWriter *) 0)->stats),
412
413 .init_shmem_cb = pgstat_bgwriter_init_shmem_cb,
414 .reset_all_cb = pgstat_bgwriter_reset_all_cb,
415 .snapshot_cb = pgstat_bgwriter_snapshot_cb,
416 },
417
419 .name = "checkpointer",
420
421 .fixed_amount = true,
422 .write_to_file = true,
423
424 .snapshot_ctl_off = offsetof(PgStat_Snapshot, checkpointer),
425 .shared_ctl_off = offsetof(PgStat_ShmemControl, checkpointer),
426 .shared_data_off = offsetof(PgStatShared_Checkpointer, stats),
427 .shared_data_len = sizeof(((PgStatShared_Checkpointer *) 0)->stats),
428
429 .init_shmem_cb = pgstat_checkpointer_init_shmem_cb,
430 .reset_all_cb = pgstat_checkpointer_reset_all_cb,
431 .snapshot_cb = pgstat_checkpointer_snapshot_cb,
432 },
433
434 [PGSTAT_KIND_IO] = {
435 .name = "io",
436
437 .fixed_amount = true,
438 .write_to_file = true,
439
440 .snapshot_ctl_off = offsetof(PgStat_Snapshot, io),
441 .shared_ctl_off = offsetof(PgStat_ShmemControl, io),
442 .shared_data_off = offsetof(PgStatShared_IO, stats),
443 .shared_data_len = sizeof(((PgStatShared_IO *) 0)->stats),
444
445 .flush_static_cb = pgstat_io_flush_cb,
446 .init_shmem_cb = pgstat_io_init_shmem_cb,
447 .reset_all_cb = pgstat_io_reset_all_cb,
448 .snapshot_cb = pgstat_io_snapshot_cb,
449 },
450
451 [PGSTAT_KIND_SLRU] = {
452 .name = "slru",
453
454 .fixed_amount = true,
455 .write_to_file = true,
456
457 .snapshot_ctl_off = offsetof(PgStat_Snapshot, slru),
458 .shared_ctl_off = offsetof(PgStat_ShmemControl, slru),
459 .shared_data_off = offsetof(PgStatShared_SLRU, stats),
460 .shared_data_len = sizeof(((PgStatShared_SLRU *) 0)->stats),
461
462 .flush_static_cb = pgstat_slru_flush_cb,
463 .init_shmem_cb = pgstat_slru_init_shmem_cb,
464 .reset_all_cb = pgstat_slru_reset_all_cb,
465 .snapshot_cb = pgstat_slru_snapshot_cb,
466 },
467
468 [PGSTAT_KIND_WAL] = {
469 .name = "wal",
470
471 .fixed_amount = true,
472 .write_to_file = true,
473
474 .snapshot_ctl_off = offsetof(PgStat_Snapshot, wal),
475 .shared_ctl_off = offsetof(PgStat_ShmemControl, wal),
476 .shared_data_off = offsetof(PgStatShared_Wal, stats),
477 .shared_data_len = sizeof(((PgStatShared_Wal *) 0)->stats),
478
479 .init_backend_cb = pgstat_wal_init_backend_cb,
480 .flush_static_cb = pgstat_wal_flush_cb,
481 .init_shmem_cb = pgstat_wal_init_shmem_cb,
482 .reset_all_cb = pgstat_wal_reset_all_cb,
483 .snapshot_cb = pgstat_wal_snapshot_cb,
484 },
485};
486
487/*
488 * Information about custom statistics kinds.
489 *
490 * These are saved in a different array than the built-in kinds to save
491 * in clarity with the initializations.
492 *
493 * Indexed by PGSTAT_KIND_CUSTOM_MIN, of size PGSTAT_KIND_CUSTOM_SIZE.
494 */
496
497/* ------------------------------------------------------------
498 * Functions managing the state of the stats system for all backends.
499 * ------------------------------------------------------------
500 */
501
502/*
503 * Read on-disk stats into memory at server start.
504 *
505 * Should only be called by the startup process or in single user mode.
506 */
507void
509{
511}
512
513/*
514 * Remove the stats file. This is currently used only if WAL recovery is
515 * needed after a crash.
516 *
517 * Should only be called by the startup process or in single user mode.
518 */
519void
521{
522 int ret;
523
524 /* NB: this needs to be done even in single user mode */
525
526 /* First, cleanup the main pgstats file */
528 if (ret != 0)
529 {
530 if (errno == ENOENT)
531 elog(DEBUG2,
532 "didn't need to unlink permanent stats file \"%s\" - didn't exist",
534 else
535 ereport(LOG,
537 errmsg("could not unlink permanent statistics file \"%s\": %m",
539 }
540 else
541 {
544 errmsg_internal("unlinked permanent statistics file \"%s\"",
546 }
547
548 /* Finish callbacks, if required */
549 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
550 {
552
553 if (kind_info && kind_info->finish)
554 kind_info->finish(STATS_DISCARD);
555 }
556
557 /*
558 * Reset stats contents. This will set reset timestamps of fixed-numbered
559 * stats to the current time (no variable stats exist).
560 */
562}
563
564/*
565 * pgstat_before_server_shutdown() needs to be called by exactly one process
566 * during regular server shutdowns. Otherwise all stats will be lost.
567 *
568 * We currently only write out stats for proc_exit(0). We might want to change
569 * that at some point... But right now pgstat_discard_stats() would be called
570 * during the start after a disorderly shutdown, anyway.
571 */
572void
574{
577
578 /*
579 * Stats should only be reported after pgstat_initialize() and before
580 * pgstat_shutdown(). This is a convenient point to catch most violations
581 * of this rule.
582 */
584
585 /* flush out our own pending changes before writing out */
586 pgstat_report_stat(true);
587
588 /*
589 * Only write out file during normal shutdown. Don't even signal that
590 * we've shutdown during irregular shutdowns, because the shutdown
591 * sequence isn't coordinated to ensure this backend shuts down last.
592 */
593 if (code == 0)
594 {
597 }
598}
599
600
601/* ------------------------------------------------------------
602 * Backend initialization / shutdown functions
603 * ------------------------------------------------------------
604 */
605
606/*
607 * Shut down a single backend's statistics reporting at process exit.
608 *
609 * Flush out any remaining statistics counts. Without this, operations
610 * triggered during backend exit (such as temp table deletions) won't be
611 * counted.
612 */
613static void
615{
618
619 /*
620 * If we got as far as discovering our own database ID, we can flush out
621 * what we did so far. Otherwise, we'd be reporting an invalid database
622 * ID, so forget it. (This means that accesses to pg_database during
623 * failed backend starts might never get counted.)
624 */
627
628 pgstat_report_stat(true);
629
630 /* there shouldn't be any pending changes left */
633
634 /* drop the backend stats entry */
637
639
640#ifdef USE_ASSERT_CHECKING
641 pgstat_is_shutdown = true;
642#endif
643}
644
645/*
646 * Initialize pgstats state, and set up our on-proc-exit hook. Called from
647 * BaseInit().
648 *
649 * NOTE: MyDatabaseId isn't set yet; so the shutdown hook has to be careful.
650 */
651void
653{
655
657
659
660 /* Backend initialization callbacks */
661 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
662 {
664
665 if (kind_info == NULL || kind_info->init_backend_cb == NULL)
666 continue;
667
668 kind_info->init_backend_cb();
669 }
670
671 /* Set up a process-exit hook to clean up */
673
674#ifdef USE_ASSERT_CHECKING
676#endif
677}
678
679
680/* ------------------------------------------------------------
681 * Public functions used by backends follow
682 * ------------------------------------------------------------
683 */
684
685/*
686 * Must be called by processes that performs DML: tcop/postgres.c, logical
687 * receiver processes, SPI worker, etc. to flush pending statistics updates to
688 * shared memory.
689 *
690 * Unless called with 'force', pending stats updates are flushed happen once
691 * per PGSTAT_MIN_INTERVAL (1000ms). When not forced, stats flushes do not
692 * block on lock acquisition, except if stats updates have been pending for
693 * longer than PGSTAT_MAX_INTERVAL (60000ms).
694 *
695 * Whenever pending stats updates remain at the end of pgstat_report_stat() a
696 * suggested idle timeout is returned. Currently this is always
697 * PGSTAT_IDLE_INTERVAL (10000ms). Callers can use the returned time to set up
698 * a timeout after which to call pgstat_report_stat(true), but are not
699 * required to do so.
700 *
701 * Note that this is called only when not within a transaction, so it is fair
702 * to use transaction stop time as an approximation of current time.
703 */
704long
705pgstat_report_stat(bool force)
706{
707 static TimestampTz pending_since = 0;
708 static TimestampTz last_flush = 0;
709 bool partial_flush;
711 bool nowait;
712
715
716 /* "absorb" the forced flush even if there's nothing to flush */
718 {
719 force = true;
720 pgStatForceNextFlush = false;
721 }
722
723 /* Don't expend a clock check if nothing to do */
726 {
727 return 0;
728 }
729
730 /*
731 * There should never be stats to report once stats are shut down. Can't
732 * assert that before the checks above, as there is an unconditional
733 * pgstat_report_stat() call in pgstat_shutdown_hook() - which at least
734 * the process that ran pgstat_before_server_shutdown() will still call.
735 */
737
738 if (force)
739 {
740 /*
741 * Stats reports are forced either when it's been too long since stats
742 * have been reported or in processes that force stats reporting to
743 * happen at specific points (including shutdown). In the former case
744 * the transaction stop time might be quite old, in the latter it
745 * would never get cleared.
746 */
748 }
749 else
750 {
752
753 if (pending_since > 0 &&
755 {
756 /* don't keep pending updates longer than PGSTAT_MAX_INTERVAL */
757 force = true;
758 }
759 else if (last_flush > 0 &&
761 {
762 /* don't flush too frequently */
763 if (pending_since == 0)
765
767 }
768 }
769
771
772 /* don't wait for lock acquisition when !force */
773 nowait = !force;
774
775 partial_flush = false;
776
777 /* flush of variable-numbered stats tracked in pending entries list */
779
780 /* flush of other stats kinds */
782 {
783 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
784 {
786
787 if (!kind_info)
788 continue;
789 if (!kind_info->flush_static_cb)
790 continue;
791
792 partial_flush |= kind_info->flush_static_cb(nowait);
793 }
794 }
795
796 last_flush = now;
797
798 /*
799 * If some of the pending stats could not be flushed due to lock
800 * contention, let the caller know when to retry.
801 */
802 if (partial_flush)
803 {
804 /* force should have prevented us from getting here */
805 Assert(!force);
806
807 /* remember since when stats have been pending */
808 if (pending_since == 0)
810
812 }
813
814 pending_since = 0;
815 pgstat_report_fixed = false;
816
817 return 0;
818}
819
820/*
821 * Force locally pending stats to be flushed during the next
822 * pgstat_report_stat() call. This is useful for writing tests.
823 */
824void
826{
828}
829
830/*
831 * Only for use by pgstat_reset_counters()
832 */
833static bool
835{
836 return entry->key.dboid == MyDatabaseId;
837}
838
839/*
840 * Reset counters for our database.
841 *
842 * Permission checking for this function is managed through the normal
843 * GRANT system.
844 */
845void
847{
849
852 ts);
853}
854
855/*
856 * Reset a single variable-numbered entry.
857 *
858 * If the stats kind is within a database, also reset the database's
859 * stat_reset_timestamp.
860 *
861 * Permission checking for this function is managed through the normal
862 * GRANT system.
863 */
864void
865pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
866{
869
870 /* not needed atm, and doesn't make sense with the current signature */
871 Assert(!pgstat_get_kind_info(kind)->fixed_amount);
872
873 /* reset the "single counter" */
874 pgstat_reset_entry(kind, dboid, objid, ts);
875
876 if (!kind_info->accessed_across_databases)
878}
879
880/*
881 * Reset stats for all entries of a kind.
882 *
883 * Permission checking for this function is managed through the normal
884 * GRANT system.
885 */
886void
888{
891
892 if (kind_info->fixed_amount)
893 kind_info->reset_all_cb(ts);
894 else
896}
897
898
899/* ------------------------------------------------------------
900 * Fetching of stats
901 * ------------------------------------------------------------
902 */
903
904/*
905 * Discard any data collected in the current transaction. Any subsequent
906 * request will cause new snapshots to be read.
907 *
908 * This is also invoked during transaction commit or abort to discard
909 * the no-longer-wanted snapshot. Updates of stats_fetch_consistency can
910 * cause this routine to be called.
911 */
912void
914{
916
923
924 /* Release memory, if any was allocated */
926 {
928
929 /* Reset variables */
931 }
932
933 /*
934 * Historically the backend_status.c facilities lived in this file, and
935 * were reset with the same function. For now keep it that way, and
936 * forward the reset request.
937 */
939
940 /* Reset this flag, as it may be possible that a cleanup was forced. */
942}
943
944void *
945pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
946{
947 PgStat_HashKey key = {0};
948 PgStat_EntryRef *entry_ref;
949 void *stats_data;
951
952 /* should be called from backends */
954 Assert(!kind_info->fixed_amount);
955
957
958 key.kind = kind;
959 key.dboid = dboid;
960 key.objid = objid;
961
962 /* if we need to build a full snapshot, do so */
965
966 /* if caching is desired, look up in cache */
968 {
969 PgStat_SnapshotEntry *entry = NULL;
970
972
973 if (entry)
974 return entry->data;
975
976 /*
977 * If we built a full snapshot and the key is not in
978 * pgStatLocal.snapshot.stats, there are no matching stats.
979 */
981 return NULL;
982 }
983
985
986 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
987
988 if (entry_ref == NULL || entry_ref->shared_entry->dropped)
989 {
990 /* create empty entry when using PGSTAT_FETCH_CONSISTENCY_CACHE */
992 {
993 PgStat_SnapshotEntry *entry = NULL;
994 bool found;
995
996 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
997 Assert(!found);
998 entry->data = NULL;
999 }
1000 return NULL;
1001 }
1002
1003 /*
1004 * Allocate in caller's context for PGSTAT_FETCH_CONSISTENCY_NONE,
1005 * otherwise we could quickly end up with a fair bit of memory used due to
1006 * repeated accesses.
1007 */
1009 stats_data = palloc(kind_info->shared_data_len);
1010 else
1012 kind_info->shared_data_len);
1013
1014 (void) pgstat_lock_entry_shared(entry_ref, false);
1016 pgstat_get_entry_data(kind, entry_ref->shared_stats),
1017 kind_info->shared_data_len);
1018 pgstat_unlock_entry(entry_ref);
1019
1021 {
1022 PgStat_SnapshotEntry *entry = NULL;
1023 bool found;
1024
1025 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
1026 entry->data = stats_data;
1027 }
1028
1029 return stats_data;
1030}
1031
1032/*
1033 * If a stats snapshot has been taken, return the timestamp at which that was
1034 * done, and set *have_snapshot to true. Otherwise *have_snapshot is set to
1035 * false.
1036 */
1039{
1042
1044 {
1045 *have_snapshot = true;
1047 }
1048
1049 *have_snapshot = false;
1050
1051 return 0;
1052}
1053
1054bool
1055pgstat_have_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
1056{
1057 /* fixed-numbered stats always exist */
1058 if (pgstat_get_kind_info(kind)->fixed_amount)
1059 return true;
1060
1061 return pgstat_get_entry_ref(kind, dboid, objid, false, NULL) != NULL;
1062}
1063
1064/*
1065 * Ensure snapshot for fixed-numbered 'kind' exists.
1066 *
1067 * Typically used by the pgstat_fetch_* functions for a kind of stats, before
1068 * massaging the data into the desired format.
1069 */
1070void
1072{
1074 Assert(pgstat_get_kind_info(kind)->fixed_amount);
1075
1078
1081 else
1083
1084 if (pgstat_is_kind_builtin(kind))
1086 else if (pgstat_is_kind_custom(kind))
1088}
1089
1090static void
1092{
1093 /*
1094 * Initialize fixed-numbered statistics data in snapshots, only for custom
1095 * stats kinds.
1096 */
1097 for (PgStat_Kind kind = PGSTAT_KIND_CUSTOM_MIN; kind <= PGSTAT_KIND_CUSTOM_MAX; kind++)
1098 {
1100
1101 if (!kind_info || !kind_info->fixed_amount)
1102 continue;
1103
1106 }
1107}
1108
1109static void
1111{
1114
1117 return;
1118
1121 "PgStat Snapshot",
1123
1127 NULL);
1128}
1129
1130static void
1132{
1135
1136 /* should only be called when we need a snapshot */
1138
1139 /* snapshot already built */
1141 return;
1142
1144
1145 Assert(pgStatLocal.snapshot.stats->members == 0);
1146
1148
1149 /*
1150 * Snapshot all variable stats.
1151 */
1153 while ((p = dshash_seq_next(&hstat)) != NULL)
1154 {
1155 PgStat_Kind kind = p->key.kind;
1157 bool found;
1158 PgStat_SnapshotEntry *entry;
1160
1161 /*
1162 * Check if the stats object should be included in the snapshot.
1163 * Unless the stats kind can be accessed from all databases (e.g.,
1164 * database stats themselves), we only include stats for the current
1165 * database or objects not associated with a database (e.g. shared
1166 * relations).
1167 */
1168 if (p->key.dboid != MyDatabaseId &&
1169 p->key.dboid != InvalidOid &&
1170 !kind_info->accessed_across_databases)
1171 continue;
1172
1173 if (p->dropped)
1174 continue;
1175
1177
1180
1182 Assert(!found);
1183
1185 pgstat_get_entry_len(kind));
1186
1187 /*
1188 * Acquire the LWLock directly instead of using
1189 * pg_stat_lock_entry_shared() which requires a reference.
1190 */
1192 memcpy(entry->data,
1194 pgstat_get_entry_len(kind));
1195 LWLockRelease(&stats_data->lock);
1196 }
1198
1199 /*
1200 * Build snapshot of all fixed-numbered stats.
1201 */
1202 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
1203 {
1205
1206 if (!kind_info)
1207 continue;
1208 if (!kind_info->fixed_amount)
1209 {
1210 Assert(kind_info->snapshot_cb == NULL);
1211 continue;
1212 }
1213
1215 }
1216
1218}
1219
1220static void
1222{
1224 int idx;
1225 bool *valid;
1226
1227 /* Position in fixed_valid or custom_valid */
1228 if (pgstat_is_kind_builtin(kind))
1229 {
1230 idx = kind;
1232 }
1233 else
1234 {
1235 idx = kind - PGSTAT_KIND_CUSTOM_MIN;
1237 }
1238
1239 Assert(kind_info->fixed_amount);
1240 Assert(kind_info->snapshot_cb != NULL);
1241
1243 {
1244 /* rebuild every time */
1245 valid[idx] = false;
1246 }
1247 else if (valid[idx])
1248 {
1249 /* in snapshot mode we shouldn't get called again */
1251 return;
1252 }
1253
1254 Assert(!valid[idx]);
1255
1256 kind_info->snapshot_cb();
1257
1258 Assert(!valid[idx]);
1259 valid[idx] = true;
1260}
1261
1262
1263/* ------------------------------------------------------------
1264 * Backend-local pending stats infrastructure
1265 * ------------------------------------------------------------
1266 */
1267
1268/*
1269 * Returns the appropriate PgStat_EntryRef, preparing it to receive pending
1270 * stats if not already done.
1271 *
1272 * If created_entry is non-NULL, it'll be set to true if the entry is newly
1273 * created, false otherwise.
1274 */
1277{
1278 PgStat_EntryRef *entry_ref;
1279
1280 /* need to be able to flush out */
1281 Assert(pgstat_get_kind_info(kind)->flush_pending_cb != NULL);
1282
1284 {
1287 "PgStat Pending",
1289 }
1290
1291 entry_ref = pgstat_get_entry_ref(kind, dboid, objid,
1292 true, created_entry);
1293
1294 if (entry_ref->pending == NULL)
1295 {
1296 size_t entrysize = pgstat_get_kind_info(kind)->pending_size;
1297
1298 Assert(entrysize != (size_t) -1);
1299
1300 entry_ref->pending = MemoryContextAllocZero(pgStatPendingContext, entrysize);
1302 }
1303
1304 return entry_ref;
1305}
1306
1307/*
1308 * Return an existing stats entry, or NULL.
1309 *
1310 * This should only be used for helper function for pgstatfuncs.c - outside of
1311 * that it shouldn't be needed.
1312 */
1315{
1316 PgStat_EntryRef *entry_ref;
1317
1318 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
1319
1320 if (entry_ref == NULL || entry_ref->pending == NULL)
1321 return NULL;
1322
1323 return entry_ref;
1324}
1325
1326void
1328{
1329 PgStat_Kind kind = entry_ref->shared_entry->key.kind;
1331 void *pending_data = entry_ref->pending;
1332
1334 /* !fixed_amount stats should be handled explicitly */
1335 Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1336
1337 if (kind_info->delete_pending_cb)
1338 kind_info->delete_pending_cb(entry_ref);
1339
1341 entry_ref->pending = NULL;
1342
1343 dlist_delete(&entry_ref->pending_node);
1344}
1345
1346/*
1347 * Flush out pending variable-numbered stats.
1348 */
1349static bool
1351{
1352 bool have_pending = false;
1353 dlist_node *cur = NULL;
1354
1355 /*
1356 * Need to be a bit careful iterating over the list of pending entries.
1357 * Processing a pending entry may queue further pending entries to the end
1358 * of the list that we want to process, so a simple iteration won't do.
1359 * Further complicating matters is that we want to delete the current
1360 * entry in each iteration from the list if we flushed successfully.
1361 *
1362 * So we just keep track of the next pointer in each loop iteration.
1363 */
1366
1367 while (cur)
1368 {
1369 PgStat_EntryRef *entry_ref =
1370 dlist_container(PgStat_EntryRef, pending_node, cur);
1371 PgStat_HashKey key = entry_ref->shared_entry->key;
1372 PgStat_Kind kind = key.kind;
1374 bool did_flush;
1376
1377 Assert(!kind_info->fixed_amount);
1378 Assert(kind_info->flush_pending_cb != NULL);
1379
1380 /* flush the stats, if possible */
1381 did_flush = kind_info->flush_pending_cb(entry_ref, nowait);
1382
1383 Assert(did_flush || nowait);
1384
1385 /* determine next entry, before deleting the pending entry */
1388 else
1389 next = NULL;
1390
1391 /* if successfully flushed, remove entry */
1392 if (did_flush)
1393 pgstat_delete_pending_entry(entry_ref);
1394 else
1395 have_pending = true;
1396
1397 cur = next;
1398 }
1399
1401
1402 return have_pending;
1403}
1404
1405
1406/* ------------------------------------------------------------
1407 * Helper / infrastructure functions
1408 * ------------------------------------------------------------
1409 */
1410
1413{
1414 for (PgStat_Kind kind = PGSTAT_KIND_BUILTIN_MIN; kind <= PGSTAT_KIND_BUILTIN_MAX; kind++)
1415 {
1417 return kind;
1418 }
1419
1420 /* Check the custom set of cumulative stats */
1422 {
1423 for (PgStat_Kind kind = PGSTAT_KIND_CUSTOM_MIN; kind <= PGSTAT_KIND_CUSTOM_MAX; kind++)
1424 {
1426
1429 return kind;
1430 }
1431 }
1432
1433 ereport(ERROR,
1435 errmsg("invalid statistics kind: \"%s\"", kind_str)));
1436 return PGSTAT_KIND_INVALID; /* avoid compiler warnings */
1437}
1438
1439static inline bool
1441{
1442 return pgstat_is_kind_builtin(kind) || pgstat_is_kind_custom(kind);
1443}
1444
1445const PgStat_KindInfo *
1447{
1448 if (pgstat_is_kind_builtin(kind))
1449 return &pgstat_kind_builtin_infos[kind];
1450
1451 if (pgstat_is_kind_custom(kind))
1452 {
1454
1457 return NULL;
1459 }
1460
1461 return NULL;
1462}
1463
1464/*
1465 * Register a new stats kind.
1466 *
1467 * PgStat_Kinds must be globally unique across all extensions. Refer
1468 * to https://wiki.postgresql.org/wiki/CustomCumulativeStats to reserve a
1469 * unique ID for your extension, to avoid conflicts with other extension
1470 * developers. During development, use PGSTAT_KIND_EXPERIMENTAL to avoid
1471 * needlessly reserving a new ID.
1472 */
1473void
1475{
1477
1478 if (kind_info->name == NULL || strlen(kind_info->name) == 0)
1479 ereport(ERROR,
1480 (errmsg("custom cumulative statistics name is invalid"),
1481 errhint("Provide a non-empty name for the custom cumulative statistics.")));
1482
1483 if (!pgstat_is_kind_custom(kind))
1484 ereport(ERROR, (errmsg("custom cumulative statistics ID %u is out of range", kind),
1485 errhint("Provide a custom cumulative statistics ID between %u and %u.",
1487
1489 ereport(ERROR,
1490 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1491 errdetail("Custom cumulative statistics must be registered while initializing modules in \"shared_preload_libraries\".")));
1492
1493 /*
1494 * Check some data for fixed-numbered stats.
1495 */
1496 if (kind_info->fixed_amount)
1497 {
1498 if (kind_info->shared_size == 0)
1499 ereport(ERROR,
1500 (errmsg("custom cumulative statistics property is invalid"),
1501 errhint("Custom cumulative statistics require a shared memory size for fixed-numbered objects.")));
1502 if (kind_info->track_entry_count)
1503 ereport(ERROR,
1504 (errmsg("custom cumulative statistics property is invalid"),
1505 errhint("Custom cumulative statistics cannot use entry count tracking for fixed-numbered objects.")));
1506 }
1507
1508 /*
1509 * If pgstat_kind_custom_infos is not available yet, allocate it.
1510 */
1512 {
1516 }
1517
1520 ereport(ERROR,
1521 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1522 errdetail("Custom cumulative statistics \"%s\" already registered with the same ID.",
1524
1525 /* check for existing custom stats with the same name */
1527 {
1529
1531 continue;
1533 ereport(ERROR,
1534 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1535 errdetail("Existing cumulative statistics with ID %u has the same name.", existing_kind)));
1536 }
1537
1538 /* Register it */
1540 ereport(LOG,
1541 (errmsg("registered custom cumulative statistics \"%s\" with ID %u",
1542 kind_info->name, kind)));
1543}
1544
1545/*
1546 * Stats should only be reported after pgstat_initialize() and before
1547 * pgstat_shutdown(). This check is put in a few central places to catch
1548 * violations of this rule more easily.
1549 */
1550#ifdef USE_ASSERT_CHECKING
1551void
1553{
1555}
1556#endif
1557
1558
1559/* ------------------------------------------------------------
1560 * reading and writing of on-disk stats file
1561 * ------------------------------------------------------------
1562 */
1563
1564/* helper for pgstat_write_statsfile() */
1565void
1566pgstat_write_chunk(FILE *fpout, void *ptr, size_t len)
1567{
1568 int rc;
1569
1570 rc = fwrite(ptr, len, 1, fpout);
1571
1572 /* We check for errors with ferror() when done writing the stats. */
1573 (void) rc;
1574}
1575
1576/*
1577 * This function is called in the last process that is accessing the shared
1578 * stats so locking is not required.
1579 */
1580static void
1582{
1583 FILE *fpout;
1589
1591
1592 /* should be called only by the checkpointer or single user mode */
1594
1595 /* we're shutting down, so it's ok to just override this */
1597
1598 elog(DEBUG2, "writing stats file \"%s\"", statfile);
1599
1600 /*
1601 * Open the statistics temp file to write out the current values.
1602 */
1604 if (fpout == NULL)
1605 {
1606 ereport(LOG,
1608 errmsg("could not open temporary statistics file \"%s\": %m",
1609 tmpfile)));
1610 return;
1611 }
1612
1613 /*
1614 * Write the file header --- currently just a format ID.
1615 */
1618
1619 /* Write various stats structs for fixed number of objects */
1620 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
1621 {
1622 char *ptr;
1623 const PgStat_KindInfo *info = pgstat_get_kind_info(kind);
1624
1625 if (!info || !info->fixed_amount)
1626 continue;
1627
1628 if (pgstat_is_kind_builtin(kind))
1629 Assert(info->snapshot_ctl_off != 0);
1630
1631 /* skip if no need to write to file */
1632 if (!info->write_to_file)
1633 continue;
1634
1636 if (pgstat_is_kind_builtin(kind))
1637 ptr = ((char *) &pgStatLocal.snapshot) + info->snapshot_ctl_off;
1638 else
1640
1644 }
1645
1646 /*
1647 * Walk through the stats entries
1648 */
1650 while ((ps = dshash_seq_next(&hstat)) != NULL)
1651 {
1654
1656
1657 /*
1658 * We should not see any "dropped" entries when writing the stats
1659 * file, as all backends and auxiliary processes should have cleaned
1660 * up their references before they terminated.
1661 *
1662 * However, since we are already shutting down, it is not worth
1663 * crashing the server over any potential cleanup issues, so we simply
1664 * skip such entries if encountered.
1665 */
1666 Assert(!ps->dropped);
1667 if (ps->dropped)
1668 continue;
1669
1670 /*
1671 * This discards data related to custom stats kinds that are unknown
1672 * to this process.
1673 */
1674 if (!pgstat_is_kind_valid(ps->key.kind))
1675 {
1676 elog(WARNING, "found unknown stats entry %u/%u/%" PRIu64,
1677 ps->key.kind, ps->key.dboid,
1678 ps->key.objid);
1679 continue;
1680 }
1681
1683
1684 kind_info = pgstat_get_kind_info(ps->key.kind);
1685
1686 /* if not dropped the valid-entry refcount should exist */
1687 Assert(pg_atomic_read_u32(&ps->refcount) > 0);
1688
1689 /* skip if no need to write to file */
1690 if (!kind_info->write_to_file)
1691 continue;
1692
1693 if (!kind_info->to_serialized_name)
1694 {
1695 /* normal stats entry, identified by PgStat_HashKey */
1698 }
1699 else
1700 {
1701 /* stats entry identified by name on disk (e.g. slots) */
1702 NameData name;
1703
1704 kind_info->to_serialized_name(&ps->key, shstats, &name);
1705
1707 pgstat_write_chunk_s(fpout, &ps->key.kind);
1709 }
1710
1711 /* Write except the header part of the entry */
1713 pgstat_get_entry_data(ps->key.kind, shstats),
1714 pgstat_get_entry_len(ps->key.kind));
1715
1716 /* Write more data for the entry, if required */
1717 if (kind_info->to_serialized_data)
1718 kind_info->to_serialized_data(&ps->key, shstats, fpout);
1719 }
1721
1722 /*
1723 * No more output to be done. Close the temp file and replace the old
1724 * pgstat.stat with it. The ferror() check replaces testing for error
1725 * after each individual fputc or fwrite (in pgstat_write_chunk()) above.
1726 */
1728
1729 if (ferror(fpout))
1730 {
1731 ereport(LOG,
1733 errmsg("could not write temporary statistics file \"%s\": %m",
1734 tmpfile)));
1735 FreeFile(fpout);
1736 unlink(tmpfile);
1737 }
1738 else if (FreeFile(fpout) < 0)
1739 {
1740 ereport(LOG,
1742 errmsg("could not close temporary statistics file \"%s\": %m",
1743 tmpfile)));
1744 unlink(tmpfile);
1745 }
1746 else if (durable_rename(tmpfile, statfile, LOG) < 0)
1747 {
1748 /* durable_rename already emitted log message */
1749 unlink(tmpfile);
1750 }
1751
1752 /* Finish callbacks, if required */
1753 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
1754 {
1756
1757 if (kind_info && kind_info->finish)
1758 kind_info->finish(STATS_WRITE);
1759 }
1760}
1761
1762/* helper for pgstat_read_statsfile() */
1763bool
1764pgstat_read_chunk(FILE *fpin, void *ptr, size_t len)
1765{
1766 return fread(ptr, 1, len, fpin) == len;
1767}
1768
1769/*
1770 * Reads in existing statistics file into memory.
1771 *
1772 * This function is called in the only process that is accessing the shared
1773 * stats so locking is not required.
1774 */
1775static void
1777{
1778 FILE *fpin;
1780 bool found;
1783
1784 /* shouldn't be called from postmaster */
1786
1787 elog(DEBUG2, "reading stats file \"%s\"", statfile);
1788
1789 /*
1790 * Try to open the stats file. If it doesn't exist, the backends simply
1791 * returns zero for anything and statistics simply starts from scratch
1792 * with empty counters.
1793 *
1794 * ENOENT is a possibility if stats collection was previously disabled or
1795 * has not yet written the stats file for the first time. Any other
1796 * failure condition is suspicious.
1797 */
1799 {
1800 if (errno != ENOENT)
1801 ereport(LOG,
1803 errmsg("could not open statistics file \"%s\": %m",
1804 statfile)));
1806 return;
1807 }
1808
1809 /*
1810 * Verify it's of the expected format.
1811 */
1813 {
1814 elog(WARNING, "could not read format ID");
1815 goto error;
1816 }
1817
1819 {
1820 elog(WARNING, "found incorrect format ID %d (expected %d)",
1822 goto error;
1823 }
1824
1825 /*
1826 * We found an existing statistics file. Read it and put all the stats
1827 * data into place.
1828 */
1829 for (;;)
1830 {
1831 int t = fgetc(fpin);
1832
1833 switch (t)
1834 {
1836 {
1837 PgStat_Kind kind;
1838 const PgStat_KindInfo *info;
1839 char *ptr;
1840
1841 /* entry for fixed-numbered stats */
1842 if (!pgstat_read_chunk_s(fpin, &kind))
1843 {
1844 elog(WARNING, "could not read stats kind for entry of type %c", t);
1845 goto error;
1846 }
1847
1848 if (!pgstat_is_kind_valid(kind))
1849 {
1850 elog(WARNING, "invalid stats kind %u for entry of type %c",
1851 kind, t);
1852 goto error;
1853 }
1854
1855 info = pgstat_get_kind_info(kind);
1856 if (!info)
1857 {
1858 elog(WARNING, "could not find information of kind %u for entry of type %c",
1859 kind, t);
1860 goto error;
1861 }
1862
1863 if (!info->fixed_amount)
1864 {
1865 elog(WARNING, "invalid fixed_amount in stats kind %u for entry of type %c",
1866 kind, t);
1867 goto error;
1868 }
1869
1870 /* Load back stats into shared memory */
1871 if (pgstat_is_kind_builtin(kind))
1872 ptr = ((char *) shmem) + info->shared_ctl_off +
1873 info->shared_data_off;
1874 else
1875 {
1876 int idx = kind - PGSTAT_KIND_CUSTOM_MIN;
1877
1878 ptr = ((char *) shmem->custom_data[idx]) +
1879 info->shared_data_off;
1880 }
1881
1882 if (!pgstat_read_chunk(fpin, ptr, info->shared_data_len))
1883 {
1884 elog(WARNING, "could not read data of stats kind %u for entry of type %c with size %u",
1885 kind, t, info->shared_data_len);
1886 goto error;
1887 }
1888
1889 break;
1890 }
1893 {
1896 PgStatShared_Common *header;
1898
1900
1901 if (t == PGSTAT_FILE_ENTRY_HASH)
1902 {
1903 /* normal stats entry, identified by PgStat_HashKey */
1904 if (!pgstat_read_chunk_s(fpin, &key))
1905 {
1906 elog(WARNING, "could not read key for entry of type %c", t);
1907 goto error;
1908 }
1909
1910 if (!pgstat_is_kind_valid(key.kind))
1911 {
1912 elog(WARNING, "invalid stats kind for entry %u/%u/%" PRIu64 " of type %c",
1913 key.kind, key.dboid,
1914 key.objid, t);
1915 goto error;
1916 }
1917
1919 if (!kind_info)
1920 {
1921 elog(WARNING, "could not find information of kind for entry %u/%u/%" PRIu64 " of type %c",
1922 key.kind, key.dboid,
1923 key.objid, t);
1924 goto error;
1925 }
1926 }
1927 else
1928 {
1929 /* stats entry identified by name on disk (e.g. slots) */
1930 PgStat_Kind kind;
1931 NameData name;
1932
1933 if (!pgstat_read_chunk_s(fpin, &kind))
1934 {
1935 elog(WARNING, "could not read stats kind for entry of type %c", t);
1936 goto error;
1937 }
1939 {
1940 elog(WARNING, "could not read name of stats kind %u for entry of type %c",
1941 kind, t);
1942 goto error;
1943 }
1944 if (!pgstat_is_kind_valid(kind))
1945 {
1946 elog(WARNING, "invalid stats kind %u for entry of type %c",
1947 kind, t);
1948 goto error;
1949 }
1950
1952 if (!kind_info)
1953 {
1954 elog(WARNING, "could not find information of kind %u for entry of type %c",
1955 kind, t);
1956 goto error;
1957 }
1958
1959 if (!kind_info->from_serialized_name)
1960 {
1961 elog(WARNING, "invalid from_serialized_name in stats kind %u for entry of type %c",
1962 kind, t);
1963 goto error;
1964 }
1965
1966 if (!kind_info->from_serialized_name(&name, &key))
1967 {
1968 /* skip over data for entry we don't care about */
1969 if (fseek(fpin, pgstat_get_entry_len(kind), SEEK_CUR) != 0)
1970 {
1971 elog(WARNING, "could not seek \"%s\" of stats kind %u for entry of type %c",
1972 NameStr(name), kind, t);
1973 goto error;
1974 }
1975
1976 continue;
1977 }
1978
1979 Assert(key.kind == kind);
1980 }
1981
1982 /*
1983 * This intentionally doesn't use pgstat_get_entry_ref() -
1984 * putting all stats into checkpointer's
1985 * pgStatEntryRefHash would be wasted effort and memory.
1986 */
1988
1989 /* don't allow duplicate entries */
1990 if (found)
1991 {
1993 elog(WARNING, "found duplicate stats entry %u/%u/%" PRIu64 " of type %c",
1994 key.kind, key.dboid,
1995 key.objid, t);
1996 goto error;
1997 }
1998
1999 header = pgstat_init_entry(key.kind, p);
2001 if (header == NULL)
2002 {
2003 /*
2004 * It would be tempting to switch this ERROR to a
2005 * WARNING, but it would mean that all the statistics
2006 * are discarded when the environment fails on OOM.
2007 */
2008 elog(ERROR, "could not allocate entry %u/%u/%" PRIu64 " of type %c",
2009 key.kind, key.dboid,
2010 key.objid, t);
2011 }
2012
2014 pgstat_get_entry_data(key.kind, header),
2016 {
2017 elog(WARNING, "could not read data for entry %u/%u/%" PRIu64 " of type %c",
2018 key.kind, key.dboid,
2019 key.objid, t);
2020 goto error;
2021 }
2022
2023 /* read more data for the entry, if required */
2024 if (kind_info->from_serialized_data)
2025 {
2026 if (!kind_info->from_serialized_data(&key, header, fpin))
2027 {
2028 elog(WARNING, "could not read auxiliary data for entry %u/%u/%" PRIu64 " of type %c",
2029 key.kind, key.dboid,
2030 key.objid, t);
2031 goto error;
2032 }
2033 }
2034
2035 break;
2036 }
2038
2039 /*
2040 * check that PGSTAT_FILE_ENTRY_END actually signals end of
2041 * file
2042 */
2043 if (fgetc(fpin) != EOF)
2044 {
2045 elog(WARNING, "could not read end-of-file");
2046 goto error;
2047 }
2048
2049 goto done;
2050
2051 default:
2052 elog(WARNING, "could not read entry of type %c", t);
2053 goto error;
2054 }
2055 }
2056
2057done:
2058 /* First, cleanup the main stats file */
2059 FreeFile(fpin);
2060
2061 elog(DEBUG2, "removing permanent stats file \"%s\"", statfile);
2063
2064 /* Finish callbacks, if required */
2065 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
2066 {
2068
2069 if (kind_info && kind_info->finish)
2070 kind_info->finish(STATS_READ);
2071 }
2072
2073 return;
2074
2075error:
2076 ereport(LOG,
2077 (errmsg("corrupted statistics file \"%s\"", statfile)));
2078
2080
2081 goto done;
2082}
2083
2084/*
2085 * Helper to reset / drop stats after a crash or after restoring stats from
2086 * disk failed, potentially after already loading parts.
2087 */
2088static void
2090{
2092
2093 /* reset fixed-numbered stats */
2094 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
2095 {
2097
2098 if (!kind_info || !kind_info->fixed_amount)
2099 continue;
2100
2101 kind_info->reset_all_cb(ts);
2102 }
2103
2104 /* and drop variable-numbered ones */
2106}
2107
2108/*
2109 * GUC assign_hook for stats_fetch_consistency.
2110 */
2111void
2112assign_stats_fetch_consistency(int newval, void *extra)
2113{
2114 /*
2115 * Changing this value in a transaction may cause snapshot state
2116 * inconsistencies, so force a clear of the current snapshot on the next
2117 * snapshot build attempt.
2118 */
2121}
Datum idx(PG_FUNCTION_ARGS)
Definition _int_op.c:262
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition atomics.h:237
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition timestamp.c:1781
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1645
Datum now(PG_FUNCTION_ARGS)
Definition timestamp.c:1609
void pgstat_clear_backend_activity_snapshot(void)
static int32 next
Definition blutils.c:225
#define NameStr(name)
Definition c.h:775
#define PG_BINARY_R
Definition c.h:1283
#define Assert(condition)
Definition c.h:883
int32_t int32
Definition c.h:552
uint64_t uint64
Definition c.h:557
#define unlikely(x)
Definition c.h:422
uint32_t uint32
Definition c.h:556
#define PG_BINARY_W
Definition c.h:1284
#define OidIsValid(objectId)
Definition c.h:798
int64 TimestampTz
Definition timestamp.h:39
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition dsa.c:957
void dshash_release_lock(dshash_table *hash_table, void *entry)
Definition dshash.c:560
void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table, bool exclusive)
Definition dshash.c:640
void dshash_seq_term(dshash_seq_status *status)
Definition dshash.c:749
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
Definition dshash.c:435
void * dshash_seq_next(dshash_seq_status *status)
Definition dshash.c:659
struct cursor * cur
Definition ecpg.c:29
int errmsg_internal(const char *fmt,...)
Definition elog.c:1170
int errcode_for_file_access(void)
Definition elog.c:886
int errdetail(const char *fmt,...)
Definition elog.c:1216
int errhint(const char *fmt,...)
Definition elog.c:1330
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define LOG
Definition elog.h:31
#define WARNING
Definition elog.h:36
#define DEBUG2
Definition elog.h:29
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition fd.c:779
int FreeFile(FILE *file)
Definition fd.c:2823
FILE * AllocateFile(const char *name, const char *mode)
Definition fd.c:2624
ProcNumber MyProcNumber
Definition globals.c:90
bool IsUnderPostmaster
Definition globals.c:120
bool IsPostmasterEnvironment
Definition globals.c:119
Oid MyDatabaseId
Definition globals.c:94
#define newval
static void dlist_init(dlist_head *head)
Definition ilist.h:314
static bool dlist_has_next(const dlist_head *head, const dlist_node *node)
Definition ilist.h:503
static dlist_node * dlist_next_node(dlist_head *head, dlist_node *node)
Definition ilist.h:537
static void dlist_delete(dlist_node *node)
Definition ilist.h:405
static dlist_node * dlist_head_node(dlist_head *head)
Definition ilist.h:565
static bool dlist_is_empty(const dlist_head *head)
Definition ilist.h:336
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition ilist.h:364
#define DLIST_STATIC_INIT(name)
Definition ilist.h:281
#define dlist_container(type, membername, ptr)
Definition ilist.h:593
struct parser_state ps
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:344
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1176
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1793
@ LW_SHARED
Definition lwlock.h:113
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition mcxt.c:1266
void pfree(void *pointer)
Definition mcxt.c:1616
MemoryContext TopMemoryContext
Definition mcxt.c:166
void * palloc(Size size)
Definition mcxt.c:1387
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:472
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition memutils.h:170
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
@ B_CHECKPOINTER
Definition miscadmin.h:363
BackendType MyBackendType
Definition miscinit.c:64
bool process_shared_preload_libraries_in_progress
Definition miscinit.c:1786
void * arg
const void size_t len
const void * data
void pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition pgstat.c:864
static bool pgStatForceNextFlush
Definition pgstat.c:249
int pgstat_fetch_consistency
Definition pgstat.c:204
#define PGSTAT_MIN_INTERVAL
Definition pgstat.c:126
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition pgstat.c:1070
static void pgstat_prep_snapshot(void)
Definition pgstat.c:1109
static const PgStat_KindInfo ** pgstat_kind_custom_infos
Definition pgstat.c:494
static void pgstat_build_snapshot_fixed(PgStat_Kind kind)
Definition pgstat.c:1220
#define PGSTAT_SNAPSHOT_HASH_SIZE
Definition pgstat.c:137
#define PGSTAT_FILE_ENTRY_END
Definition pgstat.c:143
void pgstat_delete_pending_entry(PgStat_EntryRef *entry_ref)
Definition pgstat.c:1326
void pgstat_reset_counters(void)
Definition pgstat.c:845
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *created_entry)
Definition pgstat.c:1275
static void pgstat_build_snapshot(void)
Definition pgstat.c:1130
void pgstat_initialize(void)
Definition pgstat.c:651
bool pgstat_track_counts
Definition pgstat.c:203
static void pgstat_write_statsfile(void)
Definition pgstat.c:1580
long pgstat_report_stat(bool force)
Definition pgstat.c:704
static void pgstat_read_statsfile(void)
Definition pgstat.c:1775
static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
Definition pgstat.c:281
void pgstat_reset_of_kind(PgStat_Kind kind)
Definition pgstat.c:886
static bool pgstat_flush_pending_entries(bool nowait)
Definition pgstat.c:1349
bool pgstat_report_fixed
Definition pgstat.c:218
void pgstat_before_server_shutdown(int code, Datum arg)
Definition pgstat.c:572
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition pgstat.c:1445
static void pgstat_reset_after_failure(void)
Definition pgstat.c:2088
void assign_stats_fetch_consistency(int newval, void *extra)
Definition pgstat.c:2111
void pgstat_force_next_flush(void)
Definition pgstat.c:824
static bool pgstat_is_kind_valid(PgStat_Kind kind)
Definition pgstat.c:1439
void pgstat_clear_snapshot(void)
Definition pgstat.c:912
bool pgstat_read_chunk(FILE *fpin, void *ptr, size_t len)
Definition pgstat.c:1763
TimestampTz pgstat_get_stat_snapshot_timestamp(bool *have_snapshot)
Definition pgstat.c:1037
bool pgstat_have_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition pgstat.c:1054
static MemoryContext pgStatPendingContext
Definition pgstat.c:234
static dlist_head pgStatPending
Definition pgstat.c:242
#define PGSTAT_FILE_ENTRY_HASH
Definition pgstat.c:146
static void pgstat_init_snapshot_fixed(void)
Definition pgstat.c:1090
PgStat_EntryRef * pgstat_fetch_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition pgstat.c:1313
void pgstat_restore_stats(void)
Definition pgstat.c:507
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition pgstat.c:944
#define PGSTAT_MAX_INTERVAL
Definition pgstat.c:128
#define PGSTAT_FILE_ENTRY_FIXED
Definition pgstat.c:144
#define PGSTAT_IDLE_INTERVAL
Definition pgstat.c:130
#define PGSTAT_FILE_ENTRY_NAME
Definition pgstat.c:145
void pgstat_register_kind(PgStat_Kind kind, const PgStat_KindInfo *kind_info)
Definition pgstat.c:1473
PgStat_LocalState pgStatLocal
Definition pgstat.c:212
void pgstat_discard_stats(void)
Definition pgstat.c:519
static bool match_db_entries(PgStatShared_HashEntry *entry, Datum match_data)
Definition pgstat.c:833
static bool force_stats_snapshot_clear
Definition pgstat.c:255
PgStat_Kind pgstat_get_kind_from_str(char *kind_str)
Definition pgstat.c:1411
void pgstat_write_chunk(FILE *fpout, void *ptr, size_t len)
Definition pgstat.c:1565
static void pgstat_shutdown_hook(int code, Datum arg)
Definition pgstat.c:613
@ PGSTAT_FETCH_CONSISTENCY_NONE
Definition pgstat.h:48
@ PGSTAT_FETCH_CONSISTENCY_CACHE
Definition pgstat.h:49
@ PGSTAT_FETCH_CONSISTENCY_SNAPSHOT
Definition pgstat.h:50
#define PGSTAT_STAT_PERMANENT_FILENAME
Definition pgstat.h:32
#define PGSTAT_STAT_PERMANENT_TMPFILE
Definition pgstat.h:33
#define PGSTAT_FILE_FORMAT_ID
Definition pgstat.h:217
void pgstat_archiver_init_shmem_cb(void *stats)
void pgstat_archiver_reset_all_cb(TimestampTz ts)
void pgstat_archiver_snapshot_cb(void)
void pgstat_backend_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
bool pgstat_backend_flush_cb(bool nowait)
void pgstat_bgwriter_reset_all_cb(TimestampTz ts)
void pgstat_bgwriter_init_shmem_cb(void *stats)
void pgstat_bgwriter_snapshot_cb(void)
void pgstat_checkpointer_snapshot_cb(void)
void pgstat_checkpointer_init_shmem_cb(void *stats)
void pgstat_checkpointer_reset_all_cb(TimestampTz ts)
void pgstat_database_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_update_dbstats(TimestampTz ts)
void pgstat_reset_database_timestamp(Oid dboid, TimestampTz ts)
void pgstat_report_disconnect(Oid dboid)
bool pgstat_database_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
void pgstat_function_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
bool pgstat_function_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
#define pgstat_write_chunk_s(fpout, ptr)
static void * pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry)
@ STATS_WRITE
@ STATS_READ
@ STATS_DISCARD
#define pgstat_assert_is_up()
#define pgstat_read_chunk_s(fpin, ptr)
static size_t pgstat_get_entry_len(PgStat_Kind kind)
bool pgstat_io_flush_cb(bool nowait)
Definition pgstat_io.c:189
void pgstat_io_reset_all_cb(TimestampTz ts)
Definition pgstat_io.c:287
void pgstat_io_snapshot_cb(void)
Definition pgstat_io.c:309
void pgstat_io_init_shmem_cb(void *stats)
Definition pgstat_io.c:278
#define PGSTAT_KIND_CUSTOM_MAX
Definition pgstat_kind.h:50
#define PGSTAT_KIND_ARCHIVER
Definition pgstat_kind.h:35
static bool pgstat_is_kind_builtin(PgStat_Kind kind)
Definition pgstat_kind.h:61
#define PGSTAT_KIND_WAL
Definition pgstat_kind.h:40
static bool pgstat_is_kind_custom(PgStat_Kind kind)
Definition pgstat_kind.h:67
#define PgStat_Kind
Definition pgstat_kind.h:17
#define PGSTAT_KIND_MAX
Definition pgstat_kind.h:21
#define PGSTAT_KIND_BGWRITER
Definition pgstat_kind.h:36
#define PGSTAT_KIND_CUSTOM_MIN
Definition pgstat_kind.h:49
#define PGSTAT_KIND_REPLSLOT
Definition pgstat_kind.h:30
#define PGSTAT_KIND_FUNCTION
Definition pgstat_kind.h:29
#define PGSTAT_KIND_BUILTIN_MAX
Definition pgstat_kind.h:43
#define PGSTAT_KIND_CUSTOM_SIZE
Definition pgstat_kind.h:51
#define PGSTAT_KIND_DATABASE
Definition pgstat_kind.h:27
#define PGSTAT_KIND_INVALID
Definition pgstat_kind.h:24
#define PGSTAT_KIND_SLRU
Definition pgstat_kind.h:39
#define PGSTAT_KIND_RELATION
Definition pgstat_kind.h:28
#define PGSTAT_KIND_CHECKPOINTER
Definition pgstat_kind.h:37
#define PGSTAT_KIND_MIN
Definition pgstat_kind.h:20
#define PGSTAT_KIND_BUILTIN_SIZE
Definition pgstat_kind.h:44
#define PGSTAT_KIND_IO
Definition pgstat_kind.h:38
#define PGSTAT_KIND_SUBSCRIPTION
Definition pgstat_kind.h:31
#define PGSTAT_KIND_BACKEND
Definition pgstat_kind.h:32
#define PGSTAT_KIND_BUILTIN_MIN
Definition pgstat_kind.h:42
bool pgstat_relation_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
void pgstat_relation_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_relation_delete_pending_cb(PgStat_EntryRef *entry_ref)
bool pgstat_replslot_from_serialized_name_cb(const NameData *name, PgStat_HashKey *key)
void pgstat_replslot_to_serialized_name_cb(const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name)
void pgstat_replslot_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_reset_entries_of_kind(PgStat_Kind kind, TimestampTz ts)
void pgstat_request_entry_refs_gc(void)
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)
bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
void pgstat_attach_shmem(void)
bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
void pgstat_reset_entry(PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts)
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)
void pgstat_drop_all_entries(void)
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
void pgstat_detach_shmem(void)
void pgstat_slru_snapshot_cb(void)
bool pgstat_slru_flush_cb(bool nowait)
void pgstat_slru_reset_all_cb(TimestampTz ts)
void pgstat_slru_init_shmem_cb(void *stats)
bool pgstat_subscription_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
void pgstat_subscription_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_wal_reset_all_cb(TimestampTz ts)
Definition pgstat_wal.c:158
void pgstat_wal_init_shmem_cb(void *stats)
Definition pgstat_wal.c:150
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_wal_snapshot_cb(void)
Definition pgstat_wal.c:169
int pg_strcasecmp(const char *s1, const char *s2)
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
uint64_t Datum
Definition postgres.h:70
#define InvalidOid
unsigned int Oid
static int fb(int x)
static void error(void)
pg_atomic_uint32 refcount
PgStatShared_Common * shared_stats
PgStatShared_HashEntry * shared_entry
dlist_node pending_node
PgStat_Kind kind
const char *const name
PgStat_Snapshot snapshot
dshash_table * shared_hash
PgStat_ShmemControl * shmem
void * custom_data[PGSTAT_KIND_CUSTOM_SIZE]
void * custom_data[PGSTAT_KIND_CUSTOM_SIZE]
TimestampTz snapshot_timestamp
MemoryContext context
bool custom_valid[PGSTAT_KIND_CUSTOM_SIZE]
PgStat_FetchConsistency mode
struct pgstat_snapshot_hash * stats
bool fixed_valid[PGSTAT_KIND_BUILTIN_SIZE]
Definition c.h:770
const char * name
bool IsTransactionOrTransactionBlock(void)
Definition xact.c:5011
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition xact.c:892

◆ PGSTAT_FILE_ENTRY_NAME

#define PGSTAT_FILE_ENTRY_NAME   'N' /* stats entry identified by name */

Definition at line 145 of file pgstat.c.

◆ PGSTAT_IDLE_INTERVAL

#define PGSTAT_IDLE_INTERVAL   10000

Definition at line 130 of file pgstat.c.

◆ PGSTAT_MAX_INTERVAL

#define PGSTAT_MAX_INTERVAL   60000

Definition at line 128 of file pgstat.c.

◆ PGSTAT_MIN_INTERVAL

#define PGSTAT_MIN_INTERVAL   1000

Definition at line 126 of file pgstat.c.

◆ PGSTAT_SNAPSHOT_HASH_SIZE

#define PGSTAT_SNAPSHOT_HASH_SIZE   512

Definition at line 137 of file pgstat.c.

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 173 of file pgstat.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 172 of file pgstat.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   PgStat_SnapshotEntry

Definition at line 164 of file pgstat.c.

◆ SH_EQUAL

#define SH_EQUAL (   tb,
  a,
  b 
)     pgstat_cmp_hash_key(&a, &b, sizeof(PgStat_HashKey), NULL) == 0

Definition at line 169 of file pgstat.c.

◆ SH_HASH_KEY

#define SH_HASH_KEY (   tb,
  key 
)     pgstat_hash_hash_key(&key, sizeof(PgStat_HashKey), NULL)

Definition at line 167 of file pgstat.c.

◆ SH_KEY

#define SH_KEY   key

Definition at line 166 of file pgstat.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   PgStat_HashKey

Definition at line 165 of file pgstat.c.

◆ SH_PREFIX

#define SH_PREFIX   pgstat_snapshot

Definition at line 163 of file pgstat.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 171 of file pgstat.c.

Typedef Documentation

◆ PgStat_SnapshotEntry

Function Documentation

◆ assign_stats_fetch_consistency()

void assign_stats_fetch_consistency ( int  newval,
void extra 
)

Definition at line 2111 of file pgstat.c.

2113{
2114 /*
2115 * Changing this value in a transaction may cause snapshot state
2116 * inconsistencies, so force a clear of the current snapshot on the next
2117 * snapshot build attempt.
2118 */

References force_stats_snapshot_clear, newval, and pgstat_fetch_consistency.

◆ match_db_entries()

static bool match_db_entries ( PgStatShared_HashEntry entry,
Datum  match_data 
)
static

Definition at line 833 of file pgstat.c.

835{
836 return entry->key.dboid == MyDatabaseId;

References PgStat_HashKey::dboid, PgStatShared_HashEntry::key, and MyDatabaseId.

Referenced by pgstat_reset_counters().

◆ pgstat_before_server_shutdown()

void pgstat_before_server_shutdown ( int  code,
Datum  arg 
)

Definition at line 572 of file pgstat.c.

574{
577
578 /*
579 * Stats should only be reported after pgstat_initialize() and before
580 * pgstat_shutdown(). This is a convenient point to catch most violations
581 * of this rule.
582 */
584
585 /* flush out our own pending changes before writing out */
586 pgstat_report_stat(true);
587
588 /*
589 * Only write out file during normal shutdown. Don't even signal that
590 * we've shutdown during irregular shutdowns, because the shutdown
591 * sequence isn't coordinated to ensure this backend shuts down last.
592 */
593 if (code == 0)
594 {
597 }

References Assert, fb(), PgStat_ShmemControl::is_shutdown, pgstat_report_stat(), pgstat_write_statsfile(), pgStatLocal, and PgStat_LocalState::shmem.

Referenced by CheckpointerMain(), and InitPostgres().

◆ pgstat_build_snapshot()

static void pgstat_build_snapshot ( void  )
static

Definition at line 1130 of file pgstat.c.

1132{
1135
1136 /* should only be called when we need a snapshot */
1138
1139 /* snapshot already built */
1141 return;
1142
1144
1145 Assert(pgStatLocal.snapshot.stats->members == 0);
1146
1148
1149 /*
1150 * Snapshot all variable stats.
1151 */
1153 while ((p = dshash_seq_next(&hstat)) != NULL)
1154 {
1155 PgStat_Kind kind = p->key.kind;
1157 bool found;
1158 PgStat_SnapshotEntry *entry;
1160
1161 /*
1162 * Check if the stats object should be included in the snapshot.
1163 * Unless the stats kind can be accessed from all databases (e.g.,
1164 * database stats themselves), we only include stats for the current
1165 * database or objects not associated with a database (e.g. shared
1166 * relations).
1167 */
1168 if (p->key.dboid != MyDatabaseId &&
1169 p->key.dboid != InvalidOid &&
1170 !kind_info->accessed_across_databases)
1171 continue;
1172
1173 if (p->dropped)
1174 continue;
1175
1177
1180
1182 Assert(!found);
1183
1185 pgstat_get_entry_len(kind));
1186
1187 /*
1188 * Acquire the LWLock directly instead of using
1189 * pg_stat_lock_entry_shared() which requires a reference.
1190 */
1192 memcpy(entry->data,
1194 pgstat_get_entry_len(kind));
1195 LWLockRelease(&stats_data->lock);
1196 }
1198
1199 /*
1200 * Build snapshot of all fixed-numbered stats.
1201 */
1202 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
1203 {
1205
1206 if (!kind_info)
1207 continue;
1208 if (!kind_info->fixed_amount)
1209 {
1210 Assert(kind_info->snapshot_cb == NULL);
1211 continue;
1212 }
1213
1215 }
1216

References Assert, PgStatShared_HashEntry::body, PgStat_Snapshot::context, PgStat_SnapshotEntry::data, PgStat_HashKey::dboid, PgStatShared_HashEntry::dropped, PgStat_LocalState::dsa, dsa_get_address(), dshash_seq_init(), dshash_seq_next(), dshash_seq_term(), fb(), GetCurrentTimestamp(), InvalidOid, PgStatShared_HashEntry::key, PgStat_HashKey::kind, LW_SHARED, LWLockAcquire(), LWLockRelease(), MemoryContextAlloc(), PgStat_Snapshot::mode, MyDatabaseId, pg_atomic_read_u32(), pgstat_build_snapshot_fixed(), pgstat_fetch_consistency, PGSTAT_FETCH_CONSISTENCY_SNAPSHOT, pgstat_get_entry_data(), pgstat_get_entry_len(), pgstat_get_kind_info(), PgStat_Kind, PGSTAT_KIND_MAX, PGSTAT_KIND_MIN, pgstat_prep_snapshot(), pgStatLocal, PgStatShared_HashEntry::refcount, PgStat_LocalState::shared_hash, PgStat_LocalState::snapshot, PgStat_Snapshot::snapshot_timestamp, and PgStat_Snapshot::stats.

Referenced by pgstat_fetch_entry(), and pgstat_snapshot_fixed().

◆ pgstat_build_snapshot_fixed()

static void pgstat_build_snapshot_fixed ( PgStat_Kind  kind)
static

Definition at line 1220 of file pgstat.c.

1222{
1224 int idx;
1225 bool *valid;
1226
1227 /* Position in fixed_valid or custom_valid */
1228 if (pgstat_is_kind_builtin(kind))
1229 {
1230 idx = kind;
1232 }
1233 else
1234 {
1235 idx = kind - PGSTAT_KIND_CUSTOM_MIN;
1237 }
1238
1239 Assert(kind_info->fixed_amount);
1240 Assert(kind_info->snapshot_cb != NULL);
1241
1243 {
1244 /* rebuild every time */
1245 valid[idx] = false;
1246 }
1247 else if (valid[idx])
1248 {
1249 /* in snapshot mode we shouldn't get called again */
1251 return;
1252 }
1253
1254 Assert(!valid[idx]);
1255
1256 kind_info->snapshot_cb();
1257
1258 Assert(!valid[idx]);
1259 valid[idx] = true;

References Assert, PgStat_Snapshot::custom_valid, fb(), PgStat_Snapshot::fixed_valid, idx(), pgstat_fetch_consistency, PGSTAT_FETCH_CONSISTENCY_CACHE, PGSTAT_FETCH_CONSISTENCY_NONE, pgstat_get_kind_info(), pgstat_is_kind_builtin(), PGSTAT_KIND_CUSTOM_MIN, pgStatLocal, and PgStat_LocalState::snapshot.

Referenced by pgstat_build_snapshot(), pgstat_snapshot_fixed(), and pgstat_write_statsfile().

◆ pgstat_clear_snapshot()

void pgstat_clear_snapshot ( void  )

Definition at line 912 of file pgstat.c.

914{
916
923
924 /* Release memory, if any was allocated */
926 {
928
929 /* Reset variables */
931 }
932
933 /*
934 * Historically the backend_status.c facilities lived in this file, and
935 * were reset with the same function. For now keep it that way, and
936 * forward the reset request.
937 */
939
940 /* Reset this flag, as it may be possible that a cleanup was forced. */

References PgStat_Snapshot::context, PgStat_Snapshot::custom_valid, fb(), PgStat_Snapshot::fixed_valid, force_stats_snapshot_clear, MemoryContextDelete(), PgStat_Snapshot::mode, pgstat_assert_is_up, pgstat_clear_backend_activity_snapshot(), PGSTAT_FETCH_CONSISTENCY_NONE, pgStatLocal, PgStat_LocalState::snapshot, and PgStat_Snapshot::stats.

Referenced by AtEOXact_PgStat(), pg_stat_clear_snapshot(), pgstat_get_stat_snapshot_timestamp(), pgstat_prep_snapshot(), pgstat_snapshot_fixed(), and PostPrepare_PgStat().

◆ pgstat_delete_pending_entry()

void pgstat_delete_pending_entry ( PgStat_EntryRef entry_ref)

Definition at line 1326 of file pgstat.c.

1328{
1329 PgStat_Kind kind = entry_ref->shared_entry->key.kind;
1331 void *pending_data = entry_ref->pending;
1332
1334 /* !fixed_amount stats should be handled explicitly */
1335 Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1336
1337 if (kind_info->delete_pending_cb)
1338 kind_info->delete_pending_cb(entry_ref);
1339
1341 entry_ref->pending = NULL;
1342
1343 dlist_delete(&entry_ref->pending_node);

References Assert, dlist_delete(), fb(), PgStatShared_HashEntry::key, PgStat_HashKey::kind, PgStat_EntryRef::pending, PgStat_EntryRef::pending_node, pfree(), pgstat_get_kind_info(), PgStat_Kind, and PgStat_EntryRef::shared_entry.

Referenced by pgstat_flush_pending_entries(), and pgstat_release_entry_ref().

◆ pgstat_discard_stats()

void pgstat_discard_stats ( void  )

Definition at line 519 of file pgstat.c.

521{
522 int ret;
523
524 /* NB: this needs to be done even in single user mode */
525
526 /* First, cleanup the main pgstats file */
528 if (ret != 0)
529 {
530 if (errno == ENOENT)
531 elog(DEBUG2,
532 "didn't need to unlink permanent stats file \"%s\" - didn't exist",
534 else
535 ereport(LOG,
537 errmsg("could not unlink permanent statistics file \"%s\": %m",
539 }
540 else
541 {
544 errmsg_internal("unlinked permanent statistics file \"%s\"",
546 }
547
548 /* Finish callbacks, if required */
549 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
550 {
552
553 if (kind_info && kind_info->finish)
554 kind_info->finish(STATS_DISCARD);
555 }
556
557 /*
558 * Reset stats contents. This will set reset timestamps of fixed-numbered
559 * stats to the current time (no variable stats exist).
560 */

References DEBUG2, elog, ereport, errcode_for_file_access(), errmsg(), errmsg_internal(), fb(), LOG, pgstat_get_kind_info(), PgStat_Kind, PGSTAT_KIND_MAX, PGSTAT_KIND_MIN, pgstat_reset_after_failure(), PGSTAT_STAT_PERMANENT_FILENAME, and STATS_DISCARD.

Referenced by StartupXLOG().

◆ pgstat_fetch_entry()

void * pgstat_fetch_entry ( PgStat_Kind  kind,
Oid  dboid,
uint64  objid 
)

Definition at line 944 of file pgstat.c.

946{
947 PgStat_HashKey key = {0};
948 PgStat_EntryRef *entry_ref;
949 void *stats_data;
951
952 /* should be called from backends */
954 Assert(!kind_info->fixed_amount);
955
957
958 key.kind = kind;
959 key.dboid = dboid;
960 key.objid = objid;
961
962 /* if we need to build a full snapshot, do so */
965
966 /* if caching is desired, look up in cache */
968 {
969 PgStat_SnapshotEntry *entry = NULL;
970
972
973 if (entry)
974 return entry->data;
975
976 /*
977 * If we built a full snapshot and the key is not in
978 * pgStatLocal.snapshot.stats, there are no matching stats.
979 */
981 return NULL;
982 }
983
985
986 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
987
988 if (entry_ref == NULL || entry_ref->shared_entry->dropped)
989 {
990 /* create empty entry when using PGSTAT_FETCH_CONSISTENCY_CACHE */
992 {
993 PgStat_SnapshotEntry *entry = NULL;
994 bool found;
995
996 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
997 Assert(!found);
998 entry->data = NULL;
999 }
1000 return NULL;
1001 }
1002
1003 /*
1004 * Allocate in caller's context for PGSTAT_FETCH_CONSISTENCY_NONE,
1005 * otherwise we could quickly end up with a fair bit of memory used due to
1006 * repeated accesses.
1007 */
1009 stats_data = palloc(kind_info->shared_data_len);
1010 else
1012 kind_info->shared_data_len);
1013
1014 (void) pgstat_lock_entry_shared(entry_ref, false);
1016 pgstat_get_entry_data(kind, entry_ref->shared_stats),
1017 kind_info->shared_data_len);
1018 pgstat_unlock_entry(entry_ref);
1019
1021 {
1022 PgStat_SnapshotEntry *entry = NULL;
1023 bool found;
1024
1025 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
1026 entry->data = stats_data;
1027 }
1028
1029 return stats_data;

References Assert, PgStat_Snapshot::context, PgStat_SnapshotEntry::data, PgStatShared_HashEntry::dropped, fb(), IsPostmasterEnvironment, IsUnderPostmaster, MemoryContextAlloc(), PgStat_Snapshot::mode, palloc(), pgstat_build_snapshot(), pgstat_fetch_consistency, PGSTAT_FETCH_CONSISTENCY_CACHE, PGSTAT_FETCH_CONSISTENCY_NONE, PGSTAT_FETCH_CONSISTENCY_SNAPSHOT, pgstat_get_entry_data(), pgstat_get_entry_ref(), pgstat_get_kind_info(), pgstat_lock_entry_shared(), pgstat_prep_snapshot(), pgstat_unlock_entry(), pgStatLocal, PgStat_EntryRef::shared_entry, PgStat_EntryRef::shared_stats, PgStat_LocalState::snapshot, and PgStat_Snapshot::stats.

Referenced by pgstat_fetch_replslot(), pgstat_fetch_stat_backend(), pgstat_fetch_stat_dbentry(), pgstat_fetch_stat_funcentry(), pgstat_fetch_stat_subscription(), pgstat_fetch_stat_tabentry_ext(), and test_custom_stats_var_fetch_entry().

◆ pgstat_fetch_pending_entry()

PgStat_EntryRef * pgstat_fetch_pending_entry ( PgStat_Kind  kind,
Oid  dboid,
uint64  objid 
)

Definition at line 1313 of file pgstat.c.

1315{
1316 PgStat_EntryRef *entry_ref;
1317
1318 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
1319
1320 if (entry_ref == NULL || entry_ref->pending == NULL)
1321 return NULL;
1322
1323 return entry_ref;

References fb(), PgStat_EntryRef::pending, and pgstat_get_entry_ref().

Referenced by find_funcstat_entry(), and find_tabstat_entry().

◆ pgstat_flush_pending_entries()

static bool pgstat_flush_pending_entries ( bool  nowait)
static

Definition at line 1349 of file pgstat.c.

1351{
1352 bool have_pending = false;
1353 dlist_node *cur = NULL;
1354
1355 /*
1356 * Need to be a bit careful iterating over the list of pending entries.
1357 * Processing a pending entry may queue further pending entries to the end
1358 * of the list that we want to process, so a simple iteration won't do.
1359 * Further complicating matters is that we want to delete the current
1360 * entry in each iteration from the list if we flushed successfully.
1361 *
1362 * So we just keep track of the next pointer in each loop iteration.
1363 */
1366
1367 while (cur)
1368 {
1369 PgStat_EntryRef *entry_ref =
1370 dlist_container(PgStat_EntryRef, pending_node, cur);
1371 PgStat_HashKey key = entry_ref->shared_entry->key;
1372 PgStat_Kind kind = key.kind;
1374 bool did_flush;
1376
1377 Assert(!kind_info->fixed_amount);
1378 Assert(kind_info->flush_pending_cb != NULL);
1379
1380 /* flush the stats, if possible */
1381 did_flush = kind_info->flush_pending_cb(entry_ref, nowait);
1382
1383 Assert(did_flush || nowait);
1384
1385 /* determine next entry, before deleting the pending entry */
1388 else
1389 next = NULL;
1390
1391 /* if successfully flushed, remove entry */
1392 if (did_flush)
1393 pgstat_delete_pending_entry(entry_ref);
1394 else
1395 have_pending = true;
1396
1397 cur = next;
1398 }
1399
1401
1402 return have_pending;

References Assert, cur, dlist_container, dlist_has_next(), dlist_head_node(), dlist_is_empty(), dlist_next_node(), fb(), PgStatShared_HashEntry::key, next, pgstat_delete_pending_entry(), pgstat_get_kind_info(), PgStat_Kind, pgStatPending, and PgStat_EntryRef::shared_entry.

Referenced by pgstat_report_stat().

◆ pgstat_force_next_flush()

void pgstat_force_next_flush ( void  )

Definition at line 824 of file pgstat.c.

826{

References pgStatForceNextFlush.

Referenced by pg_stat_force_next_flush().

◆ pgstat_get_kind_from_str()

PgStat_Kind pgstat_get_kind_from_str ( char kind_str)

Definition at line 1411 of file pgstat.c.

1413{
1414 for (PgStat_Kind kind = PGSTAT_KIND_BUILTIN_MIN; kind <= PGSTAT_KIND_BUILTIN_MAX; kind++)
1415 {
1417 return kind;
1418 }
1419
1420 /* Check the custom set of cumulative stats */
1422 {
1423 for (PgStat_Kind kind = PGSTAT_KIND_CUSTOM_MIN; kind <= PGSTAT_KIND_CUSTOM_MAX; kind++)
1424 {
1426
1429 return kind;
1430 }
1431 }
1432
1433 ereport(ERROR,
1435 errmsg("invalid statistics kind: \"%s\"", kind_str)));
1436 return PGSTAT_KIND_INVALID; /* avoid compiler warnings */

References ereport, errcode(), errmsg(), ERROR, fb(), idx(), name, pg_strcasecmp(), PgStat_Kind, pgstat_kind_builtin_infos, PGSTAT_KIND_BUILTIN_MAX, PGSTAT_KIND_BUILTIN_MIN, pgstat_kind_custom_infos, PGSTAT_KIND_CUSTOM_MAX, PGSTAT_KIND_CUSTOM_MIN, and PGSTAT_KIND_INVALID.

Referenced by pg_stat_have_stats().

◆ pgstat_get_kind_info()

◆ pgstat_get_stat_snapshot_timestamp()

TimestampTz pgstat_get_stat_snapshot_timestamp ( bool have_snapshot)

◆ pgstat_have_entry()

bool pgstat_have_entry ( PgStat_Kind  kind,
Oid  dboid,
uint64  objid 
)

Definition at line 1054 of file pgstat.c.

1056{
1057 /* fixed-numbered stats always exist */
1058 if (pgstat_get_kind_info(kind)->fixed_amount)
1059 return true;
1060
1061 return pgstat_get_entry_ref(kind, dboid, objid, false, NULL) != NULL;

References fb(), pgstat_get_entry_ref(), and pgstat_get_kind_info().

Referenced by pg_stat_have_stats().

◆ pgstat_init_snapshot_fixed()

static void pgstat_init_snapshot_fixed ( void  )
static

Definition at line 1090 of file pgstat.c.

1092{
1093 /*
1094 * Initialize fixed-numbered statistics data in snapshots, only for custom
1095 * stats kinds.
1096 */
1097 for (PgStat_Kind kind = PGSTAT_KIND_CUSTOM_MIN; kind <= PGSTAT_KIND_CUSTOM_MAX; kind++)
1098 {
1100
1101 if (!kind_info || !kind_info->fixed_amount)
1102 continue;
1103
1106 }

References PgStat_Snapshot::custom_data, fb(), MemoryContextAlloc(), pgstat_get_kind_info(), PgStat_Kind, PGSTAT_KIND_CUSTOM_MAX, PGSTAT_KIND_CUSTOM_MIN, pgStatLocal, PgStat_LocalState::snapshot, and TopMemoryContext.

Referenced by pgstat_initialize().

◆ pgstat_initialize()

void pgstat_initialize ( void  )

Definition at line 651 of file pgstat.c.

653{
655
657
659
660 /* Backend initialization callbacks */
661 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
662 {
664
665 if (kind_info == NULL || kind_info->init_backend_cb == NULL)
666 continue;
667
668 kind_info->init_backend_cb();
669 }
670
671 /* Set up a process-exit hook to clean up */
673
674#ifdef USE_ASSERT_CHECKING
676#endif

References Assert, before_shmem_exit(), fb(), pgstat_attach_shmem(), pgstat_get_kind_info(), pgstat_init_snapshot_fixed(), PgStat_Kind, PGSTAT_KIND_MAX, PGSTAT_KIND_MIN, and pgstat_shutdown_hook().

Referenced by BaseInit().

◆ pgstat_is_kind_valid()

static bool pgstat_is_kind_valid ( PgStat_Kind  kind)
inlinestatic

◆ pgstat_prep_pending_entry()

PgStat_EntryRef * pgstat_prep_pending_entry ( PgStat_Kind  kind,
Oid  dboid,
uint64  objid,
bool created_entry 
)

Definition at line 1275 of file pgstat.c.

1277{
1278 PgStat_EntryRef *entry_ref;
1279
1280 /* need to be able to flush out */
1281 Assert(pgstat_get_kind_info(kind)->flush_pending_cb != NULL);
1282
1284 {
1287 "PgStat Pending",
1289 }
1290
1291 entry_ref = pgstat_get_entry_ref(kind, dboid, objid,
1292 true, created_entry);
1293
1294 if (entry_ref->pending == NULL)
1295 {
1296 size_t entrysize = pgstat_get_kind_info(kind)->pending_size;
1297
1298 Assert(entrysize != (size_t) -1);
1299
1300 entry_ref->pending = MemoryContextAllocZero(pgStatPendingContext, entrysize);
1302 }
1303
1304 return entry_ref;

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, Assert, dlist_push_tail(), fb(), MemoryContextAllocZero(), PgStat_EntryRef::pending, PgStat_EntryRef::pending_node, PgStat_KindInfo::pending_size, pgstat_get_entry_ref(), pgstat_get_kind_info(), pgStatPending, pgStatPendingContext, TopMemoryContext, and unlikely.

Referenced by pgstat_init_function_usage(), pgstat_prep_database_pending(), pgstat_prep_relation_pending(), pgstat_report_subscription_conflict(), pgstat_report_subscription_error(), and test_custom_stats_var_update().

◆ pgstat_prep_snapshot()

◆ pgstat_read_chunk()

bool pgstat_read_chunk ( FILE fpin,
void ptr,
size_t  len 
)

Definition at line 1763 of file pgstat.c.

1765{
1766 return fread(ptr, 1, len, fpin) == len;

References fb(), and len.

Referenced by pgstat_read_statsfile(), and test_custom_stats_var_from_serialized_data().

◆ pgstat_read_statsfile()

static void pgstat_read_statsfile ( void  )
static

Definition at line 1775 of file pgstat.c.

1777{
1778 FILE *fpin;
1780 bool found;
1783
1784 /* shouldn't be called from postmaster */
1786
1787 elog(DEBUG2, "reading stats file \"%s\"", statfile);
1788
1789 /*
1790 * Try to open the stats file. If it doesn't exist, the backends simply
1791 * returns zero for anything and statistics simply starts from scratch
1792 * with empty counters.
1793 *
1794 * ENOENT is a possibility if stats collection was previously disabled or
1795 * has not yet written the stats file for the first time. Any other
1796 * failure condition is suspicious.
1797 */
1799 {
1800 if (errno != ENOENT)
1801 ereport(LOG,
1803 errmsg("could not open statistics file \"%s\": %m",
1804 statfile)));
1806 return;
1807 }
1808
1809 /*
1810 * Verify it's of the expected format.
1811 */
1813 {
1814 elog(WARNING, "could not read format ID");
1815 goto error;
1816 }
1817
1819 {
1820 elog(WARNING, "found incorrect format ID %d (expected %d)",
1822 goto error;
1823 }
1824
1825 /*
1826 * We found an existing statistics file. Read it and put all the stats
1827 * data into place.
1828 */
1829 for (;;)
1830 {
1831 int t = fgetc(fpin);
1832
1833 switch (t)
1834 {
1836 {
1837 PgStat_Kind kind;
1838 const PgStat_KindInfo *info;
1839 char *ptr;
1840
1841 /* entry for fixed-numbered stats */
1842 if (!pgstat_read_chunk_s(fpin, &kind))
1843 {
1844 elog(WARNING, "could not read stats kind for entry of type %c", t);
1845 goto error;
1846 }
1847
1848 if (!pgstat_is_kind_valid(kind))
1849 {
1850 elog(WARNING, "invalid stats kind %u for entry of type %c",
1851 kind, t);
1852 goto error;
1853 }
1854
1855 info = pgstat_get_kind_info(kind);
1856 if (!info)
1857 {
1858 elog(WARNING, "could not find information of kind %u for entry of type %c",
1859 kind, t);
1860 goto error;
1861 }
1862
1863 if (!info->fixed_amount)
1864 {
1865 elog(WARNING, "invalid fixed_amount in stats kind %u for entry of type %c",
1866 kind, t);
1867 goto error;
1868 }
1869
1870 /* Load back stats into shared memory */
1871 if (pgstat_is_kind_builtin(kind))
1872 ptr = ((char *) shmem) + info->shared_ctl_off +
1873 info->shared_data_off;
1874 else
1875 {
1876 int idx = kind - PGSTAT_KIND_CUSTOM_MIN;
1877
1878 ptr = ((char *) shmem->custom_data[idx]) +
1879 info->shared_data_off;
1880 }
1881
1882 if (!pgstat_read_chunk(fpin, ptr, info->shared_data_len))
1883 {
1884 elog(WARNING, "could not read data of stats kind %u for entry of type %c with size %u",
1885 kind, t, info->shared_data_len);
1886 goto error;
1887 }
1888
1889 break;
1890 }
1893 {
1896 PgStatShared_Common *header;
1898
1900
1901 if (t == PGSTAT_FILE_ENTRY_HASH)
1902 {
1903 /* normal stats entry, identified by PgStat_HashKey */
1904 if (!pgstat_read_chunk_s(fpin, &key))
1905 {
1906 elog(WARNING, "could not read key for entry of type %c", t);
1907 goto error;
1908 }
1909
1910 if (!pgstat_is_kind_valid(key.kind))
1911 {
1912 elog(WARNING, "invalid stats kind for entry %u/%u/%" PRIu64 " of type %c",
1913 key.kind, key.dboid,
1914 key.objid, t);
1915 goto error;
1916 }
1917
1919 if (!kind_info)
1920 {
1921 elog(WARNING, "could not find information of kind for entry %u/%u/%" PRIu64 " of type %c",
1922 key.kind, key.dboid,
1923 key.objid, t);
1924 goto error;
1925 }
1926 }
1927 else
1928 {
1929 /* stats entry identified by name on disk (e.g. slots) */
1930 PgStat_Kind kind;
1931 NameData name;
1932
1933 if (!pgstat_read_chunk_s(fpin, &kind))
1934 {
1935 elog(WARNING, "could not read stats kind for entry of type %c", t);
1936 goto error;
1937 }
1939 {
1940 elog(WARNING, "could not read name of stats kind %u for entry of type %c",
1941 kind, t);
1942 goto error;
1943 }
1944 if (!pgstat_is_kind_valid(kind))
1945 {
1946 elog(WARNING, "invalid stats kind %u for entry of type %c",
1947 kind, t);
1948 goto error;
1949 }
1950
1952 if (!kind_info)
1953 {
1954 elog(WARNING, "could not find information of kind %u for entry of type %c",
1955 kind, t);
1956 goto error;
1957 }
1958
1959 if (!kind_info->from_serialized_name)
1960 {
1961 elog(WARNING, "invalid from_serialized_name in stats kind %u for entry of type %c",
1962 kind, t);
1963 goto error;
1964 }
1965
1966 if (!kind_info->from_serialized_name(&name, &key))
1967 {
1968 /* skip over data for entry we don't care about */
1969 if (fseek(fpin, pgstat_get_entry_len(kind), SEEK_CUR) != 0)
1970 {
1971 elog(WARNING, "could not seek \"%s\" of stats kind %u for entry of type %c",
1972 NameStr(name), kind, t);
1973 goto error;
1974 }
1975
1976 continue;
1977 }
1978
1979 Assert(key.kind == kind);
1980 }
1981
1982 /*
1983 * This intentionally doesn't use pgstat_get_entry_ref() -
1984 * putting all stats into checkpointer's
1985 * pgStatEntryRefHash would be wasted effort and memory.
1986 */
1988
1989 /* don't allow duplicate entries */
1990 if (found)
1991 {
1993 elog(WARNING, "found duplicate stats entry %u/%u/%" PRIu64 " of type %c",
1994 key.kind, key.dboid,
1995 key.objid, t);
1996 goto error;
1997 }
1998
1999 header = pgstat_init_entry(key.kind, p);
2001 if (header == NULL)
2002 {
2003 /*
2004 * It would be tempting to switch this ERROR to a
2005 * WARNING, but it would mean that all the statistics
2006 * are discarded when the environment fails on OOM.
2007 */
2008 elog(ERROR, "could not allocate entry %u/%u/%" PRIu64 " of type %c",
2009 key.kind, key.dboid,
2010 key.objid, t);
2011 }
2012
2014 pgstat_get_entry_data(key.kind, header),
2016 {
2017 elog(WARNING, "could not read data for entry %u/%u/%" PRIu64 " of type %c",
2018 key.kind, key.dboid,
2019 key.objid, t);
2020 goto error;
2021 }
2022
2023 /* read more data for the entry, if required */
2024 if (kind_info->from_serialized_data)
2025 {
2026 if (!kind_info->from_serialized_data(&key, header, fpin))
2027 {
2028 elog(WARNING, "could not read auxiliary data for entry %u/%u/%" PRIu64 " of type %c",
2029 key.kind, key.dboid,
2030 key.objid, t);
2031 goto error;
2032 }
2033 }
2034
2035 break;
2036 }
2038
2039 /*
2040 * check that PGSTAT_FILE_ENTRY_END actually signals end of
2041 * file
2042 */
2043 if (fgetc(fpin) != EOF)
2044 {
2045 elog(WARNING, "could not read end-of-file");
2046 goto error;
2047 }
2048
2049 goto done;
2050
2051 default:
2052 elog(WARNING, "could not read entry of type %c", t);
2053 goto error;
2054 }
2055 }
2056
2057done:
2058 /* First, cleanup the main stats file */
2059 FreeFile(fpin);
2060
2061 elog(DEBUG2, "removing permanent stats file \"%s\"", statfile);
2063
2064 /* Finish callbacks, if required */
2065 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
2066 {
2068
2069 if (kind_info && kind_info->finish)
2070 kind_info->finish(STATS_READ);
2071 }
2072
2073 return;
2074
2075error:
2076 ereport(LOG,
2077 (errmsg("corrupted statistics file \"%s\"", statfile)));
2078
2080
2081 goto done;

References AllocateFile(), Assert, CHECK_FOR_INTERRUPTS, PgStat_ShmemControl::custom_data, DEBUG2, dshash_find_or_insert(), dshash_release_lock(), elog, ereport, errcode_for_file_access(), errmsg(), ERROR, error(), fb(), PgStat_KindInfo::fixed_amount, FreeFile(), idx(), IsPostmasterEnvironment, IsUnderPostmaster, LOG, name, NameStr, PG_BINARY_R, PGSTAT_FILE_ENTRY_END, PGSTAT_FILE_ENTRY_FIXED, PGSTAT_FILE_ENTRY_HASH, PGSTAT_FILE_ENTRY_NAME, PGSTAT_FILE_FORMAT_ID, pgstat_get_entry_data(), pgstat_get_entry_len(), pgstat_get_kind_info(), pgstat_init_entry(), pgstat_is_kind_builtin(), pgstat_is_kind_valid(), PgStat_Kind, PGSTAT_KIND_CUSTOM_MIN, PGSTAT_KIND_MAX, PGSTAT_KIND_MIN, pgstat_read_chunk(), pgstat_read_chunk_s, pgstat_reset_after_failure(), PGSTAT_STAT_PERMANENT_FILENAME, pgStatLocal, PgStat_KindInfo::shared_ctl_off, PgStat_KindInfo::shared_data_len, PgStat_KindInfo::shared_data_off, PgStat_LocalState::shared_hash, PgStat_LocalState::shmem, STATS_READ, and WARNING.

Referenced by pgstat_restore_stats().

◆ pgstat_register_kind()

void pgstat_register_kind ( PgStat_Kind  kind,
const PgStat_KindInfo kind_info 
)

Definition at line 1473 of file pgstat.c.

1475{
1477
1478 if (kind_info->name == NULL || strlen(kind_info->name) == 0)
1479 ereport(ERROR,
1480 (errmsg("custom cumulative statistics name is invalid"),
1481 errhint("Provide a non-empty name for the custom cumulative statistics.")));
1482
1483 if (!pgstat_is_kind_custom(kind))
1484 ereport(ERROR, (errmsg("custom cumulative statistics ID %u is out of range", kind),
1485 errhint("Provide a custom cumulative statistics ID between %u and %u.",
1487
1489 ereport(ERROR,
1490 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1491 errdetail("Custom cumulative statistics must be registered while initializing modules in \"shared_preload_libraries\".")));
1492
1493 /*
1494 * Check some data for fixed-numbered stats.
1495 */
1496 if (kind_info->fixed_amount)
1497 {
1498 if (kind_info->shared_size == 0)
1499 ereport(ERROR,
1500 (errmsg("custom cumulative statistics property is invalid"),
1501 errhint("Custom cumulative statistics require a shared memory size for fixed-numbered objects.")));
1502 if (kind_info->track_entry_count)
1503 ereport(ERROR,
1504 (errmsg("custom cumulative statistics property is invalid"),
1505 errhint("Custom cumulative statistics cannot use entry count tracking for fixed-numbered objects.")));
1506 }
1507
1508 /*
1509 * If pgstat_kind_custom_infos is not available yet, allocate it.
1510 */
1512 {
1516 }
1517
1520 ereport(ERROR,
1521 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1522 errdetail("Custom cumulative statistics \"%s\" already registered with the same ID.",
1524
1525 /* check for existing custom stats with the same name */
1527 {
1529
1531 continue;
1533 ereport(ERROR,
1534 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1535 errdetail("Existing cumulative statistics with ID %u has the same name.", existing_kind)));
1536 }
1537
1538 /* Register it */
1540 ereport(LOG,
1541 (errmsg("registered custom cumulative statistics \"%s\" with ID %u",
1542 kind_info->name, kind)));

References ereport, errdetail(), errhint(), errmsg(), ERROR, fb(), idx(), LOG, MemoryContextAllocZero(), name, PgStat_KindInfo::name, pg_strcasecmp(), pgstat_is_kind_custom(), PgStat_Kind, pgstat_kind_custom_infos, PGSTAT_KIND_CUSTOM_MAX, PGSTAT_KIND_CUSTOM_MIN, PGSTAT_KIND_CUSTOM_SIZE, process_shared_preload_libraries_in_progress, and TopMemoryContext.

Referenced by _PG_init().

◆ pgstat_report_stat()

long pgstat_report_stat ( bool  force)

Definition at line 704 of file pgstat.c.

706{
707 static TimestampTz pending_since = 0;
708 static TimestampTz last_flush = 0;
709 bool partial_flush;
711 bool nowait;
712
715
716 /* "absorb" the forced flush even if there's nothing to flush */
718 {
719 force = true;
720 pgStatForceNextFlush = false;
721 }
722
723 /* Don't expend a clock check if nothing to do */
726 {
727 return 0;
728 }
729
730 /*
731 * There should never be stats to report once stats are shut down. Can't
732 * assert that before the checks above, as there is an unconditional
733 * pgstat_report_stat() call in pgstat_shutdown_hook() - which at least
734 * the process that ran pgstat_before_server_shutdown() will still call.
735 */
737
738 if (force)
739 {
740 /*
741 * Stats reports are forced either when it's been too long since stats
742 * have been reported or in processes that force stats reporting to
743 * happen at specific points (including shutdown). In the former case
744 * the transaction stop time might be quite old, in the latter it
745 * would never get cleared.
746 */
748 }
749 else
750 {
752
753 if (pending_since > 0 &&
755 {
756 /* don't keep pending updates longer than PGSTAT_MAX_INTERVAL */
757 force = true;
758 }
759 else if (last_flush > 0 &&
761 {
762 /* don't flush too frequently */
763 if (pending_since == 0)
765
767 }
768 }
769
771
772 /* don't wait for lock acquisition when !force */
773 nowait = !force;
774
775 partial_flush = false;
776
777 /* flush of variable-numbered stats tracked in pending entries list */
779
780 /* flush of other stats kinds */
782 {
783 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
784 {
786
787 if (!kind_info)
788 continue;
789 if (!kind_info->flush_static_cb)
790 continue;
791
792 partial_flush |= kind_info->flush_static_cb(nowait);
793 }
794 }
795
796 last_flush = now;
797
798 /*
799 * If some of the pending stats could not be flushed due to lock
800 * contention, let the caller know when to retry.
801 */
802 if (partial_flush)
803 {
804 /* force should have prevented us from getting here */
805 Assert(!force);
806
807 /* remember since when stats have been pending */
808 if (pending_since == 0)
810
812 }
813
814 pending_since = 0;
815 pgstat_report_fixed = false;
816
817 return 0;

References Assert, dlist_is_empty(), fb(), GetCurrentTimestamp(), GetCurrentTransactionStopTimestamp(), PgStat_ShmemControl::is_shutdown, IsTransactionOrTransactionBlock(), now(), pgstat_assert_is_up, pgstat_flush_pending_entries(), pgstat_get_kind_info(), PGSTAT_IDLE_INTERVAL, PgStat_Kind, PGSTAT_KIND_MAX, PGSTAT_KIND_MIN, PGSTAT_MAX_INTERVAL, PGSTAT_MIN_INTERVAL, pgstat_report_fixed, pgstat_update_dbstats(), pgStatForceNextFlush, pgStatLocal, pgStatPending, PgStat_LocalState::shmem, and TimestampDifferenceExceeds().

Referenced by AllTablesyncsReady(), apply_handle_commit_internal(), apply_handle_commit_prepared(), apply_handle_prepare(), apply_handle_rollback_prepared(), apply_handle_stream_prepare(), FinishSyncWorker(), HasSubscriptionTablesCached(), LogicalRepApplyLoop(), LogicalRepSyncTableStart(), pgstat_before_server_shutdown(), pgstat_shutdown_hook(), PostgresMain(), ProcessInterrupts(), ProcessSequencesForSync(), ProcessSyncingTablesForApply(), ProcessSyncingTablesForSync(), standby_redo(), and worker_spi_main().

◆ pgstat_reset()

void pgstat_reset ( PgStat_Kind  kind,
Oid  dboid,
uint64  objid 
)

Definition at line 864 of file pgstat.c.

866{
869
870 /* not needed atm, and doesn't make sense with the current signature */
871 Assert(!pgstat_get_kind_info(kind)->fixed_amount);
872
873 /* reset the "single counter" */
874 pgstat_reset_entry(kind, dboid, objid, ts);
875
876 if (!kind_info->accessed_across_databases)

References Assert, fb(), GetCurrentTimestamp(), pgstat_get_kind_info(), pgstat_reset_database_timestamp(), and pgstat_reset_entry().

Referenced by pg_stat_reset_backend_stats(), pg_stat_reset_single_function_counters(), pg_stat_reset_single_table_counters(), pg_stat_reset_subscription_stats(), pgstat_create_transactional(), and pgstat_reset_replslot().

◆ pgstat_reset_after_failure()

static void pgstat_reset_after_failure ( void  )
static

Definition at line 2088 of file pgstat.c.

2090{
2092
2093 /* reset fixed-numbered stats */
2094 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
2095 {
2097
2098 if (!kind_info || !kind_info->fixed_amount)
2099 continue;
2100
2101 kind_info->reset_all_cb(ts);
2102 }
2103
2104 /* and drop variable-numbered ones */

References fb(), GetCurrentTimestamp(), pgstat_drop_all_entries(), pgstat_get_kind_info(), PgStat_Kind, PGSTAT_KIND_MAX, and PGSTAT_KIND_MIN.

Referenced by pgstat_discard_stats(), and pgstat_read_statsfile().

◆ pgstat_reset_counters()

◆ pgstat_reset_of_kind()

◆ pgstat_restore_stats()

void pgstat_restore_stats ( void  )

Definition at line 507 of file pgstat.c.

509{

References pgstat_read_statsfile().

Referenced by StartupXLOG().

◆ pgstat_shutdown_hook()

static void pgstat_shutdown_hook ( int  code,
Datum  arg 
)
static

Definition at line 613 of file pgstat.c.

615{
618
619 /*
620 * If we got as far as discovering our own database ID, we can flush out
621 * what we did so far. Otherwise, we'd be reporting an invalid database
622 * ID, so forget it. (This means that accesses to pg_database during
623 * failed backend starts might never get counted.)
624 */
627
628 pgstat_report_stat(true);
629
630 /* there shouldn't be any pending changes left */
633
634 /* drop the backend stats entry */
637
639
640#ifdef USE_ASSERT_CHECKING
641 pgstat_is_shutdown = true;
642#endif

References Assert, dlist_init(), dlist_is_empty(), fb(), InvalidOid, IsPostmasterEnvironment, IsUnderPostmaster, MyDatabaseId, MyProcNumber, OidIsValid, pgstat_detach_shmem(), pgstat_drop_entry(), PGSTAT_KIND_BACKEND, pgstat_report_disconnect(), pgstat_report_stat(), pgstat_request_entry_refs_gc(), and pgStatPending.

Referenced by pgstat_initialize().

◆ pgstat_snapshot_fixed()

◆ pgstat_write_chunk()

void pgstat_write_chunk ( FILE fpout,
void ptr,
size_t  len 
)

Definition at line 1565 of file pgstat.c.

1567{
1568 int rc;
1569
1570 rc = fwrite(ptr, len, 1, fpout);
1571
1572 /* We check for errors with ferror() when done writing the stats. */
1573 (void) rc;

References fb(), and len.

Referenced by pgstat_write_statsfile(), and test_custom_stats_var_to_serialized_data().

◆ pgstat_write_statsfile()

static void pgstat_write_statsfile ( void  )
static

Definition at line 1580 of file pgstat.c.

1582{
1583 FILE *fpout;
1589
1591
1592 /* should be called only by the checkpointer or single user mode */
1594
1595 /* we're shutting down, so it's ok to just override this */
1597
1598 elog(DEBUG2, "writing stats file \"%s\"", statfile);
1599
1600 /*
1601 * Open the statistics temp file to write out the current values.
1602 */
1604 if (fpout == NULL)
1605 {
1606 ereport(LOG,
1608 errmsg("could not open temporary statistics file \"%s\": %m",
1609 tmpfile)));
1610 return;
1611 }
1612
1613 /*
1614 * Write the file header --- currently just a format ID.
1615 */
1618
1619 /* Write various stats structs for fixed number of objects */
1620 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
1621 {
1622 char *ptr;
1623 const PgStat_KindInfo *info = pgstat_get_kind_info(kind);
1624
1625 if (!info || !info->fixed_amount)
1626 continue;
1627
1628 if (pgstat_is_kind_builtin(kind))
1629 Assert(info->snapshot_ctl_off != 0);
1630
1631 /* skip if no need to write to file */
1632 if (!info->write_to_file)
1633 continue;
1634
1636 if (pgstat_is_kind_builtin(kind))
1637 ptr = ((char *) &pgStatLocal.snapshot) + info->snapshot_ctl_off;
1638 else
1640
1644 }
1645
1646 /*
1647 * Walk through the stats entries
1648 */
1650 while ((ps = dshash_seq_next(&hstat)) != NULL)
1651 {
1654
1656
1657 /*
1658 * We should not see any "dropped" entries when writing the stats
1659 * file, as all backends and auxiliary processes should have cleaned
1660 * up their references before they terminated.
1661 *
1662 * However, since we are already shutting down, it is not worth
1663 * crashing the server over any potential cleanup issues, so we simply
1664 * skip such entries if encountered.
1665 */
1666 Assert(!ps->dropped);
1667 if (ps->dropped)
1668 continue;
1669
1670 /*
1671 * This discards data related to custom stats kinds that are unknown
1672 * to this process.
1673 */
1674 if (!pgstat_is_kind_valid(ps->key.kind))
1675 {
1676 elog(WARNING, "found unknown stats entry %u/%u/%" PRIu64,
1677 ps->key.kind, ps->key.dboid,
1678 ps->key.objid);
1679 continue;
1680 }
1681
1683
1684 kind_info = pgstat_get_kind_info(ps->key.kind);
1685
1686 /* if not dropped the valid-entry refcount should exist */
1687 Assert(pg_atomic_read_u32(&ps->refcount) > 0);
1688
1689 /* skip if no need to write to file */
1690 if (!kind_info->write_to_file)
1691 continue;
1692
1693 if (!kind_info->to_serialized_name)
1694 {
1695 /* normal stats entry, identified by PgStat_HashKey */
1698 }
1699 else
1700 {
1701 /* stats entry identified by name on disk (e.g. slots) */
1702 NameData name;
1703
1704 kind_info->to_serialized_name(&ps->key, shstats, &name);
1705
1707 pgstat_write_chunk_s(fpout, &ps->key.kind);
1709 }
1710
1711 /* Write except the header part of the entry */
1713 pgstat_get_entry_data(ps->key.kind, shstats),
1714 pgstat_get_entry_len(ps->key.kind));
1715
1716 /* Write more data for the entry, if required */
1717 if (kind_info->to_serialized_data)
1718 kind_info->to_serialized_data(&ps->key, shstats, fpout);
1719 }
1721
1722 /*
1723 * No more output to be done. Close the temp file and replace the old
1724 * pgstat.stat with it. The ferror() check replaces testing for error
1725 * after each individual fputc or fwrite (in pgstat_write_chunk()) above.
1726 */
1728
1729 if (ferror(fpout))
1730 {
1731 ereport(LOG,
1733 errmsg("could not write temporary statistics file \"%s\": %m",
1734 tmpfile)));
1735 FreeFile(fpout);
1736 unlink(tmpfile);
1737 }
1738 else if (FreeFile(fpout) < 0)
1739 {
1740 ereport(LOG,
1742 errmsg("could not close temporary statistics file \"%s\": %m",
1743 tmpfile)));
1744 unlink(tmpfile);
1745 }
1746 else if (durable_rename(tmpfile, statfile, LOG) < 0)
1747 {
1748 /* durable_rename already emitted log message */
1749 unlink(tmpfile);
1750 }
1751
1752 /* Finish callbacks, if required */
1753 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
1754 {
1756
1757 if (kind_info && kind_info->finish)
1758 kind_info->finish(STATS_WRITE);
1759 }

References AllocateFile(), Assert, B_CHECKPOINTER, CHECK_FOR_INTERRUPTS, PgStat_Snapshot::custom_data, DEBUG2, PgStat_LocalState::dsa, dsa_get_address(), dshash_seq_init(), dshash_seq_next(), dshash_seq_term(), durable_rename(), elog, ereport, errcode_for_file_access(), errmsg(), fb(), PgStat_KindInfo::fixed_amount, FreeFile(), IsUnderPostmaster, LOG, MyBackendType, name, pg_atomic_read_u32(), PG_BINARY_W, pgstat_assert_is_up, pgstat_build_snapshot_fixed(), pgstat_fetch_consistency, PGSTAT_FETCH_CONSISTENCY_NONE, PGSTAT_FILE_ENTRY_END, PGSTAT_FILE_ENTRY_FIXED, PGSTAT_FILE_ENTRY_HASH, PGSTAT_FILE_ENTRY_NAME, PGSTAT_FILE_FORMAT_ID, pgstat_get_entry_data(), pgstat_get_entry_len(), pgstat_get_kind_info(), pgstat_is_kind_builtin(), pgstat_is_kind_valid(), PgStat_Kind, PGSTAT_KIND_CUSTOM_MIN, PGSTAT_KIND_MAX, PGSTAT_KIND_MIN, PGSTAT_STAT_PERMANENT_FILENAME, PGSTAT_STAT_PERMANENT_TMPFILE, pgstat_write_chunk(), pgstat_write_chunk_s, pgStatLocal, ps, PgStat_KindInfo::shared_data_len, PgStat_LocalState::shared_hash, PgStat_LocalState::snapshot, PgStat_KindInfo::snapshot_ctl_off, STATS_WRITE, WARNING, and PgStat_KindInfo::write_to_file.

Referenced by pgstat_before_server_shutdown().

Variable Documentation

◆ force_stats_snapshot_clear

◆ pgstat_fetch_consistency

◆ pgstat_kind_builtin_infos

const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
static

Definition at line 281 of file pgstat.c.

282 {
283
284 /* stats kinds for variable-numbered objects */
285
287 .name = "database",
288
289 .fixed_amount = false,
290 .write_to_file = true,
291 /* so pg_stat_database entries can be seen in all databases */
292 .accessed_across_databases = true,
293
294 .shared_size = sizeof(PgStatShared_Database),
295 .shared_data_off = offsetof(PgStatShared_Database, stats),
296 .shared_data_len = sizeof(((PgStatShared_Database *) 0)->stats),
297 .pending_size = sizeof(PgStat_StatDBEntry),
298
299 .flush_pending_cb = pgstat_database_flush_cb,
300 .reset_timestamp_cb = pgstat_database_reset_timestamp_cb,
301 },
302
304 .name = "relation",
305
306 .fixed_amount = false,
307 .write_to_file = true,
308
309 .shared_size = sizeof(PgStatShared_Relation),
310 .shared_data_off = offsetof(PgStatShared_Relation, stats),
311 .shared_data_len = sizeof(((PgStatShared_Relation *) 0)->stats),
312 .pending_size = sizeof(PgStat_TableStatus),
313
314 .flush_pending_cb = pgstat_relation_flush_cb,
315 .delete_pending_cb = pgstat_relation_delete_pending_cb,
316 .reset_timestamp_cb = pgstat_relation_reset_timestamp_cb,
317 },
318
320 .name = "function",
321
322 .fixed_amount = false,
323 .write_to_file = true,
324
325 .shared_size = sizeof(PgStatShared_Function),
326 .shared_data_off = offsetof(PgStatShared_Function, stats),
327 .shared_data_len = sizeof(((PgStatShared_Function *) 0)->stats),
328 .pending_size = sizeof(PgStat_FunctionCounts),
329
330 .flush_pending_cb = pgstat_function_flush_cb,
331 .reset_timestamp_cb = pgstat_function_reset_timestamp_cb,
332 },
333
335 .name = "replslot",
336
337 .fixed_amount = false,
338 .write_to_file = true,
339
340 .accessed_across_databases = true,
341
342 .shared_size = sizeof(PgStatShared_ReplSlot),
343 .shared_data_off = offsetof(PgStatShared_ReplSlot, stats),
344 .shared_data_len = sizeof(((PgStatShared_ReplSlot *) 0)->stats),
345
346 .reset_timestamp_cb = pgstat_replslot_reset_timestamp_cb,
347 .to_serialized_name = pgstat_replslot_to_serialized_name_cb,
348 .from_serialized_name = pgstat_replslot_from_serialized_name_cb,
349 },
350
352 .name = "subscription",
353
354 .fixed_amount = false,
355 .write_to_file = true,
356 /* so pg_stat_subscription_stats entries can be seen in all databases */
357 .accessed_across_databases = true,
358
359 .shared_size = sizeof(PgStatShared_Subscription),
360 .shared_data_off = offsetof(PgStatShared_Subscription, stats),
361 .shared_data_len = sizeof(((PgStatShared_Subscription *) 0)->stats),
362 .pending_size = sizeof(PgStat_BackendSubEntry),
363
364 .flush_pending_cb = pgstat_subscription_flush_cb,
365 .reset_timestamp_cb = pgstat_subscription_reset_timestamp_cb,
366 },
367
369 .name = "backend",
370
371 .fixed_amount = false,
372 .write_to_file = false,
373
374 .accessed_across_databases = true,
375
376 .shared_size = sizeof(PgStatShared_Backend),
377 .shared_data_off = offsetof(PgStatShared_Backend, stats),
378 .shared_data_len = sizeof(((PgStatShared_Backend *) 0)->stats),
379
380 .flush_static_cb = pgstat_backend_flush_cb,
381 .reset_timestamp_cb = pgstat_backend_reset_timestamp_cb,
382 },
383
384 /* stats for fixed-numbered (mostly 1) objects */
385
387 .name = "archiver",
388
389 .fixed_amount = true,
390 .write_to_file = true,
391
392 .snapshot_ctl_off = offsetof(PgStat_Snapshot, archiver),
393 .shared_ctl_off = offsetof(PgStat_ShmemControl, archiver),
394 .shared_data_off = offsetof(PgStatShared_Archiver, stats),
395 .shared_data_len = sizeof(((PgStatShared_Archiver *) 0)->stats),
396
397 .init_shmem_cb = pgstat_archiver_init_shmem_cb,
398 .reset_all_cb = pgstat_archiver_reset_all_cb,
399 .snapshot_cb = pgstat_archiver_snapshot_cb,
400 },
401
403 .name = "bgwriter",
404
405 .fixed_amount = true,
406 .write_to_file = true,
407
408 .snapshot_ctl_off = offsetof(PgStat_Snapshot, bgwriter),
409 .shared_ctl_off = offsetof(PgStat_ShmemControl, bgwriter),
410 .shared_data_off = offsetof(PgStatShared_BgWriter, stats),
411 .shared_data_len = sizeof(((PgStatShared_BgWriter *) 0)->stats),
412
413 .init_shmem_cb = pgstat_bgwriter_init_shmem_cb,
414 .reset_all_cb = pgstat_bgwriter_reset_all_cb,
415 .snapshot_cb = pgstat_bgwriter_snapshot_cb,
416 },
417
419 .name = "checkpointer",
420
421 .fixed_amount = true,
422 .write_to_file = true,
423
424 .snapshot_ctl_off = offsetof(PgStat_Snapshot, checkpointer),
425 .shared_ctl_off = offsetof(PgStat_ShmemControl, checkpointer),
426 .shared_data_off = offsetof(PgStatShared_Checkpointer, stats),
427 .shared_data_len = sizeof(((PgStatShared_Checkpointer *) 0)->stats),
428
429 .init_shmem_cb = pgstat_checkpointer_init_shmem_cb,
430 .reset_all_cb = pgstat_checkpointer_reset_all_cb,
431 .snapshot_cb = pgstat_checkpointer_snapshot_cb,
432 },
433
434 [PGSTAT_KIND_IO] = {
435 .name = "io",
436
437 .fixed_amount = true,
438 .write_to_file = true,
439
440 .snapshot_ctl_off = offsetof(PgStat_Snapshot, io),
441 .shared_ctl_off = offsetof(PgStat_ShmemControl, io),
442 .shared_data_off = offsetof(PgStatShared_IO, stats),
443 .shared_data_len = sizeof(((PgStatShared_IO *) 0)->stats),
444
445 .flush_static_cb = pgstat_io_flush_cb,
446 .init_shmem_cb = pgstat_io_init_shmem_cb,
447 .reset_all_cb = pgstat_io_reset_all_cb,
448 .snapshot_cb = pgstat_io_snapshot_cb,
449 },
450
451 [PGSTAT_KIND_SLRU] = {
452 .name = "slru",
453
454 .fixed_amount = true,
455 .write_to_file = true,
456
457 .snapshot_ctl_off = offsetof(PgStat_Snapshot, slru),
458 .shared_ctl_off = offsetof(PgStat_ShmemControl, slru),
459 .shared_data_off = offsetof(PgStatShared_SLRU, stats),
460 .shared_data_len = sizeof(((PgStatShared_SLRU *) 0)->stats),
461
462 .flush_static_cb = pgstat_slru_flush_cb,
463 .init_shmem_cb = pgstat_slru_init_shmem_cb,
464 .reset_all_cb = pgstat_slru_reset_all_cb,
465 .snapshot_cb = pgstat_slru_snapshot_cb,
466 },
467
468 [PGSTAT_KIND_WAL] = {
469 .name = "wal",
470
471 .fixed_amount = true,
472 .write_to_file = true,
473
474 .snapshot_ctl_off = offsetof(PgStat_Snapshot, wal),
475 .shared_ctl_off = offsetof(PgStat_ShmemControl, wal),
476 .shared_data_off = offsetof(PgStatShared_Wal, stats),
477 .shared_data_len = sizeof(((PgStatShared_Wal *) 0)->stats),
478
479 .init_backend_cb = pgstat_wal_init_backend_cb,
480 .flush_static_cb = pgstat_wal_flush_cb,
481 .init_shmem_cb = pgstat_wal_init_shmem_cb,
482 .reset_all_cb = pgstat_wal_reset_all_cb,
483 .snapshot_cb = pgstat_wal_snapshot_cb,
484 },

Referenced by pgstat_get_kind_from_str(), and pgstat_get_kind_info().

◆ pgstat_kind_custom_infos

const PgStat_KindInfo** pgstat_kind_custom_infos = NULL
static

Definition at line 494 of file pgstat.c.

Referenced by pgstat_get_kind_from_str(), pgstat_get_kind_info(), and pgstat_register_kind().

◆ pgstat_report_fixed

◆ pgstat_track_counts

◆ pgStatForceNextFlush

bool pgStatForceNextFlush = false
static

Definition at line 249 of file pgstat.c.

Referenced by pgstat_force_next_flush(), and pgstat_report_stat().

◆ pgStatLocal

PgStat_LocalState pgStatLocal

Definition at line 212 of file pgstat.c.

Referenced by pgstat_acquire_entry_ref(), pgstat_archiver_reset_all_cb(), pgstat_archiver_snapshot_cb(), pgstat_attach_shmem(), pgstat_before_server_shutdown(), pgstat_bgwriter_reset_all_cb(), pgstat_bgwriter_snapshot_cb(), pgstat_build_snapshot(), pgstat_build_snapshot_fixed(), pgstat_checkpointer_reset_all_cb(), pgstat_checkpointer_snapshot_cb(), pgstat_clear_snapshot(), pgstat_detach_shmem(), pgstat_drop_database_and_contents(), pgstat_drop_entry(), pgstat_drop_entry_internal(), pgstat_drop_matching_entries(), pgstat_fetch_entry(), pgstat_fetch_slru(), pgstat_fetch_stat_archiver(), pgstat_fetch_stat_bgwriter(), pgstat_fetch_stat_checkpointer(), pgstat_fetch_stat_io(), pgstat_fetch_stat_wal(), pgstat_free_entry(), pgstat_gc_entry_refs(), pgstat_get_custom_shmem_data(), pgstat_get_custom_snapshot_data(), pgstat_get_entry_count(), pgstat_get_entry_ref(), pgstat_get_stat_snapshot_timestamp(), pgstat_init_entry(), pgstat_init_snapshot_fixed(), pgstat_io_flush_cb(), pgstat_io_reset_all_cb(), pgstat_io_snapshot_cb(), pgstat_need_entry_refs_gc(), pgstat_prep_snapshot(), pgstat_read_statsfile(), pgstat_reinit_entry(), pgstat_release_entry_ref(), pgstat_report_archiver(), pgstat_report_bgwriter(), pgstat_report_checkpointer(), pgstat_report_stat(), pgstat_request_entry_refs_gc(), pgstat_reset_matching_entries(), pgstat_reset_slru_counter_internal(), pgstat_setup_shared_refs(), pgstat_slru_flush_cb(), pgstat_slru_snapshot_cb(), pgstat_snapshot_fixed(), pgstat_wal_flush_cb(), pgstat_wal_reset_all_cb(), pgstat_wal_snapshot_cb(), pgstat_write_statsfile(), and StatsShmemInit().

◆ pgStatPending

dlist_head pgStatPending = DLIST_STATIC_INIT(pgStatPending)
static

◆ pgStatPendingContext

MemoryContext pgStatPendingContext = NULL
static

Definition at line 234 of file pgstat.c.

Referenced by pgstat_prep_pending_entry().