PostgreSQL Source Code git master
test_custom_fixed_stats.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "funcapi.h"
#include "pgstat.h"
#include "utils/builtins.h"
#include "utils/pgstat_internal.h"
Include dependency graph for test_custom_fixed_stats.c:

Go to the source code of this file.

Data Structures

struct  PgStat_StatCustomFixedEntry
 
struct  PgStatShared_CustomFixedEntry
 

Macros

#define PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS   26
 
#define FIXED_COMP(fld)   stat_snap->fld -= reset.fld;
 

Typedefs

typedef struct PgStat_StatCustomFixedEntry PgStat_StatCustomFixedEntry
 
typedef struct PgStatShared_CustomFixedEntry PgStatShared_CustomFixedEntry
 

Functions

 PG_MODULE_MAGIC_EXT (.name="test_custom_fixed_stats",.version=PG_VERSION)
 
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)
 
void _PG_init (void)
 
 PG_FUNCTION_INFO_V1 (test_custom_stats_fixed_update)
 
Datum test_custom_stats_fixed_update (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (test_custom_stats_fixed_reset)
 
Datum test_custom_stats_fixed_reset (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (test_custom_stats_fixed_report)
 
Datum test_custom_stats_fixed_report (PG_FUNCTION_ARGS)
 

Variables

static const PgStat_KindInfo custom_stats
 

Macro Definition Documentation

◆ FIXED_COMP

#define FIXED_COMP (   fld)    stat_snap->fld -= reset.fld;

◆ PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS

#define PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS   26

Definition at line 64 of file test_custom_fixed_stats.c.

Typedef Documentation

◆ PgStat_StatCustomFixedEntry

◆ PgStatShared_CustomFixedEntry

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 72 of file test_custom_fixed_stats.c.

73{
74 /* Must be loaded via shared_preload_libraries */
76 return;
77
78 /* Register custom statistics kind */
80}
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
#define PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS
static const PgStat_KindInfo custom_stats

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

◆ PG_FUNCTION_INFO_V1() [1/3]

PG_FUNCTION_INFO_V1 ( test_custom_stats_fixed_report  )

◆ PG_FUNCTION_INFO_V1() [2/3]

PG_FUNCTION_INFO_V1 ( test_custom_stats_fixed_reset  )

◆ PG_FUNCTION_INFO_V1() [3/3]

PG_FUNCTION_INFO_V1 ( test_custom_stats_fixed_update  )

◆ PG_MODULE_MAGIC_EXT()

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

◆ test_custom_stats_fixed_init_shmem_cb()

static void test_custom_stats_fixed_init_shmem_cb ( void *  stats)
static

Definition at line 87 of file test_custom_fixed_stats.c.

88{
91
92 LWLockInitialize(&stats_shmem->lock, LWTRANCHE_PGSTATS_DATA);
93}
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:698

References PgStatShared_CustomFixedEntry::lock, and LWLockInitialize().

◆ test_custom_stats_fixed_report()

Datum test_custom_stats_fixed_report ( PG_FUNCTION_ARGS  )

Definition at line 191 of file test_custom_fixed_stats.c.

192{
193 TupleDesc tupdesc;
194 Datum values[2] = {0};
195 bool nulls[2] = {false};
197
198 /* Take snapshot (applies reset offsets) */
201
202 /* Build return tuple */
203 tupdesc = CreateTemplateTupleDesc(2);
204 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "numcalls",
205 INT8OID, -1, 0);
206 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "stats_reset",
207 TIMESTAMPTZOID, -1, 0);
208 BlessTupleDesc(tupdesc);
209
210 values[0] = Int64GetDatum(stats->numcalls);
211
212 /* Handle uninitialized timestamp (no reset yet) */
213 if (stats->stat_reset_timestamp == 0)
214 {
215 nulls[1] = true;
216 }
217 else
218 {
220 }
221
222 /* Return as tuple */
224}
int16 AttrNumber
Definition: attnum.h:21
static Datum values[MAXATTR]
Definition: bootstrap.c:153
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2260
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
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
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1060
static void * pgstat_get_custom_snapshot_data(PgStat_Kind kind)
static Datum Int64GetDatum(int64 X)
Definition: postgres.h:403
uint64_t Datum
Definition: postgres.h:70
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:182
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:842
static Datum TimestampTzGetDatum(TimestampTz X)
Definition: timestamp.h:52

