PostgreSQL Source Code  git master
pgstat_internal.h File Reference
#include "common/hashfn_unstable.h"
#include "lib/dshash.h"
#include "lib/ilist.h"
#include "pgstat.h"
#include "storage/lwlock.h"
#include "utils/dsa.h"
Include dependency graph for pgstat_internal.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

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

Macros

#define SLRU_NUM_ELEMENTS   lengthof(slru_names)
 
#define pgstat_assert_is_up()   ((void)true)
 

Typedefs

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

Functions

static void pgstat_begin_changecount_write (uint32 *cc)
 
static void pgstat_end_changecount_write (uint32 *cc)
 
static uint32 pgstat_begin_changecount_read (uint32 *cc)
 
static bool pgstat_end_changecount_read (uint32 *cc, uint32 cc_before)
 
static void pgstat_copy_changecounted_stats (void *dst, void *src, size_t len, uint32 *cc)
 
static int pgstat_cmp_hash_key (const void *a, const void *b, size_t size, void *arg)
 
static uint32 pgstat_hash_hash_key (const void *d, size_t size, void *arg)
 
static size_t pgstat_get_entry_len (PgStat_Kind kind)
 
static void * pgstat_get_entry_data (PgStat_Kind kind, PgStatShared_Common *entry)
 
const PgStat_KindInfopgstat_get_kind_info (PgStat_Kind kind)
 
void pgstat_delete_pending_entry (PgStat_EntryRef *entry_ref)
 
PgStat_EntryRefpgstat_prep_pending_entry (PgStat_Kind kind, Oid dboid, Oid objoid, bool *created_entry)
 
PgStat_EntryRefpgstat_fetch_pending_entry (PgStat_Kind kind, Oid dboid, Oid objoid)
 
void * pgstat_fetch_entry (PgStat_Kind kind, Oid dboid, Oid objoid)
 
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)
 
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)
 
bool pgstat_flush_io (bool nowait)
 
void pgstat_io_init_shmem_cb (void *stats)
 
void pgstat_io_reset_all_cb (TimestampTz ts)
 
void pgstat_io_snapshot_cb (void)
 
void AtEOXact_PgStat_Relations (PgStat_SubXactStatus *xact_state, bool isCommit)
 
void AtEOSubXact_PgStat_Relations (PgStat_SubXactStatus *xact_state, bool isCommit, int nestDepth)
 
void AtPrepare_PgStat_Relations (PgStat_SubXactStatus *xact_state)
 
void PostPrepare_PgStat_Relations (PgStat_SubXactStatus *xact_state)
 
bool pgstat_relation_flush_cb (PgStat_EntryRef *entry_ref, bool nowait)
 
void pgstat_relation_delete_pending_cb (PgStat_EntryRef *entry_ref)
 
void pgstat_replslot_reset_timestamp_cb (PgStatShared_Common *header, TimestampTz ts)
 
void pgstat_replslot_to_serialized_name_cb (const PgStat_HashKey *key, const PgStatShared_Common *header, NameData *name)
 
bool pgstat_replslot_from_serialized_name_cb (const NameData *name, PgStat_HashKey *key)
 
void pgstat_attach_shmem (void)
 
void pgstat_detach_shmem (void)
 
PgStat_EntryRefpgstat_get_entry_ref (PgStat_Kind kind, Oid dboid, Oid objoid, 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, Oid objoid)
 
void pgstat_drop_all_entries (void)
 
PgStat_EntryRefpgstat_get_entry_ref_locked (PgStat_Kind kind, Oid dboid, Oid objoid, bool nowait)
 
void pgstat_reset_entry (PgStat_Kind kind, Oid dboid, Oid objoid, 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 (bool nowait)
 
void pgstat_slru_init_shmem_cb (void *stats)
 
void pgstat_slru_reset_all_cb (TimestampTz ts)
 
void pgstat_slru_snapshot_cb (void)
 
bool pgstat_flush_wal (bool nowait)
 
void pgstat_init_wal (void)
 
bool pgstat_have_pending_wal (void)
 
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, Oid objoid)
 
void pgstat_create_transactional (PgStat_Kind kind, Oid dboid, Oid objoid)
 

Variables

static const char *const slru_names []
 
PGDLLIMPORT PgStat_LocalState pgStatLocal
 
PGDLLIMPORT bool have_iostats
 
PGDLLIMPORT bool have_slrustats
 

Macro Definition Documentation

◆ pgstat_assert_is_up

#define pgstat_assert_is_up ( )    ((void)true)

Definition at line 523 of file pgstat_internal.h.

◆ SLRU_NUM_ELEMENTS

#define SLRU_NUM_ELEMENTS   lengthof(slru_names)

Definition at line 295 of file pgstat_internal.h.

Typedef Documentation

◆ PgStat_EntryRef

◆ PgStat_HashKey

◆ PgStat_KindInfo

◆ PgStat_LocalState

◆ PgStat_ShmemControl

◆ PgStat_Snapshot

◆ PgStat_SubXactStatus

◆ PgStatShared_Archiver

◆ PgStatShared_BgWriter

◆ PgStatShared_Checkpointer

◆ PgStatShared_Common

◆ PgStatShared_Database

◆ PgStatShared_Function

◆ PgStatShared_HashEntry

◆ PgStatShared_IO

◆ PgStatShared_Relation

◆ PgStatShared_ReplSlot

◆ PgStatShared_SLRU

◆ PgStatShared_Subscription

◆ PgStatShared_Wal

Function Documentation

◆ AtEOSubXact_PgStat_Relations()

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

Definition at line 595 of file pgstat_relation.c.

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

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

Referenced by AtEOSubXact_PgStat().

◆ AtEOXact_PgStat_Database()

void AtEOXact_PgStat_Database ( bool  isCommit,
bool  parallel 
)

Definition at line 249 of file pgstat_database.c.

250 {
251  /* Don't count parallel worker transaction stats */
252  if (!parallel)
253  {
254  /*
255  * Count transaction commit or abort. (We use counters, not just
256  * bools, in case the reporting message isn't sent right away.)
257  */
258  if (isCommit)
260  else
262  }
263 }
static int pgStatXactCommit
static int pgStatXactRollback

References pgStatXactCommit, and pgStatXactRollback.

Referenced by AtEOXact_PgStat().

◆ AtEOXact_PgStat_Relations()

void AtEOXact_PgStat_Relations ( PgStat_SubXactStatus xact_state,
bool  isCommit 
)

Definition at line 537 of file pgstat_relation.c.

538 {
540 
541  for (trans = xact_state->first; trans != NULL; trans = trans->next)
542  {
543  PgStat_TableStatus *tabstat;
544 
545  Assert(trans->nest_level == 1);
546  Assert(trans->upper == NULL);
547  tabstat = trans->parent;
548  Assert(tabstat->trans == trans);
549  /* restore pre-truncate/drop stats (if any) in case of aborted xact */
550  if (!isCommit)
552  /* count attempted actions regardless of commit/abort */
553  tabstat->counts.tuples_inserted += trans->tuples_inserted;
554  tabstat->counts.tuples_updated += trans->tuples_updated;
555  tabstat->counts.tuples_deleted += trans->tuples_deleted;
556  if (isCommit)
557  {
558  tabstat->counts.truncdropped = trans->truncdropped;
559  if (trans->truncdropped)
560  {
561  /* forget live/dead stats seen by backend thus far */
562  tabstat->counts.delta_live_tuples = 0;
563  tabstat->counts.delta_dead_tuples = 0;
564  }
565  /* insert adds a live tuple, delete removes one */
566  tabstat->counts.delta_live_tuples +=
567  trans->tuples_inserted - trans->tuples_deleted;
568  /* update and delete each create a dead tuple */
569  tabstat->counts.delta_dead_tuples +=
570  trans->tuples_updated + trans->tuples_deleted;
571  /* insert, update, delete each count as one change event */
572  tabstat->counts.changed_tuples +=
573  trans->tuples_inserted + trans->tuples_updated +
574  trans->tuples_deleted;
575  }
576  else
577  {
578  /* inserted tuples are dead, deleted tuples are unaffected */
579  tabstat->counts.delta_dead_tuples +=
580  trans->tuples_inserted + trans->tuples_updated;
581  /* an aborted xact generates no changed_tuple events */
582  }
583  tabstat->trans = NULL;
584  }
585 }
PgStat_Counter delta_live_tuples
Definition: pgstat.h:174
PgStat_Counter changed_tuples
Definition: pgstat.h:176

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

Referenced by AtEOXact_PgStat().

◆ AtPrepare_PgStat_Relations()

void AtPrepare_PgStat_Relations ( PgStat_SubXactStatus xact_state)

