Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 62A73633F51 for ; Tue, 3 Nov 2009 19:52:59 -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 25492-01 for ; Tue, 3 Nov 2009 23:52:42 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from vexbert.mr-paradox.net (vexbert.mr-paradox.net [198.145.180.174]) by mail.postgresql.org (Postfix) with ESMTP id C5C4963292A for ; Tue, 3 Nov 2009 19:52:48 -0400 (AST) Received: by vexbert.mr-paradox.net (Postfix, from userid 18391) id AA4CE8459D; Tue, 3 Nov 2009 18:52:46 -0500 (EST) Date: Tue, 3 Nov 2009 15:52:46 -0800 From: David Kerr To: Craig Ringer Cc: pgsql-performance@postgresql.org Subject: Re: Optimizer + bind variables Message-ID: <20091103235245.GA77360@mr-paradox.net> References: <20091103174729.GA55736@mr-paradox.net> <4AF0C014.4090000@postnewspapers.com.au> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4AF0C014.4090000@postnewspapers.com.au> User-Agent: Mutt/1.4.2.3i X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200911/32 X-Sequence-Number: 36200 On Wed, Nov 04, 2009 at 07:43:16AM +0800, Craig Ringer wrote: - 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. hmm, that's a little unclear to me. let's assume that the application is using prepare: Assuming the database hasn't changed, would: PREPARE bla1 as SELECT * from users where username = '$1'; explain execute bla1 give the same output as explain select * from users where username = 'dave'; ? - 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. that's interesting, i'll need to find out which mine are using, probably a mix of both. - > if it's possible for the plan to be different how can i generate an - > xplan for the bind version? - - xplan = explain? If so: yeah, sorry. - 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) great thanks! Dave