Skip to content

Commit ab62a64

Browse files
committed
pgstat: move transactional code into pgstat_xact.c.
The transactional integration code is largely independent from the rest of pgstat.c. Subsequent commits will add more related code. Author: Andres Freund <andres@anarazel.de> Reviewed-By: Thomas Munro <thomas.munro@gmail.com> Discussion: https://postgr.es/m/20220404041516.cctrvpadhuriawlq@alap3.anarazel.de
1 parent c3e9b07 commit ab62a64

File tree

5 files changed

+189
-163
lines changed

5 files changed

+189
-163
lines changed

src/backend/postmaster/pgstat.c

Lines changed: 30 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,6 @@ static time_t last_pgstat_start_time;
198198

199199
static bool pgStatRunningInCollector = false;
200200

201-
static PgStat_SubXactStatus *pgStatXactStack = NULL;
202-
203201
/*
204202
* Info about current "snapshot" of stats file
205203
*/
@@ -740,158 +738,6 @@ pgstat_initialize(void)
740738
}
741739

742740

743-
/* ------------------------------------------------------------
744-
* Transaction integration
745-
* ------------------------------------------------------------
746-
*/
747-
748-
/*
749-
* Called from access/transam/xact.c at top-level transaction commit/abort.
750-
*/
751-
void
752-
AtEOXact_PgStat(bool isCommit, bool parallel)
753-
{
754-
PgStat_SubXactStatus *xact_state;
755-
756-
AtEOXact_PgStat_Database(isCommit, parallel);
757-
758-
/* handle transactional stats information */
759-
xact_state = pgStatXactStack;
760-
if (xact_state != NULL)
761-
{
762-
Assert(xact_state->nest_level == 1);
763-
Assert(xact_state->prev == NULL);
764-
765-
AtEOXact_PgStat_Relations(xact_state, isCommit);
766-
}
767-
pgStatXactStack = NULL;
768-
769-
/* Make sure any stats snapshot is thrown away */
770-
pgstat_clear_snapshot();
771-
}
772-
773-
/*
774-
* Called from access/transam/xact.c at subtransaction commit/abort.
775-
*/
776-
void
777-
AtEOSubXact_PgStat(bool isCommit, int nestDepth)
778-
{
779-
PgStat_SubXactStatus *xact_state;
780-
781-
/* merge the sub-transaction's transactional stats into the parent */
782-
xact_state = pgStatXactStack;
783-
if (xact_state != NULL &&
784-
xact_state->nest_level >= nestDepth)
785-
{
786-
/* delink xact_state from stack immediately to simplify reuse case */
787-
pgStatXactStack = xact_state->prev;
788-
789-
AtEOSubXact_PgStat_Relations(xact_state, isCommit, nestDepth);
790-
791-
pfree(xact_state);
792-
}
793-
}
794-
795-
/*
796-
* Save the transactional stats state at 2PC transaction prepare.
797-
*/
798-
void
799-
AtPrepare_PgStat(void)
800-
{
801-
PgStat_SubXactStatus *xact_state;
802-
803-
xact_state = pgStatXactStack;
804-
if (xact_state != NULL)
805-
{
806-
Assert(xact_state->nest_level == 1);
807-
Assert(xact_state->prev == NULL);
808-
809-
AtPrepare_PgStat_Relations(xact_state);
810-
}
811-
}
812-
813-
/*
814-
* Clean up after successful PREPARE.
815-
*
816-
* Note: AtEOXact_PgStat is not called during PREPARE.
817-
*/
818-
void
819-
PostPrepare_PgStat(void)
820-
{
821-
PgStat_SubXactStatus *xact_state;
822-
823-
/*
824-
* We don't bother to free any of the transactional state, since it's all
825-
* in TopTransactionContext and will go away anyway.
826-
*/
827-
xact_state = pgStatXactStack;
828-
if (xact_state != NULL)
829-
{
830-
Assert(xact_state->nest_level == 1);
831-
Assert(xact_state->prev == NULL);
832-
833-
PostPrepare_PgStat_Relations(xact_state);
834-
}
835-
pgStatXactStack = NULL;
836-
837-
/* Make sure any stats snapshot is thrown away */
838-
pgstat_clear_snapshot();
839-
}
840-
841-
/*
842-
* Discard any data collected in the current transaction. Any subsequent
843-
* request will cause new snapshots to be read.
844-
*
845-
* This is also invoked during transaction commit or abort to discard
846-
* the no-longer-wanted snapshot.
847-
*/
848-
void
849-
pgstat_clear_snapshot(void)
850-
{
851-
pgstat_assert_is_up();
852-
853-
/* Release memory, if any was allocated */
854-
if (pgStatLocalContext)
855-
MemoryContextDelete(pgStatLocalContext);
856-
857-
/* Reset variables */
858-
pgStatLocalContext = NULL;
859-
pgStatDBHash = NULL;
860-
replSlotStatHash = NULL;
861-
subscriptionStatHash = NULL;
862-
863-
/*
864-
* Historically the backend_status.c facilities lived in this file, and
865-
* were reset with the same function. For now keep it that way, and
866-
* forward the reset request.
867-
*/
868-
pgstat_clear_backend_activity_snapshot();
869-
}
870-
871-
/*
872-
* Ensure (sub)transaction stack entry for the given nest_level exists, adding
873-
* it if needed.
874-
*/
875-
PgStat_SubXactStatus *
876-
pgstat_xact_stack_level_get(int nest_level)
877-
{
878-
PgStat_SubXactStatus *xact_state;
879-
880-
xact_state = pgStatXactStack;
881-
if (xact_state == NULL || xact_state->nest_level != nest_level)
882-
{
883-
xact_state = (PgStat_SubXactStatus *)
884-
MemoryContextAlloc(TopTransactionContext,
885-
sizeof(PgStat_SubXactStatus));
886-
xact_state->nest_level = nest_level;
887-
xact_state->prev = pgStatXactStack;
888-
xact_state->first = NULL;
889-
pgStatXactStack = xact_state;
890-
}
891-
return xact_state;
892-
}
893-
894-
895741
/* ------------------------------------------------------------
896742
* Public functions used by backends follow
897743
* ------------------------------------------------------------
@@ -1319,6 +1165,36 @@ pgstat_send_inquiry(TimestampTz clock_time, TimestampTz cutoff_time, Oid databas
13191165
pgstat_send(&msg, sizeof(msg));
13201166
}
13211167

1168+
/*
1169+
* Discard any data collected in the current transaction. Any subsequent
1170+
* request will cause new snapshots to be read.
1171+
*
1172+
* This is also invoked during transaction commit or abort to discard
1173+
* the no-longer-wanted snapshot.
1174+
*/
1175+
void
1176+
pgstat_clear_snapshot(void)
1177+
{
1178+
pgstat_assert_is_up();
1179+
1180+
/* Release memory, if any was allocated */
1181+
if (pgStatLocalContext)
1182+
MemoryContextDelete(pgStatLocalContext);
1183+
1184+
/* Reset variables */
1185+
pgStatLocalContext = NULL;
1186+
pgStatDBHash = NULL;
1187+
replSlotStatHash = NULL;
1188+
subscriptionStatHash = NULL;
1189+
1190+
/*
1191+
* Historically the backend_status.c facilities lived in this file, and
1192+
* were reset with the same function. For now keep it that way, and
1193+
* forward the reset request.
1194+
*/
1195+
pgstat_clear_backend_activity_snapshot();
1196+
}
1197+
13221198
/*
13231199
* Support function for the SQL-callable pgstat* functions. Returns
13241200
* the collected statistics for one database or NULL. NULL doesn't mean

src/backend/utils/activity/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ OBJS = \
2323
pgstat_function.o \
2424
pgstat_relation.o \
2525
pgstat_replslot.o \
26+
pgstat_slru.o \
2627
pgstat_subscription.o \
2728
pgstat_wal.o \
28-
pgstat_slru.o \
29+
pgstat_xact.o \
2930
wait_event.o
3031

3132
include $(top_srcdir)/src/backend/common.mk
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/* -------------------------------------------------------------------------
2+
*
3+
* pgstat_xact.c
4+
* Transactional integration for the cumulative statistics system.
5+
*
6+
* Copyright (c) 2001-2022, PostgreSQL Global Development Group
7+
*
8+
* IDENTIFICATION
9+
* src/backend/utils/activity/pgstat_xact.c
10+
* -------------------------------------------------------------------------
11+
*/
12+
13+
#include "postgres.h"
14+
15+
#include "access/transam.h"
16+
#include "access/xact.h"
17+
#include "pgstat.h"
18+
#include "utils/memutils.h"
19+
#include "utils/pgstat_internal.h"
20+
21+
22+
static PgStat_SubXactStatus *pgStatXactStack = NULL;
23+
24+
25+
/*
26+
* Called from access/transam/xact.c at top-level transaction commit/abort.
27+
*/
28+
void
29+
AtEOXact_PgStat(bool isCommit, bool parallel)
30+
{
31+
PgStat_SubXactStatus *xact_state;
32+
33+
AtEOXact_PgStat_Database(isCommit, parallel);
34+
35+
/* handle transactional stats information */
36+
xact_state = pgStatXactStack;
37+
if (xact_state != NULL)
38+
{
39+
Assert(xact_state->nest_level == 1);
40+
Assert(xact_state->prev == NULL);
41+
42+
AtEOXact_PgStat_Relations(xact_state, isCommit);
43+
}
44+
pgStatXactStack = NULL;
45+
46+
/* Make sure any stats snapshot is thrown away */
47+
pgstat_clear_snapshot();
48+
}
49+
50+
/*
51+
* Called from access/transam/xact.c at subtransaction commit/abort.
52+
*/
53+
void
54+
AtEOSubXact_PgStat(bool isCommit, int nestDepth)
55+
{
56+
PgStat_SubXactStatus *xact_state;
57+
58+
/* merge the sub-transaction's transactional stats into the parent */
59+
xact_state = pgStatXactStack;
60+
if (xact_state != NULL &&
61+
xact_state->nest_level >= nestDepth)
62+
{
63+
/* delink xact_state from stack immediately to simplify reuse case */
64+
pgStatXactStack = xact_state->prev;
65+
66+
AtEOSubXact_PgStat_Relations(xact_state, isCommit, nestDepth);
67+
68+
pfree(xact_state);
69+
}
70+
}
71+
72+
/*
73+
* Save the transactional stats state at 2PC transaction prepare.
74+
*/
75+
void
76+
AtPrepare_PgStat(void)
77+
{
78+
PgStat_SubXactStatus *xact_state;
79+
80+
xact_state = pgStatXactStack;
81+
if (xact_state != NULL)
82+
{
83+
Assert(xact_state->nest_level == 1);
84+
Assert(xact_state->prev == NULL);
85+
86+
AtPrepare_PgStat_Relations(xact_state);
87+
}
88+
}
89+
90+
/*
91+
* Clean up after successful PREPARE.
92+
*
93+
* Note: AtEOXact_PgStat is not called during PREPARE.
94+
*/
95+
void
96+
PostPrepare_PgStat(void)
97+
{
98+
PgStat_SubXactStatus *xact_state;
99+
100+
/*
101+
* We don't bother to free any of the transactional state, since it's all
102+
* in TopTransactionContext and will go away anyway.
103+
*/
104+
xact_state = pgStatXactStack;
105+
if (xact_state != NULL)
106+
{
107+
Assert(xact_state->nest_level == 1);
108+
Assert(xact_state->prev == NULL);
109+
110+
PostPrepare_PgStat_Relations(xact_state);
111+
}
112+
pgStatXactStack = NULL;
113+
114+
/* Make sure any stats snapshot is thrown away */
115+
pgstat_clear_snapshot();
116+
}
117+
118+
/*
119+
* Ensure (sub)transaction stack entry for the given nest_level exists, adding
120+
* it if needed.
121+
*/
122+
PgStat_SubXactStatus *
123+
pgstat_xact_stack_level_get(int nest_level)
124+
{
125+
PgStat_SubXactStatus *xact_state;
126+
127+
xact_state = pgStatXactStack;
128+
if (xact_state == NULL || xact_state->nest_level != nest_level)
129+
{
130+
xact_state = (PgStat_SubXactStatus *)
131+
MemoryContextAlloc(TopTransactionContext,
132+
sizeof(PgStat_SubXactStatus));
133+
xact_state->nest_level = nest_level;
134+
xact_state->prev = pgStatXactStack;
135+
xact_state->first = NULL;
136+
pgStatXactStack = xact_state;
137+
}
138+
return xact_state;
139+
}