Definition at line 676 of file pgstat_relation.c.

677 {
679 
680  for (trans = xact_state->first; trans != NULL; trans = trans->next)
681  {
683  TwoPhasePgStatRecord record;
684 
685  Assert(trans->nest_level == 1);
686  Assert(trans->upper == NULL);
687  tabstat = trans->parent;
688  Assert(tabstat->trans == trans);
689 
690  record.tuples_inserted = trans->tuples_inserted;
691  record.tuples_updated = trans->tuples_updated;
692  record.tuples_deleted = trans->tuples_deleted;
693  record.inserted_pre_truncdrop = trans->inserted_pre_truncdrop;
694  record.updated_pre_truncdrop = trans->updated_pre_truncdrop;
695  record.deleted_pre_truncdrop = trans->deleted_pre_truncdrop;
696  record.id = tabstat->id;
697  record.shared = tabstat->shared;
698  record.truncdropped = trans->truncdropped;
699 
701  &record, sizeof(TwoPhasePgStatRecord));
702  }
703 }
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:182
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:1280
#define TWOPHASE_RM_PGSTAT_ID
Definition: twophase_rmgr.h:26

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

Referenced by AtPrepare_PgStat().

◆ pgstat_archiver_init_shmem_cb()

void pgstat_archiver_init_shmem_cb ( void *  stats)

Definition at line 66 of file pgstat_archiver.c.

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

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

◆ pgstat_archiver_reset_all_cb()

void pgstat_archiver_reset_all_cb ( TimestampTz  ts)

Definition at line 74 of file pgstat_archiver.c.

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

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

◆ pgstat_archiver_snapshot_cb()

void pgstat_archiver_snapshot_cb ( void  )

Definition at line 89 of file pgstat_archiver.c.

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

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

◆ pgstat_attach_shmem()

void pgstat_attach_shmem ( void  )

Definition at line 219 of file pgstat_shmem.c.

220 {
221  MemoryContext oldcontext;
222 
223  Assert(pgStatLocal.dsa == NULL);
224 
225  /* stats shared memory persists for the backend lifetime */
227 
229  NULL);
231 
234 
235  MemoryContextSwitchTo(oldcontext);
236 }
void dsa_pin_mapping(dsa_area *area)
Definition: dsa.c:635
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition: dsa.c:545
dshash_table * dshash_attach(dsa_area *area, const dshash_parameters *params, dshash_table_handle handle, void *arg)
Definition: dshash.c:270
MemoryContext TopMemoryContext
Definition: mcxt.c:149
static const dshash_parameters dsh_params
Definition: pgstat_shmem.c:62
MemoryContextSwitchTo(old_ctx)
dshash_table * shared_hash
dshash_table_handle hash_handle

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

Referenced by pgstat_initialize().

◆ pgstat_begin_changecount_read()

static uint32 pgstat_begin_changecount_read ( uint32 cc)
inlinestatic

Definition at line 734 of file pgstat_internal.h.

735 {
736  uint32 before_cc = *cc;
737 
739 
740  pg_read_barrier();
741 
742  return before_cc;
743 }
#define pg_read_barrier()
Definition: atomics.h:149
unsigned int uint32
Definition: c.h:506
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122

References CHECK_FOR_INTERRUPTS, and pg_read_barrier.

Referenced by pgstat_copy_changecounted_stats().

◆ pgstat_begin_changecount_write()

static void pgstat_begin_changecount_write ( uint32 cc)
inlinestatic

Definition at line 712 of file pgstat_internal.h.

713 {
714  Assert((*cc & 1) == 0);
715 
717  (*cc)++;
719 }
#define pg_write_barrier()
Definition: atomics.h:150
#define START_CRIT_SECTION()
Definition: miscadmin.h:149

References Assert, pg_write_barrier, and START_CRIT_SECTION.

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

◆ pgstat_bgwriter_init_shmem_cb()

void pgstat_bgwriter_init_shmem_cb ( void *  stats)

◆ pgstat_bgwriter_reset_all_cb()

void pgstat_bgwriter_reset_all_cb ( TimestampTz  ts)

Definition at line 87 of file pgstat_bgwriter.c.

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

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

◆ pgstat_bgwriter_snapshot_cb()

void pgstat_bgwriter_snapshot_cb ( void  )

Definition at line 102 of file pgstat_bgwriter.c.

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

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

◆ pgstat_checkpointer_init_shmem_cb()

void pgstat_checkpointer_init_shmem_cb ( void *  stats)

◆ pgstat_checkpointer_reset_all_cb()

void pgstat_checkpointer_reset_all_cb ( TimestampTz  ts)

Definition at line 96 of file pgstat_checkpointer.c.

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

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

◆ pgstat_checkpointer_snapshot_cb()

void pgstat_checkpointer_snapshot_cb ( void  )

Definition at line 111 of file pgstat_checkpointer.c.

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

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

◆ pgstat_cmp_hash_key()

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

Definition at line 789 of file pgstat_internal.h.

790 {
791  Assert(size == sizeof(PgStat_HashKey) && arg == NULL);
792  return memcmp(a, b, sizeof(PgStat_HashKey));
793 }
int b
Definition: isn.c:70
int a
Definition: isn.c:69
void * arg
static pg_noinline void Size size
Definition: slab.c:607

References a, arg, Assert, b, and size.

◆ pgstat_copy_changecounted_stats()

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

Definition at line 773 of file pgstat_internal.h.

775 {
776  uint32 cc_before;
777 
778  do
779  {
780  cc_before = pgstat_begin_changecount_read(cc);
781 
782  memcpy(dst, src, len);
783  }
784  while (!pgstat_end_changecount_read(cc, cc_before));
785 }
const void size_t len
static uint32 pgstat_begin_changecount_read(uint32 *cc)
static bool pgstat_end_changecount_read(uint32 *cc, uint32 cc_before)

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

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

◆ pgstat_create_transactional()

void pgstat_create_transactional ( PgStat_Kind  kind,
Oid  dboid,
Oid  objoid 
)

Definition at line 357 of file pgstat_xact.c.

