PostgreSQL Source Code git master
Loading...
Searching...
No Matches
test_custom_fixed_stats.c
Go to the documentation of this file.
1/*--------------------------------------------------------------------------
2 *
3 * test_custom_fixed_stats.c
4 * Test module for fixed-sized custom pgstats
5 *
6 * Copyright (c) 2025-2026, PostgreSQL Global Development Group
7 *
8 * IDENTIFICATION
9 * src/test/modules/test_custom_stats/test_custom_fixed_stats.c
10 *
11 * -------------------------------------------------------------------------
12 */
13
14#include "postgres.h"
15
16#include "access/htup_details.h"
17#include "funcapi.h"
18#include "pgstat.h"
19#include "utils/builtins.h"
21#include "utils/timestamp.h"
22
24 .name = "test_custom_fixed_stats",
25 .version = PG_VERSION
26);
27
28/* Fixed-amount custom statistics entry */
34
36{
37 LWLock lock; /* protects counters */
38 uint32 changecount; /* for atomic reads */
39 PgStat_StatCustomFixedEntry stats; /* current counters */
42
43/* Callbacks for fixed-amount statistics */
44static void test_custom_stats_fixed_init_shmem_cb(void *stats);
47
49 .name = "test_custom_fixed_stats",
50 .fixed_amount = true, /* exactly one entry */
51 .write_to_file = true, /* persist to stats file */
52
53 .shared_size = sizeof(PgStat_StatCustomFixedEntry),
54 .shared_data_off = offsetof(PgStatShared_CustomFixedEntry, stats),
55 .shared_data_len = sizeof(((PgStatShared_CustomFixedEntry *) 0)->stats),
56
60};
61
62/*
63 * Kind ID for test_custom_fixed_stats.
64 */
65#define PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS 26
66
67/*--------------------------------------------------------------------------
68 * Module initialization
69 *--------------------------------------------------------------------------
70 */
71
72void
74{
75 /* Must be loaded via shared_preload_libraries */
77 return;
78
79 /* Register custom statistics kind */
81}
82
83/*
84 * test_custom_stats_fixed_init_shmem_cb
85 * Initialize shared memory structure
86 */
87static void
95
96/*
97 * test_custom_stats_fixed_reset_all_cb
98 * Reset the fixed-sized stats
99 */
100static void
102{
105
106 /* see explanation above PgStatShared_Archiver for the reset protocol */
109 &stats_shmem->stats,
110 sizeof(stats_shmem->stats),
111 &stats_shmem->changecount);
112 stats_shmem->stats.stat_reset_timestamp = ts;
114}
115
116/*
117 * test_custom_stats_fixed_snapshot_cb
118 * Copy current stats to snapshot area
119 */
120static void
122{
127 PgStat_StatCustomFixedEntry *reset_offset = &stats_shmem->reset_offset;
129
131 &stats_shmem->stats,
132 sizeof(stats_shmem->stats),
133 &stats_shmem->changecount);
134
136 memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
138
139 /* Apply reset offsets */
140#define FIXED_COMP(fld) stat_snap->fld -= reset.fld;
141 FIXED_COMP(numcalls);
142#undef FIXED_COMP
143}
144
145/*--------------------------------------------------------------------------
146 * SQL-callable functions
147 *--------------------------------------------------------------------------
148 */
149
150/*
151 * test_custom_stats_fixed_update
152 * Increment call counter
153 */
155Datum
172
173/*
174 * test_custom_stats_fixed_reset
175 * Reset statistics by calling pgstat system
176 */
178Datum
185
186/*
187 * test_custom_stats_fixed_report
188 * Return current counter values
189 */
191Datum
193{
194 TupleDesc tupdesc;
195 Datum values[2] = {0};
196 bool nulls[2] = {false};
198
199 /* Take snapshot (applies reset offsets) */
202
203 /* Build return tuple */
204 tupdesc = CreateTemplateTupleDesc(2);
205 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "numcalls",
206 INT8OID, -1, 0);
207 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "stats_reset",
208 TIMESTAMPTZOID, -1, 0);
209 BlessTupleDesc(tupdesc);
210
211 values[0] = Int64GetDatum(stats->numcalls);
212
213 /* Handle uninitialized timestamp (no reset yet) */
214 if (stats->stat_reset_timestamp == 0)
215 {
216 nulls[1] = true;
217 }
218 else
219 {
221 }
222
223 /* Return as tuple */
225}
int16 AttrNumber
Definition attnum.h:21
static Datum values[MAXATTR]
Definition bootstrap.c:147
uint32_t uint32
Definition c.h:558
int64 TimestampTz
Definition timestamp.h:39
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define PG_MODULE_MAGIC_EXT(...)
Definition fmgr.h:540
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_RETURN_DATUM(x)
Definition fmgr.h:354
#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 LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1176
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1793
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition lwlock.c:698
@ LW_SHARED
Definition lwlock.h:113
@ LW_EXCLUSIVE
Definition lwlock.h:112
bool process_shared_preload_libraries_in_progress
Definition miscinit.c:1786
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition pgstat.c:1070
void pgstat_reset_of_kind(PgStat_Kind kind)
Definition pgstat.c:886
void pgstat_register_kind(PgStat_Kind kind, const PgStat_KindInfo *kind_info)
Definition pgstat.c:1473
int64 PgStat_Counter
Definition pgstat.h:71
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)
static Datum Int64GetDatum(int64 X)
Definition postgres.h:423
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
void reset(void)
PgStat_StatCustomFixedEntry reset_offset
PgStat_StatCustomFixedEntry stats
const char *const name
void _PG_init(void)
#define PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS
Datum test_custom_stats_fixed_reset(PG_FUNCTION_ARGS)
static const PgStat_KindInfo custom_stats
#define FIXED_COMP(fld)
static void test_custom_stats_fixed_init_shmem_cb(void *stats)
static void test_custom_stats_fixed_snapshot_cb(void)
static void test_custom_stats_fixed_reset_all_cb(TimestampTz ts)
Datum test_custom_stats_fixed_update(PG_FUNCTION_ARGS)
Datum test_custom_stats_fixed_report(PG_FUNCTION_ARGS)
TupleDesc CreateTemplateTupleDesc(int natts)
Definition tupdesc.c:165
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition tupdesc.c:825
static Datum TimestampTzGetDatum(TimestampTz X)
Definition timestamp.h:52
const char * name