PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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_Lock
 
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)
 
#define pgstat_read_chunk_s(fpin, ptr)   pgstat_read_chunk(fpin, ptr, sizeof(*ptr))
 
#define pgstat_write_chunk_s(fpout, ptr)   pgstat_write_chunk(fpout, ptr, sizeof(*ptr))
 

Typedefs

typedef struct PgStat_HashKey PgStat_HashKey
 
typedef enum PgStat_StatsFileOp PgStat_StatsFileOp
 
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_Lock PgStatShared_Lock
 
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
 

Enumerations

enum  PgStat_StatsFileOp { STATS_WRITE , STATS_READ , STATS_DISCARD }
 

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 voidpgstat_get_entry_data (PgStat_Kind kind, PgStatShared_Common *entry)
 
static voidpgstat_get_custom_shmem_data (PgStat_Kind kind)
 
static voidpgstat_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)
 
voidpgstat_fetch_entry (PgStat_Kind kind, Oid dboid, uint64 objid, bool *may_free)
 
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, uint32 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_function_reset_timestamp_cb (PgStatShared_Common *header, TimestampTz ts)
 
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)
 
bool pgstat_lock_flush_cb (bool nowait)
 
void pgstat_lock_init_shmem_cb (void *stats)
 
void pgstat_lock_reset_all_cb (TimestampTz ts)
 
void pgstat_lock_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_relation_reset_timestamp_cb (PgStatShared_Common *header, TimestampTz ts)
 
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)
 
void pgstat_write_chunk (FILE *fpout, void *ptr, size_t len)
 
bool pgstat_read_chunk (FILE *fpin, void *ptr, size_t len)
 
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 678 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 708 of file pgstat_internal.h.

◆ PGSTAT_BACKEND_FLUSH_IO

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

Definition at line 706 of file pgstat_internal.h.

◆ PGSTAT_BACKEND_FLUSH_WAL

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

Definition at line 707 of file pgstat_internal.h.

◆ pgstat_read_chunk_s

#define pgstat_read_chunk_s (   fpin,
  ptr 
)    pgstat_read_chunk(fpin, ptr, sizeof(*ptr))

Definition at line 890 of file pgstat_internal.h.

◆ pgstat_write_chunk_s

#define pgstat_write_chunk_s (   fpout,
  ptr 
)    pgstat_write_chunk(fpout, ptr, sizeof(*ptr))

Definition at line 891 of file pgstat_internal.h.

◆ SLRU_NUM_ELEMENTS

#define SLRU_NUM_ELEMENTS   lengthof(slru_names)

Definition at line 406 of file pgstat_internal.h.

Typedef Documentation

◆ PgStat_EntryRef

◆ PgStat_HashKey

◆ PgStat_KindInfo

◆ PgStat_LocalState

◆ PgStat_ShmemControl

◆ PgStat_Snapshot

◆ PgStat_StatsFileOp

◆ PgStat_SubXactStatus

◆ PgStatShared_Archiver

◆ PgStatShared_Backend

◆ PgStatShared_BgWriter

◆ PgStatShared_Checkpointer

◆ PgStatShared_Common

◆ PgStatShared_Database

◆ PgStatShared_Function

◆ PgStatShared_HashEntry

◆ PgStatShared_IO

◆ PgStatShared_Lock

◆ PgStatShared_Relation

◆ PgStatShared_ReplSlot

◆ PgStatShared_SLRU

◆ PgStatShared_Subscription

◆ PgStatShared_Wal

Enumeration Type Documentation

◆ PgStat_StatsFileOp

Enumerator
STATS_WRITE 
STATS_READ 
STATS_DISCARD 

Definition at line 73 of file pgstat_internal.h.

74{
PgStat_StatsFileOp
@ STATS_WRITE
@ STATS_READ
@ STATS_DISCARD

Function Documentation

◆ AtEOSubXact_PgStat_Relations()

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

Definition at line 610 of file pgstat_relation.c.

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

References Assert, fb(), PgStat_SubXactStatus::first, pfree(), pgstat_get_xact_stack_level(), restore_truncdrop_counters(), save_truncdrop_counters(), and trans.

Referenced by AtEOSubXact_PgStat().

◆ AtEOXact_PgStat_Database()

void AtEOXact_PgStat_Database ( bool  isCommit,
bool  parallel 
)
extern

Definition at line 295 of file pgstat_database.c.

296{
297 /* Don't count parallel worker transaction stats */
298 if (!parallel)
299 {
300 /*
301 * Count transaction commit or abort. (We use counters, not just
302 * bools, in case the reporting message isn't sent right away.)
303 */
304 if (isCommit)
306 else
308 }
309}
static int pgStatXactCommit
static int pgStatXactRollback

References fb(), pgStatXactCommit, and pgStatXactRollback.

Referenced by AtEOXact_PgStat().

◆ AtEOXact_PgStat_Relations()

void AtEOXact_PgStat_Relations ( PgStat_SubXactStatus xact_state,
bool  isCommit 
)
extern

Definition at line 552 of file pgstat_relation.c.

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

References Assert, fb(), PgStat_SubXactStatus::first, restore_truncdrop_counters(), and trans.

Referenced by AtEOXact_PgStat().

◆ AtPrepare_PgStat_Relations()

void AtPrepare_PgStat_Relations ( PgStat_SubXactStatus xact_state)
extern

Definition at line 691 of file pgstat_relation.c.

692{
694
695 for (trans = xact_state->first; trans != NULL; trans = trans->next)
696 {
699
700 Assert(trans->nest_level == 1);
701 Assert(trans->upper == NULL);
702 tabstat = trans->parent;
703 Assert(tabstat->trans == trans);
704
705 record.tuples_inserted = trans->tuples_inserted;
706 record.tuples_updated = trans->tuples_updated;
707 record.tuples_deleted = trans->tuples_deleted;
708 record.inserted_pre_truncdrop = trans->inserted_pre_truncdrop;
709 record.updated_pre_truncdrop = trans->updated_pre_truncdrop;
710 record.deleted_pre_truncdrop = trans->deleted_pre_truncdrop;
711 record.id = tabstat->id;
712 record.shared = tabstat->shared;
713 record.truncdropped = trans->truncdropped;
714
716 &record, sizeof(TwoPhasePgStatRecord));
717 }
718}
#define PG_USED_FOR_ASSERTS_ONLY
Definition c.h:249
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:1277
#define TWOPHASE_RM_PGSTAT_ID

References Assert, TwoPhasePgStatRecord::deleted_pre_truncdrop, fb(), 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)
extern

Definition at line 66 of file pgstat_archiver.c.

67{
69
71}
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition lwlock.c:670

References fb(), and LWLockInitialize().

◆ pgstat_archiver_reset_all_cb()

void pgstat_archiver_reset_all_cb ( TimestampTz  ts)
extern

Definition at line 74 of file pgstat_archiver.c.

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

References PgStat_ShmemControl::archiver, fb(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), pgstat_copy_changecounted_stats(), pgStatLocal, and PgStat_LocalState::shmem.

◆ pgstat_archiver_snapshot_cb()

void pgstat_archiver_snapshot_cb ( void  )
extern

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
102 memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
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}
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
@ LW_SHARED
Definition lwlock.h:105
void reset(void)
PgStat_Snapshot snapshot
PgStat_ArchiverStats archiver

