mirror of
https://github.com/postgres/postgres.git
synced 2025-07-26 01:22:12 +03:00
Generate pg_stat_get*() functions for tables using macros
The same code pattern is repeated 17 times for int64 counters (0 for missing entry) and 5 times for timestamps (NULL for missing entry) on table entries. This code is switched to use a macro for the basic code instead, shaving a few hundred lines of originally-duplicated code. The function names remain the same, but some fields of PgStat_StatTabEntry have to be renamed to cope with the new style. Author: Bertrand Drouvot Reviewed-by: Nathan Bossart Discussion: https:/postgr.es/m/20221204173207.GA2669116@nathanxps13
This commit is contained in:
@ -36,363 +36,106 @@
|
||||
|
||||
#define HAS_PGSTAT_PERMISSIONS(role) (has_privs_of_role(GetUserId(), ROLE_PG_READ_ALL_STATS) || has_privs_of_role(GetUserId(), role))
|
||||
|
||||
Datum
|
||||
pg_stat_get_numscans(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
int64 result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = (int64) (tabentry->numscans);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
|
||||
|
||||
Datum
|
||||
pg_stat_get_lastscan(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
TimestampTz result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = tabentry->lastscan;
|
||||
|
||||
if (result == 0)
|
||||
PG_RETURN_NULL();
|
||||
else
|
||||
PG_RETURN_TIMESTAMPTZ(result);
|
||||
}
|
||||
|
||||
|
||||
Datum
|
||||
pg_stat_get_tuples_returned(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
int64 result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = (int64) (tabentry->tuples_returned);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
|
||||
|
||||
Datum
|
||||
pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
int64 result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = (int64) (tabentry->tuples_fetched);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
|
||||
|
||||
Datum
|
||||
pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
int64 result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = (int64) (tabentry->tuples_inserted);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
|
||||
|
||||
Datum
|
||||
pg_stat_get_tuples_updated(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
int64 result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = (int64) (tabentry->tuples_updated);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
|
||||
|
||||
Datum
|
||||
pg_stat_get_tuples_deleted(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
int64 result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = (int64) (tabentry->tuples_deleted);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
|
||||
|
||||
Datum
|
||||
pg_stat_get_tuples_hot_updated(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
int64 result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = (int64) (tabentry->tuples_hot_updated);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
|
||||
|
||||
Datum
|
||||
pg_stat_get_live_tuples(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
int64 result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = (int64) (tabentry->n_live_tuples);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
|
||||
|
||||
Datum
|
||||
pg_stat_get_dead_tuples(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
int64 result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = (int64) (tabentry->n_dead_tuples);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
|
||||
|
||||
Datum
|
||||
pg_stat_get_mod_since_analyze(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
int64 result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = (int64) (tabentry->changes_since_analyze);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
|
||||
|
||||
Datum
|
||||
pg_stat_get_ins_since_vacuum(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
int64 result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = (int64) (tabentry->inserts_since_vacuum);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
|
||||
|
||||
Datum
|
||||
pg_stat_get_blocks_fetched(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
int64 result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = (int64) (tabentry->blocks_fetched);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
|
||||
|
||||
Datum
|
||||
pg_stat_get_blocks_hit(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
int64 result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = (int64) (tabentry->blocks_hit);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
|
||||
Datum
|
||||
pg_stat_get_last_vacuum_time(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
TimestampTz result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = tabentry->vacuum_timestamp;
|
||||
|
||||
if (result == 0)
|
||||
PG_RETURN_NULL();
|
||||
else
|
||||
PG_RETURN_TIMESTAMPTZ(result);
|
||||
}
|
||||
|
||||
Datum
|
||||
pg_stat_get_last_autovacuum_time(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
TimestampTz result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = tabentry->autovac_vacuum_timestamp;
|
||||
|
||||
if (result == 0)
|
||||
PG_RETURN_NULL();
|
||||
else
|
||||
PG_RETURN_TIMESTAMPTZ(result);
|
||||
}
|
||||
|
||||
Datum
|
||||
pg_stat_get_last_analyze_time(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
TimestampTz result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = tabentry->analyze_timestamp;
|
||||
|
||||
if (result == 0)
|
||||
PG_RETURN_NULL();
|
||||
else
|
||||
PG_RETURN_TIMESTAMPTZ(result);
|
||||
}
|
||||
|
||||
Datum
|
||||
pg_stat_get_last_autoanalyze_time(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
TimestampTz result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = tabentry->autovac_analyze_timestamp;
|
||||
|
||||
if (result == 0)
|
||||
PG_RETURN_NULL();
|
||||
else
|
||||
PG_RETURN_TIMESTAMPTZ(result);
|
||||
}
|
||||
|
||||
Datum
|
||||
pg_stat_get_vacuum_count(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
int64 result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = (int64) (tabentry->vacuum_count);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
|
||||
Datum
|
||||
pg_stat_get_autovacuum_count(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
int64 result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = (int64) (tabentry->autovac_vacuum_count);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
|
||||
Datum
|
||||
pg_stat_get_analyze_count(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
int64 result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = (int64) (tabentry->analyze_count);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
|
||||
Datum
|
||||
pg_stat_get_autoanalyze_count(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
int64 result;
|
||||
PgStat_StatTabEntry *tabentry;
|
||||
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
||||
result = 0;
|
||||
else
|
||||
result = (int64) (tabentry->autovac_analyze_count);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
#define PG_STAT_GET_RELENTRY_INT64(stat) \
|
||||
Datum \
|
||||
CppConcat(pg_stat_get_,stat)(PG_FUNCTION_ARGS) \
|
||||
{ \
|
||||
Oid relid = PG_GETARG_OID(0); \
|
||||
int64 result; \
|
||||
PgStat_StatTabEntry *tabentry; \
|
||||
\
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL) \
|
||||
result = 0; \
|
||||
else \
|
||||
result = (int64) (tabentry->stat); \
|
||||
\
|
||||
PG_RETURN_INT64(result); \
|
||||
} \
|
||||
|
||||
/* pg_stat_get_analyze_count */
|
||||
PG_STAT_GET_RELENTRY_INT64(analyze_count);
|
||||
|
||||
/* pg_stat_get_autoanalyze_count */
|
||||
PG_STAT_GET_RELENTRY_INT64(autoanalyze_count);
|
||||
|
||||
/* pg_stat_get_autovacuum_count */
|
||||
PG_STAT_GET_RELENTRY_INT64(autovacuum_count);
|
||||
|
||||
/* pg_stat_get_blocks_fetched */
|
||||
PG_STAT_GET_RELENTRY_INT64(blocks_fetched);
|
||||
|
||||
/* pg_stat_get_blocks_hit */
|
||||
PG_STAT_GET_RELENTRY_INT64(blocks_hit);
|
||||
|
||||
/* pg_stat_get_dead_tuples */
|
||||
PG_STAT_GET_RELENTRY_INT64(dead_tuples);
|
||||
|
||||
/* pg_stat_get_ins_since_vacuum */
|
||||
PG_STAT_GET_RELENTRY_INT64(ins_since_vacuum);
|
||||
|
||||
/* pg_stat_get_live_tuples */
|
||||
PG_STAT_GET_RELENTRY_INT64(live_tuples);
|
||||
|
||||
/* pg_stat_get_mods_since_analyze */
|
||||
PG_STAT_GET_RELENTRY_INT64(mod_since_analyze);
|
||||
|
||||
/* pg_stat_get_numscans */
|
||||
PG_STAT_GET_RELENTRY_INT64(numscans);
|
||||
|
||||
/* pg_stat_get_tuples_deleted */
|
||||
PG_STAT_GET_RELENTRY_INT64(tuples_deleted);
|
||||
|
||||
/* pg_stat_get_tuples_fetched */
|
||||
PG_STAT_GET_RELENTRY_INT64(tuples_fetched);
|
||||
|
||||
/* pg_stat_get_tuples_hot_updated */
|
||||
PG_STAT_GET_RELENTRY_INT64(tuples_hot_updated);
|
||||
|
||||
/* pg_stat_get_tuples_inserted */
|
||||
PG_STAT_GET_RELENTRY_INT64(tuples_inserted);
|
||||
|
||||
/* pg_stat_get_tuples_returned */
|
||||
PG_STAT_GET_RELENTRY_INT64(tuples_returned);
|
||||
|
||||
/* pg_stat_get_tuples_updated */
|
||||
PG_STAT_GET_RELENTRY_INT64(tuples_updated);
|
||||
|
||||
/* pg_stat_get_vacuum_count */
|
||||
PG_STAT_GET_RELENTRY_INT64(vacuum_count);
|
||||
|
||||
#define PG_STAT_GET_RELENTRY_TIMESTAMPTZ(stat) \
|
||||
Datum \
|
||||
CppConcat(pg_stat_get_,stat)(PG_FUNCTION_ARGS) \
|
||||
{ \
|
||||
Oid relid = PG_GETARG_OID(0); \
|
||||
TimestampTz result; \
|
||||
PgStat_StatTabEntry *tabentry; \
|
||||
\
|
||||
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL) \
|
||||
result = 0; \
|
||||
else \
|
||||
result = tabentry->stat; \
|
||||
\
|
||||
if (result == 0) \
|
||||
PG_RETURN_NULL(); \
|
||||
else \
|
||||
PG_RETURN_TIMESTAMPTZ(result); \
|
||||
} \
|
||||
|
||||
/* pg_stat_get_last_analyze_time */
|
||||
PG_STAT_GET_RELENTRY_TIMESTAMPTZ(last_analyze_time);
|
||||
|
||||
/* pg_stat_get_last_autoanalyze_time */
|
||||
PG_STAT_GET_RELENTRY_TIMESTAMPTZ(last_autoanalyze_time);
|
||||
|
||||
/* pg_stat_get_last_autovacuum_time */
|
||||
PG_STAT_GET_RELENTRY_TIMESTAMPTZ(last_autovacuum_time);
|
||||
|
||||
/* pg_stat_get_last_vacuum_time */
|
||||
PG_STAT_GET_RELENTRY_TIMESTAMPTZ(last_vacuum_time);
|
||||
|
||||
/* pg_stat_get_lastscan */
|
||||
PG_STAT_GET_RELENTRY_TIMESTAMPTZ(lastscan);
|
||||
|
||||
Datum
|
||||
pg_stat_get_function_calls(PG_FUNCTION_ARGS)
|
||||
|
Reference in New Issue
Block a user