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 144 of file pgstat.c.

◆ PGSTAT_FILE_ENTRY_FIXED

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

Definition at line 145 of file pgstat.c.

◆ PGSTAT_FILE_ENTRY_HASH

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

Definition at line 147 of file pgstat.c.

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

◆ PGSTAT_FILE_ENTRY_NAME

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

Definition at line 146 of file pgstat.c.

◆ PGSTAT_IDLE_INTERVAL

#define PGSTAT_IDLE_INTERVAL   10000

Definition at line 131 of file pgstat.c.

◆ PGSTAT_MAX_INTERVAL

#define PGSTAT_MAX_INTERVAL   60000

Definition at line 129 of file pgstat.c.

◆ PGSTAT_MIN_INTERVAL

#define PGSTAT_MIN_INTERVAL   1000

Definition at line 127 of file pgstat.c.

◆ PGSTAT_SNAPSHOT_HASH_SIZE

#define PGSTAT_SNAPSHOT_HASH_SIZE   512

Definition at line 138 of file pgstat.c.

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 174 of file pgstat.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 173 of file pgstat.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   PgStat_SnapshotEntry

Definition at line 165 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 170 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 168 of file pgstat.c.

◆ SH_KEY

#define SH_KEY   key

Definition at line 167 of file pgstat.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   PgStat_HashKey

Definition at line 166 of file pgstat.c.

◆ SH_PREFIX

#define SH_PREFIX   pgstat_snapshot

Definition at line 164 of file pgstat.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 172 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 2129 of file pgstat.c.

