From 8e6124eb50d09fa09bec07ade2e53fcec27033a3 Mon Sep 17 00:00:00 2001 From: Melanie Plageman Date: Fri, 4 Nov 2022 16:02:30 -0400 Subject: [PATCH v4 4/4] condense flush functions --- src/backend/utils/activity/pgstat_relation.c | 153 ++++++++++--------- 1 file changed, 82 insertions(+), 71 deletions(-) diff --git a/src/backend/utils/activity/pgstat_relation.c b/src/backend/utils/activity/pgstat_relation.c index 94fbc6faea..d46d58da1e 100644 --- a/src/backend/utils/activity/pgstat_relation.c +++ b/src/backend/utils/activity/pgstat_relation.c @@ -883,83 +883,92 @@ bool pgstat_table_flush_cb(PgStat_EntryRef *entry_ref, bool nowait) { static const PgStat_TableCounts all_zeroes; - Oid dboid; - PgStat_TableStatus *lstats; /* pending stats entry */ - PgStatShared_Table *shtabstats; - PgStat_StatTabEntry *tabentry; /* table entry of shared stats */ - PgStat_StatDBEntry *dbentry; /* pending database entry */ - dboid = entry_ref->shared_entry->key.dboid; - lstats = (PgStat_TableStatus *) entry_ref->pending; - shtabstats = (PgStatShared_Table *) entry_ref->shared_stats; + PgStat_StatDBEntry *dbentry; + PgStat_StatTabEntry *stats_shmem; + + PgStat_TableCounts *stats_pending = + &((PgStat_TableStatus *) entry_ref->pending)->t_counts; /* * Ignore entries that didn't accumulate any actual counts. */ - if (memcmp(&lstats->t_counts, &all_zeroes, + if (memcmp(stats_pending, &all_zeroes, sizeof(PgStat_TableCounts)) == 0) - { return true; - } if (!pgstat_lock_entry(entry_ref, nowait)) return false; + stats_shmem = &((PgStatShared_Table *) entry_ref->shared_stats)->stats; + /* add the values to the shared entry. */ - tabentry = &shtabstats->stats; + stats_shmem->numscans += stats_pending->numscans; - tabentry->numscans += lstats->t_counts.t_numscans; - if (lstats->t_counts.t_numscans) + if (stats_pending->numscans) { TimestampTz t = GetCurrentTransactionStopTimestamp(); - if (t > tabentry->lastscan) - tabentry->lastscan = t; + if (t > stats_shmem->lastscan) + stats_shmem->lastscan = t; } - tabentry->tuples_returned += lstats->t_counts.t_tuples_returned; - tabentry->tuples_fetched += lstats->t_counts.t_tuples_fetched; - tabentry->tuples_inserted += lstats->t_counts.t_tuples_inserted; - tabentry->tuples_updated += lstats->t_counts.t_tuples_updated; - tabentry->tuples_deleted += lstats->t_counts.t_tuples_deleted; - tabentry->tuples_hot_updated += lstats->t_counts.t_tuples_hot_updated; + +#define PGSTAT_ACCUM_TAB(fld) \ + (stats_shmem)->fld += (stats_pending)->fld + + PGSTAT_ACCUM_TAB(tuples_returned); + PGSTAT_ACCUM_TAB(tuples_fetched); + PGSTAT_ACCUM_TAB(tuples_inserted); + PGSTAT_ACCUM_TAB(tuples_updated); + PGSTAT_ACCUM_TAB(tuples_deleted); + PGSTAT_ACCUM_TAB(tuples_hot_updated); + PGSTAT_ACCUM_TAB(blocks_fetched); + PGSTAT_ACCUM_TAB(blocks_hit); + +#undef PGSTAT_ACCUM_TAB /* * If table was truncated/dropped, first reset the live/dead counters. */ - if (lstats->t_counts.t_truncdropped) + if (stats_pending->truncdropped) { - tabentry->n_live_tuples = 0; - tabentry->n_dead_tuples = 0; - tabentry->inserts_since_vacuum = 0; + stats_shmem->live_tuples = 0; + stats_shmem->dead_tuples = 0; + stats_shmem->inserts_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->blocks_fetched += lstats->t_counts.t_blocks_fetched; - tabentry->blocks_hit += lstats->t_counts.t_blocks_hit; + stats_shmem->live_tuples += stats_pending->delta_live_tuples; + stats_shmem->dead_tuples += stats_pending->delta_dead_tuples; + stats_shmem->inserts_since_vacuum += stats_pending->tuples_inserted; + stats_shmem->changes_since_analyze += stats_pending->changed_tuples; - /* 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 */ + stats_shmem->live_tuples = Max(stats_shmem->live_tuples, 0); + /* Likewise for dead_tuples */ + stats_shmem->dead_tuples = Max(stats_shmem->dead_tuples, 0); pgstat_unlock_entry(entry_ref); /* The entry was successfully flushed, add the same to database stats */ - dbentry = pgstat_prep_database_pending(dboid); - dbentry->n_tuples_returned += lstats->t_counts.t_tuples_returned; - dbentry->n_tuples_fetched += lstats->t_counts.t_tuples_fetched; - dbentry->n_tuples_inserted += lstats->t_counts.t_tuples_inserted; - dbentry->n_tuples_updated += lstats->t_counts.t_tuples_updated; - dbentry->n_tuples_deleted += lstats->t_counts.t_tuples_deleted; - dbentry->n_blocks_fetched += lstats->t_counts.t_blocks_fetched; - dbentry->n_blocks_hit += lstats->t_counts.t_blocks_hit; + dbentry = pgstat_prep_database_pending(entry_ref->shared_entry->key.dboid); + +#define PGSTAT_ACCUM_TAB(fld) \ + (dbentry)->fld += (stats_pending)->fld + + PGSTAT_ACCUM_TAB(tuples_returned); + PGSTAT_ACCUM_TAB(tuples_fetched); + PGSTAT_ACCUM_TAB(tuples_inserted); + PGSTAT_ACCUM_TAB(tuples_updated); + PGSTAT_ACCUM_TAB(tuples_deleted); + PGSTAT_ACCUM_TAB(blocks_fetched); + PGSTAT_ACCUM_TAB(blocks_hit); + +#undef PGSTAT_ACCUM_TAB return true; } + /* * Flush out pending stats for the entry * @@ -973,59 +982,61 @@ bool pgstat_index_flush_cb(PgStat_EntryRef *entry_ref, bool nowait) { static const PgStat_IndexCounts all_zeroes; - Oid dboid; - - PgStat_IndexStatus *lstats; /* pending stats entry */ - PgStatShared_Index *shrelcomstats; - PgStat_StatIndEntry *indentry; /* index entry of shared stats */ - PgStat_StatDBEntry *dbentry; /* pending database entry */ + PgStat_StatDBEntry *dbentry; + PgStat_StatIndEntry *stats_shmem; - dboid = entry_ref->shared_entry->key.dboid; - lstats = (PgStat_IndexStatus *) entry_ref->pending; - shrelcomstats = (PgStatShared_Index *) entry_ref->shared_stats; + PgStat_IndexCounts *stats_pending = + &((PgStat_IndexStatus *) entry_ref->pending)->i_counts; /* * Ignore entries that didn't accumulate any actual counts, such as * indexes that were opened by the planner but not used. */ - if (memcmp(&lstats->i_counts, &all_zeroes, + if (memcmp(stats_pending, &all_zeroes, sizeof(PgStat_IndexCounts)) == 0) - { return true; - } if (!pgstat_lock_entry(entry_ref, nowait)) return false; - /* add the values to the shared entry. */ - indentry = &shrelcomstats->stats; + stats_shmem = &((PgStatShared_Index *) entry_ref->shared_stats)->stats; - indentry->numscans += lstats->i_counts.i_numscans; + /* add the values to the shared entry. */ + stats_shmem->numscans += stats_pending->numscans; - if (lstats->i_counts.i_numscans) + if (stats_pending->numscans) { TimestampTz t = GetCurrentTransactionStopTimestamp(); - if (t > indentry->lastscan) - indentry->lastscan = t; + if (t > stats_shmem->lastscan) + stats_shmem->lastscan = t; } - indentry->tuples_returned += lstats->i_counts.i_tuples_returned; - indentry->tuples_fetched += lstats->i_counts.i_tuples_fetched; - indentry->blocks_fetched += lstats->i_counts.i_blocks_fetched; - indentry->blocks_hit += lstats->i_counts.i_blocks_hit; + +#define PGSTAT_ACCUM_IDX(fld) \ + (stats_shmem)->fld += (stats_pending)->fld + PGSTAT_ACCUM_IDX(tuples_returned); + PGSTAT_ACCUM_IDX(tuples_fetched); + PGSTAT_ACCUM_IDX(blocks_fetched); + PGSTAT_ACCUM_IDX(blocks_hit); +#undef PGSTAT_ACCUM_IDX pgstat_unlock_entry(entry_ref); /* The entry was successfully flushed, add the same to database stats */ - dbentry = pgstat_prep_database_pending(dboid); - dbentry->n_tuples_returned += lstats->i_counts.i_tuples_returned; - dbentry->n_tuples_fetched += lstats->i_counts.i_tuples_fetched; - dbentry->n_blocks_fetched += lstats->i_counts.i_blocks_fetched; - dbentry->n_blocks_hit += lstats->i_counts.i_blocks_hit; + dbentry = pgstat_prep_database_pending(entry_ref->shared_entry->key.dboid); + +#define PGSTAT_ACCUM_IDX(fld) \ + (dbentry)->fld += (stats_pending)->fld + PGSTAT_ACCUM_IDX(tuples_returned); + PGSTAT_ACCUM_IDX(tuples_fetched); + PGSTAT_ACCUM_IDX(blocks_fetched); + PGSTAT_ACCUM_IDX(blocks_hit); +#undef PGSTAT_ACCUM_IDX return true; } + void pgstat_table_delete_pending_cb(PgStat_EntryRef *entry_ref) { -- 2.34.1