Full question re-write
I'm looking a First() aggregate function.
Here I found something that almost works:
CREATE OR REPLACE FUNCTION public.first_agg ( anyelement, anyelement ) RETURNS anyelement LANGUAGE sql IMMUTABLE STRICT AS $$ SELECT $1; $$; -- And then wrap an aggregate around it CREATE AGGREGATE public.first ( sfunc = public.first_agg, basetype = anyelement, stype = anyelement );
The problem is that when a varchar(n) columns passes trough the first() function, it's converted into simple varchar (without a size). Trying to return the query in a function as RETURNS SETOF anyelement, I get the following error:
ERROR: structure of query does not match function result type Estado de SQL:42804 Detalhe:Returned type character varying does not match expected type character varying(40) in column 2. Contexto:PL/pgSQL function vsr_table_at_time(anyelement,timestamp without time zone) line 31 at RETURN QUERY
In the same wiki page there is a link to a C Version of the function that would replace the above. I don't know how to install it, but I wonder if this version could solve my problem.
Meanwhile, is there a way I can change the above function so it return the exact same type of the input column?