References PgStat_ShmemControl::archiver, PgStat_Snapshot::archiver, fb(), LW_SHARED, LWLockAcquire(), LWLockRelease(), memcpy(), pgstat_copy_changecounted_stats(), pgStatLocal, reset(), PgStat_LocalState::shmem, and PgStat_LocalState::snapshot.

◆ pgstat_attach_shmem()

void pgstat_attach_shmem ( void  )
extern

Definition at line 256 of file pgstat_shmem.c.

257{
258 MemoryContext oldcontext;
259
261
262 /* stats shared memory persists for the backend lifetime */
264
266 NULL);
268
271 NULL);
272
273 MemoryContextSwitchTo(oldcontext);
274}
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:274
MemoryContext TopMemoryContext
Definition mcxt.c:166
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
static const dshash_parameters dsh_params
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(), fb(), 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)
extern

Definition at line 315 of file pgstat_backend.c.

316{
318}
bool pgstat_flush_backend(bool nowait, uint32 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 
)
extern

Definition at line 404 of file pgstat_backend.c.

405{
406 ((PgStatShared_Backend *) header)->stats.stat_reset_timestamp = ts;
407}

◆ pgstat_begin_changecount_read()

static uint32 pgstat_begin_changecount_read ( uint32 cc)
inlinestatic

Definition at line 925 of file pgstat_internal.h.

926{
927 uint32 before_cc = *cc;
928
930
932
933 return before_cc;
934}
#define pg_read_barrier()
Definition atomics.h:154
uint32_t uint32
Definition c.h:624
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:125

References CHECK_FOR_INTERRUPTS, fb(), 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 903 of file pgstat_internal.h.

904{
905 Assert((*cc & 1) == 0);
906
908 (*cc)++;
910}
#define pg_write_barrier()
Definition atomics.h:155
#define START_CRIT_SECTION()
Definition miscadmin.h:152

References Assert, pg_write_barrier, and START_CRIT_SECTION.

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

◆ pgstat_bgwriter_init_shmem_cb()

void pgstat_bgwriter_init_shmem_cb ( void stats)
extern

◆ pgstat_bgwriter_reset_all_cb()

void pgstat_bgwriter_reset_all_cb ( TimestampTz  ts)
extern

Definition at line 88 of file pgstat_bgwriter.c.

89{
91
92 /* see explanation above PgStatShared_BgWriter for the reset protocol */
95 &stats_shmem->stats,
96 sizeof(stats_shmem->stats),
97 &stats_shmem->changecount);
98 stats_shmem->stats.stat_reset_timestamp = ts;
100}
PgStatShared_BgWriter bgwriter

References PgStat_ShmemControl::bgwriter, fb(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), pgstat_copy_changecounted_stats(), pgStatLocal, and PgStat_LocalState::shmem.

◆ pgstat_bgwriter_snapshot_cb()

void pgstat_bgwriter_snapshot_cb ( void  )
extern

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
115 memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
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, fb(), LW_SHARED, LWLockAcquire(), LWLockRelease(), memcpy(), pgstat_copy_changecounted_stats(), pgStatLocal, reset(), PgStat_LocalState::shmem, and PgStat_LocalState::snapshot.

◆ pgstat_checkpointer_init_shmem_cb()

void pgstat_checkpointer_init_shmem_cb ( void stats)
extern

◆ pgstat_checkpointer_reset_all_cb()

void pgstat_checkpointer_reset_all_cb ( TimestampTz  ts)
extern

Definition at line 97 of file pgstat_checkpointer.c.

98{
100
101 /* see explanation above PgStatShared_Checkpointer for the reset protocol */
104 &stats_shmem->stats,
105 sizeof(stats_shmem->stats),
106 &stats_shmem->changecount);
107 stats_shmem->stats.stat_reset_timestamp = ts;
109}
PgStatShared_Checkpointer checkpointer

References PgStat_ShmemControl::checkpointer, fb(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), pgstat_copy_changecounted_stats(), pgStatLocal, and PgStat_LocalState::shmem.

◆ pgstat_checkpointer_snapshot_cb()

void pgstat_checkpointer_snapshot_cb ( void  )
extern

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
124 memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
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 PgStat_ShmemControl::checkpointer, PgStat_Snapshot::checkpointer, CHECKPOINTER_COMP, fb(), LW_SHARED, LWLockAcquire(), LWLockRelease(), memcpy(), pgstat_copy_changecounted_stats(), pgStatLocal, reset(), PgStat_LocalState::shmem, and PgStat_LocalState::snapshot.

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

981{
982 Assert(size == sizeof(PgStat_HashKey) && arg == NULL);
983 return memcmp(a, b, sizeof(PgStat_HashKey));
984}
Datum arg
Definition elog.c:1322
int b
Definition isn.c:74
int a
Definition isn.c:73

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

◆ pgstat_copy_changecounted_stats()

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

◆ pgstat_create_transactional()

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

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}
#define WARNING
Definition elog.h:37
#define ereport(elevel,...)
Definition elog.h:152
static char * errmsg
void pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition pgstat.c:882
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition pgstat.c:1479
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)
static void create_drop_transactional_internal(PgStat_Kind kind, Oid dboid, uint64 objid, bool is_create)
const char * name

References create_drop_transactional_internal(), ereport, errmsg, fb(), 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 
)
extern

Definition at line 438 of file pgstat_database.c.

439{
442
443 pendingent = (PgStat_StatDBEntry *) entry_ref->pending;
445
446 if (!pgstat_lock_entry(entry_ref, nowait))
447 return false;
448
449#define PGSTAT_ACCUM_DBCOUNT(item) \
450 (sharedent)->stats.item += (pendingent)->item
451
452 PGSTAT_ACCUM_DBCOUNT(xact_commit);
453 PGSTAT_ACCUM_DBCOUNT(xact_rollback);
454 PGSTAT_ACCUM_DBCOUNT(blocks_fetched);
455 PGSTAT_ACCUM_DBCOUNT(blocks_hit);
456
457 PGSTAT_ACCUM_DBCOUNT(tuples_returned);
458 PGSTAT_ACCUM_DBCOUNT(tuples_fetched);
459 PGSTAT_ACCUM_DBCOUNT(tuples_inserted);
460 PGSTAT_ACCUM_DBCOUNT(tuples_updated);
461 PGSTAT_ACCUM_DBCOUNT(tuples_deleted);
462
463 /* last_autovac_time is reported immediately */
464 Assert(pendingent->last_autovac_time == 0);
465
466 PGSTAT_ACCUM_DBCOUNT(conflict_tablespace);
467 PGSTAT_ACCUM_DBCOUNT(conflict_lock);
468 PGSTAT_ACCUM_DBCOUNT(conflict_snapshot);
469 PGSTAT_ACCUM_DBCOUNT(conflict_logicalslot);
470 PGSTAT_ACCUM_DBCOUNT(conflict_bufferpin);
471 PGSTAT_ACCUM_DBCOUNT(conflict_startup_deadlock);
472
473 PGSTAT_ACCUM_DBCOUNT(temp_bytes);
474 PGSTAT_ACCUM_DBCOUNT(temp_files);
475 PGSTAT_ACCUM_DBCOUNT(deadlocks);
476
477 /* checksum failures are reported immediately */
478 Assert(pendingent->checksum_failures == 0);
479 Assert(pendingent->last_checksum_failure == 0);
480
481 PGSTAT_ACCUM_DBCOUNT(blk_read_time);
482 PGSTAT_ACCUM_DBCOUNT(blk_write_time);
483
484 PGSTAT_ACCUM_DBCOUNT(sessions);
485 PGSTAT_ACCUM_DBCOUNT(session_time);
486 PGSTAT_ACCUM_DBCOUNT(active_time);
487 PGSTAT_ACCUM_DBCOUNT(idle_in_transaction_time);
488 PGSTAT_ACCUM_DBCOUNT(sessions_abandoned);
489 PGSTAT_ACCUM_DBCOUNT(sessions_fatal);
490 PGSTAT_ACCUM_DBCOUNT(sessions_killed);
491 PGSTAT_ACCUM_DBCOUNT(parallel_workers_to_launch);
492 PGSTAT_ACCUM_DBCOUNT(parallel_workers_launched);
493#undef PGSTAT_ACCUM_DBCOUNT
494
495 pgstat_unlock_entry(entry_ref);
496
497 memset(pendingent, 0, sizeof(*pendingent));
498
499 return true;
500}
#define PGSTAT_ACCUM_DBCOUNT(item)
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
PgStatShared_Common * shared_stats

