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 1spPgN-001IV1-A2 for pgsql-general@arkaria.postgresql.org; Sat, 14 Sep 2024 10:07:12 +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 1spPfN-009ISh-Cu for pgsql-general@arkaria.postgresql.org; Sat, 14 Sep 2024 10:06:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1spPfN-009IRE-1T for pgsql-general@lists.postgresql.org; Sat, 14 Sep 2024 10:06:09 +0000 Received: from mail-ed1-x52a.google.com ([2a00:1450:4864:20::52a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1spPfG-0018Qe-Mj for pgsql-general@postgresql.org; Sat, 14 Sep 2024 10:06:08 +0000 Received: by mail-ed1-x52a.google.com with SMTP id 4fb4d7f45d1cf-5c26a701185so413897a12.1 for ; Sat, 14 Sep 2024 03:06:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726308362; x=1726913162; darn=postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=5KI0RikbTVCrLjMQL4t7iMOPJijENtc49Wz6MZWWPZQ=; b=OXwkfuiNAw4IRmNXW8Pue/W1Oui6jZcGB42HToonkNCUOaMGAXOyLGwlwRibXihlL5 BAEtz+zstO4gmj4kEDUP9KbMjapzf5Nbr1ywHaqOjH3wReEN0RHipNNiMD7idBj2X5wV DBXsBFQ3t57q00ki4c7YYdcXCzx0Bm+UU8a4AtRRdp8COeKwiSgHbyIE0IU2p5iiT2Wn pWBXCUKA1+ZjgNMjfepfG9aqcrTMDy/kOCbNvS1JA5DUJiF8sP1coyCAUwRTVpJMMQ7B L1OU1s7jh02PcSWa+pFPbuQTQQ5PxUQlGr91zyYGonF9s5o33md5xvjvdBm9P+FpO8bp q1TA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726308362; x=1726913162; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5KI0RikbTVCrLjMQL4t7iMOPJijENtc49Wz6MZWWPZQ=; b=F04IOKEdmeMTKT00L5FWyfVNJ5gsSV0KwdAICyF38kQwXcVbWSFBF247yWpbAcd2qU x/FHfOEa3ve8xaqoPQlIRiRSA5FthyR0dVPGrjWnhaG5CR3BFhJaP28AcmplicCcNTZw rS1Wm+/5cd3MhyRiAYRSrGI3Jsf8HDlg5/dEs60//EVNRqv/RyQG7ztVRE7IVunXkaHE H1D+EQooOCWedWHGmXqzj5Hf5H1TkMAYUu2CxCP8oDbW9FP/RJXkWK1fdZERncihNlTa 2njMsZ5q98R42byuXwL6F5i5seBLUtqG6w+xV+nUWe/G+om0ABvcb2XJFje/WSyJqrGu ti2g== X-Forwarded-Encrypted: i=1; AJvYcCU6ujgc2QxKk6gZzzOu3kNxW+pxO2glYHKDWcbZ3X3SvGz7gZG8p9rQJdw78BliVA7sJfyEBtHhTAvEAVTy@postgresql.org X-Gm-Message-State: AOJu0Yz9/N00j/Vly9+Ca/Sh01oyUmDGK/iRQAhDnd5bRc6ciPsEZ9AT PlCwgEpLm3K/WNaXc8650aAol16tUhTbJg74xvbA96Bi5eN6iyEx X-Google-Smtp-Source: AGHT+IE+kSPXaBHYAbdgMMUUURpNj1hVa+mbii7EFZbBe6iTkkFh4fGJrJeipvKDAEsgYvjHtPcQew== X-Received: by 2002:a17:906:dc92:b0:a8d:2de6:cb59 with SMTP id a640c23a62f3a-a902940dfb8mr381800766b.3.1726308361201; Sat, 14 Sep 2024 03:06:01 -0700 (PDT) Received: from smtpclient.apple ([188.212.112.125]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5c42bb4971fsm508976a12.13.2024.09.14.03.06.00 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 14 Sep 2024 03:06:00 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3776.700.51\)) Subject: Re: re-novice coming back to pgsql: porting an SQLite update statement to postgres From: Alban Hertroys In-Reply-To: <5c86256a4e83b1af73da22faab54889564e93129.camel@kortschak.io> Date: Sat, 14 Sep 2024 12:05:50 +0200 Cc: Dominique Devienne , Adrian Klaver , Vincent Veyron , PostgreSQL General Content-Transfer-Encoding: quoted-printable Message-Id: <7320B905-E950-4035-9A13-8C9447AB52EC@gmail.com> References: <1aea530e4c96109b91fdde53a29e38beb625dc77.camel@kortschak.io> <20240723221121.51f2b3872d0ebfc36a6fa8ff@wanadoo.fr> <2f11b2ba-3182-492a-ab46-23cfa5ec913c@aklaver.com> <735913e4c9601a2a7ad57d2253d74451c42ba899.camel@kortschak.io> <21078220628bcebca0395a52d42b558ed965bbc0.camel@kortschak.io> <5c86256a4e83b1af73da22faab54889564e93129.camel@kortschak.io> To: Dan Kortschak X-Mailer: Apple Mail (2.3776.700.51) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 14 Sep 2024, at 10:33, Dan Kortschak = wrote: (=E2=80=A6) > I'm still having difficulties with the second part which is to update > the contents of the amend array in the JSON. >=20 > 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. (=E2=80=A6) > 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. >=20 > update > events > set > datastr =3D jsonb_set( > datastr, > '{amend}', > datastr->'amend' || jsonb_build_object( > 'time', $2::TEXT, > 'msg', $3::TEXT, > 'replace', ( > select * > from > jsonb($6::TEXT) as replacement > where > (replacement->>'start')::TIMESTAMP WITH TIME ZONE < endtime and > (replacement->>'end')::TIMESTAMP WITH TIME ZONE > starttime > ) > ) > ) > where > starttime < $5 and > endtime > $4 and > bucketrow =3D ( > select rowid from buckets where id =3D $1 > ); That=E2=80=99s because the replacement data is an array of objects, not = a single object. You need to iterate through the array elements to build your replacement = data, something like what I do here with a select (because that=E2=80=99s = way easier to play around with): with dollar6 as ( select jsonb($$[ { "data": { "foo": = 1, "bar": 2 }, "end": = "2023-06-12T19:54:51Z", "start": = "2023-06-12T19:54:39Z" } ]$$::text) replacement ) select * =20 from dollar6 cross join lateral jsonb_array_elements(replacement) r where (r->>'start')::timestamptz <=3D current_timestamp; There are probably other ways to attack this problem, this is the one I = came up with. Alban Hertroys -- If you can't see the forest for the trees, cut the trees and you'll find there is no forest.