Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*--------------------------------------------------------------------------
 *
 * test_custom_fixed_stats.c
 *      Test module for fixed-sized custom pgstats
 *
 * Copyright (c) 2025-2026, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
 *      src/test/modules/test_custom_stats/test_custom_fixed_stats.c
 *
 * -------------------------------------------------------------------------
 */
 
#include "postgres.h"
 
#include "access/htup_details.h"
#include "funcapi.h"
#include "pgstat.h"
#include "utils/builtins.h"
#include "utils/pgstat_internal.h"
#include "utils/timestamp.h"
 
PG_MODULE_MAGIC_EXT(
                    .name = "test_custom_fixed_stats",
                    .version = PG_VERSION
);
 
/* Fixed-amount custom statistics entry */
typedef struct PgStat_StatCustomFixedEntry
{
    PgStat_Counter numcalls;    /* # of times update function called */
    TimestampTz stat_reset_timestamp;
} PgStat_StatCustomFixedEntry;
 
typedef struct PgStatShared_CustomFixedEntry
{
    LWLock      lock;           /* protects counters */
    uint32      changecount;    /* for atomic reads */
    PgStat_StatCustomFixedEntry stats;  /* current counters */
    PgStat_StatCustomFixedEntry reset_offset;   /* reset baseline */
} PgStatShared_CustomFixedEntry;
 
/* Callbacks for fixed-amount statistics */
static void test_custom_stats_fixed_init_shmem_cb(void *stats);
static void test_custom_stats_fixed_reset_all_cb(TimestampTz ts);
static void test_custom_stats_fixed_snapshot_cb(void);
 
static const PgStat_KindInfo custom_stats = {
    .name = "test_custom_fixed_stats",
    .fixed_amount = true,       /* exactly one entry */
    .write_to_file = true,      /* persist to stats file */
 
    .shared_size = sizeof(PgStatShared_CustomFixedEntry),
    .shared_data_off = offsetof(PgStatShared_CustomFixedEntry, stats),
    .shared_data_len = sizeof(((PgStatShared_CustomFixedEntry *) 0)->stats),
 
    .init_shmem_cb = test_custom_stats_fixed_init_shmem_cb,
    .reset_all_cb = test_custom_stats_fixed_reset_all_cb,
    .snapshot_cb = test_custom_stats_fixed_snapshot_cb,
};
 
/*
 * Kind ID for test_custom_fixed_stats.
 */
#define PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS 26
 
/*--------------------------------------------------------------------------
 * Module initialization
 *--------------------------------------------------------------------------
 */
 
void
_PG_init(void)
{
    /* Register custom statistics kind */
    pgstat_register_kind(PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS, &custom_stats);
}
 
/*
 * test_custom_stats_fixed_init_shmem_cb
 *      Initialize shared memory structure
 */
static void
test_custom_stats_fixed_init_shmem_cb(void *stats)
{
    PgStatShared_CustomFixedEntry *stats_shmem =
        (PgStatShared_CustomFixedEntry *) stats;
 
    LWLockInitialize(&stats_shmem->lock, LWTRANCHE_PGSTATS_DATA);
}
 
/*
 * test_custom_stats_fixed_reset_all_cb
 *      Reset the fixed-sized stats
 */
static void
test_custom_stats_fixed_reset_all_cb(TimestampTz ts)
{
    PgStatShared_CustomFixedEntry *stats_shmem =
        pgstat_get_custom_shmem_data(PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS);
 
    /* see explanation above PgStatShared_Archiver for the reset protocol */
    LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
    pgstat_copy_changecounted_stats(&stats_shmem->reset_offset,
                                    &stats_shmem->stats,
                                    sizeof(stats_shmem->stats),
                                    &stats_shmem->changecount);
    stats_shmem->stats.stat_reset_timestamp = ts;
    LWLockRelease(&stats_shmem->lock);
}
 
/*
 * test_custom_stats_fixed_snapshot_cb
 *      Copy current stats to snapshot area
 */
static void
test_custom_stats_fixed_snapshot_cb(void)
{
    PgStatShared_CustomFixedEntry *stats_shmem =
        pgstat_get_custom_shmem_data(PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS);
    PgStat_StatCustomFixedEntry *stat_snap =
        pgstat_get_custom_snapshot_data(PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS);
    PgStat_StatCustomFixedEntry *reset_offset = &stats_shmem->reset_offset;
    PgStat_StatCustomFixedEntry reset;
 
    pgstat_copy_changecounted_stats(stat_snap,
                                    &stats_shmem->stats,
                                    sizeof(stats_shmem->stats),
                                    &stats_shmem->changecount);
 
    LWLockAcquire(&stats_shmem->lock, LW_SHARED);
    memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
    LWLockRelease(&stats_shmem->lock);
 
    /* Apply reset offsets */
#define FIXED_COMP(fld) stat_snap->fld -= reset.fld;
    FIXED_COMP(numcalls);
#undef FIXED_COMP
}
 
/*--------------------------------------------------------------------------
 * SQL-callable functions
 *--------------------------------------------------------------------------
 */
 
/*
 * test_custom_stats_fixed_update
 *      Increment call counter
 */
PG_FUNCTION_INFO_V1(test_custom_stats_fixed_update);
Datum
test_custom_stats_fixed_update(PG_FUNCTION_ARGS)
{
    PgStatShared_CustomFixedEntry *stats_shmem;
 
    stats_shmem = pgstat_get_custom_shmem_data(PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS);
 
    LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
 
    pgstat_begin_changecount_write(&stats_shmem->changecount);
    stats_shmem->stats.numcalls++;
    pgstat_end_changecount_write(&stats_shmem->changecount);
 
    LWLockRelease(&stats_shmem->lock);
 
    PG_RETURN_VOID();
}
 
/*
 * test_custom_stats_fixed_reset
 *      Reset statistics by calling pgstat system
 */
PG_FUNCTION_INFO_V1(test_custom_stats_fixed_reset);
Datum
test_custom_stats_fixed_reset(PG_FUNCTION_ARGS)
{
    pgstat_reset_of_kind(PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS);
 
    PG_RETURN_VOID();
}
 
/*
 * test_custom_stats_fixed_report
 *      Return current counter values
 */
PG_FUNCTION_INFO_V1(test_custom_stats_fixed_report);
Datum
test_custom_stats_fixed_report(PG_FUNCTION_ARGS)
{
    TupleDesc   tupdesc;
    Datum       values[2] = {0};
    bool        nulls[2] = {false};
    PgStat_StatCustomFixedEntry *stats;
 
    /* Take snapshot (applies reset offsets) */
    pgstat_snapshot_fixed(PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS);
    stats = pgstat_get_custom_snapshot_data(PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS);
 
    /* Build return tuple */
    tupdesc = CreateTemplateTupleDesc(2);
    TupleDescInitEntry(tupdesc, (AttrNumber) 1, "numcalls",
                       INT8OID, -1, 0);
    TupleDescInitEntry(tupdesc, (AttrNumber) 2, "stats_reset",
                       TIMESTAMPTZOID, -1, 0);
    TupleDescFinalize(tupdesc);
    BlessTupleDesc(tupdesc);
 
    values[0] = Int64GetDatum(stats->numcalls);
 
    /* Handle uninitialized timestamp (no reset yet) */
    if (stats->stat_reset_timestamp == 0)
    {
        nulls[1] = true;
    }
    else
    {
        values[1] = TimestampTzGetDatum(stats->stat_reset_timestamp);
    }
 
    /* Return as tuple */
    PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
}