References BlessTupleDesc(), CreateTemplateTupleDesc(), heap_form_tuple(), HeapTupleGetDatum(), Int64GetDatum(), PgStat_StatCustomFixedEntry::numcalls, PG_RETURN_DATUM, pgstat_get_custom_snapshot_data(), PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS, pgstat_snapshot_fixed(), PgStat_StatCustomFixedEntry::stat_reset_timestamp, TimestampTzGetDatum(), TupleDescInitEntry(), and values.

◆ test_custom_stats_fixed_reset()

Datum test_custom_stats_fixed_reset ( PG_FUNCTION_ARGS  )

Definition at line 178 of file test_custom_fixed_stats.c.

179{
181
183}
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void pgstat_reset_of_kind(PgStat_Kind kind)
Definition: pgstat.c:876

References PG_RETURN_VOID, PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS, and pgstat_reset_of_kind().

◆ test_custom_stats_fixed_reset_all_cb()

static void test_custom_stats_fixed_reset_all_cb ( TimestampTz  ts)
static

Definition at line 100 of file test_custom_fixed_stats.c.

101{
102 PgStatShared_CustomFixedEntry *stats_shmem =
104
105 /* see explanation above PgStatShared_Archiver for the reset protocol */
106 LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
108 &stats_shmem->stats,
109 sizeof(stats_shmem->stats),
110 &stats_shmem->changecount);
111 stats_shmem->stats.stat_reset_timestamp = ts;
112 LWLockRelease(&stats_shmem->lock);
113}
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1174
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1894
@ LW_EXCLUSIVE
Definition: lwlock.h:112
static void * pgstat_get_custom_shmem_data(PgStat_Kind kind)
static void pgstat_copy_changecounted_stats(void *dst, void *src, size_t len, uint32 *cc)
PgStat_StatCustomFixedEntry reset_offset
PgStat_StatCustomFixedEntry stats

References PgStatShared_CustomFixedEntry::changecount, PgStatShared_CustomFixedEntry::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), pgstat_copy_changecounted_stats(), pgstat_get_custom_shmem_data(), PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS, PgStatShared_CustomFixedEntry::reset_offset, PgStat_StatCustomFixedEntry::stat_reset_timestamp, and PgStatShared_CustomFixedEntry::stats.

◆ test_custom_stats_fixed_snapshot_cb()

static void test_custom_stats_fixed_snapshot_cb ( void  )
static

Definition at line 120 of file test_custom_fixed_stats.c.

121{
122 PgStatShared_CustomFixedEntry *stats_shmem =
124 PgStat_StatCustomFixedEntry *stat_snap =
126 PgStat_StatCustomFixedEntry *reset_offset = &stats_shmem->reset_offset;
128
130 &stats_shmem->stats,
131 sizeof(stats_shmem->stats),
132 &stats_shmem->changecount);
133
134 LWLockAcquire(&stats_shmem->lock, LW_SHARED);
135 memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
136 LWLockRelease(&stats_shmem->lock);
137
138 /* Apply reset offsets */
139#define FIXED_COMP(fld) stat_snap->fld -= reset.fld;
140 FIXED_COMP(numcalls);
141#undef FIXED_COMP
142}
@ LW_SHARED
Definition: lwlock.h:113
void reset(void)
Definition: sql-declare.c:600
#define FIXED_COMP(fld)

References PgStatShared_CustomFixedEntry::changecount, FIXED_COMP, PgStatShared_CustomFixedEntry::lock, LW_SHARED, LWLockAcquire(), LWLockRelease(), pgstat_copy_changecounted_stats(), pgstat_get_custom_shmem_data(), pgstat_get_custom_snapshot_data(), PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS, reset(), PgStatShared_CustomFixedEntry::reset_offset, and PgStatShared_CustomFixedEntry::stats.

◆ test_custom_stats_fixed_update()

Variable Documentation

◆ custom_stats

const PgStat_KindInfo custom_stats
static
Initial value:
= {
.name = "test_custom_fixed_stats",
.fixed_amount = true,
.write_to_file = true,
.shared_size = sizeof(PgStat_StatCustomFixedEntry),
.shared_data_off = offsetof(PgStatShared_CustomFixedEntry, stats),
.shared_data_len = sizeof(((PgStatShared_CustomFixedEntry *) 0)->stats),
}
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)
struct PgStat_StatCustomFixedEntry PgStat_StatCustomFixedEntry

Definition at line 47 of file test_custom_fixed_stats.c.

Referenced by _PG_init().