Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id CED98133798E for ; Sat, 5 Feb 2011 01:03:19 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 45880-06 for ; Sat, 5 Feb 2011 05:03:12 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.17.8]) by mail.postgresql.org (Postfix) with ESMTP id 0DD071336FA7 for ; Sat, 5 Feb 2011 01:03:12 -0400 (AST) Received: from [192.168.1.6] (mail.highperformancepostgresql.com [71.179.240.8]) by mrelayeu.kundenserver.de (node=mrbap2) with ESMTP (Nemesis) id 0M6xID-1Q6XbH0lL3-00xMWz; Sat, 05 Feb 2011 06:03:10 +0100 Message-ID: <4D4CDA0C.20000@2ndquadrant.com> Date: Sat, 05 Feb 2011 00:03:08 -0500 From: Greg Smith User-Agent: Thunderbird 2.0.0.24 (X11/20100317) MIME-Version: 1.0 To: Tobias Brox CC: pgsql-performance@postgresql.org Subject: Re: table partitioning and select max(id) References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:IOei8hHu9ACxSfVzX+4PHequdXPgpzGBbB7VsKZFTcV jTg4lTGm/zNTmLBRzhUfiGfMVOGHH01mmO48IesN1NDEdBz7+j fcxwp1+EarYD1WY1uPdhbroCGEdcjHADtu93qUlwOCpAZTZylw yAOz5zBgmVyWDfpU99LAZqf/8fSzU9l/od+2a3qI9fUkagcecl 1Z4qcq2gpFBj37d1JzEY6ChEo9IMNBnk4WHX8dT18w= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201102/235 X-Sequence-Number: 42373 Tobias Brox wrote: > I implemented table partitioning, and it caused havoc with a "select > max(id)" on the parent table - the query plan has changed from a > lightningly fast backwards index scan to a deadly seq scan. This problem was fixed in the upcoming 9.1: http://archives.postgresql.org/pgsql-committers/2010-11/msg00028.php http://git.postgresql.org/gitweb?p=postgresql.git;a=commitdiff;h=034967bdcbb0c7be61d0500955226e1234ec5f04 Here's the comment from that describing the main technique used to fix it: "This module tries to replace MIN/MAX aggregate functions by subqueries of the form (SELECT col FROM tab WHERE ... ORDER BY col ASC/DESC LIMIT 1) Given a suitable index on tab.col, this can be much faster than the generic scan-all-the-rows aggregation plan. We can handle multiple MIN/MAX aggregates by generating multiple subqueries, and their orderings can be different. However, if the query contains any non-optimizable aggregates, there's no point since we'll have to scan all the rows anyway." Unfortunately that change ends a series of 6 commits of optimizer refactoring in this area, so it's not the case that you just apply this one commit as a bug-fix to a 9.0 system. I have a project in process to do the full backport needed I might be able to share with you if that works out, and you're willing to run with a customer patched server process. Using one of the user-space ideas Ken suggested may very well be easier for you. I'm stuck with an app I can't rewrite to do that. -- Greg Smith 2ndQuadrant US greg@2ndQuadrant.com Baltimore, MD PostgreSQL Training, Services, and 24x7 Support www.2ndQuadrant.us "PostgreSQL 9.0 High Performance": http://www.2ndQuadrant.com/books