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

Started by Virender Singla6 days ago3 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1Virender Singla
virender.cse@gmail.com

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]/messages/by-id/awQF360iTx5Q4xEA@bdtpg 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]: /messages/by-id/awQF360iTx5Q4xEA@bdtpg

Thanks,
Virender

Attachments:

t259658_1
0001-Don-t-lose-TRUNCATE-s-reset-of-live-dead-tuple-counters.patchapplication/octet-stream; name=0001-Don-t-lose-TRUNCATE-s-reset-of-live-dead-tuple-counters.patchDownload+49-6
#2Bertrand Drouvot
bertranddrouvot.pg@gmail.com
In reply to: Virender Singla (#1)
Re: [PATCH] TRUNCATE's reset of n_live_tup/n_dead_tup is lost

Hi,

On Mon, Nov 23, 2026 at 04:49:30PM +0530, Virender Singla wrote:

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.

Thanks for the report and the patch!

IIUC, your report is the same issue already addressed by Ilya's v2-0001 in [1]/messages/by-id/1DC9D815-8754-4A76-B2B1-99C1A614A55A@localhost.localdomain.

This issue is independent from my series, which preserves the pre and post
TRUNCATE n_ins_since_vacuum and n_dead_tup counts through aborts, subtransactions
and 2PC.

I think those fixes should remain in separate patch series and threads.

[1]: /messages/by-id/1DC9D815-8754-4A76-B2B1-99C1A614A55A@localhost.localdomain

Regards,

--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

#3Virender Singla
virender.cse@gmail.com
In reply to: Bertrand Drouvot (#2)
Re: [PATCH] TRUNCATE's reset of n_live_tup/n_dead_tup is lost

IIUC, your report is the same issue already addressed by Ilya's v2-0001 in [1].

I tested this patch and it covers the cases I have described. So
closing the thread here.

Thanks Bertrand for pointing this out.

-Virender