pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ray O'Donnell <ray@rodonnell.ie>
To: Adrian Klaver <adrian.klaver@aklaver.com>
To: PostgreSQL <pgsql-general@postgresql.org>
Subject: Re: Overlapping timestamptz ranges with priority
Date: Sat, 3 Jul 2021 21:31:45 +0100
Message-ID: <97f844a6-d873-b670-a4e9-4bd3f8dfd286@rodonnell.ie> (raw)
In-Reply-To: <410efc20-181a-5af6-1a87-20ee13fae14a@aklaver.com>
References: <726445f7-ff72-9412-2e6d-2badcc58f17e@rodonnell.ie>
	<b284ebd4-8fd4-a1e7-22e8-adab855e3c7c@rodonnell.ie>
	<816a1158-01aa-ef6b-fe83-a38d7df24d54@aklaver.com>
	<d8acc5f0-e246-a49a-3701-249421bd1f19@rodonnell.ie>
	<410efc20-181a-5af6-1a87-20ee13fae14a@aklaver.com>

On 03/07/2021 21:13, Adrian Klaver wrote:
> On 7/3/21 12:16 PM, Ray O'Donnell wrote:
>> Yes, you're right - I realised that after I sent my last email. The 
>> inner loop in the function should have matched overlapping bookings by 
>> aircraft registration:
>>
>>      -- For each booking, check whether there are any with
>>      -- a higher priority and whose times overlap it.
>>      for m_overlapping in
>>        select booking_id, booking_time from bookings
>>        where booking_id < m_rec.booking_id
>>        and booking_time && m_rec.booking_time
>>      loop
>>        -- Snip away any overlapping (obscured) time.
>>        m_visible_time := m_visible_time - m_overlapping.booking_time;
>>      end loop;
> 
> Was the above supposed to show the change?

Whoops, sorry, here it is:

     for m_overlapping_time in
         select booking_id, booking_time from bookings
         where aircraft_reg = m_rec.aircraft_reg
           and booking_id < m_rec.booking_id
           and booking_time && m_rec.booking_time
     loop
         [... etc ...]

>> select booking_id, aircraft_reg, booking_time from bookings order by 
>> aircraft_reg, lower(booking_time);
>>
> 
> Pretty sure lower() is not needed, if I'm following this correctly:
> 
> https://www.postgresql.org/docs/12/functions-range.html
> 
> "The simple comparison operators <, >, <=, and >= compare the lower 
> bounds first, and only if those are equal, compare the upper bounds. 
> These comparisons are not usually very useful for ranges, but are 
> provided to allow B-tree indexes to be constructed on ranges."

Ah, good - thanks for pointing that out.

> In the case where the lower bound  is the same I'm thinking using 
> lower() will result in different ordering under different circumstances:

I see what you mean. It shouldn't matter for our use case; ordering on 
the aircraft registration and time is what counts for us, and the output 
of the function ought to produce well-ordered booking times for each 
aircraft. The other columns are used for display purposes only.

>> I need to play with it a bit more: for example, if a long, 
>> lower-priority booking is behind a short, higher-priority one such 
>> that the long one extends both before and after the short one, then 
>> the range-difference operator will give me an error about a 
>> non-contiguous result. However, I think I'm heading in the right 
>> direction now.
> 
> Great. Good luck going forward.

Thanks again for your help - much appreciated!

Ray.


-- 
Raymond O'Donnell // Galway // Ireland
ray@rodonnell.ie





view thread (15+ messages)

Message-ID: <97f844a6-d873-b670-a4e9-4bd3f8dfd286@rodonnell.ie>
Permalink:  ../97f844a6-d873-b670-a4e9-4bd3f8dfd286@rodonnell.ie/
Also on:    postgresql.org/message-id/97f844a6-d873-b670-a4e9-4bd3f8dfd286@rodonnell.ie

 · 

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: ray@rodonnell.ie, adrian.klaver@aklaver.com
  Subject: Re: Overlapping timestamptz ranges with priority
  In-Reply-To: <97f844a6-d873-b670-a4e9-4bd3f8dfd286@rodonnell.ie>
  References: <726445f7-ff72-9412-2e6d-2badcc58f17e@rodonnell.ie> <b284ebd4-8fd4-a1e7-22e8-adab855e3c7c@rodonnell.ie> <816a1158-01aa-ef6b-fe83-a38d7df24d54@aklaver.com> <d8acc5f0-e246-a49a-3701-249421bd1f19@rodonnell.ie> <410efc20-181a-5af6-1a87-20ee13fae14a@aklaver.com> <97f844a6-d873-b670-a4e9-4bd3f8dfd286@rodonnell.ie>

* 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