Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id DB794B5D91F for ; Wed, 22 Jun 2011 06:56:24 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 19741-07 for ; Wed, 22 Jun 2011 09:56:18 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail-vw0-f46.google.com (mail-vw0-f46.google.com [209.85.212.46]) by mail.postgresql.org (Postfix) with ESMTP id 44755B5D8BD for ; Wed, 22 Jun 2011 06:56:17 -0300 (ADT) Received: by vws1 with SMTP id 1so457026vws.19 for ; Wed, 22 Jun 2011 02:56:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=juffo.org; s=google; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=ZBLKQehuL27lq8gSLtl3EIxV5pp2B/LUYMciekyOLhc=; b=JW729vcBGgBqR0PMafKOwqOP2VbmKoi88Fai6HKoRdeZhCDgrLJYVHcP+6iBXzSbsH xJ5y3TG3SluRcfkp3/6naBFMhnYsVqR8jxPBw+3mIDS5Uvl+3OYDwhi2UYkx8Uq+u69h pF6K+7rY8kDIpihnhLqKseCVdB6fUDZLwE69s= DomainKey-Signature: a=rsa-sha1; c=nofws; d=juffo.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; b=T+AwyntNQtAeJcJBFOoZBw9t5BDcTN1ew14TivC8AWOep6CfL1RrmA6VCUESRXpKhv M50N0iWks70WTXC6jQcxXxFuEcOIDpvNS53wIvy71Kj3v1cu0haMI+XSpKpQIDQ8FyUU AmzXe6XaEsfd4APY9hPzLdy33BU50B3iHUxwk= Received: by 10.220.188.7 with SMTP id cy7mr182766vcb.35.1308736576117; Wed, 22 Jun 2011 02:56:16 -0700 (PDT) MIME-Version: 1.0 Received: by 10.220.202.203 with HTTP; Wed, 22 Jun 2011 02:55:46 -0700 (PDT) In-Reply-To: <4DFA4D2F.8020101@peak6.com> References: <4DFA4D2F.8020101@peak6.com> From: Marti Raudsepp Date: Wed, 22 Jun 2011 12:55:46 +0300 Message-ID: Subject: Re: seq scan in the case of max() on the primary key column To: sthomas@peak6.com Cc: Magnus Hagander , Svetlin Manavski , pgsql-performance@postgresql.org Content-Type: text/plain; charset=UTF-8 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 X-Spam-Level: X-Archive-Number: 201106/208 X-Sequence-Number: 44141 On Thu, Jun 16, 2011 at 21:36, Shaun Thomas wrote: > You can call that instead of max, and it'll be much faster. You can create > an analog for min if you need it. So for this, you'd call: Cool, I've needed this function sometimes but never bothered enough to write it myself. Now I created a wiki snippet page for this handy feature here: https://wiki.postgresql.org/wiki/Efficient_min/max_over_partitioned_table With Jim Nasby's idea to use regclass instead of relation names, the function is now half its length and probably more reliable. There's no need to touch pg_class directly at all. I also changed it to return bigint instead of integer, as that's more versatile, and the performance loss is probably negligible. Regards, Marti