References Assert, fb(), 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 
)
extern

Definition at line 503 of file pgstat_database.c.

504{
505 ((PgStatShared_Database *) header)->stats.stat_reset_timestamp = ts;
506}

◆ pgstat_delete_pending_entry()

void pgstat_delete_pending_entry ( PgStat_EntryRef entry_ref)
extern

Definition at line 1360 of file pgstat.c.

1362{
1363 PgStat_Kind kind = entry_ref->shared_entry->key.kind;
1365 void *pending_data = entry_ref->pending;
1366
1368 /* !fixed_amount stats should be handled explicitly */
1369 Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1370
1371 if (kind_info->delete_pending_cb)
1372 kind_info->delete_pending_cb(entry_ref);
1373
1375 entry_ref->pending = NULL;
1376
1377 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

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

Referenced by pgstat_flush_pending_entries(), and pgstat_release_entry_ref().

◆ pgstat_detach_shmem()

void pgstat_detach_shmem ( void  )
extern

Definition at line 277 of file pgstat_shmem.c.

278{
280
281 /* we shouldn't leave references to shared stats */
283
286
288
289 /*
290 * dsa_detach() does not decrement the DSA reference count as no segment
291 * was provided to dsa_attach_in_place(), causing no cleanup callbacks to
292 * be registered. Hence, release it manually now.
293 */
295
297}
void dsa_release_in_place(void *place)
Definition dsa.c:620
void dsa_detach(dsa_area *area)
Definition dsa.c:2002
void dshash_detach(dshash_table *hash_table)
Definition dshash.c:311
static void pgstat_release_all_entry_refs(bool discard_pending)

References Assert, PgStat_LocalState::dsa, dsa_detach(), dsa_release_in_place(), dshash_detach(), fb(), 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  )
extern

Definition at line 1095 of file pgstat_shmem.c.

1096{
1098}
void pgstat_drop_matching_entries(bool(*do_drop)(PgStatShared_HashEntry *, Datum), Datum match_data)

References fb(), and pgstat_drop_matching_entries().

Referenced by pgstat_reset_after_failure().

◆ pgstat_drop_entry()

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

Definition at line 1009 of file pgstat_shmem.c.

1010{
1011 PgStat_HashKey key = {0};
1013 bool freed = true;
1014
1015 key.kind = kind;
1016 key.dboid = dboid;
1017 key.objid = objid;
1018
1019 /* delete local reference */
1021 {
1024
1025 if (lohashent)
1027 true);
1028 }
1029
1030 /* mark entry in shared hashtable as deleted, drop if possible */
1032 if (shent)
1033 {
1035
1036 /*
1037 * Database stats contain other stats. Drop those as well when
1038 * dropping the database. XXX: Perhaps this should be done in a
1039 * slightly more principled way? But not obvious what that'd look
1040 * like, and so far this is the only case...
1041 */
1042 if (key.kind == PGSTAT_KIND_DATABASE)
1044 }
1045
1046 return freed;
1047}
void * dshash_find(dshash_table *hash_table, const void *key, bool exclusive)
Definition dshash.c:394
#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)
static void pgstat_drop_database_and_contents(Oid dboid)
static pgstat_entry_ref_hash_hash * pgStatEntryRefHash
static bool pgstat_drop_entry_internal(PgStatShared_HashEntry *shent, dshash_seq_status *hstat)

References dshash_find(), fb(), 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_replslot(), pgstat_execute_transactional_drops(), pgstat_init_function_usage(), pgstat_shutdown_hook(), and test_custom_stats_var_drop().

◆ pgstat_drop_matching_entries()

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

Definition at line 1054 of file pgstat_shmem.c.

1056{
1060
1061 /* entries are removed, take an exclusive lock */
1063 while ((ps = dshash_seq_next(&hstat)) != NULL)
1064 {
1065 if (ps->dropped)
1066 continue;
1067
1068 if (do_drop != NULL && !do_drop(ps, match_data))
1069 continue;
1070
1071 /* delete local reference */
1073 {
1076
1077 if (lohashent)
1079 true);
1080 }
1081
1084 }
1086
1087 if (not_freed_count > 0)
1089}
uint64_t uint64
Definition c.h:625
void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table, bool exclusive)
Definition dshash.c:659
void dshash_seq_term(dshash_seq_status *status)
Definition dshash.c:768
void * dshash_seq_next(dshash_seq_status *status)
Definition dshash.c:678
struct parser_state ps
void pgstat_request_entry_refs_gc(void)

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

Referenced by pgstat_drop_all_entries().

◆ pgstat_drop_transactional()

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

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

941{
943
945
946 after_cc = *cc;
947
948 /* was a write in progress when we started? */
949 if (before_cc & 1)
950 return false;
951
952 /* did writes start and complete while we read? */
953 return before_cc == after_cc;
954}

References fb(), and 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 913 of file pgstat_internal.h.

914{
915 Assert((*cc & 1) == 1);
916
918
919 (*cc)++;
920
922}
#define END_CRIT_SECTION()
Definition miscadmin.h:154

References Assert, END_CRIT_SECTION, and pg_write_barrier.

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

◆ pgstat_fetch_entry()

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

Definition at line 962 of file pgstat.c.

