Re: Adding per backend commit and rollback counters

From: Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Adding per backend commit and rollback counters
Date: 2025-08-10 07:47:09
Message-ID: aJhOfZ2c7XdHXOAU@ip-10-97-1-34.eu-west-3.compute.internal
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On Thu, Aug 07, 2025 at 08:17:26AM +0000, Bertrand Drouvot wrote:
> Hi,
>
> On Mon, Aug 04, 2025 at 02:20:48PM +0000, Bertrand Drouvot wrote:
> > This patch is pretty straightforward as it relies on the existing per backend
> > statistics machinery that has been added in 9aea73fc61d (so that there is not
> > that much design to discuss).
>
> Still, while working on adding more backend stats (more on that later), I
> realized that in v1, I missed to use pgstat_report_fixed (recently added in
> 793928c2d5a): the attached fixes that.
>
> Also, I think it's better to put the new xact pending counters in the existing
> PgStat_BackendPending, done that way in the attached.

Another metric that could be useful is to track the XIDs generated by backend.
That would help to see if a backend is consuming XIDs at a high rate.

We can not rely on the number of commits or rollbacks as they take into account
the virtual transactions.

So a new counter has been added in 0002 attached. Also, v3 changes the way
the statistics are displayed. I've in mind to add much more statistics per backend
(such a number of seqscans, vacuum count, analyze count..., I'll open a dedicated
thread for those) and I think that a single view to display them all makes
more sense than a lot of individual functions. This view is added in 0003.

To sum up, v3 contains:

0001 -

Adding per backend commit and rollback counters

It relies on the existing per backend statistics that has been added in
9aea73fc61d. A new function is called in AtEOXact_PgStat() to increment those
two new counters.

0002 -

Adding XID generation count per backend

This patch adds a new counter to record the number of XIDs generated per
backend. It will help to detect if a backend is consuming XIDs at a high rate.

Virtual transactions are not taken into account on purpose, we do want to track
only the XID where there is a risk of wraparound.

The counter is not part of PgStat_BackendPending, because we want to avoid
an extra function call in this code path to increment the counter in
PendingBackendStats. The counter increment here behaves more or less the same as
we do for WAL statistics.

0003 -

Adding the pg_stat_backend view

This view displays one row per server process, showing statistics related to
the current activity of that process. It currently displays the pid, the
number of XIDs generated, the number of commits, the number of rollbacks and the
time at which these statistics were last reset.

It's built on top of a new function (pg_stat_get_backend_statistics()). The idea
is the same as pg_stat_activity and pg_stat_get_activity().

Adding documentation and tests.

Regards,

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

Attachment Content-Type Size
v3-0001-Adding-per-backend-commit-and-rollback-counters.patch text/x-diff 5.7 KB
v3-0002-Adding-XID-generation-count-per-backend.patch text/x-diff 3.6 KB
v3-0003-Adding-the-pg_stat_backend-view.patch text/x-diff 12.8 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Nikhil Kumar Veldanda 2025-08-10 10:08:14 Re: Dead code with short varlenas in toast_save_datum()
Previous Message Alexander Lakhin 2025-08-10 07:00:00 Re: Test instability when pg_dump orders by OID