-- ============================================================================ -- 生产库迁移后同步序列:unis_crm(PG17@192.168.124.202) -> unis_scc(PG16@10.100.51.227) -- -- 成因(已实测复现):Navicat 的“结构导出”先 CREATE SEQUENCE "x_id_seq"(START 1,不带 -- 源库当前值),再 CREATE TABLE ... GENERATED BY DEFAULT AS IDENTITY。规范名被占用, -- identity 只能另建一个 *_id_seq1 并从 1 开始。新库现状:34 个 identity 列绑在 -- *_id_seq1 上,94 个序列里有 37 个已无任何列引用(其中一部分还残留着旧库数值)。 -- 所以不要按 "<表>_<列>_seq" 猜序列名,必须用 pg_get_serial_sequence() 解析; -- 否则连 pg_dump 自带的 setval 都会打到那些孤儿序列上,不报错但完全不生效。 -- -- 首选做法:用原生 pg_dump 把结构+数据一次性恢复进空库,序列名与数值都会自动对齐, -- 本脚本只是兜底自检(正常输出“上调 0 个,剩余落后 0 个”)。 -- -- 用法:灌完数据之后、放量之前执行。只在序列落后时上调,从不下调,可重复执行。 -- psql "host=10.100.51.227 port=30532 dbname=unis_scc user=root" -f 本文件 -- -- 验证:已在 PostgreSQL 16.15 上按上述成因搭建夹具,复现 identity / serial / 业务编码 -- 三种 duplicate key 失败,执行本脚本后全部恢复,重复执行幂等。 -- ============================================================================ begin; -- 序列被别的会话占用时跳过并告警,不要卡住发布 set local lock_timeout = '5s'; set local statement_timeout = '10min'; -- 待检查清单:identity / serial 列由目录解析,业务编码序列按名字显式列出 create temp table if not exists seq_targets (seq text, tbl text, col text, prefix text) on commit preserve rows; truncate seq_targets; insert into seq_targets (seq, tbl, col, prefix) select pg_get_serial_sequence(format('%I.%I', n.nspname, c.relname), a.attname), format('%I.%I', n.nspname, c.relname), a.attname, null from pg_attribute a join pg_class c on c.oid = a.attrelid join pg_namespace n on n.oid = c.relnamespace where n.nspname = 'public' and c.relkind = 'r' and a.attnum > 0 and (a.attidentity <> '' or exists ( select 1 from pg_attrdef d where d.adrelid = a.attrelid and d.adnum = a.attnum and pg_get_expr(d.adbin, d.adrelid) like 'nextval%')); -- 编码形如 QD-20261029-000822,mapper 里用 nextval() 直接取号 insert into seq_targets (seq, tbl, col, prefix) values ('public.crm_channel_expansion_code_seq', 'crm_channel_expansion', 'channel_code', 'QD'), ('public.crm_customer_code_seq', 'crm_customer', 'customer_code', 'CUS'), ('public.crm_opportunity_code_seq', 'crm_opportunity', 'opportunity_code', 'OPP'); do $$ declare r record; v_next bigint; v_max bigint; v_fixed int := 0; v_late int := 0; begin ---------------------------------------------------------------------------- -- 第一轮:落后的上调到 max + 1 ---------------------------------------------------------------------------- for r in select seq, tbl, col, prefix from seq_targets where seq is not null order by tbl, col loop begin if r.prefix is null then execute format('select coalesce(max(%I), 0) from %s', r.col, r.tbl) into v_max; else execute format( $f$select coalesce(max((substring(%I from '([0-9]+)$'))::bigint), 0) from %I where %I ~ %L$f$, r.col, r.tbl, r.col, '^' || r.prefix || '-[0-9]{8}-[0-9]+$') into v_max; end if; continue when v_max = 0; -- 空表不动序列 execute format( 'select case when is_called then last_value + 1 else last_value end from %s', r.seq) into v_next; if v_next <= v_max then -- is_called = true:下一次 nextval() 返回 v_max + 1 execute format('select setval(%L, %s, true)', r.seq, v_max); v_fixed := v_fixed + 1; raise notice '[sync] % : next % -> %', r.seq, v_next, v_max + 1; end if; exception when others then raise warning '[sync] skip % : %', r.seq, sqlerrm; end; end loop; ---------------------------------------------------------------------------- -- 第二轮:自检,正常情况下不应再有落后项 ---------------------------------------------------------------------------- for r in select seq, tbl, col, prefix from seq_targets where seq is not null order by tbl, col loop begin if r.prefix is null then execute format('select coalesce(max(%I), 0) from %s', r.col, r.tbl) into v_max; else execute format( $f$select coalesce(max((substring(%I from '([0-9]+)$'))::bigint), 0) from %I where %I ~ %L$f$, r.col, r.tbl, r.col, '^' || r.prefix || '-[0-9]{8}-[0-9]+$') into v_max; end if; continue when v_max = 0; execute format( 'select case when is_called then last_value + 1 else last_value end from %s', r.seq) into v_next; if v_next <= v_max then v_late := v_late + 1; raise warning '[check] % 仍然落后:next = %,期望 >= %', r.seq, v_next, v_max + 1; end if; exception when others then raise warning '[check] skip % : %', r.seq, sqlerrm; end; end loop; raise notice '序列同步完成:上调 % 个,剩余落后 % 个', v_fixed, v_late; end $$; commit; -- 参考:库里还有这些序列没有任何列或默认值引用(迁移残留,确认后可清理) select s.relname as unused_sequence, (select last_value from pg_sequences ps where ps.schemaname = 'public' and ps.sequencename = s.relname) as last_value from pg_class s join pg_namespace n on n.oid = s.relnamespace where s.relkind = 'S' and n.nspname = 'public' and not exists ( select 1 from pg_attrdef d where pg_get_expr(d.adbin, d.adrelid) like '%' || s.relname || '%') and not exists ( select 1 from seq_targets t where t.seq = 'public.' || s.relname or t.seq = s.relname) and s.relname not in ( select (string_to_array(seq, '.'))[2] from seq_targets where seq is not null) order by 1;