964{
965 PgStat_HashKey key = {0};
966 PgStat_EntryRef *entry_ref;
967 void *stats_data;
969
970 /* should be called from backends */
972 Assert(!kind_info->fixed_amount);
973
974 /*
975 * Initialize *may_free to false. We'll change it to true later if we end
976 * up allocating the result in the caller's context and not caching it.
977 */
978 if (may_free)
979 *may_free = false;
980
982
983 key.kind = kind;
984 key.dboid = dboid;
985 key.objid = objid;
986
987 /* if we need to build a full snapshot, do so */
990
991 /* if caching is desired, look up in cache */
993 {
994 PgStat_SnapshotEntry *entry = NULL;
995
997
998 if (entry)
999 return entry->data;
1000
1001 /*
1002 * If we built a full snapshot and the key is not in
1003 * pgStatLocal.snapshot.stats, there are no matching stats.
1004 */
1006 return NULL;
1007 }
1008
1010
1011 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
1012
1013 if (entry_ref == NULL || entry_ref->shared_entry->dropped)
1014 {
1015 /* create empty entry when using PGSTAT_FETCH_CONSISTENCY_CACHE */
1017 {
1018 PgStat_SnapshotEntry *entry = NULL;
1019 bool found;
1020
1021 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
1022 Assert(!found);
1023 entry->data = NULL;
1024 }
1025 return NULL;
1026 }
1027
1028 /*
1029 * Allocate in caller's context for PGSTAT_FETCH_CONSISTENCY_NONE,
1030 * otherwise we could quickly end up with a fair bit of memory used due to
1031 * repeated accesses.
1032 */
1034 {
1035 stats_data = palloc(kind_info->shared_data_len);
1036
1037 /*
1038 * Since we allocated the result in the caller's context and aren't
1039 * caching it, the caller can safely pfree() it.
1040 */
1041 if (may_free)
1042 *may_free = true;
1043 }
1044 else
1046 kind_info->shared_data_len);
1047
1048 (void) pgstat_lock_entry_shared(entry_ref, false);
1050 pgstat_get_entry_data(kind, entry_ref->shared_stats),
1051 kind_info->shared_data_len);
1052 pgstat_unlock_entry(entry_ref);
1053
1055 {
1056 PgStat_SnapshotEntry *entry = NULL;
1057 bool found;
1058
1059 entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
1060 entry->data = stats_data;
1061 }
1062
1063 return stats_data;
bool IsUnderPostmaster
Definition globals.c:122
bool IsPostmasterEnvironment
Definition globals.c:121
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
void * palloc(Size size)
Definition mcxt.c:1387
int pgstat_fetch_consistency
Definition pgstat.c:205
static void pgstat_prep_snapshot(void)
Definition pgstat.c:1143
static void pgstat_build_snapshot(void)
Definition pgstat.c:1164
@ PGSTAT_FETCH_CONSISTENCY_NONE
Definition pgstat.h:52
@ PGSTAT_FETCH_CONSISTENCY_CACHE
Definition pgstat.h:53
@ PGSTAT_FETCH_CONSISTENCY_SNAPSHOT
Definition pgstat.h:54
static void * pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry)
bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
MemoryContext context
PgStat_FetchConsistency mode
struct pgstat_snapshot_hash * stats

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

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

◆ pgstat_fetch_pending_entry()

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

Definition at line 1347 of file pgstat.c.

1349{
1350 PgStat_EntryRef *entry_ref;
1351
1352 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
1353
1354 if (entry_ref == NULL || entry_ref->pending == NULL)
1355 return NULL;
1356
1357 return entry_ref;

References fb(), 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,
uint32  flags 
)
extern

Definition at line 272 of file pgstat_backend.c.

273{
274 PgStat_EntryRef *entry_ref;
275 bool has_pending_data = false;
276
278 return false;
279
280 /* Some IO data pending? */
282 has_pending_data = true;
283
284 /* Some WAL data pending? */
285 if ((flags & PGSTAT_BACKEND_FLUSH_WAL) &&
287 has_pending_data = true;
288
289 if (!has_pending_data)
290 return false;
291
293 MyProcNumber, nowait);
294 if (!entry_ref)
295 return true;
296
297 /* Flush requested statistics */
298 if (flags & PGSTAT_BACKEND_FLUSH_IO)
300
301 if (flags & PGSTAT_BACKEND_FLUSH_WAL)
303
304 pgstat_unlock_entry(entry_ref);
305
306 return false;
307}
ProcNumber MyProcNumber
Definition globals.c:92
BackendType MyBackendType
Definition miscinit.c:65
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)
#define InvalidOid

References backend_has_iostats, fb(), 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)
extern

◆ pgstat_function_flush_cb()

bool pgstat_function_flush_cb ( PgStat_EntryRef entry_ref,
bool  nowait 
)
extern

Definition at line 193 of file pgstat_function.c.