358 {
359  if (pgstat_get_entry_ref(kind, dboid, objoid, false, NULL))
360  {
362  errmsg("resetting existing statistics for kind %s, db=%u, oid=%u",
363  (pgstat_get_kind_info(kind))->name, dboid, objoid));
364 
365  pgstat_reset(kind, dboid, objoid);
366  }
367 
368  create_drop_transactional_internal(kind, dboid, objoid, /* create */ true);
369 }
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define WARNING
Definition: elog.h:36
#define ereport(elevel,...)
Definition: elog.h:149
void pgstat_reset(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:777
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1306
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:407
static void create_drop_transactional_internal(PgStat_Kind kind, Oid dboid, Oid objoid, bool is_create)
Definition: pgstat_xact.c:332
const char * name

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

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

◆ pgstat_database_flush_cb()

bool pgstat_database_flush_cb ( PgStat_EntryRef entry_ref,
bool  nowait 
)

Definition at line 375 of file pgstat_database.c.

376 {
377  PgStatShared_Database *sharedent;
378  PgStat_StatDBEntry *pendingent;
379 
380  pendingent = (PgStat_StatDBEntry *) entry_ref->pending;
381  sharedent = (PgStatShared_Database *) entry_ref->shared_stats;
382 
383  if (!pgstat_lock_entry(entry_ref, nowait))
384  return false;
385 
386 #define PGSTAT_ACCUM_DBCOUNT(item) \
387  (sharedent)->stats.item += (pendingent)->item
388 
389  PGSTAT_ACCUM_DBCOUNT(xact_commit);
390  PGSTAT_ACCUM_DBCOUNT(xact_rollback);
391  PGSTAT_ACCUM_DBCOUNT(blocks_fetched);
392  PGSTAT_ACCUM_DBCOUNT(blocks_hit);
393 
394  PGSTAT_ACCUM_DBCOUNT(tuples_returned);
395  PGSTAT_ACCUM_DBCOUNT(tuples_fetched);
396  PGSTAT_ACCUM_DBCOUNT(tuples_inserted);
397  PGSTAT_ACCUM_DBCOUNT(tuples_updated);
398  PGSTAT_ACCUM_DBCOUNT(tuples_deleted);
399 
400  /* last_autovac_time is reported immediately */
401  Assert(pendingent->last_autovac_time == 0);
402 
403  PGSTAT_ACCUM_DBCOUNT(conflict_tablespace);
404  PGSTAT_ACCUM_DBCOUNT(conflict_lock);
405  PGSTAT_ACCUM_DBCOUNT(conflict_snapshot);
406  PGSTAT_ACCUM_DBCOUNT(conflict_logicalslot);
407  PGSTAT_ACCUM_DBCOUNT(conflict_bufferpin);
408  PGSTAT_ACCUM_DBCOUNT(conflict_startup_deadlock);
409 
410  PGSTAT_ACCUM_DBCOUNT(temp_bytes);
411  PGSTAT_ACCUM_DBCOUNT(temp_files);
412  PGSTAT_ACCUM_DBCOUNT(deadlocks);
413 
414  /* checksum failures are reported immediately */
415  Assert(pendingent->checksum_failures == 0);
416  Assert(pendingent->last_checksum_failure == 0);
417 
418  PGSTAT_ACCUM_DBCOUNT(blk_read_time);
419  PGSTAT_ACCUM_DBCOUNT(blk_write_time);
420 
421  PGSTAT_ACCUM_DBCOUNT(sessions);
422  PGSTAT_ACCUM_DBCOUNT(session_time);
423  PGSTAT_ACCUM_DBCOUNT(active_time);
424  PGSTAT_ACCUM_DBCOUNT(idle_in_transaction_time);
425  PGSTAT_ACCUM_DBCOUNT(sessions_abandoned);
426  PGSTAT_ACCUM_DBCOUNT(sessions_fatal);
427  PGSTAT_ACCUM_DBCOUNT(sessions_killed);
428 #undef PGSTAT_ACCUM_DBCOUNT
429 
430  pgstat_unlock_entry(entry_ref);
431 
432  memset(pendingent, 0, sizeof(*pendingent));
433 
434  return true;
435 }
#define PGSTAT_ACCUM_DBCOUNT(item)
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:613
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:585
PgStatShared_Common * shared_stats
TimestampTz last_checksum_failure
Definition: pgstat.h:345
TimestampTz last_autovac_time
Definition: pgstat.h:334
PgStat_Counter checksum_failures
Definition: pgstat.h:344

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

◆ pgstat_database_reset_timestamp_cb()

void pgstat_database_reset_timestamp_cb ( PgStatShared_Common header,
TimestampTz  ts 
)

Definition at line 438 of file pgstat_database.c.

439 {
440  ((PgStatShared_Database *) header)->stats.stat_reset_timestamp = ts;
441 }

◆ pgstat_delete_pending_entry()

void pgstat_delete_pending_entry ( PgStat_EntryRef entry_ref)

Definition at line 1199 of file pgstat.c.

1201 {
1202  PgStat_Kind kind = entry_ref->shared_entry->key.kind;
1203  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
1204  void *pending_data = entry_ref->pending;
1205 
1206  Assert(pending_data != NULL);
1207  /* !fixed_amount stats should be handled explicitly */
1208  Assert(!pgstat_get_kind_info(kind)->fixed_amount);
1209 
1210  if (kind_info->delete_pending_cb)
1211  kind_info->delete_pending_cb(entry_ref);
1212 
1213  pfree(pending_data);
1214  entry_ref->pending = NULL;
1215 
1216  dlist_delete(&entry_ref->pending_node);
static void dlist_delete(dlist_node *node)
Definition: ilist.h:405
PgStat_Kind
Definition: pgstat.h:36
PgStatShared_HashEntry * shared_entry
dlist_node pending_node
PgStat_Kind kind
void(* delete_pending_cb)(PgStat_EntryRef *sr)

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

Referenced by pgstat_flush_pending_entries(), and pgstat_release_entry_ref().

◆ pgstat_detach_shmem()

void pgstat_detach_shmem ( void  )

Definition at line 239 of file pgstat_shmem.c.

240 {
242 
243  /* we shouldn't leave references to shared stats */
245 
247  pgStatLocal.shared_hash = NULL;
248 
250 
251  /*
252  * dsa_detach() does not decrement the DSA reference count as no segment
253  * was provided to dsa_attach_in_place(), causing no cleanup callbacks to
254  * be registered. Hence, release it manually now.
255  */
257 
258  pgStatLocal.dsa = NULL;
259 }
void dsa_release_in_place(void *place)
Definition: dsa.c:605
void dsa_detach(dsa_area *area)
Definition: dsa.c:1952
void dshash_detach(dshash_table *hash_table)
Definition: dshash.c:307
static void pgstat_release_all_entry_refs(bool discard_pending)
Definition: pgstat_shmem.c:724

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

Referenced by pgstat_shutdown_hook().

◆ pgstat_drop_all_entries()

void pgstat_drop_all_entries ( void  )

Definition at line 920 of file pgstat_shmem.c.

921 {
922  dshash_seq_status hstat;
924  uint64 not_freed_count = 0;
925 
926  dshash_seq_init(&hstat, pgStatLocal.shared_hash, true);
927  while ((ps = dshash_seq_next(&hstat)) != NULL)
928  {
929  if (ps->dropped)
930  continue;
931 
932  if (!pgstat_drop_entry_internal(ps, &hstat))
933  not_freed_count++;
934  }
935  dshash_seq_term(&hstat);
936 
937  if (not_freed_count > 0)
939 }
void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table, bool exclusive)
Definition: dshash.c:638
void dshash_seq_term(dshash_seq_status *status)
Definition: dshash.c:747
void * dshash_seq_next(dshash_seq_status *status)
Definition: dshash.c:657
struct parser_state ps
void pgstat_request_entry_refs_gc(void)
Definition: pgstat_shmem.c:638
static bool pgstat_drop_entry_internal(PgStatShared_HashEntry *shent, dshash_seq_status *hstat)
Definition: pgstat_shmem.c:783

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

Referenced by pgstat_reset_after_failure().

◆ pgstat_drop_entry()

bool pgstat_drop_entry ( PgStat_Kind  kind,
Oid  dboid,
Oid  objoid 
)

Definition at line 883 of file pgstat_shmem.c.

884 {
885  PgStat_HashKey key = {.kind = kind,.dboid = dboid,.objoid = objoid};
886  PgStatShared_HashEntry *shent;
887  bool freed = true;
888 
889  /* delete local reference */
890  if (pgStatEntryRefHash)
891  {
892  PgStat_EntryRefHashEntry *lohashent =
893  pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, key);
894 
895  if (lohashent)
896  pgstat_release_entry_ref(lohashent->key, lohashent->entry_ref,
897  true);
898  }
899 
900  /* mark entry in shared hashtable as deleted, drop if possible */
901  shent = dshash_find(pgStatLocal.shared_hash, &key, true);
902  if (shent)
903  {
904  freed = pgstat_drop_entry_internal(shent, NULL);
905 
906  /*
907  * Database stats contain other stats. Drop those as well when
908  * dropping the database. XXX: Perhaps this should be done in a
909  * slightly more principled way? But not obvious what that'd look
910  * like, and so far this is the only case...
911  */
912  if (key.kind == PGSTAT_KIND_DATABASE)
914  }
915 
916  return freed;
917 }
void * dshash_find(dshash_table *hash_table, const void *key, bool exclusive)
Definition: dshash.c:390
@ PGSTAT_KIND_DATABASE
Definition: pgstat.h:41
static void pgstat_release_entry_ref(PgStat_HashKey key, PgStat_EntryRef *entry_ref, bool discard_pending)
Definition: pgstat_shmem.c:531
static void pgstat_drop_database_and_contents(Oid dboid)
Definition: pgstat_shmem.c:822
static pgstat_entry_ref_hash_hash * pgStatEntryRefHash
Definition: pgstat_shmem.c:82
PgStat_EntryRef * entry_ref
Definition: pgstat_shmem.c:28

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

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

◆ pgstat_drop_transactional()

void pgstat_drop_transactional ( PgStat_Kind  kind,
Oid  dboid,
Oid  objoid 
)

Definition at line 379 of file pgstat_xact.c.

380 {
381  create_drop_transactional_internal(kind, dboid, objoid, /* create */ false);
382 }

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

750 {
751  uint32 after_cc;
752 
753  pg_read_barrier();
754 
755  after_cc = *cc;
756 
757  /* was a write in progress when we started? */
758  if (before_cc & 1)
759  return false;
760 
761  /* did writes start and complete while we read? */
762  return before_cc == after_cc;
763 }

References pg_read_barrier.

Referenced by pgstat_copy_changecounted_stats().

◆ pgstat_end_changecount_write()

static void pgstat_end_changecount_write ( uint32 cc)
inlinestatic

Definition at line 722 of file pgstat_internal.h.

723 {
724  Assert((*cc & 1) == 1);
725 
727 
728  (*cc)++;
729 
731 }
#define END_CRIT_SECTION()
Definition: miscadmin.h:151

References Assert, END_CRIT_SECTION, and pg_write_barrier.

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

◆ pgstat_fetch_entry()

