The effect of set_config(is_local => true)
seems to be restrained to the implicit subtransaction created by your BEGIN … EXCEPTION
block.
(Skip to the end of my answer for my TL;DR solution to your problem.)
Here's what happens when I do something similar, but with the call to set_config()
outside of the BEGIN … EXCEPTION
subtransaction:
create or replace function set_config_outside_of_begin_except_block(denominator int)
returns bool
language 'plpgsql'
as $body$
begin
perform set_config('my.setting', 'set before subtransaction', true);
begin
perform 10 / denominator;
return true;
exception when division_by_zero then
return false;
end;
end;
$body$;
begin
select current_setting('my.setting') as my_setting;
select set_config_outside_of_begin_except_block(1);
rollback;
CREATE FUNCTION
BEGIN
ERROR: unrecognized configuration parameter "my.setting"
set_config_outside_of_begin_except_block
------------------------------------------
t
(1 row)
my_setting
---------------------------
set before subtransaction
(1 row)
ROLLBACK
Having moved the set_config()
call to before the BEGIN … EXCEPTION
block, the new setting for my.setting
has persisted outside our function call.
And, as you will next see, it doesn't even matter if an exception occurs in the above BEGIN … EXCEPTION
block, which makes sense, given that the set_config()
is called before that block/subtransaction is entered:
begin
select current_setting('my.setting') as my_setting;
rollback;
BEGIN
set_config_outside_of_begin_except_block
------------------------------------------
f
(1 row)
my_setting
---------------------------
set before subtransaction
(1 row)
ROLLBACK
Now, I will modify the code a bit to come closer to your example, and closer to reproducing the behavior you describe:
create or replace function set_config_in_begin_except_block(denominator int)
returns bool
language 'plpgsql'
as $body$
begin
perform set_config('my.setting', 'set in subtransaction', true);
perform 10 / denominator;
return true;
exception when division_by_zero then
return false;
end;
$body$;
begin;
select set_config_in_begin_except_block(0);
select current_setting('my.setting') as my_setting;
rollback;
CREATE FUNCTION
BEGIN
set_config_in_begin_except_block
----------------------------------
f
(1 row)
my_setting
------------
(1 row)
ROLLBACK
begin
select set_config_in_begin_except_block(1);
select current_setting('my.setting') as my_setting;
rollback;
BEGIN
set_config_in_begin_except_block
----------------------------------
t
(1 row)
my_setting
-----------------------
set in subtransaction
(1 row)
ROLLBACK
Note that, now,
- the
BEGIN … EXCEPTION
block does cause the change to my.setting
to get lost,
- but only if an exception (
division_by_zero
) indeed got raised,
- even though the
division_by_zero
was triggered by the statement after set_config()
.
So, it's somewhat close to the behaviour you describe, but not quite. Your setting didn't persist outside of your BEGIN … EXCEPTION
block regardless of whether a no_data_found
exception was hit or not.
As a little detour: I stumbled upon your question, because I was interested in whether I could treat the settings like a stack that unwinds together with the (sub)transaction stack. It turns out that I could:
create or replace function set_config_stacked(denominator int)
returns bool
language 'plpgsql'
as $body$
begin
perform set_config('my.setting', 'set before substraction', true);
begin
perform set_config('my.setting', 'set within subtransaction before division', true);
perform 10 / denominator;
return true;
exception when division_by_zero then
return false;
end;
end;
$body$;
begin;
select set_config_stacked(1);
select current_setting('my.setting') as my_setting;
rollback;
CREATE FUNCTION
BEGIN
set_config_stacked
--------------------
t
(1 row)
my_setting
-------------------------------------------
set within subtransaction before division
(1 row)
ROLLBACK
begin;
select set_config_stacked(0);
select current_setting('my.setting') as my_setting;
rollback;
BEGIN
set_config_stacked
--------------------
f
(1 row)
my_setting
-------------------------
set before substraction
(1 row)
ROLLBACK
So my.setting
is actually restored to its previous setting when the subtransaction of the BEGIN … EXCEPTION
block closes.
I need one final change to reproduce your function's behaviour:
create table tab (id uuid primary key, stuff text);
insert into tab (id, stuff) values (uuid('180e1b14-21e5-4e66-a9b8-db09139d6278'), 'some stuff');
create or replace function set_config_stacked(id$ uuid)
returns bool
language 'plpgsql'
as $body$
declare
rec tab%rowtype;
begin
perform set_config('my.setting', 'set before substraction', true);
begin
perform set_config('my.setting', 'set within subtransaction before SELECT', true);
select * into strict rec from tab where id = id$;
perform set_config('my.setting', 'set within subtransaction after SELECT', true);
return true;
exception when no_data_found then
return false;
end;
end;
$body$;
begin;
select set_config_stacked(uuid('180e1b14-21e5-4e66-a9b8-db09139d6278')); -- exist
select current_setting('my.setting') as my_setting;
rollback;
CREATE TABLE
INSERT 1 0
CREATE FUNCTION
set_config_stacked
--------------------
t
(1 row)
my_setting
----------------------------------------
set within subtransaction after SELECT
(1 row)
ROLLBACK
begin;
select set_config_stacked(uuid('cc7ad0c3-7e3a-49a0-b7d8-7b4093ae0028')); -- doesn't exist
select current_setting('my.setting') as my_setting;
rollback;
BEGIN
set_config_stacked
--------------------
f
(1 row)
my_setting
-------------------------
set before substraction
(1 row)
ROLLBACK
As you can see, there is still one aspect of the behavior that you describe which I cannot reproduce. This version of set_config_stacked()
that operates around a no_data_found
exception behaves the same as the previous version based around the division_by_zero
exception.
However, your example suggests that your settings also don't persist outside of auth.authorize()
when no exception is hit. That confounds me, and is not something I am able to reproduce, at least here on Postgres 14.
Regardless of this, your problem can be solved by moving the calls to set_config()
below the BEGIN … EXCEPTION … END
block. You will still want to set your variables within that block, but do these calls and the return true
from a new to be created outer BEGIN … END
block (without EXCEPTION
).
auth.authorizations
andperform set_config('auth.tenant_id', v_tenant_id::text, true);
never happens. What happens if you run:select * from auth.authorize(uuid('180e1b14-21e5-4e66-a9b8-db09139d6278'));
by itself? Does it returntrue
orfalse
?is_local
tofalse
inset_config
then it works, so generally the function works, but I only want the variables to exist during the transaction - so maybe I have some fundamental misunderstanding of postgres transactions?select * from auth.authorize(...)
return when run by itself? Also in your function do something like:select into _tenant_id current_setting('auth.tenant_id')
where_tenant_id
is set up in theDECLARE
portion and thenRAISE NOTICE 'tenant_id is %', _tentant_id;
. Just to see if it is actually set in the function.psql
. If it fails there then I know I have a problem. Otherwise failures when moving up to other clients is a client issue, generally the client being 'helpful'.