PostgreSQL Source Code  git master
pgstat_xact.c File Reference
#include "postgres.h"
#include "access/xact.h"
#include "pgstat.h"
#include "utils/memutils.h"
#include "utils/pgstat_internal.h"
Include dependency graph for pgstat_xact.c:

Go to the source code of this file.

Data Structures

struct  PgStat_PendingDroppedStatsItem
 

Typedefs

typedef struct PgStat_PendingDroppedStatsItem PgStat_PendingDroppedStatsItem
 

Functions

static void AtEOXact_PgStat_DroppedStats (PgStat_SubXactStatus *xact_state, bool isCommit)
 
static void AtEOSubXact_PgStat_DroppedStats (PgStat_SubXactStatus *xact_state, bool isCommit, int nestDepth)
 
void AtEOXact_PgStat (bool isCommit, bool parallel)
 
void AtEOSubXact_PgStat (bool isCommit, int nestDepth)
 
void AtPrepare_PgStat (void)
 
void PostPrepare_PgStat (void)
 
PgStat_SubXactStatuspgstat_get_xact_stack_level (int nest_level)
 
int pgstat_get_transactional_drops (bool isCommit, xl_xact_stats_item **items)
 
void pgstat_execute_transactional_drops (int ndrops, struct xl_xact_stats_item *items, bool is_redo)
 
static void create_drop_transactional_internal (PgStat_Kind kind, Oid dboid, uint64 objid, bool is_create)
 
void pgstat_create_transactional (PgStat_Kind kind, Oid dboid, uint64 objid)
 
void pgstat_drop_transactional (PgStat_Kind kind, Oid dboid, uint64 objid)
 

Variables

static PgStat_SubXactStatuspgStatXactStack = NULL
 

Typedef Documentation

◆ PgStat_PendingDroppedStatsItem

Function Documentation

◆ AtEOSubXact_PgStat()

void AtEOSubXact_PgStat ( bool  isCommit,
int  nestDepth 
)

Definition at line 113 of file pgstat_xact.c.

114 {
115  PgStat_SubXactStatus *xact_state;
116 
117  /* merge the sub-transaction's transactional stats into the parent */
118  xact_state = pgStatXactStack;
119  if (xact_state != NULL &&
120  xact_state->nest_level >= nestDepth)
121  {
122  /* delink xact_state from stack immediately to simplify reuse case */
123  pgStatXactStack = xact_state->prev;
124 
125  AtEOSubXact_PgStat_Relations(xact_state, isCommit, nestDepth);
126  AtEOSubXact_PgStat_DroppedStats(xact_state, isCommit, nestDepth);
127 
128  pfree(xact_state);
129  }
130 }
void pfree(void *pointer)
Definition: mcxt.c:1521
void AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, int nestDepth)
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat_xact.c:33
static void AtEOSubXact_PgStat_DroppedStats(PgStat_SubXactStatus *xact_state, bool isCommit, int nestDepth)
Definition: pgstat_xact.c:136
struct PgStat_SubXactStatus * prev

References AtEOSubXact_PgStat_DroppedStats(), AtEOSubXact_PgStat_Relations(), PgStat_SubXactStatus::nest_level, pfree(), pgStatXactStack, and PgStat_SubXactStatus::prev.

Referenced by AbortSubTransaction(), and CommitSubTransaction().

◆ AtEOSubXact_PgStat_DroppedStats()

static void AtEOSubXact_PgStat_DroppedStats ( PgStat_SubXactStatus xact_state,
bool  isCommit,
int  nestDepth 
)
static

Definition at line 136 of file pgstat_xact.c.

