Home Explore Blog CI



postgresql

10th chunk of `doc/src/sgml/parallel.sgml`
03fc227f0335c2c1cd5db5b4d13cfe5d0d57d27459d9dd3c0000000100000bbd
 <xref linkend="sql-alterfunction"/>, markings can be set by specifying
    <literal>PARALLEL SAFE</literal>, <literal>PARALLEL RESTRICTED</literal>, or
    <literal>PARALLEL UNSAFE</literal> as appropriate.  When using
    <xref linkend="sql-createaggregate"/>, the
    <literal>PARALLEL</literal> option can be specified with <literal>SAFE</literal>,
    <literal>RESTRICTED</literal>, or <literal>UNSAFE</literal> as the corresponding value.
  </para>

  <para>
    Functions and aggregates must be marked <literal>PARALLEL UNSAFE</literal>
    if they write to the database, change the transaction state (other than by
    using a subtransaction for error recovery), access sequences, or make
    persistent changes to
    settings.  Similarly, functions must be marked <literal>PARALLEL
    RESTRICTED</literal> if they access temporary tables, client connection state,
    cursors, prepared statements, or miscellaneous backend-local state that
    the system cannot synchronize across workers. For example,
    <literal>setseed</literal> and <literal>random</literal> are parallel restricted for
    this last reason.
  </para>

  <para>
    In general, if a function is labeled as being safe when it is restricted or
    unsafe, or if it is labeled as being restricted when it is in fact unsafe,
    it may throw errors or produce wrong answers when used in a parallel query.
    C-language functions could in theory exhibit totally undefined behavior if
    mislabeled, since there is no way for the system to protect itself against
    arbitrary C code, but in most likely cases the result will be no worse than
    for any other function. If in doubt, it is probably best to label functions
    as <literal>UNSAFE</literal>.
  </para>

  <para>
    If a function executed within a parallel worker acquires locks that are
    not held by the leader, for example by querying a table not referenced in
    the query, those locks will be released at worker exit, not end of
    transaction. If you write a function that does this, and this behavior
    difference is important to you, mark such functions as
    <literal>PARALLEL RESTRICTED</literal>
    to ensure that they execute only in the leader.
  </para>

  <para>
    Note that the query planner does not consider deferring the evaluation of
    parallel-restricted functions or aggregates involved in the query in
    order to obtain a superior plan.  So, for example, if a <literal>WHERE</literal>
    clause applied to a particular table is parallel restricted, the query
    planner will not consider performing a scan of that table in the parallel
    portion of a plan.  In some cases, it would be
    possible (and perhaps even efficient) to include the scan of that table in
    the parallel portion of the query and defer the evaluation of the
    <literal>WHERE</literal> clause so that it happens above the <literal>Gather</literal>
    node.  However, the planner does not do this.
  </para>

 </sect2>

 </sect1>

 </chapter>

Title: Parallel Safety Considerations for Functions and Aggregates
Summary
Functions and aggregates must be labeled as PARALLEL SAFE, PARALLEL RESTRICTED, or PARALLEL UNSAFE to ensure correct behavior in parallel queries. Incorrect labeling can lead to errors or wrong answers. The query planner does not consider deferring evaluation of parallel-restricted functions, and certain behaviors like lock acquisition and release can differ between parallel and non-parallel execution.