pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Is it possible to store the output of EXPLAIN into a table
6+ messages / 4 participants
[nested] [flat]

* Is it possible to store the output of EXPLAIN into a table
@ 2019-04-22 15:27 Souvik Bhattacherjee <kivuosb@gmail.com>
  2019-04-22 15:59 ` Re: Is it possible to store the output of EXPLAIN into a table Bruce Momjian <bruce@momjian.us>
  2019-04-22 16:01 ` Re: Is it possible to store the output of EXPLAIN into a table Thomas Kellerer <spam_eater@gmx.net>
  2019-04-22 16:01 ` Re: Is it possible to store the output of EXPLAIN into a table Thomas Kellerer <spam_eater@gmx.net>
  2019-04-22 16:02 ` Re: Is it possible to store the output of EXPLAIN into a table Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 4 replies; 6+ messages in thread

From: Souvik Bhattacherjee @ 2019-04-22 15:27 UTC (permalink / raw)
  To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi,

I was just wondering if it is possible to store the output of EXPLAIN into
a table.

create temp table mytab as (select * from (explain select * from table1
where attr = 5) t);


Unfortunately, the query above does not work.


Thanks,

-Souvik

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

* Re: Is it possible to store the output of EXPLAIN into a table
  2019-04-22 15:27 Is it possible to store the output of EXPLAIN into a table Souvik Bhattacherjee <kivuosb@gmail.com>
@ 2019-04-22 15:59 ` Bruce Momjian <bruce@momjian.us>
  3 siblings, 0 replies; 6+ messages in thread

From: Bruce Momjian @ 2019-04-22 15:59 UTC (permalink / raw)
  To: Souvik Bhattacherjee <kivuosb@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Mon, Apr 22, 2019 at 11:27:18AM -0400, Souvik Bhattacherjee wrote:
> Hi,
> 
> I was just wondering if it is possible to store the output of EXPLAIN into a
> table.
> 
> 
> create temp table mytab as (select * from (explain select * from table1 where
> attr = 5) t);
> 
> 
> Unfortunately, the query above does not work.

I had to do this one and put the EXPLAIN in a function and then called
the function and captured the output, see this and following slides:

	http://momjian.us/main/writings/pgsql/optimizer.pdf#page=11

-- 
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

+ As you are, so once was I.  As I am, so you will be. +
+                      Ancient Roman grave inscription +





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

* Re: Is it possible to store the output of EXPLAIN into a table
  2019-04-22 15:27 Is it possible to store the output of EXPLAIN into a table Souvik Bhattacherjee <kivuosb@gmail.com>
@ 2019-04-22 16:01 ` Thomas Kellerer <spam_eater@gmx.net>
  3 siblings, 0 replies; 6+ messages in thread

From: Thomas Kellerer @ 2019-04-22 16:01 UTC (permalink / raw)
  To: pgsql-general

Souvik Bhattacherjee schrieb am 22.04.2019 um 17:27:
> Hi,
> 
> I was just wondering if it is possible to store the output of EXPLAIN into a table.
> 
> create temp table mytab as (select * from (explain select * from table1 where attr = 5) t);
> 
> 
> Unfortunately, the query above does not work.

You can't do that directly. You need to write a function that wraps the explain, and returns the plan as a result set.

Something like:

create or replace function show_plan(to_explain text)
   returns table (line_nr integer, line text)
as
$$
declare
   l_plan_line record;
   l_line integer;
begin
    l_line := 1;
    for l_plan_line in execute 'explain (analyze, verbose, format xml)'||to_explain loop
       return query select l_line, l_plan_line."QUERY PLAN"::text;
       l_line := l_line + 1;
    end loop;
end;
$$
language plpgsql;

The you can use:

create temp table mytab as
select *
from show_plan('select * from table1 where attr = 5');








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

* Re: Is it possible to store the output of EXPLAIN into a table
  2019-04-22 15:27 Is it possible to store the output of EXPLAIN into a table Souvik Bhattacherjee <kivuosb@gmail.com>
