In PostgreSQL how do I create a function that returns an actual table?
For example, if I have the following function:
CREATE OR REPLACE FUNCTION cols(_table_name TEXT)
RETURNS TABLE (column_name TEXT,
data_type TEXT) AS
$$
SELECT column_name,
data_type
FROM information_schema.columns
WHERE table_name = _table_name
ORDER BY column_name
;
$$ LANGUAGE sql;
And the following table:
CREATE TABLE test (a TEXT, b INTEGER, c NUMERIC);
If I run SELECT cols('test');
I get:
cols
-------------
(a,text)
(b,integer)
(c,numeric)
(3 rows)
But I would like to be able to get:
column_name | data_type
-------------+-----------
a | text
b | integer
c | numeric
(3 rows)