PostgreSQL Source Code git master
test_custom_var_stats.c File Reference
#include "postgres.h"
#include "common/hashfn.h"
#include "funcapi.h"
#include "utils/builtins.h"
#include "utils/pgstat_internal.h"
Include dependency graph for test_custom_var_stats.c:

Go to the source code of this file.

Data Structures

struct  PgStat_StatCustomVarEntry
 
struct  PgStatShared_CustomVarEntry
 

Macros

#define PGSTAT_KIND_TEST_CUSTOM_VAR_STATS   25
 
#define PGSTAT_CUSTOM_VAR_STATS_IDX(name)   hash_bytes_extended((const unsigned char *) name, strlen(name), 0)
 

Typedefs

typedef struct PgStat_StatCustomVarEntry PgStat_StatCustomVarEntry
 
typedef struct PgStatShared_CustomVarEntry PgStatShared_CustomVarEntry
 

Functions

 PG_MODULE_MAGIC_EXT (.name="test_custom_var_stats",.version=PG_VERSION)
 
static bool test_custom_stats_var_flush_pending_cb (PgStat_EntryRef *entry_ref, bool nowait)
 
void _PG_init (void)
 
static PgStat_StatCustomVarEntrytest_custom_stats_var_fetch_entry (const char *stat_name)
 
 PG_FUNCTION_INFO_V1 (test_custom_stats_var_create)
 
Datum test_custom_stats_var_create (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (test_custom_stats_var_update)
 
Datum test_custom_stats_var_update (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (test_custom_stats_var_drop)
 
Datum test_custom_stats_var_drop (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (test_custom_stats_var_report)
 
Datum test_custom_stats_var_report (PG_FUNCTION_ARGS)
 

Variables

static const PgStat_KindInfo custom_stats
 

Macro Definition Documentation

◆ PGSTAT_CUSTOM_VAR_STATS_IDX

#define PGSTAT_CUSTOM_VAR_STATS_IDX (   name)    hash_bytes_extended((const unsigned char *) name, strlen(name), 0)

Definition at line 38 of file test_custom_var_stats.c.

◆ PGSTAT_KIND_TEST_CUSTOM_VAR_STATS

#define PGSTAT_KIND_TEST_CUSTOM_VAR_STATS   25

Definition at line 33 of file test_custom_var_stats.c.

Typedef Documentation

◆ PgStat_StatCustomVarEntry

◆ PgStatShared_CustomVarEntry

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 91 of file test_custom_var_stats.c.

92{
93 /* Must be loaded via shared_preload_libraries */
95 return;
96
97 /* Register custom statistics kind */
99}
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1786
void pgstat_register_kind(PgStat_Kind kind, const PgStat_KindInfo *kind_info)
Definition: pgstat.c:1463
static const PgStat_KindInfo custom_stats
#define PGSTAT_KIND_TEST_CUSTOM_VAR_STATS

References custom_stats, PGSTAT_KIND_TEST_CUSTOM_VAR_STATS, pgstat_register_kind(), and process_shared_preload_libraries_in_progress.

◆ PG_FUNCTION_INFO_V1() [1/4]

PG_FUNCTION_INFO_V1 ( test_custom_stats_var_create  )

◆ PG_FUNCTION_INFO_V1() [2/4]

PG_FUNCTION_INFO_V1 ( test_custom_stats_var_drop  )

◆ PG_FUNCTION_INFO_V1() [3/4]

PG_FUNCTION_INFO_V1 ( test_custom_stats_var_report  )

◆ PG_FUNCTION_INFO_V1() [4/4]

PG_FUNCTION_INFO_V1 ( test_custom_stats_var_update  )

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "test_custom_var_stats",
version = PG_VERSION 
)

◆ test_custom_stats_var_create()

Datum test_custom_stats_var_create ( PG_FUNCTION_ARGS  )

Definition at line 170 of file test_custom_var_stats.c.

171{
172 PgStat_EntryRef *entry_ref;
173 PgStatShared_CustomVarEntry *shared_entry;
174 char *stat_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
175
176 /* Validate name length first */
177 if (strlen(stat_name) >= NAMEDATALEN)
179 (errcode(ERRCODE_NAME_TOO_LONG),
180 errmsg("custom statistic name \"%s\" is too long", stat_name),
181 errdetail("Name must be less than %d characters.", NAMEDATALEN)));
182
183 /* Create or get existing entry */
185 PGSTAT_CUSTOM_VAR_STATS_IDX(stat_name), true);
186
187 if (!entry_ref)
189
190 shared_entry = (PgStatShared_CustomVarEntry *) entry_ref->shared_stats;
191
192 /* Zero-initialize statistics */
193 memset(&shared_entry->stats, 0, sizeof(shared_entry->stats));
194
195 pgstat_unlock_entry(entry_ref);
196
198}
int errdetail(const char *fmt,...)
Definition: elog.c:1216
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define NAMEDATALEN
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:720
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid, bool nowait)
Definition: pgstat_shmem.c:729
#define InvalidOid
Definition: postgres_ext.h:37
PgStat_StatCustomVarEntry stats
PgStatShared_Common * shared_stats
#define PGSTAT_CUSTOM_VAR_STATS_IDX(name)
char * text_to_cstring(const text *t)
Definition: varlena.c:214

