Skip to content

Commit f68cd84

Browse files
committed
injection_points: Add some fixed-numbered statistics
Like 7553443, this acts mainly as a template to show what can be achieved with fixed-numbered stats (like WAL, bgwriter, etc.) with the pluggable cumulative statistics APIs introduced in 7949d95. Fixed-numbered stats are defined in their own file, named injection_stats_fixed.c, separated entirely from the variable-numbered case in injection_stats.c. This is mainly for clarity as having both examples in the same file would be confusing. Note that this commit uses the helper routines added in 2eff9e6. The stats stored track globally the number of times injection points have been attached, detached or run. Two more fields should be added later for the number of times a point has been cached or loaded, but what's here is enough as a template. More TAP tests are added, providing coverage for fixed-numbered custom stats. Author: Michael Paquier Reviewed-by: Dmitry Dolgov, Bertrand Drouvot Discussion: https://postgr.es/m/Zmqm9j5EO0I4W8dx@paquier.xyz
1 parent 7553443 commit f68cd84

File tree

8 files changed

+229
-2
lines changed

8 files changed

+229
-2
lines changed

src/test/modules/injection_points/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ MODULE_big = injection_points
44
OBJS = \
55
$(WIN32RES) \
66
injection_points.o \
7-
injection_stats.o
7+
injection_stats.o \
8+
injection_stats_fixed.o
89
EXTENSION = injection_points
910
DATA = injection_points--1.0.sql
1011
PGFILEDESC = "injection_points - facility for injection points"

src/test/modules/injection_points/injection_points--1.0.sql

+11
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,14 @@ CREATE FUNCTION injection_points_stats_numcalls(IN point_name TEXT)
8484
RETURNS bigint
8585
AS 'MODULE_PATHNAME', 'injection_points_stats_numcalls'
8686
LANGUAGE C STRICT;
87+
88+
--
89+
-- injection_points_stats_fixed()
90+
--
91+
-- Reports fixed-numbered statistics for injection points.
92+
CREATE FUNCTION injection_points_stats_fixed(OUT numattach int8,
93+
OUT numdetach int8,
94+
OUT numrun int8)
95+
RETURNS record
96+
AS 'MODULE_PATHNAME', 'injection_points_stats_fixed'
97+
LANGUAGE C STRICT;

src/test/modules/injection_points/injection_points.c

+4
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ injection_points_attach(PG_FUNCTION_ARGS)
297297
condition.pid = MyProcPid;
298298
}
299299

300+
pgstat_report_inj_fixed(1, 0, 0);
300301
InjectionPointAttach(name, "injection_points", function, &condition,
301302
sizeof(InjectionPointCondition));
302303

