Thread: Getting a list of prepared statements

Getting a list of prepared statements

From
"Thue Janus Kristensen"
Date:
It would be very nice to have a way to get a list of prepared statements. Currently there does not seem to be any:
[1]<br/><br />My problem is this: I am using pgsql with PHP, connecting using pg_pconnect(), which will reuse pgsql
connectionsif possible. This means that the pgsql connection returned to my freshly instantiated PHP instance can have
"predefined"prepared statements. It would be very nice for performance if I were able to reuse those, however there is
noway to know what prepared statements are defined, since there is no pgsql interface to list them. In fact, I am
currentlyprefixing all my prepared query names with a per-session random string to avoid name clashes from previous
sessions.<br/><br />The only way to test if a prepared statement with a given name exists seems to be to try to define
anew one with the same name. If this gives an error then there exist a statement with that name, but I don't think you
cantell whether it is the same actual query as the new one you wanted to define.<br /><br />M. Bastin suggests in [2]
tojust keep a list in the client app. In my case this would mean modifying PHP to keep the list together with its pool
ofconnections. I guess one could modify PHP, but it seems better and simpler to just modify pgsql to return the list
(thepgsql connection presumably already keeps the list); that would fix the problem also for other programming
languagesusing persistent pgsql connections.<br /><br />I don't know what the interface should be; a possibly ignorant
suggestionis implementing a build-in stored procedure such that "SELECT * FROM _prepared_statement_list() WHERE name
LIKE'a%'" could return a table of (name TEXT, query TEXT) rows?<br />Another possibility would be using something like
"\d"in the psql client (however this is implemented in pgsql), but the IMO the stored procedure approach is better,
as<br/>-You can use SELECT and WHERE clause to filter the result.<br /> -The query will be directly available to for
examplePHP, without having to add a new postgresql function to access it. If fx PHP really wants such a function, they
canimplement it by just executing such a query.<br /><br /> I am not a pgsql hacker, so I am probably not going to
implementthis myself. But it would be nice if the pgsql project added it to the pgsql TODO list. :)<br /><br />[1] <a
href="http://archives.postgresql.org/pgsql-novice/2004-07/msg00089.php">http://archives.postgresql.org/pgsql-novice/2004-07/msg00089.php</a><br
/>[2]<a
href="http://archives.postgresql.org/pgsql-novice/2004-07/msg00090.php">http://archives.postgresql.org/pgsql-novice/2004-07/msg00090.php</a><br
/><br/>Regards, Thue  

Re: Getting a list of prepared statements

From
Tom Lane
Date:
"Thue Janus Kristensen" <thuejk@gmail.com> writes:
> It would be very nice to have a way to get a list of prepared statements.
> Currently there does not seem to be any: [1]

select * from pg_prepared_statements ?

(exists since 8.2)
        regards, tom lane


Re: Getting a list of prepared statements

From
"Thue Janus Kristensen"
Date:
Ah - thanks. :)<br /><br />I must be trusting too much in Google, because all Google returned was a pages telling me
thatit could not be done. But now I see that it is described clearly at the obvious location, <a
href="http://www.postgresql.org/docs/8.3/interactive/sql-prepare.html">http://www.postgresql.org/docs/8.3/interactive/sql-prepare.html</a>
:-/<br/><br />However, this won't be a problem in the future, for already the top hit on Google for "postgresql get
listof prepared statements" is this email thread, with me asking stupid questions the answer to which is clearly
visiblein the manual *hmm*.<br /><br />Regards, Thue<br /><br /><div class="gmail_quote">On Sun, Dec 28, 2008 at 11:15
PM,Tom Lane <span dir="ltr"><<a href="mailto:tgl@sss.pgh.pa.us">tgl@sss.pgh.pa.us</a>></span> wrote:<br
/><blockquoteclass="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex;
padding-left:1ex;"><div class="Ih2E3d">"Thue Janus Kristensen" <<a
href="mailto:thuejk@gmail.com">thuejk@gmail.com</a>>writes:<br /> > It would be very nice to have a way to get a
listof prepared statements.<br /> > Currently there does not seem to be any: [1]<br /><br /></div>select * from
pg_prepared_statements?<br /><br /> (exists since 8.2)<br /><br />                        regards, tom lane<br
/></blockquote></div><br/>