Thread: Explain

Explain

From
Jeff Davis
Date:
How can I conveniently handle output from an EXPLAIN query inside an
application? It appears to just immediately send the output to the stdout of
whatever program uses it. Is there a way to get the output inside a program,
with libpq or otherwise? I would actually like to be able to get the output in
a PHP script, but getting it in any application would be nice.

Thanks,
    Jeff Davis
 --
Jeff Davis
jdavis@dynworks.com
http://dynworks.com

Re: Explain

From
Heiko Irrgang
Date:
>How can I conveniently handle output from an EXPLAIN query inside an
>application? It appears to just immediately send the output to the stdout of
>whatever program uses it. Is there a way to get the output inside a program,
>with libpq or otherwise? I would actually like to be able to get the output in
>a PHP script, but getting it in any application would be nice.

AFAIK it dont works directly, but you can do the following:

<?php
$f=popen("psql -U username -c 'explain select * from table' -d dbname
2>/dev/stdout","r");

while($nextline=fgets($f,4096)) {
         echo($nextline."<br>");
}
pclose($f);
?>

this would dump the explain-result as you get it in psql as html-text.

with popen("command","r") you open a stream to the command and
because psql seems to send
the result of explain to stderr you have to redirect it to stdout
(2>/dev/stdout)

then you can handle the filedescriptor delivered by popen() as a normal file
with fgets(), but you have to close it with pclose() not fclose().

this works exactly the same within a c program but i dont know much
about programming postgres in c. it may be possible to have the
same result directly with libpq, but for php this seems to be
the only way to do this.

--
SC-Networks                             www: www.SC-Networks.de
Web Design, Netzwerke,
3D Animation und Multimedia
Heiko Irrgang                           Tel.: 08856/9392-00
Im Thal 2                               Fax:  08856/9392-01

82377 Penzberg                          Mail: Irrgang@SC-Networks.de

Re: Explain

From
Tom Lane
Date:
Jeff Davis <jdavis@wasabimg.com> writes:
> How can I conveniently handle output from an EXPLAIN query inside an
> application? It appears to just immediately send the output to the stdout of
> whatever program uses it. Is there a way to get the output inside a program,
> with libpq or otherwise?

The EXPLAIN result is sent as a NOTICE message, so you can trap it by
setting libpq's notice processor function --- see PQsetNoticeProcessor.

            regards, tom lane