From: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: Is it possible to store the output of EXPLAIN into a table
Date: Mon, 22 Apr 2019 18:01:13 +0200
Message-ID: <9378a90b-e03b-1477-e6b8-0efacd2613ad@gmx.net> (raw)
In-Reply-To: <CAANrPSdL9YpU0hqm=YfPUQKHVXrSCVTRO-5QekEDdEmNv5UbEA@mail.gmail.com>
References: <CAANrPSdL9YpU0hqm=YfPUQKHVXrSCVTRO-5QekEDdEmNv5UbEA@mail.gmail.com>
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');
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: spam_eater@gmx.net
Subject: Re: Is it possible to store the output of EXPLAIN into a table
In-Reply-To: <9378a90b-e03b-1477-e6b8-0efacd2613ad@gmx.net>
References: <CAANrPSdL9YpU0hqm=YfPUQKHVXrSCVTRO-5QekEDdEmNv5UbEA@mail.gmail.com> <9378a90b-e03b-1477-e6b8-0efacd2613ad@gmx.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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