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 1lzmIv-0004gZ-4N for pgsql-general@arkaria.postgresql.org; Sat, 03 Jul 2021 20:31:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lzmIt-0005uz-Pn for pgsql-general@arkaria.postgresql.org; Sat, 03 Jul 2021 20:31:55 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lzmIt-0005uq-4t for pgsql-general@lists.postgresql.org; Sat, 03 Jul 2021 20:31:55 +0000 Received: from srv01.teladesign.ie ([2001:41c9:1:41d::10]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lzmIo-0004EM-85 for pgsql-general@postgresql.org; Sat, 03 Jul 2021 20:31:54 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rodonnell.ie; s=exim; h=Content-Transfer-Encoding:Content-Type:In-Reply-To: MIME-Version:Date:Message-ID:From:References:To:Subject:Sender:Reply-To:Cc: Content-ID:Content-Description:Resent-Message-ID; bh=qSDQiIWJevrhDXCK58Jni42XPIO0Edmf7rCwsbhj/gE=; b=BDU+86zAeAcB2YLUFv9ZQ+mrK8 1fYqRwHGr4ckkhlA8K/xqB3YNyVrvalbuKNftaJoo9zzbnMOMEgJZyw8vTdzTEZFzVKOErd3J5mUT iOdJzTnHK76wjJgQK5rBvlMkphJlVbzimJ922rjQ/rLCirjEbrty73OS+QfXt4GB+NGc=; Received: from [93.107.231.214] (helo=[192.168.1.21]) by srv01.teladesign.ie with esmtpsa (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lzmIk-00054a-4W; Sat, 03 Jul 2021 21:31:46 +0100 Subject: Re: Overlapping timestamptz ranges with priority To: Adrian Klaver , 'PostgreSQL' References: <726445f7-ff72-9412-2e6d-2badcc58f17e@rodonnell.ie> <816a1158-01aa-ef6b-fe83-a38d7df24d54@aklaver.com> <410efc20-181a-5af6-1a87-20ee13fae14a@aklaver.com> From: Ray O'Donnell Message-ID: <97f844a6-d873-b670-a4e9-4bd3f8dfd286@rodonnell.ie> Date: Sat, 3 Jul 2021 21:31:45 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <410efc20-181a-5af6-1a87-20ee13fae14a@aklaver.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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