void* pgstat_fetch_entry ( PgStat_Kind  kind,
Oid  dboid,
Oid  objoid 
)

Definition at line 855 of file pgstat.c.

857 {
859  PgStat_EntryRef *entry_ref;
860  void *stats_data;
861  const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
862 
863  /* should be called from backends */
865  Assert(!kind_info->fixed_amount);
866 
868 
869  key.kind = kind;
870  key.dboid = dboid;
871  key.objoid = objoid;
872 
873  /* if we need to build a full snapshot, do so */
876 
877  /* if caching is desired, look up in cache */
879  {
880  PgStat_SnapshotEntry *entry = NULL;
881 
882  entry = pgstat_snapshot_lookup(pgStatLocal.snapshot.stats, key);
883 
884  if (entry)
885  return entry->data;
886 
887  /*
888  * If we built a full snapshot and the key is not in
889  * pgStatLocal.snapshot.stats, there are no matching stats.
890  */
892  return NULL;
893  }
894 
896 
897  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
898 
899  if (entry_ref == NULL || entry_ref->shared_entry->dropped)
900  {
901  /* create empty entry when using PGSTAT_FETCH_CONSISTENCY_CACHE */
903  {
904  PgStat_SnapshotEntry *entry = NULL;
905  bool found;
906 
907  entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
908  Assert(!found);
909  entry->data = NULL;
910  }
911  return NULL;
912  }
913 
914  /*
915  * Allocate in caller's context for PGSTAT_FETCH_CONSISTENCY_NONE,
916  * otherwise we could quickly end up with a fair bit of memory used due to
917  * repeated accesses.
918  */
920  stats_data = palloc(kind_info->shared_data_len);
921  else
923  kind_info->shared_data_len);
924 
925  pgstat_lock_entry_shared(entry_ref, false);
926  memcpy(stats_data,
927  pgstat_get_entry_data(kind, entry_ref->shared_stats),
928  kind_info->shared_data_len);
929  pgstat_unlock_entry(entry_ref);
930 
932  {
933  PgStat_SnapshotEntry *entry = NULL;
934  bool found;
935 
936  entry = pgstat_snapshot_insert(pgStatLocal.snapshot.stats, key, &found);
937  entry->data = stats_data;
938  }
939 
940  return stats_data;
bool IsUnderPostmaster
Definition: globals.c:118
bool IsPostmasterEnvironment
Definition: globals.c:117
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181
void * palloc(Size size)
Definition: mcxt.c:1317
int pgstat_fetch_consistency
Definition: pgstat.c:193
static void pgstat_prep_snapshot(void)
Definition: pgstat.c:998
static void pgstat_build_snapshot(void)
Definition: pgstat.c:1019
@ PGSTAT_FETCH_CONSISTENCY_NONE
Definition: pgstat.h:70
@ PGSTAT_FETCH_CONSISTENCY_CACHE
Definition: pgstat.h:71
@ PGSTAT_FETCH_CONSISTENCY_SNAPSHOT
Definition: pgstat.h:72
static void * pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry)
bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:601
MemoryContext context
PgStat_FetchConsistency mode
struct pgstat_snapshot_hash * stats

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

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

◆ pgstat_fetch_pending_entry()

PgStat_EntryRef* pgstat_fetch_pending_entry ( PgStat_Kind  kind,
Oid  dboid,
Oid  objoid 
)

Definition at line 1186 of file pgstat.c.

