Home >Database >Mysql Tutorial >How to Correctly Define PostgreSQL Functions Returning Record or Composite Types?
The error "ERROR: 42601: a column definition list is required for functions returning "record"" indicates that a function that is declared to return a record type does not have a column definition list specified in its return statement.
CREATE OR REPLACE FUNCTION get_user_by_username(_username text , _online bool DEFAULT false) RETURNS TABLE ( user_id int , user_name varchar , last_activity timestamptz ) LANGUAGE plpgsql AS $func$ BEGIN IF _online THEN RETURN QUERY UPDATE users u SET last_activity = current_timestamp -- ts with time zone WHERE u.user_name = _username RETURNING u.user_id , u.user_name , u.last_activity; ELSE RETURN QUERY SELECT u.user_id , u.user_name , u.last_activity FROM users u WHERE u.user_name = _username; END IF; END $func$;
Call:
SELECT * FROM get_user_by_username('myuser', true);
To return all columns of the existing table users, there is a simpler way. Postgres automatically defines a composite type of the same name for every table. Just use RETURNS SETOF users to vastly simplify the query:
CREATE OR REPLACE FUNCTION get_user_by_username(_username text , _online bool DEFAULT false) RETURNS SETOF users LANGUAGE plpgsql AS $func$ BEGIN IF _online THEN RETURN QUERY UPDATE users u SET last_activity = current_timestamp WHERE u.user_name = _username RETURNING u.*; ELSE RETURN QUERY SELECT * FROM users u WHERE u.user_name = _username; END IF; END $func$;
CREATE OR REPLACE FUNCTION get_user_by_username3(_username text , _online bool DEFAULT false) RETURNS TABLE ( users_row users , custom_addition text ) LANGUAGE plpgsql AS $func$ BEGIN IF _online THEN RETURN QUERY UPDATE users u SET last_activity = current_timestamp -- ts with time zone WHERE u.user_name = _username RETURNING u -- whole row , u.user_name || u.user_id; ELSE RETURN QUERY SELECT u, u.user_name || u.user_id FROM users u WHERE u.user_name = _username; END IF; END $func$;
The "magic" is in the function call, where we (optionally) decompose the row type:
SELECT (users_row).*, custom_addition FROM get_user_by_username('foo', true);
The above is the detailed content of How to Correctly Define PostgreSQL Functions Returning Record or Composite Types?. For more information, please follow other related articles on the PHP Chinese website!