pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Restore json{b}_populate_record{set}'s ability to take type info
Date: Mon, 19 Aug 2019 22:01:18 +0000
Message-ID: <E1hzpiI-00041s-9D@gemulon.postgresql.org> (raw)

Restore json{b}_populate_record{set}'s ability to take type info from AS.

If the record argument is NULL and has no declared type more concrete
than RECORD, we can't extract useful information about the desired
rowtype from it.  In this case, see if we're in FROM with an AS clause,
and if so extract the needed rowtype info from AS.

It worked like this before v11, but commit 37a795a60 removed the
behavior, reasoning that it was undocumented, inefficient, and utterly
not self-consistent.  If you want to take type info from an AS clause,
you should be using the json_to_record() family of functions not the
json_populate_record() family.  Also, it was already the case that
the "populate" functions would fail for a null-valued RECORD input
(with an unfriendly "record type has not been registered" error)
when there wasn't an AS clause at hand, and it wasn't obvious that
that behavior wasn't OK when there was one.  However, it emerges
that some people were depending on this to work, and indeed the
rather off-point error message you got if you left off AS encouraged
slapping on AS without switching to the json_to_record() family.

Hence, put back the fallback behavior of looking for AS.  While at it,
improve the run-time error you get when there's no place to obtain type
info; we can do a lot better than "record type has not been registered".
(We can't, unfortunately, easily improve the parse-time error message
that leads people down this path in the first place.)

While at it, I refactored the code a bit to avoid duplicating the
same logic in several different places.

Per bug #15940 from Jaroslav Sivy.  Back-patch to v11 where the
current coding came in.  (The pre-v11 deficiencies in this area
aren't regressions, so we'll leave those branches alone.)

Patch by me, based on preliminary analysis by Dmitry Dolgov.

Discussion: https://postgr.es/m/15940-2ab76dc58ffb85b6@postgresql.org

Branch
------
REL_12_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/cdc8d371e27d534626b19e0810a8de74a31e2c91

Modified Files
--------------
src/backend/utils/adt/jsonfuncs.c   | 219 +++++++++++++++++++-----------------
src/test/regress/expected/json.out  |  29 ++++-
src/test/regress/expected/jsonb.out |  29 ++++-
src/test/regress/sql/json.sql       |   6 +
src/test/regress/sql/jsonb.sql      |   6 +
5 files changed, 179 insertions(+), 110 deletions(-)



view thread (3+ messages)  latest in thread

Message-ID: <E1hzpiI-00041s-9D@gemulon.postgresql.org>
Permalink:  ../E1hzpiI-00041s-9D@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1hzpiI-00041s-9D@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us
  Subject: Re: pgsql: Restore json{b}_populate_record{set}'s ability to take type info
  In-Reply-To: <E1hzpiI-00041s-9D@gemulon.postgresql.org>
  References: <E1hzpiI-00041s-9D@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox