From e3340873d7013ff8942254cae5e86fd6e3e75c0e Mon Sep 17 00:00:00 2001 From: Sami Imseih Date: Tue, 1 Dec 2026 16:48:55 +0000 Subject: [PATCH v24 3/6] pgstat: Move pending index and function counts into a sub-struct Move the pending index counts of PgStat_RelationStatus into idx.counts, and store pending function stats as a PgStat_FunctionStatus wrapping the existing PgStat_FunctionCounts. No behavior change. This is in preparation for a future change that will allow in-transaction flushes. A pending entry then stays around after a flush and keeps accumulating, so it needs a flushed baseline next to these counts to flush only what is new. Author: Sami Imseih Suggested-by: Bertrand Drouvot Reviewed-by: Bertrand Drouvot Reviewed-by: Zsolt Parragi Discussion: https://postgr.es/m/CAA5RZ0uA-4qcD3+2hjcE_-zQUBhvWf5foPM2vzYneFKrJLsBDQ@mail.gmail.com --- src/backend/utils/activity/pgstat.c | 2 +- src/backend/utils/activity/pgstat_function.c | 18 +++++++-------- src/backend/utils/activity/pgstat_index.c | 22 +++++++++---------- src/backend/utils/adt/pgstatfuncs.c | 2 +- src/include/pgstat.h | 23 +++++++++++++++----- src/tools/pgindent/typedefs.list | 1 + 6 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/backend/utils/activity/pgstat.c b/src/backend/utils/activity/pgstat.c index badf264e7c6..7c2b03a916c 100644 --- a/src/backend/utils/activity/pgstat.c +++ b/src/backend/utils/activity/pgstat.c @@ -341,7 +341,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE] .shared_size = sizeof(PgStatShared_Function), .shared_data_off = offsetof(PgStatShared_Function, stats), .shared_data_len = sizeof(((PgStatShared_Function *) 0)->stats), - .pending_size = sizeof(PgStat_FunctionCounts), + .pending_size = sizeof(PgStat_FunctionStatus), .flush_pending_cb = pgstat_function_flush_cb, .reset_timestamp_cb = pgstat_function_reset_timestamp_cb, diff --git a/src/backend/utils/activity/pgstat_function.c b/src/backend/utils/activity/pgstat_function.c index 2a7407370e0..7e76fc8ecd2 100644 --- a/src/backend/utils/activity/pgstat_function.c +++ b/src/backend/utils/activity/pgstat_function.c @@ -73,7 +73,7 @@ pgstat_init_function_usage(FunctionCallInfo fcinfo, PgStat_FunctionCallUsage *fcu) { PgStat_EntryRef *entry_ref; - PgStat_FunctionCounts *pending; + PgStat_FunctionStatus *pending; bool created_entry; if (pgstat_track_functions <= fcinfo->flinfo->fn_stats) @@ -121,10 +121,10 @@ pgstat_init_function_usage(FunctionCallInfo fcinfo, pending = entry_ref->pending; - fcu->fs = pending; + fcu->fs = &pending->counts; /* save stats for this function, later used to compensate for recursion */ - fcu->save_f_total_time = pending->total_time; + fcu->save_f_total_time = pending->counts.total_time; /* save current backend-wide total time */ fcu->save_total = total_func_time; @@ -193,10 +193,10 @@ PgStat_FlushResult pgstat_function_flush_cb(PgStat_EntryRef *entry_ref, bool nowait, bool xact_boundary) { - PgStat_FunctionCounts *localent; + PgStat_FunctionStatus *localent; PgStatShared_Function *shfuncent; - localent = (PgStat_FunctionCounts *) entry_ref->pending; + localent = (PgStat_FunctionStatus *) entry_ref->pending; shfuncent = (PgStatShared_Function *) entry_ref->shared_stats; /* localent always has non-zero content */ @@ -204,11 +204,11 @@ pgstat_function_flush_cb(PgStat_EntryRef *entry_ref, bool nowait, if (!pgstat_lock_entry(entry_ref, nowait)) return PGSTAT_FLUSH_LOCK_CONFLICT; - shfuncent->stats.numcalls += localent->numcalls; + shfuncent->stats.numcalls += localent->counts.numcalls; shfuncent->stats.total_time += - INSTR_TIME_GET_MICROSEC(localent->total_time); + INSTR_TIME_GET_MICROSEC(localent->counts.total_time); shfuncent->stats.self_time += - INSTR_TIME_GET_MICROSEC(localent->self_time); + INSTR_TIME_GET_MICROSEC(localent->counts.self_time); pgstat_unlock_entry(entry_ref); @@ -234,7 +234,7 @@ find_funcstat_entry(Oid func_id) entry_ref = pgstat_fetch_pending_entry(PGSTAT_KIND_FUNCTION, MyDatabaseId, func_id); if (entry_ref) - return entry_ref->pending; + return &((PgStat_FunctionStatus *) entry_ref->pending)->counts; return NULL; } diff --git a/src/backend/utils/activity/pgstat_index.c b/src/backend/utils/activity/pgstat_index.c index a28a02a293f..f18480efd9f 100644 --- a/src/backend/utils/activity/pgstat_index.c +++ b/src/backend/utils/activity/pgstat_index.c @@ -50,7 +50,7 @@ pgstat_index_flush_cb(PgStat_EntryRef *entry_ref, bool nowait, * Ignore entries that didn't accumulate any actual counts, such as * indexes that were opened by the planner but not used. */ - if (pg_memory_is_all_zeros(&lstats->idx, + if (pg_memory_is_all_zeros(&lstats->idx.counts, sizeof(struct PgStat_IndexCounts))) return PGSTAT_FLUSH_DONE; @@ -60,27 +60,27 @@ pgstat_index_flush_cb(PgStat_EntryRef *entry_ref, bool nowait, /* Add the values to the shared entry. */ idxentry = &shidxstats->stats; - idxentry->numscans += lstats->idx.numscans; - if (lstats->idx.numscans) + idxentry->numscans += lstats->idx.counts.numscans; + if (lstats->idx.counts.numscans) { TimestampTz t = GetCurrentTransactionStopTimestamp(); if (t > idxentry->lastscan) idxentry->lastscan = t; } - idxentry->tuples_returned += lstats->idx.tuples_returned; - idxentry->tuples_fetched += lstats->idx.tuples_fetched; - idxentry->blocks_fetched += lstats->idx.blocks_fetched; - idxentry->blocks_hit += lstats->idx.blocks_hit; + idxentry->tuples_returned += lstats->idx.counts.tuples_returned; + idxentry->tuples_fetched += lstats->idx.counts.tuples_fetched; + idxentry->blocks_fetched += lstats->idx.counts.blocks_fetched; + idxentry->blocks_hit += lstats->idx.counts.blocks_hit; pgstat_unlock_entry(entry_ref); /* The entry was successfully flushed, add the same to database stats */ dbentry = pgstat_prep_database_pending(dboid); - dbentry->tuples_returned += lstats->idx.tuples_returned; - dbentry->tuples_fetched += lstats->idx.tuples_fetched; - dbentry->blocks_fetched += lstats->idx.blocks_fetched; - dbentry->blocks_hit += lstats->idx.blocks_hit; + dbentry->tuples_returned += lstats->idx.counts.tuples_returned; + dbentry->tuples_fetched += lstats->idx.counts.tuples_fetched; + dbentry->blocks_fetched += lstats->idx.counts.blocks_fetched; + dbentry->blocks_hit += lstats->idx.counts.blocks_hit; return PGSTAT_FLUSH_DONE; } diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c index 64b6f60516c..1c69940146c 100644 --- a/src/backend/utils/adt/pgstatfuncs.c +++ b/src/backend/utils/adt/pgstatfuncs.c @@ -1984,7 +1984,7 @@ CppConcat(pg_stat_get_xact_idx_,stat)(PG_FUNCTION_ARGS) \ if (!tabentry) \ result = 0; \ else \ - result = (int64) (tabentry->idx.stat); \ + result = (int64) (tabentry->idx.counts.stat); \ \ PG_RETURN_INT64(result); \ } diff --git a/src/include/pgstat.h b/src/include/pgstat.h index 2d4fde2651d..c80556be3ff 100644 --- a/src/include/pgstat.h +++ b/src/include/pgstat.h @@ -90,6 +90,14 @@ typedef struct PgStat_FunctionCounts instr_time self_time; } PgStat_FunctionCounts; +/* + * Pending function stats stored in PgStat_EntryRef->pending. + */ +typedef struct PgStat_FunctionStatus +{ + PgStat_FunctionCounts counts; +} PgStat_FunctionStatus; + /* * Working state needed to accumulate per-function-call timing statistics. */ @@ -239,7 +247,10 @@ typedef struct PgStat_RelationStatus } tab; /* index counters */ - PgStat_IndexCounts idx; + struct + { + PgStat_IndexCounts counts; + } idx; }; } PgStat_RelationStatus; @@ -822,7 +833,7 @@ extern void pgstat_report_analyze(Relation rel, if (pgstat_should_count_relation(rel)) \ { \ if ((rel)->pgstat_info->kind == PGSTAT_KIND_INDEX) \ - (rel)->pgstat_info->idx.tuples_fetched++; \ + (rel)->pgstat_info->idx.counts.tuples_fetched++; \ else \ (rel)->pgstat_info->tab.counts.tuples_fetched++; \ } \ @@ -832,7 +843,7 @@ extern void pgstat_report_analyze(Relation rel, if (pgstat_should_count_relation(rel)) \ { \ Assert((rel)->pgstat_info->kind == PGSTAT_KIND_INDEX); \ - (rel)->pgstat_info->idx.numscans++; \ + (rel)->pgstat_info->idx.counts.numscans++; \ } \ } while (0) #define pgstat_count_index_tuples(rel, n) \ @@ -840,7 +851,7 @@ extern void pgstat_report_analyze(Relation rel, if (pgstat_should_count_relation(rel)) \ { \ Assert((rel)->pgstat_info->kind == PGSTAT_KIND_INDEX); \ - (rel)->pgstat_info->idx.tuples_returned += (n); \ + (rel)->pgstat_info->idx.counts.tuples_returned += (n); \ } \ } while (0) #define pgstat_count_buffer_read(rel) \ @@ -848,7 +859,7 @@ extern void pgstat_report_analyze(Relation rel, if (pgstat_should_count_relation(rel)) \ { \ if ((rel)->pgstat_info->kind == PGSTAT_KIND_INDEX) \ - (rel)->pgstat_info->idx.blocks_fetched++; \ + (rel)->pgstat_info->idx.counts.blocks_fetched++; \ else \ (rel)->pgstat_info->tab.counts.blocks_fetched++; \ } \ @@ -858,7 +869,7 @@ extern void pgstat_report_analyze(Relation rel, if (pgstat_should_count_relation(rel)) \ { \ if ((rel)->pgstat_info->kind == PGSTAT_KIND_INDEX) \ - (rel)->pgstat_info->idx.blocks_hit++; \ + (rel)->pgstat_info->idx.counts.blocks_hit++; \ else \ (rel)->pgstat_info->tab.counts.blocks_hit++; \ } \ diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index e2ef599cf50..9cb1676fffb 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -2327,6 +2327,7 @@ PgStat_FetchConsistency PgStat_FlushResult PgStat_FunctionCallUsage PgStat_FunctionCounts +PgStat_FunctionStatus PgStat_HashKey PgStat_IO PgStat_IndexCounts -- 2.50.1