Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

PostgreSQL Source Code git master
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

 StaticAssertDecl ((sizeof(PgStat_Kind)+sizeof(uint64)+sizeof(Oid))==sizeof(PgStat_HashKey), "PgStat_HashKey should have no padding")
 
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)
 
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_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_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_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)
 
static uint64 pgstat_get_entry_count (PgStat_Kind kind)
 

Variables

static const char *const slru_names []
 
PGDLLIMPORT bool pgstat_report_fixed
 
PGDLLIMPORT PgStat_LocalState pgStatLocal
 

Macro Definition Documentation

◆ pgstat_assert_is_up

#define pgstat_assert_is_up ( )    ((void)true)

Definition at line 621 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 650 of file pgstat_internal.h.

◆ PGSTAT_BACKEND_FLUSH_IO

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

Definition at line 648 of file pgstat_internal.h.

◆ PGSTAT_BACKEND_FLUSH_WAL

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

Definition at line 649 of file pgstat_internal.h.

◆ SLRU_NUM_ELEMENTS

#define SLRU_NUM_ELEMENTS   lengthof(slru_names)

Definition at line 359 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:1594
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:144
PgStat_Counter tuples_updated
Definition: pgstat.h:145
PgStat_Counter delta_dead_tuples
Definition: pgstat.h:152
PgStat_Counter tuples_deleted
Definition: pgstat.h:146
PgStat_TableCounts counts
Definition: pgstat.h:179
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:178
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:151
PgStat_Counter changed_tuples
Definition: pgstat.h:153

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:223
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:1271
#define TWOPHASE_RM_PGSTAT_ID
Definition: twophase_rmgr.h:28

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
70 LWLockInitialize(&stats_shmem->lock, LWTRANCHE_PGSTATS_DATA);
71}
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:698

References PgStatShared_Archiver::lock, and LWLockInitialize().

◆ 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:1174
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1894
@ LW_EXCLUSIVE
Definition: lwlock.h:112
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:227
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:113
void reset(void)
Definition: sql-declare.c:600
TimestampTz last_failed_timestamp
Definition: pgstat.h:226
TimestampTz last_archived_timestamp
Definition: pgstat.h:222
char last_failed_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:224
PgStat_Counter failed_count
Definition: pgstat.h:223
PgStat_Counter archived_count
Definition: pgstat.h:219
char last_archived_wal[MAX_XFN_CHARS+1]
Definition: pgstat.h:220
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 251 of file pgstat_shmem.c.

252{
253 MemoryContext oldcontext;
254
255 Assert(pgStatLocal.dsa == NULL);
256
257 /* stats shared memory persists for the backend lifetime */
259
261 NULL);
263
266 NULL);
267
268 MemoryContextSwitchTo(oldcontext);
269}
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition: dsa.c:560
void dsa_pin_mapping(dsa_area *area)
Definition: dsa.c:650
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:166
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 313 of file pgstat_backend.c.

314{
316}
bool pgstat_flush_backend(bool nowait, bits32 flags)
#define PGSTAT_BACKEND_FLUSH_ALL

References PGSTAT_BACKEND_FLUSH_ALL, and pgstat_flush_backend().

◆ pgstat_backend_reset_timestamp_cb()

void pgstat_backend_reset_timestamp_cb ( PgStatShared_Common header,
TimestampTz  ts 
)

Definition at line 400 of file pgstat_backend.c.

401{
402 ((PgStatShared_Backend *) header)->stats.stat_reset_timestamp = ts;
403}

◆ pgstat_begin_changecount_read()

static uint32 pgstat_begin_changecount_read ( uint32 cc)
inlinestatic

Definition at line 851 of file pgstat_internal.h.

852{
853 uint32 before_cc = *cc;
854
856
858
859 return before_cc;
860}
#define pg_read_barrier()
Definition: atomics.h:154
uint32_t uint32
Definition: c.h:539
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122

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 829 of file pgstat_internal.h.

830{
831 Assert((*cc & 1) == 0);
832
834 (*cc)++;
836}
#define pg_write_barrier()
Definition: atomics.h:155
#define START_CRIT_SECTION()
Definition: miscadmin.h:149

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)

Definition at line 80 of file pgstat_bgwriter.c.

81{
82 PgStatShared_BgWriter *stats_shmem = (PgStatShared_BgWriter *) stats;
83
84 LWLockInitialize(&stats_shmem->lock, LWTRANCHE_PGSTATS_DATA);
85}

References PgStatShared_BgWriter::lock, and LWLockInitialize().

◆ 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:243
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)

Definition at line 89 of file pgstat_checkpointer.c.

90{
92
93 LWLockInitialize(&stats_shmem->lock, LWTRANCHE_PGSTATS_DATA);
94}

References PgStatShared_Checkpointer::lock, and LWLockInitialize().

◆ 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:266
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 906 of file pgstat_internal.h.

907{
908 Assert(size == sizeof(PgStat_HashKey) && arg == NULL);
909 return memcmp(a, b, sizeof(PgStat_HashKey));
910}
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 890 of file pgstat_internal.h.

892{
893 uint32 cc_before;
894
895 do
896 {
897 cc_before = pgstat_begin_changecount_read(cc);
898
899 memcpy(dst, src, len);
900 }
901 while (!pgstat_end_changecount_read(cc, cc_before));
902}
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:150
void pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:852
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1433
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:469
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:720
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:690
PgStatShared_Common * shared_stats
TimestampTz last_checksum_failure
Definition: pgstat.h:365
TimestampTz last_autovac_time
Definition: pgstat.h:354
PgStat_Counter checksum_failures
Definition: pgstat.h:364

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