1188 {
1189  PgStat_EntryRef *entry_ref;
1190 
1191  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
1192 
1193  if (entry_ref == NULL || entry_ref->pending == NULL)
1194  return NULL;
1195 
1196  return entry_ref;

References PgStat_EntryRef::pending, and pgstat_get_entry_ref().

Referenced by find_funcstat_entry(), and find_tabstat_entry().

◆ pgstat_flush_io()

bool pgstat_flush_io ( bool  nowait)

Definition at line 173 of file pgstat_io.c.

174 {
175  LWLock *bktype_lock;
176  PgStat_BktypeIO *bktype_shstats;
177 
178  if (!have_iostats)
179  return false;
180 
181  bktype_lock = &pgStatLocal.shmem->io.locks[MyBackendType];
182  bktype_shstats =
184 
185  if (!nowait)
186  LWLockAcquire(bktype_lock, LW_EXCLUSIVE);
187  else if (!LWLockConditionalAcquire(bktype_lock, LW_EXCLUSIVE))
188  return true;
189 
190  for (int io_object = 0; io_object < IOOBJECT_NUM_TYPES; io_object++)
191  {
192  for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++)
193  {
194  for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
195  {
196  instr_time time;
197 
198  bktype_shstats->counts[io_object][io_context][io_op] +=
199  PendingIOStats.counts[io_object][io_context][io_op];
200 
201  time = PendingIOStats.pending_times[io_object][io_context][io_op];
202 
203  bktype_shstats->times[io_object][io_context][io_op] +=
205  }
206  }
207  }
208 
210 
211  LWLockRelease(bktype_lock);
212 
213  memset(&PendingIOStats, 0, sizeof(PendingIOStats));
214 
215  have_iostats = false;
216 
217  return false;
218 }
#define INSTR_TIME_GET_MICROSEC(t)
Definition: instr_time.h:194
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1339
BackendType MyBackendType
Definition: miscinit.c:63
#define IOOP_NUM_TYPES
Definition: pgstat.h:308
#define IOCONTEXT_NUM_TYPES
Definition: pgstat.h:294
#define IOOBJECT_NUM_TYPES
Definition: pgstat.h:284
bool pgstat_bktype_io_stats_valid(PgStat_BktypeIO *backend_io, BackendType bktype)
Definition: pgstat_io.c:46
static PgStat_PendingIO PendingIOStats
Definition: pgstat_io.c:31
bool have_iostats
Definition: pgstat_io.c:32
Definition: lwlock.h:42
LWLock locks[BACKEND_NUM_TYPES]
PgStat_Counter times[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:313
PgStat_Counter counts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:312
PgStat_BktypeIO stats[BACKEND_NUM_TYPES]
Definition: pgstat.h:319
PgStat_Counter counts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat_io.c:26
instr_time pending_times[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat_io.c:27
PgStatShared_IO io

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

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

◆ pgstat_flush_wal()

bool pgstat_flush_wal ( bool  nowait)

Definition at line 82 of file pgstat_wal.c.

83 {
84  PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
85  WalUsage wal_usage_diff = {0};
86 
88  Assert(pgStatLocal.shmem != NULL &&
90 
91  /*
92  * This function can be called even if nothing at all has happened. Avoid
93  * taking lock for nothing in that case.
94  */
96  return false;
97 
98  /*
99  * We don't update the WAL usage portion of the local WalStats elsewhere.
100  * Calculate how much WAL usage counters were increased by subtracting the
101  * previous counters from the current ones.
102  */
103  WalUsageAccumDiff(&wal_usage_diff, &pgWalUsage, &prevWalUsage);
104 
105  if (!nowait)
106  LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
107  else if (!LWLockConditionalAcquire(&stats_shmem->lock, LW_EXCLUSIVE))
108  return true;
109 
110 #define WALSTAT_ACC(fld, var_to_add) \
111  (stats_shmem->stats.fld += var_to_add.fld)
112 #define WALSTAT_ACC_INSTR_TIME(fld) \
113  (stats_shmem->stats.fld += INSTR_TIME_GET_MICROSEC(PendingWalStats.fld))
114  WALSTAT_ACC(wal_records, wal_usage_diff);
115  WALSTAT_ACC(wal_fpi, wal_usage_diff);
116  WALSTAT_ACC(wal_bytes, wal_usage_diff);
117  WALSTAT_ACC(wal_buffers_full, PendingWalStats);
118  WALSTAT_ACC(wal_write, PendingWalStats);
119  WALSTAT_ACC(wal_sync, PendingWalStats);
120  WALSTAT_ACC_INSTR_TIME(wal_write_time);
121  WALSTAT_ACC_INSTR_TIME(wal_sync_time);
122 #undef WALSTAT_ACC_INSTR_TIME
123 #undef WALSTAT_ACC
124 
125  LWLockRelease(&stats_shmem->lock);
126 
127  /*
128  * Save the current counters for the subsequent calculation of WAL usage.
129  */
131 
132  /*
133  * Clear out the statistics buffer, so it can be re-used.
134  */
135  MemSet(&PendingWalStats, 0, sizeof(PendingWalStats));
136 
137  return false;
138 }
#define MemSet(start, val, len)
Definition: c.h:1020
WalUsage pgWalUsage
Definition: instrument.c:22
void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
Definition: instrument.c:286
#define WALSTAT_ACC(fld, var_to_add)
bool pgstat_have_pending_wal(void)
Definition: pgstat_wal.c:159
static WalUsage prevWalUsage
Definition: pgstat_wal.c:32
PgStat_PendingWalStats PendingWalStats
Definition: pgstat_wal.c:24
#define WALSTAT_ACC_INSTR_TIME(fld)
PgStatShared_Wal wal

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

Referenced by pgstat_report_stat(), and pgstat_report_wal().

◆ pgstat_function_flush_cb()

bool pgstat_function_flush_cb ( PgStat_EntryRef entry_ref,
bool  nowait 
)

Definition at line 193 of file pgstat_function.c.

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

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

◆ pgstat_get_entry_data()

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

Definition at line 818 of file pgstat_internal.h.

819 {
820  size_t off = pgstat_get_kind_info(kind)->shared_data_off;
821 
822  Assert(off != 0 && off < PG_UINT32_MAX);
823 
824  return ((char *) (entry)) + off;
825 }
#define PG_UINT32_MAX
Definition: c.h:590
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1306

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

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

◆ pgstat_get_entry_len()

static size_t pgstat_get_entry_len ( PgStat_Kind  kind)
inlinestatic

◆ pgstat_get_entry_ref()

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

Definition at line 407 of file pgstat_shmem.c.

409 {
410  PgStat_HashKey key = {.kind = kind,.dboid = dboid,.objoid = objoid};
411  PgStatShared_HashEntry *shhashent;
412  PgStatShared_Common *shheader = NULL;
413  PgStat_EntryRef *entry_ref;
414 
415  /*
416  * passing in created_entry only makes sense if we possibly could create
417  * entry.
418  */
419  Assert(create || created_entry == NULL);
421  Assert(pgStatLocal.shared_hash != NULL);
423 
426 
427  if (created_entry != NULL)
428  *created_entry = false;
429 
430  /*
431  * Check if other backends dropped stats that could not be deleted because
432  * somebody held references to it. If so, check this backend's references.
433  * This is not expected to happen often. The location of the check is a
434  * bit random, but this is a relatively frequently called path, so better
435  * than most.
436  */
439 
440  /*
441  * First check the lookup cache hashtable in local memory. If we find a
442  * match here we can avoid taking locks / causing contention.
443  */
444  if (pgstat_get_entry_ref_cached(key, &entry_ref))
445  return entry_ref;
446 
447  Assert(entry_ref != NULL);
448 
449  /*
450  * Do a lookup in the hash table first - it's quite likely that the entry
451  * already exists, and that way we only need a shared lock.
452  */
453  shhashent = dshash_find(pgStatLocal.shared_hash, &key, false);
454 
455  if (create && !shhashent)
456  {
457  bool shfound;
458 
459  /*
460  * It's possible that somebody created the entry since the above
461  * lookup. If so, fall through to the same path as if we'd have if it
462  * already had been created before the dshash_find() calls.
463  */
464  shhashent = dshash_find_or_insert(pgStatLocal.shared_hash, &key, &shfound);
465  if (!shfound)
466  {
467  shheader = pgstat_init_entry(kind, shhashent);
468  pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
469 
470  if (created_entry != NULL)
471  *created_entry = true;
472 
473  return entry_ref;
474  }
475  }
476 
477  if (!shhashent)
478  {
479  /*
480  * If we're not creating, delete the reference again. In all
481  * likelihood it's just a stats lookup - no point wasting memory for a
482  * shared ref to nothing...
483  */
484  pgstat_release_entry_ref(key, entry_ref, false);
485 
486  return NULL;
487  }
488  else
489  {
490  /*
491  * Can get here either because dshash_find() found a match, or if
492  * dshash_find_or_insert() found a concurrently inserted entry.
493  */
494 
495  if (shhashent->dropped && create)
496  {
497  /*
498  * There are legitimate cases where the old stats entry might not
499  * yet have been dropped by the time it's reused. The most obvious
500  * case are replication slot stats, where a new slot can be
501  * created with the same index just after dropping. But oid
502  * wraparound can lead to other cases as well. We just reset the
503  * stats to their plain state.
504  */
505  shheader = pgstat_reinit_entry(kind, shhashent);
506  pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
507 
508  if (created_entry != NULL)
509  *created_entry = true;
510 
511  return entry_ref;
512  }
513  else if (shhashent->dropped)
514  {
516  pgstat_release_entry_ref(key, entry_ref, false);
517 
518  return NULL;
519  }
520  else
521  {
522  shheader = dsa_get_address(pgStatLocal.dsa, shhashent->body);
523  pgstat_acquire_entry_ref(entry_ref, shhashent, shheader);
524 
525  return entry_ref;
526  }
527  }
528 }
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:942
void dshash_release_lock(dshash_table *hash_table, void *entry)
Definition: dshash.c:558
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
Definition: dshash.c:433
#define pgstat_assert_is_up()
static void pgstat_setup_memcxt(void)
PgStatShared_Common * pgstat_init_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
Definition: pgstat_shmem.c:268
static void pgstat_setup_shared_refs(void)
Definition: pgstat_shmem.c:316
static PgStatShared_Common * pgstat_reinit_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent)
Definition: pgstat_shmem.c:297
static void pgstat_acquire_entry_ref(PgStat_EntryRef *entry_ref, PgStatShared_HashEntry *shhashent, PgStatShared_Common *shheader)
Definition: pgstat_shmem.c:332
static bool pgstat_get_entry_ref_cached(PgStat_HashKey key, PgStat_EntryRef **entry_ref_p)
Definition: pgstat_shmem.c:351
static void pgstat_gc_entry_refs(void)
Definition: pgstat_shmem.c:660
static bool pgstat_need_entry_refs_gc(void)
Definition: pgstat_shmem.c:644

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

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

◆ pgstat_get_entry_ref_locked()

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

Definition at line 622 of file pgstat_shmem.c.

624 {
625  PgStat_EntryRef *entry_ref;
626 
627  /* find shared table stats entry corresponding to the local entry */
628  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, true, NULL);
629 
630  /* lock the shared entry to protect the content, skip if failed */
631  if (!pgstat_lock_entry(entry_ref, nowait))
632  return NULL;
633 
634  return entry_ref;
635 }

References pgstat_get_entry_ref(), and pgstat_lock_entry().

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

◆ pgstat_get_kind_info()

◆ pgstat_get_xact_stack_level()

PgStat_SubXactStatus* pgstat_get_xact_stack_level ( int  nest_level)

Definition at line 236 of file pgstat_xact.c.

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

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

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

◆ pgstat_hash_hash_key()

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

Definition at line 796 of file pgstat_internal.h.

797 {
798  const char *key = (const char *) d;
799 
800  Assert(size == sizeof(PgStat_HashKey) && arg == NULL);
801  return fasthash32(key, size, 0);
802 }
static uint32 fasthash32(const char *k, size_t len, uint64 seed)

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

◆ pgstat_have_pending_wal()

bool pgstat_have_pending_wal ( void  )

◆ pgstat_init_entry()

PgStatShared_Common* pgstat_init_entry ( PgStat_Kind  kind,
PgStatShared_HashEntry shhashent 
)

Definition at line 268 of file pgstat_shmem.c.

270 {
271  /* Create new stats entry. */
273  PgStatShared_Common *shheader;
274 
275  /*
276  * Initialize refcount to 1, marking it as valid / not dropped. The entry
277  * can't be freed before the initialization because it can't be found as
278  * long as we hold the dshash partition lock. Caller needs to increase
279  * further if a longer lived reference is needed.
280  */
281  pg_atomic_init_u32(&shhashent->refcount, 1);
282  shhashent->dropped = false;
283 
284  chunk = dsa_allocate0(pgStatLocal.dsa, pgstat_get_kind_info(kind)->shared_size);
285  shheader = dsa_get_address(pgStatLocal.dsa, chunk);
286  shheader->magic = 0xdeadbeef;
287 
288  /* Link the new entry from the hash entry. */
289  shhashent->body = chunk;
290 
292 
293  return shheader;
294 }
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:214
#define dsa_allocate0(area, size)
Definition: dsa.h:113
uint64 dsa_pointer
Definition: dsa.h:62
uint64 chunk
pg_atomic_uint32 refcount

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

Referenced by pgstat_get_entry_ref(), and pgstat_read_statsfile().

◆ pgstat_init_wal()

void pgstat_init_wal ( void  )

Definition at line 141 of file pgstat_wal.c.

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

References pgWalUsage, and prevWalUsage.

Referenced by pgstat_initialize().

◆ pgstat_io_init_shmem_cb()

void pgstat_io_init_shmem_cb ( void *  stats)

Definition at line 255 of file pgstat_io.c.

256 {
257  PgStatShared_IO *stat_shmem = (PgStatShared_IO *) stats;
258 
259  for (int i = 0; i < BACKEND_NUM_TYPES; i++)
261 }
int i
Definition: isn.c:73
#define BACKEND_NUM_TYPES
Definition: miscadmin.h:370

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

◆ pgstat_io_reset_all_cb()

void pgstat_io_reset_all_cb ( TimestampTz  ts)

Definition at line 264 of file pgstat_io.c.

265 {
266  for (int i = 0; i < BACKEND_NUM_TYPES; i++)
267  {
268  LWLock *bktype_lock = &pgStatLocal.shmem->io.locks[i];
269  PgStat_BktypeIO *bktype_shstats = &pgStatLocal.shmem->io.stats.stats[i];
270 
271  LWLockAcquire(bktype_lock, LW_EXCLUSIVE);
272 
273  /*
274  * Use the lock in the first BackendType's PgStat_BktypeIO to protect
275  * the reset timestamp as well.
276  */
277  if (i == 0)
279 
280  memset(bktype_shstats, 0, sizeof(*bktype_shstats));
281  LWLockRelease(bktype_lock);
282  }
283 }
TimestampTz stat_reset_timestamp
Definition: pgstat.h:318

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

◆ pgstat_io_snapshot_cb()

void pgstat_io_snapshot_cb ( void  )

Definition at line 286 of file pgstat_io.c.

287 {
288  for (int i = 0; i < BACKEND_NUM_TYPES; i++)
289  {
290  LWLock *bktype_lock = &pgStatLocal.shmem->io.locks[i];
291  PgStat_BktypeIO *bktype_shstats = &pgStatLocal.shmem->io.stats.stats[i];
292  PgStat_BktypeIO *bktype_snap = &pgStatLocal.snapshot.io.stats[i];
293 
294  LWLockAcquire(bktype_lock, LW_SHARED);
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)
303 
304  /* using struct assignment due to better type safety */
305  *bktype_snap = *bktype_shstats;
306  LWLockRelease(bktype_lock);
307  }
308 }

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

