pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedEXPLAIN with anonymous DO block?
5+ messages / 5 participants
[nested] [flat]
* EXPLAIN with anonymous DO block?
@ 2021-07-01 16:16 Ron <ronljohnsonjr@gmail.com>
2021-07-01 16:21 ` Re: EXPLAIN with anonymous DO block? Michael Lewis <mlewis@entrata.com>
2021-07-01 16:27 ` Re: EXPLAIN with anonymous DO block? Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 2 replies; 5+ messages in thread
From: Ron @ 2021-07-01 16:16 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Postgresql 12.5
There's a query inside a DO block which -- because it's parameterized -- I'd
rather analyze while it's in the FOR loop of a DO block, instead of pulling
it out and hard-coding the parameters.
Is this possible? If so, where do I put the EXPLAIN statement?
--
Angular momentum makes the world go 'round.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: EXPLAIN with anonymous DO block?
2021-07-01 16:16 EXPLAIN with anonymous DO block? Ron <ronljohnsonjr@gmail.com>
@ 2021-07-01 16:21 ` Michael Lewis <mlewis@entrata.com>
2021-07-01 16:28 ` Re: EXPLAIN with anonymous DO block? David G. Johnston <david.g.johnston@gmail.com>
1 sibling, 1 reply; 5+ messages in thread
From: Michael Lewis @ 2021-07-01 16:21 UTC (permalink / raw)
To: Ron <ronljohnsonjr@gmail.com>; +Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>
It sounds like you are wanting to run 'explain analyze [query]' inside a
loop inside a DO block. That isn't possible as far as I know, but
auto_explain and log_nested_statements should be able to let you profile
the whole thing and perhaps you can pick out the part you want from the
logs.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: EXPLAIN with anonymous DO block?
2021-07-01 16:16 EXPLAIN with anonymous DO block? Ron <ronljohnsonjr@gmail.com>
2021-07-01 16:21 ` Re: EXPLAIN with anonymous DO block? Michael Lewis <mlewis@entrata.com>
@ 2021-07-01 16:28 ` David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 5+ messages in thread
From: David G. Johnston @ 2021-07-01 16:28 UTC (permalink / raw)
To: Michael Lewis <mlewis@entrata.com>; +Cc: Ron <ronljohnsonjr@gmail.com>; PostgreSQL General <pgsql-general@lists.postgresql.org>
On Thu, Jul 1, 2021 at 9:22 AM Michael Lewis <mlewis@entrata.com> wrote:
> It sounds like you are wanting to run 'explain analyze [query]' inside a
> loop inside a DO block. That isn't possible as far as I know, but
> auto_explain and log_nested_statements should be able to let you profile
> the whole thing and perhaps you can pick out the part you want from the
> logs.
>
I believe it can be done technically, though basically the function will
need to be re-written for the purpose. It isn't as simple as adding an
explain somewhere since the output of explain is a result set. But as you
are already using pl/pgsql then your parameters can just be done up as
variables instead and that query should be able to be explained.
David J.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: EXPLAIN with anonymous DO block?
2021-07-01 16:16 EXPLAIN with anonymous DO block? Ron <ronljohnsonjr@gmail.com>
@ 2021-07-01 16:27 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-07-01 16:48 ` Re: EXPLAIN with anonymous DO block? Pavel Luzanov <p.luzanov@postgrespro.ru>
1 sibling, 1 reply; 5+ messages in thread
From: Tom Lane @ 2021-07-01 16:27 UTC (permalink / raw)
To: Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-general@lists.postgresql.org
Ron <ronljohnsonjr@gmail.com> writes:
> There's a query inside a DO block which -- because it's parameterized -- I'd
> rather analyze while it's in the FOR loop of a DO block, instead of pulling
> it out and hard-coding the parameters.
> Is this possible?
No.
The thing to do to duplicate the behavior of a plpgsql query is
to set it up as a PREPAREd statement (with parameters for any
plpgsql variables it references) and use EXPLAIN EXECUTE.
This also works if you're curious about the behavior of a
query issued via PQexecParams or the like.
It's recommendable to repeat the EXPLAIN half a dozen times
to see if the plancache switches from a custom to a generic
plan. (In recent PG releases, changing plan_cache_mode
is another way to check what happens.)
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: EXPLAIN with anonymous DO block?
2021-07-01 16:16 EXPLAIN with anonymous DO block? Ron <ronljohnsonjr@gmail.com>
2021-07-01 16:27 ` Re: EXPLAIN with anonymous DO block? Tom Lane <tgl@sss.pgh.pa.us>
@ 2021-07-01 16:48 ` Pavel Luzanov <p.luzanov@postgrespro.ru>
0 siblings, 0 replies; 5+ messages in thread
From: Pavel Luzanov @ 2021-07-01 16:48 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-general@lists.postgresql.org
Good day!
>> There's a query inside a DO block which -- because it's parameterized -- I'd
>> rather analyze while it's in the FOR loop of a DO block, instead of pulling
>> it out and hard-coding the parameters.
>> Is this possible?
> No.
Why not to use auto_explain module?
postgres=# LOAD 'auto_explain';
LOAD
postgres=# SET auto_explain.log_min_duration = 0;
SET
postgres=# SET auto_explain.log_nested_statements = on;
SET
postgres=# SET auto_explain.log_analyze = on;
SET
postgres=# SET auto_explain.log_level = 'NOTICE';
SET
postgres=# DO $$BEGIN FOR i IN 112 .. 113 LOOP PERFORM * FROM pg_class
WHERE oid = i::oid; END LOOP; END;$$;
NOTICE: duration: 0.013 ms plan:
Query Text: SELECT * FROM pg_class WHERE oid = i::oid
Index Scan using pg_class_oid_index on pg_class (cost=0.27..8.29 rows=1
width=265) (actual time=0.009..0.011 rows=1 loops=1)
Index Cond: (oid = '112'::oid)
NOTICE: duration: 0.016 ms plan:
Query Text: SELECT * FROM pg_class WHERE oid = i::oid
Index Scan using pg_class_oid_index on pg_class (cost=0.27..8.29 rows=1
width=265) (actual time=0.008..0.009 rows=1 loops=1)
Index Cond: (oid = '113'::oid)
DO
Pavel Luzanov
Postgres Professional: https://postgrespro.com
The Russian Postgres Company
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2021-07-01 16:48 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-07-01 16:16 EXPLAIN with anonymous DO block? Ron <ronljohnsonjr@gmail.com>
2021-07-01 16:21 ` Michael Lewis <mlewis@entrata.com>
2021-07-01 16:28 ` David G. Johnston <david.g.johnston@gmail.com>
2021-07-01 16:27 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-07-01 16:48 ` Pavel Luzanov <p.luzanov@postgrespro.ru>
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