PostgreSQL Source Code  git master
pgstat_relation.c File Reference
#include "postgres.h"
#include "access/twophase_rmgr.h"
#include "access/xact.h"
#include "catalog/partition.h"
#include "postmaster/autovacuum.h"
#include "utils/memutils.h"
#include "utils/pgstat_internal.h"
#include "utils/rel.h"
#include "utils/timestamp.h"
#include "catalog/catalog.h"
Include dependency graph for pgstat_relation.c:

Go to the source code of this file.

Data Structures

struct  TwoPhasePgStatRecord
 

Typedefs

typedef struct TwoPhasePgStatRecord TwoPhasePgStatRecord
 

Functions

static PgStat_TableStatuspgstat_prep_relation_pending (Oid rel_id, bool isshared)
 
static void add_tabstat_xact_level (PgStat_TableStatus *pgstat_info, int nest_level)
 
static void ensure_tabstat_xact_level (PgStat_TableStatus *pgstat_info)
 
static void save_truncdrop_counters (PgStat_TableXactStatus *trans, bool is_drop)
 
static void restore_truncdrop_counters (PgStat_TableXactStatus *trans)
 
void pgstat_copy_relation_stats (Relation dst, Relation src)
 
void pgstat_init_relation (Relation rel)
 
void pgstat_assoc_relation (Relation rel)
 
void pgstat_unlink_relation (Relation rel)
 
void pgstat_create_relation (Relation rel)
 
void pgstat_drop_relation (Relation rel)
 
void pgstat_report_vacuum (Oid tableoid, bool shared, PgStat_Counter livetuples, PgStat_Counter deadtuples)
 
void pgstat_report_analyze (Relation rel, PgStat_Counter livetuples, PgStat_Counter deadtuples, bool resetcounter)
 
void pgstat_count_heap_insert (Relation rel, PgStat_Counter n)
 
void pgstat_count_heap_update (Relation rel, bool hot)
 
void pgstat_count_heap_delete (Relation rel)
 
void pgstat_count_truncate (Relation rel)
 
void pgstat_update_heap_dead_tuples (Relation rel, int delta)
 
PgStat_StatTabEntrypgstat_fetch_stat_tabentry (Oid relid)
 
PgStat_StatTabEntrypgstat_fetch_stat_tabentry_ext (bool shared, Oid reloid)
 
PgStat_TableStatusfind_tabstat_entry (Oid rel_id)
 
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)
 
void pgstat_twophase_postcommit (TransactionId xid, uint16 info, void *recdata, uint32 len)
 
void pgstat_twophase_postabort (TransactionId xid, uint16 info, void *recdata, uint32 len)
 
bool pgstat_relation_flush_cb (PgStat_EntryRef *entry_ref, bool nowait)
 
void pgstat_relation_delete_pending_cb (PgStat_EntryRef *entry_ref)
 

Typedef Documentation

◆ TwoPhasePgStatRecord

Function Documentation

◆ add_tabstat_xact_level()

static void add_tabstat_xact_level ( PgStat_TableStatus pgstat_info,
int  nest_level 
)
static

Definition at line 867 of file pgstat_relation.c.

868 {
869  PgStat_SubXactStatus *xact_state;
871 
872  /*
873  * If this is the first rel to be modified at the current nest level, we
874  * first have to push a transaction stack entry.
875  */
876  xact_state = pgstat_get_xact_stack_level(nest_level);
877 
878  /* Now make a per-table stack entry */
881  sizeof(PgStat_TableXactStatus));
882  trans->nest_level = nest_level;
883  trans->upper = pgstat_info->trans;
884  trans->parent = pgstat_info;
885  trans->next = xact_state->first;
886  xact_state->first = trans;
887  pgstat_info->trans = trans;
888 }
MemoryContext TopTransactionContext
Definition: mcxt.c:146
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1048
PgStat_SubXactStatus * pgstat_get_xact_stack_level(int nest_level)
Definition: pgstat_xact.c:237
PgStat_TableXactStatus * first
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:208
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:402

References PgStat_SubXactStatus::first, MemoryContextAllocZero(), pgstat_get_xact_stack_level(), TopTransactionContext, PgStat_TableStatus::trans, and trans.

Referenced by ensure_tabstat_xact_level().

◆ AtEOSubXact_PgStat_Relations()

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

Definition at line 546 of file pgstat_relation.c.

