PostgreSQL Source Code  git master
pgstat_xact.c File Reference
#include "postgres.h"
#include "access/transam.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 117 of file pgstat_xact.c.

118 {
119  PgStat_SubXactStatus *xact_state;
120 
121  /* merge the sub-transaction's transactional stats into the parent */
122  xact_state = pgStatXactStack;
123  if (xact_state != NULL &&
124  xact_state->nest_level >= nestDepth)
125  {
126  /* delink xact_state from stack immediately to simplify reuse case */
127  pgStatXactStack = xact_state->prev;
128 
129  AtEOSubXact_PgStat_Relations(xact_state, isCommit, nestDepth);
130  AtEOSubXact_PgStat_DroppedStats(xact_state, isCommit, nestDepth);
131 
132  pfree(xact_state);
133  }
134 }
void pfree(void *pointer)
Definition: mcxt.c:1175
void AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, int nestDepth)
static PgStat_SubXactStatus * pgStatXactStack
Definition: pgstat_xact.c:34
static void AtEOSubXact_PgStat_DroppedStats(PgStat_SubXactStatus *xact_state, bool isCommit, int nestDepth)
Definition: pgstat_xact.c:140
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 140 of file pgstat_xact.c.

142 {
143  PgStat_SubXactStatus *parent_xact_state;
144  dlist_mutable_iter iter;
145  int not_freed_count = 0;
146 
147  if (xact_state->pending_drops_count == 0)
148  return;
149 
150  parent_xact_state = pgstat_get_xact_stack_level(nestDepth - 1);
151 
152  dlist_foreach_modify(iter, &xact_state->pending_drops)
153  {
156  xl_xact_stats_item *it = &pending->item;
157 
158  dlist_delete(&pending->node);
159  xact_state->pending_drops_count--;
160 
161  if (!isCommit && pending->is_create)
162  {
163  /*
164  * Subtransaction creating a new stats object aborted. Drop the
165  * stats object.
166  */
167  if (!pgstat_drop_entry(it->kind, it->dboid, it->objoid))
168  not_freed_count++;
169  pfree(pending);
170  }
171  else if (isCommit)
172  {
173  /*
174  * Subtransaction dropping a stats object committed. Can't yet
175  * remove the stats object, the surrounding transaction might
176  * still abort. Pass it on to the parent.
177  */
178  dlist_push_tail(&parent_xact_state->pending_drops, &pending->node);
179  parent_xact_state->pending_drops_count++;
180  }
181  else
182  {
183  pfree(pending);
184  }
185  }
186 
187  Assert(xact_state->pending_drops_count == 0);
188  if (not_freed_count > 0)
190 }
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
#define dlist_foreach_modify(iter, lhead)
Definition: ilist.h:543
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:317
#define dlist_container(type, membername, ptr)
Definition: ilist.h:496
Assert(fmt[strlen(fmt) - 1] !='\n')
void pgstat_request_entry_refs_gc(void)
Definition: pgstat_shmem.c:608
bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat_shmem.c:838
PgStat_SubXactStatus * pgstat_get_xact_stack_level(int nest_level)
Definition: pgstat_xact.c:243
xl_xact_stats_item item
Definition: pgstat_xact.c:24
dlist_node * cur
Definition: ilist.h:180

References Assert(), dlist_mutable_iter::cur, xl_xact_stats_item::dboid, dlist_container, dlist_delete(), dlist_foreach_modify, dlist_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, PgStat_SubXactStatus::pending_drops_count, 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 41 of file pgstat_xact.c.

42 {
43  PgStat_SubXactStatus *xact_state;
44 
45  AtEOXact_PgStat_Database(isCommit, parallel);
46 
47  /* handle transactional stats information */
48  xact_state = pgStatXactStack;
49  if (xact_state != NULL)
50  {
51  Assert(xact_state->nest_level == 1);
52  Assert(xact_state->prev == NULL);
53 
54  AtEOXact_PgStat_Relations(xact_state, isCommit);
55  AtEOXact_PgStat_DroppedStats(xact_state, isCommit);
56  }
57  pgStatXactStack = NULL;
58 
59  /* Make sure any stats snapshot is thrown away */
61 }
void pgstat_clear_snapshot(void)
Definition: pgstat.c:752
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:68

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 68 of file pgstat_xact.c.

69 {
70  dlist_mutable_iter iter;
71  int not_freed_count = 0;
72 
73  if (xact_state->pending_drops_count == 0)
74  {
75  Assert(dlist_is_empty(&xact_state->pending_drops));
76  return;
77  }
78 
79  dlist_foreach_modify(iter, &xact_state->pending_drops)
80  {
83  xl_xact_stats_item *it = &pending->item;
84 
85  if (isCommit && !pending->is_create)
86  {
87  /*
88  * Transaction that dropped an object committed. Drop the stats
89  * too.
90  */
91  if (!pgstat_drop_entry(it->kind, it->dboid, it->objoid))
92  not_freed_count++;
93  }
94  else if (!isCommit && pending->is_create)
95  {
96  /*
97  * Transaction that created an object aborted. Drop the stats
98  * associated with the object.
99  */
100  if (!pgstat_drop_entry(it->kind, it->dboid, it->objoid))
101  not_freed_count++;
102  }
103 
104  dlist_delete(&pending->node);
105  xact_state->pending_drops_count--;
106  pfree(pending);
107  }
108 
109  if (not_freed_count > 0)
111 }
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289

