From: Adrian Klaver <adrian.klaver@aklaver.com>
To: François Beausoleil <francois@teksol.info>
To: pgsql-general@lists.postgresql.org
Subject: Re: Help with writing a generate_series(tsmultirange, interval)
Date: Sat, 31 Jul 2021 12:49:39 -0700
Message-ID: <7aa77027-63c5-21df-181f-0852f9ceadf9@aklaver.com> (raw)
In-Reply-To: <7DEBDB22-4E50-4A78-99FE-AF8C03261665@teksol.info>
References: <7DEBDB22-4E50-4A78-99FE-AF8C03261665@teksol.info>
On 7/31/21 11:59 AM, François Beausoleil wrote:
> Hello all!
>
> I’m excited for multi ranges, as they fit nicely into a scheduling app. What I’m trying to express is something along the lines of « Every weekday from 2021-08-01 and 2021-10-01, from 9 AM to 8 PM, every 90 minutes ». You can think of public transit for the model.
>
> Initially, I was going to create a table with every departure recorded, and was going to refresh the table on every write to the parent table, but that means maintaining many rows for every change to the schedule. Then, I remembered multi ranges in PG14, and they fit nicely with what I had in mind.
>
> Now that I can store the info I want, I’d like to iterate over the ranges, so I can generate the exact departure times, something similar to this:
>
> SELECT instant
> FROM generate_series(
> '{["2021-08-02 08:00:00","2021-08-02 11:00:00"),["2021-08-03 08:00:00","2021-08-03 20:00:00"]’}
> , interval ’90 minutes’) as instant;
>
> 2021-08-02 08:00
> 2021-08-02 09:30
> — 2021-08-02 11:00 excluded as the range excludes its upper bound
> 2021-08-03 08:00:00
> 2021-08-03 09:30:00
> 2021-08-03 11:00:00
> 2021-08-03 12:30:00
> 2021-08-03 14:00:00
> 2021-08-03 15:30:00
> 2021-08-03 17:00:00
> 2021-08-03 18:30:00
> 2021-08-03 20:00:00 — included, as the upper bound is inclusive
>
> That function doesn’t exist, and I can’t seem to find a function to iterate over a multi range either. Does such a function exist? I’m specifically looking at https://www.postgresql.org/docs/14/functions-range.html.
>
> This is a toy application, a spike to see what’s possible.
>
> I wanted to avoid maintaining a table with hundreds of rows per route, if a route runs frequently enough (every 10 minutes, 8 AM to 8 PM, over a year). Of course, I can avoid refreshing the departures table if the schedule hasn’t changed, but still, preparing this table will not take a constant amount of time; e.g. it will depend on the schedule’s size.
>
> Any tips appreciated!
How about:
SELECT
*
FROM
generate_series ('2021-08-02 08:00:00'::timestamp, '2021-08-02
10:59:00'::timestamp, interval '90 minutes') AS instant
UNION
SELECT
*
FROM
generate_series ('2021-08-03 08:00:00'::timestamp, '2021-08-03
20:00:00'::timestamp, interval '90 minutes') AS instant
ORDER BY instant;
instant
---------------------
2021-08-02 08:00:00
2021-08-02 09:30:00
2021-08-03 08:00:00
2021-08-03 09:30:00
2021-08-03 11:00:00
2021-08-03 12:30:00
2021-08-03 14:00:00
2021-08-03 15:30:00
2021-08-03 17:00:00
2021-08-03 18:30:00
2021-08-03 20:00:00
> François
>
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
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: <7aa77027-63c5-21df-181f-0852f9ceadf9@aklaver.com>
References: <7DEBDB22-4E50-4A78-99FE-AF8C03261665@teksol.info> <7aa77027-63c5-21df-181f-0852f9ceadf9@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