CREATE OR REPLACE VIEW sequence_setvals AS 
 SELECT ((((('SELECT setval(''"'::text || cs.relname::text) || '"'', max("'::text) || a.attname::text) || '")) FROM
"'::text) || c.relname::text) || '";'::text AS expr
   FROM pg_class c, pg_class cs, pg_attribute a, pg_attrdef d
  WHERE cs.relkind = 'S'::"char" AND d.adsrc ~ cs.relname::text AND c.oid = a.attrelid AND c.oid = d.adrelid AND d.adnum = a.attnum;


CREATE OR REPLACE FUNCTION sys_refresh_sequences()
  RETURNS integer AS
'
    DECLARE
        myseq RECORD;
    BEGIN
        FOR myseq IN select * from sequence_setvals LOOP
            RAISE NOTICE ''Executing - %'', myseq.expr;
            EXECUTE myseq.expr;
        END LOOP;

        return 1;
    END;
'
  LANGUAGE plpgsql VOLATILE
  COST 100;