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 1spOE1-0017Cr-7a for pgsql-general@arkaria.postgresql.org; Sat, 14 Sep 2024 08:33:51 +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 1spOE0-007h30-Ep for pgsql-general@arkaria.postgresql.org; Sat, 14 Sep 2024 08:33:48 +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 1spODz-007h2p-Eq for pgsql-general@lists.postgresql.org; Sat, 14 Sep 2024 08:33:48 +0000 Received: from mail-40136.proton.ch ([185.70.40.136]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1spODu-0015vC-Oc for pgsql-general@postgresql.org; Sat, 14 Sep 2024 08:33:46 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kortschak.io; s=protonmail; t=1726302819; x=1726562019; bh=qNhXKt55y6PGmLFARuIfmCXW8nAcA01LyOqE+8QckFQ=; 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=hiHFWfKyv/JRskNxJi7cgGiXSj3n6zBciTJHlU68h3zFFK2iL7GNYuKU80xtI8ag0 s75YDNgYReSWiVLsvb1MeZX0WgZvCHZnsKMOqZMFqd2Fnwq9PyeYljJVoJY7XDG3Re jtBsHfFJ0hUcjhxptKrTOWsgSZHJfvMv/R9MjRC9MVwhfPbtBoqBHfDSrBveeaIqHc 5OnCjulRxX8NhChyVsQsib6FKMfmfLhjS5NnDSZR5UIfMvwU60KxtW9ulK+ngtPu/7 mTLkU4GOpzqKlEoqMQ8SqzqTVZxQ6ha/6K6o3lEuvAmNztQUxm3nSE+m0211WOFVlh VJewvipc7Oa+w== Date: Sat, 14 Sep 2024 08:33:33 +0000 To: Dominique Devienne , Adrian Klaver From: Dan Kortschak Cc: Vincent Veyron , pgsql-general@postgresql.org Subject: Re: re-novice coming back to pgsql: porting an SQLite update statement to postgres Message-ID: <5c86256a4e83b1af73da22faab54889564e93129.camel@kortschak.io> In-Reply-To: <21078220628bcebca0395a52d42b558ed965bbc0.camel@kortschak.io> References: <1aea530e4c96109b91fdde53a29e38beb625dc77.camel@kortschak.io> <20240723221121.51f2b3872d0ebfc36a6fa8ff@wanadoo.fr> <2f11b2ba-3182-492a-ab46-23cfa5ec913c@aklaver.com> <735913e4c9601a2a7ad57d2253d74451c42ba899.camel@kortschak.io> <21078220628bcebca0395a52d42b558ed965bbc0.camel@kortschak.io> Feedback-ID: 18519778:user:proton X-Pm-Message-ID: 58402957f70d0247bccf526af7b24bce4971e139 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 On Wed, 2024-07-24 at 00:23 +0000, Dan Kortschak wrote: > On 7/23/24 13:11, Vincent Veyron wrote: > > On Mon, 15 Jul 2024 20:31:13 +0000 > >=20 > > This is the goto page for anything SQL : > > https://www.postgresql.org/docs/current/sql-commands.html > >=20 > > For DateTime types : > > https://www.postgresql.org/docs/current/datatype-datetime.html > >=20 > > For JSON types : > > https://www.postgresql.org/docs/current/datatype-json.html >=20 > Thanks, I will work through those. I've had a chance to attack this. The first part of the problem was that I could not figure out how to get the multiple statement transaction that I using in SQLite to work with PostgreSQL. The solution was to use the host language's Postres binding transaction functions and send the statements separately. The first part, to ensure the JSON array exist is solved with =09update =09=09events =09set=20 =09=09datastr =3D jsonb_set(datastr, '{amend}', '[]') =09where =09=09starttime < $3 and =09=09endtime > $2 and =09=09not datastr::jsonb ? 'amend' and =09=09bucketrow =3D ( =09=09=09select rowid from buckets where id =3D $1 =09=09); I'm still having difficulties with the second part which is to update the contents of the amend array in the JSON. So far I'm able to append the relevant details to the append array, but I'm unable to correctly select the corrects elements from the $6 argument, which is in the form [{"start":,"end":,"data":}, ...]. The first update statement gives me broadly what I want, but includes elements of the array that it shouldn't. =09update =09=09events =09set =09=09datastr =3D jsonb_set( =09=09=09datastr, =09=09=09'{amend}', =09=09=09datastr->'amend' || jsonb_build_object( =09=09=09=09'time', $2::TEXT, =09=09=09=09'msg', $3::TEXT, =09=09=09=09'replace', ( =09=09=09=09=09-- This select is for comparison with the code below. =09=09=09=09=09select * from jsonb($6::TEXT) =09=09=09=09) =09=09=09) =09=09) =09where =09=09starttime < $5 and =09=09endtime > $4 and =09=09bucketrow =3D ( =09=09=09select rowid from buckets where id =3D $1 =09=09); If I filter on the start and end time, I end up with no element coming through at all and the "replace" field ends up null. =09update =09=09events =09set =09=09datastr =3D jsonb_set( =09=09=09datastr, =09=09=09'{amend}', =09=09=09datastr->'amend' || jsonb_build_object( =09=09=09=09'time', $2::TEXT, =09=09=09=09'msg', $3::TEXT, =09=09=09=09'replace', ( =09=09=09=09=09select * =09=09=09=09=09from =09=09=09=09=09=09jsonb($6::TEXT) as replacement =09=09=09=09=09where =09=09=09=09=09=09(replacement->>'start')::TIMESTAMP WITH TIME ZONE < endti= me and =09=09=09=09=09=09(replacement->>'end')::TIMESTAMP WITH TIME ZONE > startti= me =09=09=09=09) =09=09=09) =09=09) =09where =09=09starttime < $5 and =09=09endtime > $4 and =09=09bucketrow =3D ( =09=09=09select rowid from buckets where id =3D $1 =09=09); Can anyone suggest what I might be missing? One thing that occurs to me is that due to the host language the timezone in starttime and endtime is the local timezone, while the timezone in the elements of the $6 argument are in UTC. I've tried forcing the timezones to match and this does not appear to be the issue. This can be seen in the case of output from the first, non-filtering update statement above: =09{ =09=09... =09=09"start": "2023-06-13T05:24:50+09:30", =09=09"end": "2023-06-13T05:24:55+09:30", =09=09"data": { =09=09=09... =09=09=09"amend": [ =09=09=09=09{ =09=09=09=09=09"endtime": "2023-06-13T05:24:55+09:30", =09=09=09=09=09"msg": "testing", =09=09=09=09=09"replace": [ =09=09=09=09=09=09{ =09=09=09=09=09=09=09"data": { =09=09=09=09=09=09=09=09... =09=09=09=09=09=09=09}, =09=09=09=09=09=09=09"end": "2023-06-12T19:54:51Z", =09=09=09=09=09=09=09"start": "2023-06-12T19:54:39Z" =09=09=09=09=09=09} =09=09=09=09=09], =09=09=09=09=09"starttime": "2023-06-13T05:24:50+09:30", =09=09=09=09=09... =09=09=09=09} =09=09=09] =09=09} =09}, (I'm hoping there's not a dumb logic error in the statement; it's equivalent works with SQLite, and mapped all to UTC we have starttime=3D2023-06-12T19:54:50Z endtime=3D2023-06-12T19:54:55Z start=3D202= 3- 06-12T19:54:39Z and end=3D2023-06-12T19:54:51Z which is an overlap).