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 1m9uzY-0000L4-2d for pgsql-general@arkaria.postgresql.org; Sat, 31 Jul 2021 19:49:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m9uzW-0006M6-OF for pgsql-general@arkaria.postgresql.org; Sat, 31 Jul 2021 19:49:50 +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 1m9uzV-0006Ly-KQ for pgsql-general@lists.postgresql.org; Sat, 31 Jul 2021 19:49:50 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m9uzP-00031O-5A for pgsql-general@lists.postgresql.org; Sat, 31 Jul 2021 19:49:48 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id A505A5C0125; Sat, 31 Jul 2021 15:49:41 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Sat, 31 Jul 2021 15:49:41 -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=N ZL/9j3Xaq1eqIV0QUunaFKlKuqVkIRyAFIWd0manGY=; b=L2IK8N8qIRLX3xzrC OPnJ9/+rAa2jCgFBH7AojfXJO4O0osMzfCsGhOfuzBpvqgzYwpwbq1AjTzc1FGAv Z4txpuepatskF7ZjLgycj8Ufyb6/YZ/yf5DiLi+apyE7ItlCbxjvRqQa6hVanj9v wRng4ZeQD/VO9+oqjZpLthDgpAzRSbehA2ZZMRRcSs0y9yKcUjBGGj3JW/abh+HP cdyGbkm+8w3OGBnBukNuOPBWjGKqZoHIBin94Ev5uKSWYOb5m3D6+P1vUnu9wYr4 OjS9r3XwCMwR1f/dVzehgZJKHS4aKSPuuoIiLTrZaEDWDZVAyXLz9HhJ+6ydvVNo ItaLA== 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=NZL/9j3Xaq1eqIV0QUunaFKlKuqVkIRyAFIWd0man GY=; b=tpYFqmZaYEYO0IVhg2DDnB82l2ve/nwxAfsRPfYKP+0D16WweQOtR1UnH GhxnZ3vKJ05Vn9Mc1KL5oFjNtQqeB+J3EJJt1nbLOLd5+2WxjC5LzseU5A4xzFur fCGRlWMkfBClm7owM2pDjJN44uhN1WyrgqAIIgZZhuI7f2yEs4oAhpHr25jvqD4m E6o3N0IcKxBxuHWVytLertK0PkETIQHzrWTkR0JX8S/DbgDpci3/oXIQjJoIJqpY lRH7r/hROELLUvTu2lTInl0APLevv444rs2m6EyMYeOiZhtJaLoc3C19xTndDQQd YFOdJdgtwVHxH+pgjXBgCsABHIDEg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrheejgdduvddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpeduudefjeetveffhefgudehtdfhvdetkeeiueehfeev veduteekleeuheegudehhfenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgne cuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhi rghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 31 Jul 2021 15:49:40 -0400 (EDT) Subject: Re: Help with writing a generate_series(tsmultirange, interval) To: =?UTF-8?Q?Fran=c3=a7ois_Beausoleil?= , pgsql-general@lists.postgresql.org References: <7DEBDB22-4E50-4A78-99FE-AF8C03261665@teksol.info> From: Adrian Klaver Message-ID: <7aa77027-63c5-21df-181f-0852f9ceadf9@aklaver.com> Date: Sat, 31 Jul 2021 12:49:39 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: <7DEBDB22-4E50-4A78-99FE-AF8C03261665@teksol.info> 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/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