<xref linkend="guc-max-parallel-workers-per-gather"/> must be set to a
value that is greater than zero. This is a special case of the more
general principle that no more workers should be used than the number
configured via <varname>max_parallel_workers_per_gather</varname>.
</para>
</listitem>
</itemizedlist>
<para>
In addition, the system must not be running in single-user mode. Since
the entire database system is running as a single process in this situation,
no background workers will be available.
</para>
<para>
Even when it is in general possible for parallel query plans to be
generated, the planner will not generate them for a given query
if any of the following are true:
</para>
<itemizedlist>
<listitem>
<para>
The query writes any data or locks any database rows. If a query
contains a data-modifying operation either at the top level or within
a CTE, no parallel plans for that query will be generated. As an
exception, the following commands, which create a new table and populate
it, can use a parallel plan for the underlying <literal>SELECT</literal>
part of the query:
<itemizedlist>
<listitem>
<para><command>CREATE TABLE ... AS</command></para>
</listitem>
<listitem>
<para><command>SELECT INTO</command></para>
</listitem>
<listitem>
<para><command>CREATE MATERIALIZED VIEW</command></para>
</listitem>
<listitem>
<para><command>REFRESH MATERIALIZED VIEW</command></para>
</listitem>
</itemizedlist>
</para>
</listitem>
<listitem>
<para>
The query might be suspended during execution. In any situation in
which the system thinks that partial or incremental execution might
occur, no parallel plan is generated. For example, a cursor created
using <link linkend="sql-declare">DECLARE CURSOR</link> will never use
a parallel plan. Similarly, a PL/pgSQL loop of the form
<literal>FOR x IN query LOOP .. END LOOP</literal> will never use a
parallel plan, because the parallel query system is unable to verify
that the code in the loop is safe to execute while parallel query is
active.
</para>
</listitem>
<listitem>
<para>
The query uses any function marked <literal>PARALLEL UNSAFE</literal>.
Most system-defined functions are <literal>PARALLEL SAFE</literal>,
but user-defined functions are marked <literal>PARALLEL
UNSAFE</literal> by default. See the discussion of
<xref linkend="parallel-safety"/>.
</para>
</listitem>
<listitem>
<para>
The query is running inside of another query that is already parallel.
For example, if a function called by a parallel query issues an SQL
query itself, that query will never use a parallel plan. This is a
limitation of the current implementation, but it may not be desirable
to remove this limitation, since it could result in a single query
using a very large number of processes.
</para>
</listitem>
</itemizedlist>
<para>
Even when a parallel query plan is generated for a particular query, there
are several circumstances under which it will be impossible to execute
that plan in parallel at execution time. If this occurs, the leader
will execute the portion of the plan below the <literal>Gather</literal>
node entirely by itself, almost as if the <literal>Gather</literal> node were
not present. This will happen if any of the following conditions are met:
</para>
<itemizedlist>
<listitem>
<para>
No background workers can be obtained because of the limitation that
the total number of background workers cannot exceed
<xref linkend="guc-max-worker-processes"/>.