DO $$ DECLARE m_cnt bigint; BEGIN IF EXISTS ( SELECT 1 FROM pg_class c INNER JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relname = '{{.SequenceName}}' AND n.nspname = '{{.SchemaName}}' AND c.relkind = 'S' ) THEN SELECT COALESCE(MAX({{quote_ident .ColumnName}}), 0) + 1 FROM {{quote_ident .SchemaName}}.{{quote_ident .TableName}} INTO m_cnt; PERFORM setval('{{quote_ident .SchemaName}}.{{quote_ident .SequenceName}}'::regclass, m_cnt); END IF; END; $$;