138 {
139  PgStat_SubXactStatus *parent_xact_state;
140  dlist_mutable_iter iter;
141  int not_freed_count = 0;
142 
143  if (dclist_count(&xact_state->pending_drops) == 0)
144  return;
145 
146  parent_xact_state = pgstat_get_xact_stack_level(nestDepth - 1);
147 
148  dclist_foreach_modify(iter, &xact_state->pending_drops)
149  {
152  xl_xact_stats_item *it = &pending->item;
153  uint64 objid = ((uint64) it->objid_hi) << 32 | it->objid_lo;
154 
155  dclist_delete_from(&xact_state->pending_drops, &pending->node);
156 
157  if (!isCommit && pending->is_create)
158  {
159  /*
160  * Subtransaction creating a new stats object aborted. Drop the
161  * stats object.
162  */
163  if (!pgstat_drop_entry(it->kind, it->dboid, objid))
164  not_freed_count++;
165  pfree(pending);
166  }
167  else if (isCommit)
168  {
169  /*
170  * Subtransaction dropping a stats object committed. Can't yet
171  * remove the stats object, the surrounding transaction might
172  * still abort. Pass it on to the parent.
173  */
174  dclist_push_tail(&parent_xact_state->pending_drops, &pending->node);
175  }
176  else
177  {
178  pfree(pending);
179  }
180  }
181 
182  Assert(dclist_count(&xact_state->pending_drops) == 0);
183  if (not_freed_count > 0)
185 }
#define Assert(condition)
Definition: c.h:812
uint64_t uint64
Definition: c.h:486
#define dclist_container(type, membername, ptr)
Definition: ilist.h:947
static void dclist_push_tail(dclist_head *head, dlist_node *node)
Definition: ilist.h:709
static uint32 dclist_count(const dclist_head *head)
Definition: ilist.h:932
static void dclist_delete_from(dclist_head *head, dlist_node *node)
Definition: ilist.h:763
#define dclist_foreach_modify(iter, lhead)
Definition: ilist.h:973
void pgstat_request_entry_refs_gc(void)
Definition: pgstat_shmem.c:700
bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat_shmem.c:953
PgStat_SubXactStatus * pgstat_get_xact_stack_level(int nest_level)
Definition: pgstat_xact.c:238
xl_xact_stats_item item
Definition: pgstat_xact.c:23
dlist_node * cur
Definition: ilist.h:200
uint32 objid_lo
Definition: xact.h:291
uint32 objid_hi
Definition: xact.h:292

References Assert, dlist_mutable_iter::cur, xl_xact_stats_item::dboid, dclist_container, dclist_count(), dclist_delete_from(), dclist_foreach_modify, dclist_push_tail(), PgStat_PendingDroppedStatsItem::is_create, PgStat_PendingDroppedStatsItem::item, xl_xact_stats_item::kind, PgStat_PendingDroppedStatsItem::node, xl_xact_stats_item::objid_hi, xl_xact_stats_item::objid_lo, PgStat_SubXactStatus::pending_drops, pfree(), pgstat_drop_entry(), pgstat_get_xact_stack_level(), and pgstat_request_entry_refs_gc().

Referenced by AtEOSubXact_PgStat().

◆ AtEOXact_PgStat()

void AtEOXact_PgStat ( bool  isCommit,
bool  parallel 
)

Definition at line 40 of file pgstat_xact.c.

41 {
42  PgStat_SubXactStatus *xact_state;
43 
44  AtEOXact_PgStat_Database(isCommit, parallel);
45 
46  /* handle transactional stats information */
47  xact_state = pgStatXactStack;
48  if (xact_state != NULL)
49  {
50  Assert(xact_state->nest_level == 1);
51  Assert(xact_state->prev == NULL);
52 
53  AtEOXact_PgStat_Relations(xact_state, isCommit);
54  AtEOXact_PgStat_DroppedStats(xact_state, isCommit);
55  }
56  pgStatXactStack = NULL;
57 
58  /* Make sure any stats snapshot is thrown away */
60 }
void pgstat_clear_snapshot(void)
Definition: pgstat.c:907
void AtEOXact_PgStat_Database(bool isCommit, bool parallel)
void AtEOXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit)
static void AtEOXact_PgStat_DroppedStats(PgStat_SubXactStatus *xact_state, bool isCommit)
Definition: pgstat_xact.c:67

References Assert, AtEOXact_PgStat_Database(), AtEOXact_PgStat_DroppedStats(), AtEOXact_PgStat_Relations(), PgStat_SubXactStatus::nest_level, pgstat_clear_snapshot(), pgStatXactStack, and PgStat_SubXactStatus::prev.

Referenced by AbortTransaction(), CommitTransaction(), and FinishPreparedTransaction().

◆ AtEOXact_PgStat_DroppedStats()

static void AtEOXact_PgStat_DroppedStats ( PgStat_SubXactStatus xact_state,
bool  isCommit 
)
static

Definition at line 67 of file pgstat_xact.c.

68 {
69  dlist_mutable_iter iter;
70  int not_freed_count = 0;
71 
72  if (dclist_count(&xact_state->pending_drops) == 0)
73  return;
74 
75  dclist_foreach_modify(iter, &xact_state->pending_drops)
76  {
79  xl_xact_stats_item *it = &pending->item;
80  uint64 objid = ((uint64) it->objid_hi) << 32 | it->objid_lo;
81 
82  if (isCommit && !pending->is_create)
83  {
84  /*
85  * Transaction that dropped an object committed. Drop the stats
86  * too.
87  */
88  if (!pgstat_drop_entry(it->kind, it->dboid, objid))
89  not_freed_count++;
90  }
91  else if (!isCommit && pending->is_create)
92  {
93  /*
94  * Transaction that created an object aborted. Drop the stats
95  * associated with the object.
96  */
97  if (!pgstat_drop_entry(it->kind, it->dboid, objid))
98  not_freed_count++;
99  }
100 
101  dclist_delete_from(&xact_state->pending_drops, &pending->node);
102  pfree(pending);
103  }
104 
105  if (not_freed_count > 0)
107 }

References dlist_mutable_iter::cur, xl_xact_stats_item::dboid, dclist_container, dclist_count(), dclist_delete_from(), dclist_foreach_modify, PgStat_PendingDroppedStatsItem::is_create, PgStat_PendingDroppedStatsItem::item, xl_xact_stats_item::kind, PgStat_PendingDroppedStatsItem::node, xl_xact_stats_item::objid_hi, xl_xact_stats_item::objid_lo, PgStat_SubXactStatus::pending_drops, pfree(), pgstat_drop_entry(), and pgstat_request_entry_refs_gc().

Referenced by AtEOXact_PgStat().

◆ AtPrepare_PgStat()

void AtPrepare_PgStat ( void  )

Definition at line 191 of file pgstat_xact.c.

192 {
193  PgStat_SubXactStatus *xact_state;
194 
195  xact_state = pgStatXactStack;
196  if (xact_state != NULL)
197  {
198  Assert(xact_state->nest_level == 1);
199  Assert(xact_state->prev == NULL);
200 
201  AtPrepare_PgStat_Relations(xact_state);
202  }
203 }
void AtPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)

References Assert, AtPrepare_PgStat_Relations(), PgStat_SubXactStatus::nest_level, pgStatXactStack, and PgStat_SubXactStatus::prev.

Referenced by PrepareTransaction().

◆ create_drop_transactional_internal()

static void create_drop_transactional_internal ( PgStat_Kind  kind,
Oid  dboid,
uint64  objid,
bool  is_create 
)
static

Definition at line 335 of file pgstat_xact.c.

336 {
337  int nest_level = GetCurrentTransactionNestLevel();
338  PgStat_SubXactStatus *xact_state;
341 
342  xact_state = pgstat_get_xact_stack_level(nest_level);
343 
344  drop->is_create = is_create;
345  drop->item.kind = kind;
346  drop->item.dboid = dboid;
347  drop->item.objid_lo = (uint32) objid;
348  drop->item.objid_hi = (uint32) (objid >> 32);
349 
350  dclist_push_tail(&xact_state->pending_drops, &drop->node);
351 }
uint32_t uint32
Definition: c.h:485
MemoryContext TopTransactionContext
Definition: mcxt.c:154
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:928

References xl_xact_stats_item::dboid, dclist_push_tail(), GetCurrentTransactionNestLevel(), PgStat_PendingDroppedStatsItem::is_create, PgStat_PendingDroppedStatsItem::item, xl_xact_stats_item::kind, MemoryContextAlloc(), PgStat_PendingDroppedStatsItem::node, xl_xact_stats_item::objid_hi, xl_xact_stats_item::objid_lo, PgStat_SubXactStatus::pending_drops, pgstat_get_xact_stack_level(), and TopTransactionContext.

Referenced by pgstat_create_transactional(), and pgstat_drop_transactional().

◆ pgstat_create_transactional()

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

Definition at line 361 of file pgstat_xact.c.

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

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

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

◆ pgstat_drop_transactional()

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

Definition at line 384 of file pgstat_xact.c.

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

References create_drop_transactional_internal().

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

