CREATE OR REPLACE FUNCTION normalize_seq() RETURNS VOID AS $$ DECLARE statements CURSOR FOR SELECT '''' || relname || '''' as seqname,substring(relname from 1 for (char_length(relname) -7)) as tablename FROM pg_class WHERE relkind = 'S' and relname in (select table_name||'_id_seq' from information_schema.columns where table_catalog = 'golden_24_10' and table_schema = 'public' and column_name = 'id'); BEGIN FOR smt IN statements LOOP RAISE NOTICE 'Secuencia actualizada: %',smt.seqname; EXECUTE 'select pg_catalog.setval(' || smt.seqname || ', (select max(id)+1 from ' || smt.tablename || '), true)'; END LOOP; END; $$ LANGUAGE PLPGSQL; SELECT normalize_seq();