PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pgstat_internal.h File Reference
#include "common/hashfn_unstable.h"
#include "lib/dshash.h"
#include "lib/ilist.h"
#include "pgstat.h"
#include "storage/lwlock.h"
#include "utils/dsa.h"
Include dependency graph for pgstat_internal.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PgStat_HashKey
 
struct  PgStatShared_HashEntry
 
struct  PgStatShared_Common
 
struct  PgStat_EntryRef
 
struct  PgStat_SubXactStatus
 
struct  PgStat_KindInfo
 
struct  PgStatShared_Archiver
 
struct  PgStatShared_BgWriter
 
struct  PgStatShared_Checkpointer
 
struct  PgStatShared_IO
 
struct  PgStatShared_SLRU
 
struct  PgStatShared_Wal
 
struct  PgStatShared_Database
 
struct  PgStatShared_Relation
 
struct  PgStatShared_Function
 
struct  PgStatShared_Subscription
 
struct  PgStatShared_ReplSlot
 
struct  PgStatShared_Backend
 
struct  PgStat_ShmemControl
 
struct  PgStat_Snapshot
 
struct  PgStat_LocalState
 

Macros

#define SLRU_NUM_ELEMENTS   lengthof(slru_names)
 
#define pgstat_assert_is_up()   ((void)true)
 
#define PGSTAT_BACKEND_FLUSH_IO   (1 << 0) /* Flush I/O statistics */
 
#define PGSTAT_BACKEND_FLUSH_WAL   (1 << 1) /* Flush WAL statistics */
 
#define PGSTAT_BACKEND_FLUSH_ALL   (PGSTAT_BACKEND_FLUSH_IO | PGSTAT_BACKEND_FLUSH_WAL)
 

Typedefs

typedef struct PgStat_HashKey PgStat_HashKey
 
typedef struct PgStatShared_HashEntry PgStatShared_HashEntry
 
typedef struct PgStatShared_Common PgStatShared_Common
 
typedef struct PgStat_EntryRef PgStat_EntryRef
 
typedef struct PgStat_SubXactStatus PgStat_SubXactStatus
 
typedef struct PgStat_KindInfo PgStat_KindInfo
 
typedef struct PgStatShared_Archiver PgStatShared_Archiver
 
typedef struct PgStatShared_BgWriter PgStatShared_BgWriter
 
typedef struct PgStatShared_Checkpointer PgStatShared_Checkpointer
 
typedef struct PgStatShared_IO PgStatShared_IO
 
typedef struct PgStatShared_SLRU PgStatShared_SLRU
 
typedef struct PgStatShared_Wal PgStatShared_Wal
 
typedef struct PgStatShared_Database PgStatShared_Database
 
typedef struct PgStatShared_Relation PgStatShared_Relation
 
typedef struct PgStatShared_Function PgStatShared_Function
 
typedef struct PgStatShared_Subscription PgStatShared_Subscription
 
typedef struct PgStatShared_ReplSlot PgStatShared_ReplSlot
 
typedef struct PgStatShared_Backend PgStatShared_Backend
 
typedef struct PgStat_ShmemControl PgStat_ShmemControl
 
typedef struct PgStat_Snapshot PgStat_Snapshot
 
typedef struct PgStat_LocalState PgStat_LocalState
 

Functions

static void pgstat_begin_changecount_write (uint32 *cc)
 
static void pgstat_end_changecount_write (uint32 *cc)
 
static uint32 pgstat_begin_changecount_read (uint32 *cc)
 
static bool pgstat_end_changecount_read (uint32 *cc, uint32 cc_before)
 
static void pgstat_copy_changecounted_stats (void *dst, void *src, size_t len, uint32 *cc)
 
static int pgstat_cmp_hash_key (const void *a, const void *b, size_t size, void *arg)
 
static uint32 pgstat_hash_hash_key (const void *d, size_t size, void *arg)
 
static size_t pgstat_get_entry_len (PgStat_Kind kind)
 
static void * pgstat_get_entry_data (PgStat_Kind kind, PgStatShared_Common *entry)
 
static void * pgstat_get_custom_shmem_data (PgStat_Kind kind)
 
static void * pgstat_get_custom_snapshot_data (PgStat_Kind kind)
 
const PgStat_KindInfopgstat_get_kind_info (PgStat_Kind kind)
 
void pgstat_register_kind (PgStat_Kind kind, const PgStat_KindInfo *kind_info)
 
void pgstat_delete_pending_entry (PgStat_EntryRef *entry_ref)
 
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_fetch_entry (PgStat_Kind kind, Oid dboid, uint64 objid)
 
void pgstat_snapshot_fixed (PgStat_Kind kind)
 
void pgstat_archiver_init_shmem_cb (void *stats)
 
void pgstat_archiver_reset_all_cb (TimestampTz ts)
 
void pgstat_archiver_snapshot_cb (void)
 
bool pgstat_flush_backend (bool nowait, bits32 flags)
 
bool pgstat_backend_flush_cb (bool nowait)
 
bool pgstat_backend_have_pending_cb (void)
 
void pgstat_backend_reset_timestamp_cb (PgStatShared_Common *header, TimestampTz ts)
 
void pgstat_bgwriter_init_shmem_cb (void *stats)
 
void pgstat_bgwriter_reset_all_cb (TimestampTz ts)
 
void pgstat_bgwriter_snapshot_cb (void)
 
void pgstat_checkpointer_init_shmem_cb (void *stats)
 
void pgstat_checkpointer_reset_all_cb (TimestampTz ts)
 
void pgstat_checkpointer_snapshot_cb (void)
 
void pgstat_report_disconnect (Oid dboid)
 
void pgstat_update_dbstats (TimestampTz ts)
 
void AtEOXact_PgStat_Database (bool isCommit, bool parallel)
 
PgStat_StatDBEntrypgstat_prep_database_pending (Oid dboid)
 
void pgstat_reset_database_timestamp (Oid dboid, TimestampTz ts)
 
bool pgstat_database_flush_cb (PgStat_EntryRef *entry_ref, bool nowait)
 
void pgstat_database_reset_timestamp_cb (PgStatShared_Common *header, TimestampTz ts)
 
bool pgstat_function_flush_cb (PgStat_EntryRef *entry_ref, bool nowait)
 
void pgstat_flush_io (bool nowait)
 
bool pgstat_io_have_pending_cb (void)
 
bool pgstat_io_flush_cb (bool nowait)
 
void pgstat_io_init_shmem_cb (void *stats)
 
void pgstat_io_reset_all_cb (TimestampTz ts)
 
void pgstat_io_snapshot_cb (void)
 
void AtEOXact_PgStat_Relations (PgStat_SubXactStatus *xact_state, bool isCommit)
 
void AtEOSubXact_PgStat_Relations (PgStat_SubXactStatus *xact_state, bool isCommit, int nestDepth)
 
void AtPrepare_PgStat_Relations (PgStat_SubXactStatus *xact_state)
 
void PostPrepare_PgStat_Relations (PgStat_SubXactStatus *xact_state)
 
bool pgstat_relation_flush_cb (PgStat_EntryRef *entry_ref, bool nowait)
 
void pgstat_relation_delete_pending_cb (PgStat_EntryRef *entry_ref)
 
void pgstat_replslot_reset_timestamp_cb (PgStatShared_Common *header, TimestampTz ts)
 
void pgstat_replslot_to_serialized_name_cb (const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name)
 
bool pgstat_replslot_from_serialized_name_cb (const NameData *name, PgStat_HashKey *key)
 
void pgstat_attach_shmem (void)
 
void pgstat_detach_shmem (void)
 
PgStat_EntryRefpgstat_get_entry_ref (PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)
 
bool pgstat_lock_entry (PgStat_EntryRef *entry_ref, bool nowait)
 
bool pgstat_lock_entry_shared (PgStat_EntryRef *entry_ref, bool nowait)
 
void pgstat_unlock_entry (PgStat_EntryRef *entry_ref)
 
bool pgstat_drop_entry (PgStat_Kind kind, Oid dboid, uint64 objid)
 
void pgstat_drop_all_entries (void)
 
void pgstat_drop_matching_entries (bool(*do_drop)(PgStatShared_HashEntry *, Datum), Datum match_data)
 
PgStat_EntryRefpgstat_get_entry_ref_locked (PgStat_Kind kind, Oid dboid, uint64 objid, bool nowait)
 
void pgstat_reset_entry (PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts)
 
void pgstat_reset_entries_of_kind (PgStat_Kind kind, TimestampTz ts)
 
void pgstat_reset_matching_entries (bool(*do_reset)(PgStatShared_HashEntry *, Datum), Datum match_data, TimestampTz ts)
 
void pgstat_request_entry_refs_gc (void)
 
PgStatShared_Commonpgstat_init_entry (PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
 
bool pgstat_slru_have_pending_cb (void)
 
bool pgstat_slru_flush_cb (bool nowait)
 
void pgstat_slru_init_shmem_cb (void *stats)
 
void pgstat_slru_reset_all_cb (TimestampTz ts)
 
void pgstat_slru_snapshot_cb (void)
 
void pgstat_wal_init_backend_cb (void)
 
bool pgstat_wal_have_pending_cb (void)
 
bool pgstat_wal_flush_cb (bool nowait)
 
void pgstat_wal_init_shmem_cb (void *stats)
 
void pgstat_wal_reset_all_cb (TimestampTz ts)
 
void pgstat_wal_snapshot_cb (void)
 
bool pgstat_subscription_flush_cb (PgStat_EntryRef *entry_ref, bool nowait)
 
void pgstat_subscription_reset_timestamp_cb (PgStatShared_Common *header, TimestampTz ts)
 
PgStat_SubXactStatuspgstat_get_xact_stack_level (int nest_level)
 
void pgstat_drop_transactional (PgStat_Kind kind, Oid dboid, uint64 objid)
 
void pgstat_create_transactional (PgStat_Kind kind, Oid dboid, uint64 objid)
 

Variables

static const char *const slru_names []
 
PGDLLIMPORT PgStat_LocalState pgStatLocal
 

Macro Definition Documentation

◆ pgstat_assert_is_up

#define pgstat_assert_is_up ( )    ((void)true)

Definition at line 597 of file pgstat_internal.h.

◆ PGSTAT_BACKEND_FLUSH_ALL

#define PGSTAT_BACKEND_FLUSH_ALL   (PGSTAT_BACKEND_FLUSH_IO | PGSTAT_BACKEND_FLUSH_WAL)

Definition at line 626 of file pgstat_internal.h.

◆ PGSTAT_BACKEND_FLUSH_IO

#define PGSTAT_BACKEND_FLUSH_IO   (1 << 0) /* Flush I/O statistics */

Definition at line 624 of file pgstat_internal.h.

◆ PGSTAT_BACKEND_FLUSH_WAL

#define PGSTAT_BACKEND_FLUSH_WAL   (1 << 1) /* Flush WAL statistics */

Definition at line 625 of file pgstat_internal.h.

◆ SLRU_NUM_ELEMENTS

#define SLRU_NUM_ELEMENTS   lengthof(slru_names)

Definition at line 345 of file pgstat_internal.h.

Typedef Documentation

◆ PgStat_EntryRef

◆ PgStat_HashKey

◆ PgStat_KindInfo

◆ PgStat_LocalState

◆ PgStat_ShmemControl

◆ PgStat_Snapshot

◆ PgStat_SubXactStatus

◆ PgStatShared_Archiver

◆ PgStatShared_Backend

◆ PgStatShared_BgWriter

◆ PgStatShared_Checkpointer

◆ PgStatShared_Common

◆ PgStatShared_Database

◆ PgStatShared_Function

◆ PgStatShared_HashEntry

◆ PgStatShared_IO

◆ PgStatShared_Relation

◆ PgStatShared_ReplSlot

◆ PgStatShared_SLRU

◆ PgStatShared_Subscription

◆ PgStatShared_Wal

Function Documentation

◆ AtEOSubXact_PgStat_Relations()

void AtEOSubXact_PgStat_Relations ( PgStat_SubXactStatus xact_state,
bool  isCommit,
int  nestDepth 
)

Definition at line 609 of file pgstat_relation.c.

610{
612 PgStat_TableXactStatus *next_trans;
613
614 for (trans = xact_state->first; trans != NULL; trans = next_trans)
615 {
616 PgStat_TableStatus *tabstat;
617
618 next_trans = trans->next;
619 Assert(trans->nest_level == nestDepth);
620 tabstat = trans->parent;
621 Assert(tabstat->trans == trans);
622
623 if (isCommit)
624 {
625 if (trans->upper && trans->upper->nest_level == nestDepth - 1)
626 {
627 if (trans->truncdropped)
628 {
629 /* propagate the truncate/drop status one level up */
630 save_truncdrop_counters(trans->upper, false);
631 /* replace upper xact stats with ours */
632 trans->upper->tuples_inserted = trans->tuples_inserted;
633 trans->upper->tuples_updated = trans->tuples_updated;
634 trans->upper->tuples_deleted = trans->tuples_deleted;
635 }
636 else
637 {
638 trans->upper->tuples_inserted += trans->tuples_inserted;
639 trans->upper->tuples_updated += trans->tuples_updated;
640 trans->upper->tuples_deleted += trans->tuples_deleted;
641 }
642 tabstat->trans = trans->upper;
643 pfree(trans);
644 }
645 else
646 {
647 /*
648 * When there isn't an immediate parent state, we can just
649 * reuse the record instead of going through a palloc/pfree
650 * pushup (this works since it's all in TopTransactionContext
651 * anyway). We have to re-link it into the parent level,
652 * though, and that might mean pushing a new entry into the
653 * pgStatXactStack.
654 */
655 PgStat_SubXactStatus *upper_xact_state;
656
657 upper_xact_state = pgstat_get_xact_stack_level(nestDepth - 1);
658 trans->next = upper_xact_state->first;
659 upper_xact_state->first = trans;
660 trans->nest_level = nestDepth - 1;
661 }
662 }
663 else
664 {
665 /*
666 * On abort, update top-level tabstat counts, then forget the
667 * subtransaction
668 */
669
670 /* first restore values obliterated by truncate/drop */
672 /* count attempted actions regardless of commit/abort */
673 tabstat->counts.tuples_inserted += trans->tuples_inserted;
674 tabstat->counts.tuples_updated += trans->tuples_updated;
675 tabstat->counts.tuples_deleted += trans->tuples_deleted;
676 /* inserted tuples are dead, deleted tuples are unaffected */
677 tabstat->counts.delta_dead_tuples +=
678 trans->tuples_inserted + trans->tuples_updated;
679 tabstat->trans = trans->upper;
680 pfree(trans);
681 }
682 }
683}
Assert(PointerIsAligned(start, uint64))
void pfree(void *pointer)
Definition: mcxt.c:2152
static void save_truncdrop_counters(PgStat_TableXactStatus *trans, bool is_drop)
static void restore_truncdrop_counters(PgStat_TableXactStatus *trans)
PgStat_SubXactStatus * pgstat_get_xact_stack_level(int nest_level)
Definition: pgstat_xact.c:238
PgStat_TableXactStatus * first
PgStat_Counter tuples_inserted
Definition: pgstat.h:143
PgStat_Counter tuples_updated
Definition: pgstat.h:144
PgStat_Counter delta_dead_tuples
Definition: pgstat.h:151
PgStat_Counter tuples_deleted
Definition: pgstat.h:145
PgStat_TableCounts counts
Definition: pgstat.h:178
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:177
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:402

References Assert(), PgStat_TableStatus::counts, PgStat_TableCounts::delta_dead_tuples, PgStat_SubXactStatus::first, pfree(), pgstat_get_xact_stack_level(), restore_truncdrop_counters(), save_truncdrop_counters(), PgStat_TableStatus::trans, trans, PgStat_TableCounts::tuples_deleted, PgStat_TableCounts::tuples_inserted, and PgStat_TableCounts::tuples_updated.

Referenced by AtEOSubXact_PgStat().

◆ AtEOXact_PgStat_Database()

void AtEOXact_PgStat_Database ( bool  isCommit,
bool  parallel 
)

Definition at line 285 of file pgstat_database.c.

286{
287 /* Don't count parallel worker transaction stats */
288 if (!parallel)
289 {
290 /*
291 * Count transaction commit or abort. (We use counters, not just
292 * bools, in case the reporting message isn't sent right away.)
293 */
294 if (isCommit)
296 else
298 }
299}
static int pgStatXactCommit
static int pgStatXactRollback

References pgStatXactCommit, and pgStatXactRollback.

Referenced by AtEOXact_PgStat().

◆ AtEOXact_PgStat_Relations()

void AtEOXact_PgStat_Relations ( PgStat_SubXactStatus xact_state,
bool  isCommit 
)

Definition at line 551 of file pgstat_relation.c.

552{
554
555 for (trans = xact_state->first; trans != NULL; trans = trans->next)
556 {
557 PgStat_TableStatus *tabstat;
558
559 Assert(trans->nest_level == 1);
560 Assert(trans->upper == NULL);
561 tabstat = trans->parent;
562 Assert(tabstat->trans == trans);
563 /* restore pre-truncate/drop stats (if any) in case of aborted xact */
564 if (!isCommit)
566 /* count attempted actions regardless of commit/abort */
567 tabstat->counts.tuples_inserted += trans->tuples_inserted;
568 tabstat->counts.tuples_updated += trans->tuples_updated;
569 tabstat->counts.tuples_deleted += trans->tuples_deleted;
570 if (isCommit)
571 {
572 tabstat->counts.truncdropped = trans->truncdropped;
573 if (trans->truncdropped)
574 {
575 /* forget live/dead stats seen by backend thus far */
576 tabstat->counts.delta_live_tuples = 0;
577 tabstat->counts.delta_dead_tuples = 0;
578 }
579 /* insert adds a live tuple, delete removes one */
580 tabstat->counts.delta_live_tuples +=
581 trans->tuples_inserted - trans->tuples_deleted;
582 /* update and delete each create a dead tuple */
583 tabstat->counts.delta_dead_tuples +=
584 trans->tuples_updated + trans->tuples_deleted;
585 /* insert, update, delete each count as one change event */
586 tabstat->counts.changed_tuples +=
587 trans->tuples_inserted + trans->tuples_updated +
588 trans->tuples_deleted;
589 }
590 else
591 {
592 /* inserted tuples are dead, deleted tuples are unaffected */
593 tabstat->counts.delta_dead_tuples +=
594 trans->tuples_inserted + trans->tuples_updated;
595 /* an aborted xact generates no changed_tuple events */
596 }
597 tabstat->trans = NULL;
598 }
599}
PgStat_Counter delta_live_tuples
Definition: pgstat.h:150
PgStat_Counter changed_tuples
Definition: pgstat.h:152

References Assert(), PgStat_TableCounts::changed_tuples, PgStat_TableStatus::counts, PgStat_TableCounts::delta_dead_tuples, PgStat_TableCounts::delta_live_tuples, PgStat_SubXactStatus::first, restore_truncdrop_counters(), PgStat_TableStatus::trans, trans, PgStat_TableCounts::truncdropped, PgStat_TableCounts::tuples_deleted, PgStat_TableCounts::tuples_inserted, and PgStat_TableCounts::tuples_updated.

Referenced by AtEOXact_PgStat().

◆ AtPrepare_PgStat_Relations()

void AtPrepare_PgStat_Relations ( PgStat_SubXactStatus xact_state)

Definition at line 690 of file pgstat_relation.c.

691{
693
694 for (trans = xact_state->first; trans != NULL; trans = trans->next)
695 {
698
699 Assert(trans->nest_level == 1);
700 Assert(trans->upper == NULL);
701 tabstat = trans->parent;
702 Assert(tabstat->trans == trans);
703
704 record.tuples_inserted = trans->tuples_inserted;
705 record.tuples_updated = trans->tuples_updated;
706 record.tuples_deleted = trans->tuples_deleted;
707 record.inserted_pre_truncdrop = trans->inserted_pre_truncdrop;
708 record.updated_pre_truncdrop = trans->updated_pre_truncdrop;
709 record.deleted_pre_truncdrop = trans->deleted_pre_truncdrop;
710 record.id = tabstat->id;
711 record.shared = tabstat->shared;
712 record.truncdropped = trans->truncdropped;
713
715 &record, sizeof(TwoPhasePgStatRecord));
716 }
717}
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:224
PgStat_Counter deleted_pre_truncdrop
PgStat_Counter inserted_pre_truncdrop
PgStat_Counter updated_pre_truncdrop
PgStat_Counter tuples_deleted
PgStat_Counter tuples_inserted
PgStat_Counter tuples_updated
void RegisterTwoPhaseRecord(TwoPhaseRmgrId rmid, uint16 info, const void *data, uint32 len)
Definition: twophase.c:1264
#define TWOPHASE_RM_PGSTAT_ID
Definition: twophase_rmgr.h:26