◆ pgstat_lock_entry()

bool pgstat_lock_entry ( PgStat_EntryRef entry_ref,
bool  nowait 
)

◆ pgstat_lock_entry_shared()

bool pgstat_lock_entry_shared ( PgStat_EntryRef entry_ref,
bool  nowait 
)

Definition at line 601 of file pgstat_shmem.c.

602 {
603  LWLock *lock = &entry_ref->shared_stats->lock;
604 
605  if (nowait)
606  return LWLockConditionalAcquire(lock, LW_SHARED);
607 
608  LWLockAcquire(lock, LW_SHARED);
609  return true;
610 }

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

Referenced by pgstat_fetch_entry().

◆ pgstat_prep_database_pending()

PgStat_StatDBEntry* pgstat_prep_database_pending ( Oid  dboid)

Definition at line 333 of file pgstat_database.c.

334 {
335  PgStat_EntryRef *entry_ref;
336 
337  /*
338  * This should not report stats on database objects before having
339  * connected to a database.
340  */
342 
344  NULL);
345 
346  return entry_ref->pending;
347 }
#define OidIsValid(objectId)
Definition: c.h:775
Oid MyDatabaseId
Definition: globals.c:92
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid, bool *created_entry)
Definition: pgstat.c:1148
#define InvalidOid
Definition: postgres_ext.h:36

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

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

◆ pgstat_prep_pending_entry()

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

Definition at line 1148 of file pgstat.c.

1150 {
1151  PgStat_EntryRef *entry_ref;
1152 
1153  /* need to be able to flush out */
1154  Assert(pgstat_get_kind_info(kind)->flush_pending_cb != NULL);
1155 
1157  {
1160  "PgStat Pending",
1162  }
1163 
1164  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid,
1165  true, created_entry);
1166 
1167  if (entry_ref->pending == NULL)
1168  {
1169  size_t entrysize = pgstat_get_kind_info(kind)->pending_size;
1170 
1171  Assert(entrysize != (size_t) -1);
1172 
1173  entry_ref->pending = MemoryContextAllocZero(pgStatPendingContext, entrysize);
1175  }
1176 
1177  return entry_ref;
#define unlikely(x)
Definition: c.h:311
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:364
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1215
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:170
static MemoryContext pgStatPendingContext
Definition: pgstat.c:218
static dlist_head pgStatPending
Definition: pgstat.c:226

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

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

◆ pgstat_relation_delete_pending_cb()

void pgstat_relation_delete_pending_cb ( PgStat_EntryRef entry_ref)

Definition at line 885 of file pgstat_relation.c.

886 {
887  PgStat_TableStatus *pending = (PgStat_TableStatus *) entry_ref->pending;
888 
889  if (pending->relation)
891 }
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
void pgstat_unlink_relation(Relation rel)
Relation relation
Definition: pgstat.h:203

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

◆ pgstat_relation_flush_cb()

bool pgstat_relation_flush_cb ( PgStat_EntryRef entry_ref,
bool  nowait 
)

Definition at line 802 of file pgstat_relation.c.

803 {
804  static const PgStat_TableCounts all_zeroes;
805  Oid dboid;
806  PgStat_TableStatus *lstats; /* pending stats entry */
807  PgStatShared_Relation *shtabstats;
808  PgStat_StatTabEntry *tabentry; /* table entry of shared stats */
809  PgStat_StatDBEntry *dbentry; /* pending database entry */
810 
811  dboid = entry_ref->shared_entry->key.dboid;
812  lstats = (PgStat_TableStatus *) entry_ref->pending;
813  shtabstats = (PgStatShared_Relation *) entry_ref->shared_stats;
814 
815  /*
816  * Ignore entries that didn't accumulate any actual counts, such as
817  * indexes that were opened by the planner but not used.
818  */
819  if (memcmp(&lstats->counts, &all_zeroes,
820  sizeof(PgStat_TableCounts)) == 0)
821  {
822  return true;
823  }
824 
825  if (!pgstat_lock_entry(entry_ref, nowait))
826  return false;
827 
828  /* add the values to the shared entry. */
829  tabentry = &shtabstats->stats;
830 
831  tabentry->numscans += lstats->counts.numscans;
832  if (lstats->counts.numscans)
833  {
835 
836  if (t > tabentry->lastscan)
837  tabentry->lastscan = t;
838  }
839  tabentry->tuples_returned += lstats->counts.tuples_returned;
840  tabentry->tuples_fetched += lstats->counts.tuples_fetched;
841  tabentry->tuples_inserted += lstats->counts.tuples_inserted;
842  tabentry->tuples_updated += lstats->counts.tuples_updated;
843  tabentry->tuples_deleted += lstats->counts.tuples_deleted;
844  tabentry->tuples_hot_updated += lstats->counts.tuples_hot_updated;
846 
847  /*
848  * If table was truncated/dropped, first reset the live/dead counters.
849  */
850  if (lstats->counts.truncdropped)
851  {
852  tabentry->live_tuples = 0;
853  tabentry->dead_tuples = 0;
854  tabentry->ins_since_vacuum = 0;
855  }
856 
857  tabentry->live_tuples += lstats->counts.delta_live_tuples;
858  tabentry->dead_tuples += lstats->counts.delta_dead_tuples;
859  tabentry->mod_since_analyze += lstats->counts.changed_tuples;
860  tabentry->ins_since_vacuum += lstats->counts.tuples_inserted;
861  tabentry->blocks_fetched += lstats->counts.blocks_fetched;
862  tabentry->blocks_hit += lstats->counts.blocks_hit;
863 
864  /* Clamp live_tuples in case of negative delta_live_tuples */
865  tabentry->live_tuples = Max(tabentry->live_tuples, 0);
866  /* Likewise for dead_tuples */
867  tabentry->dead_tuples = Max(tabentry->dead_tuples, 0);
868 
869  pgstat_unlock_entry(entry_ref);
870 
871  /* The entry was successfully flushed, add the same to database stats */
872  dbentry = pgstat_prep_database_pending(dboid);
873  dbentry->tuples_returned += lstats->counts.tuples_returned;
874  dbentry->tuples_fetched += lstats->counts.tuples_fetched;
875  dbentry->tuples_inserted += lstats->counts.tuples_inserted;
876  dbentry->tuples_updated += lstats->counts.tuples_updated;
877  dbentry->tuples_deleted += lstats->counts.tuples_deleted;
878  dbentry->blocks_fetched += lstats->counts.blocks_fetched;
879  dbentry->blocks_hit += lstats->counts.blocks_hit;
880 
881  return true;
882 }
#define Max(x, y)
Definition: c.h:998
int64 TimestampTz
Definition: timestamp.h:39
PgStat_StatDBEntry * pgstat_prep_database_pending(Oid dboid)
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_StatTabEntry stats
PgStat_Counter tuples_updated
Definition: pgstat.h:332
PgStat_Counter tuples_inserted
Definition: pgstat.h:331
PgStat_Counter tuples_returned
Definition: pgstat.h:329
PgStat_Counter blocks_hit
Definition: pgstat.h:328
PgStat_Counter blocks_fetched
Definition: pgstat.h:327
PgStat_Counter tuples_deleted
Definition: pgstat.h:333
PgStat_Counter tuples_fetched
Definition: pgstat.h:330
PgStat_Counter tuples_fetched
Definition: pgstat.h:405
PgStat_Counter ins_since_vacuum
Definition: pgstat.h:416
PgStat_Counter blocks_hit
Definition: pgstat.h:419
PgStat_Counter mod_since_analyze
Definition: pgstat.h:415
PgStat_Counter tuples_deleted
Definition: pgstat.h:409
PgStat_Counter tuples_hot_updated
Definition: pgstat.h:410
PgStat_Counter tuples_updated
Definition: pgstat.h:408
PgStat_Counter live_tuples
Definition: pgstat.h:413
PgStat_Counter numscans
Definition: pgstat.h:401
PgStat_Counter dead_tuples
Definition: pgstat.h:414
PgStat_Counter blocks_fetched
Definition: pgstat.h:418
PgStat_Counter tuples_returned
Definition: pgstat.h:404
TimestampTz lastscan
Definition: pgstat.h:402
PgStat_Counter tuples_inserted
Definition: pgstat.h:407
PgStat_Counter tuples_newpage_updated
Definition: pgstat.h:411
PgStat_Counter blocks_hit
Definition: pgstat.h:179
PgStat_Counter numscans
Definition: pgstat.h:162
PgStat_Counter tuples_hot_updated
Definition: pgstat.h:170
PgStat_Counter tuples_returned
Definition: pgstat.h:164
PgStat_Counter blocks_fetched
Definition: pgstat.h:178
PgStat_Counter tuples_fetched
Definition: pgstat.h:165
PgStat_Counter tuples_newpage_updated
Definition: pgstat.h:171
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition: xact.c:889

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