References ereport, errcode(), errdetail(), errmsg(), ERROR, InvalidOid, NAMEDATALEN, PG_GETARG_TEXT_PP, PG_RETURN_VOID, PGSTAT_CUSTOM_VAR_STATS_IDX, pgstat_get_entry_ref_locked(), PGSTAT_KIND_TEST_CUSTOM_VAR_STATS, pgstat_unlock_entry(), PgStat_EntryRef::shared_stats, PgStatShared_CustomVarEntry::stats, and text_to_cstring().

◆ test_custom_stats_var_drop()

Datum test_custom_stats_var_drop ( PG_FUNCTION_ARGS  )

Definition at line 234 of file test_custom_var_stats.c.

235{
236 char *stat_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
237
238 /* Drop entry and request GC if the entry could not be freed */
240 PGSTAT_CUSTOM_VAR_STATS_IDX(stat_name)))
242
244}
void pgstat_request_entry_refs_gc(void)
Definition: pgstat_shmem.c:745
bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid)

References InvalidOid, PG_GETARG_TEXT_PP, PG_RETURN_VOID, PGSTAT_CUSTOM_VAR_STATS_IDX, pgstat_drop_entry(), PGSTAT_KIND_TEST_CUSTOM_VAR_STATS, pgstat_request_entry_refs_gc(), and text_to_cstring().

◆ test_custom_stats_var_fetch_entry()

static PgStat_StatCustomVarEntry * test_custom_stats_var_fetch_entry ( const char *  stat_name)
static

Definition at line 147 of file test_custom_var_stats.c.

148{
149 /* Fetch entry by hashed name */
153 PGSTAT_CUSTOM_VAR_STATS_IDX(stat_name));
154}
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:934

References InvalidOid, PGSTAT_CUSTOM_VAR_STATS_IDX, pgstat_fetch_entry(), and PGSTAT_KIND_TEST_CUSTOM_VAR_STATS.

Referenced by test_custom_stats_var_report().

◆ test_custom_stats_var_flush_pending_cb()

static bool test_custom_stats_var_flush_pending_cb ( PgStat_EntryRef entry_ref,
bool  nowait 
)
static

Definition at line 116 of file test_custom_var_stats.c.

117{
118 PgStat_StatCustomVarEntry *pending_entry;
119 PgStatShared_CustomVarEntry *shared_entry;
120
121 pending_entry = (PgStat_StatCustomVarEntry *) entry_ref->pending;
122 shared_entry = (PgStatShared_CustomVarEntry *) entry_ref->shared_stats;
123
124 if (!pgstat_lock_entry(entry_ref, nowait))
125 return false;
126
127 /* Add pending counts to shared totals */
128 shared_entry->stats.numcalls += pending_entry->numcalls;
129
130 pgstat_unlock_entry(entry_ref);
131
132 return true;
133}
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:690

References PgStat_StatCustomVarEntry::numcalls, PgStat_EntryRef::pending, pgstat_lock_entry(), pgstat_unlock_entry(), PgStat_EntryRef::shared_stats, and PgStatShared_CustomVarEntry::stats.

◆ test_custom_stats_var_report()

Datum test_custom_stats_var_report ( PG_FUNCTION_ARGS  )

Definition at line 255 of file test_custom_var_stats.c.

