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 1hjSHD-0002V4-1U for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Jul 2019 17:45:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hjSHB-0002RV-Ps for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Jul 2019 17:45:37 +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 1hjSHB-0002RO-Hb for pgsql-hackers@lists.postgresql.org; Fri, 05 Jul 2019 17:45:37 +0000 Received: from xvm-110-146.dc2.ghst.net ([46.226.110.146] helo=fetter.org) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hjSH8-0003zm-Al for pgsql-hackers@postgresql.org; Fri, 05 Jul 2019 17:45:37 +0000 Received: by fetter.org (Postfix, from userid 1001) id 5F6E040965; Fri, 5 Jul 2019 19:45:32 +0200 (CEST) Date: Fri, 5 Jul 2019 19:45:32 +0200 From: David Fetter To: Paul A Jungwirth Cc: Jeff Davis , Pgsql Hackers Subject: Re: range_agg Message-ID: <20190705174531.GE24679@fetter.org> References: <16d71dc8-34cf-5ebd-1ce5-ccd93c0a14f9@illuminatedcomputing.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Jul 05, 2019 at 09:58:02AM -0700, Paul A Jungwirth wrote: > On Mon, Jul 1, 2019 at 3:38 PM Jeff Davis wrote: > > > > For getting into core though, it should be a more complete set of > > related operations. The patch is implicitly introducing the concept of > > a "multirange" (in this case, an array of ranges), but it's not making > > the concept whole. > > > > What else should return a multirange? This patch implements the union- > > agg of ranges, but we also might want an intersection-agg of ranges > > (that is, the set of ranges that are subranges of every input). Given > > that there are other options here, the name "range_agg" is too generic > > to mean union-agg specifically. > > Thanks for the review! > > I like the idea of adding a multirange type that works like range > types, although I'm not sure I want to build it. :-) > > My own motivations for the range_agg patch are for temporal databases, > where I have two use-cases: checking temporal foreign keys [1] and > doing a Snodgrass "coalesce" operation [2]. The FKs use-case is more > important. For coalesce I just immediately UNNEST the array, so a > multirange would sort of "get in the way". It's no big deal if I can > cast a multirange to an array, although for something that would run > on every INSERT/UPDATE/DELETE I'd like to understand what the cast > would cost us in performance. But coalesce isn't part of SQL:2011 and > should be optional behavior or just something in an extension. The FKs > use case matters to me a lot more, and I think a multirange would be > fine for that. Also a multirange would solve the generics problem > Pavel asked about. (I'll say more about that in a reply to him.) > > I'm not convinced that an intersection aggregate function for > multiranges would be used by anyone, but I don't mind building one. > Every other *_agg function has an "additive" sense, not a > "subtractive" sense. json{,b}_object_agg are the closest since you > *could* imagine intersection semantics for those, but they are unions. > So in terms of *naming* I think using "range_agg" for union semantics > is natural and would fit expectations. (I'm not the first to name this > function range_agg btw: [3]). > > But there is clearly more than one worthwhile way to aggregate ranges: > > - David suggested weighted_range_agg and covering_range_agg. At PGCon If I understand the cases correctly, the combination of covering_range and multi_range types covers all cases. To recap, covering_range_agg assigns a weight, possibly 0, to each non-overlapping sub-range. A cast from covering_range to multi_range would meld adjacent ranges with non-zero weights into single ranges in O(N) (N sub-ranges) time and some teensy amount of memory for tracking progress of adjacent ranges of non-zero weight. Gaps would just be multi_range consisting of the sub-ranges of the covering_range with weight 0, and wouldn't require any tracking. What have I missed? Best, David. -- David Fetter http://fetter.org/ Phone: +1 415 235 3778 Remember to vote! Consider donating to Postgres: http://www.postgresql.org/about/donate