PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
injection_stats.c
Go to the documentation of this file.
1/*--------------------------------------------------------------------------
2 *
3 * injection_stats.c
4 * Code for 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.c
11 *
12 * -------------------------------------------------------------------------
13 */
14
15#include "postgres.h"
16
17#include "fmgr.h"
18
19#include "common/hashfn.h"
20#include "injection_stats.h"
21#include "pgstat.h"
22#include "utils/builtins.h"
24
25/* Structures for statistics of injection points */
26typedef struct PgStat_StatInjEntry
27{
28 PgStat_Counter numcalls; /* number of times point has been run */
30
32{
36
37static bool injection_stats_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
38
40 .name = "injection_points",
41 .fixed_amount = false, /* Bounded by the number of points */
42 .write_to_file = true,
43 .track_entry_count = true,
44
45 /* Injection points are system-wide */
46 .accessed_across_databases = true,
47
48 .shared_size = sizeof(PgStatShared_InjectionPoint),
49 .shared_data_off = offsetof(PgStatShared_InjectionPoint, stats),
50 .shared_data_len = sizeof(((PgStatShared_InjectionPoint *) 0)->stats),
51 .pending_size = sizeof(PgStat_StatInjEntry),
52 .flush_pending_cb = injection_stats_flush_cb,
53};
54
55/*
56 * Compute stats entry idx from point name with an 8-byte hash.
57 */
58#define PGSTAT_INJ_IDX(name) hash_bytes_extended((const unsigned char *) name, strlen(name), 0)
59
60/*
61 * Kind ID reserved for statistics of injection points.
62 */
63#define PGSTAT_KIND_INJECTION 25
64
65/* Track if stats are loaded */
66static bool inj_stats_loaded = false;
67
68/*
69 * Callback for stats handling
70 */
71static bool
73{
74 PgStat_StatInjEntry *localent;
76
77 localent = (PgStat_StatInjEntry *) entry_ref->pending;
78 shfuncent = (PgStatShared_InjectionPoint *) entry_ref->shared_stats;
79
80 if (!pgstat_lock_entry(entry_ref, nowait))
81 return false;
82
83 shfuncent->stats.numcalls += localent->numcalls;
84
85 pgstat_unlock_entry(entry_ref);
86
87 return true;
88}
89
90/*
91 * Support function for the SQL-callable pgstat* functions. Returns
92 * a pointer to the injection point statistics struct.
93 */
96{
97 PgStat_StatInjEntry *entry = NULL;
98
100 return NULL;
101
102 /* Compile the lookup key as a hash of the point name */
106 return entry;
107}
108
109/*
110 * Workhorse to do the registration work, called in _PG_init().
111 */
112void
114{
116
117 /* mark stats as loaded */
118 inj_stats_loaded = true;
119}
120
121/*
122 * Report injection point creation.
123 */
124void
126{
127 PgStat_EntryRef *entry_ref;
129
130 /* leave if disabled */
132 return;
133
135 PGSTAT_INJ_IDX(name), NULL);
136
137 shstatent = (PgStatShared_InjectionPoint *) entry_ref->shared_stats;
138
139 /* initialize shared memory data */
140 memset(&shstatent->stats, 0, sizeof(shstatent->stats));
141}
142
143/*
144 * Report injection point drop.
145 */
146void
148{
149 /* leave if disabled */
151 return;
152
156}
157
158/*
159 * Report statistics for injection point.
160 *
161 * This is simple because the set of stats to report currently is simple:
162 * track the number of times a point has been run.
163 */
164void
166{
167 PgStat_EntryRef *entry_ref;
168 PgStat_StatInjEntry *pending;
169
170 /* leave if disabled */
172 return;
173
175 PGSTAT_INJ_IDX(name), NULL);
176
177 pending = (PgStat_StatInjEntry *) entry_ref->pending;
178
179 /* Update the injection point pending statistics */
180 pending->numcalls++;
181}
182
183/*
184 * SQL function returning the number of times an injection point
185 * has been called.
186 */
188Datum
190{
193
194 if (entry == NULL)
196
198}
199
200/*
201 * SQL function returning the number of entries allocated for injection
202 * points in the shared hashtable of pgstats.
203 */
205Datum
207{
209}
210
211/* Only used by injection_points_stats_drop() */
212static bool
214{
215 return entry->key.kind == PGSTAT_KIND_INJECTION;
216}
217
218/*
219 * SQL function that drops all injection point statistics.
220 */
222Datum
224{
226
228}
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
bool inj_stats_enabled
void pgstat_report_inj(const char *name)
void pgstat_register_inj(void)
static bool inj_stats_loaded
Datum injection_points_stats_count(PG_FUNCTION_ARGS)
#define PGSTAT_INJ_IDX(name)
void pgstat_create_inj(const char *name)
struct PgStat_StatInjEntry PgStat_StatInjEntry
void pgstat_drop_inj(const char *name)
#define PGSTAT_KIND_INJECTION
PG_FUNCTION_INFO_V1(injection_points_stats_numcalls)
static bool match_inj_entries(PgStatShared_HashEntry *entry, Datum match_data)
static bool injection_stats_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
static const PgStat_KindInfo injection_stats
struct PgStatShared_InjectionPoint PgStatShared_InjectionPoint
Datum injection_points_stats_drop(PG_FUNCTION_ARGS)
Datum injection_points_stats_numcalls(PG_FUNCTION_ARGS)
static PgStat_StatInjEntry * pgstat_fetch_stat_injentry(const char *name)
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *created_entry)
Definition: pgstat.c:1265
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:934
void pgstat_register_kind(PgStat_Kind kind, const PgStat_KindInfo *kind_info)
Definition: pgstat.c:1463
int64 PgStat_Counter
Definition: pgstat.h:66
static uint64 pgstat_get_entry_count(PgStat_Kind kind)
void pgstat_request_entry_refs_gc(void)
Definition: pgstat_shmem.c:745
bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
void pgstat_drop_matching_entries(bool(*do_drop)(PgStatShared_HashEntry *, Datum), Datum match_data)
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:720
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:690
uint64_t Datum
Definition: postgres.h:70
#define InvalidOid
Definition: postgres_ext.h:37
PgStat_StatInjEntry stats
PgStatShared_Common header
PgStatShared_Common * shared_stats
PgStat_Kind kind
const char *const name
PgStat_Counter numcalls
char * text_to_cstring(const text *t)
Definition: varlena.c:214
const char * name