2131{
2132 /*
2133 * Changing this value in a transaction may cause snapshot state
2134 * inconsistencies, so force a clear of the current snapshot on the next
2135 * snapshot build attempt.
2136 */

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 851 of file pgstat.c.

853{
854 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 590 of file pgstat.c.

592{
595
596 /*
597 * Stats should only be reported after pgstat_initialize() and before
598 * pgstat_shutdown(). This is a convenient point to catch most violations
599 * of this rule.
600 */
602
603 /* flush out our own pending changes before writing out */
604 pgstat_report_stat(true);
605
606 /*
607 * Only write out file during normal shutdown. Don't even signal that
608 * we've shutdown during irregular shutdowns, because the shutdown
609 * sequence isn't coordinated to ensure this backend shuts down last.
610 */
611 if (code == 0)
612 {
615 }

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 1148 of file pgstat.c.

1150{
1153
1154 /* should only be called when we need a snapshot */
1156
1157 /* snapshot already built */
1159 return;
1160
1162
1163 Assert(pgStatLocal.snapshot.stats->members == 0);
1164
1166
1167 /*
1168 * Snapshot all variable stats.
1169 */
1171 while ((p = dshash_seq_next(&hstat)) != NULL)
1172 {
1173 PgStat_Kind kind = p->key.kind;
1175 bool found;
1176 PgStat_SnapshotEntry *entry;
1178
1179 /*
1180 * Check if the stats object should be included in the snapshot.
1181 * Unless the stats kind can be accessed from all databases (e.g.,
1182 * database stats themselves), we only include stats for the current
1183 * database or objects not associated with a database (e.g. shared
1184 * relations).
1185 */
1186 if (p->key.dboid != MyDatabaseId &&
1187 p->key.dboid != InvalidOid &&
1188 !kind_info->accessed_across_databases)
1189 continue;
1190
1191 if (p->dropped)
1192 continue;
1193
1195
1198
1200 Assert(!found);
1201
1203 pgstat_get_entry_len(kind));
1204
1205 /*
1206 * Acquire the LWLock directly instead of using
1207 * pg_stat_lock_entry_shared() which requires a reference.
1208 */
1210 memcpy(entry->data,
1212 pgstat_get_entry_len(kind));
1213 LWLockRelease(&stats_data->lock);
1214 }
1216
1217 /*
1218 * Build snapshot of all fixed-numbered stats.
1219 */
1220 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
1221 {
1223
1224 if (!kind_info)
1225 continue;
1226 if (!kind_info->fixed_amount)
1227 {
1228 Assert(kind_info->snapshot_cb == NULL);
1229 continue;
1230 }
1231
1233 }
1234

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(), memcpy(), 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 1238 of file pgstat.c.

1240{
1242 int idx;
1243 bool *valid;
1244
1245 /* Position in fixed_valid or custom_valid */
1246 if (pgstat_is_kind_builtin(kind))
1247 {
1248 idx = kind;
1250 }
1251 else
1252 {
1253 idx = kind - PGSTAT_KIND_CUSTOM_MIN;
1255 }
1256
1257 Assert(kind_info->fixed_amount);
1258 Assert(kind_info->snapshot_cb != NULL);
1259
1261 {
1262 /* rebuild every time */
1263 valid[idx] = false;
1264 }
1265 else if (valid[idx])
1266 {
1267 /* in snapshot mode we shouldn't get called again */
1269 return;
1270 }
1271
1272 Assert(!valid[idx]);
1273
1274 kind_info->snapshot_cb();
1275
1276 Assert(!valid[idx]);
1277 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 930 of file pgstat.c.

932{
934
941
942 /* Release memory, if any was allocated */
944 {
946
947 /* Reset variables */
949 }
950
951 /*
952 * Historically the backend_status.c facilities lived in this file, and
953 * were reset with the same function. For now keep it that way, and
954 * forward the reset request.
955 */
957
958 /* 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 1344 of file pgstat.c.

1346{
1347 PgStat_Kind kind = entry_ref->shared_entry->key.kind;
1349 void *pending_data = entry_ref->pending;
1350
1352 /* !fixed_amount stats should be handled explicitly */
1353 Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1354
1355 if (kind_info->delete_pending_cb)
1356 kind_info->delete_pending_cb(entry_ref);
1357
1359 entry_ref->pending = NULL;
1360
1361 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 537 of file pgstat.c.

539{
540 int ret;
541
542 /* NB: this needs to be done even in single user mode */
543
544 /* First, cleanup the main pgstats file */
546 if (ret != 0)
547 {
548 if (errno == ENOENT)
549 elog(DEBUG2,
550 "didn't need to unlink permanent stats file \"%s\" - didn't exist",
552 else
553 ereport(LOG,
555 errmsg("could not unlink permanent statistics file \"%s\": %m",
557 }
558 else
559 {
562 errmsg_internal("unlinked permanent statistics file \"%s\"",
564 }
565
566 /* Finish callbacks, if required */
567 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
568 {
570
571 if (kind_info && kind_info->finish)
572 kind_info->finish(STATS_DISCARD);
573 }
574
575 /*
576 * Reset stats contents. This will set reset timestamps of fixed-numbered
577 * stats to the current time (no variable stats exist).
578 */

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 962 of file pgstat.c.

964{
965 PgStat_HashKey key = {0};
966 PgStat_EntryRef *entry_ref;
967 void *stats_data;
969
970 /* should be called from backends */
972 Assert(!kind_info->fixed_amount);
973
975
976 key.kind = kind;
977 key.dboid = dboid;
978 key.objid = objid;
979
980 /* if we need to build a full snapshot, do so */
983
984 /* if caching is desired, look up in cache */
986 {
987 PgStat_SnapshotEntry *entry = NULL;
988
990
991 if (entry)
992 return entry->data;
993
994 /*
995 * If we built a full snapshot and the key is not in
996 * pgStatLocal.snapshot.stats, there are no matching stats.
997 */
999 return NULL;
1000 }
1001
1003
1004 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
1005
1006 if (entry_ref == NULL || entry_ref->shared_entry->dropped)
1007 {
1008 /* create empty entry when using PGSTAT_FETCH_CONSISTENCY_CACHE */
1010 {
1011 PgStat_SnapshotEntry *entry = NULL;
1012 bool found;
1013
1014 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
1015 Assert(!found);
1016 entry->data = NULL;
1017 }
1018 return NULL;
1019 }
1020
1021 /*
1022 * Allocate in caller's context for PGSTAT_FETCH_CONSISTENCY_NONE,
1023 * otherwise we could quickly end up with a fair bit of memory used due to
1024 * repeated accesses.
1025 */
1027 stats_data = palloc(kind_info->shared_data_len);
1028 else
1030 kind_info->shared_data_len);
1031
1032 (void) pgstat_lock_entry_shared(entry_ref, false);
1034 pgstat_get_entry_data(kind, entry_ref->shared_stats),
1035 kind_info->shared_data_len);
1036 pgstat_unlock_entry(entry_ref);
1037
1039 {
1040 PgStat_SnapshotEntry *entry = NULL;
1041 bool found;
1042
1043 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
1044 entry->data = stats_data;
1045 }
1046
1047 return stats_data;

References Assert, PgStat_Snapshot::context, PgStat_SnapshotEntry::data, PgStatShared_HashEntry::dropped, fb(), IsPostmasterEnvironment, IsUnderPostmaster, memcpy(), 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 1331 of file pgstat.c.

1333{
1334 PgStat_EntryRef *entry_ref;
1335
1336 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
1337
1338 if (entry_ref == NULL || entry_ref->pending == NULL)
1339 return NULL;
1340
1341 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 1367 of file pgstat.c.

1369{
1370 bool have_pending = false;
1371 dlist_node *cur = NULL;
1372
1373 /*
1374 * Need to be a bit careful iterating over the list of pending entries.
1375 * Processing a pending entry may queue further pending entries to the end
1376 * of the list that we want to process, so a simple iteration won't do.
1377 * Further complicating matters is that we want to delete the current
1378 * entry in each iteration from the list if we flushed successfully.
1379 *
1380 * So we just keep track of the next pointer in each loop iteration.
1381 */
1384
1385 while (cur)
1386 {
1387 PgStat_EntryRef *entry_ref =
1388 dlist_container(PgStat_EntryRef, pending_node, cur);
1389 PgStat_HashKey key = entry_ref->shared_entry->key;
1390 PgStat_Kind kind = key.kind;
1392 bool did_flush;
1394
1395 Assert(!kind_info->fixed_amount);
1396 Assert(kind_info->flush_pending_cb != NULL);
1397
1398 /* flush the stats, if possible */
1399 did_flush = kind_info->flush_pending_cb(entry_ref, nowait);
1400
1401 Assert(did_flush || nowait);
1402
1403 /* determine next entry, before deleting the pending entry */
1406 else
1407 next = NULL;
1408
1409 /* if successfully flushed, remove entry */
1410 if (did_flush)
1411 pgstat_delete_pending_entry(entry_ref);
1412 else
1413 have_pending = true;
1414
1415 cur = next;
1416 }
1417
1419
1420 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 842 of file pgstat.c.

844{

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 1429 of file pgstat.c.

1431{
1432 for (PgStat_Kind kind = PGSTAT_KIND_BUILTIN_MIN; kind <= PGSTAT_KIND_BUILTIN_MAX; kind++)
1433 {
1435 return kind;
1436 }
1437
1438 /* Check the custom set of cumulative stats */
1440 {
1441 for (PgStat_Kind kind = PGSTAT_KIND_CUSTOM_MIN; kind <= PGSTAT_KIND_CUSTOM_MAX; kind++)
1442 {
1444
1447 return kind;
1448 }
1449 }
1450
1451 ereport(ERROR,
1453 errmsg("invalid statistics kind: \"%s\"", kind_str)));
1454 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 1072 of file pgstat.c.

1074{
1075 /* fixed-numbered stats always exist */
1076 if (pgstat_get_kind_info(kind)->fixed_amount)
1077 return true;
1078
1079 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 1108 of file pgstat.c.

1110{
1111 /*
1112 * Initialize fixed-numbered statistics data in snapshots, only for custom
1113 * stats kinds.
1114 */
1115 for (PgStat_Kind kind = PGSTAT_KIND_CUSTOM_MIN; kind <= PGSTAT_KIND_CUSTOM_MAX; kind++)
1116 {
1118
1119 if (!kind_info || !kind_info->fixed_amount)
1120 continue;
1121
1124 }

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 669 of file pgstat.c.

671{
673
675
677
678 /* Backend initialization callbacks */
679 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
680 {
682
683 if (kind_info == NULL || kind_info->init_backend_cb == NULL)
684 continue;
685
686 kind_info->init_backend_cb();
687 }
688
689 /* Set up a process-exit hook to clean up */
691
692#ifdef USE_ASSERT_CHECKING
694#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 1293 of file pgstat.c.

1295{
1296 PgStat_EntryRef *entry_ref;
1297
1298 /* need to be able to flush out */
1299 Assert(pgstat_get_kind_info(kind)->flush_pending_cb != NULL);
1300
1302 {
1305 "PgStat Pending",
1307 }
1308
1309 entry_ref = pgstat_get_entry_ref(kind, dboid, objid,
1310 true, created_entry);
1311
1312 if (entry_ref->pending == NULL)
1313 {
1314 size_t entrysize = pgstat_get_kind_info(kind)->pending_size;
1315
1316 Assert(entrysize != (size_t) -1);
1317
1318 entry_ref->pending = MemoryContextAllocZero(pgStatPendingContext, entrysize);
1320 }
1321
1322 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 1781 of file pgstat.c.

1783{
1784 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 1793 of file pgstat.c.

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

1493{
1495
1496 if (kind_info->name == NULL || strlen(kind_info->name) == 0)
1497 ereport(ERROR,
1498 (errmsg("custom cumulative statistics name is invalid"),
1499 errhint("Provide a non-empty name for the custom cumulative statistics.")));
1500
1501 if (!pgstat_is_kind_custom(kind))
1502 ereport(ERROR, (errmsg("custom cumulative statistics ID %u is out of range", kind),
1503 errhint("Provide a custom cumulative statistics ID between %u and %u.",
1505
1507 ereport(ERROR,
1508 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1509 errdetail("Custom cumulative statistics must be registered while initializing modules in \"shared_preload_libraries\".")));
1510
1511 /*
1512 * Check some data for fixed-numbered stats.
1513 */
1514 if (kind_info->fixed_amount)
1515 {
1516 if (kind_info->shared_size == 0)
1517 ereport(ERROR,
1518 (errmsg("custom cumulative statistics property is invalid"),
1519 errhint("Custom cumulative statistics require a shared memory size for fixed-numbered objects.")));
1520 if (kind_info->track_entry_count)
1521 ereport(ERROR,
1522 (errmsg("custom cumulative statistics property is invalid"),
1523 errhint("Custom cumulative statistics cannot use entry count tracking for fixed-numbered objects.")));
1524 }
1525
1526 /*
1527 * If pgstat_kind_custom_infos is not available yet, allocate it.
1528 */
1530 {
1534 }
1535
1538 ereport(ERROR,
1539 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1540 errdetail("Custom cumulative statistics \"%s\" already registered with the same ID.",
1542
1543 /* check for existing custom stats with the same name */
1545 {
1547
1549 continue;
1551 ereport(ERROR,
1552 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1553 errdetail("Existing cumulative statistics with ID %u has the same name.", existing_kind)));
1554 }
1555
1556 /* Register it */
1558 ereport(LOG,
1559 (errmsg("registered custom cumulative statistics \"%s\" with ID %u",
1560 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 722 of file pgstat.c.

724{
725 static TimestampTz pending_since = 0;
726 static TimestampTz last_flush = 0;
727 bool partial_flush;
729 bool nowait;
730
733
734 /* "absorb" the forced flush even if there's nothing to flush */
736 {
737 force = true;
738 pgStatForceNextFlush = false;
739 }
740
741 /* Don't expend a clock check if nothing to do */
744 {
745 return 0;
746 }
747
748 /*
749 * There should never be stats to report once stats are shut down. Can't
750 * assert that before the checks above, as there is an unconditional
751 * pgstat_report_stat() call in pgstat_shutdown_hook() - which at least
752 * the process that ran pgstat_before_server_shutdown() will still call.
753 */
755
756 if (force)
757 {
758 /*
759 * Stats reports are forced either when it's been too long since stats
760 * have been reported or in processes that force stats reporting to
761 * happen at specific points (including shutdown). In the former case
762 * the transaction stop time might be quite old, in the latter it
763 * would never get cleared.
764 */
766 }
767 else
768 {
770
771 if (pending_since > 0 &&
773 {
774 /* don't keep pending updates longer than PGSTAT_MAX_INTERVAL */
775 force = true;
776 }
777 else if (last_flush > 0 &&
779 {
780 /* don't flush too frequently */
781 if (pending_since == 0)
783
785 }
786 }
787
789
790 /* don't wait for lock acquisition when !force */
791 nowait = !force;
792
793 partial_flush = false;
794
795 /* flush of variable-numbered stats tracked in pending entries list */
797
798 /* flush of other stats kinds */
800 {
801 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
802 {
804
805 if (!kind_info)
806 continue;
807 if (!kind_info->flush_static_cb)
808 continue;
809
810 partial_flush |= kind_info->flush_static_cb(nowait);
811 }
812 }
813
814 last_flush = now;
815
816 /*
817 * If some of the pending stats could not be flushed due to lock
818 * contention, let the caller know when to retry.
819 */
820 if (partial_flush)
821 {
822 /* force should have prevented us from getting here */
823 Assert(!force);
824
825 /* remember since when stats have been pending */
826 if (pending_since == 0)
828
830 }
831
832 pending_since = 0;
833 pgstat_report_fixed = false;
834
835 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 882 of file pgstat.c.

884{
887
888 /* not needed atm, and doesn't make sense with the current signature */
889 Assert(!pgstat_get_kind_info(kind)->fixed_amount);
890
891 /* reset the "single counter" */
892 pgstat_reset_entry(kind, dboid, objid, ts);
893
894 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 2106 of file pgstat.c.

2108{
2110
2111 /* reset fixed-numbered stats */
2112 for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
2113 {
2115
2116 if (!kind_info || !kind_info->fixed_amount)
2117 continue;
2118
2119 kind_info->reset_all_cb(ts);
2120 }
2121
2122 /* 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 525 of file pgstat.c.

527{

References pgstat_read_statsfile().

Referenced by StartupXLOG().

◆ pgstat_shutdown_hook()

static void pgstat_shutdown_hook ( int  code,
Datum  arg 
)
static

Definition at line 631 of file pgstat.c.

633{
636
637 /*
638 * If we got as far as discovering our own database ID, we can flush out
639 * what we did so far. Otherwise, we'd be reporting an invalid database
640 * ID, so forget it. (This means that accesses to pg_database during
641 * failed backend starts might never get counted.)
642 */
645
646 pgstat_report_stat(true);
647
648 /* there shouldn't be any pending changes left */
651
652 /* drop the backend stats entry */
655
657
658#ifdef USE_ASSERT_CHECKING
659 pgstat_is_shutdown = true;
660#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 1583 of file pgstat.c.

1585{
1586 int rc;
1587
1588 rc = fwrite(ptr, len, 1, fpout);
1589
1590 /* We check for errors with ferror() when done writing the stats. */
1591 (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 1598 of file pgstat.c.

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

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 282 of file pgstat.c.

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

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 512 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 250 of file pgstat.c.

Referenced by pgstat_force_next_flush(), and pgstat_report_stat().

◆ pgStatLocal

PgStat_LocalState pgStatLocal

Definition at line 213 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_lock(), 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_lock_flush_cb(), pgstat_lock_reset_all_cb(), pgstat_lock_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(), StatsShmemInit(), and StatsShmemRequest().

◆ pgStatPending

dlist_head pgStatPending = DLIST_STATIC_INIT(pgStatPending)
static

◆ pgStatPendingContext

MemoryContext pgStatPendingContext = NULL
static

Definition at line 235 of file pgstat.c.

Referenced by pgstat_prep_pending_entry().