References Assert(), TwoPhasePgStatRecord::deleted_pre_truncdrop, PgStat_SubXactStatus::first, TwoPhasePgStatRecord::id, TwoPhasePgStatRecord::inserted_pre_truncdrop, PG_USED_FOR_ASSERTS_ONLY, RegisterTwoPhaseRecord(), TwoPhasePgStatRecord::shared, trans, TwoPhasePgStatRecord::truncdropped, TwoPhasePgStatRecord::tuples_deleted, TwoPhasePgStatRecord::tuples_inserted, TwoPhasePgStatRecord::tuples_updated, TWOPHASE_RM_PGSTAT_ID, and TwoPhasePgStatRecord::updated_pre_truncdrop.

Referenced by AtPrepare_PgStat().

◆ pgstat_archiver_init_shmem_cb()

void pgstat_archiver_init_shmem_cb ( void *  stats)

Definition at line 66 of file pgstat_archiver.c.

67{
68 PgStatShared_Archiver *stats_shmem = (PgStatShared_Archiver *) stats;
69
71}
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:721
@ LWTRANCHE_PGSTATS_DATA
Definition: lwlock.h:210

References PgStatShared_Archiver::lock, LWLockInitialize(), and LWTRANCHE_PGSTATS_DATA.

◆ pgstat_archiver_reset_all_cb()

void pgstat_archiver_reset_all_cb ( TimestampTz  ts)

Definition at line 74 of file pgstat_archiver.c.

75{
77
78 /* see explanation above PgStatShared_Archiver for the reset protocol */
79 LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
81 &stats_shmem->stats,
82 sizeof(stats_shmem->stats),
83 &stats_shmem->changecount);
84 stats_shmem->stats.stat_reset_timestamp = ts;
85 LWLockRelease(&stats_shmem->lock);
86}
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1182
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1902
@ LW_EXCLUSIVE
Definition: lwlock.h:114
PgStat_LocalState pgStatLocal
Definition: pgstat.c:212
static void pgstat_copy_changecounted_stats(void *dst, void *src, size_t len, uint32 *cc)
PgStat_ArchiverStats reset_offset
PgStat_ArchiverStats stats
TimestampTz stat_reset_timestamp
Definition: pgstat.h:226
PgStat_ShmemControl * shmem
PgStatShared_Archiver archiver

References PgStat_ShmemControl::archiver, PgStatShared_Archiver::changecount, PgStatShared_Archiver::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), pgstat_copy_changecounted_stats(), pgStatLocal, PgStatShared_Archiver::reset_offset, PgStat_LocalState::shmem, PgStat_ArchiverStats::stat_reset_timestamp, and PgStatShared_Archiver::stats.

◆ pgstat_archiver_snapshot_cb()

void pgstat_archiver_snapshot_cb ( void  )

Definition at line 89 of file pgstat_archiver.c.

90{
93 PgStat_ArchiverStats *reset_offset = &stats_shmem->reset_offset;
95
97 &stats_shmem->stats,
98 sizeof(stats_shmem->stats),
99 &stats_shmem->changecount);
100
101 LWLockAcquire(&stats_shmem->lock, LW_SHARED);
102 memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
103 LWLockRelease(&stats_shmem->lock);
104
105 /* compensate by reset offsets */
106 if (stat_snap->archived_count == reset.archived_count)
107 {
108 stat_snap->last_archived_wal[0] = 0;
109 stat_snap->last_archived_timestamp = 0;
110 }
111 stat_snap->archived_count -= reset.archived_count;
112
113 if (stat_snap->failed_count == reset.failed_count)
114 {
115 stat_snap->last_failed_wal[0] = 0;
116 stat_snap->last_failed_timestamp = 0;
117 }
118 stat_snap->failed_count -= reset.failed_count;
119}
@ LW_SHARED
Definition: lwlock.h:115
void reset(void)
Definition: sql-declare.c:600
TimestampTz last_failed_timestamp
Definition: pgstat.h:225
TimestampTz last_archived_timestamp
Definition: pgstat.h:221
char last_failed_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:223
PgStat_Counter failed_count
Definition: pgstat.h:222
PgStat_Counter archived_count
Definition: pgstat.h:218
char last_archived_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:219
PgStat_Snapshot snapshot
PgStat_ArchiverStats archiver

References PgStat_ArchiverStats::archived_count, PgStat_ShmemControl::archiver, PgStat_Snapshot::archiver, PgStatShared_Archiver::changecount, PgStat_ArchiverStats::failed_count, PgStat_ArchiverStats::last_archived_timestamp, PgStat_ArchiverStats::last_archived_wal, PgStat_ArchiverStats::last_failed_timestamp, PgStat_ArchiverStats::last_failed_wal, PgStatShared_Archiver::lock, LW_SHARED, LWLockAcquire(), LWLockRelease(), pgstat_copy_changecounted_stats(), pgStatLocal, reset(), PgStatShared_Archiver::reset_offset, PgStat_LocalState::shmem, PgStat_LocalState::snapshot, and PgStatShared_Archiver::stats.

◆ pgstat_attach_shmem()

void pgstat_attach_shmem ( void  )

Definition at line 244 of file pgstat_shmem.c.

245{
246 MemoryContext oldcontext;
247
248 Assert(pgStatLocal.dsa == NULL);
249
250 /* stats shared memory persists for the backend lifetime */
252
254 NULL);
256
259
260 MemoryContextSwitchTo(oldcontext);
261}
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition: dsa.c:545
void dsa_pin_mapping(dsa_area *area)
Definition: dsa.c:635
dshash_table * dshash_attach(dsa_area *area, const dshash_parameters *params, dshash_table_handle handle, void *arg)
Definition: dshash.c:270
MemoryContext TopMemoryContext
Definition: mcxt.c:165
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
static const dshash_parameters dsh_params
Definition: pgstat_shmem.c:62
dshash_table * shared_hash
dshash_table_handle hash_handle

References Assert(), PgStat_LocalState::dsa, dsa_attach_in_place(), dsa_pin_mapping(), dsh_params, dshash_attach(), PgStat_ShmemControl::hash_handle, MemoryContextSwitchTo(), pgStatLocal, PgStat_ShmemControl::raw_dsa_area, PgStat_LocalState::shared_hash, PgStat_LocalState::shmem, and TopMemoryContext.

Referenced by pgstat_initialize().

◆ pgstat_backend_flush_cb()

bool pgstat_backend_flush_cb ( bool  nowait)

Definition at line 322 of file pgstat_backend.c.

323{
325}
bool pgstat_flush_backend(bool nowait, bits32 flags)
#define PGSTAT_BACKEND_FLUSH_ALL

References PGSTAT_BACKEND_FLUSH_ALL, and pgstat_flush_backend().

◆ pgstat_backend_have_pending_cb()

bool pgstat_backend_have_pending_cb ( void  )

Definition at line 308 of file pgstat_backend.c.

309{
311 return false;
312
314}
BackendType MyBackendType
Definition: miscinit.c:64
bool pgstat_tracks_backend_bktype(BackendType bktype)
static bool pgstat_backend_wal_have_pending(void)
static bool backend_has_iostats

References backend_has_iostats, MyBackendType, pgstat_backend_wal_have_pending(), and pgstat_tracks_backend_bktype().

◆ pgstat_backend_reset_timestamp_cb()

void pgstat_backend_reset_timestamp_cb ( PgStatShared_Common header,
TimestampTz  ts 
)

Definition at line 409 of file pgstat_backend.c.

410{
411 ((PgStatShared_Backend *) header)->stats.stat_reset_timestamp = ts;
412}

◆ pgstat_begin_changecount_read()

static uint32 pgstat_begin_changecount_read ( uint32 cc)
inlinestatic

Definition at line 816 of file pgstat_internal.h.

817{
818 uint32 before_cc = *cc;
819
821
823
824 return before_cc;
825}
#define pg_read_barrier()
Definition: atomics.h:156
uint32_t uint32
Definition: c.h:502
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123

References CHECK_FOR_INTERRUPTS, and pg_read_barrier.

Referenced by pgstat_copy_changecounted_stats().

◆ pgstat_begin_changecount_write()

static void pgstat_begin_changecount_write ( uint32 cc)
inlinestatic

Definition at line 794 of file pgstat_internal.h.

795{
796 Assert((*cc & 1) == 0);
797
799 (*cc)++;
801}
#define pg_write_barrier()
Definition: atomics.h:157
#define START_CRIT_SECTION()
Definition: miscadmin.h:150

References Assert(), pg_write_barrier, and START_CRIT_SECTION.

Referenced by pgstat_report_archiver(), pgstat_report_bgwriter(), pgstat_report_checkpointer(), and pgstat_report_inj_fixed().

◆ pgstat_bgwriter_init_shmem_cb()

void pgstat_bgwriter_init_shmem_cb ( void *  stats)

◆ pgstat_bgwriter_reset_all_cb()

