skip to Main Content
CREATE OR REPLACE FUNCTION wrapperFunction()
  RETURNS bytea AS
$BODY$
BEGIN
    SELECT function1() || function2();
END;
$BODY$
  LANGUAGE plpgsql

Is there a way to make calls to function1 and function2 parallel?

(they are read only, no side effects and independent)

2

Answers


  1. No, you cannot do that.

    To call two functions in parallel, you need to have two database sessions.

    Login or Signup to reply.
  2. You can mark functions as parallel safe.

    This requires that your function is actually safe (otherwise you can end up with wrong answers, etc.), and it’s up to the query planner to actually determine that the functions will run in parallel.

    There’s also quite a few limitations that your function need to adhere to:

    The following operations are always parallel restricted:

    • Scans of common table expressions (CTEs).
    • Scans of temporary tables.
    • Scans of foreign tables, unless the foreign data wrapper has an IsForeignScanParallelSafe API that indicates otherwise.
    • Plan nodes to which an InitPlan is attached.
    • Plan nodes that reference a correlated SubPlan.

    So the answer is "it depends". It might be easier to cache the result in some way (update a computed column) or make it answerable by a join – if possible.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search