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, Oid objoid, bool is_create)
 
void pgstat_create_transactional (PgStat_Kind kind, Oid dboid, Oid objoid)
 
void pgstat_drop_transactional (PgStat_Kind kind, Oid dboid, Oid objoid)
 

Variables

static PgStat_SubXactStatuspgStatXactStack = NULL
 

Typedef Documentation

◆ PgStat_PendingDroppedStatsItem

Function Documentation

◆ AtEOSubXact_PgStat()

void AtEOSubXact_PgStat ( bool  isCommit,
int  nestDepth 
)

Definition at line 112 of file pgstat_xact.c.

113 {
114  PgStat_SubXactStatus *xact_state;
115 
116  /* merge the sub-transaction's transactional stats into the parent */
117  xact_state = pgStatXactStack;
118  if (xact_state != NULL &&
119  xact_state->nest_level >= nestDepth)
120  {
121  /* delink xact_state from stack immediately to simplify reuse case */
122  pgStatXactStack = xact_state->prev;
123 
124  AtEOSubXact_PgStat_Relations(xact_state, isCommit, nestDepth);
125  AtEOSubXact_PgStat_DroppedStats(xact_state, isCommit, nestDepth);
126 
127  pfree(xact_state);
128  }
129 }
void pfree(void *pointer)
Definition: mcxt.c:1520
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:135
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 135 of file pgstat_xact.c.

137 {
138  PgStat_SubXactStatus *parent_xact_state;
139  dlist_mutable_iter iter;
140  int not_freed_count = 0;
141 
142  if (dclist_count(&xact_state->pending_drops) == 0)
143  return;
144 
145  parent_xact_state = pgstat_get_xact_stack_level(nestDepth - 1);
146 
147  dclist_foreach_modify(iter, &xact_state->pending_drops)
148  {
151  xl_xact_stats_item *it = &pending->item;
152 
153  dclist_delete_from(&xact_state->pending_drops, &pending->node);
154 
155  if (!isCommit && pending->is_create)
156  {
157  /*
158  * Subtransaction creating a new stats object aborted. Drop the
159  * stats object.
160  */
161  if (!pgstat_drop_entry(it->kind, it->dboid, it->objoid))
162  not_freed_count++;
163  pfree(pending);
164  }
165  else if (isCommit)
166  {
167  /*
168  * Subtransaction dropping a stats object committed. Can't yet
169  * remove the stats object, the surrounding transaction might
170  * still abort. Pass it on to the parent.
171  */
172  dclist_push_tail(&parent_xact_state->pending_drops, &pending->node);
173  }
174  else
175  {
176  pfree(pending);
177  }
178  }
179 
180  Assert(dclist_count(&xact_state->pending_drops) == 0);
181  if (not_freed_count > 0)
183 }
#define Assert(condition)
Definition: c.h:858
#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:629
bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat_shmem.c:874
PgStat_SubXactStatus * pgstat_get_xact_stack_level(int nest_level)
Definition: pgstat_xact.c:236
xl_xact_stats_item item
Definition: pgstat_xact.c:23
dlist_node * cur
Definition: ilist.h:200

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::objoid, 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:782
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 
81  if (isCommit && !pending->is_create)
82  {
83  /*
84  * Transaction that dropped an object committed. Drop the stats
85  * too.
86  */
87  if (!pgstat_drop_entry(it->kind, it->dboid, it->objoid))
88  not_freed_count++;
89  }
90  else if (!isCommit && pending->is_create)
91  {
92  /*
93  * Transaction that created an object aborted. Drop the stats
94  * associated with the object.
95  */
96  if (!pgstat_drop_entry(it->kind, it->dboid, it->objoid))
97  not_freed_count++;
98  }
99 
100  dclist_delete_from(&xact_state->pending_drops, &pending->node);
101  pfree(pending);
102  }
103 
104  if (not_freed_count > 0)
106 }

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::objoid, 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 189 of file pgstat_xact.c.

190 {
191  PgStat_SubXactStatus *xact_state;
192 
193  xact_state = pgStatXactStack;
194  if (xact_state != NULL)
195  {
196  Assert(xact_state->nest_level == 1);
197  Assert(xact_state->prev == NULL);
198 
199  AtPrepare_PgStat_Relations(xact_state);
200  }
201 }
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,
Oid  objoid,
bool  is_create 
)
static

Definition at line 332 of file pgstat_xact.c.

333 {
334  int nest_level = GetCurrentTransactionNestLevel();
335  PgStat_SubXactStatus *xact_state;
338 
339  xact_state = pgstat_get_xact_stack_level(nest_level);
340 
341  drop->is_create = is_create;
342  drop->item.kind = kind;
343  drop->item.dboid = dboid;
344  drop->item.objoid = objoid;
345 
346  dclist_push_tail(&xact_state->pending_drops, &drop->node);
347 }
MemoryContext TopTransactionContext
Definition: mcxt.c:154
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1180
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:926

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::objoid, 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,
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:734
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1263
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:398
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_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_execute_transactional_drops()

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

Definition at line 312 of file pgstat_xact.c.

313 {
314  int not_freed_count = 0;
315 
316  if (ndrops == 0)
317  return;
318 
319  for (int i = 0; i < ndrops; i++)
320  {
321  xl_xact_stats_item *it = &items[i];
322 
323  if (!pgstat_drop_entry(it->kind, it->dboid, it->objoid))
324  not_freed_count++;
325  }
326 
327  if (not_freed_count > 0)
329 }
int i
Definition: isn.c:73
static ItemArray items
Definition: test_tidstore.c:49

References xl_xact_stats_item::dboid, i, items, xl_xact_stats_item::kind, xl_xact_stats_item::objoid, 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 270 of file pgstat_xact.c.

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

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