◆ pgstat_execute_transactional_drops()

void pgstat_execute_transactional_drops ( int  ndrops,
struct xl_xact_stats_item items,
bool  is_redo 
)

Definition at line 314 of file pgstat_xact.c.

315 {
316  int not_freed_count = 0;
317 
318  if (ndrops == 0)
319  return;
320 
321  for (int i = 0; i < ndrops; i++)
322  {
323  xl_xact_stats_item *it = &items[i];
324  uint64 objid = ((uint64) it->objid_hi) << 32 | it->objid_lo;
325 
326  if (!pgstat_drop_entry(it->kind, it->dboid, objid))
327  not_freed_count++;
328  }
329 
330  if (not_freed_count > 0)
332 }
int i
Definition: isn.c:72
static ItemArray items
Definition: test_tidstore.c:48

References xl_xact_stats_item::dboid, i, items, xl_xact_stats_item::kind, xl_xact_stats_item::objid_hi, xl_xact_stats_item::objid_lo, pgstat_drop_entry(), and pgstat_request_entry_refs_gc().

Referenced by FinishPreparedTransaction(), xact_redo_abort(), and xact_redo_commit().

◆ pgstat_get_transactional_drops()

int pgstat_get_transactional_drops ( bool  isCommit,
xl_xact_stats_item **  items 
)

Definition at line 272 of file pgstat_xact.c.

273 {
275  int nitems = 0;
276  dlist_iter iter;
277 
278  if (xact_state == NULL)
279  return 0;
280 
281  /*
282  * We expect to be called for subtransaction abort (which logs a WAL
283  * record), but not for subtransaction commit (which doesn't).
284  */
285  Assert(!isCommit || xact_state->nest_level == 1);
286  Assert(!isCommit || xact_state->prev == NULL);
287 
288  *items = palloc(dclist_count(&xact_state->pending_drops)
289  * sizeof(xl_xact_stats_item));
290 
291  dclist_foreach(iter, &xact_state->pending_drops)
292  {
295 
296  if (isCommit && pending->is_create)
297  continue;
298  if (!isCommit && !pending->is_create)
299  continue;
300 
301  Assert(nitems < dclist_count(&xact_state->pending_drops));
302  (*items)[nitems++] = pending->item;
303  }
304 
305  return nitems;
306 }
#define dclist_foreach(iter, lhead)
Definition: ilist.h:970
#define nitems(x)
Definition: indent.h:31
void * palloc(Size size)
Definition: mcxt.c:1317
dlist_node * cur
Definition: ilist.h:179

References Assert, dlist_iter::cur, dclist_container, dclist_count(), dclist_foreach, PgStat_PendingDroppedStatsItem::is_create, PgStat_PendingDroppedStatsItem::item, items, PgStat_SubXactStatus::nest_level, nitems, palloc(), PgStat_SubXactStatus::pending_drops, pgStatXactStack, and PgStat_SubXactStatus::prev.

Referenced by RecordTransactionAbort(), RecordTransactionCommit(), and StartPrepare().

◆ pgstat_get_xact_stack_level()

PgStat_SubXactStatus* pgstat_get_xact_stack_level ( int  nest_level)

Definition at line 238 of file pgstat_xact.c.

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

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

◆ PostPrepare_PgStat()

void PostPrepare_PgStat ( void  )

Definition at line 211 of file pgstat_xact.c.

212 {
213  PgStat_SubXactStatus *xact_state;
214 
215  /*
216  * We don't bother to free any of the transactional state, since it's all
217  * in TopTransactionContext and will go away anyway.
218  */
219  xact_state = pgStatXactStack;
220  if (xact_state != NULL)
221  {
222  Assert(xact_state->nest_level == 1);
223  Assert(xact_state->prev == NULL);
224 
225  PostPrepare_PgStat_Relations(xact_state);
226  }
227  pgStatXactStack = NULL;
228 
229  /* Make sure any stats snapshot is thrown away */
231 }
void PostPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)

References Assert, PgStat_SubXactStatus::nest_level, pgstat_clear_snapshot(), pgStatXactStack, PostPrepare_PgStat_Relations(), and PgStat_SubXactStatus::prev.

Referenced by PrepareTransaction().

Variable Documentation

◆ pgStatXactStack