PostgreSQL迁库后序列号冲突

通过如下SQL查询序列号,复制出需要drop的行执行删除

SELECT 'DROP SEQUENCE "'||n.nspname||'"."'||c.relname||'";' FROM pg_class c 
left join pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind ='S'
ORDER BY c.relname

删除序列号没有带seq1的序列

with t(s) as(
SELECT 'DROP SEQUENCE "'||n.nspname||'"."'||c.relname||'";' FROM pg_class c 
left join pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind ='S'
ORDER BY c.relname)
SELECT * from t where t.s not like '%seq1";';

来源

  1. PostgreSQL:“more than one owned sequence found“异常问题解决