pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Oliver Jowett <oliver@opencloud.com>
To: Kris Jurka <books@ejurka.com>
Cc: pgsql-jdbc@postgresql.org <pgsql-jdbc@postgresql.org>
Cc: tgl@sss.pgh.pa.us
Subject: Re: v3proto Parse/Bind and the query planner
Date: Wed, 19 May 2004 11:50:09 +1200
Message-ID: <40AAA131.3010602@opencloud.com> (raw)
In-Reply-To: <Pine.BSO.4.56.0405171600500.24398@leary.csoft.net>
References: <40A84236.6070809@opencloud.com>
	<Pine.BSO.4.56.0405171600500.24398@leary.csoft.net>

Kris Jurka wrote:

> I seem to recall Tom Lane speculating about delaying the planning of a
> prepared statement until it's first execution so it would have the bound
> values and then using that plan with subsequent parameters.  In the common
> case multiple executions of a prepared query would use values of similar
> statistical likelihood, but this wouldn't solve the example you gave if
> you executed the plan twice for values that aren't similar.  I think this 
> would be a reasonable compromise position.

I don't see why the first execution is special in the general case.. you 
will suddenly get queries where the performance depends on the parameter 
values of *previous* queries (well beyond cache effects) which is not a 
very nice property to have.

Planning on the first Bind would be particularly bad for the case I 
presented earlier (highly uneven index distribution) -- if the first 
execution happens to use a highly selective value and chooses an index 
scan, subsequent executions with non-selective values will perform very 
badly (considerably worse than the seqscan currently chosen for a 
parameterized query). The cure seems worse than the disease in this case.

Instead, how about something like:

  - For named statements, plan at Parse time always.
  - For unnamed statements, plan at Bind time always.

The assumption here is that if the client is using the unnamed 
statement, it's unlikely that it will be repeatedly reusing that 
statement with different parameter values so there is little benefit to 
preserving the query plan at the cost of being unable to plan for 
specific parameter values. If the client is using named statements, 
there's no change in behaviour from the current approach, so presumably 
the client knows what it's doing! :)

A client that's aware of this behaviour can get some control over when 
the planning is done without needing a protocol change. e.g. in the JDBC 
driver we already have setUseServerPrepare(); that could be used to 
control whether named statements are used when executing a 
PreparedStatement or not. The application can enable named statements 
where it knows the cost of re-planning the query repeatedly is greater 
than the benefit of having actual parameter values to plan from -- e.g. 
if the indexes have an even distribution of values.

(is this better discussed on -hackers?)

>>The existing strategy of doing parameter replacement on the driver side 
>>works, but we lose the benefits of passing parameters via Bind.
> 
> 
> We also have the possibility of doing selective replacement and binding 
> other values.  This would allow using binary bytea transfers (which are 
> unlikely to be used in selectivity estimates) while doing parameter 
> replacement for other values.

This is a possibility but it's going to lead to pretty ugly code to 
track the JDBC parameter index vs. the backend's idea of the parameter 
index. We also go back to playing games with the parser to make it 
interpret our parameters as the type we specified at the JDBC level. It 
seems a bit counterproductive to jump through several layers of parsing 
and re-substitution just to specify parameter types that JDBC already 
knew, when there's already a protocol mechanism to specify type 
information directly to the backend.

We'd also end up with two code paths again if we wanted to benefit from 
named statements for queries that aren't affected by this case and would 
benefit from all of their parameters being real parameters (this is one 
of the reasons I'm making these changes in the first place, so..)

-O



view thread (7+ messages)  latest in thread

Message-ID: <40AAA131.3010602@opencloud.com>
Permalink:  ../40AAA131.3010602@opencloud.com/
Also on:    postgresql.org/message-id/40AAA131.3010602@opencloud.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: oliver@opencloud.com, books@ejurka.com, tgl@sss.pgh.pa.us
  Subject: Re: v3proto Parse/Bind and the query planner
  In-Reply-To: <40AAA131.3010602@opencloud.com>
  References: <40A84236.6070809@opencloud.com> <Pine.BSO.4.56.0405171600500.24398@leary.csoft.net> <40AAA131.3010602@opencloud.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