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 1hXWtr-0004Om-10 for pgsql-bugs@arkaria.postgresql.org; Sun, 02 Jun 2019 20:16:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hXWtp-0001hh-Je for pgsql-bugs@arkaria.postgresql.org; Sun, 02 Jun 2019 20:16:13 +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 1hXWtp-0001gp-0s for pgsql-bugs@lists.postgresql.org; Sun, 02 Jun 2019 20:16:13 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hXWtm-0005nJ-B8 for pgsql-bugs@lists.postgresql.org; Sun, 02 Jun 2019 20:16:11 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x52KEHL1019462; Sun, 2 Jun 2019 16:14:17 -0400 From: Tom Lane To: Jeff Janes cc: Andrew Dunstan , Robert Vollmert , pgsql-bugs@lists.postgresql.org Subject: Re: inconsistent behaviour of json_to_record and friends with embedded json In-reply-to: References: Comments: In-reply-to Jeff Janes message dated "Sat, 01 Jun 2019 15:06:27 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <19440.1559506429.0@sss.pgh.pa.us> Date: Sun, 02 Jun 2019 16:14:17 -0400 Message-ID: <19461.1559506457@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <19440.1559506429.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Jeff Janes writes: > On Thu, May 30, 2019 at 10:41 AM Robert Vollmert wrote: >> 1. select * from json_to_record('{"out": "{\"key\": 1}"}') as (out json= ); >> Postgres 10/11: >> 1. gives >> ERROR: invalid input syntax for type json >> DETAIL: Token "key" is invalid. >> CONTEXT: JSON data, line 1: "{"key... > This is caused by commit: > commit cf35346e813e5a1373f308d397bb0a8f3f21d530 > Author: Andrew Dunstan > Date: Thu Apr 6 22:11:21 2017 -0400 > = > Make json_populate_record and friends operate recursively > As far as I can tell, this was not an intended change, so I agree it is > probably a bug. I agree. It looks to me like the problem is this over-optimistic assumption: /* * Add quotes around string value (should be already escaped) = if * converting to json/jsonb. */ No, it's *not* already escaped. Fixing the code to use escape_json() is a bit tedious, because for some reason that function wasn't designed to support non-null-terminated input, but with the attached patch we get what seems to me like sane behavior: regression=3D# select * from json_to_record('{"out": "{\"key\": 1}"}') as = (out json); out = ---------------- "{\"key\": 1}" (1 row) regression=3D# select * from json_to_record('{"out": {"key": 1}}') as (out= json); out = ------------ {"key": 1} (1 row) i.e. what you get is either a string or an object, the same as it was in the input (same for all combinations of json/jsonb in/out). Not terribly surprisingly, this patch changes no existing regression test cases. We should add some, but I've not done so here. Also, modulo this bug for the json-input case, this *is* an intentional behavioral change from 9.6, but it seems fairly poorly documented to me. Should we try to improve that? regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fix-populate-record-json-escaping-1.patch"; charset="us-ascii" Content-ID: <19440.1559506429.2@sss.pgh.pa.us> Content-Description: fix-populate-record-json-escaping-1.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jso= nfuncs.c index 28bdc72..9e7035c 100644 --- a/src/backend/utils/adt/jsonfuncs.c +++ b/src/backend/utils/adt/jsonfuncs.c @@ -2803,26 +2803,7 @@ populate_scalar(ScalarIOData *io, Oid typid, int32 = typmod, JsValue *jsv) = json =3D jsv->val.json.str; Assert(json); - - /* already done the hard work in the json case */ - if ((typid =3D=3D JSONOID || typid =3D=3D JSONBOID) && - jsv->val.json.type =3D=3D JSON_TOKEN_STRING) - { - /* - * Add quotes around string value (should be already escaped) if - * converting to json/jsonb. - */ - - if (len < 0) - len =3D strlen(json); - - str =3D palloc(len + sizeof(char) * 3); - str[0] =3D '"'; - memcpy(&str[1], json, len); - str[len + 1] =3D '"'; - str[len + 2] =3D '\0'; - } - else if (len >=3D 0) + if (len >=3D 0) { /* Need to copy non-null-terminated string */ str =3D palloc(len + 1 * sizeof(char)); @@ -2830,7 +2811,21 @@ populate_scalar(ScalarIOData *io, Oid typid, int32 = typmod, JsValue *jsv) str[len] =3D '\0'; } else - str =3D json; /* null-terminated string */ + str =3D json; /* string is already null-terminated */ + + /* If converting to json/jsonb, make string into valid JSON literal */ + if ((typid =3D=3D JSONOID || typid =3D=3D JSONBOID) && + jsv->val.json.type =3D=3D JSON_TOKEN_STRING) + { + StringInfoData buf; + + initStringInfo(&buf); + escape_json(&buf, str); + /* free temporary buffer */ + if (str !=3D json) + pfree(str); + str =3D buf.data; + } } else { ------- =_aaaaaaaaaa0--