CREATE FUNCTION get_header_line(int, text) RETURNS SETOF text AS $$ SELECT (regexp_matches(lines, '^'||$2||': (.*?)$', 'gni'))[1] FROM header WHERE mail_id=$1; $$ LANGUAGE sql;