pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: louis <louis@ibms.sinica.edu.tw>
To: pgsql-jdbc@postgresql.org
Subject: The logic behind the prepared statement in PostgreSQL
Date: Wed, 06 Oct 2010 19:41:42 +0800
Message-ID: <1286365302.13691.35.camel@london> (raw)

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

view thread (8+ messages)  latest in thread

Message-ID: <1286365302.13691.35.camel@london>
Permalink:  ../1286365302.13691.35.camel@london/
Also on:    postgresql.org/message-id/1286365302.13691.35.camel@london

 · 

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: louis@ibms.sinica.edu.tw
  Subject: Re: The logic behind the prepared statement in PostgreSQL
  In-Reply-To: <1286365302.13691.35.camel@london>
  References: <1286365302.13691.35.camel@london>

* 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