pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: François Beausoleil <francois@teksol.info>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Help with writing a generate_series(tsmultirange, interval)
Date: Sat, 31 Jul 2021 17:34:31 -0700
Message-ID: <77b75e5a-c765-2161-9588-45958e23a660@aklaver.com> (raw)
In-Reply-To: <D4F46A5E-1619-4508-9693-4C6F3C251F5B@teksol.info>
References: <7DEBDB22-4E50-4A78-99FE-AF8C03261665@teksol.info>
	<7aa77027-63c5-21df-181f-0852f9ceadf9@aklaver.com>
	<D4F46A5E-1619-4508-9693-4C6F3C251F5B@teksol.info>

On 7/31/21 5:16 PM, François Beausoleil wrote:
> Hello Adrian,
> 

> Yes, in fact, I wrote the following:
> 
> --------------------------------------------------------------------------------------------------------------------------
> 
> CREATE FUNCTION generate_series(tstzrange, interval) RETURNS SETOF 
> timestamp with time zone AS $$
>    SELECT n
>    FROM generate_series(lower($1), upper($1), $2) AS t0(n)
>    WHERE $1 @> n
> $$ LANGUAGE sql immutable;
> 
> CREATE FUNCTION generate_series(tsrange, interval) RETURNS SETOF 
> timestamp without time zone AS $$
>    SELECT n
>    FROM generate_series(lower($1), upper($1), $2) AS t0(n)
>    WHERE $1 @> n
> $$ LANGUAGE sql immutable;
> 
> That was the easy part. My end goal is to iterate over a tsmultirange: I 
> would like to get each individual range from a given multi range. 
> Ideally, I’d like to do that without parsing the textual version of the 
> multi range.
> 
> While mowing the lawn, I thought that since the syntax of multi ranges 
> is similar to arrays, maybe I could use unnest(), but sadly, that was 
> not to be the case:
> 
> # select 
> unnest('{[2021-08-02,2021-08-04],[2021-08-07,2021-08-09)}'::tsmultirange);
> ERROR:  function unnest(tsmultirange) does not exist
> LINE 1: select unnest('{[2021-08-02,2021-08-04],[2021-08-07,2021-08-...
> 
> Apparently, PG can accept multi range values, but can’t do much with 
> them at the time, except to check for inclusion/exclusion.

I see your mowing the lawn and raise walking the dog. This rang some 
bells and then I remembered when in doubt consult depesz:

https://www.depesz.com/2021/07/15/how-to-get-list-of-elements-from-multiranges/

https://www.postgresql.org/message-id/20210715121508.GA30348@depesz.com

> 
> Thanks for your time!
> François
> 
>>> François
>>
>>
>> --
>> Adrian Klaver
>> adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (6+ messages)  latest in thread

Message-ID: <77b75e5a-c765-2161-9588-45958e23a660@aklaver.com>
Permalink:  ../77b75e5a-c765-2161-9588-45958e23a660@aklaver.com/
Also on:    postgresql.org/message-id/77b75e5a-c765-2161-9588-45958e23a660@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, francois@teksol.info
  Subject: Re: Help with writing a generate_series(tsmultirange, interval)
  In-Reply-To: <77b75e5a-c765-2161-9588-45958e23a660@aklaver.com>
  References: <7DEBDB22-4E50-4A78-99FE-AF8C03261665@teksol.info> <7aa77027-63c5-21df-181f-0852f9ceadf9@aklaver.com> <D4F46A5E-1619-4508-9693-4C6F3C251F5B@teksol.info> <77b75e5a-c765-2161-9588-45958e23a660@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox