Re: parametrized statements, but always replan?

From: Kevin Murphy <murphy(at)genome(dot)chop(dot)edu>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: parametrized statements, but always replan?
Date: 2006-12-20 21:22:00
Message-ID: 4589A978.9030501@genome.chop.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

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

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message Kevin Murphy 2006-12-20 21:28:50 Re: parametrized statements, but always replan?
Previous Message Mark Lewis 2006-12-20 20:41:39 Re: parametrized statements, but always replan?