I have a table defined like so:
create table users (
id serial primary key,
name text,
email text,
);
...and I want to write a function that returns rows of the shape:
(
id integer,
name text,
email text,
some_other_column boolean,
)
I managed to get this working with the code below, but I would rather not re-define the columns from the users table:
create or replace function get_users ()
returns table (
id integer,
name text,
email text,
some_other_column boolean,
) as $$
select users.*, true as some_other_column from users;
$$ language sql;
Is there a way to dynamically create a row type by doing something like this? (postgres complains of a syntax error at users.*
):
create or replace function get_users ()
returns table (
users.*,
some_other_column boolean
) as $$
select users.*, true as some_other_column from users;
$$ language sql;
Please note that the following query executed directly works just fine:
select users.*, true as some_other_column from users;
The ultimate goal here is to end up with a function callable like select * from get_users()
that returns rows that include both columns from existing tables and additional columns. I do not want the caller to worry about exactly how to call the function.
My assumption is that since I can write simple sql that returns the dynamic rows, I ought to be able to store that sql in the database in some way that preserves the structure of the returned rows.
select * from get_users()
. The DB provides a whole set of functions that I'd like to keep consistent. Maybe there's a way to use views? – Radke