Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m40Go-0007Zb-EB for pgsql-general@arkaria.postgresql.org; Thu, 15 Jul 2021 12:15:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m40Gn-0000P6-EH for pgsql-general@arkaria.postgresql.org; Thu, 15 Jul 2021 12:15:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m40Gn-0000OM-2i for pgsql-general@lists.postgresql.org; Thu, 15 Jul 2021 12:15:13 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m40Gk-0004zT-IG for pgsql-general@postgresql.org; Thu, 15 Jul 2021 12:15:12 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Reply-To:Message-ID:Subject:Cc:To:Sender:From:Date: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=OOXjcUl/DKATayxs4N+lHRMoUM21Ia+f+wQrfES53EE=; b=dX6csCQGiEMmQW1ljaI2d0H7C/ u+QCGHEOvo6GvahSgIwt/a3hgRfpZjtKeduCtuWIy+/Iyi8zF9Z9/yBbQswKmaynHrDelP6HAHSfu eQvxxBdj6gubAxT4hvxPt9pCaMAC2B4izycYGtsh6Y2lRfq6rIE5Nz/qEmJFOZXRtMso=; Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by depesz.com with esmtpa (Exim 4.92) (envelope-from ) id 1m40Gj-0004HI-51; Thu, 15 Jul 2021 14:15:09 +0200 Date: Thu, 15 Jul 2021 14:15:09 +0200 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: Ben Chobot Cc: pgsql-general Subject: Re: looping over multirange segments? Message-ID: <20210715121508.GA30348@depesz.com> Reply-To: depesz@depesz.com References: <623b65ef-6bfe-ec35-45c0-341e8f9ab7a8@silentmedia.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <623b65ef-6bfe-ec35-45c0-341e8f9ab7a8@silentmedia.com> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Jul 14, 2021 at 04:19:48PM -0700, Ben Chobot wrote: > I'm really, really liking the multirange types in PG14. Thank you for making > them! Unfortunately I am struggling with how to loop over the segments of a > multirange. There doesn't seem to be a way to convert them to arrays, and I > can't just use plpgsql's FOREACH on one. Am I missing something obvious? It > seems like a reasonable thing to want to do. > > FWIW, my actual end goal is to take something like this: > > select int8range(1,10)::int8multirange - int8range(4,6)::int8multirange; >     ?column? > ──────────────── >  {[1,4),[6,10)} > > ...and turn it into this: > > select ???; >  int8range │ ?column? > ───────────┼────────── >  [1,4)     │        0 >  [6,10)    │        0 There isn't anything nice now. There was, for a brief moment, unnest() over multiranges, but it got reverted. While it's not there, you can use regexps to split it. I know it's ugly, but it should work, for now. For example you can: select * from regexp_matches(_YOUR_MULTIRANGE_::text, '[\[(][^\])]+[\])]', 'g'); I wrote more, including explanation, and ready-to-use function, in here: https://www.depesz.com/2021/07/15/how-to-get-list-of-elements-from-multiranges/ depesz