pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
PreparedStatement clearParameters
4+ messages / 3 participants
[nested] [flat]

* PreparedStatement clearParameters
@ 2006-02-20 12:58  Thomas Burdairon <tburdairon@entelience.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Thomas Burdairon @ 2006-02-20 12:58 UTC (permalink / raw)
  To: pgsql-jdbc

Hello all
First, i'm not sure about if this is the right place to post it, but  
didn't see any bugtracker on the web site.
I don't know if it's a known bug, i didn't found any reference to it  
on the Todo page, or in mailing list archives
Then, i don't even know if it's a bug, maybe it's a feature (?)

I've seen a strange comportment of implementation of  
PreparedStatement and especially the clearParameters method.
The normal functionnement of this is that if I create a  
PreparedStatement, the query is sent once to the server, and then  
only parameters are sent to the server.
clearParameters erase parameters already set in the PreparedStatement  
object.

But, it seems the clearParameters method remove us all the benefit of  
prepare statements, because the query is sent again on each  
executeQuery/executeUpdate call.
But without using clearParameters, the comportment is correct

Tested on
Postgresql : 8.1 (MacOsX)
JDBC driver : release 404

sample code :


create table test(id int, desc text);


int n=1000;
PreparedStatement pst = db.prepareStatement("INSERT INTO test(id,  
desc) VALUES (?, ?)");
for(int i=0; i< n; i++){
     pst.setInt(1, i);
     pst.setString(2, "haha");
     pst.executeUpdate();
}

//opposite to

PreparedStatement pst2 = db.prepareStatement("INSERT INTO test(id,  
desc) VALUES (?, ?)");
for(int i=0; i< n; i++){
     pst.clearParameters();
     pst.setInt(1, i);
     pst.setString(2, "hoho");
     pst.executeUpdate();
}


To check the communication between jdbc driver and postgresql server,  
you can use this simple shell script :
sudo tcpdump -i lo0 -A -s 65535 tcp port 5432  (assuming the  
postgresql port is 5432)




Thomas





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: PreparedStatement clearParameters
@ 2006-02-20 17:58  Kris Jurka <books@ejurka.com>
  parent: Thomas Burdairon <tburdairon@entelience.com>
  1 sibling, 1 reply; 4+ messages in thread

From: Kris Jurka @ 2006-02-20 17:58 UTC (permalink / raw)
  To: Thomas Burdairon <tburdairon@entelience.com>; +Cc: pgsql-jdbc



On Mon, 20 Feb 2006, Thomas Burdairon wrote:

> I've seen a strange comportment of implementation of PreparedStatement 
> and especially the clearParameters method. The normal functionnement of 
> this is that if I create a PreparedStatement, the query is sent once to 
> the server, and then only parameters are sent to the server. 
> clearParameters erase parameters already set in the PreparedStatement 
> object.
>
> But, it seems the clearParameters method remove us all the benefit of 
> prepare statements, because the query is sent again on each 
> executeQuery/executeUpdate call. But without using clearParameters, the 
> comportment is correct.

Yes, this is a problem.  The coding of clearParameters clears the type 
information associated with the PreparedStatement as well as the bind 
values.  This causes the query to get reprepared as it thinks the types 
have changed.  This code is necessary because the statement must 
be replanned when something like this happens:

ps.setInt(1, 1);
ps.execute();
ps.setLong(1, 1);
ps.execute();

I suspect that just not clearing the type information will fix this, but 
I'll need to test that a little.  Thanks for the report.

Kris Jurka




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: PreparedStatement clearParameters
@ 2006-02-21 01:42  Kris Jurka <books@ejurka.com>
  parent: Kris Jurka <books@ejurka.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Kris Jurka @ 2006-02-21 01:42 UTC (permalink / raw)
  To: Thomas Burdairon <tburdairon@entelience.com>; +Cc: pgsql-jdbc



On Mon, 20 Feb 2006, Kris Jurka wrote:

> On Mon, 20 Feb 2006, Thomas Burdairon wrote:
>
>> I've seen a strange comportment of implementation of PreparedStatement and 
>> especially the clearParameters method. The normal functionnement of this is 
>> that if I create a PreparedStatement, the query is sent once to the server, 
>> and then only parameters are sent to the server. clearParameters erase 
>> parameters already set in the PreparedStatement object.
>> 
>> But, it seems the clearParameters method remove us all the benefit of 
>> prepare statements, because the query is sent again on each 
>> executeQuery/executeUpdate call. But without using clearParameters, the 
>> comportment is correct.
>
> Yes, this is a problem.  The coding of clearParameters clears the type 
> information associated with the PreparedStatement as well as the bind values. 
> This causes the query to get reprepared as it thinks the types have changed.

Actually I was too quick to agree with you.  Testing this doesn't show 
anything going wrong.  I suspect you are seeing the effect of the 
prepareThreshold URL parameter.  By default we only reuse a 
PreparedStatement on the fifth execution of it, before that we reprepare 
it every time because of the potential performance differences from 
generating a plan with exact vs default values.  Many people use 
PrepareStatements only for the cleanliness and portability of avoiding 
interpolating parameters manually and never reuse the statement.  For 
single use queries we want the planner/optimizer to create the best 
available plan for that query, so we do not reuse the plan for later 
executions.

Please try your testcase again adding loglevel=2 to your URL to 
show when the driver is actually sending Parse messages.  Then try adding 
prepareThreshold=1 to your URL to tell the driver that you want to reuse 
the query plan after one execution.

Kris Jurka



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: PreparedStatement clearParameters
@ 2006-02-21 10:21  Marc Herbert <Marc.Herbert@continuent.com>
  parent: Thomas Burdairon <tburdairon@entelience.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Marc Herbert @ 2006-02-21 10:21 UTC (permalink / raw)
  To: pgsql-jdbc

Thomas Burdairon <tburdairon@entelience.com> writes:

> But, it seems the clearParameters method remove us all the benefit of
> prepare statements, because the query is sent again on each
> executeQuery/executeUpdate call.

> Then, i don't even know if it's a bug, maybe it's a feature (?)

I have never read anything in the specifications of the JDBC interface
that _guarantees_ anything concerning performance. I only found more
or less fuzzy statements like "it is pre-compiled" (what does that
mean formally? to what extent?) or that "execution can be faster".

The corresponding ODBC documentation is a bit more precise
 <http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcprepared_execution.asp;
except it specifically caters for the zero-preparation case:
 
  If the driver cannot emulate statement preparation, it stores the
  statement when SQLPrepare is called and submits it for execution when
  SQLExecute is called.
  Because emulated statement preparation is not perfect, SQLExecute can
  return any errors normally returned by SQLPrepare.

So this definitely look like a feature ;-)

Please correct me when I am wrong.

By the way ODBC does not confuse parametrization and preparation like
JDBC does.





^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2006-02-21 10:21 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-02-20 12:58 PreparedStatement clearParameters Thomas Burdairon <tburdairon@entelience.com>
2006-02-20 17:58 ` Kris Jurka <books@ejurka.com>
2006-02-21 01:42   ` Kris Jurka <books@ejurka.com>
2006-02-21 10:21 ` Marc Herbert <Marc.Herbert@continuent.com>

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