void pgstat_bgwriter_reset_all_cb ( TimestampTz  ts)

Definition at line 88 of file pgstat_bgwriter.c.

89{
91
92 /* see explanation above PgStatShared_BgWriter for the reset protocol */
93 LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
95 &stats_shmem->stats,
96 sizeof(stats_shmem->stats),
97 &stats_shmem->changecount);
98 stats_shmem->stats.stat_reset_timestamp = ts;
99 LWLockRelease(&stats_shmem->lock);
100}
PgStat_BgWriterStats reset_offset
PgStat_BgWriterStats stats
TimestampTz stat_reset_timestamp
Definition: pgstat.h:242
PgStatShared_BgWriter bgwriter

References PgStat_ShmemControl::bgwriter, PgStatShared_BgWriter::changecount, PgStatShared_BgWriter::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), pgstat_copy_changecounted_stats(), pgStatLocal, PgStatShared_BgWriter::reset_offset, PgStat_LocalState::shmem, PgStat_BgWriterStats::stat_reset_timestamp, and PgStatShared_BgWriter::stats.

◆ pgstat_bgwriter_snapshot_cb()

void pgstat_bgwriter_snapshot_cb ( void  )

Definition at line 103 of file pgstat_bgwriter.c.

104{
106 PgStat_BgWriterStats *reset_offset = &stats_shmem->reset_offset;
108
110 &stats_shmem->stats,
111 sizeof(stats_shmem->stats),
112 &stats_shmem->changecount);
113
114 LWLockAcquire(&stats_shmem->lock, LW_SHARED);
115 memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
116 LWLockRelease(&stats_shmem->lock);
117
118 /* compensate by reset offsets */
119#define BGWRITER_COMP(fld) pgStatLocal.snapshot.bgwriter.fld -= reset.fld;
120 BGWRITER_COMP(buf_written_clean);
121 BGWRITER_COMP(maxwritten_clean);
122 BGWRITER_COMP(buf_alloc);
123#undef BGWRITER_COMP
124}
#define BGWRITER_COMP(fld)
PgStat_BgWriterStats bgwriter

References PgStat_ShmemControl::bgwriter, PgStat_Snapshot::bgwriter, BGWRITER_COMP, PgStatShared_BgWriter::changecount, PgStatShared_BgWriter::lock, LW_SHARED, LWLockAcquire(), LWLockRelease(), pgstat_copy_changecounted_stats(), pgStatLocal, reset(), PgStatShared_BgWriter::reset_offset, PgStat_LocalState::shmem, PgStat_LocalState::snapshot, and PgStatShared_BgWriter::stats.

◆ pgstat_checkpointer_init_shmem_cb()

void pgstat_checkpointer_init_shmem_cb ( void *  stats)

◆ pgstat_checkpointer_reset_all_cb()

void pgstat_checkpointer_reset_all_cb ( TimestampTz  ts)

Definition at line 97 of file pgstat_checkpointer.c.

98{
100
101 /* see explanation above PgStatShared_Checkpointer for the reset protocol */
102 LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
104 &stats_shmem->stats,
105 sizeof(stats_shmem->stats),
106 &stats_shmem->changecount);
107 stats_shmem->stats.stat_reset_timestamp = ts;
108 LWLockRelease(&stats_shmem->lock);
109}
PgStat_CheckpointerStats reset_offset
PgStat_CheckpointerStats stats
TimestampTz stat_reset_timestamp
Definition: pgstat.h:265
PgStatShared_Checkpointer checkpointer

References PgStatShared_Checkpointer::changecount, PgStat_ShmemControl::checkpointer, PgStatShared_Checkpointer::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), pgstat_copy_changecounted_stats(), pgStatLocal, PgStatShared_Checkpointer::reset_offset, PgStat_LocalState::shmem, PgStat_CheckpointerStats::stat_reset_timestamp, and PgStatShared_Checkpointer::stats.

◆ pgstat_checkpointer_snapshot_cb()

void pgstat_checkpointer_snapshot_cb ( void  )

Definition at line 112 of file pgstat_checkpointer.c.

113{
115 PgStat_CheckpointerStats *reset_offset = &stats_shmem->reset_offset;
117
119 &stats_shmem->stats,
120 sizeof(stats_shmem->stats),
121 &stats_shmem->changecount);
122
123 LWLockAcquire(&stats_shmem->lock, LW_SHARED);
124 memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
125 LWLockRelease(&stats_shmem->lock);
126
127 /* compensate by reset offsets */
128#define CHECKPOINTER_COMP(fld) pgStatLocal.snapshot.checkpointer.fld -= reset.fld;
129 CHECKPOINTER_COMP(num_timed);
130 CHECKPOINTER_COMP(num_requested);
131 CHECKPOINTER_COMP(num_performed);
132 CHECKPOINTER_COMP(restartpoints_timed);
133 CHECKPOINTER_COMP(restartpoints_requested);
134 CHECKPOINTER_COMP(restartpoints_performed);
135 CHECKPOINTER_COMP(write_time);
136 CHECKPOINTER_COMP(sync_time);
137 CHECKPOINTER_COMP(buffers_written);
138 CHECKPOINTER_COMP(slru_written);
139#undef CHECKPOINTER_COMP
140}
#define CHECKPOINTER_COMP(fld)
PgStat_CheckpointerStats checkpointer

References PgStatShared_Checkpointer::changecount, PgStat_ShmemControl::checkpointer, PgStat_Snapshot::checkpointer, CHECKPOINTER_COMP, PgStatShared_Checkpointer::lock, LW_SHARED, LWLockAcquire(), LWLockRelease(), pgstat_copy_changecounted_stats(), pgStatLocal, reset(), PgStatShared_Checkpointer::reset_offset, PgStat_LocalState::shmem, PgStat_LocalState::snapshot, and PgStatShared_Checkpointer::stats.

◆ pgstat_cmp_hash_key()

static int pgstat_cmp_hash_key ( const void *  a,
const void *  b,
size_t  size,
void *  arg 
)
inlinestatic

Definition at line 871 of file pgstat_internal.h.

872{
873 Assert(size == sizeof(PgStat_HashKey) && arg == NULL);
874 return memcmp(a, b, sizeof(PgStat_HashKey));
875}
int b
Definition: isn.c:74
int a
Definition: isn.c:73
void * arg

References a, arg, Assert(), and b.

◆ pgstat_copy_changecounted_stats()

static void pgstat_copy_changecounted_stats ( void *  dst,
void *  src,
size_t  len,
uint32 cc 
)
inlinestatic

Definition at line 855 of file pgstat_internal.h.

857{
858 uint32 cc_before;
859
860 do
861 {
862 cc_before = pgstat_begin_changecount_read(cc);
863
864 memcpy(dst, src, len);
865 }
866 while (!pgstat_end_changecount_read(cc, cc_before));
867}
const void size_t len
static uint32 pgstat_begin_changecount_read(uint32 *cc)
static bool pgstat_end_changecount_read(uint32 *cc, uint32 cc_before)

References len, pgstat_begin_changecount_read(), and pgstat_end_changecount_read().

Referenced by injection_stats_fixed_reset_all_cb(), injection_stats_fixed_snapshot_cb(), pgstat_archiver_reset_all_cb(), pgstat_archiver_snapshot_cb(), pgstat_bgwriter_reset_all_cb(), pgstat_bgwriter_snapshot_cb(), pgstat_checkpointer_reset_all_cb(), and pgstat_checkpointer_snapshot_cb().

◆ pgstat_create_transactional()

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

Definition at line 361 of file pgstat_xact.c.

362{
363 if (pgstat_get_entry_ref(kind, dboid, objid, false, NULL))
364 {
366 errmsg("resetting existing statistics for kind %s, db=%u, oid=%" PRIu64,
367 (pgstat_get_kind_info(kind))->name, dboid,
368 objid));
369
370 pgstat_reset(kind, dboid, objid);
371 }
372
373 create_drop_transactional_internal(kind, dboid, objid, /* create */ true);
374}
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define WARNING
Definition: elog.h:36
#define ereport(elevel,...)
Definition: elog.h:149
void pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:866
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1450
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:444
static void create_drop_transactional_internal(PgStat_Kind kind, Oid dboid, uint64 objid, bool is_create)
Definition: pgstat_xact.c:335
const char * name

References create_drop_transactional_internal(), ereport, errmsg(), name, pgstat_get_entry_ref(), pgstat_get_kind_info(), pgstat_reset(), and WARNING.

Referenced by pgstat_create_function(), pgstat_create_relation(), and pgstat_create_subscription().

◆ pgstat_database_flush_cb()

bool pgstat_database_flush_cb ( PgStat_EntryRef entry_ref,
bool  nowait 
)

Definition at line 428 of file pgstat_database.c.

429{
430 PgStatShared_Database *sharedent;
431 PgStat_StatDBEntry *pendingent;
432
433 pendingent = (PgStat_StatDBEntry *) entry_ref->pending;
434 sharedent = (PgStatShared_Database *) entry_ref->shared_stats;
435
436 if (!pgstat_lock_entry(entry_ref, nowait))
437 return false;
438
439#define PGSTAT_ACCUM_DBCOUNT(item) \
440 (sharedent)->stats.item += (pendingent)->item
441
442 PGSTAT_ACCUM_DBCOUNT(xact_commit);
443 PGSTAT_ACCUM_DBCOUNT(xact_rollback);
444 PGSTAT_ACCUM_DBCOUNT(blocks_fetched);
445 PGSTAT_ACCUM_DBCOUNT(blocks_hit);
446
447 PGSTAT_ACCUM_DBCOUNT(tuples_returned);
448 PGSTAT_ACCUM_DBCOUNT(tuples_fetched);
449 PGSTAT_ACCUM_DBCOUNT(tuples_inserted);
450 PGSTAT_ACCUM_DBCOUNT(tuples_updated);
451 PGSTAT_ACCUM_DBCOUNT(tuples_deleted);
452
453 /* last_autovac_time is reported immediately */
454 Assert(pendingent->last_autovac_time == 0);
455
456 PGSTAT_ACCUM_DBCOUNT(conflict_tablespace);
457 PGSTAT_ACCUM_DBCOUNT(conflict_lock);
458 PGSTAT_ACCUM_DBCOUNT(conflict_snapshot);
459 PGSTAT_ACCUM_DBCOUNT(conflict_logicalslot);
460 PGSTAT_ACCUM_DBCOUNT(conflict_bufferpin);
461 PGSTAT_ACCUM_DBCOUNT(conflict_startup_deadlock);
462
463 PGSTAT_ACCUM_DBCOUNT(temp_bytes);
464 PGSTAT_ACCUM_DBCOUNT(temp_files);
465 PGSTAT_ACCUM_DBCOUNT(deadlocks);
466
467 /* checksum failures are reported immediately */
468 Assert(pendingent->checksum_failures == 0);
469 Assert(pendingent->last_checksum_failure == 0);
470
471 PGSTAT_ACCUM_DBCOUNT(blk_read_time);
472 PGSTAT_ACCUM_DBCOUNT(blk_write_time);
473
474 PGSTAT_ACCUM_DBCOUNT(sessions);
475 PGSTAT_ACCUM_DBCOUNT(session_time);
476 PGSTAT_ACCUM_DBCOUNT(active_time);
477 PGSTAT_ACCUM_DBCOUNT(idle_in_transaction_time);
478 PGSTAT_ACCUM_DBCOUNT(sessions_abandoned);
479 PGSTAT_ACCUM_DBCOUNT(sessions_fatal);
480 PGSTAT_ACCUM_DBCOUNT(sessions_killed);
481 PGSTAT_ACCUM_DBCOUNT(parallel_workers_to_launch);
482 PGSTAT_ACCUM_DBCOUNT(parallel_workers_launched);
483#undef PGSTAT_ACCUM_DBCOUNT
484
485 pgstat_unlock_entry(entry_ref);
486
487 memset(pendingent, 0, sizeof(*pendingent));
488
489 return true;
490}
#define PGSTAT_ACCUM_DBCOUNT(item)
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:684
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:654
PgStatShared_Common * shared_stats
TimestampTz last_checksum_failure
Definition: pgstat.h:364
TimestampTz last_autovac_time
Definition: pgstat.h:353
PgStat_Counter checksum_failures
Definition: pgstat.h:363

References Assert(), PgStat_StatDBEntry::checksum_failures, PgStat_StatDBEntry::last_autovac_time, PgStat_StatDBEntry::last_checksum_failure, PgStat_EntryRef::pending, PGSTAT_ACCUM_DBCOUNT, pgstat_lock_entry(), pgstat_unlock_entry(), and PgStat_EntryRef::shared_stats.

◆ pgstat_database_reset_timestamp_cb()

void pgstat_database_reset_timestamp_cb ( PgStatShared_Common header,
TimestampTz  ts 
)

Definition at line 493 of file pgstat_database.c.

494{
495 ((PgStatShared_Database *) header)->stats.stat_reset_timestamp = ts;
496}

◆ pgstat_delete_pending_entry()

void pgstat_delete_pending_entry ( PgStat_EntryRef entry_ref)

Definition at line 1331 of file pgstat.c.