256{
257 FuncCallContext *funcctx;
258 char *stat_name;
259 PgStat_StatCustomVarEntry *stat_entry;
260
261 if (SRF_IS_FIRSTCALL())
262 {
263 TupleDesc tupdesc;
264 MemoryContext oldcontext;
265
266 /* Initialize SRF context */
267 funcctx = SRF_FIRSTCALL_INIT();
268 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
269
270 /* Get composite return type */
271 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
272 elog(ERROR, "test_custom_stats_var_report: return type is not composite");
273
274 funcctx->tuple_desc = BlessTupleDesc(tupdesc);
275 funcctx->max_calls = 1; /* single row result */
276
277 MemoryContextSwitchTo(oldcontext);
278 }
279
280 funcctx = SRF_PERCALL_SETUP();
281
282 if (funcctx->call_cntr < funcctx->max_calls)
283 {
284 Datum values[2];
285 bool nulls[2] = {false, false};
286 HeapTuple tuple;
287
288 stat_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
289 stat_entry = test_custom_stats_var_fetch_entry(stat_name);
290
291 /* Return row only if entry exists */
292 if (stat_entry)
293 {
294 values[0] = PointerGetDatum(cstring_to_text(stat_name));
295 values[1] = Int64GetDatum(stat_entry->numcalls);
296
297 tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
298 SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
299 }
300 }
301
302 SRF_RETURN_DONE(funcctx);
303}
static Datum values[MAXATTR]
Definition: bootstrap.c:153
#define elog(elevel,...)
Definition: elog.h:226
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2260
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
static Datum Int64GetDatum(int64 X)
Definition: postgres.h:403
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
uint64_t Datum
Definition: postgres.h:70
uint64 max_calls
Definition: funcapi.h:74
uint64 call_cntr
Definition: funcapi.h:65
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
TupleDesc tuple_desc
Definition: funcapi.h:112
static PgStat_StatCustomVarEntry * test_custom_stats_var_fetch_entry(const char *stat_name)
text * cstring_to_text(const char *s)
Definition: varlena.c:181

References BlessTupleDesc(), FuncCallContext::call_cntr, cstring_to_text(), elog, ERROR, get_call_result_type(), heap_form_tuple(), HeapTupleGetDatum(), Int64GetDatum(), FuncCallContext::max_calls, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, PgStat_StatCustomVarEntry::numcalls, PG_GETARG_TEXT_PP, PointerGetDatum(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, test_custom_stats_var_fetch_entry(), text_to_cstring(), FuncCallContext::tuple_desc, TYPEFUNC_COMPOSITE, and values.

◆ test_custom_stats_var_update()

Datum test_custom_stats_var_update ( PG_FUNCTION_ARGS  )

Definition at line 209 of file test_custom_var_stats.c.

210{
211 char *stat_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
212 PgStat_EntryRef *entry_ref;
213 PgStat_StatCustomVarEntry *pending_entry;
214
215 /* Get pending entry in local memory */
217 PGSTAT_CUSTOM_VAR_STATS_IDX(stat_name), NULL);
218
219 pending_entry = (PgStat_StatCustomVarEntry *) entry_ref->pending;
220 pending_entry->numcalls++;
221
223}
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *created_entry)
Definition: pgstat.c:1265

References InvalidOid, PgStat_StatCustomVarEntry::numcalls, PgStat_EntryRef::pending, PG_GETARG_TEXT_PP, PG_RETURN_VOID, PGSTAT_CUSTOM_VAR_STATS_IDX, PGSTAT_KIND_TEST_CUSTOM_VAR_STATS, pgstat_prep_pending_entry(), and text_to_cstring().

Variable Documentation

◆ custom_stats

const PgStat_KindInfo custom_stats
static
Initial value:
= {
.name = "test_custom_var_stats",
.fixed_amount = false,
.write_to_file = true,
.track_entry_count = true,
.accessed_across_databases = true,
.shared_size = sizeof(PgStatShared_CustomVarEntry),
.shared_data_off = offsetof(PgStatShared_CustomVarEntry, stats),
.shared_data_len = sizeof(((PgStatShared_CustomVarEntry *) 0)->stats),
.pending_size = sizeof(PgStat_StatCustomVarEntry),
}
struct PgStatShared_CustomVarEntry PgStatShared_CustomVarEntry
static bool test_custom_stats_var_flush_pending_cb(PgStat_EntryRef *entry_ref, bool nowait)

Definition at line 72 of file test_custom_var_stats.c.

Referenced by _PG_init().