◆ pgstat_replslot_from_serialized_name_cb()

bool pgstat_replslot_from_serialized_name_cb ( const NameData name,
PgStat_HashKey key 
)

Definition at line 202 of file pgstat_replslot.c.

203 {
204  int idx = get_replslot_index(NameStr(*name), true);
205 
206  /* slot might have been deleted */
207  if (idx == -1)
208  return false;
209 
210  key->kind = PGSTAT_KIND_REPLSLOT;
211  key->dboid = InvalidOid;
212  key->objoid = idx;
213 
214  return true;
215 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
#define NameStr(name)
Definition: c.h:746
@ PGSTAT_KIND_REPLSLOT
Definition: pgstat.h:44
static int get_replslot_index(const char *name, bool need_lock)

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

◆ pgstat_replslot_reset_timestamp_cb()

void pgstat_replslot_reset_timestamp_cb ( PgStatShared_Common header,
TimestampTz  ts 
)

Definition at line 218 of file pgstat_replslot.c.

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

◆ pgstat_replslot_to_serialized_name_cb()

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

Definition at line 189 of file pgstat_replslot.c.

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

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

◆ pgstat_report_disconnect()

void pgstat_report_disconnect ( Oid  dboid)

Definition at line 208 of file pgstat_database.c.

209 {
210  PgStat_StatDBEntry *dbentry;
211 
213  return;
214 
216 
217  switch (pgStatSessionEndCause)
218  {
219  case DISCONNECT_NOT_YET:
220  case DISCONNECT_NORMAL:
221  /* we don't collect these */
222  break;
224  dbentry->sessions_abandoned++;
225  break;
226  case DISCONNECT_FATAL:
227  dbentry->sessions_fatal++;
228  break;
229  case DISCONNECT_KILLED:
230  dbentry->sessions_killed++;
231  break;
232  }
233 }
@ DISCONNECT_NOT_YET
Definition: pgstat.h:78
@ DISCONNECT_FATAL
Definition: pgstat.h:81
@ DISCONNECT_KILLED
Definition: pgstat.h:82
@ DISCONNECT_CLIENT_EOF
Definition: pgstat.h:80
@ DISCONNECT_NORMAL
Definition: pgstat.h:79
static bool pgstat_should_report_connstat(void)
SessionEndType pgStatSessionEndCause
PgStat_Counter sessions_fatal
Definition: pgstat.h:353
PgStat_Counter sessions_abandoned
Definition: pgstat.h:352
PgStat_Counter sessions_killed
Definition: pgstat.h:354

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

Referenced by pgstat_shutdown_hook().

◆ pgstat_request_entry_refs_gc()

void pgstat_request_entry_refs_gc ( void  )

◆ pgstat_reset_database_timestamp()

void pgstat_reset_database_timestamp ( Oid  dboid,
TimestampTz  ts 
)

Definition at line 354 of file pgstat_database.c.

355 {
356  PgStat_EntryRef *dbref;
357  PgStatShared_Database *dbentry;
358 
360  false);
361 
362  dbentry = (PgStatShared_Database *) dbref->shared_stats;
363  dbentry->stats.stat_reset_timestamp = ts;
364 
365  pgstat_unlock_entry(dbref);
366 }
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, Oid objoid, bool nowait)
Definition: pgstat_shmem.c:622
PgStat_StatDBEntry stats
TimestampTz stat_reset_timestamp
Definition: pgstat.h:356

References InvalidOid, MyDatabaseId, pgstat_get_entry_ref_locked(), PGSTAT_KIND_DATABASE, pgstat_unlock_entry(), PgStat_EntryRef::shared_stats, PgStat_StatDBEntry::stat_reset_timestamp, and PgStatShared_Database::stats.

Referenced by pgstat_reset().

◆ pgstat_reset_entries_of_kind()

void pgstat_reset_entries_of_kind ( PgStat_Kind  kind,
TimestampTz  ts 
)

Definition at line 1014 of file pgstat_shmem.c.

1015 {
1017 }
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)
Definition: pgstat_shmem.c:978
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,
Oid  objoid,
TimestampTz  ts 
)

Definition at line 958 of file pgstat_shmem.c.

959 {
960  PgStat_EntryRef *entry_ref;
961 
962  Assert(!pgstat_get_kind_info(kind)->fixed_amount);
963 
964  entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
965  if (!entry_ref || entry_ref->shared_entry->dropped)
966  return;
967 
968  (void) pgstat_lock_entry(entry_ref, false);
969  shared_stat_reset_contents(kind, entry_ref->shared_stats, ts);
970  pgstat_unlock_entry(entry_ref);
971 }
static void shared_stat_reset_contents(PgStat_Kind kind, PgStatShared_Common *header, TimestampTz ts)
Definition: pgstat_shmem.c:942

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

Referenced by pgstat_create_subscription(), and pgstat_reset().

◆ pgstat_reset_matching_entries()

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

Definition at line 978 of file pgstat_shmem.c.

980 {
981  dshash_seq_status hstat;
983 
984  /* dshash entry is not modified, take shared lock */
985  dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);
986  while ((p = dshash_seq_next(&hstat)) != NULL)
987  {
988  PgStatShared_Common *header;
989 
990  if (p->dropped)
991  continue;
992 
993  if (!do_reset(p, match_data))
994  continue;
995 
996  header = dsa_get_address(pgStatLocal.dsa, p->body);
997 
998  LWLockAcquire(&header->lock, LW_EXCLUSIVE);
999 
1000  shared_stat_reset_contents(p->key.kind, header, ts);
1001 
1002  LWLockRelease(&header->lock);
1003  }
1004  dshash_seq_term(&hstat);
1005 }

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

Referenced by pgstat_reset_counters(), and pgstat_reset_entries_of_kind().

◆ pgstat_slru_flush()

bool pgstat_slru_flush ( bool  nowait)

Definition at line 156 of file pgstat_slru.c.

