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

From: Sami Imseih <samimseih(dot)pg(at)gmail(dot)com>
To: Michael Paquier <michael(at)paquier(dot)xyz>
Cc: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org, Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com>, 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-12-04 02:59:48
Message-ID: CAN12+YKFEOP+8XRNX3PT=Sfu5zEzT2cLdALJO+NSEnPuagvr2Q@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I spent some time today looking at whether the current patches will
work if we choose to go the next step and put some automation in core
around flushing stats. They mostly do, but it turned up one more change
needed, which is a way to flush without waiting for locks, which I discuss at
the end.

The automated in-transaciton flush was raised in the
pg_stat_statements scalability
thread [1], and Lukas mentioned the following:

```
I've been poking at this locally with a few different use cases I
have, and keep coming back to what I think is the biggest open issue
in v5 and v6: When we flush, and how we can bridge the gap between
what the cumulative statistics system does today and what
pg_stat_statements users expect.
```

and later on:

```
I think what we need is an in-core mechanism that can flush stats
kinds opting in at statement end, at most once a second, or while idle
(including idle in transaction) within about 10 seconds.
```

I agree with this. We will not provide updated stats at the end of
every execution while in a transaction, but we should at least flush
automatically following the same semantics we have today at
transaction boundaries, at most once per second, and within 10
seconds if the session goes idle in transaction, so the last
statement's stats are not held until commit. That is the same
behavior as the transaction boundary flush, applied per statement.

The attached nocfbot patch, on top of the current set, does this. It
builds on the earlier example I posted in [2], which flushed
non-transactional stats at statement boundaries throttled to once
every 10 seconds, and adds the following.

1/ Statement boundaries are throttled with PGSTAT_MIN_INTERVAL, using
instr_time rather than GetCurrentTimestamp(). Thanks to 294520c4448,
instr_time reads the TSC where available, so there is no extra
gettimeofday() per statement.

2/ The idle case reuses IDLE_STATS_UPDATE_TIMEOUT with
PGSTAT_IDLE_INTERVAL, the same as outside a transaction.

3/ The automatic flush does not wait for locks.

I am also proposing a stats_flush_in_transaction GUC, on by default,
to disable in-transaction flushes. A GUC was also discussed in the
first proposal [3], with opinions split between making this
configurable and hardcoding it like the other PGSTAT_*_INTERVAL
values. I am on the fence about this one. The benchmarks I ran don't
show any regression, but this is a behavior change that a user may
want to turn off. I am not 100% sure we need it, and that can be
decided later on.

As Lukas mentioned, this can't be fully guaranteed for long-running
statements, since there is no facility to flush mid-query, and IMO we
would not want to go down that route. The POC also flushes the
non-transactional stats of all kinds rather than only kinds that opt
in, as Lukas suggested; that could be a flag in PgStat_KindInfo as a
follow-up. Logical replication apply workers are not covered yet.

I ran benchmarks on c7i instances from 4 to 32 vCPUs and did not see
measurable overhead. The details are in the commit message.

As mentioned at the top, I think the non-blocking flush option should be
added, but I will hold off on that for now.

```
long
pgstat_report_stat(bool force)
{
return pgstat_report_stat_internal(force, false);
}

static long pgstat_report_stat_internal(bool force, bool never_wait);
```

[1] https://www.postgresql.org/message-id/CAP53PkzizggqCeS2bsXYjfLyJi2MUNWnu_85-rhz%3DwfjZ3ypng%40mail.gmail.com
[2] https://www.postgresql.org/message-id/CAA5RZ0u84eMFeFWMaEo0D84ed3jF_RY0%3DRWD8tDwQWsNsU1qvA%40mail.gmail.com
[3] https://www.postgresql.org/message-id/flat/aWTVEycKj7Qh/SXH(at)ip-10-97-1-34(dot)eu-west-3(dot)compute(dot)internal

--
Sami Imseih
Amazon Web Services (AWS)

Attachment Content-Type Size
nocfbot-0001-pgstat-Flush-pending-stats-at-statement-boundaries-w.patch application/octet-stream 12.8 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message John Naylor 2026-12-04 03:09:00 Re: memory leak in bounded sorts
Previous Message Michael Paquier 2026-12-04 02:59:05 Re: Reapply graceful socket shutdown on Windows (revert 29992a6a509)