From a1b1bb2e9cc0a643a4b0dd85e44f521c8687fc2b Mon Sep 17 00:00:00 2001 From: Sami Imseih Date: Tue, 1 Dec 2026 15:58:26 +0000 Subject: [PATCH v23 1/6] pgstat: Track HOT and new-page updates per (sub)transaction Keep tuples_hot_updated and tuples_newpage_updated in PgStat_TableXactStatus next to tuples_updated, instead of counting them directly in the pending relation entry. Unlike tuples_updated, they are not reset by truncate or drop. No behavior change, since pending stats are only flushed outside a transaction. This is in preparation for a future change that will allow in-transaction flushes, which must not publish these counts ahead of n_tup_upd. Author: Sami Imseih 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_relation.c | 32 ++++++++++++++++++-- src/include/pgstat.h | 3 ++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/backend/utils/activity/pgstat_relation.c b/src/backend/utils/activity/pgstat_relation.c index 82861e88e0e..6195a920f2f 100644 --- a/src/backend/utils/activity/pgstat_relation.c +++ b/src/backend/utils/activity/pgstat_relation.c @@ -460,12 +460,14 @@ pgstat_count_heap_update(Relation rel, bool hot, bool newpage) /* * tuples_hot_updated and tuples_newpage_updated counters are - * nontransactional, so just advance them + * nontransactional, but keep them with tuples_updated until the + * (sub)transaction ends, so a flush does not publish them ahead of + * it. */ if (hot) - pgstat_info->tab.counts_xact.tuples_hot_updated++; + pgstat_info->tab.trans->tuples_hot_updated++; else if (newpage) - pgstat_info->tab.counts_xact.tuples_newpage_updated++; + pgstat_info->tab.trans->tuples_newpage_updated++; } } @@ -610,6 +612,10 @@ find_relstat_entry_kind(PgStat_Kind kind, Oid rel_id) relstatus->tab.counts_xact.tuples_inserted += trans->tuples_inserted; relstatus->tab.counts_xact.tuples_updated += trans->tuples_updated; relstatus->tab.counts_xact.tuples_deleted += trans->tuples_deleted; + relstatus->tab.counts_xact.tuples_hot_updated += + trans->tuples_hot_updated; + relstatus->tab.counts_xact.tuples_newpage_updated += + trans->tuples_newpage_updated; } return relstatus; @@ -643,6 +649,10 @@ AtEOXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit) relstat->tab.counts_xact.tuples_inserted += trans->tuples_inserted; relstat->tab.counts_xact.tuples_updated += trans->tuples_updated; relstat->tab.counts_xact.tuples_deleted += trans->tuples_deleted; + relstat->tab.counts_xact.tuples_hot_updated += + trans->tuples_hot_updated; + relstat->tab.counts_xact.tuples_newpage_updated += + trans->tuples_newpage_updated; if (isCommit) { relstat->tab.truncdropped = trans->truncdropped; @@ -715,6 +725,10 @@ AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, in trans->upper->tuples_updated += trans->tuples_updated; trans->upper->tuples_deleted += trans->tuples_deleted; } + /* truncate/drop doesn't reset these */ + trans->upper->tuples_hot_updated += trans->tuples_hot_updated; + trans->upper->tuples_newpage_updated += + trans->tuples_newpage_updated; relstat->tab.trans = trans->upper; pfree(trans); } @@ -749,6 +763,10 @@ AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, in relstat->tab.counts_xact.tuples_inserted += trans->tuples_inserted; relstat->tab.counts_xact.tuples_updated += trans->tuples_updated; relstat->tab.counts_xact.tuples_deleted += trans->tuples_deleted; + relstat->tab.counts_xact.tuples_hot_updated += + trans->tuples_hot_updated; + relstat->tab.counts_xact.tuples_newpage_updated += + trans->tuples_newpage_updated; /* inserted tuples are dead, deleted tuples are unaffected */ relstat->tab.counts_xact.delta_dead_tuples += trans->tuples_inserted + trans->tuples_updated; @@ -799,6 +817,10 @@ AtPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state) * dead tuple counts are preserved in the 2PC state file. * * Note: AtEOXact_PgStat_Relations is not called during PREPARE. + * + * HOT and new-page update counts don't depend on whether the transaction + * commits, so move them into the pending entry to be reported by this + * backend. */ void PostPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state) @@ -810,6 +832,10 @@ PostPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state) PgStat_RelationStatus *relstat; relstat = trans->parent; + relstat->tab.counts_xact.tuples_hot_updated += + trans->tuples_hot_updated; + relstat->tab.counts_xact.tuples_newpage_updated += + trans->tuples_newpage_updated; relstat->tab.trans = NULL; } } diff --git a/src/include/pgstat.h b/src/include/pgstat.h index 9b99062f853..2d4fde2651d 100644 --- a/src/include/pgstat.h +++ b/src/include/pgstat.h @@ -252,6 +252,9 @@ typedef struct PgStat_TableXactStatus PgStat_Counter tuples_inserted; /* tuples inserted in (sub)xact */ PgStat_Counter tuples_updated; /* tuples updated in (sub)xact */ PgStat_Counter tuples_deleted; /* tuples deleted in (sub)xact */ + PgStat_Counter tuples_hot_updated; /* tuples HOT updated in (sub)xact */ + PgStat_Counter tuples_newpage_updated; /* tuples updated to a new page in + * (sub)xact */ bool truncdropped; /* relation truncated/dropped in this * (sub)xact */ /* tuples i/u/d prior to truncate/drop */ -- 2.50.1