row_to_json(), NULL values, and AS

Neil Conway <neil.conway@gmail.com>

From: Neil Conway <neil.conway@gmail.com>
To: pgsql-bugs@postgresql.org
Date: 2018-06-14T22:57:27Z
Lists: pgsql-bugs, pgsql-hackers
Hi,

The following behavior does not seem self-consistent to me:

postgres=# select json_agg(row_to_json(x)) from (select *, null from
generate_series(1, 3)) x;
                                                       json_agg
-----------------------------------------------------------------------------------------------------------------------
 [{"generate_series":1,"?column?":null},
{"generate_series":2,"?column?":null},
{"generate_series":3,"?column?":null}]
(1 row)

postgres=# select json_agg(row_to_json(x)) from (select *,
row_to_json(null) as jjj from generate_series(1, 3)) x;
                                                json_agg
--------------------------------------------------------------------------------------------------------
 [{"generate_series":1,"jjj":null}, {"generate_series":2,"jjj":null},
{"generate_series":3,"jjj":null}]
(1 row)

postgres=# select json_agg(row_to_json(x)) from (select *,
row_to_json(null) from generate_series(1, 3)) x;
      json_agg
--------------------
 [null, null, null]
(1 row)

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.

Thanks,
Neil


Commits

  1. Remove obsolete prohibition on function name matching a column name.

  2. Consider syntactic form when disambiguating function vs column reference.