Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id C5FAB9FB2CA for ; Thu, 1 Feb 2007 21:44:37 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 66782-06 for ; Thu, 1 Feb 2007 21:44:29 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id DA3419F93A4 for ; Thu, 1 Feb 2007 21:44:29 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from ambcs01.ccalmr.ogi.edu (ambcs01.ccalmr.ogi.edu [129.95.72.11]) by svr4.postgresql.org (Postfix) with ESMTP id 4188F5AF892 for ; Fri, 2 Feb 2007 01:44:29 +0000 (GMT) Received: from [129.95.72.57] (amb25.ccalmr.ogi.edu [129.95.72.57]) by ambcs01.ccalmr.ogi.edu (8.12.11.20060308/8.12.11) with ESMTP id l121iL4w007366 for ; Thu, 1 Feb 2007 17:44:21 -0800 Message-ID: <45C29775.3020507@stccmop.org> Date: Thu, 01 Feb 2007 17:44:21 -0800 From: Bill Howe User-Agent: Thunderbird 1.5.0.9 (X11/20061219) MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: index scan through a subquery Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200702/54 X-Sequence-Number: 23108 Why should these queries have different plans? create table foo (a int PRIMARY KEY); Q1: explain select max(a) from foo > Result (cost=0.04..0.05 rows=1 width=0) > InitPlan > -> Limit (cost=0.00..0.04 rows=1 width=4) > -> Index Scan Backward using foo_pkey on foo > (cost=0.00..76.10 rows=2140 width=4) > Filter: (a IS NOT NULL) Q2: explain select max(a) from (select * from foo) as f > Aggregate (cost=36.75..36.76 rows=1 width=4) > -> Seq Scan on foo (cost=0.00..31.40 rows=2140 width=4) I need the lovely index scan, but my table is hidden behind a view, and all I get is the ugly sequential scan. Any ideas on how to convince the optimizer to unfold the subquery properly? Bill