unis_crm/sql/20261119_idp_sys_param_setv...

84 lines
4.6 KiB
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

-- sys_param 主键序列自检:把 param_id 的序列抬到不低于表内 max(param_id)
--
-- 为什么单独一个脚本(真库实测):开发库里 sys_param 有 5 行、max(param_id)=5,
-- 而 public.sys_param_param_id_seq 的 last_value=2 —— 序列落后 3 位。
-- 后果是**任何 INSERT 都必撞 sys_param_pkey**,包括:
-- * PUT /api/idp/admin/config 首次给某个键建覆盖行(框架 create() 走 nextval);
-- * sql/20261119_idp_sys_param_seed.sql 的种子插入(本仓实测死在这)。
-- 这类坑不会自己好:只要有人手工插过 sys_param(带显式 param_id)而没动序列,就重新出现。
--
-- 只上调不下调(last_value >= max 时一句都不写),is_called=true 表示下一个 nextval 必须严格
-- 大于 max(param_id),不会撞回已有行。setval 带存在性守卫:序列改名 / 不在 public schema 时
-- 判空跳过,不以 42P01 关系不存在打断整个事务(本轮审计 §20(D))。
-- 代码侧也有一条同口径的自检(IdpRuntimeConfig.alignParamIdSequence,只在新建覆盖行前跑),
-- 两条不冲突:脚本修库里当下的状态,代码兜住后续再漂移的窗口。跑完脚本 dbcheck 的
-- sys_param_seq_lag 必须 <= 0。
--
-- 执行:psql -f sql/20261119_idp_sys_param_setval.sql
-- 回滚:不需要 —— 序列前移不回退,也不该回退(回退等于把坑埋回去)。
begin;
set search_path to public;
do $$
declare
v_sequence_name text;
v_max_id bigint;
v_last_value bigint;
v_is_called boolean;
begin
if to_regclass('public.sys_param') is null then
raise notice 'public.sys_param 不存在,本脚本不适用(已整段跳过)';
return;
end if;
-- 序列名不硬猜:pg_get_serial_sequence 按列的真实归属取(改名、换 schema 都能拿到)。
select pg_get_serial_sequence('public.sys_param', 'param_id') into v_sequence_name;
select coalesce(max(param_id), 0) into v_max_id from public.sys_param;
if v_max_id = 0 then
raise notice 'sys_param 表内没有数据,序列不需要动';
return;
end if;
-- 守卫与 setval 必须在同一条语句里(本轮 SQL 危害门的判据 C 按单条语句判):
-- 先判序列在不在,再抬到 max(param_id)。形状照 sql/20261119_idp_menu_perm.sql 里已验证的那段。
if to_regclass(v_sequence_name) is not null then
-- 先读当下位置,兑现上面那句「只上调」。is_called 必须一起读:false 表示 last_value 本身就是
-- 下一个要发的 id,那时 last_value = max 仍会撞主键,只有严格大于才允许跳过。
execute format('select last_value, is_called from %s', v_sequence_name)
into v_last_value, v_is_called;
end if;
-- 守卫与 setval 落在同一条语句里(本仓 SQL 危害门的判据 C 按单条语句判,拆开就被判成裸 setval)。
if to_regclass(v_sequence_name) is not null
and not ((v_is_called and v_last_value >= v_max_id)
or (not v_is_called and v_last_value > v_max_id)) then
execute format('select setval(%L, %s, true)', v_sequence_name, v_max_id);
raise notice 'sys_param 主键序列 % 已从 last_value=% 抬到 max(param_id)=%(is_called=true)',
v_sequence_name, coalesce(v_last_value, 0), v_max_id;
elsif to_regclass(v_sequence_name) is not null then
raise notice 'sys_param 主键序列 % 已在 max(param_id)=% 之上(last_value=%, is_called=%),一句未写',
v_sequence_name, v_max_id, v_last_value, v_is_called;
else
raise notice '没拿到 sys_param.param_id 的序列名(列不是 serial/identity 或序列已改名),已跳过';
end if;
end $$;
-- 自检结果直接打在输出里,跑完一眼能确认(与 dbcheck.py 的 sys_param_seq_lag 同一个口径)。
-- 读 pg_sequences 而不是裸读序列:序列改名/不在 public 时这句自己不许先炸(同一个判据 C 的理由)。
select coalesce((select max(param_id) from public.sys_param), 0) as max_param_id,
coalesce((select last_value from pg_sequences
where schemaname = 'public'
and sequencename = 'sys_param_param_id_seq'), 0) as sequence_last_value,
coalesce((select max(param_id) from public.sys_param), 0)
- coalesce((select last_value from pg_sequences
where schemaname = 'public'
and sequencename = 'sys_param_param_id_seq'), 0) as sequence_lag;
commit;
-- 回滚:无。序列位置是单调前移的,回滚会重新制造主键冲突。