pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Can functions containing a CTE be PARALLEL SAFE?
3+ messages / 2 participants
[nested] [flat]

* Can functions containing a CTE be PARALLEL SAFE?
@ 2019-10-17 03:09  Erwin Brandstetter <brsaweda@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Erwin Brandstetter @ 2019-10-17 03:09 UTC (permalink / raw)
  To: pgsql-general

[The manual for Postgres 12 says][1]:

> The following operations are always parallel restricted.

>  - Scans of common table expressions (CTEs).
>  - Scans of temporary tables.
>  - ...

Further down on the same [manual page:][2]

> [...] 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.

No mention of CTEs.

I searched the list archives and found a statement from Thomas Munro
[here][3]:

> That means that these CTEs can only be scanned in the leader process.

Now I am unsure whether I can use `PARALLEL SAFE` for functions containing
a CTE (while  fulfilling all other criteria)?

Would the new inlining of CTEs in Postgres 12 have any role in this?

I posted a [similar question on dba.SE][4].

Regards
Erwin Brandstetter

  [1]: https://www.postgresql.org/docs/12/parallel-safety.html
  [2]:
https://www.postgresql.org/docs/12/parallel-safety.html#PARALLEL-LABELING
  [3]:
https://www.postgresql.org/message-id/CAEepm=03s4Yih+c0pCTfKi0O8zgq-P4VfCVEk5VVXcR6aXHm4A@mail.gmail...
  [4]: https://dba.stackexchange.com/q/251274/3684


 That means that these CTEs can only be scanned in the
leader process.

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Can functions containing a CTE be PARALLEL SAFE?
@ 2019-10-17 09:20  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Erwin Brandstetter <brsaweda@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2019-10-17 09:20 UTC (permalink / raw)
  To: Erwin Brandstetter <brsaweda@gmail.com>; +Cc: pgsql-general

Erwin Brandstetter <brsaweda@gmail.com> writes:
>> The following operations are always parallel restricted.
>> - Scans of common table expressions (CTEs).

> Now I am unsure whether I can use `PARALLEL SAFE` for functions containing
> a CTE (while  fulfilling all other criteria)?

AFAIR, the reason for treating CTEs as parallel restricted is simply to
guarantee single evaluation of the CTE.  Within a function, that would
only matter per-function-execution, so I can't see why a function
containing such a query couldn't be pushed down to workers for execution.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Can functions containing a CTE be PARALLEL SAFE?
@ 2019-10-17 13:13  Erwin Brandstetter <brsaweda@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Erwin Brandstetter @ 2019-10-17 13:13 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general

Makes sense, thanks for the confirmation.
Maybe clarify in the manual?

Regards
Erwin

On Thu, Oct 17, 2019 at 11:20 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:

> Erwin Brandstetter <brsaweda@gmail.com> writes:
> >> The following operations are always parallel restricted.
> >> - Scans of common table expressions (CTEs).
>
> > Now I am unsure whether I can use `PARALLEL SAFE` for functions
> containing
> > a CTE (while  fulfilling all other criteria)?
>
> AFAIR, the reason for treating CTEs as parallel restricted is simply to
> guarantee single evaluation of the CTE.  Within a function, that would
> only matter per-function-execution, so I can't see why a function
> containing such a query couldn't be pushed down to workers for execution.
>
>                         regards, tom lane
>

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2019-10-17 13:13 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-10-17 03:09 Can functions containing a CTE be PARALLEL SAFE? Erwin Brandstetter <brsaweda@gmail.com>
2019-10-17 09:20 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-10-17 13:13   ` Erwin Brandstetter <brsaweda@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox