From: Tom Lane <tgl@sss.pgh.pa.us>
To: Neil Conway <neil.conway@gmail.com>
Cc: pgsql-hackers@lists.postgresql.org
Cc: pgsql-bugs@postgresql.org
Subject: Re: row_to_json(), NULL values, and AS
Date: Sat, 16 Jun 2018 10:42:26 -0400
Message-ID: <14510.1529160146@sss.pgh.pa.us> (raw)
In-Reply-To: <32293.1529091298@sss.pgh.pa.us>
References: <CAOW5sYa3Wp7KozCuzjOdw6PiOYPi6D=VvRybtH2S=2C0SVmRmA@mail.gmail.com>
<18365.1529018904@sss.pgh.pa.us>
<32582.1529074393@sss.pgh.pa.us>
<CAOW5sYY2bkRutaoTm1CG5VoYhZk6YvL1m8MkUONJvXmtfkFH_g@mail.gmail.com>
<32293.1529091298@sss.pgh.pa.us>
I wrote:
> Neil Conway <neil.conway@gmail.com> writes:
>> On Fri, Jun 15, 2018 at 7:53 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>>> I'm a bit hesitant to muck with this behavior, given that it's stood
>>> for ~20 years. However, if we did want to touch it, maybe the right
>>> thing would be to give up the absolutist position that f(x) and x.f
>>> are exactly interchangeable. We could say instead that we prefer the
>>> function interpretation if function syntax is used, and the column
>>> interpretation if column syntax is used.
>> Interesting! Your proposed change seems quite reasonable to me.
> Here's a proposed patch for that. (It needs to be applied over the
> fixes in <14497.1529089235@sss.pgh.pa.us>, which are unrelated but
> touch some of the same code.) I didn't add any test cases yet,
> but probably it's desirable to have one.
It occurred to me this morning that there's actually a dump/reload
hazard in the current behavior. Consider
regression=# create table t1 (f1 int, f2 text);
CREATE TABLE
regression=# create view v1 as select row_to_json(t1) as j from t1;
CREATE VIEW
regression=# alter table t1 add column row_to_json text;
ALTER TABLE
regression=# \d+ v1
View "public.v1"
Column | Type | Collation | Nullable | Default | Storage | Description
--------+------+-----------+----------+---------+----------+-------------
j | json | | | | extended |
View definition:
SELECT row_to_json(t1.*) AS j
FROM t1;
At this point, pg_dump will naively dump the view as
CREATE VIEW public.v1 AS
SELECT row_to_json(t1.*) AS j
FROM public.t1;
which, with the historical behavior, will be read as a reference to
the subsequently-added column, giving a view definition that means
something else entirely:
regression=# \d+ v1
View "public.v1"
Column | Type | Collation | Nullable | Default | Storage | Description
--------+------+-----------+----------+---------+----------+-------------
j | text | | | | extended |
View definition:
SELECT t1.row_to_json AS j
FROM t1;
The proposed change fixes this on the parsing side, with no need
for any hacking in ruleutils.c (hence it will fix it for pre-existing
dump files too).
So I'm now pretty well convinced that this is a good change and we
should slip it into v11. I'm still afraid to back-patch though.
I vaguely recall one or two prior complaints in this area, but not
so many that it's worth taking a chance of breaking applications
in minor releases.
regards, tom lane
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-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, neil.conway@gmail.com, pgsql-hackers@lists.postgresql.org
Subject: Re: row_to_json(), NULL values, and AS
In-Reply-To: <14510.1529160146@sss.pgh.pa.us>
References: <CAOW5sYa3Wp7KozCuzjOdw6PiOYPi6D=VvRybtH2S=2C0SVmRmA@mail.gmail.com> <18365.1529018904@sss.pgh.pa.us> <32582.1529074393@sss.pgh.pa.us> <CAOW5sYY2bkRutaoTm1CG5VoYhZk6YvL1m8MkUONJvXmtfkFH_g@mail.gmail.com> <32293.1529091298@sss.pgh.pa.us> <14510.1529160146@sss.pgh.pa.us>
* 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