Skip to content

Instantly share code, notes, and snippets.

@stelf
Created March 1, 2017 09:33
Show Gist options
  • Save stelf/8b234393adc5ef453a810c34a2947c03 to your computer and use it in GitHub Desktop.
Save stelf/8b234393adc5ef453a810c34a2947c03 to your computer and use it in GitHub Desktop.
update all sequences with some value in postgresql
create or replace function update_all_seqs(inc integer) returns void as $$
DECLARE
r pg_class%rowtype;
i name;
isit integer;
BEGIN
FOR i in SELECT c.relname from pg_class c where c.relkind = 'S' LOOP
isit := POSITION('pv_' in i);
IF (isit = 1) THEN
execute 'select last_value from visionr.' || i into isit;
execute 'alter sequence visionr.' || i || ' restart with ' || (isit+inc);
RAISE NOTICE 'update d(%)', i::varchar;
END IF;
END LOOP;
END;
$$ LANGUAGE plpgsql;
select f();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment