Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 779C06327ED for ; Mon, 1 Mar 2010 14:15:13 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 96833-04 for ; Mon, 1 Mar 2010 18:15:03 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id C42046322F3 for ; Mon, 1 Mar 2010 14:15:02 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id o21IF0j1020629; Mon, 1 Mar 2010 13:15:00 -0500 (EST) To: Rainer Pruy cc: "pgsql-performance@postgresql.org" Subject: Re: Query slowing down significantly?? In-reply-to: <4B8BFDEF.1080006@acrys.com> References: <4B8BB815.4060104@acrys.com> <17910.1267460132@sss.pgh.pa.us> <4B8BFDEF.1080006@acrys.com> Comments: In-reply-to Rainer Pruy message dated "Mon, 01 Mar 2010 18:48:31 +0100" Date: Mon, 01 Mar 2010 13:15:00 -0500 Message-ID: <20628.1267467300@sss.pgh.pa.us> From: Tom Lane 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: 201003/10 X-Sequence-Number: 37835 Rainer Pruy writes: > The prepared statement gives: > ... > And that is quite a bad plan given the current distribution of values. Yeah. The planner really needs to know the actual parameter values in order to pick the best plan for this case. One thing that you might be able to do to avoid giving up on prepared statements entirely is to use an "unnamed" rather than named prepared statement here. That will lead to the query plan being prepared only when the parameter values are made available, rather than in advance. It'd depend on what client library you're using whether this is a simple change or not. regards, tom lane