parallel safe on user defined functions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

parallel safe on user defined functions

Voillequin, Jean-Marc

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!

-----------------------------------------
Moody's monitors email communications through its networks for regulatory compliance purposes and to protect its customers, employees and business and where allowed to do so by applicable law. The information contained in this e-mail message, and any attachment thereto, is confidential and may not be disclosed without our express permission. If you are not the intended recipient or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution or copying of this message, or any attachment thereto, in whole or in part, is strictly prohibited. If you have received this message in error, please immediately notify us by telephone, fax or e-mail and delete the message and all of its attachments. Every effort is made to keep our network free from viruses. You should, however, review this e-mail message, as well as any attachment thereto, for viruses. We take no responsibility and have no liability for any computer virus which may be transferred via this e-mail message.

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
-----------------------------------------
Reply | Threaded
Open this post in threaded view
|

Re: parallel safe on user defined functions

Tom Lane-2
"Voillequin, Jean-Marc" <[hidden email]> 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