The manual for Postgres 12 says:
The following operations are always parallel restricted.
- Scans of common table expressions (CTEs).
- Scans of temporary tables.
- ...
Further down on the same manual page:
[...] 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
andrandom
are parallel restricted for this last reason.
No mention of CTEs. Now I am unsure whether I can use PARALLEL SAFE
for functions containing a CTE. It would make sense to me for those to be only PARALLEL RESTRICTED
.
Context: I have to determine the best label for existing user-defined functions. The setting is new since Postgres 9.6, and can have a massive impact on performance as operations involving functions that are not PARALLEL SAFE
will not be performed by parallel workers, PARALLEL RESTRICTED
only by the leader. (And PARALLEL USAFE
disables parallelism altogether.)
I posted a related question on pgsql-general.
parallel safe
as this is about possible side effects. Of course your CTE contains DML, that the function is not parallel safe