Created
July 26, 2024 17:25
-
-
Save tetri/0e714a0a9207ae9b26885519a4906e81 to your computer and use it in GitHub Desktop.
Consulta que retorna o número de registros por tabela para o usuário informado.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION public.sp_conta_filtros(p_idusuario numeric) | |
RETURNS TABLE(tabela character varying, counttabela numeric, countfiltro numeric) | |
LANGUAGE plpgsql | |
AS $function$ | |
declare tabelas record; | |
conta numeric; | |
begin | |
drop table if exists __contafiltro; | |
CREATE TEMPORARY TABLE __contafiltro | |
(tabela varchar(255), counttabela decimal(18), countfiltro decimal(18)) WITH ( OIDS=FALSE ); | |
for tabelas in | |
select tn.tablename, | |
tf.filter, | |
coalesce(( | |
SELECT n_live_tup | |
FROM pg_stat_user_tables pgtable | |
where pgtable.relname = lower(tn.tablename) | |
),0) as numeroregistros | |
from dctablename tn | |
inner join dctablefilter tf on tf.tablename = tn.tablename | |
order by 1 asc | |
LOOP | |
RAISE NOTICE 'Tabela: (%)', tabelas.tablename; | |
execute 'select count(*) conta ' || | |
' from ( ' || replace (tabelas.filter, '?', p_idusuario::varchar) || ') as aux' | |
INTO conta; | |
insert into __contafiltro values(tabelas.tablename,tabelas.numeroregistros,conta); | |
END LOOP; | |
RETURN QUERY select '_Todos'::varchar as tabela, | |
sum(__contafiltro.counttabela) as counttabela, | |
sum(__contafiltro.countfiltro) as countfiltro | |
FROM __contafiltro | |
group by 1 | |
union all | |
SELECT __contafiltro.tabela, | |
__contafiltro.counttabela, | |
__contafiltro.countfiltro | |
FROM __contafiltro | |
order by 3 desc; | |
drop table if exists __contafiltro; | |
END;$function$ | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment