pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Explain says 8 workers planned, only 1 executed
6+ messages / 4 participants
[nested] [flat]

* Explain says 8 workers planned, only 1 executed
@ 2020-03-21 17:25  Alastair McKinley <a.mckinley@analyticsengines.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Alastair McKinley @ 2020-03-21 17:25 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi all,

I have a long running query that I have tweaked along with config (e.g. min_parallel_table_scan_size) to execute nicely and very fast in parallel which works as expected executed directly from psql client.  The query is then embedded in a psql function like "return query select * from function_that_executes_the_query()".

I am checking the explain output (using execute explain $query) just before executing inside my function and it the plan is identical to what I would expect, planning 8 workers.  However, this query actually only uses 1 worker and takes many times longer than when ran directly on the psql command line with the same server configuration parameters.

Why would the explain output be different from the executed plan? Is this a limitation of plpgsql functions? Is there any way to debug this further?

If it is meaningful during parallel execution I notice lots of "postgres: parallel worker" proceses in top and when executing from my function just a single "postgres: $user $db $host($pid) SELECT" processes.

Best regards,

Alastair

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

* Re: Explain says 8 workers planned, only 1 executed
@ 2020-03-21 17:38  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Alastair McKinley <a.mckinley@analyticsengines.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Adrian Klaver @ 2020-03-21 17:38 UTC (permalink / raw)
  To: Alastair McKinley <a.mckinley@analyticsengines.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On 3/21/20 10:25 AM, Alastair McKinley wrote:
> Hi all,
> 
> I have a long running query that I have tweaked along with config (e.g. 
> min_parallel_table_scan_size) to execute nicely and very fast in 
> parallel which works as expected executed directly from psql client.  
> The query is then embedded in a psql function like "return query select 
> * from function_that_executes_the_query()".

Postgres version?

What is happening in function_that_executes_the_query()?

You might want to take a look at below to see any of the conditions apply:

https://www.postgresql.org/docs/12/when-can-parallel-query-be-used.html

> 
> I am checking the explain output (using execute explain $query) just 
> before executing inside my function and it the plan is identical to what 
> I would expect, planning 8 workers.  However, this query actually only 
> uses 1 worker and takes many times longer than when ran directly on the 
> psql command line with the same server configuration parameters.
> 
> Why would the explain output be different from the executed plan? Is 
> this a limitation of plpgsql functions? Is there any way to debug this 
> further?
> 
> If it is meaningful during parallel execution I notice lots of 
> "postgres: parallel worker" proceses in top and when executing from my 
> function just a single "postgres: $user $db $host($pid) SELECT" processes.
> 
> Best regards,
> 
> Alastair
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





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

* Re: Explain says 8 workers planned, only 1 executed
@ 2020-03-21 17:58  Alastair McKinley <a.mckinley@analyticsengines.com>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Alastair McKinley @ 2020-03-21 17:58 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi Adrian,

Thanks for getting back to me.

Postgres version is:

PostgreSQL 11.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit

I simplified it to the following structure:

create function f() returns setof my_type as
$$
declare
q text;
output text;
begin
    select generate_query1() into q; -- the query q executes in parallel with 8 workers if executed standalone
    for output in execute('explain ' || q) loop
        raise notice '%',output;  -- this plan says 8 workers will be launched
    end loop;
    return query execute q; -- this launches one worker

   select generate_query2() into q;
    for output in execute('explain ' || q) loop
        raise notice '%',output;  -- this plan says 8 workers will be launched
    end loop;
    return query execute q; -- this also launches one worker
end;
language plpgsql;

Should this work in principle or am I missing something subtle about parallel dynamic queries in plpgsql functions?  Does the outer function need to be parallel safe?
Might a stored proc work better?

Best regards,

Alastair


________________________________
From: Adrian Klaver <adrian.klaver@aklaver.com>
Sent: 21 March 2020 17:38
To: Alastair McKinley <a.mckinley@analyticsengines.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Explain says 8 workers planned, only 1 executed

On 3/21/20 10:25 AM, Alastair McKinley wrote:
> Hi all,
>
> I have a long running query that I have tweaked along with config (e.g.
> min_parallel_table_scan_size) to execute nicely and very fast in
> parallel which works as expected executed directly from psql client.
> The query is then embedded in a psql function like "return query select
> * from function_that_executes_the_query()".

Postgres version?

What is happening in function_that_executes_the_query()?

You might want to take a look at below to see any of the conditions apply:

https://www.postgresql.org/docs/12/when-can-parallel-query-be-used.html

>
> I am checking the explain output (using execute explain $query) just
> before executing inside my function and it the plan is identical to what
> I would expect, planning 8 workers.  However, this query actually only
> uses 1 worker and takes many times longer than when ran directly on the
> psql command line with the same server configuration parameters.
>
> Why would the explain output be different from the executed plan? Is
> this a limitation of plpgsql functions? Is there any way to debug this
> further?
>
> If it is meaningful during parallel execution I notice lots of
> "postgres: parallel worker" proceses in top and when executing from my
> function just a single "postgres: $user $db $host($pid) SELECT" processes.
>
> Best regards,
>
> Alastair
>
>


--
Adrian Klaver
adrian.klaver@aklaver.com

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

* Re: Explain says 8 workers planned, only 1 executed
@ 2020-03-21 20:50  Jeremy Smith <jeremy@musicsmith.net>
  parent: Alastair McKinley <a.mckinley@analyticsengines.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Jeremy Smith @ 2020-03-21 20:50 UTC (permalink / raw)
  To: Alastair McKinley <a.mckinley@analyticsengines.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Unfortunately, return query will never use parallel workers.  See:
https://stackoverflow.com/q/58079898/895640 and
https://www.postgresql.org/message-id/16040-eaacad11fecfb198@postgresql.org

On Sat, Mar 21, 2020 at 1:59 PM Alastair McKinley <
a.mckinley@analyticsengines.com> wrote:

> Hi Adrian,
>
> Thanks for getting back to me.
>
> Postgres version is:
>
> PostgreSQL 11.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5
> 20150623 (Red Hat 4.8.5-39), 64-bit
>
> I simplified it to the following structure:
>
> create function f() returns setof my_type as
> $$
> declare
> q text;
> output text;
> begin
>     select generate_query1() into q; -- the query q executes in parallel
> with 8 workers if executed standalone
>     for output in execute('explain ' || q) loop
>         raise notice '%',output;  -- this plan says 8 workers will be
> launched
>     end loop;
>     return query execute q; -- this launches one worker
>
>    select generate_query2() into q;
>     for output in execute('explain ' || q) loop
>         raise notice '%',output;  -- this plan says 8 workers will be
> launched
>     end loop;
>     return query execute q; -- this also launches one worker
> end;
> language plpgsql;
>
> Should this work in principle or am I missing something subtle about
> parallel dynamic queries in plpgsql functions?  Does the outer function
> need to be parallel safe?
> Might a stored proc work better?
>
> Best regards,
>
> Alastair
>
>
> ------------------------------
> *From:* Adrian Klaver <adrian.klaver@aklaver.com>
> *Sent:* 21 March 2020 17:38
> *To:* Alastair McKinley <a.mckinley@analyticsengines.com>;
> pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
> *Subject:* Re: Explain says 8 workers planned, only 1 executed
>
> On 3/21/20 10:25 AM, Alastair McKinley wrote:
> > Hi all,
> >
> > I have a long running query that I have tweaked along with config (e.g.
> > min_parallel_table_scan_size) to execute nicely and very fast in
> > parallel which works as expected executed directly from psql client.
> > The query is then embedded in a psql function like "return query select
> > * from function_that_executes_the_query()".
>
> Postgres version?
>
> What is happening in function_that_executes_the_query()?
>
> You might want to take a look at below to see any of the conditions apply:
>
> https://www.postgresql.org/docs/12/when-can-parallel-query-be-used.html
>
> >
> > I am checking the explain output (using execute explain $query) just
> > before executing inside my function and it the plan is identical to what
> > I would expect, planning 8 workers.  However, this query actually only
> > uses 1 worker and takes many times longer than when ran directly on the
> > psql command line with the same server configuration parameters.
> >
> > Why would the explain output be different from the executed plan? Is
> > this a limitation of plpgsql functions? Is there any way to debug this
> > further?
> >
> > If it is meaningful during parallel execution I notice lots of
> > "postgres: parallel worker" proceses in top and when executing from my
> > function just a single "postgres: $user $db $host($pid) SELECT"
> processes.
> >
> > Best regards,
> >
> > Alastair
> >
> >
>
>
> --
> Adrian Klaver
> adrian.klaver@aklaver.com
>

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

* Re: Explain says 8 workers planned, only 1 executed
@ 2020-03-21 21:16  Alastair McKinley <a.mckinley@analyticsengines.com>
  parent: Jeremy Smith <jeremy@musicsmith.net>
  0 siblings, 1 reply; 6+ messages in thread

From: Alastair McKinley @ 2020-03-21 21:16 UTC (permalink / raw)
  To: Jeremy Smith <jeremy@musicsmith.net>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi Jeremy,

Thanks for solving the mystery.  I think this might be a missing point in section 15.2 in the docs.

I wonder will this ever be improved or should I just write to temporary tables instead of return query?

Best regards,

Alastair
________________________________
From: Jeremy Smith <jeremy@musicsmith.net>
Sent: 21 March 2020 20:50
To: Alastair McKinley <a.mckinley@analyticsengines.com>
Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Explain says 8 workers planned, only 1 executed

