Thread: parallel safe on user defined functions
Hello,
I want to mark some functions as parallel safe, but I think it’s difficult to understand the documentation:
"Functions and aggregates must be marked PARALLEL UNSAFE if they write to the database, access sequences, change the transaction state even temporarily
(e.g., a PL/pgSQL function which establishes an EXCEPTION block to catch errors), or make persistent changes to settings.
Similarly, functions must be marked PARALLEL RESTRICTED if they access temporary tables, client connection state, cursors, prepared statements,
or miscellaneous backend-local state which the system cannot synchronize across workers.
For example, setseed and random are parallel restricted for this last reason."
For instance, is the following plpython3u function parallel safe?
create or replace function f1(key text, val_default in text default null) returns text as $$
return GD.get(key.lower(),val_default)
$$ language plpython3u;
=> Is the Python GD dictionary a "miscellaneous backend-local state" ?
Same question for a function that raises an exception with the keyword "raise":
create or replace function ...
...
raise invalid_datetime_format;
...
end;
$$ language plpgsql;
=> Is it a transaction state change?
Same question for a function that performs an "execute into":
create or replace function f2(d date, n numeric) returns date as $$
declare
ret date;
begin
execute format('select (''%s''::date + interval ''%s'')::date',d,concat(n,' day')) into ret;
return ret;
end;
$$ language plpgsql;
=> Is this execute statement be considered as a cursor?
Thanks!
This email was sent to you by Moody’s Investors Service EMEA Limited
Registered office address:
One Canada Square
Canary Wharf
London, E14 5FA
Registered in England and Wales No: 8922701
"Voillequin, Jean-Marc" <Jean-Marc.Voillequin@moodys.com> writes: > For instance, is the following plpython3u function parallel safe? > create or replace function f1(key text, val_default in text default null) returns text as $$ > return GD.get(key.lower(),val_default) > $$ language plpython3u; No. There's no mechanism for sharing Python interpreter state across processes. > => Is it a transaction state change? Throwing an error is always OK (we could hardly decree otherwise). > execute format('select (''%s''::date + interval ''%s'')::date',d,concat(n,' day')) into ret; > => Is this execute statement be considered as a cursor? No, it's just a weird way of spelling a variable assignment. regards, tom lane