157 {
158  PgStatShared_SLRU *stats_shmem = &pgStatLocal.shmem->slru;
159  int i;
160 
161  if (!have_slrustats)
162  return false;
163 
164  if (!nowait)
165  LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
166  else if (!LWLockConditionalAcquire(&stats_shmem->lock, LW_EXCLUSIVE))
167  return true;
168 
169  for (i = 0; i < SLRU_NUM_ELEMENTS; i++)
170  {
171  PgStat_SLRUStats *sharedent = &stats_shmem->stats[i];
172  PgStat_SLRUStats *pendingent = &pending_SLRUStats[i];
173 
174 #define SLRU_ACC(fld) sharedent->fld += pendingent->fld
175  SLRU_ACC(blocks_zeroed);
176  SLRU_ACC(blocks_hit);
177  SLRU_ACC(blocks_read);
179  SLRU_ACC(blocks_exists);
180  SLRU_ACC(flush);
181  SLRU_ACC(truncate);
182 #undef SLRU_ACC
183  }
184 
185  /* done, clear the pending entry */
187 
188  LWLockRelease(&stats_shmem->lock);
189 
190  have_slrustats = false;
191 
192  return false;
193 }
static int64 blocks_written
Definition: pg_checksums.c:39
#define SLRU_NUM_ELEMENTS
bool have_slrustats
Definition: pgstat_slru.c:35
static PgStat_SLRUStats pending_SLRUStats[SLRU_NUM_ELEMENTS]
Definition: pgstat_slru.c:34
#define SLRU_ACC(fld)
PgStat_SLRUStats stats[SLRU_NUM_ELEMENTS]
PgStatShared_SLRU slru

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

Referenced by pgstat_report_stat().

◆ pgstat_slru_init_shmem_cb()

void pgstat_slru_init_shmem_cb ( void *  stats)

Definition at line 196 of file pgstat_slru.c.

197 {
198  PgStatShared_SLRU *stats_shmem = (PgStatShared_SLRU *) stats;
199 
201 }

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

◆ pgstat_slru_reset_all_cb()

void pgstat_slru_reset_all_cb ( TimestampTz  ts)

Definition at line 204 of file pgstat_slru.c.

205 {
206  for (int i = 0; i < SLRU_NUM_ELEMENTS; i++)
208 }
static void pgstat_reset_slru_counter_internal(int index, TimestampTz ts)
Definition: pgstat_slru.c:246

References i, pgstat_reset_slru_counter_internal(), and SLRU_NUM_ELEMENTS.

◆ pgstat_slru_snapshot_cb()

void pgstat_slru_snapshot_cb ( void  )

Definition at line 211 of file pgstat_slru.c.

212 {
213  PgStatShared_SLRU *stats_shmem = &pgStatLocal.shmem->slru;
214 
215  LWLockAcquire(&stats_shmem->lock, LW_SHARED);
216 
217  memcpy(pgStatLocal.snapshot.slru, &stats_shmem->stats,
218  sizeof(stats_shmem->stats));
219 
220  LWLockRelease(&stats_shmem->lock);
221 }
PgStat_SLRUStats slru[SLRU_NUM_ELEMENTS]

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

◆ pgstat_snapshot_fixed()

◆ pgstat_subscription_flush_cb()

bool pgstat_subscription_flush_cb ( PgStat_EntryRef entry_ref,
bool  nowait 
)

Definition at line 88 of file pgstat_subscription.c.

89 {
90  PgStat_BackendSubEntry *localent;
91  PgStatShared_Subscription *shsubent;
92 
93  localent = (PgStat_BackendSubEntry *) entry_ref->pending;
94  shsubent = (PgStatShared_Subscription *) entry_ref->shared_stats;
95 
96  /* localent always has non-zero content */
97 
98  if (!pgstat_lock_entry(entry_ref, nowait))
99  return false;
100 
101 #define SUB_ACC(fld) shsubent->stats.fld += localent->fld
102  SUB_ACC(apply_error_count);
103  SUB_ACC(sync_error_count);
104 #undef SUB_ACC
105 
106  pgstat_unlock_entry(entry_ref);
107  return true;
108 }
#define SUB_ACC(fld)

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

◆ pgstat_subscription_reset_timestamp_cb()

void pgstat_subscription_reset_timestamp_cb ( PgStatShared_Common header,
TimestampTz  ts 
)

Definition at line 111 of file pgstat_subscription.c.

112 {
113  ((PgStatShared_Subscription *) header)->stats.stat_reset_timestamp = ts;
114 }

◆ pgstat_unlock_entry()

◆ pgstat_update_dbstats()

void pgstat_update_dbstats ( TimestampTz  ts)

Definition at line 270 of file pgstat_database.c.

271 {
272  PgStat_StatDBEntry *dbentry;
273 
274  /*
275  * If not connected to a database yet, don't attribute time to "shared
276  * state" (InvalidOid is used to track stats for shared relations, etc.).
277  */
278  if (!OidIsValid(MyDatabaseId))
279  return;
280 
282 
283  /*
284  * Accumulate xact commit/rollback and I/O timings to stats entry of the
285  * current database.
286  */
287  dbentry->xact_commit += pgStatXactCommit;
288  dbentry->xact_rollback += pgStatXactRollback;
291 
293  {
294  long secs;
295  int usecs;
296 
297  /*
298  * pgLastSessionReportTime is initialized to MyStartTimestamp by
299  * pgstat_report_connect().
300  */
301  TimestampDifference(pgLastSessionReportTime, ts, &secs, &usecs);
303  dbentry->session_time += (PgStat_Counter) secs * 1000000 + usecs;
304  dbentry->active_time += pgStatActiveTime;
306  }
307 
308  pgStatXactCommit = 0;
309  pgStatXactRollback = 0;
312  pgStatActiveTime = 0;
314 }
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1731
int64 PgStat_Counter
Definition: pgstat.h:89
PgStat_Counter pgStatActiveTime
PgStat_Counter pgStatBlockReadTime
PgStat_Counter pgStatBlockWriteTime
static PgStat_Counter pgLastSessionReportTime
PgStat_Counter pgStatTransactionIdleTime
PgStat_Counter blk_write_time
Definition: pgstat.h:347
PgStat_Counter xact_rollback
Definition: pgstat.h:326
PgStat_Counter blk_read_time
Definition: pgstat.h:346
PgStat_Counter xact_commit
Definition: pgstat.h:325
PgStat_Counter session_time
Definition: pgstat.h:349
PgStat_Counter active_time
Definition: pgstat.h:350
PgStat_Counter idle_in_transaction_time
Definition: pgstat.h:351

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

Referenced by pgstat_report_stat().

◆ pgstat_wal_init_shmem_cb()

void pgstat_wal_init_shmem_cb ( void *  stats)

Definition at line 167 of file pgstat_wal.c.

168 {
169  PgStatShared_Wal *stats_shmem = (PgStatShared_Wal *) stats;
170 
172 }

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

◆ pgstat_wal_reset_all_cb()

void pgstat_wal_reset_all_cb ( TimestampTz  ts)

Definition at line 175 of file pgstat_wal.c.

176 {
177  PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
178 
179  LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
180  memset(&stats_shmem->stats, 0, sizeof(stats_shmem->stats));
181  stats_shmem->stats.stat_reset_timestamp = ts;
182  LWLockRelease(&stats_shmem->lock);
183 }
PgStat_WalStats stats
TimestampTz stat_reset_timestamp
Definition: pgstat.h:441

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

◆ pgstat_wal_snapshot_cb()

void pgstat_wal_snapshot_cb ( void  )

Definition at line 186 of file pgstat_wal.c.

187 {
188  PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
189 
190  LWLockAcquire(&stats_shmem->lock, LW_SHARED);
191  memcpy(&pgStatLocal.snapshot.wal, &stats_shmem->stats,
192  sizeof(pgStatLocal.snapshot.wal));
193  LWLockRelease(&stats_shmem->lock);
194 }
PgStat_WalStats wal

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

◆ PostPrepare_PgStat_Relations()

void PostPrepare_PgStat_Relations ( PgStat_SubXactStatus xact_state)

Definition at line 714 of file pgstat_relation.c.

715 {
717 
718  for (trans = xact_state->first; trans != NULL; trans = trans->next)
719  {
720  PgStat_TableStatus *tabstat;
721 
722  tabstat = trans->parent;
723  tabstat->trans = NULL;
724  }
725 }

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

Referenced by PostPrepare_PgStat().

Variable Documentation

◆ have_iostats

PGDLLIMPORT bool have_iostats
extern

Definition at line 32 of file pgstat_io.c.

Referenced by pgstat_count_io_op_n(), pgstat_flush_io(), and pgstat_report_stat().

◆ have_slrustats

PGDLLIMPORT bool have_slrustats
extern

Definition at line 35 of file pgstat_slru.c.

Referenced by get_slru_entry(), pgstat_report_stat(), and pgstat_slru_flush().

◆ pgStatLocal

PGDLLIMPORT PgStat_LocalState pgStatLocal
extern

◆ slru_names

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

Definition at line 284 of file pgstat_internal.h.

Referenced by pgstat_get_slru_index(), and pgstat_get_slru_name().