pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <heikki@enterprisedb.com>
To: Flipper <flipper@gammadue.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: prepared statement: are they pre-compiled?
Date: Thu, 05 Jul 2007 09:53:51 +0100
Message-ID: <468CB19F.6080108@enterprisedb.com> (raw)
In-Reply-To: <200707051023.25650.flipper@gammadue.com>
References: <200707051023.25650.flipper@gammadue.com>

Flipper wrote:
> Hi all,
> a little question about prepared statement. I know that one advantage of using 
> prepared statement is the fact that the statament could be "precompiled" by 
> the server before the binding of parameters, is it true? 
> But while observing the messages sent from the FE to the BE by the postgresql 
> driver, I noticed that the query is sent along with the bind of the 
> parameters, that is the when the executeQuery() method is called on the 
> prepared statement, both the parse and bind messages are sent. I thought the 
> bind message have to be sent before the bind one, but it seems to me it's 
> not. Anyone can please help me understand this?

It depends on the prepareThreshold parameter. The default is 5, which 
means that the query is parsed and planned separately for each call to 
executeQuery, until the 5th call. After that, a generic plan is 
generated and used thereafter.

For queries that are executed few times, planning the query each time is 
more efficient because the parameter values can be used in the planning, 
which can give you a better plan. For example, if you have a partial 
index on X > 10, and you run a query with WHERE X > ?, you can only use 
the partial index if the parameter > 10. When the query is planned using 
the parameter given, the planner can check its value and use the partial 
index when possible, but when the plan is made for use with arbitrary 
parameters later on, the partial index can't be used. Partial indexes 
are an obvious example, but there's many other cases where the parameter 
values can make a big difference in the plan.

-- 
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com



view thread (5+ messages)  latest in thread

Message-ID: <468CB19F.6080108@enterprisedb.com>
Permalink:  ../468CB19F.6080108@enterprisedb.com/
Also on:    postgresql.org/message-id/468CB19F.6080108@enterprisedb.com

 · 

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: heikki@enterprisedb.com, flipper@gammadue.com
  Subject: Re: prepared statement: are they pre-compiled?
  In-Reply-To: <468CB19F.6080108@enterprisedb.com>
  References: <200707051023.25650.flipper@gammadue.com> <468CB19F.6080108@enterprisedb.com>

* 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