Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 0B5771335B36 for ; Wed, 6 Oct 2010 08:59:05 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 27560-05 for ; Wed, 6 Oct 2010 11:58:53 +0000 (UTC) X-Greylist: from auto-whitelisted 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 D64B41337B47 for ; Wed, 6 Oct 2010 08:58:52 -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 o96Bfck3009214 for ; Wed, 6 Oct 2010 19:41:39 +0800 (CST) Subject: The logic behind the prepared statement in PostgreSQL From: louis To: pgsql-jdbc@postgresql.org Content-Type: multipart/alternative; boundary="=-5igNkl21l2N83yV2vaz9" Date: Wed, 06 Oct 2010 19:41:42 +0800 Message-ID: <1286365302.13691.35.camel@london> Mime-Version: 1.0 X-Mailer: Evolution 2.28.1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.01 tagged_above=-10 required=5 tests=BAYES_20=-0.001, HTML_MESSAGE=0.001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201010/54 X-Sequence-Number: 21022 --=-5igNkl21l2N83yV2vaz9 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit 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 foo where value = ?"); 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 --=-5igNkl21l2N83yV2vaz9 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: 7bit 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 foo where value = ?");
	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

--=-5igNkl21l2N83yV2vaz9--