Why does to_json take "anyelement" rather than "any"?

From: Nikhil Benesch <nikhil(dot)benesch(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Why does to_json take "anyelement" rather than "any"?
Date: 2020-11-03 16:53:55
Message-ID: 5b332d27-d41e-733a-c1c8-9cebea31d585@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

to_json is declared as taking "anyelement" as input, which means
you can't pass it something of unknown type:

postgres=# SELECT to_json('foo');
ERROR: could not determine polymorphic type because input has type unknown

But this works fine with the very similar json_build_array function:

postgres=# SELECT json_build_array('foo');
json_build_array
------------------
["foo"]
(1 row)

The difference is that json_build_array takes type "any" as input, while
to_json takes "anyelement" as input.

Is there some reason to_json couldn't be switched to take "any" as input?
Hacking this together seems to mostly just work:

postgres=# CREATE FUNCTION my_to_json ("any") RETURNS json LANGUAGE 'internal' AS 'to_json';
postgres=# SELECT my_to_json('foo');
my_to_json
------------
"foo"
(1 row)

Is there something I'm missing?

Nikhil

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message David G. Johnston 2020-11-03 17:20:53 Re: PATCH: Batch/pipelining support for libpq
Previous Message Robert Haas 2020-11-03 16:52:01 Re: Online checksums verification in the backend