Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id E2FC79F9775 for ; Sun, 30 Sep 2007 04:08:05 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 66488-10 for ; Sun, 30 Sep 2007 04:08:00 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mirth.inuus.com (mirth.inuus.com [67.100.95.85]) by postgresql.org (Postfix) with ESMTP id 158FD9F9768 for ; Sun, 30 Sep 2007 04:07:59 -0300 (ADT) Received: from mirth.inuus.com (localhost.localdomain [127.0.0.1]) by mirth.inuus.com (8.14.1/8.14.1) with ESMTP id l8U77b8f030360 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 30 Sep 2007 00:07:37 -0700 Received: (from lindner@localhost) by mirth.inuus.com (8.14.1/8.14.1/Submit) id l8U77PQV030359; Sun, 30 Sep 2007 00:07:25 -0700 X-Authentication-Warning: mirth.inuus.com: lindner set sender to lindner@inuus.com using -f Date: Sun, 30 Sep 2007 00:07:25 -0700 From: Paul Lindner To: Oliver Jowett Cc: Paul Lindner , Gregory Stark , pgsql-jdbc@postgresql.org Subject: Re: Prepared Statements vs. pgbouncer Message-ID: <20070930070725.GL3140@inuus.com> References: <20070929002513.GG3140@inuus.com> <87sl4xvmn2.fsf@oxford.xeocode.com> <20070929144635.GK3140@inuus.com> <46FED6A2.8000209@opencloud.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="j3olVFx0FsM75XyV" Content-Disposition: inline In-Reply-To: <46FED6A2.8000209@opencloud.com> User-Agent: Mutt/1.5.14 (2007-02-12) X-Virus-Scanned: ClamAV 0.91.2/4433/Sat Sep 29 16:13:47 2007 on mirth.inuus.com X-Virus-Status: Clean X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200709/146 X-Sequence-Number: 17568 --j3olVFx0FsM75XyV Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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. =20 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: >=20 > >How do we fix this? Short term? Long term? >=20 > Build a connection pooling thing that does what you want it to do and=20 > implements the protocol *completely*. >=20 > For example the whole named statement problem goes away if the bit of=20 > software doing the pooling keeps track of >=20 > (a) which statements were prepared under which names on the "client"=20 > 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 >=20 > Note that the names don't necessarily match up, the pool can merrily=20 > assign its own names on the server side. >=20 > Then it doesn't matter at all what names the clients use, it's=20 > irrelevant, they're still scoped within the connection they originated=20 > from as the protocol expects. Internally the pool then maps them to a=20 > new statement name on whichever real server connection it decides to=20 > push queries to. If the statement hasn't been prepared on that=20 > connection yet, that's fine, you just re-prepare it under a new name=20 > from the data you have stored. >=20 > And you get a "shared" prepared statement cache per server connection=20 > for free. You can also implement whatever logic you want for managing=20 > this cache, garbage collection and timing out idle statements, etc, as=20 > you see fit. >=20 > This is basically what I meant by fixing pgbouncer. No, it's not trivial= =20 > to do, but there's no technical reason why it can't be done, you'll just= =20 > need to throw development time at it. >=20 > You can also run this with a heterogenous client environment and not=20 > have to worry about clients following some particular subset of the=20 > protocol or cooperating over statement names and the like. >=20 > -O --=20 Paul Lindner ||||| | | | | | | | | | lindner@inuus.com --j3olVFx0FsM75XyV Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQFG/0ss+FkoUnP1gFQRAqaWAKCV4IsoZmp/6p07pcuHxOQSc8RbBwCfTUmk O8ZCOie2qrT+sqpQbIoANHQ= =kZX5 -----END PGP SIGNATURE----- --j3olVFx0FsM75XyV--