Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1j87vO-0004Mc-Ka for pgsql-sql@arkaria.postgresql.org; Sat, 29 Feb 2020 19:37:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j87vN-0004LA-Fu for pgsql-sql@arkaria.postgresql.org; Sat, 29 Feb 2020 19:37:21 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j87vN-0004JP-7S for pgsql-sql@lists.postgresql.org; Sat, 29 Feb 2020 19:37:21 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1j87vL-00030L-1q for pgsql-sql@lists.postgresql.org; Sat, 29 Feb 2020 19:37:20 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id 01TJbF0f016371; Sat, 29 Feb 2020 14:37:16 -0500 From: Tom Lane To: Wayne cc: pgsql-sql@lists.postgresql.org Subject: Re: min()/max() with BRIN indexes In-reply-to: <20200229165024.GD20190@ln-1.useunix.net> References: <20200229165024.GD20190@ln-1.useunix.net> Comments: In-reply-to Wayne message dated "Sat, 29 Feb 2020 16:50:24 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <16369.1583005035.1@sss.pgh.pa.us> Date: Sat, 29 Feb 2020 14:37:15 -0500 Message-ID: <16370.1583005035@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Wayne writes: > I have rather large tables that use a time stamp as an index. New entries > are continuously added to the table with the current time. If I convert > from BTREE to BRIN indexes and select records with specific date ranges > the BRIN is used and performance is acceptable. However I often want to > get the latest time stamp using the max() function. I didn't expect that > this would result in a sequential scan of the table and skip the BRIN > index. > Is this expected behavior? Yeah. In principle a BRIN index could be used to accelerate finding min or max, but there's no actual support for that at the moment ... and in any case, it'd still be substantially slower than the equivalent with a btree index, which can locate the extremal values immediately. For this particular case, you might be able to fake it with something like select max(ts) from mytab where ts > 'some cutoff' if you can estimate some not-too-far-before-current-time cutoff that you are sure you'll find some records after. regards, tom lane