Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1srtz8-005HWe-FH for pgsql-general@arkaria.postgresql.org; Sat, 21 Sep 2024 06:52:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1srtz5-00ET35-S0 for pgsql-general@arkaria.postgresql.org; Sat, 21 Sep 2024 06:52:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1srtz5-00ET2s-B5 for pgsql-general@lists.postgresql.org; Sat, 21 Sep 2024 06:52:48 +0000 Received: from mail-4317.proton.ch ([185.70.43.17]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1srtz3-000IOS-F4 for pgsql-general@lists.postgresql.org; Sat, 21 Sep 2024 06:52:47 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kortschak.io; s=protonmail; t=1726901561; x=1727160761; bh=3u7bYPHsa9Q8H+Gin0/vx/YP/xWcAGIB9WniS6levNg=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=vQVzu5VcnLpXqLCeH9+FHMJ9Jw4oyPEeh9zztBBS9a5DhrB9Cs4IQ1c1ZKCRjDHHF kqTfrW8IX0gnu0gNWyp0zfj0M8EmcX/Px+9VeM+CpiVFWQ0phwWKC2B3yw/Dq30NOL qxvWsHMobm2wBn6+IisfYAmWjlwje2lLwW1Xhuixl+SfmWzFo8WqZK10R2aIy/oCe7 fwAg5UaRYE838O6n3XtbrukOjRKFBmeudGUjJNNGVsYrlyR0yBdIwbfDSod2Fkduj8 9hk1Nu65vDhg+eXkf7kgWEeliUcpPMLz6BTouDTnn89bb9hoaZ63L/cF0yMdHyXenh eGOyKrGC2J6uA== Date: Sat, 21 Sep 2024 06:52:36 +0000 To: Willow Chargin , Alban Hertroys From: Dan Kortschak Cc: pgsql-general@lists.postgresql.org Subject: Re: re-novice coming back to pgsql: porting an SQLite update statement to postgres Message-ID: In-Reply-To: <8772eb2d72658c21cce576edc1cc26cd7962c895.camel@kortschak.io> References: <1aea530e4c96109b91fdde53a29e38beb625dc77.camel@kortschak.io> <735913e4c9601a2a7ad57d2253d74451c42ba899.camel@kortschak.io> <21078220628bcebca0395a52d42b558ed965bbc0.camel@kortschak.io> <5c86256a4e83b1af73da22faab54889564e93129.camel@kortschak.io> <7320B905-E950-4035-9A13-8C9447AB52EC@gmail.com> <48019d21fd7c669afb83161337f7e007ae619534.camel@kortschak.io> <93c6b90cd39ae5fdf49cbd698e8de0c6b6f9bf3f.camel@kortschak.io> <90CC4567-4C9E-4F83-902C-A3CE5C3C74E5@gmail.com> <8772eb2d72658c21cce576edc1cc26cd7962c895.camel@kortschak.io> Feedback-ID: 18519778:user:proton X-Pm-Message-ID: 8f33a72dda0ca2c961e3cffdf50cd29cf5e5006c MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is the toy with the shape of data that will be seen in the application. The final trick was to use to_jsonb to allow the timestamptz to be put back into the jsonb. WITH replace AS ( SELECT jsonb($$[ {"start": "2023-06-12T19:54:39.248859996+10:00", "end": "2023-06-12= T19:54:59.248859996+10:00", "data": {"item":1}}, {"start": "2023-06-12T19:54:50.248859996+10:00", "end": "2023-06-12= T19:59:39.248859996+10:00", "data": {"item":2}}, {"start": "2023-06-12T19:56:39.248859996+10:00", "end": "2023-06-12= T19:57:39.248859996+10:00", "data": {"item":3}}, {"start": "2023-06-12T19:54:39.248859996+10:00", "end": "2023-06-12= T20:54:49.248859996+10:00", "data": {"item":4}}, {"start": "2024-06-12T19:54:39.248859996+10:00", "end": "2024-06-12= T19:59:39.248859996+10:00", "data": {"item":5}} ]$$) replacements ) SELECT jsonb_agg(new ORDER BY idx) trimmed_replacements FROM replace, LATERAL ( SELECT idx, jsonb_object_agg(key, CASE WHEN key =3D 'start' THEN to_jsonb(greatest(old::text::TIMESTAMPTZ, '2023-06= -12T19:54:50+10:00'::TIMESTAMPTZ)) WHEN key =3D 'end' THEN to_jsonb(least(old::text::TIMESTAMPTZ, '2023-06-12= T19:58:00+10:00'::TIMESTAMPTZ)) ELSE old END=20 ) FROM jsonb_array_elements(replacements) WITH ORDINALITY rs(r, idx), jsonb_each(r) each(key, old) WHERE (r->>'start')::TIMESTAMPTZ < '2023-06-12T19:58:00+10:00'::TIMES= TAMPTZ and=20 (r->>'end')::TIMESTAMPTZ > '2023-06-12T19:54:50+10:00'::TIMESTA= MPTZ GROUP BY idx ) news(idx, new);