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.89) (envelope-from ) id 1hufJ6-0000VR-99 for pgsql-general@arkaria.postgresql.org; Mon, 05 Aug 2019 15:53:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hufJ4-0000ik-MI for pgsql-general@arkaria.postgresql.org; Mon, 05 Aug 2019 15:53:54 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hufJ4-0000Ty-6b for pgsql-general@lists.postgresql.org; Mon, 05 Aug 2019 15:53:54 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1hufJ1-0007PL-A8 for pgsql-general@postgresql.org; Mon, 05 Aug 2019 15:53:52 +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 x75FrlTW002668; Mon, 5 Aug 2019 11:53:47 -0400 From: Tom Lane To: Alvaro Herrera cc: Jeremy Finzel , Postgres General Subject: Re: Aggregate not using BRIN index on timestamp In-reply-to: <20190805154238.GA7309@alvherre.pgsql> References: <20190805154238.GA7309@alvherre.pgsql> Comments: In-reply-to Alvaro Herrera message dated "Mon, 05 Aug 2019 11:42:38 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2666.1565020427.1@sss.pgh.pa.us> Date: Mon, 05 Aug 2019 11:53:47 -0400 Message-ID: <2667.1565020427@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alvaro Herrera writes: > For btrees, we have planagg.c which transforms min() and max() into > subqueries (SELECT .. WHERE ... ORDER BY .. LIMIT 1). > In a BRIN index, you could execute the search by scanning the index to > determine which ranges contain the least/greatest values, and then using > a bitmap scan to scan those. I'm not sure that this is a transformation > that can be applied cleanly, since that thing I describe doesn't look to > be a "subquery". But maybe it can -- I think you'd need a special > executor node. FWIW, I suspect the hard part would be dealing with cases where the extremal ranges (according to the index) contain no live tuples (according to the query's snapshot). The btree case handles the invisible-tuples problem by continuing a scan started at the index endpoint until it finds a visible tuple --- which, in the worst case, can take a long time. It's not obvious to me what you'd do with BRIN. regards, tom lane