547 {
549  PgStat_TableXactStatus *next_trans;
550 
551  for (trans = xact_state->first; trans != NULL; trans = next_trans)
552  {
553  PgStat_TableStatus *tabstat;
554 
555  next_trans = trans->next;
556  Assert(trans->nest_level == nestDepth);
557  tabstat = trans->parent;
558  Assert(tabstat->trans == trans);
559 
560  if (isCommit)
561  {
562  if (trans->upper && trans->upper->nest_level == nestDepth - 1)
563  {
564  if (trans->truncdropped)
565  {
566  /* propagate the truncate/drop status one level up */
567  save_truncdrop_counters(trans->upper, false);
568  /* replace upper xact stats with ours */
569  trans->upper->tuples_inserted = trans->tuples_inserted;
570  trans->upper->tuples_updated = trans->tuples_updated;
571  trans->upper->tuples_deleted = trans->tuples_deleted;
572  }
573  else
574  {
575  trans->upper->tuples_inserted += trans->tuples_inserted;
576  trans->upper->tuples_updated += trans->tuples_updated;
577  trans->upper->tuples_deleted += trans->tuples_deleted;
578  }
579  tabstat->trans = trans->upper;
580  pfree(trans);
581  }
582  else
583  {
584  /*
585  * When there isn't an immediate parent state, we can just
586  * reuse the record instead of going through a palloc/pfree
587  * pushup (this works since it's all in TopTransactionContext
588  * anyway). We have to re-link it into the parent level,
589  * though, and that might mean pushing a new entry into the
590  * pgStatXactStack.
591  */
592  PgStat_SubXactStatus *upper_xact_state;
593 
594  upper_xact_state = pgstat_get_xact_stack_level(nestDepth - 1);
595  trans->next = upper_xact_state->first;
596  upper_xact_state->first = trans;
597  trans->nest_level = nestDepth - 1;
598  }
599  }
600  else
601  {
602  /*
603  * On abort, update top-level tabstat counts, then forget the
604  * subtransaction
605  */
606 
607  /* first restore values obliterated by truncate/drop */
609  /* count attempted actions regardless of commit/abort */
610  tabstat->t_counts.t_tuples_inserted += trans->tuples_inserted;
611  tabstat->t_counts.t_tuples_updated += trans->tuples_updated;
612  tabstat->t_counts.t_tuples_deleted += trans->tuples_deleted;
613  /* inserted tuples are dead, deleted tuples are unaffected */
614  tabstat->t_counts.t_delta_dead_tuples +=
615  trans->tuples_inserted + trans->tuples_updated;
616  tabstat->trans = trans->upper;
617  pfree(trans);
618  }
619  }
620 }
Assert(fmt[strlen(fmt) - 1] !='\n')
void pfree(void *pointer)
Definition: mcxt.c:1436
static void save_truncdrop_counters(PgStat_TableXactStatus *trans, bool is_drop)
static void restore_truncdrop_counters(PgStat_TableXactStatus *trans)
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:182
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:175
PgStat_Counter t_tuples_updated
Definition: pgstat.h:176
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:177
PgStat_TableCounts t_counts
Definition: pgstat.h:209

References Assert(), PgStat_SubXactStatus::first, pfree(), pgstat_get_xact_stack_level(), restore_truncdrop_counters(), save_truncdrop_counters(), PgStat_TableStatus::t_counts, PgStat_TableCounts::t_delta_dead_tuples, PgStat_TableCounts::t_tuples_deleted, PgStat_TableCounts::t_tuples_inserted, PgStat_TableCounts::t_tuples_updated, PgStat_TableStatus::trans, and trans.

Referenced by AtEOSubXact_PgStat().

◆ AtEOXact_PgStat_Relations()

void AtEOXact_PgStat_Relations ( PgStat_SubXactStatus xact_state,
bool  isCommit 
)

Definition at line 488 of file pgstat_relation.c.

489 {
491 
492  for (trans = xact_state->first; trans != NULL; trans = trans->next)
493  {
494  PgStat_TableStatus *tabstat;
495 
496  Assert(trans->nest_level == 1);
497  Assert(trans->upper == NULL);
498  tabstat = trans->parent;
499  Assert(tabstat->trans == trans);
500  /* restore pre-truncate/drop stats (if any) in case of aborted xact */
501  if (!isCommit)
503  /* count attempted actions regardless of commit/abort */
504  tabstat->t_counts.t_tuples_inserted += trans->tuples_inserted;
505  tabstat->t_counts.t_tuples_updated += trans->tuples_updated;
506  tabstat->t_counts.t_tuples_deleted += trans->tuples_deleted;
507  if (isCommit)
508  {
509  tabstat->t_counts.t_truncdropped = trans->truncdropped;
510  if (trans->truncdropped)
511  {
512  /* forget live/dead stats seen by backend thus far */
513  tabstat->t_counts.t_delta_live_tuples = 0;
514  tabstat->t_counts.t_delta_dead_tuples = 0;
515  }
516  /* insert adds a live tuple, delete removes one */
517  tabstat->t_counts.t_delta_live_tuples +=
518  trans->tuples_inserted - trans->tuples_deleted;
519  /* update and delete each create a dead tuple */
520  tabstat->t_counts.t_delta_dead_tuples +=
521  trans->tuples_updated + trans->tuples_deleted;
522  /* insert, update, delete each count as one change event */
523  tabstat->t_counts.t_changed_tuples +=
524  trans->tuples_inserted + trans->tuples_updated +
525  trans->tuples_deleted;
526  }
527  else
528  {
529  /* inserted tuples are dead, deleted tuples are unaffected */
530  tabstat->t_counts.t_delta_dead_tuples +=
531  trans->tuples_inserted + trans->tuples_updated;
532  /* an aborted xact generates no changed_tuple events */
533  }
534  tabstat->trans = NULL;
535  }
536 }
PgStat_Counter t_delta_live_tuples
Definition: pgstat.h:181
bool t_truncdropped
Definition: pgstat.h:179
PgStat_Counter t_changed_tuples
Definition: pgstat.h:183

References Assert(), PgStat_SubXactStatus::first, restore_truncdrop_counters(), PgStat_TableCounts::t_changed_tuples, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_delta_dead_tuples, PgStat_TableCounts::t_delta_live_tuples, PgStat_TableCounts::t_truncdropped, PgStat_TableCounts::t_tuples_deleted, PgStat_TableCounts::t_tuples_inserted, PgStat_TableCounts::t_tuples_updated, PgStat_TableStatus::trans, and trans.

Referenced by AtEOXact_PgStat().

◆ AtPrepare_PgStat_Relations()

void AtPrepare_PgStat_Relations ( PgStat_SubXactStatus xact_state)

Definition at line 627 of file pgstat_relation.c.

628 {
630 
631  for (trans = xact_state->first; trans != NULL; trans = trans->next)
632  {
634  TwoPhasePgStatRecord record;
635 
636  Assert(trans->nest_level == 1);
637  Assert(trans->upper == NULL);
638  tabstat = trans->parent;
639  Assert(tabstat->trans == trans);
640 
641  record.tuples_inserted = trans->tuples_inserted;
642  record.tuples_updated = trans->tuples_updated;
643  record.tuples_deleted = trans->tuples_deleted;
644  record.inserted_pre_truncdrop = trans->inserted_pre_truncdrop;
645  record.updated_pre_truncdrop = trans->updated_pre_truncdrop;
646  record.deleted_pre_truncdrop = trans->deleted_pre_truncdrop;
647  record.t_id = tabstat->t_id;
648  record.t_shared = tabstat->t_shared;
649  record.t_truncdropped = trans->truncdropped;
650 
652  &record, sizeof(TwoPhasePgStatRecord));
653  }
654 }
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:166
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:1257
#define TWOPHASE_RM_PGSTAT_ID
Definition: twophase_rmgr.h:26

References Assert(), TwoPhasePgStatRecord::deleted_pre_truncdrop, PgStat_SubXactStatus::first, TwoPhasePgStatRecord::inserted_pre_truncdrop, PG_USED_FOR_ASSERTS_ONLY, RegisterTwoPhaseRecord(), TwoPhasePgStatRecord::t_id, TwoPhasePgStatRecord::t_shared, TwoPhasePgStatRecord::t_truncdropped, trans, TwoPhasePgStatRecord::tuples_deleted, TwoPhasePgStatRecord::tuples_inserted, TwoPhasePgStatRecord::tuples_updated, TWOPHASE_RM_PGSTAT_ID, and TwoPhasePgStatRecord::updated_pre_truncdrop.

Referenced by AtPrepare_PgStat().

◆ ensure_tabstat_xact_level()

static void ensure_tabstat_xact_level ( PgStat_TableStatus pgstat_info)
static

Definition at line 894 of file pgstat_relation.c.

895 {
896  int nest_level = GetCurrentTransactionNestLevel();
897 
898  if (pgstat_info->trans == NULL ||
899  pgstat_info->trans->nest_level != nest_level)
900  add_tabstat_xact_level(pgstat_info, nest_level);
901 }
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:914

References add_tabstat_xact_level(), GetCurrentTransactionNestLevel(), PgStat_TableXactStatus::nest_level, and PgStat_TableStatus::trans.

Referenced by pgstat_count_heap_delete(), pgstat_count_heap_insert(), pgstat_count_heap_update(), and pgstat_count_truncate().

◆ find_tabstat_entry()

PgStat_TableStatus* find_tabstat_entry ( Oid  rel_id)

Definition at line 466 of file pgstat_relation.c.

467 {
468  PgStat_EntryRef *entry_ref;
469 
471  if (!entry_ref)
473 
474  if (entry_ref)
475  return entry_ref->pending;
476  return NULL;
477 }
Oid MyDatabaseId
Definition: globals.c:89
PgStat_EntryRef * pgstat_fetch_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:1101
@ PGSTAT_KIND_RELATION
Definition: pgstat.h:42
#define InvalidOid
Definition: postgres_ext.h:36

References InvalidOid, MyDatabaseId, PgStat_EntryRef::pending, pgstat_fetch_pending_entry(), and PGSTAT_KIND_RELATION.

Referenced by pg_stat_get_xact_blocks_fetched(), pg_stat_get_xact_blocks_hit(), pg_stat_get_xact_numscans(), pg_stat_get_xact_tuples_deleted(), pg_stat_get_xact_tuples_fetched(), pg_stat_get_xact_tuples_hot_updated(), pg_stat_get_xact_tuples_inserted(), pg_stat_get_xact_tuples_returned(), and pg_stat_get_xact_tuples_updated().

◆ pgstat_assoc_relation()

void pgstat_assoc_relation ( Relation  rel)

Definition at line 133 of file pgstat_relation.c.

134 {
135  Assert(rel->pgstat_enabled);
136  Assert(rel->pgstat_info == NULL);
137 
138  /* Else find or make the PgStat_TableStatus entry, and update link */
140  rel->rd_rel->relisshared);
141 
142  /* don't allow link a stats to multiple relcache entries */
143  Assert(rel->pgstat_info->relation == NULL);
144 
145  /* mark this relation as the owner */
146  rel->pgstat_info->relation = rel;
147 }
static PgStat_TableStatus * pgstat_prep_relation_pending(Oid rel_id, bool isshared)
#define RelationGetRelid(relation)
Definition: rel.h:501
Relation relation
Definition: pgstat.h:210
bool pgstat_enabled
Definition: rel.h:249
Form_pg_class rd_rel
Definition: rel.h:110
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:251

References Assert(), RelationData::pgstat_enabled, RelationData::pgstat_info, pgstat_prep_relation_pending(), RelationData::rd_rel, PgStat_TableStatus::relation, and RelationGetRelid.

◆ pgstat_copy_relation_stats()

void pgstat_copy_relation_stats ( Relation  dst,
Relation  src 
)

Definition at line 59 of file pgstat_relation.c.

60 {
61  PgStat_StatTabEntry *srcstats;
62  PgStatShared_Relation *dstshstats;
63  PgStat_EntryRef *dst_ref;
64 
65  srcstats = pgstat_fetch_stat_tabentry_ext(src->rd_rel->relisshared,
66  RelationGetRelid(src));
67  if (!srcstats)
68  return;
69 
71  dst->rd_rel->relisshared ? InvalidOid : MyDatabaseId,
72  RelationGetRelid(dst),
73  false);
74 
75  dstshstats = (PgStatShared_Relation *) dst_ref->shared_stats;
76  dstshstats->stats = *srcstats;
77 
78  pgstat_unlock_entry(dst_ref);
79 }
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry_ext(bool shared, Oid reloid)
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:599
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, Oid objoid, bool nowait)
Definition: pgstat_shmem.c:608
PgStat_StatTabEntry stats
PgStatShared_Common * shared_stats

References InvalidOid, MyDatabaseId, pgstat_fetch_stat_tabentry_ext(), pgstat_get_entry_ref_locked(), PGSTAT_KIND_RELATION, pgstat_unlock_entry(), RelationData::rd_rel, RelationGetRelid, PgStat_EntryRef::shared_stats, and PgStatShared_Relation::stats.

Referenced by index_concurrently_swap().

◆ pgstat_count_heap_delete()

void pgstat_count_heap_delete ( Relation  rel)

Definition at line 384 of file pgstat_relation.c.

385 {
387  {
388  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
389 
390  ensure_tabstat_xact_level(pgstat_info);
391  pgstat_info->trans->tuples_deleted++;
392  }
393 }
#define pgstat_should_count_relation(rel)
Definition: pgstat.h:520
static void ensure_tabstat_xact_level(PgStat_TableStatus *pgstat_info)
PgStat_Counter tuples_deleted
Definition: pgstat.h:221

References ensure_tabstat_xact_level(), RelationData::pgstat_info, pgstat_should_count_relation, PgStat_TableStatus::trans, and PgStat_TableXactStatus::tuples_deleted.

Referenced by heap_abort_speculative(), and heap_delete().

◆ pgstat_count_heap_insert()

void pgstat_count_heap_insert ( Relation  rel,
PgStat_Counter  n 
)

Definition at line 350 of file pgstat_relation.c.

351 {
353  {
354  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
355 
356  ensure_tabstat_xact_level(pgstat_info);
357  pgstat_info->trans->tuples_inserted += n;
358  }
359 }
PgStat_Counter tuples_inserted
Definition: pgstat.h:219

References ensure_tabstat_xact_level(), RelationData::pgstat_info, pgstat_should_count_relation, PgStat_TableStatus::trans, and PgStat_TableXactStatus::tuples_inserted.

Referenced by ExecRefreshMatView(), heap_insert(), and heap_multi_insert().

◆ pgstat_count_heap_update()

void pgstat_count_heap_update ( Relation  rel,
bool  hot 
)

Definition at line 365 of file pgstat_relation.c.

366 {
368  {
369  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
370 
371  ensure_tabstat_xact_level(pgstat_info);
372  pgstat_info->trans->tuples_updated++;
373 
374  /* t_tuples_hot_updated is nontransactional, so just advance it */
375  if (hot)
376  pgstat_info->t_counts.t_tuples_hot_updated++;
377  }
378 }
PgStat_Counter t_tuples_hot_updated
Definition: pgstat.h:178
PgStat_Counter tuples_updated
Definition: pgstat.h:220

References ensure_tabstat_xact_level(), RelationData::pgstat_info, pgstat_should_count_relation, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_tuples_hot_updated, PgStat_TableStatus::trans, and PgStat_TableXactStatus::tuples_updated.

Referenced by heap_update().

◆ pgstat_count_truncate()

void pgstat_count_truncate ( Relation  rel)

◆ pgstat_create_relation()

void pgstat_create_relation ( Relation  rel)

Definition at line 170 of file pgstat_relation.c.

171 {
173  rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId,
174  RelationGetRelid(rel));
175 }
void pgstat_create_transactional(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat_xact.c:358

References InvalidOid, MyDatabaseId, pgstat_create_transactional(), PGSTAT_KIND_RELATION, RelationData::rd_rel, and RelationGetRelid.

Referenced by heap_create().

◆ pgstat_drop_relation()

void pgstat_drop_relation ( Relation  rel)

Definition at line 181 of file pgstat_relation.c.

182 {
183  int nest_level = GetCurrentTransactionNestLevel();
184  PgStat_TableStatus *pgstat_info;
185 
187  rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId,
188  RelationGetRelid(rel));
189 
191  return;
192 
193  /*
194  * Transactionally set counters to 0. That ensures that accesses to
195  * pg_stat_xact_all_tables inside the transaction show 0.
196  */
197  pgstat_info = rel->pgstat_info;
198  if (pgstat_info->trans &&
199  pgstat_info->trans->nest_level == nest_level)
200  {
201  save_truncdrop_counters(pgstat_info->trans, true);
202  pgstat_info->trans->tuples_inserted = 0;
203  pgstat_info->trans->tuples_updated = 0;
204  pgstat_info->trans->tuples_deleted = 0;
205  }
206 }
void pgstat_drop_transactional(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat_xact.c:380

References GetCurrentTransactionNestLevel(), InvalidOid, MyDatabaseId, PgStat_TableXactStatus::nest_level, pgstat_drop_transactional(), RelationData::pgstat_info, PGSTAT_KIND_RELATION, pgstat_should_count_relation, RelationData::rd_rel, RelationGetRelid, save_truncdrop_counters(), PgStat_TableStatus::trans, PgStat_TableXactStatus::tuples_deleted, PgStat_TableXactStatus::tuples_inserted, and PgStat_TableXactStatus::tuples_updated.

Referenced by heap_drop_with_catalog(), and index_drop().

◆ pgstat_fetch_stat_tabentry()

PgStat_StatTabEntry* pgstat_fetch_stat_tabentry ( Oid  relid)

Definition at line 439 of file pgstat_relation.c.

440 {
441  return pgstat_fetch_stat_tabentry_ext(IsSharedRelation(relid), relid);
442 }
bool IsSharedRelation(Oid relationId)
Definition: catalog.c:245

References IsSharedRelation(), and pgstat_fetch_stat_tabentry_ext().

◆ pgstat_fetch_stat_tabentry_ext()

PgStat_StatTabEntry* pgstat_fetch_stat_tabentry_ext ( bool  shared,
Oid  reloid 
)

Definition at line 449 of file pgstat_relation.c.

450 {
451  Oid dboid = (shared ? InvalidOid : MyDatabaseId);
452 
453  return (PgStat_StatTabEntry *)
455 }
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:779
unsigned int Oid
Definition: postgres_ext.h:31

References InvalidOid, MyDatabaseId, pgstat_fetch_entry(), and PGSTAT_KIND_RELATION.

Referenced by do_autovacuum(), pgstat_copy_relation_stats(), pgstat_fetch_stat_tabentry(), and recheck_relation_needs_vacanalyze().

◆ pgstat_init_relation()

void pgstat_init_relation ( Relation  rel)

Definition at line 93 of file pgstat_relation.c.

94 {
95  char relkind = rel->rd_rel->relkind;
96 
97  /*
98  * We only count stats for relations with storage and partitioned tables
99  */
100  if (!RELKIND_HAS_STORAGE(relkind) && relkind != RELKIND_PARTITIONED_TABLE)
101  {
102  rel->pgstat_enabled = false;
103  rel->pgstat_info = NULL;
104  return;
105  }
106 
107  if (!pgstat_track_counts)
108  {
109  if (rel->pgstat_info)
111 
112  /* We're not counting at all */
113  rel->pgstat_enabled = false;
114  rel->pgstat_info = NULL;
115  return;
116  }
117 
118  rel->pgstat_enabled = true;
119 }
bool pgstat_track_counts
Definition: pgstat.c:186
void pgstat_unlink_relation(Relation rel)

References RelationData::pgstat_enabled, RelationData::pgstat_info, pgstat_track_counts, pgstat_unlink_relation(), and RelationData::rd_rel.

Referenced by relation_open(), and try_relation_open().

◆ pgstat_prep_relation_pending()

static PgStat_TableStatus * pgstat_prep_relation_pending ( Oid  rel_id,
bool  isshared 
)
static

Definition at line 848 of file pgstat_relation.c.

849 {
850  PgStat_EntryRef *entry_ref;
851  PgStat_TableStatus *pending;
852 
854  isshared ? InvalidOid : MyDatabaseId,
855  rel_id, NULL);
856  pending = entry_ref->pending;
857  pending->t_id = rel_id;
858  pending->t_shared = isshared;
859 
860  return pending;
861 }
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid, bool *created_entry)
Definition: pgstat.c:1063

References InvalidOid, MyDatabaseId, PgStat_EntryRef::pending, PGSTAT_KIND_RELATION, pgstat_prep_pending_entry(), PgStat_TableStatus::t_id, and PgStat_TableStatus::t_shared.

Referenced by pgstat_assoc_relation(), pgstat_twophase_postabort(), and pgstat_twophase_postcommit().

◆ pgstat_relation_delete_pending_cb()

void pgstat_relation_delete_pending_cb ( PgStat_EntryRef entry_ref)

Definition at line 835 of file pgstat_relation.c.

836 {
837  PgStat_TableStatus *pending = (PgStat_TableStatus *) entry_ref->pending;
838 
839  if (pending->relation)
841 }
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77

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 753 of file pgstat_relation.c.

754 {
755  static const PgStat_TableCounts all_zeroes;
756  Oid dboid;
757  PgStat_TableStatus *lstats; /* pending stats entry */
758  PgStatShared_Relation *shtabstats;
759  PgStat_StatTabEntry *tabentry; /* table entry of shared stats */
760  PgStat_StatDBEntry *dbentry; /* pending database entry */
761 
762  dboid = entry_ref->shared_entry->key.dboid;
763  lstats = (PgStat_TableStatus *) entry_ref->pending;
764  shtabstats = (PgStatShared_Relation *) entry_ref->shared_stats;
765 
766  /*
767  * Ignore entries that didn't accumulate any actual counts, such as
768  * indexes that were opened by the planner but not used.
769  */
770  if (memcmp(&lstats->t_counts, &all_zeroes,
771  sizeof(PgStat_TableCounts)) == 0)
772  {
773  return true;
774  }
775 
776  if (!pgstat_lock_entry(entry_ref, nowait))
777  return false;
778 
779  /* add the values to the shared entry. */
780  tabentry = &shtabstats->stats;
781 
782  tabentry->numscans += lstats->t_counts.t_numscans;
783  if (lstats->t_counts.t_numscans)
784  {
786 
787  if (t > tabentry->lastscan)
788  tabentry->lastscan = t;
789  }
790  tabentry->tuples_returned += lstats->t_counts.t_tuples_returned;
791  tabentry->tuples_fetched += lstats->t_counts.t_tuples_fetched;
792  tabentry->tuples_inserted += lstats->t_counts.t_tuples_inserted;
793  tabentry->tuples_updated += lstats->t_counts.t_tuples_updated;
794  tabentry->tuples_deleted += lstats->t_counts.t_tuples_deleted;
795  tabentry->tuples_hot_updated += lstats->t_counts.t_tuples_hot_updated;
796 
797  /*
798  * If table was truncated/dropped, first reset the live/dead counters.
799  */
800  if (lstats->t_counts.t_truncdropped)
801  {
802  tabentry->live_tuples = 0;
803  tabentry->dead_tuples = 0;
804  tabentry->ins_since_vacuum = 0;
805  }
806 
807  tabentry->live_tuples += lstats->t_counts.t_delta_live_tuples;
808  tabentry->dead_tuples += lstats->t_counts.t_delta_dead_tuples;
809  tabentry->mod_since_analyze += lstats->t_counts.t_changed_tuples;
810  tabentry->ins_since_vacuum += lstats->t_counts.t_tuples_inserted;
811  tabentry->blocks_fetched += lstats->t_counts.t_blocks_fetched;
812  tabentry->blocks_hit += lstats->t_counts.t_blocks_hit;
813 
814  /* Clamp live_tuples in case of negative delta_live_tuples */
815  tabentry->live_tuples = Max(tabentry->live_tuples, 0);
816  /* Likewise for dead_tuples */
817  tabentry->dead_tuples = Max(tabentry->dead_tuples, 0);
818 
819  pgstat_unlock_entry(entry_ref);
820 
821  /* The entry was successfully flushed, add the same to database stats */
822  dbentry = pgstat_prep_database_pending(dboid);
823  dbentry->tuples_returned += lstats->t_counts.t_tuples_returned;
824  dbentry->tuples_fetched += lstats->t_counts.t_tuples_fetched;
825  dbentry->tuples_inserted += lstats->t_counts.t_tuples_inserted;
826  dbentry->tuples_updated += lstats->t_counts.t_tuples_updated;
827  dbentry->tuples_deleted += lstats->t_counts.t_tuples_deleted;
828  dbentry->blocks_fetched += lstats->t_counts.t_blocks_fetched;
829  dbentry->blocks_hit += lstats->t_counts.t_blocks_hit;
830 
831  return true;
832 }
#define Max(x, y)
Definition: c.h:982
int64 TimestampTz
Definition: timestamp.h:39
PgStat_StatDBEntry * pgstat_prep_database_pending(Oid dboid)
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:571
PgStatShared_HashEntry * shared_entry
PgStat_Counter tuples_updated
Definition: pgstat.h:288
PgStat_Counter tuples_inserted
Definition: pgstat.h:287
PgStat_Counter tuples_returned
Definition: pgstat.h:285
PgStat_Counter blocks_hit
Definition: pgstat.h:284
PgStat_Counter blocks_fetched
Definition: pgstat.h:283
PgStat_Counter tuples_deleted
Definition: pgstat.h:289
PgStat_Counter tuples_fetched
Definition: pgstat.h:286
PgStat_Counter tuples_fetched
Definition: pgstat.h:360
PgStat_Counter ins_since_vacuum
Definition: pgstat.h:370
PgStat_Counter blocks_hit
Definition: pgstat.h:373
PgStat_Counter mod_since_analyze
Definition: pgstat.h:369
PgStat_Counter tuples_deleted
Definition: pgstat.h:364
PgStat_Counter tuples_hot_updated
Definition: pgstat.h:365
PgStat_Counter tuples_updated
Definition: pgstat.h:363
PgStat_Counter live_tuples
Definition: pgstat.h:367
PgStat_Counter numscans
Definition: pgstat.h:356
PgStat_Counter dead_tuples
Definition: pgstat.h:368
PgStat_Counter blocks_fetched
Definition: pgstat.h:372
PgStat_Counter tuples_returned
Definition: pgstat.h:359
TimestampTz lastscan
Definition: pgstat.h:357
PgStat_Counter tuples_inserted
Definition: pgstat.h:362
PgStat_Counter t_tuples_fetched
Definition: pgstat.h:173
PgStat_Counter t_numscans
Definition: pgstat.h:170
PgStat_Counter t_blocks_hit
Definition: pgstat.h:186
PgStat_Counter t_blocks_fetched
Definition: pgstat.h:185
PgStat_Counter t_tuples_returned
Definition: pgstat.h:172
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition: xact.c:876

References PgStat_StatDBEntry::blocks_fetched, PgStat_StatTabEntry::blocks_fetched, PgStat_StatDBEntry::blocks_hit, PgStat_StatTabEntry::blocks_hit, PgStat_HashKey::dboid, PgStat_StatTabEntry::dead_tuples, GetCurrentTransactionStopTimestamp(), PgStat_StatTabEntry::ins_since_vacuum, PgStatShared_HashEntry::key, PgStat_StatTabEntry::lastscan, PgStat_StatTabEntry::live_tuples, Max, PgStat_StatTabEntry::mod_since_analyze, 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::t_blocks_fetched, PgStat_TableCounts::t_blocks_hit, PgStat_TableCounts::t_changed_tuples, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_delta_dead_tuples, PgStat_TableCounts::t_delta_live_tuples, PgStat_TableCounts::t_numscans, PgStat_TableCounts::t_truncdropped, PgStat_TableCounts::t_tuples_deleted, PgStat_TableCounts::t_tuples_fetched, PgStat_TableCounts::t_tuples_hot_updated, PgStat_TableCounts::t_tuples_inserted, PgStat_TableCounts::t_tuples_returned, PgStat_TableCounts::t_tuples_updated, PgStat_StatDBEntry::tuples_deleted, PgStat_StatTabEntry::tuples_deleted, PgStat_StatDBEntry::tuples_fetched, PgStat_StatTabEntry::tuples_fetched, PgStat_StatTabEntry::tuples_hot_updated, PgStat_StatDBEntry::tuples_inserted, PgStat_StatTabEntry::tuples_inserted, PgStat_StatDBEntry::tuples_returned, PgStat_StatTabEntry::tuples_returned, PgStat_StatDBEntry::tuples_updated, and PgStat_StatTabEntry::tuples_updated.

◆ pgstat_report_analyze()

void pgstat_report_analyze ( Relation  rel,
PgStat_Counter  livetuples,
PgStat_Counter  deadtuples,
bool  resetcounter 
)

Definition at line 270 of file pgstat_relation.c.

