Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 533C92E00E8 for ; Wed, 13 Feb 2008 15:19:00 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 30169-02 for ; Wed, 13 Feb 2008 15:18:53 -0400 (AST) Received: from mail63.csoft.net (leary.csoft.net [205.205.214.5]) by postgresql.org (Postfix) with SMTP id 463182E0150 for ; Wed, 13 Feb 2008 15:18:56 -0400 (AST) Received: (qmail 15787 invoked by uid 1112); 13 Feb 2008 19:18:55 -0000 Date: Wed, 13 Feb 2008 14:18:54 -0500 (EST) From: Kris Jurka X-X-Sender: books@leary.csoft.net To: Josh Berkus cc: PostgreSQL - JDBC Subject: Re: JDBC, prepared queries, and partitioning In-Reply-To: Message-ID: References: <200802131005.30553.josh@agliodbs.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200802/52 X-Sequence-Number: 18259 On Wed, 13 Feb 2008, Kris Jurka wrote: > Using protocol version 2 will not prepare queries so constraint exclusion can > be used. Just append protocolVersion=2 to your URL. > Also, to add what a real fix would look like: Right now the JDBC driver always uses server side prepared statements for Java PreparedStatements. There are two reasons to use a PreparedStatement: to send parameters out of line and to avoid replanning. Neither the JDBC API nor the fe-be protocol really allow the user to specify why they are using a prepared statement, so the driver has to guess and provide a weak hint to the server. We have a prepare threshold (settable per connection or per statement) that indicates the number of times the statement has been executed to consider it as a prepared statement that's used to avoid replanning. Once we hit that the threshold we tell the server to use a named statement and it comes up with a generic plan that should theoretically be acceptable for a wide variety of parameter values. Prior to this switchover we use an unnamed statement that indicates to the server that it should use the first set of parameters it gets to construct the best plan for those parameters. So on an unnamed statement the server considers the parameter values in the plan cost estimates, but it can't do constraint exclusion because there is no guarantee that the plan generated won't be used again with other parameters (even though the JDBC driver won't). So the disconnect currently is that the JDBC driver has no way of telling the server, "I promise I'll never use this plan for anything else" to allow it to do constraint exclusion. So this requires an extension to the protocol to fix properly. Kris Jurka