194{
197
198 localent = (PgStat_FunctionCounts *) entry_ref->pending;
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:454

References fb(), INSTR_TIME_GET_MICROSEC, PgStat_EntryRef::pending, pgstat_lock_entry(), pgstat_unlock_entry(), and PgStat_EntryRef::shared_stats.

◆ pgstat_function_reset_timestamp_cb()

void pgstat_function_reset_timestamp_cb ( PgStatShared_Common header,
TimestampTz  ts 
)
extern

Definition at line 218 of file pgstat_function.c.

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

◆ pgstat_get_custom_shmem_data()

static void * pgstat_get_custom_shmem_data ( PgStat_Kind  kind)
inlinestatic

Definition at line 1034 of file pgstat_internal.h.

1035{
1036 int idx = kind - PGSTAT_KIND_CUSTOM_MIN;
1037
1039 Assert(pgstat_get_kind_info(kind)->fixed_amount);
1040
1042}
Datum idx(PG_FUNCTION_ARGS)
Definition _int_op.c:262
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition pgstat.c:1479
PGDLLIMPORT PgStat_LocalState pgStatLocal
Definition pgstat.c:213
static bool pgstat_is_kind_custom(PgStat_Kind kind)
Definition pgstat_kind.h:68
#define PGSTAT_KIND_CUSTOM_MIN
Definition pgstat_kind.h:50
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 test_custom_stats_fixed_reset_all_cb(), test_custom_stats_fixed_snapshot_cb(), and test_custom_stats_fixed_update().

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

1023{
1024 Assert(pgstat_get_kind_info(kind)->track_entry_count);
1025
1027}
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition atomics.h:467
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.

◆ pgstat_get_entry_data()

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

Definition at line 1009 of file pgstat_internal.h.

1010{
1011 size_t off = pgstat_get_kind_info(kind)->shared_data_off;
1012
1013 Assert(off != 0 && off < PG_UINT32_MAX);
1014
1015 return ((char *) (entry)) + off;
1016}
#define PG_UINT32_MAX
Definition c.h:674

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

◆ pgstat_get_entry_ref()

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

Definition at line 474 of file pgstat_shmem.c.

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

References Assert, PgStat_LocalState::dsa, dsa_get_address(), dshash_delete_entry(), dshash_find(), dshash_find_or_insert, dshash_release_lock(), ereport, errcode(), errdetail(), errmsg, ERROR, fb(), PgStat_ShmemControl::is_shutdown, 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(), pgstat_reset_entry(), and test_custom_stats_var_report().

◆ pgstat_get_entry_ref_locked()

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

Definition at line 734 of file pgstat_shmem.c.

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

References fb(), 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_replslotsync(), pgstat_report_vacuum(), pgstat_reset_database_timestamp(), and test_custom_stats_var_create().

◆ pgstat_get_kind_info()

const PgStat_KindInfo * pgstat_get_kind_info ( PgStat_Kind  kind)
extern

Definition at line 1479 of file pgstat.c.

1481{
1482 if (pgstat_is_kind_builtin(kind))
1483 return &pgstat_kind_builtin_infos[kind];
1484
1485 if (pgstat_is_kind_custom(kind))
1486 {
1488
1491 return NULL;
1493 }
1494
1495 return NULL;
static const PgStat_KindInfo ** pgstat_kind_custom_infos
Definition pgstat.c:512
static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
Definition pgstat.c:282
static bool pgstat_is_kind_builtin(PgStat_Kind kind)
Definition pgstat_kind.h:62

References fb(), 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_discard_stats(), 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)
extern

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(), fb(), 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 987 of file pgstat_internal.h.

988{
989 const char *key = (const char *) d;
990
991 Assert(size == sizeof(PgStat_HashKey) && arg == NULL);
992 return fasthash32(key, size, 0);
993}
static uint32 fasthash32(const char *k, size_t len, uint64 seed)

References arg, Assert, fasthash32(), and fb().

◆ pgstat_init_entry()

PgStatShared_Common * pgstat_init_entry ( PgStat_Kind  kind,
PgStatShared_HashEntry shhashent 
)
extern

Definition at line 313 of file pgstat_shmem.c.

315{
316 /* Create new stats entry. */
317 dsa_pointer chunk;
320
321 /*
322 * Initialize refcount to 1, marking it as valid / not dropped. The entry
323 * can't be freed before the initialization because it can't be found as
324 * long as we hold the dshash partition lock. Caller needs to increase
325 * further if a longer lived reference is needed.
326 */
327 pg_atomic_init_u32(&shhashent->refcount, 1);
328
329 /*
330 * Initialize "generation" to 0, as freshly created.
331 */
332 pg_atomic_init_u32(&shhashent->generation, 0);
333 shhashent->dropped = false;
334
336 kind_info->shared_size,
338 if (chunk == InvalidDsaPointer)
339 return NULL;
340
342 shheader->magic = 0xdeadbeef;
343
344 /* Link the new entry from the hash entry. */
345 shhashent->body = chunk;
346
347 /* Increment entry count, if required. */
348 if (kind_info->track_entry_count)
350
352
353 return shheader;
354}
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:532
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

References PgStat_LocalState::dsa, DSA_ALLOC_NO_OOM, DSA_ALLOC_ZERO, dsa_allocate_extended(), dsa_get_address(), PgStat_ShmemControl::entry_counts, fb(), InvalidDsaPointer, LWLockInitialize(), pg_atomic_fetch_add_u64(), pg_atomic_init_u32(), pgstat_get_kind_info(), pgStatLocal, and PgStat_LocalState::shmem.

Referenced by pgstat_get_entry_ref(), and pgstat_read_statsfile().

◆ pgstat_io_flush_cb()

bool pgstat_io_flush_cb ( bool  nowait)
extern

Definition at line 189 of file pgstat_io.c.

190{
193
194 if (!have_iostats)
195 return false;
196
200
201 if (!nowait)
204 return true;
205
207 {
209 {
210 for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
211 {
212 instr_time time;
213
216
219
221
224 }
225 }
226 }
227
229
231
233
234 have_iostats = false;
235
236 return false;
237}
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1321
#define IOOP_NUM_TYPES
Definition pgstat.h:323
#define IOCONTEXT_NUM_TYPES
Definition pgstat.h:297
#define IOOBJECT_NUM_TYPES
Definition pgstat.h:286
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
LWLock locks[BACKEND_NUM_TYPES]
PgStat_BktypeIO stats[BACKEND_NUM_TYPES]
Definition pgstat.h:346
PgStat_Counter counts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition pgstat.h:339
uint64 bytes[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition pgstat.h:338
instr_time pending_times[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition pgstat.h:340

References Assert, PgStat_PendingIO::bytes, PgStat_PendingIO::counts, fb(), 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, and PgStatShared_IO::stats.

Referenced by pgstat_flush_io().

◆ pgstat_io_init_shmem_cb()

void pgstat_io_init_shmem_cb ( void stats)
extern

Definition at line 278 of file pgstat_io.c.

279{
281
282 for (int i = 0; i < BACKEND_NUM_TYPES; i++)
284}
int i
Definition isn.c:77
#define BACKEND_NUM_TYPES
Definition miscadmin.h:392

References BACKEND_NUM_TYPES, fb(), i, and LWLockInitialize().

◆ pgstat_io_reset_all_cb()

void pgstat_io_reset_all_cb ( TimestampTz  ts)
extern

Definition at line 287 of file pgstat_io.c.

288{
289 for (int i = 0; i < BACKEND_NUM_TYPES; i++)
290 {
293
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
305 }
306}
TimestampTz stat_reset_timestamp
Definition pgstat.h:345

References BACKEND_NUM_TYPES, fb(), 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  )
extern

Definition at line 309 of file pgstat_io.c.

310{
311 for (int i = 0; i < BACKEND_NUM_TYPES; i++)
312 {
316
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 */
330 }
331}

References BACKEND_NUM_TYPES, fb(), 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 
)
extern

Definition at line 713 of file pgstat_shmem.c.

714{
715 LWLock *lock = &entry_ref->shared_stats->lock;
716
717 if (nowait)
719
721 return true;
722}

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

Referenced by pgstat_fetch_entry().

◆ pgstat_lock_flush_cb()

bool pgstat_lock_flush_cb ( bool  nowait)
extern

Definition at line 51 of file pgstat_lock.c.

52{
55
56 if (!have_lockstats)
57 return false;
58
61
62 if (!nowait)
65 return true;
66
67 for (int i = 0; i <= LOCKTAG_LAST_TYPE; i++)
68 {
69#define LOCKSTAT_ACC(fld) \
70 (shstats->stats.stats[i].fld += PendingLockStats.stats[i].fld)
71 LOCKSTAT_ACC(waits);
72 LOCKSTAT_ACC(wait_time);
73 LOCKSTAT_ACC(fastpath_exceeded);
74#undef LOCKSTAT_ACC
75 }
76
78
80 have_lockstats = false;
81
82 return false;
83}
#define LOCKTAG_LAST_TYPE
Definition locktag.h:52
static bool have_lockstats
Definition pgstat_lock.c:23
static PgStat_PendingLock PendingLockStats
Definition pgstat_lock.c:22
#define LOCKSTAT_ACC(fld)
PgStatShared_Lock lock

References fb(), have_lockstats, i, PgStatShared_Lock::lock, PgStat_ShmemControl::lock, LOCKSTAT_ACC, LOCKTAG_LAST_TYPE, LW_EXCLUSIVE, LWLockAcquire(), LWLockConditionalAcquire(), LWLockRelease(), PendingLockStats, pgStatLocal, and PgStat_LocalState::shmem.

Referenced by pgstat_lock_flush().

◆ pgstat_lock_init_shmem_cb()

void pgstat_lock_init_shmem_cb ( void stats)
extern

Definition at line 86 of file pgstat_lock.c.

References fb(), and LWLockInitialize().

◆ pgstat_lock_reset_all_cb()

◆ pgstat_lock_snapshot_cb()

◆ pgstat_prep_database_pending()

PgStat_StatDBEntry * pgstat_prep_database_pending ( Oid  dboid)
extern

Definition at line 396 of file pgstat_database.c.

397{
398 PgStat_EntryRef *entry_ref;
399
400 /*
401 * This should not report stats on database objects before having
402 * connected to a database.
403 */
405
407 NULL);
408
409 return entry_ref->pending;
410}
#define OidIsValid(objectId)
Definition c.h:858
Oid MyDatabaseId
Definition globals.c:96
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *created_entry)
Definition pgstat.c:1309

