pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kevin Murphy <murphy@genome.chop.edu>
To: pgsql-jdbc@postgresql.org
Subject: Re: parametrized statements, but always replan?
Date: Wed, 20 Dec 2006 16:22:00 -0500
Message-ID: <4589A978.9030501@genome.chop.edu> (raw)
In-Reply-To: <81F542C8-256C-4C29-AC7D-9223BBB3B2E0@fastcrypt.com>
References: <45899296.9040008@genome.chop.edu>
	<81F542C8-256C-4C29-AC7D-9223BBB3B2E0@fastcrypt.com>

Dave Cramer wrote:
> The only time prepared statements won't replan is if you re-use the 
> prepared statement, which your code does not.
>
> However... AFAIK, postgresql does not plan based on the parameter.

OK, PG definitely plans based on the parameter value.   I turned on plan 
logging in the server log, and without specifying prepareThreshold at 
all (e.g. setting it to 0), I can see that PG is making different plan 
estimates for different parameter values.  It might be using the same 
general plan, but I can't bear to wade through all that output to tell.  
Looking at ANALYZE output on the unadorned, non-prepared query, I can 
see that different plans may be made, depending on the parameter.  In a 
table with millions of rows, there are tens of thousands of distinct 
values of this parameter, with a very wide distribution.  One value has 
200K occurrences, many others just a handful.  Using a different plan 
for the outliers can be (and is) very helpful.  When I wrap the query in 
a SQL PREPARE statement, a generic plan is made that works OK for 99.9% 
of the column values, but for the most common column value, that plan is 
20,000 times slower than the plan that is chosen by the planner when it 
knows that the query involves that most common value.  Hence my interest 
in what is going on behind prepareStatement().  I'm good now.

-Kevin Murphy




view thread (7+ messages)  latest in thread

Message-ID: <4589A978.9030501@genome.chop.edu>
Permalink:  ../4589A978.9030501@genome.chop.edu/
Also on:    postgresql.org/message-id/4589A978.9030501@genome.chop.edu

 · 

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: murphy@genome.chop.edu
  Subject: Re: parametrized statements, but always replan?
  In-Reply-To: <4589A978.9030501@genome.chop.edu>
  References: <45899296.9040008@genome.chop.edu> <81F542C8-256C-4C29-AC7D-9223BBB3B2E0@fastcrypt.com> <4589A978.9030501@genome.chop.edu>

* 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