Skip to content

Instantly share code, notes, and snippets.

@mzupan
Created April 21, 2026 16:25
Show Gist options
  • Select an option

  • Save mzupan/7ae42d7204862af019c26967bf4adf2d to your computer and use it in GitHub Desktop.

Select an option

Save mzupan/7ae42d7204862af019c26967bf4adf2d to your computer and use it in GitHub Desktop.
DO $$
DECLARE
rec RECORD;
max_val BIGINT;
seq_name TEXT;
BEGIN
FOR rec IN
SELECT table_schema, table_name, column_name
FROM information_schema.columns
WHERE column_default LIKE 'nextval%'
AND table_schema NOT IN ('pg_catalog', 'information_schema')
LOOP
seq_name := pg_get_serial_sequence(
rec.table_schema || '.' || rec.table_name,
rec.column_name
);
IF seq_name IS NOT NULL THEN
EXECUTE format(
'SELECT COALESCE(MAX(%I), 1) FROM %I.%I',
rec.column_name, rec.table_schema, rec.table_name
) INTO max_val;
PERFORM setval(seq_name, max_val + 1000, true);
RAISE NOTICE 'Set % to %', seq_name, max_val + 1000;
END IF;
END LOOP;
END $$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment