Thread: RQ: Prepared statements used by multiple connections

RQ: Prepared statements used by multiple connections

From
Bojidar Mihajlov
Date:
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

Re: [HACKERS] RQ: Prepared statements used by multiple connections

From
Neil Conway
Date:
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


Re: [HACKERS] RQ: Prepared statements used by multiple connections

From
Christopher Kings-Lynne
Date:
> 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

Re: [HACKERS] RQ: Prepared statements used by multiple connections

From
Christopher Kings-Lynne
Date:
> ... 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

Re: [HACKERS] RQ: Prepared statements used by multiple connections

From
Michael Fuhr
Date:
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/

Re: RQ: Prepared statements used by multiple connections

From
Bojidar Mihajlov
Date:
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