Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 4B8521337B47 for ; Wed, 6 Oct 2010 08:57:08 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 43435-08 for ; Wed, 6 Oct 2010 11:57:02 +0000 (UTC) X-Greylist: delayed 00:15:18.926138 by SQLgrey-1.7.6 Received: from ibms.sinica.edu.tw (mail1.ibms.sinica.edu.tw [140.109.40.109]) by mail.postgresql.org (Postfix) with ESMTP id 81F1B1335B36 for ; Wed, 6 Oct 2010 08:57:00 -0300 (ADT) Received: from [192.168.6.213] ([140.109.54.2]) (authenticated bits=0) by ibms.sinica.edu.tw (8.14.0/8.14.0) with ESMTP id o96Buxx0009603 for ; Wed, 6 Oct 2010 19:56:59 +0800 (CST) Subject: The logic behind the prepared statement in PostgreSQL From: louis To: pgsql-jdbc@postgresql.org In-Reply-To: <1286365302.13691.35.camel@london> References: <1286365302.13691.35.camel@london> Content-Type: text/plain; charset="UTF-8" Date: Wed, 06 Oct 2010 19:57:02 +0800 Message-ID: <1286366222.13691.36.camel@london> Mime-Version: 1.0 X-Mailer: Evolution 2.28.1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.011 tagged_above=-10 required=5 tests=BAYES_40=-0.001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201010/53 X-Sequence-Number: 21021 Hi, I've got several questions regarding the behaviors of prepared statement in PostgreSQL. Environment: JDK version: 6 JDBC driver: 8.4-701.jdbc3 No connection pool is used prepareThreshold is set to 1 Code: for (int i = 0; i < 10; i++) { PreparedStatement statement = conn.prepareStatement("select * from gsd_36c_unique where tag_length = ?"); statement.setInt(1, 20); statement.executeQuery(); // statement is left un-closed on purpose; // check the contents of pg_prepared_statement; } Obvervation: The pg_prepared_statements contents of the 4th iteration is as follows: S_2 select * from gsd_36c_unique where tag_length = $1 2010-10-06 19:01:06.052824+08 {integer} f S_4 select * from gsd_36c_unique where tag_length = $1 2010-10-06 19:01:06.63442+08 {integer} f S_3 select * from gsd_36c_unique where tag_length = $1 2010-10-06 19:01:06.317623+08 {integer} f S_1 BEGIN 2010-10-06 19:01:06.052524+08 {} f S_5 select * from gsd_36c_unique where tag_length = $1 2010-10-06 19:01:06.811521+08 {integer} f Discussion: 1. Even though all the prepare statements have the identical SQL string, the postgresql server creates one execution plan for each statement. So I think the execution plan is fetched by name (S_1, S_2, etc.) instead of SQL query string from the server-side. On the other hand, a JDBC code tracing shows that the execution plan is fetched by SQL query String. Is the above assertion correct? 3. In the 5th iteration all the previously-prepared statements are dropped from the postgresql server, the pg_prepared_statements shows the following: pg_prepared_statemnt S_6 select * from gsd_36c_unique where tag_length = $1 2010-10-06 19:01:06.99907+08 {integer} f pg_prepared_statemnt S_1 BEGIN 2010-10-06 19:01:06.052524+08 {} f I think it's because postgresql JDBC Driver has a default limit for the max number of preparedStatement, which is 4. Can anyone tell me where to adjuest this parameter? Cheers, Louis