PostgreSQL Source Code git master
injection_stats_fixed.c
Go to the documentation of this file.
1/*--------------------------------------------------------------------------
2 *
3 * injection_stats_fixed.c
4 * Code for fixed-numbered statistics of injection points.
5 *
6 * Portions Copyright (c) 1996-2025, 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"
25
26/* Structures for statistics of injection points, fixed-size */
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 PgStat_Counter numcached; /* number of points cached */
33 PgStat_Counter numloaded; /* number of points loaded */
36
38{
39 LWLock lock; /* protects all the counters */
44
45/* Callbacks for fixed-numbered stats */
46static void injection_stats_fixed_init_shmem_cb(void *stats);
48static void injection_stats_fixed_snapshot_cb(void);
49
51 .name = "injection_points_fixed",
52 .fixed_amount = true,
53 .write_to_file = true,
54
55 .shared_size = sizeof(PgStat_StatInjFixedEntry),
56 .shared_data_off = offsetof(PgStatShared_InjectionPointFixed, stats),
57 .shared_data_len = sizeof(((PgStatShared_InjectionPointFixed *) 0)->stats),
58
62};
63
64/*
65 * Kind ID reserved for statistics of injection points.
66 */
67#define PGSTAT_KIND_INJECTION_FIXED 130
68
69/* Track if fixed-numbered stats are loaded */
70static bool inj_fixed_loaded = false;
71
72static void
74{
77
79}
80
81static void
83{
86
87 LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
89 &stats_shmem->stats,
90 sizeof(stats_shmem->stats),
91 &stats_shmem->changecount);
92 stats_shmem->stats.stat_reset_timestamp = ts;
93 LWLockRelease(&stats_shmem->lock);
94}
95
96static void
98{
101 PgStat_StatInjFixedEntry *stat_snap =
103 PgStat_StatInjFixedEntry *reset_offset = &stats_shmem->reset_offset;
105
107 &stats_shmem->stats,
108 sizeof(stats_shmem->stats),
109 &stats_shmem->changecount);
110
111 LWLockAcquire(&stats_shmem->lock, LW_SHARED);
112 memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
113 LWLockRelease(&stats_shmem->lock);
114
115 /* compensate by reset offsets */
116#define FIXED_COMP(fld) stat_snap->fld -= reset.fld;
117 FIXED_COMP(numattach);
118 FIXED_COMP(numdetach);
119 FIXED_COMP(numrun);
120 FIXED_COMP(numcached);
121 FIXED_COMP(numloaded);
122#undef FIXED_COMP
123}
124
125/*
126 * Workhorse to do the registration work, called in _PG_init().
127 */
128void
130{
132
133 /* mark stats as loaded */
134 inj_fixed_loaded = true;
135}
136
137/*
138 * Report fixed number of statistics for an injection point.
139 */
140void
142 uint32 numdetach,
143 uint32 numrun,
144 uint32 numcached,
145 uint32 numloaded)
146{
148
149 /* leave if disabled */
151 return;
152
154
156 stats_shmem->stats.numattach += numattach;
157 stats_shmem->stats.numdetach += numdetach;
158 stats_shmem->stats.numrun += numrun;
159 stats_shmem->stats.numcached += numcached;
160 stats_shmem->stats.numloaded += numloaded;
162}
163
164/*
165 * SQL function returning fixed-numbered statistics for injection points.
166 */
168Datum
170{
171 TupleDesc tupdesc;
172 Datum values[5] = {0};
173 bool nulls[5] = {0};
175
178
181
182 /* Initialise attributes information in the tuple descriptor */
183 tupdesc = CreateTemplateTupleDesc(5);
184 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "numattach",
185 INT8OID, -1, 0);
186 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "numdetach",
187 INT8OID, -1, 0);
188 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "numrun",
189 INT8OID, -1, 0);
190 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "numcached",
191 INT8OID, -1, 0);
192 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "numloaded",
193 INT8OID, -1, 0);
194 BlessTupleDesc(tupdesc);
195
196 values[0] = Int64GetDatum(stats->numattach);
197 values[1] = Int64GetDatum(stats->numdetach);
198 values[2] = Int64GetDatum(stats->numrun);
199 values[3] = Int64GetDatum(stats->numcached);
200 values[4] = Int64GetDatum(stats->numloaded);
201 nulls[0] = false;
202 nulls[1] = false;
203 nulls[2] = false;
204 nulls[3] = false;
205 nulls[4] = false;
206
207 /* Returns the record as Datum */
209}
int16 AttrNumber
Definition: attnum.h:21
static Datum values[MAXATTR]
Definition: bootstrap.c:151
uint32_t uint32
Definition: c.h:502
int64 TimestampTz
Definition: timestamp.h:39
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2258
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
bool inj_stats_enabled
struct PgStat_StatInjFixedEntry PgStat_StatInjFixedEntry
static void injection_stats_fixed_reset_all_cb(TimestampTz ts)
static bool inj_fixed_loaded
void pgstat_register_inj_fixed(void)
#define FIXED_COMP(fld)
Datum injection_points_stats_fixed(PG_FUNCTION_ARGS)
#define PGSTAT_KIND_INJECTION_FIXED
struct PgStatShared_InjectionPointFixed PgStatShared_InjectionPointFixed
void pgstat_report_inj_fixed(uint32 numattach, uint32 numdetach, uint32 numrun, uint32 numcached, uint32 numloaded)
static const PgStat_KindInfo injection_stats_fixed
static void injection_stats_fixed_snapshot_cb(void)
static void injection_stats_fixed_init_shmem_cb(void *stats)
PG_FUNCTION_INFO_V1(injection_points_stats_fixed)
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1179
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1899
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:718
@ LWTRANCHE_PGSTATS_DATA
Definition: lwlock.h:208
@ LW_SHARED
Definition: lwlock.h:115
@ LW_EXCLUSIVE
Definition: lwlock.h:114
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1078
void pgstat_register_kind(PgStat_Kind kind, const PgStat_KindInfo *kind_info)
Definition: pgstat.c:1481
int64 PgStat_Counter
Definition: pgstat.h:65
static void * pgstat_get_custom_snapshot_data(PgStat_Kind kind)
static void * pgstat_get_custom_shmem_data(PgStat_Kind kind)
static void pgstat_end_changecount_write(uint32 *cc)
static void pgstat_begin_changecount_write(uint32 *cc)
static void pgstat_copy_changecounted_stats(void *dst, void *src, size_t len, uint32 *cc)
uintptr_t Datum
Definition: postgres.h:69
void reset(void)
Definition: sql-declare.c:600
Definition: lwlock.h:42
PgStat_StatInjFixedEntry reset_offset
const char *const name
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:164
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:801