1316{
1317 PgStat_Kind kind = entry_ref->shared_entry->key.kind;
1318 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1319 void *pending_data = entry_ref->pending;
1320
1321 Assert(pending_data != NULL);
1322 /* !fixed_amount stats should be handled explicitly */
1323 Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1324
1325 if (kind_info->delete_pending_cb)
1326 kind_info->delete_pending_cb(entry_ref);
1327
1328 pfree(pending_data);
1329 entry_ref->pending = NULL;
1330
1331 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 272 of file pgstat_shmem.c.

273{
275
276 /* we shouldn't leave references to shared stats */
278
281
283
284 /*
285 * dsa_detach() does not decrement the DSA reference count as no segment
286 * was provided to dsa_attach_in_place(), causing no cleanup callbacks to
287 * be registered. Hence, release it manually now.
288 */
290
291 pgStatLocal.dsa = NULL;
292}
void dsa_release_in_place(void *place)
Definition: dsa.c:620
void dsa_detach(dsa_area *area)
Definition: dsa.c:1967
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:838

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 1090 of file pgstat_shmem.c.

1091{
1093}
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 1004 of file pgstat_shmem.c.

1005{
1006 PgStat_HashKey key = {0};
1008 bool freed = true;
1009
1010 key.kind = kind;
1011 key.dboid = dboid;
1012 key.objid = objid;
1013
1014 /* delete local reference */
1016 {
1017 PgStat_EntryRefHashEntry *lohashent =
1018 pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, key);
1019
1020 if (lohashent)
1021 pgstat_release_entry_ref(lohashent->key, lohashent->entry_ref,
1022 true);
1023 }
1024
1025 /* mark entry in shared hashtable as deleted, drop if possible */
1026 shent = dshash_find(pgStatLocal.shared_hash, &key, true);
1027 if (shent)
1028 {
1029 freed = pgstat_drop_entry_internal(shent, NULL);
1030
1031 /*
1032 * Database stats contain other stats. Drop those as well when
1033 * dropping the database. XXX: Perhaps this should be done in a
1034 * slightly more principled way? But not obvious what that'd look
1035 * like, and so far this is the only case...
1036 */
1037 if (key.kind == PGSTAT_KIND_DATABASE)
1039 }
1040
1041 return freed;
1042}
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:612
static void pgstat_drop_database_and_contents(Oid dboid)
Definition: pgstat_shmem.c:943
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:902
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 1049 of file pgstat_shmem.c.

1051{
1052 dshash_seq_status hstat;
1054 uint64 not_freed_count = 0;
1055
1056 /* entries are removed, take an exclusive lock */
1058 while ((ps = dshash_seq_next(&hstat)) != NULL)
1059 {
1060 if (ps->dropped)
1061 continue;
1062
1063 if (do_drop != NULL && !do_drop(ps, match_data))
1064 continue;
1065
1066 /* delete local reference */
1068 {
1069 PgStat_EntryRefHashEntry *lohashent =
1070 pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, ps->key);
1071
1072 if (lohashent)
1073 pgstat_release_entry_ref(lohashent->key, lohashent->entry_ref,
1074 true);
1075 }
1076
1077 if (!pgstat_drop_entry_internal(ps, &hstat))
1078 not_freed_count++;
1079 }
1080 dshash_seq_term(&hstat);
1081
1082 if (not_freed_count > 0)
1084}
uint64_t uint64
Definition: c.h:540
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:745

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 866 of file pgstat_internal.h.

867{
868 uint32 after_cc;
869
871
872 after_cc = *cc;
873
874 /* was a write in progress when we started? */
875 if (before_cc & 1)
876 return false;
877
878 /* did writes start and complete while we read? */
879 return before_cc == after_cc;
880}

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 839 of file pgstat_internal.h.

840{
841 Assert((*cc & 1) == 1);
842
844
845 (*cc)++;
846
848}
#define END_CRIT_SECTION()
Definition: miscadmin.h:151

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

934{
935 PgStat_HashKey key = {0};
936 PgStat_EntryRef *entry_ref;
937 void *stats_data;
938 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
939
940 /* should be called from backends */
942 Assert(!kind_info->fixed_amount);
943
945
946 key.kind = kind;
947 key.dboid = dboid;
948 key.objid = objid;
949
950 /* if we need to build a full snapshot, do so */
953
954 /* if caching is desired, look up in cache */
956 {
957 PgStat_SnapshotEntry *entry = NULL;
958
959 entry = pgstat_snapshot_lookup(pgStatLocal.snapshot.stats, key);
960
961 if (entry)
962 return entry->data;
963
964 /*
965 * If we built a full snapshot and the key is not in
966 * pgStatLocal.snapshot.stats, there are no matching stats.
967 */
969 return NULL;
970 }
971
973
974 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
975
976 if (entry_ref == NULL || entry_ref->shared_entry->dropped)
977 {
978 /* create empty entry when using PGSTAT_FETCH_CONSISTENCY_CACHE */
980 {
981 PgStat_SnapshotEntry *entry = NULL;
982 bool found;
983
984 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
985 Assert(!found);
986 entry->data = NULL;
987 }
988 return NULL;
989 }
990
991 /*
992 * Allocate in caller's context for PGSTAT_FETCH_CONSISTENCY_NONE,
993 * otherwise we could quickly end up with a fair bit of memory used due to
994 * repeated accesses.
995 */
997 stats_data = palloc(kind_info->shared_data_len);
998 else
1000 kind_info->shared_data_len);
1001
1002 (void) pgstat_lock_entry_shared(entry_ref, false);
1003 memcpy(stats_data,
1004 pgstat_get_entry_data(kind, entry_ref->shared_stats),
1005 kind_info->shared_data_len);
1006 pgstat_unlock_entry(entry_ref);
1007
1009 {
1010 PgStat_SnapshotEntry *entry = NULL;
1011 bool found;
1012
1013 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
1014 entry->data = stats_data;
1015 }
1016
1017 return stats_data;
bool IsUnderPostmaster
Definition: globals.c:120
bool IsPostmasterEnvironment
Definition: globals.c:119
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1229
void * palloc(Size size)
Definition: mcxt.c:1365
int pgstat_fetch_consistency
Definition: pgstat.c:204
static void pgstat_prep_snapshot(void)
Definition: pgstat.c:1097
static void pgstat_build_snapshot(void)
Definition: pgstat.c:1118
@ PGSTAT_FETCH_CONSISTENCY_NONE
Definition: pgstat.h:47
@ PGSTAT_FETCH_CONSISTENCY_CACHE
Definition: pgstat.h:48
@ PGSTAT_FETCH_CONSISTENCY_SNAPSHOT
Definition: pgstat.h:49
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:708
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 1301 of file pgstat.c.

