parallel safe on user defined functions

From: "Voillequin, Jean-Marc" <Jean-Marc(dot)Voillequin(at)moodys(dot)com>
To: "pgsql-sql(at)lists(dot)postgresql(dot)org" <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: parallel safe on user defined functions
Date: 2020-12-02 15:25:37
Message-ID: DM6PR20MB3329006C838039F0B3F43E64BEF30@DM6PR20MB3329.namprd20.prod.outlook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

-----------------------------------------

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2020-12-02 16:15:35 Re: parallel safe on user defined functions
Previous Message Samed YILDIRIM 2020-12-02 14:44:43 Re: Join push down on FDW partitions