[PATCH] TRUNCATE's reset of n_live_tup/n_dead_tup is lost

From: Virender Singla <virender(dot)cse(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, bertranddrouvot(dot)pg(at)gmail(dot)com
Subject: [PATCH] TRUNCATE's reset of n_live_tup/n_dead_tup is lost
Date: 2026-11-23 11:19:30
Message-ID: CAM6Zo8zXLtvtNVgW5Qz_XH03gnUcK4P=EaypAZyNApkCAeqDcg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I was investigating wrong statistics in an ETL pipeline where a session
truncated a table, followed by an immediate load. Further analysis (with
help from LLM) pointed to a possible bug.

=== Issue Description ===

After a committed TRUNCATE, pg_stat_all_tables can keep the pre-truncate
n_live_tup, n_dead_tup and n_ins_since_vacuum. This happens when the same
backend commits another transaction that modifies rows in that table before
its pending stats are flushed.

=== Cause ===

TRUNCATE sets a "truncdropped" flag in the backend's pending entry for the
table, so that the next flush zeroes the shared live/dead counters before
adding the pending deltas. But AtEOXact_PgStat_Relations() assigns that
flag at every commit:

relstat->tab.truncdropped = trans->truncdropped;

So a later transaction that modified the table without truncating it
sets the flag back to false,
and pgstat_relation_flush_cb() then adds the new deltas on top of the old
shared counters. pgstat_twophase_postcommit() has the same assignment, so
COMMIT PREPARED of such a transaction loses the reset as well.

=== Reproducer ===

CREATE TABLE t (a int) WITH (autovacuum_enabled = off);
INSERT INTO t SELECT generate_series(1, 1000);
UPDATE t SET a = a WHERE a <= 100;
SELECT pg_stat_force_next_flush();

DO $$
BEGIN
TRUNCATE t;
COMMIT;
INSERT INTO t VALUES (1);
COMMIT;
END $$;
SELECT pg_stat_force_next_flush();

SELECT n_live_tup, n_dead_tup, n_ins_since_vacuum
FROM pg_stat_user_tables WHERE relname = 't';

actual: 1001 | 100 | 1001
expected: 1 | 0 | 1

=== Fix ===

The attached patch only ever sets the flag at commit, in both places.
Nothing else needs to clear it: the pending entry is freed after a
successful flush, and if the flush is skipped the reset is still owed. A
rolled-back TRUNCATE never sets it, since the flag is only touched in the
commit branch. The patch adds a regression test for the regular commit
path.

Hi Bertrand,

I saw your thread from earlier today [1] touching the same area. It looks
like this case is not covered by your patch series. Assuming this is
an issue, feel
free to add this into your patches, or we can handle it separately here. I
haven't rebased on top of your changes yet.

[1] https://www.postgresql.org/message-id/flat/awQF360iTx5Q4xEA%40bdtpg

Thanks,
Virender

Attachment Content-Type Size
0001-Don-t-lose-TRUNCATE-s-reset-of-live-dead-tuple-counters.patch application/octet-stream 6.9 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jakub Wartak 2026-11-23 11:20:56 Re: [RFC PATCH v1] On-demand WAL replay: accept connections before crash recovery has applied the WAL
Previous Message Álvaro Herrera 2026-11-23 10:53:24 Re: MERGE fails to reject source references in NOT MATCHED BY SOURCE actions