References Assert(), dlist_mutable_iter::cur, xl_xact_stats_item::dboid, dlist_container, dlist_delete(), dlist_foreach_modify, dlist_is_empty(), PgStat_PendingDroppedStatsItem::is_create, PgStat_PendingDroppedStatsItem::item, xl_xact_stats_item::kind, PgStat_PendingDroppedStatsItem::node, xl_xact_stats_item::objoid, PgStat_SubXactStatus::pending_drops, PgStat_SubXactStatus::pending_drops_count, pfree(), pgstat_drop_entry(), and pgstat_request_entry_refs_gc().

Referenced by AtEOXact_PgStat().

◆ AtPrepare_PgStat()

void AtPrepare_PgStat ( void  )

Definition at line 196 of file pgstat_xact.c.

197 {
198  PgStat_SubXactStatus *xact_state;
199 
200  xact_state = pgStatXactStack;
201  if (xact_state != NULL)
202  {
203  Assert(xact_state->nest_level == 1);
204  Assert(xact_state->prev == NULL);
205 
206  AtPrepare_PgStat_Relations(xact_state);
207  }
208 }
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 340 of file pgstat_xact.c.

341 {
342  int nest_level = GetCurrentTransactionNestLevel();
343  PgStat_SubXactStatus *xact_state;
346 
347  xact_state = pgstat_get_xact_stack_level(nest_level);
348 
349  drop->is_create = is_create;
350  drop->item.kind = kind;
351  drop->item.dboid = dboid;
352  drop->item.objoid = objoid;
353 
354  dlist_push_tail(&xact_state->pending_drops, &drop->node);
355  xact_state->pending_drops_count++;
356 }
MemoryContext TopTransactionContext
Definition: mcxt.c:53
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:910

References xl_xact_stats_item::dboid, dlist_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_SubXactStatus::pending_drops_count, 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 366 of file pgstat_xact.c.

367 {
368  if (pgstat_get_entry_ref(kind, dboid, objoid, false, NULL))
369  {
371  errmsg("resetting existing stats for type %s, db=%u, oid=%u",
372  (pgstat_get_kind_info(kind))->name, dboid, objoid));
373 
374  pgstat_reset(kind, dboid, objoid);
375  }
376 
377  create_drop_transactional_internal(kind, dboid, objoid, /* create */ true);
378 }
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define WARNING
Definition: elog.h:30
#define ereport(elevel,...)
Definition: elog.h:143
const char * name
Definition: encode.c:561
void pgstat_reset(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:705
const PgStat_KindInfo * pgstat_get_kind_info(PgStat_Kind kind)
Definition: pgstat.c:1211
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:393
static void create_drop_transactional_internal(PgStat_Kind kind, Oid dboid, Oid objoid, bool is_create)
Definition: pgstat_xact.c:340

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 388 of file pgstat_xact.c.

389 {
390  create_drop_transactional_internal(kind, dboid, objoid, /* create */ false);
391 }

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 320 of file pgstat_xact.c.

321 {
322  int not_freed_count = 0;
323 
324  if (ndrops == 0)
325  return;
326 
327  for (int i = 0; i < ndrops; i++)
328  {
329  xl_xact_stats_item *it = &items[i];
330 
331  if (!pgstat_drop_entry(it->kind, it->dboid, it->objoid))
332  not_freed_count++;
333  }
334 
335  if (not_freed_count > 0)
337 }
int i
Definition: isn.c:73

References xl_xact_stats_item::dboid, i, 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 278 of file pgstat_xact.c.

279 {
281  int nitems = 0;
282  dlist_iter iter;
283 
284  if (xact_state == NULL)
285  return 0;
286 
287  /*
288  * We expect to be called for subtransaction abort (which logs a WAL
289  * record), but not for subtransaction commit (which doesn't).
290  */
291  Assert(!isCommit || xact_state->nest_level == 1);
292  Assert(!isCommit || xact_state->prev == NULL);
293 
294  *items = palloc(xact_state->pending_drops_count
295  * sizeof(xl_xact_stats_item));
296 
297  dlist_foreach(iter, &xact_state->pending_drops)
298  {
301 
302  if (isCommit && pending->is_create)
303  continue;
304  if (!isCommit && !pending->is_create)
305  continue;
306 
307  Assert(nitems < xact_state->pending_drops_count);
308  (*items)[nitems++] = pending->item;
309  }
310 
311  return nitems;
312 }
#define dlist_foreach(iter, lhead)
Definition: ilist.h:526
void * palloc(Size size)
Definition: mcxt.c:1068
dlist_node * cur
Definition: ilist.h:161

References Assert(), dlist_iter::cur, dlist_container, dlist_foreach, PgStat_PendingDroppedStatsItem::is_create, PgStat_PendingDroppedStatsItem::item, PgStat_SubXactStatus::nest_level, palloc(), PgStat_SubXactStatus::pending_drops, PgStat_SubXactStatus::pending_drops_count, 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 243 of file pgstat_xact.c.

244 {
245  PgStat_SubXactStatus *xact_state;
246 
247  xact_state = pgStatXactStack;
248  if (xact_state == NULL || xact_state->nest_level != nest_level)
249  {
250  xact_state = (PgStat_SubXactStatus *)
252  sizeof(PgStat_SubXactStatus));
253  dlist_init(&xact_state->pending_drops);
254  xact_state->pending_drops_count = 0;
255  xact_state->nest_level = nest_level;
256  xact_state->prev = pgStatXactStack;
257  xact_state->first = NULL;
258  pgStatXactStack = xact_state;
259  }
260  return xact_state;
261 }
static void dlist_init(dlist_head *head)
Definition: ilist.h:278
PgStat_TableXactStatus * first

References dlist_init(), PgStat_SubXactStatus::first, MemoryContextAlloc(), PgStat_SubXactStatus::nest_level, PgStat_SubXactStatus::pending_drops, PgStat_SubXactStatus::pending_drops_count, 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 216 of file pgstat_xact.c.

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