pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Paul Lindner <lindner@inuus.com>
To: Oliver Jowett <oliver@opencloud.com>
Cc: Paul Lindner <lindner@inuus.com>
Cc: Gregory Stark <stark@enterprisedb.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Prepared Statements vs. pgbouncer
Date: Sun, 30 Sep 2007 00:07:25 -0700
Message-ID: <20070930070725.GL3140@inuus.com> (raw)
In-Reply-To: <46FED6A2.8000209@opencloud.com>
References: <20070929002513.GG3140@inuus.com>
	<87sl4xvmn2.fsf@oxford.xeocode.com>
	<20070929144635.GK3140@inuus.com>
	<46FED6A2.8000209@opencloud.com>

Your proposal below is interesting, but is a much larger scale problem
than I want to deal with.  It also requires that the middleware layer
to do deep packet inspection, which is suboptimal from a latency and
performance standpoint.

Anyway, I've been thinking about this for a little while and think the
following changes would not cripple the driver, but would provide
correct behavior in the face of a server that could potentially change
backends:

1) Use hashing to choose a prepared statement name

   If we take the hash of the prepared statement text and prefix with
   S_ we can be assured of using the same unique prepared statement
   name across all application servers.

   (And yes, I know that hashes are not perfect and collisions can
    occur.  Highly unlikely if we choose a good hash)

2) If we try to prepare a statement with an hashed name and it already
   exists then we ignore the error and continue.

3) If we receive an error while executing a prepared statement with an
   hashed name the driver will try to re-prepare the statement and
   re-execute the query.  If an error occurs after this retry step then
   error are surfaced to the caller.


If no-one has a more workable solution I'll probably go ahead and
implement the modifications I've listed above.  

Of course I'd be happy to publish the patch and maintain the fork for
anyone else that might want to use middleware software with their Java
clients.


-- Paul

On Sun, Sep 30, 2007 at 11:50:10AM +1300, Oliver Jowett wrote:
> Paul Lindner wrote:
> 
> >How do we fix this?  Short term?  Long term?
> 
> Build a connection pooling thing that does what you want it to do and 
> implements the protocol *completely*.
> 
> For example the whole named statement problem goes away if the bit of 
> software doing the pooling keeps track of
> 
> (a) which statements were prepared under which names on the "client" 
> side connections (including query text & OIDs)
> (b) which statements were prepared under which names on the "server" side
> (c) relevant transaction and session state on both sides
> 
> Note that the names don't necessarily match up, the pool can merrily 
> assign its own names on the server side.
> 
> Then it doesn't matter at all what names the clients use, it's 
> irrelevant, they're still scoped within the connection they originated 
> from as the protocol expects. Internally the pool then maps them to a 
> new statement name on whichever real server connection it decides to 
> push queries to. If the statement hasn't been prepared on that 
> connection yet, that's fine, you just re-prepare it under a new name 
> from the data you have stored.
> 
> And you get a "shared" prepared statement cache per server connection 
> for free. You can also implement whatever logic you want for managing 
> this cache, garbage collection and timing out idle statements, etc, as 
> you see fit.
> 
> This is basically what I meant by fixing pgbouncer. No, it's not trivial 
> to do, but there's no technical reason why it can't be done, you'll just 
> need to throw development time at it.
> 
> You can also run this with a heterogenous client environment and not 
> have to worry about clients following some particular subset of the 
> protocol or cooperating over statement names and the like.
> 
> -O

-- 
Paul Lindner        ||||| | | | |  |  |  |   |   |
lindner@inuus.com

view thread (48+ messages)  latest in thread

Message-ID: <20070930070725.GL3140@inuus.com>
Permalink:  ../20070930070725.GL3140@inuus.com/
Also on:    postgresql.org/message-id/20070930070725.GL3140@inuus.com

 · 

reply

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-jdbc@postgresql.org
  Cc: lindner@inuus.com, oliver@opencloud.com, stark@enterprisedb.com
  Subject: Re: Prepared Statements vs. pgbouncer
  In-Reply-To: <20070930070725.GL3140@inuus.com>
  References: <20070929002513.GG3140@inuus.com> <87sl4xvmn2.fsf@oxford.xeocode.com> <20070929144635.GK3140@inuus.com> <46FED6A2.8000209@opencloud.com> <20070930070725.GL3140@inuus.com>

* 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