1333{
1334 PgStat_Kind kind = entry_ref->shared_entry->key.kind;
1335 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1336 void *pending_data = entry_ref->pending;
1337
1338 Assert(pending_data != NULL);
1339 /* !fixed_amount stats should be handled explicitly */
1340 Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1341
1342 if (kind_info->delete_pending_cb)
1343 kind_info->delete_pending_cb(entry_ref);
1344
1345 pfree(pending_data);
1346 entry_ref->pending = NULL;
1347
1348 dlist_delete(&entry_ref->pending_node);
static void dlist_delete(dlist_node *node)
Definition: ilist.h:405
#define PgStat_Kind
Definition: pgstat_kind.h:17
PgStatShared_HashEntry * shared_entry
dlist_node pending_node
PgStat_Kind kind
void(* delete_pending_cb)(PgStat_EntryRef *sr)

References Assert(), PgStat_KindInfo::delete_pending_cb, dlist_delete(), 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_detach_shmem()

void pgstat_detach_shmem ( void  )

Definition at line 264 of file pgstat_shmem.c.

265{
267
268 /* we shouldn't leave references to shared stats */
270
273
275
276 /*
277 * dsa_detach() does not decrement the DSA reference count as no segment
278 * was provided to dsa_attach_in_place(), causing no cleanup callbacks to
279 * be registered. Hence, release it manually now.
280 */
282
283 pgStatLocal.dsa = NULL;
284}
void dsa_release_in_place(void *place)
Definition: dsa.c:605
void dsa_detach(dsa_area *area)
Definition: dsa.c:1952
void dshash_detach(dshash_table *hash_table)
Definition: dshash.c:307
static void pgstat_release_all_entry_refs(bool discard_pending)
Definition: pgstat_shmem.c:802

References Assert(), PgStat_LocalState::dsa, dsa_detach(), dsa_release_in_place(), dshash_detach(), pgstat_release_all_entry_refs(), pgStatLocal, PgStat_ShmemControl::raw_dsa_area, PgStat_LocalState::shared_hash, and PgStat_LocalState::shmem.

Referenced by pgstat_shutdown_hook().

◆ pgstat_drop_all_entries()

void pgstat_drop_all_entries ( void  )

Definition at line 1051 of file pgstat_shmem.c.

1052{
1054}
void pgstat_drop_matching_entries(bool(*do_drop)(PgStatShared_HashEntry *, Datum), Datum match_data)

References pgstat_drop_matching_entries().

Referenced by pgstat_reset_after_failure().

◆ pgstat_drop_entry()

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

Definition at line 962 of file pgstat_shmem.c.

963{
966 bool freed = true;
967
968 /* clear padding */
969 memset(&key, 0, sizeof(struct PgStat_HashKey));
970
971 key.kind = kind;
972 key.dboid = dboid;
973 key.objid = objid;
974
975 /* delete local reference */
977 {
978 PgStat_EntryRefHashEntry *lohashent =
979 pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, key);
980
981 if (lohashent)
982 pgstat_release_entry_ref(lohashent->key, lohashent->entry_ref,
983 true);
984 }
985
986 /* mark entry in shared hashtable as deleted, drop if possible */
987 shent = dshash_find(pgStatLocal.shared_hash, &key, true);
988 if (shent)
989 {
990 freed = pgstat_drop_entry_internal(shent, NULL);
991
992 /*
993 * Database stats contain other stats. Drop those as well when
994 * dropping the database. XXX: Perhaps this should be done in a
995 * slightly more principled way? But not obvious what that'd look
996 * like, and so far this is the only case...
997 */
998 if (key.kind == PGSTAT_KIND_DATABASE)
1000 }
1001
1002 return freed;
1003}
void * dshash_find(dshash_table *hash_table, const void *key, bool exclusive)
Definition: dshash.c:390
#define PGSTAT_KIND_DATABASE
Definition: pgstat_kind.h:27
static void pgstat_release_entry_ref(PgStat_HashKey key, PgStat_EntryRef *entry_ref, bool discard_pending)
Definition: pgstat_shmem.c:576
static void pgstat_drop_database_and_contents(Oid dboid)
Definition: pgstat_shmem.c:901
static pgstat_entry_ref_hash_hash * pgStatEntryRefHash
Definition: pgstat_shmem.c:82
static bool pgstat_drop_entry_internal(PgStatShared_HashEntry *shent, dshash_seq_status *hstat)
Definition: pgstat_shmem.c:861
PgStat_EntryRef * entry_ref
Definition: pgstat_shmem.c:28

References dshash_find(), PgStat_EntryRefHashEntry::entry_ref, PgStat_EntryRefHashEntry::key, sort-test::key, pgstat_drop_database_and_contents(), pgstat_drop_entry_internal(), PGSTAT_KIND_DATABASE, pgstat_release_entry_ref(), pgStatEntryRefHash, pgStatLocal, and PgStat_LocalState::shared_hash.

Referenced by AtEOSubXact_PgStat_DroppedStats(), AtEOXact_PgStat_DroppedStats(), pgstat_drop_inj(), pgstat_drop_replslot(), pgstat_execute_transactional_drops(), pgstat_init_function_usage(), and pgstat_shutdown_hook().

◆ pgstat_drop_matching_entries()

void pgstat_drop_matching_entries ( bool(*)(PgStatShared_HashEntry *, Datum do_drop,
Datum  match_data 
)

Definition at line 1010 of file pgstat_shmem.c.

1012{
1013 dshash_seq_status hstat;
1015 uint64 not_freed_count = 0;
1016
1017 /* entries are removed, take an exclusive lock */
1019 while ((ps = dshash_seq_next(&hstat)) != NULL)
1020 {
1021 if (ps->dropped)
1022 continue;
1023
1024 if (do_drop != NULL && !do_drop(ps, match_data))
1025 continue;
1026
1027 /* delete local reference */
1029 {
1030 PgStat_EntryRefHashEntry *lohashent =
1031 pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, ps->key);
1032
1033 if (lohashent)
1034 pgstat_release_entry_ref(lohashent->key, lohashent->entry_ref,
1035 true);
1036 }
1037
1038 if (!pgstat_drop_entry_internal(ps, &hstat))
1039 not_freed_count++;
1040 }
1041 dshash_seq_term(&hstat);
1042
1043 if (not_freed_count > 0)
1045}
uint64_t uint64
Definition: c.h:503
void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table, bool exclusive)
Definition: dshash.c:638
void dshash_seq_term(dshash_seq_status *status)
Definition: dshash.c:747
void * dshash_seq_next(dshash_seq_status *status)
Definition: dshash.c:657
struct parser_state ps
void pgstat_request_entry_refs_gc(void)
Definition: pgstat_shmem.c:709

References dshash_seq_init(), dshash_seq_next(), dshash_seq_term(), PgStat_EntryRefHashEntry::entry_ref, PgStat_EntryRefHashEntry::key, pgstat_drop_entry_internal(), pgstat_release_entry_ref(), pgstat_request_entry_refs_gc(), pgStatEntryRefHash, pgStatLocal, ps, and PgStat_LocalState::shared_hash.

Referenced by injection_points_stats_drop(), and pgstat_drop_all_entries().

◆ pgstat_drop_transactional()

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

Definition at line 384 of file pgstat_xact.c.

385{
386 create_drop_transactional_internal(kind, dboid, objid, /* create */ false);
387}

References create_drop_transactional_internal().

Referenced by pgstat_drop_database(), pgstat_drop_function(), pgstat_drop_relation(), and pgstat_drop_subscription().

◆ pgstat_end_changecount_read()

static bool pgstat_end_changecount_read ( uint32 cc,
uint32  cc_before 
)
inlinestatic

Definition at line 831 of file pgstat_internal.h.

832{
833 uint32 after_cc;
834
836
837 after_cc = *cc;
838
839 /* was a write in progress when we started? */
840 if (before_cc & 1)
841 return false;
842
843 /* did writes start and complete while we read? */
844 return before_cc == after_cc;
845}

References pg_read_barrier.

Referenced by pgstat_copy_changecounted_stats().

◆ pgstat_end_changecount_write()

static void pgstat_end_changecount_write ( uint32 cc)
inlinestatic

Definition at line 804 of file pgstat_internal.h.

805{
806 Assert((*cc & 1) == 1);
807
809
810 (*cc)++;
811
813}
#define END_CRIT_SECTION()
Definition: miscadmin.h:152

References Assert(), END_CRIT_SECTION, and pg_write_barrier.

Referenced by pgstat_report_archiver(), pgstat_report_bgwriter(), pgstat_report_checkpointer(), and pgstat_report_inj_fixed().

◆ pgstat_fetch_entry()

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

Definition at line 946 of file pgstat.c.

948{
950 PgStat_EntryRef *entry_ref;
951 void *stats_data;
952 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
953
954 /* should be called from backends */
956 Assert(!kind_info->fixed_amount);
957
959
960 /* clear padding */
961 memset(&key, 0, sizeof(struct PgStat_HashKey));
962
963 key.kind = kind;
964 key.dboid = dboid;
965 key.objid = objid;
966
967 /* if we need to build a full snapshot, do so */
970
971 /* if caching is desired, look up in cache */
973 {
974 PgStat_SnapshotEntry *entry = NULL;
975
976 entry = pgstat_snapshot_lookup(pgStatLocal.snapshot.stats, key);
977
978 if (entry)
979 return entry->data;
980
981 /*
982 * If we built a full snapshot and the key is not in
983 * pgStatLocal.snapshot.stats, there are no matching stats.
984 */
986 return NULL;
987 }
988
990
991 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
992
993 if (entry_ref == NULL || entry_ref->shared_entry->dropped)
994 {
995 /* create empty entry when using PGSTAT_FETCH_CONSISTENCY_CACHE */
997 {
998 PgStat_SnapshotEntry *entry = NULL;
999 bool found;
1000
1001 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
1002 Assert(!found);
1003 entry->data = NULL;
1004 }
1005 return NULL;
1006 }
1007
1008 /*
1009 * Allocate in caller's context for PGSTAT_FETCH_CONSISTENCY_NONE,
1010 * otherwise we could quickly end up with a fair bit of memory used due to
1011 * repeated accesses.
1012 */
1014 stats_data = palloc(kind_info->shared_data_len);
1015 else
1017 kind_info->shared_data_len);
1018
1019 (void) pgstat_lock_entry_shared(entry_ref, false);
1020 memcpy(stats_data,
1021 pgstat_get_entry_data(kind, entry_ref->shared_stats),
1022 kind_info->shared_data_len);
1023 pgstat_unlock_entry(entry_ref);
1024
1026 {
1027 PgStat_SnapshotEntry *entry = NULL;
1028 bool found;
1029
1030 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
1031 entry->data = stats_data;
1032 }
1033
1034 return stats_data;
bool IsUnderPostmaster
Definition: globals.c:121
bool IsPostmasterEnvironment
Definition: globals.c:120
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1260
void * palloc(Size size)
Definition: mcxt.c:1945
int pgstat_fetch_consistency
Definition: pgstat.c:204
static void pgstat_prep_snapshot(void)
Definition: pgstat.c:1114
static void pgstat_build_snapshot(void)
Definition: pgstat.c:1135
@ PGSTAT_FETCH_CONSISTENCY_NONE
Definition: pgstat.h:46
@ PGSTAT_FETCH_CONSISTENCY_CACHE
Definition: pgstat.h:47
@ PGSTAT_FETCH_CONSISTENCY_SNAPSHOT
Definition: pgstat.h:48
static void * pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry)
bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:672
MemoryContext context
PgStat_FetchConsistency mode
struct pgstat_snapshot_hash * stats

References Assert(), PgStat_Snapshot::context, PgStat_SnapshotEntry::data, PgStatShared_HashEntry::dropped, PgStat_KindInfo::fixed_amount, IsPostmasterEnvironment, IsUnderPostmaster, sort-test::key, 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_KindInfo::shared_data_len, 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_injentry(), pgstat_fetch_stat_subscription(), and pgstat_fetch_stat_tabentry_ext().

◆ pgstat_fetch_pending_entry()

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

Definition at line 1318 of file pgstat.c.

1320{
1321 PgStat_EntryRef *entry_ref;
1322
1323 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
1324
1325 if (entry_ref == NULL || entry_ref->pending == NULL)
1326 return NULL;
1327
1328 return entry_ref;

References PgStat_EntryRef::pending, and pgstat_get_entry_ref().

Referenced by find_funcstat_entry(), and find_tabstat_entry().

◆ pgstat_flush_backend()

bool pgstat_flush_backend ( bool  nowait,
bits32  flags 
)

Definition at line 267 of file pgstat_backend.c.

268{
269 PgStat_EntryRef *entry_ref;
270 bool has_pending_data = false;
271
273 return false;
274
275 /* Some IO data pending? */
277 has_pending_data = true;
278
279 /* Some WAL data pending? */
280 if ((flags & PGSTAT_BACKEND_FLUSH_WAL) &&
282 has_pending_data = true;
283
284 if (!has_pending_data)
285 return false;
286
288 MyProcNumber, nowait);
289 if (!entry_ref)
290 return true;
291
292 /* Flush requested statistics */
293 if (flags & PGSTAT_BACKEND_FLUSH_IO)
295
296 if (flags & PGSTAT_BACKEND_FLUSH_WAL)
298
299 pgstat_unlock_entry(entry_ref);
300
301 return false;
302}
ProcNumber MyProcNumber
Definition: globals.c:91
static void pgstat_flush_backend_entry_wal(PgStat_EntryRef *entry_ref)
static void pgstat_flush_backend_entry_io(PgStat_EntryRef *entry_ref)
#define PGSTAT_BACKEND_FLUSH_IO
#define PGSTAT_BACKEND_FLUSH_WAL
#define PGSTAT_KIND_BACKEND
Definition: pgstat_kind.h:32
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid, bool nowait)
Definition: pgstat_shmem.c:693
#define InvalidOid
Definition: postgres_ext.h:35

References backend_has_iostats, InvalidOid, MyBackendType, MyProcNumber, PGSTAT_BACKEND_FLUSH_IO, PGSTAT_BACKEND_FLUSH_WAL, pgstat_backend_wal_have_pending(), pgstat_flush_backend_entry_io(), pgstat_flush_backend_entry_wal(), pgstat_get_entry_ref_locked(), PGSTAT_KIND_BACKEND, pgstat_tracks_backend_bktype(), and pgstat_unlock_entry().

Referenced by pgstat_backend_flush_cb(), pgstat_report_analyze(), pgstat_report_vacuum(), pgstat_report_wal(), WalSndLoop(), and WalSndWaitForWal().

◆ pgstat_flush_io()

void pgstat_flush_io ( bool  nowait)

Definition at line 183 of file pgstat_io.c.

184{
185 (void) pgstat_io_flush_cb(nowait);
186}
bool pgstat_io_flush_cb(bool nowait)
Definition: pgstat_io.c:197

References pgstat_io_flush_cb().

Referenced by pgstat_report_analyze(), pgstat_report_bgwriter(), pgstat_report_checkpointer(), pgstat_report_vacuum(), pgstat_report_wal(), WalSndLoop(), and WalSndWaitForWal().

◆ pgstat_function_flush_cb()

bool pgstat_function_flush_cb ( PgStat_EntryRef entry_ref,
bool  nowait 
)

Definition at line 193 of file pgstat_function.c.

194{
195 PgStat_FunctionCounts *localent;
196 PgStatShared_Function *shfuncent;
197
198 localent = (PgStat_FunctionCounts *) entry_ref->pending;
199 shfuncent = (PgStatShared_Function *) entry_ref->shared_stats;
200
201 /* localent always has non-zero content */
202
203 if (!pgstat_lock_entry(entry_ref, nowait))
204 return false;
205
206 shfuncent->stats.numcalls += localent->numcalls;
207 shfuncent->stats.total_time +=
209 shfuncent->stats.self_time +=
211
212 pgstat_unlock_entry(entry_ref);
213
214 return true;
215}
#define INSTR_TIME_GET_MICROSEC(t)
Definition: instr_time.h:194
PgStat_StatFuncEntry stats
PgStat_Counter numcalls
Definition: pgstat.h:82
instr_time total_time
Definition: pgstat.h:83
instr_time self_time
Definition: pgstat.h:84
PgStat_Counter self_time
Definition: pgstat.h:385
PgStat_Counter numcalls
Definition: pgstat.h:382
PgStat_Counter total_time
Definition: pgstat.h:384

References INSTR_TIME_GET_MICROSEC, PgStat_FunctionCounts::numcalls, PgStat_StatFuncEntry::numcalls, PgStat_EntryRef::pending, pgstat_lock_entry(), pgstat_unlock_entry(), PgStat_FunctionCounts::self_time, PgStat_StatFuncEntry::self_time, PgStat_EntryRef::shared_stats, PgStatShared_Function::stats, PgStat_FunctionCounts::total_time, and PgStat_StatFuncEntry::total_time.

◆ pgstat_get_custom_shmem_data()

static void * pgstat_get_custom_shmem_data ( PgStat_Kind  kind)
inlinestatic

Definition at line 914 of file pgstat_internal.h.

915{
916 int idx = kind - PGSTAT_KIND_CUSTOM_MIN;
917
919 Assert(pgstat_get_kind_info(kind)->fixed_amount);
920
922}
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:262
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1450
PGDLLIMPORT PgStat_LocalState pgStatLocal
Definition: pgstat.c:212
static bool pgstat_is_kind_custom(PgStat_Kind kind)
Definition: pgstat_kind.h:67
#define PGSTAT_KIND_CUSTOM_MIN
Definition: pgstat_kind.h:49
void * custom_data[PGSTAT_KIND_CUSTOM_SIZE]

References Assert(), PgStat_ShmemControl::custom_data, idx(), pgstat_get_kind_info(), pgstat_is_kind_custom(), PGSTAT_KIND_CUSTOM_MIN, pgStatLocal, and PgStat_LocalState::shmem.

Referenced by injection_stats_fixed_reset_all_cb(), injection_stats_fixed_snapshot_cb(), and pgstat_report_inj_fixed().

◆ pgstat_get_custom_snapshot_data()

static void * pgstat_get_custom_snapshot_data ( PgStat_Kind  kind)
inlinestatic

◆ pgstat_get_entry_data()

static void * pgstat_get_entry_data ( PgStat_Kind  kind,
PgStatShared_Common entry 
)
inlinestatic

Definition at line 900 of file pgstat_internal.h.

901{
902 size_t off = pgstat_get_kind_info(kind)->shared_data_off;
903
904 Assert(off != 0 && off < PG_UINT32_MAX);
905
906 return ((char *) (entry)) + off;
907}
#define PG_UINT32_MAX
Definition: c.h:561

References Assert(), PG_UINT32_MAX, pgstat_get_kind_info(), and PgStat_KindInfo::shared_data_off.

Referenced by pgstat_build_snapshot(), pgstat_fetch_entry(), pgstat_read_statsfile(), pgstat_reinit_entry(), pgstat_write_statsfile(), and shared_stat_reset_contents().

◆ pgstat_get_entry_len()

static size_t pgstat_get_entry_len ( PgStat_Kind  kind)
inlinestatic

◆ pgstat_get_entry_ref()

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

Definition at line 444 of file pgstat_shmem.c.

446{
448 PgStatShared_HashEntry *shhashent;
449 PgStatShared_Common *shheader = NULL;
450 PgStat_EntryRef *entry_ref;
451
452 /* clear padding */
453 memset(&key, 0, sizeof(struct PgStat_HashKey));
454
455 key.kind = kind;
456 key.dboid = dboid;
457 key.objid = objid;
458
459 /*
460 * passing in created_entry only makes sense if we possibly could create
461 * entry.
462 */
463 Assert(create || created_entry == NULL);
467
470
471 if (created_entry != NULL)
472 *created_entry = false;
473
474 /*
475 * Check if other backends dropped stats that could not be deleted because
476 * somebody held references to it. If so, check this backend's references.
477 * This is not expected to happen often. The location of the check is a
478 * bit random, but this is a relatively frequently called path, so better
479 * than most.
480 */
483
484 /*
485 * First check the lookup cache hashtable in local memory. If we find a
486 * match here we can avoid taking locks / causing contention.
487 */
488 if (pgstat_get_entry_ref_cached(key, &entry_ref))
489 return entry_ref;
490
491 Assert(entry_ref != NULL);
492
493 /*
494 * Do a lookup in the hash table first - it's quite likely that the entry
495 * already exists, and that way we only need a shared lock.
496 */
497 shhashent = dshash_find(pgStatLocal.shared_hash, &key, false);
498
499 if (create && !shhashent)
500 {
501 bool shfound;
502
503 /*
504 * It's possible that somebody created the entry since the above
505 * lookup. If so, fall through to the same path as if we'd have if it
506 * already had been created before the dshash_find() calls.
507 */
508 shhashent = dshash_find_or_insert(pgStatLocal.shared_hash, &key, &shfound);
509 if (!shfound)
510 {
511 shheader = pgstat_init_entry(kind, shhashent);
512 pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
513
514 if (created_entry != NULL)
515 *created_entry = true;
516
517 return entry_ref;
518 }
519 }
520
521 if (!shhashent)
522 {
523 /*
524 * If we're not creating, delete the reference again. In all
525 * likelihood it's just a stats lookup - no point wasting memory for a
526 * shared ref to nothing...
527 */
528 pgstat_release_entry_ref(key, entry_ref, false);
529
530 return NULL;
531 }
532 else
533 {
534 /*
535 * Can get here either because dshash_find() found a match, or if
536 * dshash_find_or_insert() found a concurrently inserted entry.
537 */
538
539 if (shhashent->dropped && create)
540 {
541 /*
542 * There are legitimate cases where the old stats entry might not
543 * yet have been dropped by the time it's reused. The most obvious
544 * case are replication slot stats, where a new slot can be
545 * created with the same index just after dropping. But oid
546 * wraparound can lead to other cases as well. We just reset the
547 * stats to their plain state, while incrementing its "generation"
548 * in the shared entry for any remaining local references.
549 */
550 shheader = pgstat_reinit_entry(kind, shhashent);
551 pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
552
553 if (created_entry != NULL)
554 *created_entry = true;
555
556 return entry_ref;
557 }
558 else if (shhashent->dropped)
559 {
561 pgstat_release_entry_ref(key, entry_ref, false);
562
563 return NULL;
564 }
565 else
566 {
567 shheader = dsa_get_address(pgStatLocal.dsa, shhashent->body);
568 pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
569
570 return entry_ref;
571 }
572 }
573}
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:942
void dshash_release_lock(dshash_table *hash_table, void *entry)
Definition: dshash.c:558
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
Definition: dshash.c:433
#define pgstat_assert_is_up()
static void pgstat_setup_memcxt(void)
static void pgstat_setup_shared_refs(void)
Definition: pgstat_shmem.c:352
static PgStatShared_Common * pgstat_reinit_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
Definition: pgstat_shmem.c:327
static void pgstat_acquire_entry_ref(PgStat_EntryRef *entry_ref, PgStatShared_HashEntry *shhashent, PgStatShared_Common *shheader)
Definition: pgstat_shmem.c:368
PgStatShared_Common * pgstat_init_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
Definition: pgstat_shmem.c:293
static bool pgstat_get_entry_ref_cached(PgStat_HashKey key, PgStat_EntryRef **entry_ref_p)
Definition: pgstat_shmem.c:388
static void pgstat_gc_entry_refs(void)
Definition: pgstat_shmem.c:731
static bool pgstat_need_entry_refs_gc(void)
Definition: pgstat_shmem.c:715

References Assert(), PgStatShared_HashEntry::body, PgStatShared_HashEntry::dropped, PgStat_LocalState::dsa, dsa_get_address(), dshash_find(), dshash_find_or_insert(), dshash_release_lock(), PgStat_ShmemControl::is_shutdown, sort-test::key, pgstat_acquire_entry_ref(), pgstat_assert_is_up, pgstat_gc_entry_refs(), pgstat_get_entry_ref_cached(), pgstat_init_entry(), pgstat_need_entry_refs_gc(), pgstat_reinit_entry(), pgstat_release_entry_ref(), pgstat_setup_memcxt(), pgstat_setup_shared_refs(), pgStatLocal, PgStat_LocalState::shared_hash, and PgStat_LocalState::shmem.

Referenced by pgstat_acquire_replslot(), pgstat_create_subscription(), pgstat_create_transactional(), pgstat_fetch_entry(), pgstat_fetch_pending_entry(), pgstat_get_entry_ref_locked(), pgstat_have_entry(), pgstat_prep_pending_entry(), pgstat_prepare_report_checksum_failure(), pgstat_report_checksum_failures_in_db(), and pgstat_reset_entry().

◆ pgstat_get_entry_ref_locked()

PgStat_EntryRef * pgstat_get_entry_ref_locked ( PgStat_Kind  kind,
Oid  dboid,
uint64  objid,
bool  nowait 
)

Definition at line 693 of file pgstat_shmem.c.

695{
696 PgStat_EntryRef *entry_ref;
697
698 /* find shared table stats entry corresponding to the local entry */
699 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, true, NULL);
700
701 /* lock the shared entry to protect the content, skip if failed */
702 if (!pgstat_lock_entry(entry_ref, nowait))
703 return NULL;
704
705 return entry_ref;
706}

References pgstat_get_entry_ref(), and pgstat_lock_entry().

Referenced by pgstat_copy_relation_stats(), pgstat_create_backend(), pgstat_create_replslot(), pgstat_flush_backend(), pgstat_report_analyze(), pgstat_report_autovac(), pgstat_report_replslot(), pgstat_report_vacuum(), and pgstat_reset_database_timestamp().

◆ pgstat_get_kind_info()

const PgStat_KindInfo * pgstat_get_kind_info ( PgStat_Kind  kind)

Definition at line 1450 of file pgstat.c.