Unfortunately, return query will never use parallel workers.  See: https://stackoverflow.com/q/58079898/895640 and https://www.postgresql.org/message-id/16040-eaacad11fecfb198@postgresql.org

On Sat, Mar 21, 2020 at 1:59 PM Alastair McKinley <a.mckinley@analyticsengines.com<mailto:a.mckinley@analyticsengines.com>> wrote:
Hi Adrian,

Thanks for getting back to me.

Postgres version is:

PostgreSQL 11.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit

I simplified it to the following structure:

create function f() returns setof my_type as
$$
declare
q text;
output text;
begin
    select generate_query1() into q; -- the query q executes in parallel with 8 workers if executed standalone
    for output in execute('explain ' || q) loop
        raise notice '%',output;  -- this plan says 8 workers will be launched
    end loop;
    return query execute q; -- this launches one worker

   select generate_query2() into q;
    for output in execute('explain ' || q) loop
        raise notice '%',output;  -- this plan says 8 workers will be launched
    end loop;
    return query execute q; -- this also launches one worker
end;
language plpgsql;

Should this work in principle or am I missing something subtle about parallel dynamic queries in plpgsql functions?  Does the outer function need to be parallel safe?
Might a stored proc work better?

Best regards,

Alastair


________________________________
From: Adrian Klaver <adrian.klaver@aklaver.com<mailto:adrian.klaver@aklaver.com>>
Sent: 21 March 2020 17:38
To: Alastair McKinley <a.mckinley@analyticsengines.com<mailto:a.mckinley@analyticsengines.com>>; pgsql-general@lists.postgresql.org<mailto:pgsql-general@lists.postgresql.org> <pgsql-general@lists.postgresql.org<mailto:pgsql-general@lists.postgresql.org>>
Subject: Re: Explain says 8 workers planned, only 1 executed

On 3/21/20 10:25 AM, Alastair McKinley wrote:
> Hi all,
>
> I have a long running query that I have tweaked along with config (e.g.
> min_parallel_table_scan_size) to execute nicely and very fast in
> parallel which works as expected executed directly from psql client.
> The query is then embedded in a psql function like "return query select
> * from function_that_executes_the_query()".

Postgres version?

What is happening in function_that_executes_the_query()?

You might want to take a look at below to see any of the conditions apply:

https://www.postgresql.org/docs/12/when-can-parallel-query-be-used.html

>
> I am checking the explain output (using execute explain $query) just
> before executing inside my function and it the plan is identical to what
> I would expect, planning 8 workers.  However, this query actually only
> uses 1 worker and takes many times longer than when ran directly on the
> psql command line with the same server configuration parameters.
>
> Why would the explain output be different from the executed plan? Is
> this a limitation of plpgsql functions? Is there any way to debug this
> further?
>
> If it is meaningful during parallel execution I notice lots of
> "postgres: parallel worker" proceses in top and when executing from my
> function just a single "postgres: $user $db $host($pid) SELECT" processes.
>
> Best regards,
>
> Alastair
>
>


--
Adrian Klaver
adrian.klaver@aklaver.com<mailto:adrian.klaver@aklaver.com>

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

* Re: Explain says 8 workers planned, only 1 executed
@ 2020-03-22 03:25  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Alastair McKinley <a.mckinley@analyticsengines.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Tom Lane @ 2020-03-22 03:25 UTC (permalink / raw)
  To: Alastair McKinley <a.mckinley@analyticsengines.com>; +Cc: Jeremy Smith <jeremy@musicsmith.net>; Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Alastair McKinley <a.mckinley@analyticsengines.com> writes:
> Thanks for solving the mystery.  I think this might be a missing point in section 15.2 in the docs.
> I wonder will this ever be improved or should I just write to temporary tables instead of return query?

I just posted a patch to improve that [1], but it's not something we'd be
likely to back-patch into existing releases.

			regards, tom lane

[1] https://www.postgresql.org/message-id/1741.1584847383%40sss.pgh.pa.us





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


end of thread, other threads:[~2020-03-22 03:25 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-03-21 17:25 Explain says 8 workers planned, only 1 executed Alastair McKinley <a.mckinley@analyticsengines.com>
2020-03-21 17:38 ` Adrian Klaver <adrian.klaver@aklaver.com>
2020-03-21 17:58   ` Alastair McKinley <a.mckinley@analyticsengines.com>
2020-03-21 20:50     ` Jeremy Smith <jeremy@musicsmith.net>
2020-03-21 21:16       ` Alastair McKinley <a.mckinley@analyticsengines.com>
2020-03-22 03:25         ` Tom Lane <tgl@sss.pgh.pa.us>

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