@ 2019-04-22 16:01 ` Thomas Kellerer <spam_eater@gmx.net>
  3 siblings, 0 replies; 6+ messages in thread

From: Thomas Kellerer @ 2019-04-22 16:01 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Souvik Bhattacherjee schrieb am 22.04.2019 um 17:27:
> Hi,
>
> I was just wondering if it is possible to store the output of EXPLAIN into a table.
>
> create temp table mytab as (select * from (explain select * from table1 where attr = 5) t);
>
>
> Unfortunately, the query above does not work.

You can't do that directly. You need to write a function that wraps the explain, and returns the plan as a result set.

Something like:

create or replace function show_plan(to_explain text)
   returns table (line_nr integer, line text)
as
$$
declare
   l_plan_line record;
   l_line integer;
begin
    l_line := 1;
    for l_plan_line in execute 'explain (analyze, verbose, format xml)'||to_explain loop
       return query select l_line, l_plan_line."QUERY PLAN"::text;
       l_line := l_line + 1;
    end loop;
end;
$$
language plpgsql;

The you can use:

create temp table mytab as
select *
from show_plan('select * from table1 where attr = 5');







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

* Re: Is it possible to store the output of EXPLAIN into a table
  2019-04-22 15:27 Is it possible to store the output of EXPLAIN into a table Souvik Bhattacherjee <kivuosb@gmail.com>
@ 2019-04-22 16:02 ` Tom Lane <tgl@sss.pgh.pa.us>
  2019-04-23 23:11   ` Re: Is it possible to store the output of EXPLAIN into a table Souvik Bhattacherjee <kivuosb@gmail.com>
  3 siblings, 1 reply; 6+ messages in thread

From: Tom Lane @ 2019-04-22 16:02 UTC (permalink / raw)
  To: Souvik Bhattacherjee <kivuosb@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Souvik Bhattacherjee <kivuosb@gmail.com> writes:
> I was just wondering if it is possible to store the output of EXPLAIN into
> a table.

EXPLAIN won't do that directly, but you could make a plpgsql function
along the lines of

      for t in execute explain ...
         return next t;

(too lazy to check the exact details here, but I believe you can find
related examples in our regression tests) and then store the function's
result into a table.

			regards, tom lane





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

* Re: Is it possible to store the output of EXPLAIN into a table
  2019-04-22 15:27 Is it possible to store the output of EXPLAIN into a table Souvik Bhattacherjee <kivuosb@gmail.com>
  2019-04-22 16:02 ` Re: Is it possible to store the output of EXPLAIN into a table Tom Lane <tgl@sss.pgh.pa.us>
@ 2019-04-23 23:11   ` Souvik Bhattacherjee <kivuosb@gmail.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Souvik Bhattacherjee @ 2019-04-23 23:11 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; Bruce Momjian <bruce@momjian.us>; Thomas Kellerer <spam_eater@gmx.net>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Thanks, works fine!

-Souvik

On Mon, Apr 22, 2019 at 12:02 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

> Souvik Bhattacherjee <kivuosb@gmail.com> writes:
> > I was just wondering if it is possible to store the output of EXPLAIN
> into
> > a table.
>
> EXPLAIN won't do that directly, but you could make a plpgsql function
> along the lines of
>
>       for t in execute explain ...
>          return next t;
>
> (too lazy to check the exact details here, but I believe you can find
> related examples in our regression tests) and then store the function's
> result into a table.
>
>                         regards, tom lane
>

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


end of thread, other threads:[~2019-04-23 23:11 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-04-22 15:27 Is it possible to store the output of EXPLAIN into a table Souvik Bhattacherjee <kivuosb@gmail.com>
2019-04-22 15:59 ` Bruce Momjian <bruce@momjian.us>
2019-04-22 16:01 ` Thomas Kellerer <spam_eater@gmx.net>
2019-04-22 16:01 ` Thomas Kellerer <spam_eater@gmx.net>
2019-04-22 16:02 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-04-23 23:11   ` Souvik Bhattacherjee <kivuosb@gmail.com>

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