Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id C88FB2E2DFF for ; Tue, 4 Dec 2007 16:15:30 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 30553-09 for ; Tue, 4 Dec 2007 16:15:09 -0400 (AST) Received: from oxford.xeocode.com (unknown [87.127.95.196]) by postgresql.org (Postfix) with ESMTP id 687172E2DE9 for ; Tue, 4 Dec 2007 16:15:23 -0400 (AST) Received: from localhost.enta.net ([127.0.0.1] helo=oxford.xeocode.com) by oxford.xeocode.com with esmtp (Exim 4.67) (envelope-from ) id 1IzeAr-0001XY-Pz; Tue, 04 Dec 2007 20:15:21 +0000 To: "Julian Mehnle" Cc: Subject: Re: Bad query plans for queries on partitioned table In-Reply-To: <200712041944.13543.julian@mehnle.net> (Julian Mehnle's message of "Tue\, 4 Dec 2007 19\:44\:08 +0000") User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux) X-Draft-From: ("nnimap+mail01.enterprisedb.com:INBOX.performance" 3476) References: <200712041944.13543.julian@mehnle.net> From: Gregory Stark Organization: EnterpriseDB Date: Tue, 04 Dec 2007 20:15:21 +0000 Message-ID: <87ir3e6wti.fsf@oxford.xeocode.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/57 X-Sequence-Number: 27873 "Julian Mehnle" writes: > However, if I restrict the query to just the partitions that actually do > have data in them ... There are a few things going on here. 1) The optimizer can't build a plan which ignores those partitions because the statistics are just approximations. You could insert into one of them at any time and the statistics won't update immediately. If you have a partition which is empty of some type of data you can put a constraint on it to promise the optimizer that that condition will stay true. 2) The optimizer is assuming that empty tables have a default 1,000 records in them with no idea about their statistics. Otherwise you get terrible plans on tables which have just been created or never analyzed. In this case that's causing it to think there will be tons of matches on what is apparently a very selective criterion. 3) The optimizer is a bit dumb about partitioned tables. But I'm not sure if that's actually the fault here. Try adding one record of data to each of those partitions or putting a constraint on them which will allow constraint_exclusion (I assume you have that enabled?) to kick in. You'll still be bitten by the parent table but hopefully that's not enough to cause a problem. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com Ask me about EnterpriseDB's On-Demand Production Tuning