Re: pgstat: Flush some statistics within running transactions, take 2

From: Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com>
To: Sami Imseih <samimseih(dot)pg(at)gmail(dot)com>
Cc: Michael Paquier <michael(at)paquier(dot)xyz>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, Kyotaro Horiguchi <horikyota(dot)ntt(at)gmail(dot)com>, Lukas Fittl <lukas(at)fittl(dot)com>
Subject: Re: pgstat: Flush some statistics within running transactions, take 2
Date: 2026-11-16 10:38:56
Message-ID: avrdQFr1B681qhzR@bdtpg
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On Fri, Nov 13, 2026 at 01:22:27PM -0600, Sami Imseih wrote:
> With v19, your example reports n_tup_ins = 1 during the sleep. I added
> a test for this in stats.sql.

Thanks for the new patch version!

> I also spent some more time looking at this patch, and found a few
> unrelated fixes:
>
> 1/ I removed the CHECK_FOR_INTERRUPTS() from the flush loop. The loop
> is short anyway, and the check could let a cancel request turn the
> flush at idle into an ERROR after the query had already finished.

What about omitting the CFI only for idle flush case?

Also, I think this:

+ deadtuples -= rel->pgstat_info->tab.counts_xact.delta_dead_tuples -
+ rel->pgstat_info->tab.flushed_xact.delta_dead_tuples;

can produce a wrong n_dead_tup value after a committed TRUNCATE. For example:

"
CREATE TABLE analyze_flush_bug(id int) WITH (autovacuum_enabled = off);

CREATE PROCEDURE analyze_flush_probe(
INOUT after_first_flush bigint DEFAULT NULL,
INOUT after_analyze bigint DEFAULT NULL)
LANGUAGE plpgsql AS $$
BEGIN
BEGIN
INSERT INTO analyze_flush_bug SELECT generate_series(1, 5);
RAISE EXCEPTION 'abort subxact';
EXCEPTION WHEN OTHERS THEN
NULL;
END;

PERFORM pg_stat_force_next_flush();

SELECT n_dead_tup INTO after_first_flush
FROM pg_stat_user_tables
WHERE relname = 'analyze_flush_bug';

TRUNCATE analyze_flush_bug;
COMMIT;

ANALYZE analyze_flush_bug;

SELECT n_dead_tup INTO after_analyze
FROM pg_stat_user_tables
WHERE relname = 'analyze_flush_bug';
END
$$;

CALL analyze_flush_probe();
"

produces:

after_first_flush | after_analyze
-------------------+---------------
5 | 5

I'd expect after_analyze to be 0. Maybe the flushed live/dead tuple baselines
should also be reset when a committed TRUNCATE resets the corresponding counts?

Regards,

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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Magnus Hagander 2026-11-16 10:48:46 Re: Key joins
Previous Message Shlok Kyal 2026-11-16 10:38:43 Re: Initial table sync copies the whole table when it is no longer published