1303{
1304 PgStat_EntryRef *entry_ref;
1305
1306 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
1307
1308 if (entry_ref == NULL || entry_ref->pending == NULL)
1309 return NULL;
1310
1311 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 270 of file pgstat_backend.c.

271{
272 PgStat_EntryRef *entry_ref;
273 bool has_pending_data = false;
274
276 return false;
277
278 /* Some IO data pending? */
280 has_pending_data = true;
281
282 /* Some WAL data pending? */
283 if ((flags & PGSTAT_BACKEND_FLUSH_WAL) &&
285 has_pending_data = true;
286
287 if (!has_pending_data)
288 return false;
289
291 MyProcNumber, nowait);
292 if (!entry_ref)
293 return true;
294
295 /* Flush requested statistics */
296 if (flags & PGSTAT_BACKEND_FLUSH_IO)
298
299 if (flags & PGSTAT_BACKEND_FLUSH_WAL)
301
302 pgstat_unlock_entry(entry_ref);
303
304 return false;
305}
ProcNumber MyProcNumber
Definition: globals.c:90
BackendType MyBackendType
Definition: miscinit.c:64
static void pgstat_flush_backend_entry_wal(PgStat_EntryRef *entry_ref)
bool pgstat_tracks_backend_bktype(BackendType bktype)
static void pgstat_flush_backend_entry_io(PgStat_EntryRef *entry_ref)
static bool pgstat_backend_wal_have_pending(void)
static bool backend_has_iostats
#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:729
#define InvalidOid
Definition: postgres_ext.h:37

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 175 of file pgstat_io.c.

176{
177 (void) pgstat_io_flush_cb(nowait);
178}
bool pgstat_io_flush_cb(bool nowait)
Definition: pgstat_io.c:189

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:83
instr_time total_time
Definition: pgstat.h:84
instr_time self_time
Definition: pgstat.h:85
PgStat_Counter self_time
Definition: pgstat.h:386
PgStat_Counter numcalls
Definition: pgstat.h:383
PgStat_Counter total_time
Definition: pgstat.h:385

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 960 of file pgstat_internal.h.

961{
962 int idx = kind - PGSTAT_KIND_CUSTOM_MIN;
963
965 Assert(pgstat_get_kind_info(kind)->fixed_amount);
966
968}
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:262
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1433
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_count()

static uint64 pgstat_get_entry_count ( PgStat_Kind  kind)
inlinestatic

Definition at line 948 of file pgstat_internal.h.

949{
950 Assert(pgstat_get_kind_info(kind)->track_entry_count);
951
953}
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:465
pg_atomic_uint64 entry_counts[PGSTAT_KIND_MAX]

References Assert(), PgStat_ShmemControl::entry_counts, pg_atomic_read_u64(), pgstat_get_kind_info(), pgStatLocal, and PgStat_LocalState::shmem.

Referenced by injection_points_stats_count().

◆ pgstat_get_entry_data()

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

Definition at line 935 of file pgstat_internal.h.

936{
937 size_t off = pgstat_get_kind_info(kind)->shared_data_off;
938
939 Assert(off != 0 && off < PG_UINT32_MAX);
940
941 return ((char *) (entry)) + off;
942}
#define PG_UINT32_MAX
Definition: c.h:596

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 469 of file pgstat_shmem.c.

471{
472 PgStat_HashKey key = {0};
473 PgStatShared_HashEntry *shhashent;
474 PgStatShared_Common *shheader = NULL;
475 PgStat_EntryRef *entry_ref;
476
477 key.kind = kind;
478 key.dboid = dboid;
479 key.objid = objid;
480
481 /*
482 * passing in created_entry only makes sense if we possibly could create
483 * entry.
484 */
485 Assert(create || created_entry == NULL);
489
492
493 if (created_entry != NULL)
494 *created_entry = false;
495
496 /*
497 * Check if other backends dropped stats that could not be deleted because
498 * somebody held references to it. If so, check this backend's references.
499 * This is not expected to happen often. The location of the check is a
500 * bit random, but this is a relatively frequently called path, so better
501 * than most.
502 */
505
506 /*
507 * First check the lookup cache hashtable in local memory. If we find a
508 * match here we can avoid taking locks / causing contention.
509 */
510 if (pgstat_get_entry_ref_cached(key, &entry_ref))
511 return entry_ref;
512
513 Assert(entry_ref != NULL);
514
515 /*
516 * Do a lookup in the hash table first - it's quite likely that the entry
517 * already exists, and that way we only need a shared lock.
518 */
519 shhashent = dshash_find(pgStatLocal.shared_hash, &key, false);
520
521 if (create && !shhashent)
522 {
523 bool shfound;
524
525 /*
526 * It's possible that somebody created the entry since the above
527 * lookup. If so, fall through to the same path as if we'd have if it
528 * already had been created before the dshash_find() calls.
529 */
530 shhashent = dshash_find_or_insert(pgStatLocal.shared_hash, &key, &shfound);
531 if (!shfound)
532 {
533 shheader = pgstat_init_entry(kind, shhashent);
534 if (shheader == NULL)
535 {
536 /*
537 * Failed the allocation of a new entry, so clean up the
538 * shared hashtable before giving up.
539 */
541
543 (errcode(ERRCODE_OUT_OF_MEMORY),
544 errmsg("out of memory"),
545 errdetail("Failed while allocating entry %u/%u/%" PRIu64 ".",
546 key.kind, key.dboid, key.objid)));
547 }
548 pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
549
550 if (created_entry != NULL)
551 *created_entry = true;
552
553 return entry_ref;
554 }
555 }
556
557 if (!shhashent)
558 {
559 /*
560 * If we're not creating, delete the reference again. In all
561 * likelihood it's just a stats lookup - no point wasting memory for a
562 * shared ref to nothing...
563 */
564 pgstat_release_entry_ref(key, entry_ref, false);
565
566 return NULL;
567 }
568 else
569 {
570 /*
571 * Can get here either because dshash_find() found a match, or if
572 * dshash_find_or_insert() found a concurrently inserted entry.
573 */
574
575 if (shhashent->dropped && create)
576 {
577 /*
578 * There are legitimate cases where the old stats entry might not
579 * yet have been dropped by the time it's reused. The most obvious
580 * case are replication slot stats, where a new slot can be
581 * created with the same index just after dropping. But oid
582 * wraparound can lead to other cases as well. We just reset the
583 * stats to their plain state, while incrementing its "generation"
584 * in the shared entry for any remaining local references.
585 */
586 shheader = pgstat_reinit_entry(kind, shhashent);
587 pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
588
589 if (created_entry != NULL)
590 *created_entry = true;
591
592 return entry_ref;
593 }
594 else if (shhashent->dropped)
595 {
597 pgstat_release_entry_ref(key, entry_ref, false);
598
599 return NULL;
600 }
601 else
602 {
603 shheader = dsa_get_address(pgStatLocal.dsa, shhashent->body);
604 pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
605
606 return entry_ref;
607 }
608 }
609}
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:957
void dshash_delete_entry(dshash_table *hash_table, void *entry)
Definition: dshash.c:541
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
int errdetail(const char *fmt,...)
Definition: elog.c:1207
int errcode(int sqlerrcode)
Definition: elog.c:854
#define ERROR
Definition: elog.h:39
#define pgstat_assert_is_up()
static void pgstat_setup_memcxt(void)
static void pgstat_setup_shared_refs(void)
Definition: pgstat_shmem.c:377
static PgStatShared_Common * pgstat_reinit_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
Definition: pgstat_shmem.c:352
static void pgstat_acquire_entry_ref(PgStat_EntryRef *entry_ref, PgStatShared_HashEntry *shhashent, PgStatShared_Common *shheader)
Definition: pgstat_shmem.c:393
PgStatShared_Common * pgstat_init_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
Definition: pgstat_shmem.c:308
static bool pgstat_get_entry_ref_cached(PgStat_HashKey key, PgStat_EntryRef **entry_ref_p)
Definition: pgstat_shmem.c:413
static void pgstat_gc_entry_refs(void)
Definition: pgstat_shmem.c:767
static bool pgstat_need_entry_refs_gc(void)
Definition: pgstat_shmem.c:751

References Assert(), PgStatShared_HashEntry::body, PgStatShared_HashEntry::dropped, PgStat_LocalState::dsa, dsa_get_address(), dshash_delete_entry(), dshash_find(), dshash_find_or_insert(), dshash_release_lock(), ereport, errcode(), errdetail(), errmsg(), ERROR, 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 729 of file pgstat_shmem.c.

731{
732 PgStat_EntryRef *entry_ref;
733
734 /* find shared table stats entry corresponding to the local entry */
735 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, true, NULL);
736
737 /* lock the shared entry to protect the content, skip if failed */
738 if (!pgstat_lock_entry(entry_ref, nowait))
739 return NULL;
740
741 return entry_ref;
742}

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

1435{
1436 if (pgstat_is_kind_builtin(kind))
1437 return &pgstat_kind_builtin_infos[kind];
1438
1439 if (pgstat_is_kind_custom(kind))
1440 {
1442
1443 if (pgstat_kind_custom_infos == NULL ||
1445 return NULL;
1447 }
1448
1449 return NULL;
static const PgStat_KindInfo ** pgstat_kind_custom_infos
Definition: pgstat.c:492
static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
Definition: pgstat.c:281
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_free_entry(), pgstat_get_custom_shmem_data(), pgstat_get_custom_snapshot_data(), pgstat_get_entry_count(), 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:171
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 913 of file pgstat_internal.h.

914{
915 const char *key = (const char *) d;
916
917 Assert(size == sizeof(PgStat_HashKey) && arg == NULL);
918 return fasthash32(key, size, 0);
919}
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 308 of file pgstat_shmem.c.

310{
311 /* Create new stats entry. */
312 dsa_pointer chunk;
313 PgStatShared_Common *shheader;
314 const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
315
316 /*
317 * Initialize refcount to 1, marking it as valid / not dropped. The entry
318 * can't be freed before the initialization because it can't be found as
319 * long as we hold the dshash partition lock. Caller needs to increase
320 * further if a longer lived reference is needed.
321 */
322 pg_atomic_init_u32(&shhashent->refcount, 1);
323
324 /*
325 * Initialize "generation" to 0, as freshly created.
326 */
327 pg_atomic_init_u32(&shhashent->generation, 0);
328 shhashent->dropped = false;
329
331 kind_info->shared_size,
333 if (chunk == InvalidDsaPointer)
334 return NULL;
335
336 shheader = dsa_get_address(pgStatLocal.dsa, chunk);
337 shheader->magic = 0xdeadbeef;
338
339 /* Link the new entry from the hash entry. */
340 shhashent->body = chunk;
341
342 /* Increment entry count, if required. */
343 if (kind_info->track_entry_count)
345
346 LWLockInitialize(&shheader->lock, LWTRANCHE_PGSTATS_DATA);
347
348 return shheader;
349}
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:219
static uint64 pg_atomic_fetch_add_u64(volatile pg_atomic_uint64 *ptr, int64 add_)
Definition: atomics.h:520
dsa_pointer dsa_allocate_extended(dsa_area *area, size_t size, int flags)
Definition: dsa.c:686
uint64 dsa_pointer
Definition: dsa.h:62
#define InvalidDsaPointer
Definition: dsa.h:78
#define DSA_ALLOC_NO_OOM
Definition: dsa.h:74
#define DSA_ALLOC_ZERO
Definition: dsa.h:75
pg_atomic_uint32 refcount
pg_atomic_uint32 generation

References PgStatShared_HashEntry::body, PgStatShared_HashEntry::dropped, PgStat_LocalState::dsa, DSA_ALLOC_NO_OOM, DSA_ALLOC_ZERO, dsa_allocate_extended(), dsa_get_address(), PgStat_ShmemControl::entry_counts, PgStatShared_HashEntry::generation, InvalidDsaPointer, PgStatShared_Common::lock, LWLockInitialize(), PgStatShared_Common::magic, pg_atomic_fetch_add_u64(), pg_atomic_init_u32(), pgstat_get_kind_info(), pgStatLocal, PgStatShared_HashEntry::refcount, PgStat_KindInfo::shared_size, PgStat_LocalState::shmem, and PgStat_KindInfo::track_entry_count.

Referenced by pgstat_get_entry_ref(), and pgstat_read_statsfile().

◆ pgstat_io_flush_cb()

bool pgstat_io_flush_cb ( bool  nowait)

Definition at line 189 of file pgstat_io.c.

190{
191 LWLock *bktype_lock;
192 PgStat_BktypeIO *bktype_shstats;
193
194 if (!have_iostats)
195 return false;
196
197 bktype_lock = &pgStatLocal.shmem->io.locks[MyBackendType];
198 bktype_shstats =
200
201 if (!nowait)
202 LWLockAcquire(bktype_lock, LW_EXCLUSIVE);
203 else if (!LWLockConditionalAcquire(bktype_lock, LW_EXCLUSIVE))
204 return true;
205
206 for (int io_object = 0; io_object < IOOBJECT_NUM_TYPES; io_object++)
207 {
208 for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++)
209 {
210 for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
211 {
212 instr_time time;
213
214 bktype_shstats->counts[io_object][io_context][io_op] +=
215 PendingIOStats.counts[io_object][io_context][io_op];
216
217 bktype_shstats->bytes[io_object][io_context][io_op] +=
218 PendingIOStats.bytes[io_object][io_context][io_op];
219
220 time = PendingIOStats.pending_times[io_object][io_context][io_op];
221
222 bktype_shstats->times[io_object][io_context][io_op] +=
224 }
225 }
226 }
227
229
230 LWLockRelease(bktype_lock);
231
232 memset(&PendingIOStats, 0, sizeof(PendingIOStats));
233
234 have_iostats = false;
235
236 return false;
237}
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1345
#define IOOP_NUM_TYPES
Definition: pgstat.h:317
#define IOCONTEXT_NUM_TYPES
Definition: pgstat.h:291
#define IOOBJECT_NUM_TYPES
Definition: pgstat.h:280
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:327
uint64 bytes[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:325
PgStat_Counter counts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:326
PgStat_BktypeIO stats[BACKEND_NUM_TYPES]
Definition: pgstat.h:340
PgStat_Counter counts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:333
uint64 bytes[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:332
instr_time pending_times[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:334
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_init_shmem_cb()

void pgstat_io_init_shmem_cb ( void *  stats)

Definition at line 278 of file pgstat_io.c.

279{
280 PgStatShared_IO *stat_shmem = (PgStatShared_IO *) stats;
281
282 for (int i = 0; i < BACKEND_NUM_TYPES; i++)
283 LWLockInitialize(&stat_shmem->locks[i], LWTRANCHE_PGSTATS_DATA);
284}
int i
Definition: isn.c:77
#define BACKEND_NUM_TYPES
Definition: miscadmin.h:376

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

◆ pgstat_io_reset_all_cb()

void pgstat_io_reset_all_cb ( TimestampTz  ts)

Definition at line 287 of file pgstat_io.c.

288{
289 for (int i = 0; i < BACKEND_NUM_TYPES; i++)
290 {
291 LWLock *bktype_lock = &pgStatLocal.shmem->io.locks[i];
292 PgStat_BktypeIO *bktype_shstats = &pgStatLocal.shmem->io.stats.stats[i];
293
294 LWLockAcquire(bktype_lock, LW_EXCLUSIVE);
295
296 /*
297 * Use the lock in the first BackendType's PgStat_BktypeIO to protect
298 * the reset timestamp as well.
299 */
300 if (i == 0)
302
303 memset(bktype_shstats, 0, sizeof(*bktype_shstats));
304 LWLockRelease(bktype_lock);
305 }
306}
TimestampTz stat_reset_timestamp
Definition: pgstat.h:339

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 309 of file pgstat_io.c.

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

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 708 of file pgstat_shmem.c.

709{
710 LWLock *lock = &entry_ref->shared_stats->lock;
711
712 if (nowait)
714
716 return true;
717}

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:775
Oid MyDatabaseId
Definition: globals.c:94
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *created_entry)
Definition: pgstat.c:1263

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

1265{
1266 PgStat_EntryRef *entry_ref;
1267
1268 /* need to be able to flush out */
1269 Assert(pgstat_get_kind_info(kind)->flush_pending_cb != NULL);
1270
1272 {
1275 "PgStat Pending",
1277 }
1278
1279 entry_ref = pgstat_get_entry_ref(kind, dboid, objid,
1280 true, created_entry);
1281
1282 if (entry_ref->pending == NULL)
1283 {
1284 size_t entrysize = pgstat_get_kind_info(kind)->pending_size;
1285
1286 Assert(entrysize != (size_t) -1);
1287
1288 entry_ref->pending = MemoryContextAllocZero(pgStatPendingContext, entrysize);
1290 }
1291
1292 return entry_ref;
#define unlikely(x)
Definition: c.h:403
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:364
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1263
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:170
static MemoryContext pgStatPendingContext
Definition: pgstat.c:234
static dlist_head pgStatPending
Definition: pgstat.c:242

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

1463{
1465
1466 if (kind_info->name == NULL || strlen(kind_info->name) == 0)
1467 ereport(ERROR,
1468 (errmsg("custom cumulative statistics name is invalid"),
1469 errhint("Provide a non-empty name for the custom cumulative statistics.")));
1470
1471 if (!pgstat_is_kind_custom(kind))
1472 ereport(ERROR, (errmsg("custom cumulative statistics ID %u is out of range", kind),
1473 errhint("Provide a custom cumulative statistics ID between %u and %u.",
1475
1477 ereport(ERROR,
1478 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1479 errdetail("Custom cumulative statistics must be registered while initializing modules in \"shared_preload_libraries\".")));
1480
1481 /*
1482 * Check some data for fixed-numbered stats.
1483 */
1484 if (kind_info->fixed_amount)
1485 {
1486 if (kind_info->shared_size == 0)
1487 ereport(ERROR,
1488 (errmsg("custom cumulative statistics property is invalid"),
1489 errhint("Custom cumulative statistics require a shared memory size for fixed-numbered objects.")));
1490 if (kind_info->track_entry_count)
1491 ereport(ERROR,
1492 (errmsg("custom cumulative statistics property is invalid"),
1493 errhint("Custom cumulative statistics cannot use entry count tracking for fixed-numbered objects.")));
1494 }
1495
1496 /*
1497 * If pgstat_kind_custom_infos is not available yet, allocate it.
1498 */
1499 if (pgstat_kind_custom_infos == NULL)
1500 {
1504 }
1505
1506 if (pgstat_kind_custom_infos[idx] != NULL &&
1508 ereport(ERROR,
1509 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1510 errdetail("Custom cumulative statistics \"%s\" already registered with the same ID.",
1512
1513 /* check for existing custom stats with the same name */
1514 for (PgStat_Kind existing_kind = PGSTAT_KIND_CUSTOM_MIN; existing_kind <= PGSTAT_KIND_CUSTOM_MAX; existing_kind++)
1515 {
1516 uint32 existing_idx = existing_kind - PGSTAT_KIND_CUSTOM_MIN;
1517
1518 if (pgstat_kind_custom_infos[existing_idx] == NULL)
1519 continue;
1520 if (!pg_strcasecmp(pgstat_kind_custom_infos[existing_idx]->name, kind_info->name))
1521 ereport(ERROR,
1522 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1523 errdetail("Existing cumulative statistics with ID %u has the same name.", existing_kind)));
1524 }
1525
1526 /* Register it */
1527 pgstat_kind_custom_infos[idx] = kind_info;
1528 ereport(LOG,
1529 (errmsg("registered custom cumulative statistics \"%s\" with ID %u",
1530 kind_info->name, kind)));
int errhint(const char *fmt,...)
Definition: elog.c:1321
#define LOG
Definition: elog.h:31
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1786
#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, TopMemoryContext, and PgStat_KindInfo::track_entry_count.

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:180

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:998
int64 TimestampTz
Definition: timestamp.h:39
static bool pg_memory_is_all_zeros(const void *ptr, size_t len)
Definition: memutils.h:219
PgStat_StatDBEntry * pgstat_prep_database_pending(Oid dboid)
unsigned int Oid
Definition: postgres_ext.h:32
PgStat_StatTabEntry stats
PgStat_Counter tuples_updated
Definition: pgstat.h:352
PgStat_Counter tuples_inserted
Definition: pgstat.h:351
PgStat_Counter tuples_returned
Definition: pgstat.h:349
PgStat_Counter blocks_hit
Definition: pgstat.h:348
PgStat_Counter blocks_fetched
Definition: pgstat.h:347
PgStat_Counter tuples_deleted
Definition: pgstat.h:353
PgStat_Counter tuples_fetched
Definition: pgstat.h:350
PgStat_Counter tuples_fetched
Definition: pgstat.h:428
PgStat_Counter ins_since_vacuum
Definition: pgstat.h:439
PgStat_Counter blocks_hit
Definition: pgstat.h:442
PgStat_Counter mod_since_analyze
Definition: pgstat.h:438
PgStat_Counter tuples_deleted
Definition: pgstat.h:432
PgStat_Counter tuples_hot_updated
Definition: pgstat.h:433
PgStat_Counter tuples_updated
Definition: pgstat.h:431
PgStat_Counter live_tuples
Definition: pgstat.h:436
PgStat_Counter numscans
Definition: pgstat.h:424
PgStat_Counter dead_tuples
Definition: pgstat.h:437
PgStat_Counter blocks_fetched
Definition: pgstat.h:441
PgStat_Counter tuples_returned
Definition: pgstat.h:427
TimestampTz lastscan
Definition: pgstat.h:425
PgStat_Counter tuples_inserted
Definition: pgstat.h:430
PgStat_Counter tuples_newpage_updated
Definition: pgstat.h:434
PgStat_Counter blocks_hit
Definition: pgstat.h:156
PgStat_Counter numscans
Definition: pgstat.h:139
PgStat_Counter tuples_hot_updated
Definition: pgstat.h:147
PgStat_Counter tuples_returned
Definition: pgstat.h:141
PgStat_Counter blocks_fetched
Definition: pgstat.h:155
PgStat_Counter tuples_fetched
Definition: pgstat.h:142
PgStat_Counter tuples_newpage_updated
Definition: pgstat.h:148
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:752
#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:226
bool ReplicationSlotName(int index, Name name)
Definition: slot.c:562

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:55
@ DISCONNECT_FATAL
Definition: pgstat.h:58
@ DISCONNECT_KILLED
Definition: pgstat.h:59
@ DISCONNECT_CLIENT_EOF
Definition: pgstat.h:57
@ DISCONNECT_NORMAL
Definition: pgstat.h:56
static bool pgstat_should_report_connstat(void)
SessionEndType pgStatSessionEndCause
PgStat_Counter sessions_fatal
Definition: pgstat.h:373
PgStat_Counter sessions_abandoned
Definition: pgstat.h:372
PgStat_Counter sessions_killed
Definition: pgstat.h:374

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 1168 of file pgstat_shmem.c.

1169{
1171}
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:222

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 1112 of file pgstat_shmem.c.

1113{
1114 PgStat_EntryRef *entry_ref;
1115
1116 Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1117
1118 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
1119 if (!entry_ref || entry_ref->shared_entry->dropped)
1120 return;
1121
1122 (void) pgstat_lock_entry(entry_ref, false);
1123 shared_stat_reset_contents(kind, entry_ref->shared_stats, ts);
1124 pgstat_unlock_entry(entry_ref);
1125}
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 1132 of file pgstat_shmem.c.

1134{
1135 dshash_seq_status hstat;
1137
1138 /* dshash entry is not modified, take shared lock */
1139 dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
1140 while ((p = dshash_seq_next(&hstat)) != NULL)
1141 {
1142 PgStatShared_Common *header;
1143
1144 if (p->dropped)
1145 continue;
1146
1147 if (!do_reset(p, match_data))
1148 continue;
1149
1150 header = dsa_get_address(pgStatLocal.dsa, p->body);
1151
1152 LWLockAcquire(&header->lock, LW_EXCLUSIVE);
1153
1154 shared_stat_reset_contents(p->key.kind, header, ts);
1155
1156 LWLockRelease(&header->lock);
1157 }
1158 dshash_seq_term(&hstat);
1159}

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 142 of file pgstat_slru.c.

143{
144 PgStatShared_SLRU *stats_shmem = &pgStatLocal.shmem->slru;
145 int i;
146
147 if (!have_slrustats)
148 return false;
149
150 if (!nowait)
151 LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
152 else if (!LWLockConditionalAcquire(&stats_shmem->lock, LW_EXCLUSIVE))
153 return true;
154
155 for (i = 0; i < SLRU_NUM_ELEMENTS; i++)
156 {
157 PgStat_SLRUStats *sharedent = &stats_shmem->stats[i];
158 PgStat_SLRUStats *pendingent = &pending_SLRUStats[i];
159
160#define SLRU_ACC(fld) sharedent->fld += pendingent->fld
161 SLRU_ACC(blocks_zeroed);
162 SLRU_ACC(blocks_hit);
163 SLRU_ACC(blocks_read);
165 SLRU_ACC(blocks_exists);
166 SLRU_ACC(flush);
167 SLRU_ACC(truncate);
168#undef SLRU_ACC
169 }
170
171 /* done, clear the pending entry */
173
174 LWLockRelease(&stats_shmem->lock);
175
176 have_slrustats = false;
177
178 return false;
179}
#define MemSet(start, val, len)
Definition: c.h:1020
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_init_shmem_cb()

void pgstat_slru_init_shmem_cb ( void *  stats)

Definition at line 182 of file pgstat_slru.c.

183{
184 PgStatShared_SLRU *stats_shmem = (PgStatShared_SLRU *) stats;
185
186 LWLockInitialize(&stats_shmem->lock, LWTRANCHE_PGSTATS_DATA);
187}

References PgStatShared_SLRU::lock, and LWLockInitialize().

◆ pgstat_slru_reset_all_cb()

void pgstat_slru_reset_all_cb ( TimestampTz  ts)

Definition at line 190 of file pgstat_slru.c.

191{
192 for (int i = 0; i < SLRU_NUM_ELEMENTS; i++)
194}
static void pgstat_reset_slru_counter_internal(int index, TimestampTz ts)
Definition: pgstat_slru.c:233

References i, pgstat_reset_slru_counter_internal(), and SLRU_NUM_ELEMENTS.

◆ pgstat_slru_snapshot_cb()

void pgstat_slru_snapshot_cb ( void  )

Definition at line 197 of file pgstat_slru.c.

198{
199 PgStatShared_SLRU *stats_shmem = &pgStatLocal.shmem->slru;
200
201 LWLockAcquire(&stats_shmem->lock, LW_SHARED);
202
203 memcpy(pgStatLocal.snapshot.slru, &stats_shmem->stats,
204 sizeof(stats_shmem->stats));
205
206 LWLockRelease(&stats_shmem->lock);
207}
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 1058 of file pgstat.c.

1060{
1062 Assert(pgstat_get_kind_info(kind)->fixed_amount);
1063
1066
1069 else
1071
1072 if (pgstat_is_kind_builtin(kind))
1074 else if (pgstat_is_kind_custom(kind))
static void pgstat_build_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1208
static bool pgstat_is_kind_valid(PgStat_Kind kind)
Definition: pgstat.c:1427
void pgstat_clear_snapshot(void)
Definition: pgstat.c:900
static bool force_stats_snapshot_clear
Definition: pgstat.c:255
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:64
#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:66
PgStat_Counter pgStatActiveTime
PgStat_Counter pgStatBlockReadTime
PgStat_Counter pgStatBlockWriteTime
static PgStat_Counter pgLastSessionReportTime
PgStat_Counter pgStatTransactionIdleTime
PgStat_Counter blk_write_time
Definition: pgstat.h:367
PgStat_Counter xact_rollback
Definition: pgstat.h:346
PgStat_Counter blk_read_time
Definition: pgstat.h:366
PgStat_Counter xact_commit
Definition: pgstat.h:345
PgStat_Counter session_time
Definition: pgstat.h:369
PgStat_Counter active_time
Definition: pgstat.h:370
PgStat_Counter idle_in_transaction_time
Definition: pgstat.h:371

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 91 of file pgstat_wal.c.

92{
93 PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
94 WalUsage wal_usage_diff = {0};
95
97 Assert(pgStatLocal.shmem != NULL &&
99
100 /*
101 * This function can be called even if nothing at all has happened. Avoid
102 * taking lock for nothing in that case.
103 */
105 return false;
106
107 /*
108 * We don't update the WAL usage portion of the local WalStats elsewhere.
109 * Calculate how much WAL usage counters were increased by subtracting the
110 * previous counters from the current ones.
111 */
112 WalUsageAccumDiff(&wal_usage_diff, &pgWalUsage, &prevWalUsage);
113
114 if (!nowait)
115 LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
116 else if (!LWLockConditionalAcquire(&stats_shmem->lock, LW_EXCLUSIVE))
117 return true;
118
119#define WALSTAT_ACC(fld, var_to_add) \
120 (stats_shmem->stats.wal_counters.fld += var_to_add.fld)
121 WALSTAT_ACC(wal_records, wal_usage_diff);
122 WALSTAT_ACC(wal_fpi, wal_usage_diff);
123 WALSTAT_ACC(wal_bytes, wal_usage_diff);
124 WALSTAT_ACC(wal_buffers_full, wal_usage_diff);
125#undef WALSTAT_ACC
126
127 LWLockRelease(&stats_shmem->lock);
128
129 /*
130 * Save the current counters for the subsequent calculation of WAL usage.
131 */
133
134 return false;
135}
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
static bool pgstat_wal_have_pending(void)
Definition: pgstat_wal.c:78
PgStatShared_Wal wal

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

Referenced by pgstat_report_wal().

◆ pgstat_wal_init_backend_cb()

void pgstat_wal_init_backend_cb ( void  )

Definition at line 138 of file pgstat_wal.c.

139{
140 /*
141 * Initialize prevWalUsage with pgWalUsage so that pgstat_wal_flush_cb()
142 * can calculate how much pgWalUsage counters are increased by subtracting
143 * prevWalUsage from pgWalUsage.
144 */
146}

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
153 LWLockInitialize(&stats_shmem->lock, LWTRANCHE_PGSTATS_DATA);
154}

References PgStatShared_Wal::lock, and LWLockInitialize().

◆ 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:482

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().

◆ StaticAssertDecl()

StaticAssertDecl ( (sizeof(PgStat_Kind)+sizeof(uint64)+sizeof(Oid))  = =sizeof(PgStat_HashKey),
"PgStat_HashKey should have no padding"   
)

Variable Documentation

◆ pgstat_report_fixed

◆ 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_count(), pgstat_get_entry_ref(), pgstat_get_stat_snapshot_timestamp(), pgstat_init_entry(), pgstat_init_snapshot_fixed(), pgstat_io_flush_cb(), pgstat_io_reset_all_cb(), pgstat_io_snapshot_cb(), pgstat_need_entry_refs_gc(), pgstat_prep_snapshot(), pgstat_read_statsfile(), pgstat_reinit_entry(), pgstat_release_entry_ref(), pgstat_report_archiver(), pgstat_report_bgwriter(), pgstat_report_checkpointer(), pgstat_report_stat(), pgstat_request_entry_refs_gc(), pgstat_reset_matching_entries(), pgstat_reset_slru_counter_internal(), pgstat_setup_shared_refs(), pgstat_slru_flush_cb(), pgstat_slru_snapshot_cb(), pgstat_snapshot_fixed(), pgstat_wal_flush_cb(), pgstat_wal_reset_all_cb(), pgstat_wal_snapshot_cb(), pgstat_write_statsfile(), and StatsShmemInit().

◆ slru_names

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

Definition at line 348 of file pgstat_internal.h.

Referenced by pgstat_get_slru_index(), and pgstat_get_slru_name().