Hi,
On 12/3/22 9:16 PM, Nathan Bossart wrote:
On Sat, Dec 03, 2022 at 10:31:19AM +0100, Drouvot, Bertrand wrote:
On 12/3/22 1:51 AM, Nathan Bossart wrote:
Can we hard-code the prefix in the macro? It looks like all of these use
the same one.
Good point! Done in V2 attached.
Thanks. I editorialized a bit in the attached v3. I'm not sure that my
proposed names for the macros are actually an improvement. WDYT?
Thanks! I do prefer the macros definition ordering that you're proposing (that makes
pgstatfuncs.c "easier" to read).
As far the names, I think it's better to replace "TAB" with "REL" (like in v4 attached): the reason
is that those macros will be used in [1] for both tables and indexes stats (and so we'd have to replace "TAB"
with "REL" in [1]).
Having "REL" already in place reduces the changes that will be needed in [1].
[1]:
https://www.postgresql.org/message-id/flat/f572abe7-a1bb-e13b-48c7-2ca150546...@gmail.com
Regards,
--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com
diff --git a/src/backend/access/heap/README.HOT
b/src/backend/access/heap/README.HOT
index 68c6709aa8..6fd1767f70 100644
--- a/src/backend/access/heap/README.HOT
+++ b/src/backend/access/heap/README.HOT
@@ -271,7 +271,7 @@ physical tuple by eliminating an intermediate heap-only
tuple or
replacing a physical root tuple by a redirect pointer, a decrement in
the table's number of dead tuples is reported to pgstats, which may
postpone autovacuuming. Note that we do not count replacing a root tuple
-by a DEAD line pointer as decrementing n_dead_tuples; we still want
+by a DEAD line pointer as decrementing dead_tuples; we still want
autovacuum to run to clean up the index entries and DEAD item.
This area probably needs further work ...
diff --git a/src/backend/postmaster/autovacuum.c
b/src/backend/postmaster/autovacuum.c
index 601834d4b4..0746d80224 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -3081,9 +3081,9 @@ relation_needs_vacanalyze(Oid relid,
if (PointerIsValid(tabentry) && AutoVacuumingActive())
{
reltuples = classForm->reltuples;
- vactuples = tabentry->n_dead_tuples;
- instuples = tabentry->inserts_since_vacuum;
- anltuples = tabentry->changes_since_analyze;
+ vactuples = tabentry->dead_tuples;
+ instuples = tabentry->ins_since_vacuum;
+ anltuples = tabentry->mod_since_analyze;
/* If the table hasn't yet been vacuumed, take reltuples as
zero */
if (reltuples < 0)
diff --git a/src/backend/utils/activity/pgstat_relation.c
b/src/backend/utils/activity/pgstat_relation.c
index f92e16e7af..a9c05153d9 100644
--- a/src/backend/utils/activity/pgstat_relation.c
+++ b/src/backend/utils/activity/pgstat_relation.c
@@ -231,8 +231,8 @@ pgstat_report_vacuum(Oid tableoid, bool shared,
shtabentry = (PgStatShared_Relation *) entry_ref->shared_stats;
tabentry = &shtabentry->stats;
- tabentry->n_live_tuples = livetuples;
- tabentry->n_dead_tuples = deadtuples;
+ tabentry->live_tuples = livetuples;
+ tabentry->dead_tuples = deadtuples;
/*
* It is quite possible that a non-aggressive VACUUM ended up skipping
@@ -244,16 +244,16 @@ pgstat_report_vacuum(Oid tableoid, bool shared,
* autovacuum. An anti-wraparound autovacuum will catch any persistent
* stragglers.
*/
- tabentry->inserts_since_vacuum = 0;
+ tabentry->ins_since_vacuum = 0;
if (IsAutoVacuumWorkerProcess())
{
- tabentry->autovac_vacuum_timestamp = ts;
- tabentry->autovac_vacuum_count++;
+ tabentry->last_autovacuum_time = ts;
+ tabentry->autovacuum_count++;
}
else
{
- tabentry->vacuum_timestamp = ts;
+ tabentry->last_vacuum_time = ts;
tabentry->vacuum_count++;
}
@@ -264,7 +264,7 @@ pgstat_report_vacuum(Oid tableoid, bool shared,
* Report that the table was just analyzed.
*
* Caller must provide new live- and dead-tuples estimates, as well as a
- * flag indicating whether to reset the changes_since_analyze counter.
+ * flag indicating whether to reset the mod_since_analyze counter.
*/
void
pgstat_report_analyze(Relation rel,
@@ -318,25 +318,25 @@ pgstat_report_analyze(Relation rel,
shtabentry = (PgStatShared_Relation *) entry_ref->shared_stats;
tabentry = &shtabentry->stats;
- tabentry->n_live_tuples = livetuples;
- tabentry->n_dead_tuples = deadtuples;
+ tabentry->live_tuples = livetuples;
+ tabentry->dead_tuples = deadtuples;
/*
- * If commanded, reset changes_since_analyze to zero. This forgets any
+ * If commanded, reset mod_since_analyze to zero. This forgets any
* changes that were committed while the ANALYZE was in progress, but we
* have no good way to estimate how many of those there were.
*/
if (resetcounter)
- tabentry->changes_since_analyze = 0;
+ tabentry->mod_since_analyze = 0;
if (IsAutoVacuumWorkerProcess())
{
- tabentry->autovac_analyze_timestamp = GetCurrentTimestamp();
- tabentry->autovac_analyze_count++;
+ tabentry->last_autoanalyze_time = GetCurrentTimestamp();
+ tabentry->autoanalyze_count++;
}
else
{
- tabentry->analyze_timestamp = GetCurrentTimestamp();
+ tabentry->last_analyze_time = GetCurrentTimestamp();
tabentry->analyze_count++;
}
@@ -798,22 +798,22 @@ pgstat_relation_flush_cb(PgStat_EntryRef *entry_ref, bool
nowait)
*/
if (lstats->t_counts.t_truncdropped)
{
- tabentry->n_live_tuples = 0;
- tabentry->n_dead_tuples = 0;
- tabentry->inserts_since_vacuum = 0;
+ tabentry->live_tuples = 0;
+ tabentry->dead_tuples = 0;
+ tabentry->ins_since_vacuum = 0;
}
- tabentry->n_live_tuples += lstats->t_counts.t_delta_live_tuples;
- tabentry->n_dead_tuples += lstats->t_counts.t_delta_dead_tuples;
- tabentry->changes_since_analyze += lstats->t_counts.t_changed_tuples;
- tabentry->inserts_since_vacuum += lstats->t_counts.t_tuples_inserted;
+ tabentry->live_tuples += lstats->t_counts.t_delta_live_tuples;
+ tabentry->dead_tuples += lstats->t_counts.t_delta_dead_tuples;
+ tabentry->mod_since_analyze += lstats->t_counts.t_changed_tuples;
+ tabentry->ins_since_vacuum += lstats->t_counts.t_tuples_inserted;
tabentry->blocks_fetched += lstats->t_counts.t_blocks_fetched;
tabentry->blocks_hit += lstats->t_counts.t_blocks_hit;
- /* Clamp n_live_tuples in case of negative delta_live_tuples */
- tabentry->n_live_tuples = Max(tabentry->n_live_tuples, 0);
- /* Likewise for n_dead_tuples */
- tabentry->n_dead_tuples = Max(tabentry->n_dead_tuples, 0);
+ /* Clamp live_tuples in case of negative delta_live_tuples */
+ tabentry->live_tuples = Max(tabentry->live_tuples, 0);
+ /* Likewise for dead_tuples */
+ tabentry->dead_tuples = Max(tabentry->dead_tuples, 0);
pgstat_unlock_entry(entry_ref);
diff --git a/src/backend/utils/adt/pgstatfuncs.c
b/src/backend/utils/adt/pgstatfuncs.c
index ae3365d917..973979508d 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -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)
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index 9e2ce6f011..bc6349727b 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -364,22 +364,22 @@ typedef struct PgStat_StatTabEntry
PgStat_Counter tuples_deleted;
PgStat_Counter tuples_hot_updated;
- PgStat_Counter n_live_tuples;
- PgStat_Counter n_dead_tuples;
- PgStat_Counter changes_since_analyze;
- PgStat_Counter inserts_since_vacuum;
+ PgStat_Counter live_tuples;
+ PgStat_Counter dead_tuples;
+ PgStat_Counter mod_since_analyze;
+ PgStat_Counter ins_since_vacuum;
PgStat_Counter blocks_fetched;
PgStat_Counter blocks_hit;
- TimestampTz vacuum_timestamp; /* user initiated vacuum */
+ TimestampTz last_vacuum_time; /* user initiated vacuum */
PgStat_Counter vacuum_count;
- TimestampTz autovac_vacuum_timestamp; /* autovacuum initiated */
- PgStat_Counter autovac_vacuum_count;
- TimestampTz analyze_timestamp; /* user initiated */
+ TimestampTz last_autovacuum_time; /* autovacuum initiated */
+ PgStat_Counter autovacuum_count;
+ TimestampTz last_analyze_time; /* user initiated */
PgStat_Counter analyze_count;
- TimestampTz autovac_analyze_timestamp; /* autovacuum initiated */
- PgStat_Counter autovac_analyze_count;
+ TimestampTz last_autoanalyze_time; /* autovacuum initiated */
+ PgStat_Counter autoanalyze_count;
} PgStat_StatTabEntry;
typedef struct PgStat_WalStats