If I create a function f that queries a function I think it becomes easier to read if the return type is the name of a table instead of RETURNS TABLE(id integer, name text).
CREATE TABLE users ( id integer, name text );
CREATE OR REPLACE FUNCTION f()
RETURNS users
AS $$
SELECT * FROM users
WHERE FALSE
$$
LANGUAGE SQL;
But I'm getting weird results when the query in the function is returning zero rows.
SELECT * FROM f();
Expected result
+------+--------+
| id | name |
|------+--------|
+------+--------+
Actual result
+--------+--------+
| id | name |
|--------+--------|
| <null> | <null> |
+--------+--------+
If there are more columns they will still all be null. If the query in the function returns any rows then it works as expected. I'm not getting this behaviour if I'm using the RETURNS TABLE(...) syntax.
Is there a way to get around this?
I'm using PostgreSQL 9.6