1452{
1453 if (pgstat_is_kind_builtin(kind))
1454 return &pgstat_kind_builtin_infos[kind];
1455
1456 if (pgstat_is_kind_custom(kind))
1457 {
1459
1460 if (pgstat_kind_custom_infos == NULL ||
1462 return NULL;
1464 }
1465
1466 return NULL;
static const PgStat_KindInfo ** pgstat_kind_custom_infos
Definition: pgstat.c:491
static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
Definition: pgstat.c:276
static bool pgstat_is_kind_builtin(PgStat_Kind kind)
Definition: pgstat_kind.h:61

References idx(), pgstat_is_kind_builtin(), pgstat_is_kind_custom(), pgstat_kind_builtin_infos, pgstat_kind_custom_infos, and PGSTAT_KIND_CUSTOM_MIN.

Referenced by pgstat_build_snapshot(), pgstat_build_snapshot_fixed(), pgstat_create_transactional(), pgstat_delete_pending_entry(), pgstat_drop_entry_internal(), pgstat_fetch_entry(), pgstat_flush_pending_entries(), pgstat_get_custom_shmem_data(), pgstat_get_custom_snapshot_data(), pgstat_get_entry_data(), pgstat_get_entry_len(), pgstat_have_entry(), pgstat_init_entry(), pgstat_init_snapshot_fixed(), pgstat_initialize(), pgstat_prep_pending_entry(), pgstat_read_statsfile(), pgstat_report_stat(), pgstat_reset(), pgstat_reset_after_failure(), pgstat_reset_entry(), pgstat_reset_of_kind(), pgstat_snapshot_fixed(), pgstat_write_statsfile(), shared_stat_reset_contents(), StatsShmemInit(), and StatsShmemSize().

◆ pgstat_get_xact_stack_level()

PgStat_SubXactStatus * pgstat_get_xact_stack_level ( int  nest_level)

Definition at line 238 of file pgstat_xact.c.

239{
240 PgStat_SubXactStatus *xact_state;
241
242 xact_state = pgStatXactStack;
243 if (xact_state == NULL || xact_state->nest_level != nest_level)
244 {
245 xact_state = (PgStat_SubXactStatus *)
247 sizeof(PgStat_SubXactStatus));
248 dclist_init(&xact_state->pending_drops);
249 xact_state->nest_level = nest_level;
250 xact_state->prev = pgStatXactStack;
251 xact_state->first = NULL;
252 pgStatXactStack = xact_state;
253 }
254 return xact_state;
255}
static void dclist_init(dclist_head *head)
Definition: ilist.h:671
MemoryContext TopTransactionContext
Definition: mcxt.c:170
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat_xact.c:33
struct PgStat_SubXactStatus * prev

References dclist_init(), PgStat_SubXactStatus::first, MemoryContextAlloc(), PgStat_SubXactStatus::nest_level, PgStat_SubXactStatus::pending_drops, pgStatXactStack, PgStat_SubXactStatus::prev, and TopTransactionContext.

Referenced by add_tabstat_xact_level(), AtEOSubXact_PgStat_DroppedStats(), AtEOSubXact_PgStat_Relations(), and create_drop_transactional_internal().

◆ pgstat_hash_hash_key()

static uint32 pgstat_hash_hash_key ( const void *  d,
size_t  size,
void *  arg 
)
inlinestatic

Definition at line 878 of file pgstat_internal.h.

879{
880 const char *key = (const char *) d;
881
882 Assert(size == sizeof(PgStat_HashKey) && arg == NULL);
883 return fasthash32(key, size, 0);
884}
static uint32 fasthash32(const char *k, size_t len, uint64 seed)

References arg, Assert(), fasthash32(), and sort-test::key.

◆ pgstat_init_entry()

PgStatShared_Common * pgstat_init_entry ( PgStat_Kind  kind,
PgStatShared_HashEntry shhashent 
)

Definition at line 293 of file pgstat_shmem.c.

295{
296 /* Create new stats entry. */
297 dsa_pointer chunk;
298 PgStatShared_Common *shheader;
299
300 /*
301 * Initialize refcount to 1, marking it as valid / not dropped. The entry
302 * can't be freed before the initialization because it can't be found as
303 * long as we hold the dshash partition lock. Caller needs to increase
304 * further if a longer lived reference is needed.
305 */
306 pg_atomic_init_u32(&shhashent->refcount, 1);
307
308 /*
309 * Initialize "generation" to 0, as freshly created.
310 */
311 pg_atomic_init_u32(&shhashent->generation, 0);
312 shhashent->dropped = false;
313
314 chunk = dsa_allocate0(pgStatLocal.dsa, pgstat_get_kind_info(kind)->shared_size);
315 shheader = dsa_get_address(pgStatLocal.dsa, chunk);
316 shheader->magic = 0xdeadbeef;
317
318 /* Link the new entry from the hash entry. */
319 shhashent->body = chunk;
320
322
323 return shheader;
324}
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:221
#define dsa_allocate0(area, size)
Definition: dsa.h:113
uint64 dsa_pointer
Definition: dsa.h:62
pg_atomic_uint32 refcount
pg_atomic_uint32 generation

References PgStatShared_HashEntry::body, PgStatShared_HashEntry::dropped, PgStat_LocalState::dsa, dsa_allocate0, dsa_get_address(), PgStatShared_HashEntry::generation, PgStatShared_Common::lock, LWLockInitialize(), LWTRANCHE_PGSTATS_DATA, PgStatShared_Common::magic, pg_atomic_init_u32(), pgstat_get_kind_info(), pgStatLocal, and PgStatShared_HashEntry::refcount.

Referenced by pgstat_get_entry_ref(), and pgstat_read_statsfile().

◆ pgstat_io_flush_cb()

bool pgstat_io_flush_cb ( bool  nowait)

Definition at line 197 of file pgstat_io.c.

198{
199 LWLock *bktype_lock;
200 PgStat_BktypeIO *bktype_shstats;
201
202 if (!have_iostats)
203 return false;
204
205 bktype_lock = &pgStatLocal.shmem->io.locks[MyBackendType];
206 bktype_shstats =
208
209 if (!nowait)
210 LWLockAcquire(bktype_lock, LW_EXCLUSIVE);
211 else if (!LWLockConditionalAcquire(bktype_lock, LW_EXCLUSIVE))
212 return true;
213
214 for (int io_object = 0; io_object < IOOBJECT_NUM_TYPES; io_object++)
215 {
216 for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++)
217 {
218 for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
219 {
220 instr_time time;
221
222 bktype_shstats->counts[io_object][io_context][io_op] +=
223 PendingIOStats.counts[io_object][io_context][io_op];
224
225 bktype_shstats->bytes[io_object][io_context][io_op] +=
226 PendingIOStats.bytes[io_object][io_context][io_op];
227
228 time = PendingIOStats.pending_times[io_object][io_context][io_op];
229
230 bktype_shstats->times[io_object][io_context][io_op] +=
232 }
233 }
234 }
235
237
238 LWLockRelease(bktype_lock);
239
240 memset(&PendingIOStats, 0, sizeof(PendingIOStats));
241
242 have_iostats = false;
243
244 return false;
245}
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1353
#define IOOP_NUM_TYPES
Definition: pgstat.h:316
#define IOCONTEXT_NUM_TYPES
Definition: pgstat.h:290
#define IOOBJECT_NUM_TYPES
Definition: pgstat.h:279
bool pgstat_bktype_io_stats_valid(PgStat_BktypeIO *backend_io, BackendType bktype)
Definition: pgstat_io.c:37
static PgStat_PendingIO PendingIOStats
Definition: pgstat_io.c:23
static bool have_iostats
Definition: pgstat_io.c:24
Definition: lwlock.h:42
LWLock locks[BACKEND_NUM_TYPES]
PgStat_Counter times[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:326
uint64 bytes[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:324
PgStat_Counter counts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:325
PgStat_BktypeIO stats[BACKEND_NUM_TYPES]
Definition: pgstat.h:339
PgStat_Counter counts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:332
uint64 bytes[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:331
instr_time pending_times[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:333
PgStatShared_IO io

References Assert(), PgStat_BktypeIO::bytes, PgStat_PendingIO::bytes, PgStat_BktypeIO::counts, PgStat_PendingIO::counts, have_iostats, INSTR_TIME_GET_MICROSEC, PgStat_ShmemControl::io, IOCONTEXT_NUM_TYPES, IOOBJECT_NUM_TYPES, IOOP_NUM_TYPES, PgStatShared_IO::locks, LW_EXCLUSIVE, LWLockAcquire(), LWLockConditionalAcquire(), LWLockRelease(), MyBackendType, PgStat_PendingIO::pending_times, PendingIOStats, pgstat_bktype_io_stats_valid(), pgStatLocal, PgStat_LocalState::shmem, PgStat_IO::stats, PgStatShared_IO::stats, and PgStat_BktypeIO::times.

Referenced by pgstat_flush_io().

◆ pgstat_io_have_pending_cb()

bool pgstat_io_have_pending_cb ( void  )

Definition at line 174 of file pgstat_io.c.

175{
176 return have_iostats;
177}

References have_iostats.

◆ pgstat_io_init_shmem_cb()

void pgstat_io_init_shmem_cb ( void *  stats)

Definition at line 286 of file pgstat_io.c.

287{
288 PgStatShared_IO *stat_shmem = (PgStatShared_IO *) stats;
289
290 for (int i = 0; i < BACKEND_NUM_TYPES; i++)
292}
int i
Definition: isn.c:77
#define BACKEND_NUM_TYPES
Definition: miscadmin.h:377

References BACKEND_NUM_TYPES, i, PgStatShared_IO::locks, LWLockInitialize(), and LWTRANCHE_PGSTATS_DATA.

◆ pgstat_io_reset_all_cb()

void pgstat_io_reset_all_cb ( TimestampTz  ts)

Definition at line 295 of file pgstat_io.c.

296{
297 for (int i = 0; i < BACKEND_NUM_TYPES; i++)
298 {
299 LWLock *bktype_lock = &pgStatLocal.shmem->io.locks[i];
300 PgStat_BktypeIO *bktype_shstats = &pgStatLocal.shmem->io.stats.stats[i];
301
302 LWLockAcquire(bktype_lock, LW_EXCLUSIVE);
303
304 /*
305 * Use the lock in the first BackendType's PgStat_BktypeIO to protect
306 * the reset timestamp as well.
307 */
308 if (i == 0)
310
311 memset(bktype_shstats, 0, sizeof(*bktype_shstats));
312 LWLockRelease(bktype_lock);
313 }
314}
TimestampTz stat_reset_timestamp
Definition: pgstat.h:338

References BACKEND_NUM_TYPES, i, PgStat_ShmemControl::io, PgStatShared_IO::locks, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), pgStatLocal, PgStat_LocalState::shmem, PgStat_IO::stat_reset_timestamp, PgStat_IO::stats, and PgStatShared_IO::stats.

◆ pgstat_io_snapshot_cb()

void pgstat_io_snapshot_cb ( void  )

Definition at line 317 of file pgstat_io.c.

318{
319 for (int i = 0; i < BACKEND_NUM_TYPES; i++)
320 {
321 LWLock *bktype_lock = &pgStatLocal.shmem->io.locks[i];
322 PgStat_BktypeIO *bktype_shstats = &pgStatLocal.shmem->io.stats.stats[i];
323 PgStat_BktypeIO *bktype_snap = &pgStatLocal.snapshot.io.stats[i];
324
325 LWLockAcquire(bktype_lock, LW_SHARED);
326
327 /*
328 * Use the lock in the first BackendType's PgStat_BktypeIO to protect
329 * the reset timestamp as well.
330 */
331 if (i == 0)
334
335 /* using struct assignment due to better type safety */
336 *bktype_snap = *bktype_shstats;
337 LWLockRelease(bktype_lock);
338 }
339}

References BACKEND_NUM_TYPES, i, PgStat_ShmemControl::io, PgStat_Snapshot::io, PgStatShared_IO::locks, LW_SHARED, LWLockAcquire(), LWLockRelease(), pgStatLocal, PgStat_LocalState::shmem, PgStat_LocalState::snapshot, PgStat_IO::stat_reset_timestamp, PgStat_IO::stats, and PgStatShared_IO::stats.

◆ pgstat_lock_entry()

◆ pgstat_lock_entry_shared()

bool pgstat_lock_entry_shared ( PgStat_EntryRef entry_ref,
bool  nowait 
)

Definition at line 672 of file pgstat_shmem.c.

673{
674 LWLock *lock = &entry_ref->shared_stats->lock;
675
676 if (nowait)
678
680 return true;
681}

References PgStatShared_Common::lock, LW_SHARED, LWLockAcquire(), LWLockConditionalAcquire(), and PgStat_EntryRef::shared_stats.

Referenced by pgstat_fetch_entry().

◆ pgstat_prep_database_pending()

PgStat_StatDBEntry * pgstat_prep_database_pending ( Oid  dboid)

Definition at line 386 of file pgstat_database.c.

387{
388 PgStat_EntryRef *entry_ref;
389
390 /*
391 * This should not report stats on database objects before having
392 * connected to a database.
393 */
395
397 NULL);
398
399 return entry_ref->pending;
400}
#define OidIsValid(objectId)
Definition: c.h:746
Oid MyDatabaseId
Definition: globals.c:95
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *created_entry)
Definition: pgstat.c:1280

References Assert(), InvalidOid, MyDatabaseId, OidIsValid, PgStat_EntryRef::pending, PGSTAT_KIND_DATABASE, and pgstat_prep_pending_entry().

Referenced by pgstat_relation_flush_cb(), pgstat_report_connect(), pgstat_report_deadlock(), pgstat_report_disconnect(), pgstat_report_recovery_conflict(), pgstat_report_tempfile(), pgstat_update_dbstats(), and pgstat_update_parallel_workers_stats().

◆ pgstat_prep_pending_entry()

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

Definition at line 1280 of file pgstat.c.

1282{
1283 PgStat_EntryRef *entry_ref;
1284
1285 /* need to be able to flush out */
1286 Assert(pgstat_get_kind_info(kind)->flush_pending_cb != NULL);
1287
1289 {
1292 "PgStat Pending",
1294 }
1295
1296 entry_ref = pgstat_get_entry_ref(kind, dboid, objid,
1297 true, created_entry);
1298
1299 if (entry_ref->pending == NULL)
1300 {
1301 size_t entrysize = pgstat_get_kind_info(kind)->pending_size;
1302
1303 Assert(entrysize != (size_t) -1);
1304
1305 entry_ref->pending = MemoryContextAllocZero(pgStatPendingContext, entrysize);
1307 }
1308
1309 return entry_ref;
#define unlikely(x)
Definition: c.h:347
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:364
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1294
#define AllocSetContextCreate
Definition: memutils.h:149
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:190
static MemoryContext pgStatPendingContext
Definition: pgstat.c:229
static dlist_head pgStatPending
Definition: pgstat.c:237

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, Assert(), dlist_push_tail(), 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_create_inj(), pgstat_init_function_usage(), pgstat_prep_database_pending(), pgstat_prep_relation_pending(), pgstat_report_inj(), pgstat_report_subscription_conflict(), and pgstat_report_subscription_error().

◆ pgstat_register_kind()

void pgstat_register_kind ( PgStat_Kind  kind,
const PgStat_KindInfo kind_info 
)

Definition at line 1478 of file pgstat.c.

1480{
1482
1483 if (kind_info->name == NULL || strlen(kind_info->name) == 0)
1484 ereport(ERROR,
1485 (errmsg("custom cumulative statistics name is invalid"),
1486 errhint("Provide a non-empty name for the custom cumulative statistics.")));
1487
1488 if (!pgstat_is_kind_custom(kind))
1489 ereport(ERROR, (errmsg("custom cumulative statistics ID %u is out of range", kind),
1490 errhint("Provide a custom cumulative statistics ID between %u and %u.",
1492
1494 ereport(ERROR,
1495 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1496 errdetail("Custom cumulative statistics must be registered while initializing modules in \"shared_preload_libraries\".")));
1497
1498 /*
1499 * Check some data for fixed-numbered stats.
1500 */
1501 if (kind_info->fixed_amount)
1502 {
1503 if (kind_info->shared_size == 0)
1504 ereport(ERROR,
1505 (errmsg("custom cumulative statistics property is invalid"),
1506 errhint("Custom cumulative statistics require a shared memory size for fixed-numbered objects.")));
1507 }
1508
1509 /*
1510 * If pgstat_kind_custom_infos is not available yet, allocate it.
1511 */
1512 if (pgstat_kind_custom_infos == NULL)
1513 {
1517 }
1518
1519 if (pgstat_kind_custom_infos[idx] != NULL &&
1521 ereport(ERROR,
1522 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1523 errdetail("Custom cumulative statistics \"%s\" already registered with the same ID.",
1525
1526 /* check for existing custom stats with the same name */
1527 for (PgStat_Kind existing_kind = PGSTAT_KIND_CUSTOM_MIN; existing_kind <= PGSTAT_KIND_CUSTOM_MAX; existing_kind++)
1528 {
1529 uint32 existing_idx = existing_kind - PGSTAT_KIND_CUSTOM_MIN;
1530
1531 if (pgstat_kind_custom_infos[existing_idx] == NULL)
1532 continue;
1533 if (!pg_strcasecmp(pgstat_kind_custom_infos[existing_idx]->name, kind_info->name))
1534 ereport(ERROR,
1535 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1536 errdetail("Existing cumulative statistics with ID %u has the same name.", existing_kind)));
1537 }
1538
1539 /* Register it */
1540 pgstat_kind_custom_infos[idx] = kind_info;
1541 ereport(LOG,
1542 (errmsg("registered custom cumulative statistics \"%s\" with ID %u",
1543 kind_info->name, kind)));
int errdetail(const char *fmt,...)
Definition: elog.c:1204
int errhint(const char *fmt,...)
Definition: elog.c:1318
#define LOG
Definition: elog.h:31
#define ERROR
Definition: elog.h:39
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1837
#define PGSTAT_KIND_CUSTOM_MAX
Definition: pgstat_kind.h:50
#define PGSTAT_KIND_CUSTOM_SIZE
Definition: pgstat_kind.h:51
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
const char *const name

References ereport, errdetail(), errhint(), errmsg(), ERROR, PgStat_KindInfo::fixed_amount, 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, PgStat_KindInfo::shared_size, and TopMemoryContext.

Referenced by pgstat_register_inj(), and pgstat_register_inj_fixed().

◆ pgstat_relation_delete_pending_cb()

void pgstat_relation_delete_pending_cb ( PgStat_EntryRef entry_ref)

Definition at line 905 of file pgstat_relation.c.

906{
907 PgStat_TableStatus *pending = (PgStat_TableStatus *) entry_ref->pending;
908
909 if (pending->relation)
911}
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
void pgstat_unlink_relation(Relation rel)
Relation relation
Definition: pgstat.h:179

References if(), PgStat_EntryRef::pending, pgstat_unlink_relation(), and PgStat_TableStatus::relation.

◆ pgstat_relation_flush_cb()

bool pgstat_relation_flush_cb ( PgStat_EntryRef entry_ref,
bool  nowait 
)

Definition at line 816 of file pgstat_relation.c.

817{
818 Oid dboid;
819 PgStat_TableStatus *lstats; /* pending stats entry */
820 PgStatShared_Relation *shtabstats;
821 PgStat_StatTabEntry *tabentry; /* table entry of shared stats */
822 PgStat_StatDBEntry *dbentry; /* pending database entry */
823
824 dboid = entry_ref->shared_entry->key.dboid;
825 lstats = (PgStat_TableStatus *) entry_ref->pending;
826 shtabstats = (PgStatShared_Relation *) entry_ref->shared_stats;
827
828 /*
829 * Ignore entries that didn't accumulate any actual counts, such as
830 * indexes that were opened by the planner but not used.
831 */
832 if (pg_memory_is_all_zeros(&lstats->counts,
833 sizeof(struct PgStat_TableCounts)))
834 return true;
835
836 if (!pgstat_lock_entry(entry_ref, nowait))
837 return false;
838
839 /* add the values to the shared entry. */
840 tabentry = &shtabstats->stats;
841
842 tabentry->numscans += lstats->counts.numscans;
843 if (lstats->counts.numscans)
844 {
846
847 if (t > tabentry->lastscan)
848 tabentry->lastscan = t;
849 }
850 tabentry->tuples_returned += lstats->counts.tuples_returned;
851 tabentry->tuples_fetched += lstats->counts.tuples_fetched;
852 tabentry->tuples_inserted += lstats->counts.tuples_inserted;
853 tabentry->tuples_updated += lstats->counts.tuples_updated;
854 tabentry->tuples_deleted += lstats->counts.tuples_deleted;
855 tabentry->tuples_hot_updated += lstats->counts.tuples_hot_updated;
857
858 /*
859 * If table was truncated/dropped, first reset the live/dead counters.
860 */
861 if (lstats->counts.truncdropped)
862 {
863 tabentry->live_tuples = 0;
864 tabentry->dead_tuples = 0;
865 tabentry->ins_since_vacuum = 0;
866 }
867
868 tabentry->live_tuples += lstats->counts.delta_live_tuples;
869 tabentry->dead_tuples += lstats->counts.delta_dead_tuples;
870 tabentry->mod_since_analyze += lstats->counts.changed_tuples;
871
872 /*
873 * Using tuples_inserted to update ins_since_vacuum does mean that we'll
874 * track aborted inserts too. This isn't ideal, but otherwise probably
875 * not worth adding an extra field for. It may just amount to autovacuums
876 * triggering for inserts more often than they maybe should, which is
877 * probably not going to be common enough to be too concerned about here.
878 */
879 tabentry->ins_since_vacuum += lstats->counts.tuples_inserted;
880
881 tabentry->blocks_fetched += lstats->counts.blocks_fetched;
882 tabentry->blocks_hit += lstats->counts.blocks_hit;
883
884 /* Clamp live_tuples in case of negative delta_live_tuples */
885 tabentry->live_tuples = Max(tabentry->live_tuples, 0);
886 /* Likewise for dead_tuples */
887 tabentry->dead_tuples = Max(tabentry->dead_tuples, 0);
888
889 pgstat_unlock_entry(entry_ref);
890
891 /* The entry was successfully flushed, add the same to database stats */
892 dbentry = pgstat_prep_database_pending(dboid);
893 dbentry->tuples_returned += lstats->counts.tuples_returned;
894 dbentry->tuples_fetched += lstats->counts.tuples_fetched;
895 dbentry->tuples_inserted += lstats->counts.tuples_inserted;
896 dbentry->tuples_updated += lstats->counts.tuples_updated;
897 dbentry->tuples_deleted += lstats->counts.tuples_deleted;
898 dbentry->blocks_fetched += lstats->counts.blocks_fetched;
899 dbentry->blocks_hit += lstats->counts.blocks_hit;
900
901 return true;
902}
#define Max(x, y)
Definition: c.h:969
int64 TimestampTz
Definition: timestamp.h:39
static bool pg_memory_is_all_zeros(const void *ptr, size_t len)
Definition: memutils.h:239
PgStat_StatDBEntry * pgstat_prep_database_pending(Oid dboid)
unsigned int Oid
Definition: postgres_ext.h:30
PgStat_StatTabEntry stats
PgStat_Counter tuples_updated
Definition: pgstat.h:351
PgStat_Counter tuples_inserted
Definition: pgstat.h:350
PgStat_Counter tuples_returned
Definition: pgstat.h:348
PgStat_Counter blocks_hit
Definition: pgstat.h:347
PgStat_Counter blocks_fetched
Definition: pgstat.h:346
PgStat_Counter tuples_deleted
Definition: pgstat.h:352
PgStat_Counter tuples_fetched
Definition: pgstat.h:349
PgStat_Counter tuples_fetched
Definition: pgstat.h:427
PgStat_Counter ins_since_vacuum
Definition: pgstat.h:438
PgStat_Counter blocks_hit
Definition: pgstat.h:441
PgStat_Counter mod_since_analyze
Definition: pgstat.h:437
PgStat_Counter tuples_deleted
Definition: pgstat.h:431
PgStat_Counter tuples_hot_updated
Definition: pgstat.h:432
PgStat_Counter tuples_updated
Definition: pgstat.h:430
PgStat_Counter live_tuples
Definition: pgstat.h:435
PgStat_Counter numscans
Definition: pgstat.h:423
PgStat_Counter dead_tuples
Definition: pgstat.h:436
PgStat_Counter blocks_fetched
Definition: pgstat.h:440
PgStat_Counter tuples_returned
Definition: pgstat.h:426
TimestampTz lastscan
Definition: pgstat.h:424
PgStat_Counter tuples_inserted
Definition: pgstat.h:429
PgStat_Counter tuples_newpage_updated
Definition: pgstat.h:433
PgStat_Counter blocks_hit
Definition: pgstat.h:155
PgStat_Counter numscans
Definition: pgstat.h:138
PgStat_Counter tuples_hot_updated
Definition: pgstat.h:146
PgStat_Counter tuples_returned
Definition: pgstat.h:140
PgStat_Counter blocks_fetched
Definition: pgstat.h:154
PgStat_Counter tuples_fetched
Definition: pgstat.h:141
PgStat_Counter tuples_newpage_updated
Definition: pgstat.h:147
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition: xact.c:891

References PgStat_TableCounts::blocks_fetched, PgStat_StatDBEntry::blocks_fetched, PgStat_StatTabEntry::blocks_fetched, PgStat_TableCounts::blocks_hit, PgStat_StatDBEntry::blocks_hit, PgStat_StatTabEntry::blocks_hit, PgStat_TableCounts::changed_tuples, PgStat_TableStatus::counts, PgStat_HashKey::dboid, PgStat_StatTabEntry::dead_tuples, PgStat_TableCounts::delta_dead_tuples, PgStat_TableCounts::delta_live_tuples, GetCurrentTransactionStopTimestamp(), PgStat_StatTabEntry::ins_since_vacuum, PgStatShared_HashEntry::key, PgStat_StatTabEntry::lastscan, PgStat_StatTabEntry::live_tuples, Max, PgStat_StatTabEntry::mod_since_analyze, PgStat_TableCounts::numscans, PgStat_StatTabEntry::numscans, PgStat_EntryRef::pending, pg_memory_is_all_zeros(), pgstat_lock_entry(), pgstat_prep_database_pending(), pgstat_unlock_entry(), PgStat_EntryRef::shared_entry, PgStat_EntryRef::shared_stats, PgStatShared_Relation::stats, PgStat_TableCounts::truncdropped, PgStat_TableCounts::tuples_deleted, PgStat_StatDBEntry::tuples_deleted, PgStat_StatTabEntry::tuples_deleted, PgStat_TableCounts::tuples_fetched, PgStat_StatDBEntry::tuples_fetched, PgStat_StatTabEntry::tuples_fetched, PgStat_TableCounts::tuples_hot_updated, PgStat_StatTabEntry::tuples_hot_updated, PgStat_TableCounts::tuples_inserted, PgStat_StatDBEntry::tuples_inserted, PgStat_StatTabEntry::tuples_inserted, PgStat_TableCounts::tuples_newpage_updated, PgStat_StatTabEntry::tuples_newpage_updated, PgStat_TableCounts::tuples_returned, PgStat_StatDBEntry::tuples_returned, PgStat_StatTabEntry::tuples_returned, PgStat_TableCounts::tuples_updated, PgStat_StatDBEntry::tuples_updated, and PgStat_StatTabEntry::tuples_updated.

◆ pgstat_replslot_from_serialized_name_cb()

bool pgstat_replslot_from_serialized_name_cb ( const NameData name,
PgStat_HashKey key 
)

Definition at line 202 of file pgstat_replslot.c.

203{
204 int idx = get_replslot_index(NameStr(*name), true);
205
206 /* slot might have been deleted */
207 if (idx == -1)
208 return false;
209
211 key->dboid = InvalidOid;
212 key->objid = idx;
213
214 return true;
215}
#define NameStr(name)
Definition: c.h:717
#define PGSTAT_KIND_REPLSLOT
Definition: pgstat_kind.h:30
static int get_replslot_index(const char *name, bool need_lock)

References get_replslot_index(), idx(), InvalidOid, sort-test::key, name, NameStr, and PGSTAT_KIND_REPLSLOT.

◆ pgstat_replslot_reset_timestamp_cb()

void pgstat_replslot_reset_timestamp_cb ( PgStatShared_Common header,
TimestampTz  ts 
)

Definition at line 218 of file pgstat_replslot.c.

219{
220 ((PgStatShared_ReplSlot *) header)->stats.stat_reset_timestamp = ts;
221}

◆ pgstat_replslot_to_serialized_name_cb()

void pgstat_replslot_to_serialized_name_cb ( const PgStat_HashKey key,
const PgStatShared_Common header,
NameData name 
)

Definition at line 189 of file pgstat_replslot.c.

190{
191 /*
192 * This is only called late during shutdown. The set of existing slots
193 * isn't allowed to change at this point, we can assume that a slot exists
194 * at the offset.
195 */
196 if (!ReplicationSlotName(key->objid, name))
197 elog(ERROR, "could not find name for replication slot index %" PRIu64,
198 key->objid);
199}
#define elog(elevel,...)
Definition: elog.h:225
bool ReplicationSlotName(int index, Name name)
Definition: slot.c:528

References elog, ERROR, sort-test::key, name, and ReplicationSlotName().

◆ pgstat_report_disconnect()

void pgstat_report_disconnect ( Oid  dboid)

Definition at line 244 of file pgstat_database.c.

245{
246 PgStat_StatDBEntry *dbentry;
247
249 return;
250
252
253 switch (pgStatSessionEndCause)
254 {
257 /* we don't collect these */
258 break;
260 dbentry->sessions_abandoned++;
261 break;
262 case DISCONNECT_FATAL:
263 dbentry->sessions_fatal++;
264 break;
266 dbentry->sessions_killed++;
267 break;
268 }
269}
@ DISCONNECT_NOT_YET
Definition: pgstat.h:54
@ DISCONNECT_FATAL
Definition: pgstat.h:57
@ DISCONNECT_KILLED
Definition: pgstat.h:58
@ DISCONNECT_CLIENT_EOF
Definition: pgstat.h:56
@ DISCONNECT_NORMAL
Definition: pgstat.h:55
static bool pgstat_should_report_connstat(void)
SessionEndType pgStatSessionEndCause
PgStat_Counter sessions_fatal
Definition: pgstat.h:372
PgStat_Counter sessions_abandoned
Definition: pgstat.h:371
PgStat_Counter sessions_killed
Definition: pgstat.h:373

References DISCONNECT_CLIENT_EOF, DISCONNECT_FATAL, DISCONNECT_KILLED, DISCONNECT_NORMAL, DISCONNECT_NOT_YET, MyDatabaseId, pgstat_prep_database_pending(), pgstat_should_report_connstat(), pgStatSessionEndCause, PgStat_StatDBEntry::sessions_abandoned, PgStat_StatDBEntry::sessions_fatal, and PgStat_StatDBEntry::sessions_killed.

Referenced by pgstat_shutdown_hook().

◆ pgstat_request_entry_refs_gc()

◆ pgstat_reset_database_timestamp()

void pgstat_reset_database_timestamp ( Oid  dboid,
TimestampTz  ts 
)

◆ pgstat_reset_entries_of_kind()

void pgstat_reset_entries_of_kind ( PgStat_Kind  kind,
TimestampTz  ts 
)

Definition at line 1129 of file pgstat_shmem.c.

1130{
1132}
static bool match_kind(PgStatShared_HashEntry *p, Datum match_data)
void pgstat_reset_matching_entries(bool(*do_reset)(PgStatShared_HashEntry *, Datum), Datum match_data, TimestampTz ts)
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217

References Int32GetDatum(), match_kind(), and pgstat_reset_matching_entries().

Referenced by pgstat_reset_of_kind().

◆ pgstat_reset_entry()

void pgstat_reset_entry ( PgStat_Kind  kind,
Oid  dboid,
uint64  objid,
TimestampTz  ts 
)

Definition at line 1073 of file pgstat_shmem.c.

1074{
1075 PgStat_EntryRef *entry_ref;
1076
1077 Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1078
1079 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
1080 if (!entry_ref || entry_ref->shared_entry->dropped)
1081 return;
1082
1083 (void) pgstat_lock_entry(entry_ref, false);
1084 shared_stat_reset_contents(kind, entry_ref->shared_stats, ts);
1085 pgstat_unlock_entry(entry_ref);
1086}
static void shared_stat_reset_contents(PgStat_Kind kind, PgStatShared_Common *header, TimestampTz ts)

References Assert(), PgStatShared_HashEntry::dropped, pgstat_get_entry_ref(), pgstat_get_kind_info(), pgstat_lock_entry(), pgstat_unlock_entry(), PgStat_EntryRef::shared_entry, shared_stat_reset_contents(), and PgStat_EntryRef::shared_stats.

Referenced by pgstat_create_subscription(), and pgstat_reset().

◆ pgstat_reset_matching_entries()

void pgstat_reset_matching_entries ( bool(*)(PgStatShared_HashEntry *, Datum do_reset,
Datum  match_data,
TimestampTz  ts 
)

Definition at line 1093 of file pgstat_shmem.c.

1095{
1096 dshash_seq_status hstat;
1098
1099 /* dshash entry is not modified, take shared lock */
1100 dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
1101 while ((p = dshash_seq_next(&hstat)) != NULL)
1102 {
1103 PgStatShared_Common *header;
1104
1105 if (p->dropped)
1106 continue;
1107
1108 if (!do_reset(p, match_data))
1109 continue;
1110
1111 header = dsa_get_address(pgStatLocal.dsa, p->body);
1112
1113 LWLockAcquire(&header->lock, LW_EXCLUSIVE);
1114
1115 shared_stat_reset_contents(p->key.kind, header, ts);
1116
1117 LWLockRelease(&header->lock);
1118 }
1119 dshash_seq_term(&hstat);
1120}

References PgStatShared_HashEntry::body, PgStatShared_HashEntry::dropped, PgStat_LocalState::dsa, dsa_get_address(), dshash_seq_init(), dshash_seq_next(), dshash_seq_term(), PgStatShared_HashEntry::key, PgStat_HashKey::kind, PgStatShared_Common::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), pgStatLocal, PgStat_LocalState::shared_hash, and shared_stat_reset_contents().

Referenced by pgstat_reset_counters(), and pgstat_reset_entries_of_kind().

◆ pgstat_slru_flush_cb()

bool pgstat_slru_flush_cb ( bool  nowait)

Definition at line 165 of file pgstat_slru.c.

166{
167 PgStatShared_SLRU *stats_shmem = &pgStatLocal.shmem->slru;
168 int i;
169
170 if (!have_slrustats)
171 return false;
172
173 if (!nowait)
174 LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
175 else if (!LWLockConditionalAcquire(&stats_shmem->lock, LW_EXCLUSIVE))
176 return true;
177
178 for (i = 0; i < SLRU_NUM_ELEMENTS; i++)
179 {
180 PgStat_SLRUStats *sharedent = &stats_shmem->stats[i];
181 PgStat_SLRUStats *pendingent = &pending_SLRUStats[i];
182
183#define SLRU_ACC(fld) sharedent->fld += pendingent->fld
184 SLRU_ACC(blocks_zeroed);
185 SLRU_ACC(blocks_hit);
186 SLRU_ACC(blocks_read);
188 SLRU_ACC(blocks_exists);
189 SLRU_ACC(flush);
190 SLRU_ACC(truncate);
191#undef SLRU_ACC
192 }
193
194 /* done, clear the pending entry */
196
197 LWLockRelease(&stats_shmem->lock);
198
199 have_slrustats = false;
200
201 return false;
202}
#define MemSet(start, val, len)
Definition: c.h:991
static int64 blocks_written
Definition: pg_checksums.c:38
#define SLRU_NUM_ELEMENTS
static bool have_slrustats
Definition: pgstat_slru.c:35
static PgStat_SLRUStats pending_SLRUStats[SLRU_NUM_ELEMENTS]
Definition: pgstat_slru.c:34
#define SLRU_ACC(fld)
PgStat_SLRUStats stats[SLRU_NUM_ELEMENTS]
PgStatShared_SLRU slru

References blocks_written, have_slrustats, i, PgStatShared_SLRU::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockConditionalAcquire(), LWLockRelease(), MemSet, pending_SLRUStats, pgStatLocal, PgStat_LocalState::shmem, PgStat_ShmemControl::slru, SLRU_ACC, SLRU_NUM_ELEMENTS, and PgStatShared_SLRU::stats.

◆ pgstat_slru_have_pending_cb()

bool pgstat_slru_have_pending_cb ( void  )

Definition at line 150 of file pgstat_slru.c.

151{
152 return have_slrustats;
153}

References have_slrustats.

◆ pgstat_slru_init_shmem_cb()

void pgstat_slru_init_shmem_cb ( void *  stats)

Definition at line 205 of file pgstat_slru.c.

206{
207 PgStatShared_SLRU *stats_shmem = (PgStatShared_SLRU *) stats;
208
210}

References PgStatShared_SLRU::lock, LWLockInitialize(), and LWTRANCHE_PGSTATS_DATA.

◆ pgstat_slru_reset_all_cb()

void pgstat_slru_reset_all_cb ( TimestampTz  ts)

Definition at line 213 of file pgstat_slru.c.

214{
215 for (int i = 0; i < SLRU_NUM_ELEMENTS; i++)
217}
static void pgstat_reset_slru_counter_internal(int index, TimestampTz ts)
Definition: pgstat_slru.c:255

References i, pgstat_reset_slru_counter_internal(), and SLRU_NUM_ELEMENTS.

◆ pgstat_slru_snapshot_cb()

void pgstat_slru_snapshot_cb ( void  )

Definition at line 220 of file pgstat_slru.c.

221{
222 PgStatShared_SLRU *stats_shmem = &pgStatLocal.shmem->slru;
223
224 LWLockAcquire(&stats_shmem->lock, LW_SHARED);
225
226 memcpy(pgStatLocal.snapshot.slru, &stats_shmem->stats,
227 sizeof(stats_shmem->stats));
228
229 LWLockRelease(&stats_shmem->lock);
230}
PgStat_SLRUStats slru[SLRU_NUM_ELEMENTS]

References PgStatShared_SLRU::lock, LW_SHARED, LWLockAcquire(), LWLockRelease(), pgStatLocal, PgStat_LocalState::shmem, PgStat_ShmemControl::slru, PgStat_Snapshot::slru, PgStat_LocalState::snapshot, and PgStatShared_SLRU::stats.

◆ pgstat_snapshot_fixed()

void pgstat_snapshot_fixed ( PgStat_Kind  kind)

Definition at line 1075 of file pgstat.c.

1077{
1079 Assert(pgstat_get_kind_info(kind)->fixed_amount);
1080
1083
1086 else
1088
1089 if (pgstat_is_kind_builtin(kind))
1091 else if (pgstat_is_kind_custom(kind))
static void pgstat_build_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1225
static bool pgstat_is_kind_valid(PgStat_Kind kind)
Definition: pgstat.c:1444
void pgstat_clear_snapshot(void)
Definition: pgstat.c:914
static bool force_stats_snapshot_clear
Definition: pgstat.c:250
bool custom_valid[PGSTAT_KIND_CUSTOM_SIZE]
bool fixed_valid[PGSTAT_KIND_BUILTIN_SIZE]

References Assert(), PgStat_Snapshot::custom_valid, PgStat_Snapshot::fixed_valid, force_stats_snapshot_clear, pgstat_build_snapshot(), pgstat_build_snapshot_fixed(), pgstat_clear_snapshot(), pgstat_fetch_consistency, PGSTAT_FETCH_CONSISTENCY_SNAPSHOT, pgstat_get_kind_info(), pgstat_is_kind_builtin(), pgstat_is_kind_custom(), pgstat_is_kind_valid(), PGSTAT_KIND_CUSTOM_MIN, pgStatLocal, and PgStat_LocalState::snapshot.

Referenced by injection_points_stats_fixed(), pgstat_fetch_slru(), pgstat_fetch_stat_archiver(), pgstat_fetch_stat_bgwriter(), pgstat_fetch_stat_checkpointer(), pgstat_fetch_stat_io(), and pgstat_fetch_stat_wal().

◆ pgstat_subscription_flush_cb()

bool pgstat_subscription_flush_cb ( PgStat_EntryRef entry_ref,
bool  nowait 
)

Definition at line 103 of file pgstat_subscription.c.

104{
105 PgStat_BackendSubEntry *localent;
107
108 localent = (PgStat_BackendSubEntry *) entry_ref->pending;
109 shsubent = (PgStatShared_Subscription *) entry_ref->shared_stats;
110
111 /* localent always has non-zero content */
112
113 if (!pgstat_lock_entry(entry_ref, nowait))
114 return false;
115
116#define SUB_ACC(fld) shsubent->stats.fld += localent->fld
117 SUB_ACC(apply_error_count);
118 SUB_ACC(sync_error_count);
119 for (int i = 0; i < CONFLICT_NUM_TYPES; i++)
120 SUB_ACC(conflict_count[i]);
121#undef SUB_ACC
122
123 pgstat_unlock_entry(entry_ref);
124 return true;
125}
#define CONFLICT_NUM_TYPES
Definition: conflict.h:54
#define SUB_ACC(fld)

References CONFLICT_NUM_TYPES, i, PgStat_EntryRef::pending, pgstat_lock_entry(), pgstat_unlock_entry(), PgStat_EntryRef::shared_stats, and SUB_ACC.

◆ pgstat_subscription_reset_timestamp_cb()

void pgstat_subscription_reset_timestamp_cb ( PgStatShared_Common header,
TimestampTz  ts 
)

Definition at line 128 of file pgstat_subscription.c.

129{
130 ((PgStatShared_Subscription *) header)->stats.stat_reset_timestamp = ts;
131}

◆ pgstat_unlock_entry()

◆ pgstat_update_dbstats()

void pgstat_update_dbstats ( TimestampTz  ts)

Definition at line 323 of file pgstat_database.c.

324{
325 PgStat_StatDBEntry *dbentry;
326
327 /*
328 * If not connected to a database yet, don't attribute time to "shared
329 * state" (InvalidOid is used to track stats for shared relations, etc.).
330 */
332 return;
333
335
336 /*
337 * Accumulate xact commit/rollback and I/O timings to stats entry of the
338 * current database.
339 */
340 dbentry->xact_commit += pgStatXactCommit;
344
346 {
347 long secs;
348 int usecs;
349
350 /*
351 * pgLastSessionReportTime is initialized to MyStartTimestamp by
352 * pgstat_report_connect().
353 */
356 dbentry->session_time += (PgStat_Counter) secs * 1000000 + usecs;
357 dbentry->active_time += pgStatActiveTime;
359 }
360
367}
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1721
int64 PgStat_Counter
Definition: pgstat.h:65
PgStat_Counter pgStatActiveTime
PgStat_Counter pgStatBlockReadTime
PgStat_Counter pgStatBlockWriteTime
static PgStat_Counter pgLastSessionReportTime
PgStat_Counter pgStatTransactionIdleTime
PgStat_Counter blk_write_time
Definition: pgstat.h:366
PgStat_Counter xact_rollback
Definition: pgstat.h:345
PgStat_Counter blk_read_time
Definition: pgstat.h:365
PgStat_Counter xact_commit
Definition: pgstat.h:344
PgStat_Counter session_time
Definition: pgstat.h:368
PgStat_Counter active_time
Definition: pgstat.h:369
PgStat_Counter idle_in_transaction_time
Definition: pgstat.h:370

References PgStat_StatDBEntry::active_time, PgStat_StatDBEntry::blk_read_time, PgStat_StatDBEntry::blk_write_time, PgStat_StatDBEntry::idle_in_transaction_time, MyDatabaseId, OidIsValid, pgLastSessionReportTime, pgstat_prep_database_pending(), pgstat_should_report_connstat(), pgStatActiveTime, pgStatBlockReadTime, pgStatBlockWriteTime, pgStatTransactionIdleTime, pgStatXactCommit, pgStatXactRollback, PgStat_StatDBEntry::session_time, TimestampDifference(), PgStat_StatDBEntry::xact_commit, and PgStat_StatDBEntry::xact_rollback.

Referenced by pgstat_report_stat().

◆ pgstat_wal_flush_cb()

bool pgstat_wal_flush_cb ( bool  nowait)

Definition at line 82 of file pgstat_wal.c.

83{
84 PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
85 WalUsage wal_usage_diff = {0};
86
88 Assert(pgStatLocal.shmem != NULL &&
90
91 /*
92 * This function can be called even if nothing at all has happened. Avoid
93 * taking lock for nothing in that case.
94 */
96 return false;
97
98 /*
99 * We don't update the WAL usage portion of the local WalStats elsewhere.
100 * Calculate how much WAL usage counters were increased by subtracting the
101 * previous counters from the current ones.
102 */
103 WalUsageAccumDiff(&wal_usage_diff, &pgWalUsage, &prevWalUsage);
104
105 if (!nowait)
106 LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
107 else if (!LWLockConditionalAcquire(&stats_shmem->lock, LW_EXCLUSIVE))
108 return true;
109
110#define WALSTAT_ACC(fld, var_to_add) \
111 (stats_shmem->stats.wal_counters.fld += var_to_add.fld)
112 WALSTAT_ACC(wal_records, wal_usage_diff);
113 WALSTAT_ACC(wal_fpi, wal_usage_diff);
114 WALSTAT_ACC(wal_bytes, wal_usage_diff);
115 WALSTAT_ACC(wal_buffers_full, wal_usage_diff);
116#undef WALSTAT_ACC
117
118 LWLockRelease(&stats_shmem->lock);
119
120 /*
121 * Save the current counters for the subsequent calculation of WAL usage.
122 */
124
125 return false;
126}
WalUsage pgWalUsage
Definition: instrument.c:22
void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
Definition: instrument.c:287
#define WALSTAT_ACC(fld, var_to_add)
static WalUsage prevWalUsage
Definition: pgstat_wal.c:30
bool pgstat_wal_have_pending_cb(void)
Definition: pgstat_wal.c:143
PgStatShared_Wal wal

References Assert(), PgStat_ShmemControl::is_shutdown, IsPostmasterEnvironment, IsUnderPostmaster, PgStatShared_Wal::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockConditionalAcquire(), LWLockRelease(), pgstat_wal_have_pending_cb(), pgStatLocal, pgWalUsage, prevWalUsage, PgStat_LocalState::shmem, PgStat_ShmemControl::wal, WALSTAT_ACC, and WalUsageAccumDiff().

Referenced by pgstat_report_wal().

◆ pgstat_wal_have_pending_cb()

bool pgstat_wal_have_pending_cb ( void  )

Definition at line 143 of file pgstat_wal.c.

144{
146}
int64 wal_records
Definition: instrument.h:53

References pgWalUsage, prevWalUsage, and WalUsage::wal_records.

Referenced by pgstat_wal_flush_cb().

◆ pgstat_wal_init_backend_cb()

void pgstat_wal_init_backend_cb ( void  )

Definition at line 129 of file pgstat_wal.c.

130{
131 /*
132 * Initialize prevWalUsage with pgWalUsage so that pgstat_wal_flush_cb()
133 * can calculate how much pgWalUsage counters are increased by subtracting
134 * prevWalUsage from pgWalUsage.
135 */
137}

References pgWalUsage, and prevWalUsage.

◆ pgstat_wal_init_shmem_cb()

void pgstat_wal_init_shmem_cb ( void *  stats)

Definition at line 149 of file pgstat_wal.c.

150{
151 PgStatShared_Wal *stats_shmem = (PgStatShared_Wal *) stats;
152
154}

References PgStatShared_Wal::lock, LWLockInitialize(), and LWTRANCHE_PGSTATS_DATA.

◆ pgstat_wal_reset_all_cb()

void pgstat_wal_reset_all_cb ( TimestampTz  ts)

Definition at line 157 of file pgstat_wal.c.

158{
159 PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
160
161 LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
162 memset(&stats_shmem->stats, 0, sizeof(stats_shmem->stats));
163 stats_shmem->stats.stat_reset_timestamp = ts;
164 LWLockRelease(&stats_shmem->lock);
165}
PgStat_WalStats stats
TimestampTz stat_reset_timestamp
Definition: pgstat.h:481

References PgStatShared_Wal::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), pgStatLocal, PgStat_LocalState::shmem, PgStat_WalStats::stat_reset_timestamp, PgStatShared_Wal::stats, and PgStat_ShmemControl::wal.

◆ pgstat_wal_snapshot_cb()

void pgstat_wal_snapshot_cb ( void  )

◆ PostPrepare_PgStat_Relations()

void PostPrepare_PgStat_Relations ( PgStat_SubXactStatus xact_state)

Definition at line 728 of file pgstat_relation.c.

729{
731
732 for (trans = xact_state->first; trans != NULL; trans = trans->next)
733 {
734 PgStat_TableStatus *tabstat;
735
736 tabstat = trans->parent;
737 tabstat->trans = NULL;
738 }
739}

References PgStat_SubXactStatus::first, PgStat_TableStatus::trans, and trans.

Referenced by PostPrepare_PgStat().

Variable Documentation

◆ pgStatLocal

PGDLLIMPORT PgStat_LocalState pgStatLocal
extern

Definition at line 212 of file pgstat.c.

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

◆ slru_names

const char* const slru_names[]
static
Initial value:
= {
"commit_timestamp",
"multixact_member",
"multixact_offset",
"notify",
"serializable",
"subtransaction",
"transaction",
"other"
}

Definition at line 334 of file pgstat_internal.h.

Referenced by pgstat_get_slru_index(), and pgstat_get_slru_name().