Postgres Procedure Returns Table
I suppose you can solve it by using an explicit cast.

CREATE FUNCTION test(tic text)
RETURNS TABLE (foo VARCHAR, bar int)
AS $$
DECLARE
  sql text;
BEGIN
  sql := 'SELECT ''' || tic || ''', NULL';
  RETURN QUERY EXECUTE sql;
END
$$
LANGUAGE plpgsql;

SELECT test('A');



  ERROR:  structure of query does not match function result type DETAIL:
  Returned type text does not match expected type character varying in
  column 1.


CREATE FUNCTION test1(tic text)
RETURNS TABLE (foo VARCHAR, bar int)
AS $$
DECLARE
  sql text;
BEGIN
  sql := 'SELECT ''' || tic || '''::VARCHAR, NULL::int';
  RETURN QUERY EXECUTE sql;
END
$$
LANGUAGE plpgsql;

SELECT test1('A');




| test1 |
| :---- |
| (A,)  |



db&lt;>fiddle here