Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 7E2D66331A0 for ; Fri, 3 Apr 2009 10:46:40 -0300 (ADT) 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 24901-08 for ; Fri, 3 Apr 2009 10:46:25 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.249]) by mail.postgresql.org (Postfix) with ESMTP id 37F84632EDA for ; Fri, 3 Apr 2009 10:46:34 -0300 (ADT) Received: by an-out-0708.google.com with SMTP id c5so690917anc.28 for ; Fri, 03 Apr 2009 06:46:32 -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=uFxZxvBX/9RBg8ezX7rc/C0+zDArF4IWn2af5LHiXjc=; b=sXW2dOp+hPbRaZvkKeuCtAZx6R5YqsmPJye6n/wcW9sPW97UuWuyQ0gQN1EJmdvwtk DufYA95iQGd5Fi1vfRJhjT11I6qothPBhk4l4QIObAcJLBWQJDjyMbyu7a9E4Z/3fD7Q JbXUZFKibo8mBKTCN+mCSsQd7Cjald/XK7pRo= 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=RfGCyai4C+TdTO9nmeEXdCro0FqcUSHCk8NJVvl2+uwWnB19t+s3ssU0HBHffnRBjC GjALToGbQiBk3g2ohX8dkRCpqjgm7lrN1PZLqDOE+sGOg+hlfPWmBa0K2D5rQ0Sa8Hr3 Vo/MXIBBOCpN7gqP2S+hr5fDMUjI0tJmIqoBw= MIME-Version: 1.0 Received: by 10.100.163.8 with SMTP id l8mr2732825ane.104.1238766392582; Fri, 03 Apr 2009 06:46:32 -0700 (PDT) In-Reply-To: References: Date: Fri, 3 Apr 2009 09:46:32 -0400 Message-ID: <603c8f070904030646u654f4cbbh92e7b42180fa04d0@mail.gmail.com> Subject: Re: Rewriting using rules for performance From: Robert Haas 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.085 tagged_above=0 required=5 tests=AWL=0.085 X-Spam-Level: X-Archive-Number: 200904/59 X-Sequence-Number: 33426 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. I think this would be clearer if you gave an actual example of what you're trying to accomplish, but the short answer is "no". The rule system lets you create multiple query trees to perform multiple actions (for example, when an INSERT command is issued, do the original insert plus also an update) and it implements views. But it's independent of query planning. On the other hand, the query planner should be figuring out which index to use without any help from you. If it's not, something is wrong. ...Robert