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 1iLrRW-00043b-70 for pgsql-general@arkaria.postgresql.org; Sat, 19 Oct 2019 16:19:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iLrRU-00025M-0c for pgsql-general@arkaria.postgresql.org; Sat, 19 Oct 2019 16:19:00 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iLrRT-00024E-JJ for pgsql-general@lists.postgresql.org; Sat, 19 Oct 2019 16:18:59 +0000 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iLrRQ-00055k-6C for pgsql-general@lists.postgresql.org; Sat, 19 Oct 2019 16:18:57 +0000 Received: by mail-wr1-x442.google.com with SMTP id l10so8884244wrb.2 for ; Sat, 19 Oct 2019 09:18:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=wBcyIhMac4WyldA+YIbFbeWLrITN4gLkdNIbj+l6JNc=; b=h6EWADki37kD+5q+7JmEilLSbqC+1Sxuxclvur/YfBaRnrc0rROiQ8ozrfnSailvqk aSCe/FP1JMTXEZtqB/4fEHaNE/97o5G/aPz+LgQrLH/f4tn3p0duAE4pTJtGcY07IuXZ ZiQE2gadGPcATu2FYLFfX/OLjmxJ69qC0Mr66ndwTykN+qPNe6cyktf/O2BvrPF9AQYr TwXRw+7CWaJVEaxHpvVApqPvHE2Ga5b4+//NIXhhuJluv+YvcEZIi4j6t5zZIVhFPUDI ysIq3OWqdRi5nJANZNkA7Bj6YrAGr3Fu8GnGaTRXVYkHsW2PZbtSvKuO8S7QSiyNFvD6 3Iwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=wBcyIhMac4WyldA+YIbFbeWLrITN4gLkdNIbj+l6JNc=; b=Tp2QOaXRmqj3yFVPhlPJNtwL4IiO0Zk13ESaPXCQB70RiAVbP2DpGGFqTLNi2reaHK 1Z5kAVqNTRRR+VKHXb0pjQPA5Eky2nJ63//Rr6Q8oCa1ZitUwPRFT47TMcTqZwkqrtmS 35LbSAOWKu8d1V3bpmeStZGZeXT0LRuP/QpBn0eWJ5B7ibOx6sT2PokVR9qRTvRKU4ba JfupY/+NDTYA4zz3X6oV8a5g7vhBC4Fzqi2xacpyUL+hCMG3jo7vuMOqHNNfcPRkXOhc ksYpnLEx4ShAeR1NpB+zdSNRoeItkcgwuR0tQfo0mOwE8bwCPfHVHw4+Ne0bKFm7FfTD qNvA== X-Gm-Message-State: APjAAAWmPY9Wdgp7vsaKmDqOGlzQsWJokb/Xywwfb+KuYOvNQUBeeMnp vpckW3MPyIQAmmiaC4IYvD7LCA== X-Google-Smtp-Source: APXvYqxRELYldlmb5V/zrSHsNInHHyDR04NNvU7TSX0Jq1uaEib8HTpOyfOfM0q0fV5kpJhg5uLQ3Q== X-Received: by 2002:adf:da42:: with SMTP id r2mr11900191wrl.383.1571501933861; Sat, 19 Oct 2019 09:18:53 -0700 (PDT) Received: from localhost (ip-86-49-253-183.net.upcbroadband.cz. [86.49.253.183]) by smtp.gmail.com with ESMTPSA id r65sm9020939wmr.9.2019.10.19.09.18.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Oct 2019 09:18:53 -0700 (PDT) Date: Sat, 19 Oct 2019 18:18:51 +0200 From: Tomas Vondra To: Andrew Dunstan Cc: Mark Felder , pgsql-general@lists.postgresql.org, pgsql-hackers@lists.postgresql.org Subject: Re: jsonb_set() strictness considered harmful to data Message-ID: <20191019161851.v7dwloiiav5yrpwu@development> References: <7dc242bd-b78f-2e0d-0c38-7b4b299f11bf@2ndQuadrant.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <7dc242bd-b78f-2e0d-0c38-7b4b299f11bf@2ndQuadrant.com> User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sat, Oct 19, 2019 at 11:26:50AM -0400, Andrew Dunstan wrote: > > ... > >The hyperbole here is misplaced. There is a difference between a bug and >a POLA violation. This might be the latter, but it isn't the former. So >please tone it down a bit. It's not the function that's unsafe, but the >ill-informed use of it. > > >We invented jsonb_set() (credit to Dmitry Dolgov). And we've had it >since 9.5. That's five releases ago.  So it's a bit late to be coming to >us telling us it's not safe (according to your preconceptions of what it >should be doing). > > >We could change it prospectively (i.e. from release 13 on) if we choose. >But absent an actual bug (i.e. acting contrary to documented behaviour) >we do not normally backpatch such changes, especially when there is a >simple workaround for the perceived problem. And it's that policy that >is in large measure responsible for Postgres' deserved reputation for >stability. > Yeah. > >Incidentally, why is your function written in plpgsql? Wouldn't a simple >SQL wrapper be better? > > > create or replace function safe_jsonb_set >     (target jsonb, path text[], new_value jsonb, create_missing > boolean default true) > returns jsonb as > $func$ >     select case when new_value is null then target else > jsonb_set(target, path, new_value, create_missing) end > $func$ language sql; > > >And if we were to change it I'm not at all sure that we should do it the >way that's suggested here, which strikes me as no more intuitive than >the current behaviour. Rather I think we should possibly fill in a json >null in the indicated place. > Not sure, but that seems rather confusing to me, because it's mixing SQL NULL and JSON null, i.e. it's not clear to me why jsonb_set(..., "...", NULL) should do the same thing as jsonb_set(..., "...", 'null':jsonb) I'm not entirely surprised it's what MySQL does ;-) but I'd say treating it as a deletion of the key (just like MSSQL) is somewhat more sensible. But I admit it's quite subjective. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services