References Assert, fb(), 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 
)
extern

Definition at line 1309 of file pgstat.c.

1311{
1312 PgStat_EntryRef *entry_ref;
1313
1314 /* need to be able to flush out */
1315 Assert(pgstat_get_kind_info(kind)->flush_pending_cb != NULL);
1316
1318 {
1321 "PgStat Pending",
1323 }
1324
1325 entry_ref = pgstat_get_entry_ref(kind, dboid, objid,
1326 true, created_entry);
1327
1328 if (entry_ref->pending == NULL)
1329 {
1330 size_t entrysize = pgstat_get_kind_info(kind)->pending_size;
1331
1332 Assert(entrysize != (size_t) -1);
1333
1334 entry_ref->pending = MemoryContextAllocZero(pgStatPendingContext, entrysize);
1336 }
1337
1338 return entry_ref;
#define unlikely(x)
Definition c.h:438
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition ilist.h:364
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition mcxt.c:1266
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition memutils.h:170
static MemoryContext pgStatPendingContext
Definition pgstat.c:235
static dlist_head pgStatPending
Definition pgstat.c:243

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

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

◆ pgstat_read_chunk()

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

Definition at line 1797 of file pgstat.c.

1799{
1800 return fread(ptr, 1, len, fpin) == len;

References fb(), and len.

Referenced by pgstat_read_statsfile(), and test_custom_stats_var_from_serialized_data().

◆ pgstat_register_kind()

void pgstat_register_kind ( PgStat_Kind  kind,
const PgStat_KindInfo kind_info 
)
extern

Definition at line 1507 of file pgstat.c.

1509{
1511
1512 if (kind_info->name == NULL || strlen(kind_info->name) == 0)
1513 ereport(ERROR,
1514 (errmsg("custom cumulative statistics name is invalid"),
1515 errhint("Provide a non-empty name for the custom cumulative statistics.")));
1516
1517 if (!pgstat_is_kind_custom(kind))
1518 ereport(ERROR, (errmsg("custom cumulative statistics ID %u is out of range", kind),
1519 errhint("Provide a custom cumulative statistics ID between %u and %u.",
1521
1523 ereport(ERROR,
1524 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1525 errdetail("Custom cumulative statistics must be registered while initializing modules in \"shared_preload_libraries\".")));
1526
1527 /*
1528 * Check some data for fixed-numbered stats.
1529 */
1530 if (kind_info->fixed_amount)
1531 {
1532 if (kind_info->shared_size == 0)
1533 ereport(ERROR,
1534 (errmsg("custom cumulative statistics property is invalid"),
1535 errhint("Custom cumulative statistics require a shared memory size for fixed-numbered objects.")));
1536 if (kind_info->track_entry_count)
1537 ereport(ERROR,
1538 (errmsg("custom cumulative statistics property is invalid"),
1539 errhint("Custom cumulative statistics cannot use entry count tracking for fixed-numbered objects.")));
1540 }
1541
1542 /*
1543 * If pgstat_kind_custom_infos is not available yet, allocate it.
1544 */
1546 {
1550 }
1551
1554 ereport(ERROR,
1555 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1556 errdetail("Custom cumulative statistics \"%s\" already registered with the same ID.",
1558
1559 /* check for existing custom stats with the same name */
1561 {
1563
1565 continue;
1567 ereport(ERROR,
1568 (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
1569 errdetail("Existing cumulative statistics with ID %u has the same name.", existing_kind)));
1570 }
1571
1572 /* Register it */
1574 ereport(LOG,
1575 (errmsg("registered custom cumulative statistics \"%s\" with ID %u",
1576 kind_info->name, kind)));
#define LOG
Definition elog.h:32
int errhint(const char *fmt,...) pg_attribute_printf(1
bool process_shared_preload_libraries_in_progress
Definition miscinit.c:1788
#define PGSTAT_KIND_CUSTOM_MAX
Definition pgstat_kind.h:51
#define PGSTAT_KIND_CUSTOM_SIZE
Definition pgstat_kind.h:52
int pg_strcasecmp(const char *s1, const char *s2)
const char *const name

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

Referenced by _PG_init().

◆ pgstat_relation_delete_pending_cb()

void pgstat_relation_delete_pending_cb ( PgStat_EntryRef entry_ref)
extern

Definition at line 906 of file pgstat_relation.c.

907{
908 PgStat_TableStatus *pending = (PgStat_TableStatus *) entry_ref->pending;
909
910 if (pending->relation)
911 pgstat_unlink_relation(pending->relation);
912}
void pgstat_unlink_relation(Relation rel)

References 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 
)
extern

Definition at line 817 of file pgstat_relation.c.

818{
819 Oid dboid;
820 PgStat_TableStatus *lstats; /* pending stats entry */
822 PgStat_StatTabEntry *tabentry; /* table entry of shared stats */
823 PgStat_StatDBEntry *dbentry; /* pending database entry */
824
825 dboid = entry_ref->shared_entry->key.dboid;
826 lstats = (PgStat_TableStatus *) entry_ref->pending;
828
829 /*
830 * Ignore entries that didn't accumulate any actual counts, such as
831 * indexes that were opened by the planner but not used.
832 */
833 if (pg_memory_is_all_zeros(&lstats->counts,
834 sizeof(struct PgStat_TableCounts)))
835 return true;
836
837 if (!pgstat_lock_entry(entry_ref, nowait))
838 return false;
839
840 /* add the values to the shared entry. */
841 tabentry = &shtabstats->stats;
842
843 tabentry->numscans += lstats->counts.numscans;
844 if (lstats->counts.numscans)
845 {
847
848 if (t > tabentry->lastscan)
849 tabentry->lastscan = t;
850 }
851 tabentry->tuples_returned += lstats->counts.tuples_returned;
852 tabentry->tuples_fetched += lstats->counts.tuples_fetched;
853 tabentry->tuples_inserted += lstats->counts.tuples_inserted;
854 tabentry->tuples_updated += lstats->counts.tuples_updated;
855 tabentry->tuples_deleted += lstats->counts.tuples_deleted;
856 tabentry->tuples_hot_updated += lstats->counts.tuples_hot_updated;
857 tabentry->tuples_newpage_updated += lstats->counts.tuples_newpage_updated;
858
859 /*
860 * If table was truncated/dropped, first reset the live/dead counters.
861 */
862 if (lstats->counts.truncdropped)
863 {
864 tabentry->live_tuples = 0;
865 tabentry->dead_tuples = 0;
866 tabentry->ins_since_vacuum = 0;
867 }
868
869 tabentry->live_tuples += lstats->counts.delta_live_tuples;
870 tabentry->dead_tuples += lstats->counts.delta_dead_tuples;
871 tabentry->mod_since_analyze += lstats->counts.changed_tuples;
872
873 /*
874 * Using tuples_inserted to update ins_since_vacuum does mean that we'll
875 * track aborted inserts too. This isn't ideal, but otherwise probably
876 * not worth adding an extra field for. It may just amount to autovacuums
877 * triggering for inserts more often than they maybe should, which is
878 * probably not going to be common enough to be too concerned about here.
879 */
880 tabentry->ins_since_vacuum += lstats->counts.tuples_inserted;
881
882 tabentry->blocks_fetched += lstats->counts.blocks_fetched;
883 tabentry->blocks_hit += lstats->counts.blocks_hit;
884
885 /* Clamp live_tuples in case of negative delta_live_tuples */
886 tabentry->live_tuples = Max(tabentry->live_tuples, 0);
887 /* Likewise for dead_tuples */
888 tabentry->dead_tuples = Max(tabentry->dead_tuples, 0);
889
890 pgstat_unlock_entry(entry_ref);
891
892 /* The entry was successfully flushed, add the same to database stats */
894 dbentry->tuples_returned += lstats->counts.tuples_returned;
895 dbentry->tuples_fetched += lstats->counts.tuples_fetched;
896 dbentry->tuples_inserted += lstats->counts.tuples_inserted;
897 dbentry->tuples_updated += lstats->counts.tuples_updated;
898 dbentry->tuples_deleted += lstats->counts.tuples_deleted;
899 dbentry->blocks_fetched += lstats->counts.blocks_fetched;
900 dbentry->blocks_hit += lstats->counts.blocks_hit;
901
902 return true;
903}
#define Max(x, y)
Definition c.h:1085
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
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition xact.c:893

References PgStat_HashKey::dboid, fb(), GetCurrentTransactionStopTimestamp(), PgStatShared_HashEntry::key, Max, PgStat_EntryRef::pending, pg_memory_is_all_zeros(), pgstat_lock_entry(), pgstat_prep_database_pending(), pgstat_unlock_entry(), PgStat_EntryRef::shared_entry, and PgStat_EntryRef::shared_stats.

◆ pgstat_relation_reset_timestamp_cb()

void pgstat_relation_reset_timestamp_cb ( PgStatShared_Common header,
TimestampTz  ts 
)
extern

Definition at line 915 of file pgstat_relation.c.

916{
917 ((PgStatShared_Relation *) header)->stats.stat_reset_time = ts;
918}

◆ pgstat_replslot_from_serialized_name_cb()

bool pgstat_replslot_from_serialized_name_cb ( const NameData name,
PgStat_HashKey key 
)
extern

Definition at line 233 of file pgstat_replslot.c.

234{
235 int idx = get_replslot_index(NameStr(*name), true);
236
237 /* slot might have been deleted */
238 if (idx == -1)
239 return false;
240
242 key->dboid = InvalidOid;
243 key->objid = idx;
244
245 return true;
246}
#define NameStr(name)
Definition c.h:835
#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, name, NameStr, and PGSTAT_KIND_REPLSLOT.

◆ pgstat_replslot_reset_timestamp_cb()

void pgstat_replslot_reset_timestamp_cb ( PgStatShared_Common header,
TimestampTz  ts 
)
extern

Definition at line 249 of file pgstat_replslot.c.

250{
251 ((PgStatShared_ReplSlot *) header)->stats.stat_reset_timestamp = ts;
252}

◆ pgstat_replslot_to_serialized_name_cb()

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

Definition at line 220 of file pgstat_replslot.c.

221{
222 /*
223 * This is only called late during shutdown. The set of existing slots
224 * isn't allowed to change at this point, we can assume that a slot exists
225 * at the offset.
226 */
227 if (!ReplicationSlotName(key->objid, name))
228 elog(ERROR, "could not find name for replication slot index %" PRIu64,
229 key->objid);
230}
#define elog(elevel,...)
Definition elog.h:228
bool ReplicationSlotName(int index, Name name)
Definition slot.c:598

References elog, ERROR, fb(), name, and ReplicationSlotName().

◆ pgstat_report_disconnect()

void pgstat_report_disconnect ( Oid  dboid)
extern

Definition at line 254 of file pgstat_database.c.

255{
257
259 return;
260
262
263 switch (pgStatSessionEndCause)
264 {
267 /* we don't collect these */
268 break;
270 dbentry->sessions_abandoned++;
271 break;
272 case DISCONNECT_FATAL:
273 dbentry->sessions_fatal++;
274 break;
276 dbentry->sessions_killed++;
277 break;
278 }
279}
@ DISCONNECT_NOT_YET
Definition pgstat.h:60
@ DISCONNECT_FATAL
Definition pgstat.h:63
@ DISCONNECT_KILLED
Definition pgstat.h:64
@ DISCONNECT_CLIENT_EOF
Definition pgstat.h:62
@ DISCONNECT_NORMAL
Definition pgstat.h:61
static bool pgstat_should_report_connstat(void)
SessionEndType pgStatSessionEndCause

References DISCONNECT_CLIENT_EOF, DISCONNECT_FATAL, DISCONNECT_KILLED, DISCONNECT_NORMAL, DISCONNECT_NOT_YET, fb(), pgstat_prep_database_pending(), pgstat_should_report_connstat(), and pgStatSessionEndCause.

Referenced by pgstat_shutdown_hook().

◆ pgstat_request_entry_refs_gc()

◆ pgstat_reset_database_timestamp()

void pgstat_reset_database_timestamp ( Oid  dboid,
TimestampTz  ts 
)
extern

Definition at line 417 of file pgstat_database.c.

418{
421
423 false);
424
425 dbentry = (PgStatShared_Database *) dbref->shared_stats;
426 dbentry->stats.stat_reset_timestamp = ts;
427
429}

References fb(), InvalidOid, pgstat_get_entry_ref_locked(), PGSTAT_KIND_DATABASE, and pgstat_unlock_entry().

Referenced by pgstat_reset().

◆ pgstat_reset_entries_of_kind()

void pgstat_reset_entries_of_kind ( PgStat_Kind  kind,
TimestampTz  ts 
)
extern

Definition at line 1173 of file pgstat_shmem.c.

1174{
1176}
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:212

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 
)
extern

Definition at line 1117 of file pgstat_shmem.c.

1118{
1119 PgStat_EntryRef *entry_ref;
1120
1121 Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1122
1123 entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
1124 if (!entry_ref || entry_ref->shared_entry->dropped)
1125 return;
1126
1127 (void) pgstat_lock_entry(entry_ref, false);
1128 shared_stat_reset_contents(kind, entry_ref->shared_stats, ts);
1129 pgstat_unlock_entry(entry_ref);
1130}
static void shared_stat_reset_contents(PgStat_Kind kind, PgStatShared_Common *header, TimestampTz ts)

References Assert, PgStatShared_HashEntry::dropped, fb(), 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 
)
extern

◆ pgstat_slru_flush_cb()

bool pgstat_slru_flush_cb ( bool  nowait)
extern

Definition at line 143 of file pgstat_slru.c.

144{
146 int i;
147
148 if (!have_slrustats)
149 return false;
150
151 if (!nowait)
154 return true;
155
156 for (i = 0; i < SLRU_NUM_ELEMENTS; i++)
157 {
160
161#define SLRU_ACC(fld) sharedent->fld += pendingent->fld
162 SLRU_ACC(blocks_zeroed);
163 SLRU_ACC(blocks_hit);
164 SLRU_ACC(blocks_read);
166 SLRU_ACC(blocks_exists);
167 SLRU_ACC(flush);
168 SLRU_ACC(truncate);
169#undef SLRU_ACC
170 }
171
172 /* done, clear the pending entry */
174
176
177 have_slrustats = false;
178
179 return false;
180}
#define MemSet(start, val, len)
Definition c.h:1107
static int64 blocks_written
#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)
PgStatShared_SLRU slru

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

◆ pgstat_slru_init_shmem_cb()

void pgstat_slru_init_shmem_cb ( void stats)
extern

Definition at line 183 of file pgstat_slru.c.

References fb(), and LWLockInitialize().

◆ pgstat_slru_reset_all_cb()

void pgstat_slru_reset_all_cb ( TimestampTz  ts)
extern

Definition at line 191 of file pgstat_slru.c.

192{
193 for (int i = 0; i < SLRU_NUM_ELEMENTS; i++)
195}
static void pgstat_reset_slru_counter_internal(int index, TimestampTz ts)

References i, pgstat_reset_slru_counter_internal(), and SLRU_NUM_ELEMENTS.

◆ pgstat_slru_snapshot_cb()

void pgstat_slru_snapshot_cb ( void  )
extern

◆ pgstat_snapshot_fixed()

void pgstat_snapshot_fixed ( PgStat_Kind  kind)
extern

Definition at line 1104 of file pgstat.c.

1106{
1108 Assert(pgstat_get_kind_info(kind)->fixed_amount);
1109
1112
1115 else
1117
1118 if (pgstat_is_kind_builtin(kind))
1120 else if (pgstat_is_kind_custom(kind))
static void pgstat_build_snapshot_fixed(PgStat_Kind kind)
Definition pgstat.c:1254
static bool pgstat_is_kind_valid(PgStat_Kind kind)
Definition pgstat.c:1473
void pgstat_clear_snapshot(void)
Definition pgstat.c:930
static bool force_stats_snapshot_clear
Definition pgstat.c:256
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 pgstat_fetch_slru(), pgstat_fetch_stat_archiver(), pgstat_fetch_stat_bgwriter(), pgstat_fetch_stat_checkpointer(), pgstat_fetch_stat_io(), pgstat_fetch_stat_lock(), pgstat_fetch_stat_wal(), and test_custom_stats_fixed_report().

◆ pgstat_subscription_flush_cb()

bool pgstat_subscription_flush_cb ( PgStat_EntryRef entry_ref,
bool  nowait 
)
extern

Definition at line 120 of file pgstat_subscription.c.

121{
124
125 localent = (PgStat_BackendSubEntry *) entry_ref->pending;
127
128 /* localent always has non-zero content */
129
130 if (!pgstat_lock_entry(entry_ref, nowait))
131 return false;
132
133#define SUB_ACC(fld) shsubent->stats.fld += localent->fld
134 SUB_ACC(apply_error_count);
135 SUB_ACC(sync_seq_error_count);
136 SUB_ACC(sync_table_error_count);
137 for (int i = 0; i < CONFLICT_NUM_TYPES; i++)
138 SUB_ACC(conflict_count[i]);
139#undef SUB_ACC
140
141 pgstat_unlock_entry(entry_ref);
142 return true;
143}
#define CONFLICT_NUM_TYPES
Definition conflict.h:64
#define SUB_ACC(fld)

References CONFLICT_NUM_TYPES, fb(), 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 
)
extern

Definition at line 146 of file pgstat_subscription.c.

147{
148 ((PgStatShared_Subscription *) header)->stats.stat_reset_timestamp = ts;
149}

◆ pgstat_unlock_entry()

◆ pgstat_update_dbstats()

void pgstat_update_dbstats ( TimestampTz  ts)
extern

Definition at line 333 of file pgstat_database.c.

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

References fb(), MyDatabaseId, OidIsValid, pgLastSessionReportTime, pgstat_prep_database_pending(), pgstat_should_report_connstat(), pgStatActiveTime, pgStatBlockReadTime, pgStatBlockWriteTime, pgStatTransactionIdleTime, pgStatXactCommit, pgStatXactRollback, and TimestampDifference().

Referenced by pgstat_report_stat().

◆ pgstat_wal_flush_cb()

bool pgstat_wal_flush_cb ( bool  nowait)
extern

Definition at line 91 of file pgstat_wal.c.

92{
95
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 */
113
114 if (!nowait)
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_fpi_bytes, wal_usage_diff);
125 WALSTAT_ACC(wal_buffers_full, wal_usage_diff);
126#undef WALSTAT_ACC
127
129
130 /*
131 * Save the current counters for the subsequent calculation of WAL usage.
132 */
134
135 return false;
136}
WalUsage pgWalUsage
Definition instrument.c:27
void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
Definition instrument.c:367
#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, fb(), PgStat_ShmemControl::is_shutdown, IsPostmasterEnvironment, IsUnderPostmaster, 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  )
extern

Definition at line 139 of file pgstat_wal.c.

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

References pgWalUsage, and prevWalUsage.

◆ pgstat_wal_init_shmem_cb()

void pgstat_wal_init_shmem_cb ( void stats)
extern

Definition at line 150 of file pgstat_wal.c.

References fb(), and LWLockInitialize().

◆ pgstat_wal_reset_all_cb()

void pgstat_wal_reset_all_cb ( TimestampTz  ts)
extern

Definition at line 158 of file pgstat_wal.c.

159{
161
163 memset(&stats_shmem->stats, 0, sizeof(stats_shmem->stats));
164 stats_shmem->stats.stat_reset_timestamp = ts;
166}

References fb(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), pgStatLocal, PgStat_LocalState::shmem, and PgStat_ShmemControl::wal.

◆ pgstat_wal_snapshot_cb()

◆ pgstat_write_chunk()

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

Definition at line 1599 of file pgstat.c.

1601{
1602 int rc;
1603
1604 rc = fwrite(ptr, len, 1, fpout);
1605
1606 /* We check for errors with ferror() when done writing the stats. */
1607 (void) rc;

References fb(), and len.

Referenced by pgstat_write_statsfile(), and test_custom_stats_var_to_serialized_data().

◆ PostPrepare_PgStat_Relations()

void PostPrepare_PgStat_Relations ( PgStat_SubXactStatus xact_state)
extern

Definition at line 729 of file pgstat_relation.c.

730{
732
733 for (trans = xact_state->first; trans != NULL; trans = trans->next)
734 {
736
737 tabstat = trans->parent;
738 tabstat->trans = NULL;
739 }
740}
struct PgStat_TableXactStatus * trans
Definition pgstat.h:184

References fb(), 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 213 of file pgstat.c.

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

◆ slru_names

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

Definition at line 395 of file pgstat_internal.h.

395 {
396 "commit_timestamp",
397 "multixact_member",
398 "multixact_offset",
399 "notify",
400 "serializable",
401 "subtransaction",
402 "transaction",
403 "other" /* has to be last */
404};

Referenced by pgstat_get_slru_index(), and pgstat_get_slru_name().