I am developing a function in Postgres that aims to retrieve for each record of a query the result value of a scan contained in a set of functions. Of these functions only one will return the correct value. These functions have a common prefix 'fn_condicao' ' and receive as an parameter an object of type' my_table '.
As the number of functions that make the check is not known, I decided to consult the Postgres catalog, from the table pg.catalog.pg_proc searching for functions with the prefix 'fn_condicao_ ' and run them dynamically from the command EXECUTE .
My problem is how to pass the parameter correctly to the EXECUTE command.
How to indicate in commented out in the function below 'select ' || funcoes.proname || '(' || registro || ')';
that the entry is of type my_table?
create or replace function testa_condicoes()
returns void as
$$
declare
registro minha_table%rowtype;
funcoes pg_proc%rowtype;
begin
set search_path = 'pg_catalog';
for registro in (select * from minha_table where id in (1,2,3)) loop
for funcoes in (
SELECT p.proname
FROM pg_namespace n
JOIN pg_proc p
ON p.pronamespace = n.oid
WHERE n.nspname = 'operacional'
and p.proname like ('fn_condicao_%')
order by p.proname)
loop
--execute 'select ' || funcoes.proname || '(' || registro || ')';
end loop;
end loop;
end;
$$
language plpgsql;
Example of functions
create or replace function fn_condicao_1(registro minha_table)
returns bigint as
$$
begin
if (registro.atributo1 > registro.atributo2) then
return 1;
end if;
return null;
end;
$$
language plpgsql;