273 {
274  PgStat_EntryRef *entry_ref;
275  PgStatShared_Relation *shtabentry;
276  PgStat_StatTabEntry *tabentry;
277  Oid dboid = (rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId);
278 
279  if (!pgstat_track_counts)
280  return;
281 
282  /*
283  * Unlike VACUUM, ANALYZE might be running inside a transaction that has
284  * already inserted and/or deleted rows in the target table. ANALYZE will
285  * have counted such rows as live or dead respectively. Because we will
286  * report our counts of such rows at transaction end, we should subtract
287  * off these counts from the update we're making now, else they'll be
288  * double-counted after commit. (This approach also ensures that the
289  * shared stats entry ends up with the right numbers if we abort instead
290  * of committing.)
291  *
292  * Waste no time on partitioned tables, though.
293  */
294  if (pgstat_should_count_relation(rel) &&
295  rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
296  {
298 
299  for (trans = rel->pgstat_info->trans; trans; trans = trans->upper)
300  {
301  livetuples -= trans->tuples_inserted - trans->tuples_deleted;
302  deadtuples -= trans->tuples_updated + trans->tuples_deleted;
303  }
304  /* count stuff inserted by already-aborted subxacts, too */
305  deadtuples -= rel->pgstat_info->t_counts.t_delta_dead_tuples;
306  /* Since ANALYZE's counts are estimates, we could have underflowed */
307  livetuples = Max(livetuples, 0);
308  deadtuples = Max(deadtuples, 0);
309  }
310 
311  /* block acquiring lock for the same reason as pgstat_report_autovac() */
313  RelationGetRelid(rel),
314  false);
315  /* can't get dropped while accessed */
316  Assert(entry_ref != NULL && entry_ref->shared_stats != NULL);
317 
318  shtabentry = (PgStatShared_Relation *) entry_ref->shared_stats;
319  tabentry = &shtabentry->stats;
320 
321  tabentry->live_tuples = livetuples;
322  tabentry->dead_tuples = deadtuples;
323 
324  /*
325  * If commanded, reset mod_since_analyze to zero. This forgets any
326  * changes that were committed while the ANALYZE was in progress, but we
327  * have no good way to estimate how many of those there were.
328  */
329  if (resetcounter)
330  tabentry->mod_since_analyze = 0;
331 
333  {
335  tabentry->autoanalyze_count++;
336  }
337  else
338  {
340  tabentry->analyze_count++;
341  }
342 
343  pgstat_unlock_entry(entry_ref);
344 }
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3319
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1585
PgStat_Counter analyze_count
Definition: pgstat.h:380
TimestampTz last_analyze_time
Definition: pgstat.h:379
PgStat_Counter autoanalyze_count
Definition: pgstat.h:382
TimestampTz last_autoanalyze_time
Definition: pgstat.h:381

References PgStat_StatTabEntry::analyze_count, Assert(), PgStat_StatTabEntry::autoanalyze_count, PgStat_StatTabEntry::dead_tuples, GetCurrentTimestamp(), if(), InvalidOid, IsAutoVacuumWorkerProcess(), PgStat_StatTabEntry::last_analyze_time, PgStat_StatTabEntry::last_autoanalyze_time, PgStat_StatTabEntry::live_tuples, Max, PgStat_StatTabEntry::mod_since_analyze, MyDatabaseId, pgstat_get_entry_ref_locked(), RelationData::pgstat_info, PGSTAT_KIND_RELATION, pgstat_should_count_relation, pgstat_track_counts, pgstat_unlock_entry(), RelationData::rd_rel, RelationGetRelid, PgStat_EntryRef::shared_stats, PgStatShared_Relation::stats, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_delta_dead_tuples, PgStat_TableStatus::trans, and trans.

Referenced by do_analyze_rel().

◆ pgstat_report_vacuum()

void pgstat_report_vacuum ( Oid  tableoid,
bool  shared,
PgStat_Counter  livetuples,
PgStat_Counter  deadtuples 
)

Definition at line 212 of file pgstat_relation.c.

214 {
215  PgStat_EntryRef *entry_ref;
216  PgStatShared_Relation *shtabentry;
217  PgStat_StatTabEntry *tabentry;
218  Oid dboid = (shared ? InvalidOid : MyDatabaseId);
219  TimestampTz ts;
220 
221  if (!pgstat_track_counts)
222  return;
223 
224  /* Store the data in the table's hash table entry. */
225  ts = GetCurrentTimestamp();
226 
227  /* block acquiring lock for the same reason as pgstat_report_autovac() */
229  dboid, tableoid, false);
230 
231  shtabentry = (PgStatShared_Relation *) entry_ref->shared_stats;
232  tabentry = &shtabentry->stats;
233 
234  tabentry->live_tuples = livetuples;
235  tabentry->dead_tuples = deadtuples;
236 
237  /*
238  * It is quite possible that a non-aggressive VACUUM ended up skipping
239  * various pages, however, we'll zero the insert counter here regardless.
240  * It's currently used only to track when we need to perform an "insert"
241  * autovacuum, which are mainly intended to freeze newly inserted tuples.
242  * Zeroing this may just mean we'll not try to vacuum the table again
243  * until enough tuples have been inserted to trigger another insert
244  * autovacuum. An anti-wraparound autovacuum will catch any persistent
245  * stragglers.
246  */
247  tabentry->ins_since_vacuum = 0;
248 
250  {
251  tabentry->last_autovacuum_time = ts;
252  tabentry->autovacuum_count++;
253  }
254  else
255  {
256  tabentry->last_vacuum_time = ts;
257  tabentry->vacuum_count++;
258  }
259 
260  pgstat_unlock_entry(entry_ref);
261 }
PgStat_Counter vacuum_count
Definition: pgstat.h:376
TimestampTz last_autovacuum_time
Definition: pgstat.h:377
PgStat_Counter autovacuum_count
Definition: pgstat.h:378
TimestampTz last_vacuum_time
Definition: pgstat.h:375

References PgStat_StatTabEntry::autovacuum_count, PgStat_StatTabEntry::dead_tuples, GetCurrentTimestamp(), if(), PgStat_StatTabEntry::ins_since_vacuum, InvalidOid, IsAutoVacuumWorkerProcess(), PgStat_StatTabEntry::last_autovacuum_time, PgStat_StatTabEntry::last_vacuum_time, PgStat_StatTabEntry::live_tuples, MyDatabaseId, pgstat_get_entry_ref_locked(), PGSTAT_KIND_RELATION, pgstat_track_counts, pgstat_unlock_entry(), PgStat_EntryRef::shared_stats, PgStatShared_Relation::stats, and PgStat_StatTabEntry::vacuum_count.

Referenced by heap_vacuum_rel().

◆ pgstat_twophase_postabort()

void pgstat_twophase_postabort ( TransactionId  xid,
uint16  info,
void *  recdata,
uint32  len 
)

◆ pgstat_twophase_postcommit()

void pgstat_twophase_postcommit ( TransactionId  xid,
uint16  info,
void *  recdata,
uint32  len 
)

Definition at line 684 of file pgstat_relation.c.

686 {
687  TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
688  PgStat_TableStatus *pgstat_info;
689 
690  /* Find or create a tabstat entry for the rel */
691  pgstat_info = pgstat_prep_relation_pending(rec->t_id, rec->t_shared);
692 
693  /* Same math as in AtEOXact_PgStat, commit case */
694  pgstat_info->t_counts.t_tuples_inserted += rec->tuples_inserted;
695  pgstat_info->t_counts.t_tuples_updated += rec->tuples_updated;
696  pgstat_info->t_counts.t_tuples_deleted += rec->tuples_deleted;
697  pgstat_info->t_counts.t_truncdropped = rec->t_truncdropped;
698  if (rec->t_truncdropped)
699  {
700  /* forget live/dead stats seen by backend thus far */
701  pgstat_info->t_counts.t_delta_live_tuples = 0;
702  pgstat_info->t_counts.t_delta_dead_tuples = 0;
703  }
704  pgstat_info->t_counts.t_delta_live_tuples +=
705  rec->tuples_inserted - rec->tuples_deleted;
706  pgstat_info->t_counts.t_delta_dead_tuples +=
707  rec->tuples_updated + rec->tuples_deleted;
708  pgstat_info->t_counts.t_changed_tuples +=
709  rec->tuples_inserted + rec->tuples_updated +
710  rec->tuples_deleted;
711 }

References pgstat_prep_relation_pending(), PgStat_TableCounts::t_changed_tuples, PgStat_TableStatus::t_counts, PgStat_TableCounts::t_delta_dead_tuples, PgStat_TableCounts::t_delta_live_tuples, TwoPhasePgStatRecord::t_id, TwoPhasePgStatRecord::t_shared, TwoPhasePgStatRecord::t_truncdropped, PgStat_TableCounts::t_truncdropped, PgStat_TableCounts::t_tuples_deleted, PgStat_TableCounts::t_tuples_inserted, PgStat_TableCounts::t_tuples_updated, TwoPhasePgStatRecord::tuples_deleted, TwoPhasePgStatRecord::tuples_inserted, and TwoPhasePgStatRecord::tuples_updated.

◆ pgstat_unlink_relation()

void pgstat_unlink_relation ( Relation  rel)

Definition at line 154 of file pgstat_relation.c.

155 {
156  /* remove the link to stats info if any */
157  if (rel->pgstat_info == NULL)
158  return;
159 
160  /* link sanity check */
161  Assert(rel->pgstat_info->relation == rel);
162  rel->pgstat_info->relation = NULL;
163  rel->pgstat_info = NULL;
164 }

References Assert(), RelationData::pgstat_info, and PgStat_TableStatus::relation.

Referenced by pgstat_init_relation(), pgstat_relation_delete_pending_cb(), and RelationDestroyRelation().

◆ pgstat_update_heap_dead_tuples()

void pgstat_update_heap_dead_tuples ( Relation  rel,
int  delta 
)

Definition at line 422 of file pgstat_relation.c.

423 {
425  {
426  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
427 
428  pgstat_info->t_counts.t_delta_dead_tuples -= delta;
429  }
430 }

References RelationData::pgstat_info, pgstat_should_count_relation, PgStat_TableStatus::t_counts, and PgStat_TableCounts::t_delta_dead_tuples.

Referenced by heap_page_prune_opt().

◆ PostPrepare_PgStat_Relations()

void PostPrepare_PgStat_Relations ( PgStat_SubXactStatus xact_state)

Definition at line 665 of file pgstat_relation.c.

666 {
668 
669  for (trans = xact_state->first; trans != NULL; trans = trans->next)
670  {
671  PgStat_TableStatus *tabstat;
672 
673  tabstat = trans->parent;
674  tabstat->trans = NULL;
675  }
676 }

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

Referenced by PostPrepare_PgStat().

◆ restore_truncdrop_counters()

static void restore_truncdrop_counters ( PgStat_TableXactStatus trans)
static

Definition at line 928 of file pgstat_relation.c.

929 {
930  if (trans->truncdropped)
931  {
932  trans->tuples_inserted = trans->inserted_pre_truncdrop;
933  trans->tuples_updated = trans->updated_pre_truncdrop;
934  trans->tuples_deleted = trans->deleted_pre_truncdrop;
935  }
936 }

References trans.

Referenced by AtEOSubXact_PgStat_Relations(), and AtEOXact_PgStat_Relations().

◆ save_truncdrop_counters()

static void save_truncdrop_counters ( PgStat_TableXactStatus trans,
bool  is_drop 
)
static

Definition at line 913 of file pgstat_relation.c.

914 {
915  if (!trans->truncdropped || is_drop)
916  {
917  trans->inserted_pre_truncdrop = trans->tuples_inserted;
918  trans->updated_pre_truncdrop = trans->tuples_updated;
919  trans->deleted_pre_truncdrop = trans->tuples_deleted;
920  trans->truncdropped = true;
921  }
922 }

References trans.

Referenced by AtEOSubXact_PgStat_Relations(), pgstat_count_truncate(), and pgstat_drop_relation().