Re: New feature request for adding session information to PostgreSQL transaction log

From: Andres Freund <andres(at)anarazel(dot)de>
To: Chapman Flack <jcflack(at)acm(dot)org>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Sumanth Vishwaraj <sumanth(dot)vishwaraj(at)oracle(dot)com>, "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Mahesh Rao <mahesh(dot)p(dot)rao(at)oracle(dot)com>, Nazia Zaidi <najiah(dot)abide(at)oracle(dot)com>, Jitesh Tiwari <jitesh(dot)tiwari(at)oracle(dot)com>, Nick Wagner <nick(dot)wagner(at)oracle(dot)com>, Scott Corbin <scott(dot)corbin(at)oracle(dot)com>, Mack Bell <mack(dot)bell(at)oracle(dot)com>, Avinash Dubey <avinash(dot)x(dot)dubey(at)oracle(dot)com>
Subject: Re: New feature request for adding session information to PostgreSQL transaction log
Date: 2025-01-21 18:50:23
Message-ID: c6vi7ysizbuitfjtagw5cgrbjasc2waurpw6ktvuctenbzs7ds@seiadfyqbo6l
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On 2025-01-21 13:28:43 -0500, Chapman Flack wrote:
> On 01/19/25 12:02, Tom Lane wrote:
> > You can build that yourself, typically by adding a trigger that stores
> > the value of "current_user" into inserted/updated rows. (If you want
> > to also track deletions, a separate audit log table would work
> > better.) The event-trigger feature might also be useful.
>
> I wonder how close one could get to the customer request (better
> forensics without having to build extra columns and triggers at the
> SQL level) with an extension and existing hooks.
>
> I haven't used it, but isn't there now a facility for inserting
> additional custom records into the WAL? With ClientAuthentication_hook,
> could an extension add a record there for the creation of a new session,
> with timestamp and authenticated role oid?

I'd probably not create custom records, I'd just use pg_logical_emit_message()
with an appropriate prefix. That way you can emit both transactional and
non-transactional records etc

> Could an XactCallback be used to add a custom record at commit time
> identifying the responsible session? There would then be enough breadcrumbs
> to follow forensically from the commit to the session to the credentials.

Yes.

> An added custom record at commit time likely costs more in space than
> extending the existing commit record with a session id, but seems like
> something an extension could do without changes in core.

The added space overhead should be small enough to not really matter in most
scenarios. Unless you do a lot of tiny tiny transaction it's not going to be a
lot compared of the size of WAL for actual DML.

The one issue I see is that it's not quite trivial to emit a WAL record with
extra information for a transaction iff the transaction actually performed
DML.

Greetings,

Andres Freund

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2025-01-21 18:52:30 Re: SQL:2011 application time
Previous Message Artem Gavrilov 2025-01-21 18:47:37 Re: [PATCH] Optionally record Plan IDs to track plan changes for a query