One way is to use the custom_variable_classes configuration custom_variable_classes . It seems to be intended to configure additional modules, but can also be used to store arbitrary values ββin the current database session.
In postgresql.conf you need to add something along the lines of the following contents:
custom_variable_classes = 'local'
The first time you connect to the database, you can store any required information in a user class, for example:
SET local.userid = 'foobar';
And later, you can get this value using the current_setting() function:
SELECT current_setting('local.userid');
Adding an entry to the log table might look something like this:
INSERT INTO audit_log VALUES (now(), current_setting('local.userid'), ...)
davea
source share