HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

set session - custom variable to store user id

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
userstorecustomsessionvariableset

Problem

I want to store user id in custom session variable and use it (read) in trigger procedures to authorize user actions. I found something like this:

set session "myapp.user" = '12345';
...
SELECT current_setting('myapp.user');


and it seems to work - I thought that "myapp.user" should be declared in .conf file but it seems that I can create session variables on the fly (I didn't change .conf file at all).

Is there any disadvantage of doing like this?

Solution

Before version 9.2, you needed to add your custom class variable to custom_variable_classes parameter in postgresql.conf, like:

custom_variable_classes = 'myapp'


In 9.2, this requirement has been removed:


Remove the custom_variable_classes parameter (Tom Lane)


The checking provided by this setting was dubious. Now any setting can
be prefixed by any class name.

So, since 9.2 you can just set your custom class variable as you are doing currently, no need to worry about changing postgresql.conf.

Code Snippets

custom_variable_classes = 'myapp'

Context

StackExchange Database Administrators Q#97095, answer score: 10

Revisions (0)

No revisions yet.