Re: Optimizer + bind variables

From: Craig Ringer <craig(at)postnewspapers(dot)com(dot)au>
To: David Kerr <dmk(at)mr-paradox(dot)net>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Optimizer + bind variables
Date: 2009-11-03 23:43:16
Message-ID: 4AF0C014.4090000@postnewspapers.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

David Kerr wrote:
> Does/is it possible for the PG optimizer come up with differnet plans when
> you're using bind variables vs when you send static values?

Yes, if the bind variable form causes your DB access driver to use a
server-side prepared statement. Pg can't use its statistics to improve
its query planning if it doesn't have a value for a parameter when it's
building the query plan.

Whether a server-side prepared statement is used or not depends on how
you're connecting to the database - ie your DB access driver and
version. If you're using JDBC, I *think* the JDBC driver does parameter
placement client-side unless you're using a JDBC prepared statement and
the JDBC prepared statement is re-used several times, at which point it
sets up a server-side prepared statement. AFAIK otherwise it uses
client-side (or Pg protocol level) parameter placement.

> if it's possible for the plan to be different how can i generate an
> xplan for the bind version?

xplan = explain? If so:

Use PREPARE to prepare a statement with the params, then use:

EXPLAIN EXECUTE prepared_statement_name(params);

eg:

x=> PREPARE blah AS SELECT * FROM generate_series(1,100);
PREPARE
x=> EXPLAIN EXECUTE blah;
QUERY PLAN
------------------------------------------------------------------------
Function Scan on generate_series (cost=0.00..12.50 rows=1000 width=4)
(1 row)

--
Craig Ringer

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message David Kerr 2009-11-03 23:52:46 Re: Optimizer + bind variables
Previous Message Kevin Grittner 2009-11-03 21:50:58 Re: Queryplan within FTS/GIN index -search.