In general I found the treatment of variables in Postgres much confusing compared to typical programming languages, and then I came to this one:
DO $$
DECLARE
x INTEGER;
BEGIN
x := 5; -- imagine doing some involved query to get to this value
RAISE NOTICE 'x is %', x;
SET app.foo = x;
RAISE NOTICE 'app.foo is %', (SELECT current_setting('app.foo'));
END;
$$;
Much to my surprise, app.foo is x
gets printed, where I expected app.foo is 5
. From there I arrive to the conclusion that the right hand side of the SET statement is not evaluated, and it has to be constant. Is this how it's supposed to be?
Is there any way to assign a result of a query into a configuration parameter?
Context: I'm using current_setting
for row level security ala https://www.enterprisedb.com/postgres-tutorials/how-implement-column-and-row-level-security-postgresql