From feccae7edde51f3f7fdc2acb135b8b22b8b93b2a Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Tue, 17 Nov 2026 17:03:02 +0000
Subject: [PATCH v1 2/4] Handle n_ins_since_vacuum in prepared transactions

The existing pgstat 2PC record cannot preserve inserts on both sides of a
transactional TRUNCATE. Consequently, COMMIT PREPARED can lose inserts after the
truncate, while ROLLBACK PREPARED can lose inserts before it.

Extend the record with the current and pre-truncate n_ins_since_vacuum counts,
and restore the appropriate values when the prepared transaction is committed or
rolled back.

Keep the old record as a prefix and distinguish the layouts by length, so
prepared transactions written by older binaries remain readable. Add isolation
test coverage for both outcomes.

Author: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Reviewed-by:
Discussion: https://postgr.es/m/
Backpatch-through: 15
---
 src/backend/utils/activity/pgstat_relation.c | 60 ++++++++++++++------
 src/test/isolation/expected/stats.out        | 42 +++++++++++++-
 src/test/isolation/expected/stats_1.out      | 42 +++++++++++++-
 src/test/isolation/specs/stats.spec          | 36 ++++++++++++
 src/tools/pgindent/typedefs.list             |  1 +
 5 files changed, 161 insertions(+), 20 deletions(-)
  31.4% src/backend/utils/activity/
  56.0% src/test/isolation/expected/
  12.2% src/test/isolation/specs/

diff --git a/src/backend/utils/activity/pgstat_relation.c b/src/backend/utils/activity/pgstat_relation.c
index 9003b4cf8e4..7dfda750e64 100644
--- a/src/backend/utils/activity/pgstat_relation.c
+++ b/src/backend/utils/activity/pgstat_relation.c
@@ -41,6 +41,18 @@ typedef struct TwoPhasePgStatRecord
 	bool		truncdropped;	/* was the relation truncated/dropped? */
 } TwoPhasePgStatRecord;
 
+/*
+ * Extended 2PC state for n_ins_since_vacuum. Keep the existing record as
+ * the prefix so that prepared transactions created by an older binary remain
+ * readable after a minor-version update.
+ */
+typedef struct TwoPhasePgStatRecordV2
+{
+	TwoPhasePgStatRecord base;
+	PgStat_Counter ins_since_vacuum;
+	PgStat_Counter ins_since_vacuum_pre_truncdrop;
+} TwoPhasePgStatRecordV2;
+
 
 static PgStat_RelationStatus *pgstat_prep_relation_pending(PgStat_Kind kind,
 														   Oid rel_id, bool isshared);
@@ -799,26 +811,29 @@ AtPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
 
 	for (trans = xact_state->first; trans != NULL; trans = trans->next)
 	{
-		PgStat_RelationStatus *relstat PG_USED_FOR_ASSERTS_ONLY;
-		TwoPhasePgStatRecord record;
+		PgStat_RelationStatus *relstat;
+		TwoPhasePgStatRecordV2 record;
 
 		Assert(trans->nest_level == 1);
 		Assert(trans->upper == NULL);
 		relstat = trans->parent;
 		Assert(relstat->tab.trans == trans);
 
-		record.tuples_inserted = trans->tuples_inserted;
-		record.tuples_updated = trans->tuples_updated;
-		record.tuples_deleted = trans->tuples_deleted;
-		record.inserted_pre_truncdrop = trans->inserted_pre_truncdrop;
-		record.updated_pre_truncdrop = trans->updated_pre_truncdrop;
-		record.deleted_pre_truncdrop = trans->deleted_pre_truncdrop;
-		record.id = relstat->tab.id;
-		record.shared = relstat->tab.shared;
-		record.truncdropped = trans->truncdropped;
+		record.base.tuples_inserted = trans->tuples_inserted;
+		record.base.tuples_updated = trans->tuples_updated;
+		record.base.tuples_deleted = trans->tuples_deleted;
+		record.base.inserted_pre_truncdrop = trans->inserted_pre_truncdrop;
+		record.base.updated_pre_truncdrop = trans->updated_pre_truncdrop;
+		record.base.deleted_pre_truncdrop = trans->deleted_pre_truncdrop;
+		record.base.id = relstat->tab.id;
+		record.base.shared = relstat->tab.shared;
+		record.base.truncdropped = trans->truncdropped;
+		record.ins_since_vacuum = relstat->tab.counts_xact.ins_since_vacuum;
+		record.ins_since_vacuum_pre_truncdrop = trans->ins_since_vacuum_pre_truncdrop +
+			trans->inserted_pre_truncdrop;
 
 		RegisterTwoPhaseRecord(TWOPHASE_RM_PGSTAT_ID, 0,
-							   &record, sizeof(TwoPhasePgStatRecord));
+							   &record, sizeof(TwoPhasePgStatRecordV2));
 	}
 }
 
