set isolation level for postgresql stored procedures set isolation level for postgresql stored procedures postgresql postgresql

set isolation level for postgresql stored procedures


You can't do that.

What you could do is have your function check what the current transaction isolation level is and abort if it's not the one you want. You can do this by running SELECT current_setting('transaction_isolation') and then checking the result.


The language of the function makes no difference whatsoever.

This fails:

test=# create function test() returns int as $$  set transaction isolation level serializable;  select 1;$$ language sql;CREATE FUNCTIONtest=# select test();ERROR:  SET TRANSACTION ISOLATION LEVEL must be called before any queryCONTEXT:  SQL function "test" statement 1

Note that in your particular example, you could do this using a trigger on your first table. Just make sure that row count updates are done in a consistent order to avoid dead-locks, and you'll do fine in repeatable-read mode.

I'm a fan of standards

The PL/languages are platform specific.


Transaction isolation means which changes made in other concurent transactions you can access.

If you want to serialize execution you have to use locks.

You may use after row trigger and update count. "UPDATE row_counts_table" will lock table and all transactions will be serialized. It is slow.

In your example you have two statements. Insert is executed but update have to wait other transactions and count is not valid in this period.