Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id C7C426327C9 for ; Fri, 3 Apr 2009 12:52:39 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 16821-10 for ; Fri, 3 Apr 2009 12:52:38 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-bw0-f169.google.com (mail-bw0-f169.google.com [209.85.218.169]) by mail.postgresql.org (Postfix) with ESMTP id 4044D632581 for ; Fri, 3 Apr 2009 12:52:37 -0300 (ADT) Received: by bwz17 with SMTP id 17so1615886bwz.19 for ; Fri, 03 Apr 2009 08:52:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=XeFdMhf32BC28qT4oVj9xKylZfiMFn177JQ0xMt/Zk8=; b=pJgMHO1ioHJKa3LugBSd+D++E2r3JvrYw9VeqtpksQiuJkmvgJ9VXEMQG6M+z7wYM6 C30Wsynv/Z+MzVWHNYY9C3G7czCskdlXROPDZxOrbQ6keNQPJVnWmcoKsjIxV7JX72DM TfiDjqNK+aeCDQXWU9nM1wmW5hGthjay4bATs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=xHOmowVu+NpIbfF0o+HBTzaYFMXvncmxwqCdVoEXh0E9NSwa89UPA71JmSUBFTTGuh u/eZnQrtiP59l5jE/qijCrGtacfDSCK35BI3iTme8TPY8lFwf5GrMNrQvAkk6/jkQyus hCOtM/Gh8m21JO6AWcU7Za47I0ZgZ9CQICpfI= MIME-Version: 1.0 Received: by 10.103.228.7 with SMTP id f7mr667219mur.0.1238773956747; Fri, 03 Apr 2009 08:52:36 -0700 (PDT) In-Reply-To: References: Date: Fri, 3 Apr 2009 11:52:36 -0400 Message-ID: Subject: Re: Rewriting using rules for performance From: Merlin Moncure To: Matthew Wakeling Cc: pgsql-performance@postgresql.org 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=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200904/75 X-Sequence-Number: 33442 On Fri, Apr 3, 2009 at 9:17 AM, Matthew Wakeling wrote: > > So, I have a view. The query that the view uses can be written two different > ways, to use two different indexes. Then I use the view in another query, > under some circumstances the first way will be quick, and under other > circumstances the second way will be quick. > > What I want to know is, can I create a view that has both queries, and > allows the planner to choose which one to use? The documentation seems to > say so in http://www.postgresql.org/docs/8.3/interactive/querytree.html (the > rule system "creates zero or more query trees as result"), but doesn't say > how one would do it. yes. create view v as select * from ( select true as b, pg_sleep(1)::text union all select false as b, pg_sleep(1)::text ) q; recent versions of pg are smart enough to optimize (in some cases): select * from v where b; merlin