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 1lzjv6-0000Go-QF for pgsql-general@arkaria.postgresql.org; Sat, 03 Jul 2021 17:59:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lzjv5-0003FJ-9z for pgsql-general@arkaria.postgresql.org; Sat, 03 Jul 2021 17:59:11 +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 1lzjv3-0003F7-LS for pgsql-general@lists.postgresql.org; Sat, 03 Jul 2021 17:59:11 +0000 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lzjv0-00037K-Un for pgsql-general@postgresql.org; Sat, 03 Jul 2021 17:59:08 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.west.internal (Postfix) with ESMTP id BA933320091C; Sat, 3 Jul 2021 13:59:04 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Sat, 03 Jul 2021 13:59:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=7 tAWtmKJ0tPomTZgq/54TYb5NSEy94/ASubrKbs/mMU=; b=EKwrMUEqTEewPZNRu 5htclNsOLllWZX4F/6wVj1tjRB90XTjf0hfNuCnsvCHYX1I2M1ImUcLLeMNzx569 WkuEjknyVAQ2ChBx/YmgGr/Swerqai25yYhFygY2yYvabigMT9HlZVajjDs/pqlO 15zRT10NiWoWBHeVgWNYJ0bjkEKA62uBqjwnQrtQ3WnA90cdsxnhg/ZopeUq75ev Y1Itt+5Tb3aVPWy6nQR7O6aCr6bKfFduAtF2rqBtbPERLgWBf5jMYtoSrMg/iwul QxIg5yG4zEXWoqJ1ojii0HWuPPBll648nq0Zw5H5puq3fUmUVE+D1aEn5kNdAfhO I35TA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=7tAWtmKJ0tPomTZgq/54TYb5NSEy94/ASubrKbs/m MU=; b=eb5Td5LbzyRLefHcmVRN9WR8tiJdu8aAeLqik67SgrAUHgI4qY257Tr38 ySrKbA8xdO91wImw3pVlSo3NyRbKeguSe89oPlAOaregROhraXKDJNGOSgWM8nlT GwkvDO1vU9zzNDrRME8HqyeTxmjrY/1MIkRS03dR4I2UtB/IgrbmvO/jGZ6ntzvt XbXlnRU6H0V1BOOnpCx3oZRYRHEUyXy0i4iSCtBDSgb0YbLd6AFUQ9sfkPtIOsxM aPrvmCp6QMmctmmnjXzlHA1iYJL3c30ZE4FCSFxUv/KGTk4OWApq8SzpGDUkMPck DWsYMFv/cOWJqvXtDOHUSBsQIiZmw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrfeejtddguddujecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesth ekredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeevveefve efueegffekkefhgeeiueffgfetueejvdfghfeujeejhfekjeelgedtgeenucevlhhushht vghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrg hvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 3 Jul 2021 13:59:03 -0400 (EDT) Subject: Re: Overlapping timestamptz ranges with priority To: Ray O'Donnell , 'PostgreSQL' References: <726445f7-ff72-9412-2e6d-2badcc58f17e@rodonnell.ie> From: Adrian Klaver Message-ID: <816a1158-01aa-ef6b-fe83-a38d7df24d54@aklaver.com> Date: Sat, 3 Jul 2021 10:59:02 -0700 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: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 7/3/21 9:32 AM, Ray O'Donnell wrote: > On 27/06/2021 23:41, Ray O'Donnell wrote: >> Hi all, >> >> I'm playing with timestamptz ranges for a hobby project. I have a >> table with a tstzrange column, in which the timestamps can overlap; >> where they do, rows with a higher priority (derived from a bigint >> primary key column) should be picked. >> >> What I'd like to do is present a view which shows timestamp ranges at >>  the front of the queue, as it were; where ranges overlap, these may >> be segments of a range from a particular row. I'm having trouble with >> this and would appreciate suggestions. > > I've come up with a way of doing it using a function... it's not going > to be very efficient if the number of rows gets large, due to nested > loops, but as the system generally keeps only a limited number of > bookings (no more that a few hundred), I think it'll do - certainly as a > first run at it. > > Firstly, the table structure (as it now stands) on which the function > will operate: > > CREATE TABLE bookings > ( >     booking_id bigint NOT NULL GENERATED ALWAYS AS IDENTITY, >     aircraft_reg text NOT NULL, >     type_code text NOT NULL, >     booking_time tstzrange NOT NULL, >     owner_uid text NOT NULL, >     owner_name text NOT NULL, > >     CONSTRAINT bookings_pk PRIMARY KEY (booking_id), > >     (... foregin keys etc...) > ); > > > And here's the function: > > create or replace function get_visible_bookings() > returns setof bookings > language plpgsql > as > $$ > declare >   m_rec bookings; >   m_overlapping record; >   m_visible_time tstzrange; > begin >   -- Loop through all bookings on the system, ordered on booking ID. >   -- The booking ID also give the queue priority of the booking: >   -- bookings with a lower ID have a higher priority. >   for m_rec in >     select * from bookings order by booking_id >   loop >     m_visible_time := m_rec.booking_time; > >     -- 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; > >     -- If any of the current booking's time is still visible, >     -- then return the row with what's left of the time. >     if not isempty(m_visible_time) then >     return next row(m_rec.booking_id, m_rec.aircraft_reg, >           m_rec.type_code, m_visible_time, >           m_rec.owner_uid, m_rec.owner_name); >     end if; >   end loop; > >   return; > end; > $$; I'm not sure this is doing what you think it is; select * from bookings order by booking_id; booking_id | aircraft_reg | type_code | booking_time | owner_uid | owner_name ------------+--------------+-----------+-----------------------------------------------------+-----------+----- 1 | A1ZX | type1 | ["2021-07-03 10:00:00-07","2021-07-03 14:00:00-07"] | 1 | aklaver 2 | A1ZX | type1 | ["2021-07-03 12:00:00-07","2021-07-03 16:00:00-07"] | 1 | aklaver 3 | A1ZX | type1 | ["2021-07-04 09:00:00-07","2021-07-04 12:00:00-07"] | 1 | aklaver 4 | B2CA | type2 | ["2021-07-03 09:00:00-07","2021-07-03 12:00:00-07"] | 2 | wilbur 5 | B2CA | type2 | ["2021-07-03 11:00:00-07","2021-07-03 14:00:00-07"] | 2 | wilbur select * from get_visible_bookings(); booking_id | aircraft_reg | type_code | booking_time | owner_uid | owner_name ------------+--------------+-----------+-----------------------------------------------------+-----------+------------ 1 | A1ZX | type1 | ["2021-07-03 10:00:00-07","2021-07-03 14:00:00-07"] | 1 | aklaver 2 | A1ZX | type1 | ("2021-07-03 14:00:00-07","2021-07-03 16:00:00-07"] | 1 | aklaver 3 | A1ZX | type1 | ["2021-07-04 09:00:00-07","2021-07-04 12:00:00-07"] | 1 | aklaver 4 | B2CA | type2 | ["2021-07-03 09:00:00-07","2021-07-03 10:00:00-07") | 2 | wilbur (4 rows) The booking_id for aircraft B2CA with booking_time of ["2021-07-03 11:00:00-07","2021-07-03 14:00:00-07"] is not accounted for. There is a step missing that accounts for bookings being assigned to a particular aircraft. > > > -- Adrian Klaver adrian.klaver@aklaver.com