src/include/pgstat.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -969,13 +969,6 @@ extern void PgstatCollectorMain(int argc, char *argv[]) pg_attribute_noreturn();
969969
/* Functions for backend initialization */
970970
extern void pgstat_initialize(void);
971971

972-
/* transactional integration */
973-
extern void AtEOXact_PgStat(bool isCommit, bool parallel);
974-
extern void AtEOSubXact_PgStat(bool isCommit, int nestDepth);
975-
extern void AtPrepare_PgStat(void);
976-
extern void PostPrepare_PgStat(void);
977-
extern void pgstat_clear_snapshot(void);
978-
979972
/* Functions called from backends */
980973
extern void pgstat_report_stat(bool force);
981974
extern void pgstat_vacuum_stat(void);
@@ -986,6 +979,7 @@ extern void pgstat_reset_single_counter(Oid objectid, PgStat_Single_Reset_Type t
986979
extern void pgstat_reset_shared_counters(const char *);
987980

988981
/* stats accessors */
982+
extern void pgstat_clear_snapshot(void);
989983
extern PgStat_ArchiverStats *pgstat_fetch_stat_archiver(void);
990984
extern PgStat_BgWriterStats *pgstat_fetch_stat_bgwriter(void);
991985
extern PgStat_CheckpointerStats *pgstat_fetch_stat_checkpointer(void);
@@ -1157,6 +1151,16 @@ extern void pgstat_report_subscription_error(Oid subid, bool is_apply_error);
11571151
extern void pgstat_report_subscription_drop(Oid subid);
11581152

11591153

1154+
/*
1155+
* Functions in pgstat_xact.c
1156+
*/
1157+
1158+
extern void AtEOXact_PgStat(bool isCommit, bool parallel);
1159+
extern void AtEOSubXact_PgStat(bool isCommit, int nestDepth);
1160+
extern void AtPrepare_PgStat(void);
1161+
extern void PostPrepare_PgStat(void);
1162+
1163+
11601164
/*
11611165
* Functions in pgstat_wal.c
11621166
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy