5

In relation to my other question "What’s the best way to audit log DELETEs?". What's the PostgreSQL equivalent of CONTEXT_INFO?

I want to log deletes using a trigger, but since I'm not using the database user as my app's logical user, I cannot log the CURRENT_USER from the trigger code as the user who deleted the record. But for INSERT and UPDATE it is possible to log the record changes from trigger since you can just add a user field in the record, say inserted_by and last_updated_by, and use these fields to log to audit table.

Dale K
  • 25,246
  • 15
  • 42
  • 71
Hao
  • 8,047
  • 18
  • 63
  • 92
  • 1
    Please remember that not everybody knows MSSQL, so saying "what's the equivalent of XXX" is not really well stated question - explain what CONTEXT_INFO is, and what it is used for. Then - somebody with Pg skills can check how to get given functionality. –  Mar 30 '09 at 17:58

2 Answers2

2

http://www.postgres.cz/index.php/PostgreSQL_SQL_Tricks_II#Any_other_session_variables

PostgreSQL provides more variants, please, look to this site.

Pavel Stehule
  • 42,331
  • 5
  • 91
  • 94
Milen A. Radev
  • 60,241
  • 22
  • 105
  • 110
1

The accepted answer is outdated.

In recent versions of postgresql (since 9.3 or 9.4 I think), one can set / retrieve config variables are only alive for the current session [or transaction]. Documentation Reference

example of setting a session variable:

hal=# select set_config('myvar.foo', 'bar', false);
 set_config 
------------
 bar
(1 row)

hal=# select current_setting('myvar.foo');
 current_setting 
-----------------
 bar
(1 row)

It is possible to use the function current_setting as one would use other set returning functions.

example:

create table customers (id int);
insert into customers values (1), (2), (3), (4);

select c.*, s.*
from customers c
left join current_setting('myvar.foo') s
       on c.id = length(s)
Haleemur Ali
  • 26,718
  • 5
  • 61
  • 85