Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iLa8m-0005y5-BK for pgsql-general@arkaria.postgresql.org; Fri, 18 Oct 2019 21:50:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iLa8i-0000G5-Q6 for pgsql-general@arkaria.postgresql.org; Fri, 18 Oct 2019 21:50:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iLa8i-0000Fd-Eu for pgsql-general@lists.postgresql.org; Fri, 18 Oct 2019 21:50:28 +0000 Received: from smtp.burggraben.net ([2a01:4f8:140:510a::3]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iLa8b-0000w5-Bn for pgsql-general@lists.postgresql.org; Fri, 18 Oct 2019 21:50:27 +0000 Received: from elch.exwg.net (elch.exwg.net [IPv6:2001:470:7120:1:127b:44ff:fe4f:148d]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "elch.exwg.net", Issuer "Christoph Moench-Tegeder" (verified OK)) by smtp.burggraben.net (Postfix) with ESMTPS id 897A1C0030E; Fri, 18 Oct 2019 23:50:18 +0200 (CEST) Received: by elch.exwg.net (Postfix, from userid 1000) id 3CF801399FD; Fri, 18 Oct 2019 23:50:18 +0200 (CEST) Date: Fri, 18 Oct 2019 23:50:18 +0200 From: Christoph Moench-Tegeder To: Ariadne Conill Cc: pgsql-general@lists.postgresql.org, feld@freebsd.org Subject: Re: jsonb_set() strictness considered harmful to data Message-ID: <20191018215018.GB56427@elch.exwg.net> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.12.2 (2019-09-21) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ## Ariadne Conill (ariadne@dereferenced.org): > update users set info=jsonb_set(info, '{bar}', info->'foo'); > > Typically, this works nicely, except for cases where evaluating > info->'foo' results in an SQL null being returned. When that happens, > jsonb_set() returns an SQL null, which then results in data loss.[3] So why don't you use the facilities of SQL to make sure to only touch the rows which match the prerequisites? UPDATE users SET info = jsonb_set(info, '{bar}', info->'foo') WHERE info->'foo' IS NOT NULL; No special wrappers required. Regards, Christoph -- Spare Space