Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fUCPV-0005HG-9T for pgsql-bugs@arkaria.postgresql.org; Sat, 16 Jun 2018 14:42:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fUCPT-000500-BD for pgsql-bugs@arkaria.postgresql.org; Sat, 16 Jun 2018 14:42:35 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fUCPT-0004zn-0r; Sat, 16 Jun 2018 14:42:35 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fUCPP-0001aK-Gy; Sat, 16 Jun 2018 14:42:33 +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 w5GEgQs2014511; Sat, 16 Jun 2018 10:42:26 -0400 From: Tom Lane To: Neil Conway cc: pgsql-hackers@lists.postgresql.org, pgsql-bugs@postgresql.org Subject: Re: row_to_json(), NULL values, and AS In-reply-to: <32293.1529091298@sss.pgh.pa.us> References: <18365.1529018904@sss.pgh.pa.us> <32582.1529074393@sss.pgh.pa.us> <32293.1529091298@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Fri, 15 Jun 2018 15:34:58 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <14509.1529160146.1@sss.pgh.pa.us> Date: Sat, 16 Jun 2018 10:42:26 -0400 Message-ID: <14510.1529160146@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I wrote: > Neil Conway writes: >> On Fri, Jun 15, 2018 at 7:53 AM Tom Lane 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