@@ -342,6 +343,7 @@ injection_points_run(PG_FUNCTION_ARGS)
342343
{
343344
char *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
344345

346+
pgstat_report_inj_fixed(0, 0, 1);
345347
INJECTION_POINT(name);
346348

347349
PG_RETURN_VOID();
@@ -432,6 +434,7 @@ injection_points_detach(PG_FUNCTION_ARGS)
432434
{
433435
char *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
434436

437+
pgstat_report_inj_fixed(0, 1, 0);
435438
if (!InjectionPointDetach(name))
436439
elog(ERROR, "could not detach injection point \"%s\"", name);
437440

@@ -459,4 +462,5 @@ _PG_init(void)
459462
return;
460463

461464
pgstat_register_inj();
465+
pgstat_register_inj_fixed();
462466
}

src/test/modules/injection_points/injection_stats.h

+7
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@
1515
#ifndef INJECTION_STATS
1616
#define INJECTION_STATS
1717

18+
/* injection_stats.c */
1819
extern void pgstat_register_inj(void);
1920
extern void pgstat_create_inj(const char *name);
2021
extern void pgstat_drop_inj(const char *name);
2122
extern void pgstat_report_inj(const char *name);
2223

24+
/* injection_stats_fixed.c */
25+
extern void pgstat_register_inj_fixed(void);
26+
extern void pgstat_report_inj_fixed(uint32 numattach,
27+
uint32 numdetach,
28+
uint32 numrun);
29+
2330
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/*--------------------------------------------------------------------------
2+
*
3+
* injection_stats_fixed.c
4+
* Code for fixed-numbered statistics of injection points.
5+
*
6+
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7+
* Portions Copyright (c) 1994, Regents of the University of California
8+
*
9+
* IDENTIFICATION
10+
* src/test/modules/injection_points/injection_stats_fixed.c
11+
*
12+
* -------------------------------------------------------------------------
13+
*/
14+
15+
#include "postgres.h"
16+
17+
#include "fmgr.h"
18+
19+
#include "common/hashfn.h"
20+
#include "funcapi.h"
21+
#include "injection_stats.h"
22+
#include "pgstat.h"
23+
#include "utils/builtins.h"
24+
#include "utils/pgstat_internal.h"
25+
26+
/* Structures for statistics of injection points, fixed-size */
27+
typedef struct PgStat_StatInjFixedEntry
28+
{
29+
PgStat_Counter numattach; /* number of points attached */
30+
PgStat_Counter numdetach; /* number of points detached */
31+
PgStat_Counter numrun; /* number of points run */
32+
TimestampTz stat_reset_timestamp;
33+
} PgStat_StatInjFixedEntry;
34+
35+
typedef struct PgStatShared_InjectionPointFixed
36+
{
37+
LWLock lock; /* protects all the counters */
38+
uint32 changecount;
39+
PgStat_StatInjFixedEntry stats;
40+
PgStat_StatInjFixedEntry reset_offset;
41+
} PgStatShared_InjectionPointFixed;
42+
43+
/* Callbacks for fixed-numbered stats */
44+
static void injection_stats_fixed_init_shmem_cb(void *stats);
45+
static void injection_stats_fixed_reset_all_cb(TimestampTz ts);
46+
static void injection_stats_fixed_snapshot_cb(void);
47+
48+
static const PgStat_KindInfo injection_stats_fixed = {
49+
.name = "injection_points_fixed",
50+
.fixed_amount = true,
51+
52+
.shared_size = sizeof(PgStat_StatInjFixedEntry),
53+
.shared_data_off = offsetof(PgStatShared_InjectionPointFixed, stats),
54+
.shared_data_len = sizeof(((PgStatShared_InjectionPointFixed *) 0)->stats),
55+
56+
.init_shmem_cb = injection_stats_fixed_init_shmem_cb,
57+
.reset_all_cb = injection_stats_fixed_reset_all_cb,
58+
.snapshot_cb = injection_stats_fixed_snapshot_cb,
59+
};
60+
61+
/*
62+
* Kind ID reserved for statistics of injection points.
63+
*/
64+
#define PGSTAT_KIND_INJECTION_FIXED 130
65+
66+
/* Track if fixed-numbered stats are loaded */
67+
static bool inj_fixed_loaded = false;
68+
69+
static void
70+
injection_stats_fixed_init_shmem_cb(void *stats)
71+
{
72+
PgStatShared_InjectionPointFixed *stats_shmem =
73+
(PgStatShared_InjectionPointFixed *) stats;
74+
75+
LWLockInitialize(&stats_shmem->lock, LWTRANCHE_PGSTATS_DATA);
76+
}
77+
78+
static void
79+
injection_stats_fixed_reset_all_cb(TimestampTz ts)
80+
{
81+
PgStatShared_InjectionPointFixed *stats_shmem =
82+
pgstat_get_custom_shmem_data(PGSTAT_KIND_INJECTION_FIXED);
83+
84+
LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
85+
pgstat_copy_changecounted_stats(&stats_shmem->reset_offset,
86+
&stats_shmem->stats,
87+
sizeof(stats_shmem->stats),
88+
&stats_shmem->changecount);
89+
stats_shmem->stats.stat_reset_timestamp = ts;
90+
LWLockRelease(&stats_shmem->lock);
91+
}
92+
93+
static void
94+
injection_stats_fixed_snapshot_cb(void)
95+
{
96+
PgStatShared_InjectionPointFixed *stats_shmem =
97+
pgstat_get_custom_shmem_data(PGSTAT_KIND_INJECTION_FIXED);
98+
PgStat_StatInjFixedEntry *stat_snap =
99+
pgstat_get_custom_snapshot_data(PGSTAT_KIND_INJECTION_FIXED);
100+
PgStat_StatInjFixedEntry *reset_offset = &stats_shmem->reset_offset;
101+
PgStat_StatInjFixedEntry reset;
102+
103+
pgstat_copy_changecounted_stats(stat_snap,
104+
&stats_shmem->stats,
105+
sizeof(stats_shmem->stats),
106+
&stats_shmem->changecount);
107+
108+
LWLockAcquire(&stats_shmem->lock, LW_SHARED);
109+
memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
110+
LWLockRelease(&stats_shmem->lock);
111+
112+
/* compensate by reset offsets */
113+
#define FIXED_COMP(fld) stat_snap->fld -= reset.fld;
114+
FIXED_COMP(numattach);
115+
FIXED_COMP(numdetach);
116+
FIXED_COMP(numrun);
117+
#undef FIXED_COMP
118+
}
119+
120+
/*
121+
* Workhorse to do the registration work, called in _PG_init().
122+
*/
123+
void
124+
pgstat_register_inj_fixed(void)
125+
{
126+
pgstat_register_kind(PGSTAT_KIND_INJECTION_FIXED, &injection_stats_fixed);
127+
128+
/* mark stats as loaded */
129+
inj_fixed_loaded = true;
130+
}
131+
132+
/*
133+
* Report fixed number of statistics for an injection point.
134+
*/
135+
void
136+
pgstat_report_inj_fixed(uint32 numattach,
137+
uint32 numdetach,
138+
uint32 numrun)
139+
{
140+
PgStatShared_InjectionPointFixed *stats_shmem;
141+
142+
/* leave if disabled */
143+
if (!inj_fixed_loaded)
144+
return;
145+
146+
stats_shmem = pgstat_get_custom_shmem_data(PGSTAT_KIND_INJECTION_FIXED);
147+
148+
pgstat_begin_changecount_write(&stats_shmem->changecount);
149+
stats_shmem->stats.numattach += numattach;
150+
stats_shmem->stats.numdetach += numdetach;
151+
stats_shmem->stats.numrun += numrun;
152+
pgstat_end_changecount_write(&stats_shmem->changecount);
153+
}
154+
155+
/*
156+
* SQL function returning fixed-numbered statistics for injection points.
157+
*/
158+
PG_FUNCTION_INFO_V1(injection_points_stats_fixed);
159+
Datum
160+
injection_points_stats_fixed(PG_FUNCTION_ARGS)
161+
{
162+
TupleDesc tupdesc;
163+
Datum values[3] = {0};
164+
bool nulls[3] = {0};
165+
PgStat_StatInjFixedEntry *stats;
166+
167+
if (!inj_fixed_loaded)
168+
PG_RETURN_NULL();
169+
170+
pgstat_snapshot_fixed(PGSTAT_KIND_INJECTION_FIXED);
171+
stats = pgstat_get_custom_snapshot_data(PGSTAT_KIND_INJECTION_FIXED);
172+
173+
/* Initialise attributes information in the tuple descriptor */
174+
tupdesc = CreateTemplateTupleDesc(3);
175+
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "numattach",
176+
INT8OID, -1, 0);
177+
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "numdetach",
178+
INT8OID, -1, 0);
179+
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "numrun",
180+
INT8OID, -1, 0);
181+
BlessTupleDesc(tupdesc);
182+
183+
values[0] = Int64GetDatum(stats->numattach);
184+
values[1] = Int64GetDatum(stats->numdetach);
185+
values[2] = Int64GetDatum(stats->numrun);
186+
nulls[0] = false;
187+
nulls[1] = false;
188+
nulls[2] = false;
189+
190+
/* Returns the record as Datum */
191+
PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
192+
}

src/test/modules/injection_points/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ endif
77
injection_points_sources = files(
88
'injection_points.c',
99
'injection_stats.c',
10+
'injection_stats_fixed.c',
1011
)
1112

1213
if host_system == 'windows'

src/test/modules/injection_points/t/001_stats.pl

+10-1
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,27 @@
3333
my $numcalls = $node->safe_psql('postgres',
3434
"SELECT injection_points_stats_numcalls('stats-notice');");
3535
is($numcalls, '2', 'number of stats calls');
36+
my $fixedstats = $node->safe_psql('postgres',
37+
"SELECT * FROM injection_points_stats_fixed();");
38+
is($fixedstats, '1|0|2', 'number of fixed stats');
3639

3740
# Restart the node cleanly, stats should still be around.
3841
$node->restart;
3942
$numcalls = $node->safe_psql('postgres',
4043
"SELECT injection_points_stats_numcalls('stats-notice');");
4144
is($numcalls, '2', 'number of stats after clean restart');
45+
$fixedstats = $node->safe_psql('postgres',
46+
"SELECT * FROM injection_points_stats_fixed();");
47+
is($fixedstats, '1|0|2', 'number of fixed stats after clean restart');
4248

4349
# On crash the stats are gone.
4450
$node->stop('immediate');
4551
$node->start;
4652
$numcalls = $node->safe_psql('postgres',
4753
"SELECT injection_points_stats_numcalls('stats-notice');");
48-
is($numcalls, '', 'number of stats after clean restart');
54+
is($numcalls, '', 'number of stats after crash');
55+
$fixedstats = $node->safe_psql('postgres',
56+
"SELECT * FROM injection_points_stats_fixed();");
57+
is($fixedstats, '0|0|0', 'number of fixed stats after crash');
4958

5059
done_testing();

src/tools/pgindent/typedefs.list

+2
Original file line numberDiff line numberDiff line change
@@ -2120,6 +2120,7 @@ PgStatShared_Database
21202120
PgStatShared_Function
21212121
PgStatShared_HashEntry
21222122
PgStatShared_InjectionPoint
2123+
PgStatShared_InjectionPointFixed
21232124
PgStatShared_IO
21242125
PgStatShared_Relation
21252126
PgStatShared_ReplSlot
@@ -2152,6 +2153,7 @@ PgStat_SnapshotEntry
21522153
PgStat_StatDBEntry
21532154
PgStat_StatFuncEntry
21542155
PgStat_StatInjEntry
2156+
PgStat_StatInjFixedEntry
21552157
PgStat_StatReplSlotEntry
21562158
PgStat_StatSubEntry
21572159
PgStat_StatTabEntry

0 commit comments

Comments
 (0)