Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id D84AF633DC6 for ; Tue, 3 Nov 2009 19:43:34 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 24027-05 for ; Tue, 3 Nov 2009 23:43:18 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by mail.postgresql.org (Postfix) with ESMTP id BA35B633F51 for ; Tue, 3 Nov 2009 19:43:23 -0400 (AST) Received: from localhost (access [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id C30A11E0443; Wed, 4 Nov 2009 07:43:18 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (access.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id pgvw5nrLMeDy; Wed, 4 Nov 2009 07:43:16 +0800 (WST) Received: from [10.1.1.5] (124-169-36-119.dyn.iinet.net.au [124.169.36.119]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTPSA id C50BF1E0440; Wed, 4 Nov 2009 07:43:16 +0800 (WST) Message-ID: <4AF0C014.4090000@postnewspapers.com.au> Date: Wed, 04 Nov 2009 07:43:16 +0800 From: Craig Ringer User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: David Kerr CC: pgsql-performance@postgresql.org Subject: Re: Optimizer + bind variables References: <20091103174729.GA55736@mr-paradox.net> In-Reply-To: <20091103174729.GA55736@mr-paradox.net> X-Enigmail-Version: 0.95.7 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.351 tagged_above=-10 required=5 tests=AWL=0.248, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200911/31 X-Sequence-Number: 36199 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