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 1fTbfL-0005Hs-6V for pgsql-bugs@arkaria.postgresql.org; Thu, 14 Jun 2018 23:28:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fTbfJ-0006mk-IU for pgsql-bugs@arkaria.postgresql.org; Thu, 14 Jun 2018 23:28:29 +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 1fTbfJ-0006mZ-8V for pgsql-bugs@lists.postgresql.org; Thu, 14 Jun 2018 23:28:29 +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 1fTbfG-0006XU-M1 for pgsql-bugs@postgresql.org; Thu, 14 Jun 2018 23:28:28 +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 w5ENSOgV018366; Thu, 14 Jun 2018 19:28:24 -0400 From: Tom Lane To: Neil Conway cc: pgsql-bugs@postgresql.org Subject: Re: row_to_json(), NULL values, and AS In-reply-to: References: Comments: In-reply-to Neil Conway message dated "Thu, 14 Jun 2018 15:57:27 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <18364.1529018904.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 14 Jun 2018 19:28:24 -0400 Message-ID: <18365.1529018904@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk [ Hi Neil, long time no see ] Neil Conway writes: > The following behavior does not seem self-consistent to me: Likewise. > In particular, it is unclear to me why removing the targetlist alias > in the subquery in the third example should change the result set of > the parent query. Looking at "explain verbose" output, it seems like it's not row_to_json's fault; rather, we seem to be mishandling expansion of the whole-row Var: regression=3D# explain verbose select json_agg(row_to_json(x)) from (selec= t *, row_to_json(null) as jjj from generate_series(1, 3)) x; QUERY PLAN = = --------------------------------------------------------------------------= --------------- Aggregate (cost=3D15.00..15.01 rows=3D1 width=3D32) Output: json_agg(row_to_json(ROW(generate_series.generate_series, NULL:= :json))) -> Function Scan on pg_catalog.generate_series (cost=3D0.00..10.00 ro= ws=3D1000 width=3D4) Output: generate_series.generate_series Function Call: generate_series(1, 3) (5 rows) That's fine, but: regression=3D# explain verbose select json_agg(row_to_json(x)) from (selec= t *, row_to_json(null) from generate_series(1, 3)) x; QUERY PLAN = = --------------------------------------------------------------------------= --------------- Aggregate (cost=3D12.50..12.51 rows=3D1 width=3D32) Output: json_agg(NULL::json) -> Function Scan on pg_catalog.generate_series (cost=3D0.00..10.00 ro= ws=3D1000 width=3D0) Output: generate_series.generate_series Function Call: generate_series(1, 3) (5 rows) That looks like it might be a bug in what we do with whole-row Vars during subquery flattening. But if you put an "offset 0" into the subquery to prevent flattening, you get different but just as weird misbehavior: regression=3D# explain verbose select json_agg(row_to_json(x)) from (selec= t *, row_to_json(null) as jjj from generate_series(1, 3) offset 0) x; QUERY PLAN = = --------------------------------------------------------------------------= ---------------------- Aggregate (cost=3D25.00..25.02 rows=3D1 width=3D32) Output: json_agg(row_to_json(x.*)) -> Subquery Scan on x (cost=3D0.00..20.00 rows=3D1000 width=3D28) Output: x.* -> Function Scan on pg_catalog.generate_series (cost=3D0.00..10= .00 rows=3D1000 width=3D36) Output: generate_series.generate_series, NULL::json Function Call: generate_series(1, 3) (7 rows) regression=3D# explain verbose select json_agg(row_to_json(x)) from (selec= t *, row_to_json(null) from generate_series(1, 3) offset 0) x; QUERY PLAN = = --------------------------------------------------------------------------= ---------------- Aggregate (cost=3D22.50..22.52 rows=3D1 width=3D32) Output: json_agg((NULL::json)) -> Function Scan on pg_catalog.generate_series (cost=3D0.00..10.00 ro= ws=3D1000 width=3D36) Output: NULL::integer, NULL::json Function Call: generate_series(1, 3) (5 rows) I'm not sure if this is just another artifact of the same problem. The extra parens in the json_agg() argument are suspicious to put it mildly, but I've not dug into it to see what the plan tree really looks like. regards, tom lane