@@ -841,8 +856,7 @@ PostPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
 
 		relstat = trans->parent;
 		if (trans->truncdropped)
-			relstat->tab.counts_xact.ins_since_vacuum +=
-				trans->ins_since_vacuum_pre_truncdrop;
+			relstat->tab.counts_xact.ins_since_vacuum = 0;
 		relstat->tab.trans = NULL;
 	}
 }
@@ -857,7 +871,11 @@ pgstat_twophase_postcommit(FullTransactionId fxid, uint16 info,
 						   void *recdata, uint32 len)
 {
 	TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
+	TwoPhasePgStatRecordV2 *rec_v2 = (TwoPhasePgStatRecordV2 *) recdata;
 	PgStat_RelationStatus *pgstat_info;
+	bool		has_ins_since_vacuum = len >= sizeof(TwoPhasePgStatRecordV2);
+
+	Assert(len == sizeof(TwoPhasePgStatRecord) || has_ins_since_vacuum);
 
 	/* Find or create a relstat entry for the rel */
 	pgstat_info = pgstat_prep_relation_pending(PGSTAT_KIND_RELATION, rec->id, rec->shared);
@@ -872,7 +890,8 @@ pgstat_twophase_postcommit(FullTransactionId fxid, uint16 info,
 		/* forget live/dead stats seen by backend thus far */
 		pgstat_info->tab.counts_xact.delta_live_tuples = 0;
 		pgstat_info->tab.counts_xact.delta_dead_tuples = 0;
-		pgstat_info->tab.counts_xact.ins_since_vacuum = 0;
+		pgstat_info->tab.counts_xact.ins_since_vacuum =
+			has_ins_since_vacuum ? rec_v2->ins_since_vacuum : 0;
 	}
 	pgstat_info->tab.counts_xact.delta_live_tuples +=
 		rec->tuples_inserted - rec->tuples_deleted;
@@ -895,7 +914,12 @@ pgstat_twophase_postabort(FullTransactionId fxid, uint16 info,
 						  void *recdata, uint32 len)
 {
 	TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
+	TwoPhasePgStatRecordV2 *rec_v2 = (TwoPhasePgStatRecordV2 *) recdata;
 	PgStat_RelationStatus *pgstat_info;
+	PgStat_Counter ins_since_vacuum = rec->tuples_inserted;
+	bool		has_ins_since_vacuum = len >= sizeof(TwoPhasePgStatRecordV2);
+
+	Assert(len == sizeof(TwoPhasePgStatRecord) || has_ins_since_vacuum);
 
 	/* Find or create a relstat entry for the rel */
 	pgstat_info = pgstat_prep_relation_pending(PGSTAT_KIND_RELATION, rec->id, rec->shared);
@@ -906,13 +930,17 @@ pgstat_twophase_postabort(FullTransactionId fxid, uint16 info,
 		rec->tuples_inserted = rec->inserted_pre_truncdrop;
 		rec->tuples_updated = rec->updated_pre_truncdrop;
 		rec->tuples_deleted = rec->deleted_pre_truncdrop;
+		if (has_ins_since_vacuum)
+			ins_since_vacuum += rec_v2->ins_since_vacuum + rec_v2->ins_since_vacuum_pre_truncdrop;
+		else
+			ins_since_vacuum = rec->tuples_inserted;
 	}
 	pgstat_info->tab.counts_xact.tuples_inserted += rec->tuples_inserted;
 	pgstat_info->tab.counts_xact.tuples_updated += rec->tuples_updated;
 	pgstat_info->tab.counts_xact.tuples_deleted += rec->tuples_deleted;
 	pgstat_info->tab.counts_xact.delta_dead_tuples +=
 		rec->tuples_inserted + rec->tuples_updated;
-	pgstat_info->tab.counts_xact.ins_since_vacuum += rec->tuples_inserted;
+	pgstat_info->tab.counts_xact.ins_since_vacuum += ins_since_vacuum;
 }
 
 /*
diff --git a/src/test/isolation/expected/stats.out b/src/test/isolation/expected/stats.out
index cfad309ccf3..56b489d5495 100644
--- a/src/test/isolation/expected/stats.out
+++ b/src/test/isolation/expected/stats.out
@@ -2830,7 +2830,7 @@ seq_scan|seq_tup_read|n_tup_ins|n_tup_upd|n_tup_del|n_live_tup|n_dead_tup|vacuum
 (1 row)
 
 
-starting permutation: s1_table_insert s1_begin s1_table_update_k1 s1_table_update_k1 s1_table_truncate s1_table_insert_k1 s1_table_update_k1 s1_prepare_a s1_ff s2_commit_prepared_a s2_ff s1_table_stats
+starting permutation: s1_table_insert s1_begin s1_table_update_k1 s1_table_update_k1 s1_trunc_insert_five s1_savepoint_p1 s1_trunc_insert_five s1_rollback_to_p1 s1_table_truncate s1_trunc_truncate s1_table_insert_k1 s1_table_update_k1 s1_trunc_insert_five s1_savepoint_p2 s1_trunc_insert_five s1_rollback_to_p2 s1_prepare_a s1_ff s2_commit_prepared_a s2_ff s1_table_stats s1_trunc_ins_since_vacuum
 pg_stat_force_next_flush
 ------------------------
                         
@@ -2840,9 +2840,18 @@ step s1_table_insert: INSERT INTO test_stat_tab(key, value) VALUES('k1', 1), ('k
 step s1_begin: BEGIN;
 step s1_table_update_k1: UPDATE test_stat_tab SET value = value + 1 WHERE key = 'k1';
 step s1_table_update_k1: UPDATE test_stat_tab SET value = value + 1 WHERE key = 'k1';
+step s1_trunc_insert_five: INSERT INTO test_stat_trunc SELECT generate_series(1, 5);
+step s1_savepoint_p1: SAVEPOINT p1;
+step s1_trunc_insert_five: INSERT INTO test_stat_trunc SELECT generate_series(1, 5);
+step s1_rollback_to_p1: ROLLBACK TO SAVEPOINT p1;
 step s1_table_truncate: TRUNCATE test_stat_tab;
+step s1_trunc_truncate: TRUNCATE test_stat_trunc;
 step s1_table_insert_k1: INSERT INTO test_stat_tab(key, value) VALUES('k1', 1);
 step s1_table_update_k1: UPDATE test_stat_tab SET value = value + 1 WHERE key = 'k1';
+step s1_trunc_insert_five: INSERT INTO test_stat_trunc SELECT generate_series(1, 5);
+step s1_savepoint_p2: SAVEPOINT p2;
+step s1_trunc_insert_five: INSERT INTO test_stat_trunc SELECT generate_series(1, 5);
+step s1_rollback_to_p2: ROLLBACK TO SAVEPOINT p2;
 step s1_prepare_a: PREPARE TRANSACTION 'a';
 step s1_ff: SELECT pg_stat_force_next_flush();
 pg_stat_force_next_flush
@@ -2875,6 +2884,16 @@ seq_scan|seq_tup_read|n_tup_ins|n_tup_upd|n_tup_del|n_live_tup|n_dead_tup|vacuum
        3|           9|        5|        1|        0|         1|         1|           0
 (1 row)
 
+step s1_trunc_ins_since_vacuum: 
+    SELECT pg_stat_get_ins_since_vacuum(tso.oid) AS n_ins_since_vacuum
+    FROM test_stat_oid AS tso
+    WHERE tso.name = 'test_stat_trunc'
+
+n_ins_since_vacuum
+------------------
+                10
+(1 row)
+
 
 starting permutation: s1_table_insert s1_begin s1_table_update_k1 s1_table_update_k1 s1_table_truncate s1_table_insert_k1 s1_table_update_k1 s1_prepare_a s1_rollback_prepared_a s1_ff s1_table_stats
 pg_stat_force_next_flush
@@ -2916,7 +2935,7 @@ seq_scan|seq_tup_read|n_tup_ins|n_tup_upd|n_tup_del|n_live_tup|n_dead_tup|vacuum
 (1 row)
 
 
-starting permutation: s1_table_insert s1_begin s1_table_update_k1 s1_table_update_k1 s1_table_truncate s1_table_insert_k1 s1_table_update_k1 s1_prepare_a s2_rollback_prepared_a s1_ff s2_ff s1_table_stats
+starting permutation: s1_table_insert s1_begin s1_table_update_k1 s1_table_update_k1 s1_trunc_insert_five s1_savepoint_p1 s1_trunc_insert_five s1_rollback_to_p1 s1_table_truncate s1_trunc_truncate s1_table_insert_k1 s1_table_update_k1 s1_trunc_insert_five s1_savepoint_p2 s1_trunc_insert_five s1_rollback_to_p2 s1_prepare_a s2_rollback_prepared_a s1_ff s2_ff s1_table_stats s1_trunc_ins_since_vacuum
 pg_stat_force_next_flush
 ------------------------
                         
@@ -2926,9 +2945,18 @@ step s1_table_insert: INSERT INTO test_stat_tab(key, value) VALUES('k1', 1), ('k
 step s1_begin: BEGIN;
 step s1_table_update_k1: UPDATE test_stat_tab SET value = value + 1 WHERE key = 'k1';
 step s1_table_update_k1: UPDATE test_stat_tab SET value = value + 1 WHERE key = 'k1';
+step s1_trunc_insert_five: INSERT INTO test_stat_trunc SELECT generate_series(1, 5);
+step s1_savepoint_p1: SAVEPOINT p1;
+step s1_trunc_insert_five: INSERT INTO test_stat_trunc SELECT generate_series(1, 5);
+step s1_rollback_to_p1: ROLLBACK TO SAVEPOINT p1;
 step s1_table_truncate: TRUNCATE test_stat_tab;
+step s1_trunc_truncate: TRUNCATE test_stat_trunc;
 step s1_table_insert_k1: INSERT INTO test_stat_tab(key, value) VALUES('k1', 1);
 step s1_table_update_k1: UPDATE test_stat_tab SET value = value + 1 WHERE key = 'k1';
+step s1_trunc_insert_five: INSERT INTO test_stat_trunc SELECT generate_series(1, 5);
+step s1_savepoint_p2: SAVEPOINT p2;
+step s1_trunc_insert_five: INSERT INTO test_stat_trunc SELECT generate_series(1, 5);
+step s1_rollback_to_p2: ROLLBACK TO SAVEPOINT p2;
 step s1_prepare_a: PREPARE TRANSACTION 'a';
 step s2_rollback_prepared_a: ROLLBACK PREPARED 'a';
 step s1_ff: SELECT pg_stat_force_next_flush();
@@ -2961,6 +2989,16 @@ seq_scan|seq_tup_read|n_tup_ins|n_tup_upd|n_tup_del|n_live_tup|n_dead_tup|vacuum
        3|           9|        4|        2|        0|         4|         2|           0
 (1 row)
 
+step s1_trunc_ins_since_vacuum: 
+    SELECT pg_stat_get_ins_since_vacuum(tso.oid) AS n_ins_since_vacuum
+    FROM test_stat_oid AS tso
+    WHERE tso.name = 'test_stat_trunc'
+
+n_ins_since_vacuum
+------------------
+                20
+(1 row)
+
 
 starting permutation: s1_table_insert s1_table_update_k1 s1_begin s1_table_delete_k1 s1_table_insert_k1 s1_table_update_k1 s1_table_update_k1 s1_table_drop s1_prepare_a s1_rollback_prepared_a s1_ff s1_table_stats
 pg_stat_force_next_flush
diff --git a/src/test/isolation/expected/stats_1.out b/src/test/isolation/expected/stats_1.out
index e1d937784cb..9560f3a4d51 100644
--- a/src/test/isolation/expected/stats_1.out
+++ b/src/test/isolation/expected/stats_1.out
@@ -2844,7 +2844,7 @@ seq_scan|seq_tup_read|n_tup_ins|n_tup_upd|n_tup_del|n_live_tup|n_dead_tup|vacuum
 (1 row)
 
 
-starting permutation: s1_table_insert s1_begin s1_table_update_k1 s1_table_update_k1 s1_table_truncate s1_table_insert_k1 s1_table_update_k1 s1_prepare_a s1_ff s2_commit_prepared_a s2_ff s1_table_stats
+starting permutation: s1_table_insert s1_begin s1_table_update_k1 s1_table_update_k1 s1_trunc_insert_five s1_savepoint_p1 s1_trunc_insert_five s1_rollback_to_p1 s1_table_truncate s1_trunc_truncate s1_table_insert_k1 s1_table_update_k1 s1_trunc_insert_five s1_savepoint_p2 s1_trunc_insert_five s1_rollback_to_p2 s1_prepare_a s1_ff s2_commit_prepared_a s2_ff s1_table_stats s1_trunc_ins_since_vacuum
 pg_stat_force_next_flush
 ------------------------
                         
@@ -2854,9 +2854,18 @@ step s1_table_insert: INSERT INTO test_stat_tab(key, value) VALUES('k1', 1), ('k
 step s1_begin: BEGIN;
 step s1_table_update_k1: UPDATE test_stat_tab SET value = value + 1 WHERE key = 'k1';
 step s1_table_update_k1: UPDATE test_stat_tab SET value = value + 1 WHERE key = 'k1';
+step s1_trunc_insert_five: INSERT INTO test_stat_trunc SELECT generate_series(1, 5);
+step s1_savepoint_p1: SAVEPOINT p1;
+step s1_trunc_insert_five: INSERT INTO test_stat_trunc SELECT generate_series(1, 5);
+step s1_rollback_to_p1: ROLLBACK TO SAVEPOINT p1;
 step s1_table_truncate: TRUNCATE test_stat_tab;
+step s1_trunc_truncate: TRUNCATE test_stat_trunc;
 step s1_table_insert_k1: INSERT INTO test_stat_tab(key, value) VALUES('k1', 1);
 step s1_table_update_k1: UPDATE test_stat_tab SET value = value + 1 WHERE key = 'k1';
+step s1_trunc_insert_five: INSERT INTO test_stat_trunc SELECT generate_series(1, 5);
+step s1_savepoint_p2: SAVEPOINT p2;
+step s1_trunc_insert_five: INSERT INTO test_stat_trunc SELECT generate_series(1, 5);
+step s1_rollback_to_p2: ROLLBACK TO SAVEPOINT p2;
 step s1_prepare_a: PREPARE TRANSACTION 'a';
 ERROR:  prepared transactions are disabled
 step s1_ff: SELECT pg_stat_force_next_flush();
@@ -2891,6 +2900,16 @@ seq_scan|seq_tup_read|n_tup_ins|n_tup_upd|n_tup_del|n_live_tup|n_dead_tup|vacuum
        3|           9|        4|        2|        0|         4|         2|           0
 (1 row)
 
+step s1_trunc_ins_since_vacuum: 
+    SELECT pg_stat_get_ins_since_vacuum(tso.oid) AS n_ins_since_vacuum
+    FROM test_stat_oid AS tso
+    WHERE tso.name = 'test_stat_trunc'
+
+n_ins_since_vacuum
+------------------
+                20
+(1 row)
+
 
 starting permutation: s1_table_insert s1_begin s1_table_update_k1 s1_table_update_k1 s1_table_truncate s1_table_insert_k1 s1_table_update_k1 s1_prepare_a s1_rollback_prepared_a s1_ff s1_table_stats
 pg_stat_force_next_flush
@@ -2934,7 +2953,7 @@ seq_scan|seq_tup_read|n_tup_ins|n_tup_upd|n_tup_del|n_live_tup|n_dead_tup|vacuum
 (1 row)
 
 
-starting permutation: s1_table_insert s1_begin s1_table_update_k1 s1_table_update_k1 s1_table_truncate s1_table_insert_k1 s1_table_update_k1 s1_prepare_a s2_rollback_prepared_a s1_ff s2_ff s1_table_stats
+starting permutation: s1_table_insert s1_begin s1_table_update_k1 s1_table_update_k1 s1_trunc_insert_five s1_savepoint_p1 s1_trunc_insert_five s1_rollback_to_p1 s1_table_truncate s1_trunc_truncate s1_table_insert_k1 s1_table_update_k1 s1_trunc_insert_five s1_savepoint_p2 s1_trunc_insert_five s1_rollback_to_p2 s1_prepare_a s2_rollback_prepared_a s1_ff s2_ff s1_table_stats s1_trunc_ins_since_vacuum
 pg_stat_force_next_flush
 ------------------------
                         
@@ -2944,9 +2963,18 @@ step s1_table_insert: INSERT INTO test_stat_tab(key, value) VALUES('k1', 1), ('k
 step s1_begin: BEGIN;
 step s1_table_update_k1: UPDATE test_stat_tab SET value = value + 1 WHERE key = 'k1';
 step s1_table_update_k1: UPDATE test_stat_tab SET value = value + 1 WHERE key = 'k1';
+step s1_trunc_insert_five: INSERT INTO test_stat_trunc SELECT generate_series(1, 5);
+step s1_savepoint_p1: SAVEPOINT p1;
+step s1_trunc_insert_five: INSERT INTO test_stat_trunc SELECT generate_series(1, 5);
+step s1_rollback_to_p1: ROLLBACK TO SAVEPOINT p1;
 step s1_table_truncate: TRUNCATE test_stat_tab;
+step s1_trunc_truncate: TRUNCATE test_stat_trunc;
 step s1_table_insert_k1: INSERT INTO test_stat_tab(key, value) VALUES('k1', 1);
 step s1_table_update_k1: UPDATE test_stat_tab SET value = value + 1 WHERE key = 'k1';
+step s1_trunc_insert_five: INSERT INTO test_stat_trunc SELECT generate_series(1, 5);
+step s1_savepoint_p2: SAVEPOINT p2;
+step s1_trunc_insert_five: INSERT INTO test_stat_trunc SELECT generate_series(1, 5);
+step s1_rollback_to_p2: ROLLBACK TO SAVEPOINT p2;
 step s1_prepare_a: PREPARE TRANSACTION 'a';
 ERROR:  prepared transactions are disabled
 step s2_rollback_prepared_a: ROLLBACK PREPARED 'a';
@@ -2981,6 +3009,16 @@ seq_scan|seq_tup_read|n_tup_ins|n_tup_upd|n_tup_del|n_live_tup|n_dead_tup|vacuum
        3|           9|        4|        2|        0|         4|         2|           0
 (1 row)
 
+step s1_trunc_ins_since_vacuum: 
+    SELECT pg_stat_get_ins_since_vacuum(tso.oid) AS n_ins_since_vacuum
+    FROM test_stat_oid AS tso
+    WHERE tso.name = 'test_stat_trunc'
+
+n_ins_since_vacuum
+------------------
+                20
+(1 row)
+
 
 starting permutation: s1_table_insert s1_table_update_k1 s1_begin s1_table_delete_k1 s1_table_insert_k1 s1_table_update_k1 s1_table_update_k1 s1_table_drop s1_prepare_a s1_rollback_prepared_a s1_ff s1_table_stats
 pg_stat_force_next_flush
diff --git a/src/test/isolation/specs/stats.spec b/src/test/isolation/specs/stats.spec
index da16710da0f..72c7b993a65 100644
--- a/src/test/isolation/specs/stats.spec
+++ b/src/test/isolation/specs/stats.spec
@@ -6,6 +6,9 @@ setup
     INSERT INTO test_stat_tab(key, value) VALUES('k0', 1);
     INSERT INTO test_stat_oid(name, oid) VALUES('test_stat_tab', 'test_stat_tab'::regclass);
 
+    CREATE TABLE test_stat_trunc(value int);
+    INSERT INTO test_stat_oid(name, oid) VALUES('test_stat_trunc', 'test_stat_trunc'::regclass);
+
     -- include 10us sleep to ensure that runtime measures as more than zero
     CREATE FUNCTION test_stat_func() RETURNS VOID LANGUAGE plpgsql AS
       $$BEGIN PERFORM pg_sleep(10e-6); END;$$;
@@ -33,6 +36,7 @@ teardown
     DROP TABLE test_slru_stats;
 
     DROP TABLE IF EXISTS test_stat_tab;
+    DROP TABLE IF EXISTS test_stat_trunc;
     DROP FUNCTION IF EXISTS test_stat_func();
     DROP FUNCTION IF EXISTS test_stat_func2();
     DROP FUNCTION test_stat_func_ifexists();
@@ -47,6 +51,10 @@ step s1_clear_snapshot { SELECT pg_stat_clear_snapshot(); }
 step s1_begin { BEGIN; }
 step s1_commit { COMMIT; }
 step s1_rollback { ROLLBACK; }
+step s1_savepoint_p1 { SAVEPOINT p1; }
+step s1_rollback_to_p1 { ROLLBACK TO SAVEPOINT p1; }
+step s1_savepoint_p2 { SAVEPOINT p2; }
+step s1_rollback_to_p2 { ROLLBACK TO SAVEPOINT p2; }
 step s1_prepare_a { PREPARE TRANSACTION 'a'; }
 step s1_commit_prepared_a { COMMIT PREPARED 'a'; }
 step s1_rollback_prepared_a { ROLLBACK PREPARED 'a'; }
@@ -97,6 +105,8 @@ step s1_table_update_k2 { UPDATE test_stat_tab SET value = value + 1 WHERE key =
 step s1_table_delete_k1 { DELETE FROM test_stat_tab WHERE key = 'k1';}
 step s1_table_truncate { TRUNCATE test_stat_tab; }
 step s1_table_drop { DROP TABLE test_stat_tab; }
+step s1_trunc_insert_five { INSERT INTO test_stat_trunc SELECT generate_series(1, 5); }
+step s1_trunc_truncate { TRUNCATE test_stat_trunc; }
 
 step s1_table_stats {
     SELECT
@@ -112,6 +122,12 @@ step s1_table_stats {
     WHERE tso.name = 'test_stat_tab'
 }
 
+step s1_trunc_ins_since_vacuum {
+    SELECT pg_stat_get_ins_since_vacuum(tso.oid) AS n_ins_since_vacuum
+    FROM test_stat_oid AS tso
+    WHERE tso.name = 'test_stat_trunc'
+}
+
 # SLRU stats steps
 step s1_slru_save_stats {
 	INSERT INTO test_slru_stats VALUES('notify', 'blks_zeroed',
@@ -565,14 +581,24 @@ permutation
   s1_begin
   s1_table_update_k1 # should *not* be counted, different rel
   s1_table_update_k1 # ditto
+  s1_trunc_insert_five
+  s1_savepoint_p1
+  s1_trunc_insert_five
+  s1_rollback_to_p1
   s1_table_truncate
+  s1_trunc_truncate
   s1_table_insert_k1 # should be counted
   s1_table_update_k1 # ditto
+  s1_trunc_insert_five
+  s1_savepoint_p2
+  s1_trunc_insert_five
+  s1_rollback_to_p2
   s1_prepare_a
   s1_ff # flush out non-transactional stats, might happen anyway
   s2_commit_prepared_a
   s2_ff
   s1_table_stats
+  s1_trunc_ins_since_vacuum
 
 # S1 prepares, S1 aborts prepared
 permutation
@@ -594,13 +620,23 @@ permutation
   s1_begin
   s1_table_update_k1 # should be counted
   s1_table_update_k1 # ditto
+  s1_trunc_insert_five
+  s1_savepoint_p1
+  s1_trunc_insert_five
+  s1_rollback_to_p1
   s1_table_truncate
+  s1_trunc_truncate
   s1_table_insert_k1 # should *not* be counted, different rel
   s1_table_update_k1 # ditto
+  s1_trunc_insert_five
+  s1_savepoint_p2
+  s1_trunc_insert_five
+  s1_rollback_to_p2
   s1_prepare_a
   s2_rollback_prepared_a
   s1_ff s2_ff
   s1_table_stats
+  s1_trunc_ins_since_vacuum
 
 
 ### 2PC: rolled back drop maintains live / dead counters
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index f10eb469266..504954987c2 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -3283,6 +3283,7 @@ TwoPhaseCallback
 TwoPhaseFileHeader
 TwoPhaseLockRecord
 TwoPhasePgStatRecord
+TwoPhasePgStatRecordV2
 TwoPhasePredicateLockRecord
 TwoPhasePredicateRecord
 TwoPhasePredicateRecordType
-- 
2.34.1

