RQ: Prepared statements used by multiple connections

Started by Bojidar Mihajlovalmost 21 years ago7 messages
#1Bojidar Mihajlov
bmihajlov@yahoo.com

Hi all !

I need a mechanism to keep my queries in optimized
state so that multiple processes can use them.

__________________________________
Do you Yahoo!?
Yahoo! Mail - Easier than ever with enhanced search. Learn more.
http://info.mail.yahoo.com/mail_250

#2Christopher Kings-Lynne
chriskl@familyhealth.com.au
In reply to: Bojidar Mihajlov (#1)
Re: [HACKERS] RQ: Prepared statements used by multiple connections

I need a mechanism to keep my queries in optimized
state so that multiple processes can use them.

You should use stored procedures then.

For instance, say you want to keep 'SELECT * FROM table WHERE id=x'
prepared. You would go:

CREATE OR REPLACE FUNCTION get_table_id(integer) RETURNS SETOF table AS
'SELECT * FROM table WHERE id=$1' LANGUAGE SQL;

PostgreSQL will store a prepared version of that statement after its
first use.

You use it like this:

SELECT * FROM get_table_id(3);

Chris

#3Neil Conway
neilc@samurai.com
In reply to: Christopher Kings-Lynne (#2)
Re: [HACKERS] RQ: Prepared statements used by multiple connections

Christopher Kings-Lynne wrote:

I need a mechanism to keep my queries in optimized state so that
multiple processes can use them.

You should use stored procedures then.

For instance, say you want to keep 'SELECT * FROM table WHERE id=x'
prepared. You would go:

CREATE OR REPLACE FUNCTION get_table_id(integer) RETURNS SETOF table AS
'SELECT * FROM table WHERE id=$1' LANGUAGE SQL;

PostgreSQL will store a prepared version of that statement after its
first use.

... a prepared version that is local to the backend that invokes the
function, yes (i.e. it will be planned once per backend). So ISTM this
is equivalent functionality to what you can get using PREPARE or the
extended query protocol.

There currently isn't support for prepared queries that span multiple
connections. There has been prior discussion of the topic, so try
searching the pgsql-hackers archive. There are some implementation
issues that require thought (e.g. managing dependencies, storing a
potentially unbounded set of prepared queries in a finite amount of shmem).

-Neil

#4Christopher Kings-Lynne
chriskl@familyhealth.com.au
In reply to: Neil Conway (#3)
Re: [HACKERS] RQ: Prepared statements used by multiple connections

... a prepared version that is local to the backend that invokes the
function, yes (i.e. it will be planned once per backend). So ISTM this
is equivalent functionality to what you can get using PREPARE or the
extended query protocol.

Are you sure it's only per-backend? I thought I tested it and it seemed
to prepare it everywhere... oh well.

Either way, it avoids the problem with prepared queries in that you
cannot know in advance if your query has already been prepared or not.

Chris

#5Michael Fuhr
mike@fuhr.org
In reply to: Christopher Kings-Lynne (#4)
Re: [HACKERS] RQ: Prepared statements used by multiple connections

On Tue, Jan 25, 2005 at 10:36:34AM +0000, Christopher Kings-Lynne wrote:

... a prepared version that is local to the backend that invokes the
function, yes (i.e. it will be planned once per backend). So ISTM this
is equivalent functionality to what you can get using PREPARE or the
extended query protocol.

Are you sure it's only per-backend? I thought I tested it and it seemed
to prepare it everywhere... oh well.

How did you test it? You can use settings like debug_print_plan
and client_min_messages to see what's happening in each backend.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

#6Bojidar Mihajlov
bmihajlov@yahoo.com
In reply to: Michael Fuhr (#5)
Re: RQ: Prepared statements used by multiple connections

It looks it couldn't happen this a way.
Did somebody find out an alternative.
Is reasonable some idea based on a connection pool ?
-Bozhidar

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

#7Merlin Moncure
merlin.moncure@rcsonline.com
In reply to: Bojidar Mihajlov (#6)
Re: [HACKERS] RQ: Prepared statements used by multiple connections

... a prepared version that is local to the backend that invokes the
function, yes (i.e. it will be planned once per backend). So ISTM

this

is equivalent functionality to what you can get using PREPARE or the
extended query protocol.

Are you sure it's only per-backend? I thought I tested it and it

seemed

to prepare it everywhere... oh well.

Plpgsql functions at the least are compiled by each backend. I take
advantage of this...I use schemas and I don't have to keep a copy of the
function for each dataset. I think vanilla sql functions might be
different.

Either way, it avoids the problem with prepared queries in that you
cannot know in advance if your query has already been prepared or not.

Yep. I like things the way they are, but I can feel the pain of
applications that don't (or can't) keep connections open.

Merlin