PostgreSQL忘记输入sqlin条件sqlin更新整张表的解决办法

发布时间:2024-05-16
虽然出现这个错误很挫,但有时候还是会被你或者你的同事碰到。为了避免这个错误,postgresql数据库中可以通过触发器来解决,这里用的是plpgsql 。
1、修改postgresql.conf配置增加:custom_variable_classes = ‘limits’
重新加载:pg_ctl reload
2、创建触发器函数create or replace function zero_counter() returns trigger language plpgsql as$body$declarebegin perform set_config(\'limits.test\', \'0\', true); if tg_op = \'update\' then return new; end if; return old;end;$body$;create or replace function limit_modifications() returns trigger language plpgsql as$body$declare i int4;begin i := current_setting(\'limits.test\')::int4 1; perform set_config(\'limits.test\', i::text, true); if i > 5000 then raise exception \'% of more than 5000 row is forbidden.\', tg_op; end if; if tg_op = \'update\' then return new; end if; return old;end;$body$;3、测试forummon=# create table test as select i as id, \'password for: \' || i as pass from generate_series(1,10000) i;forummon=# create trigger zero_counter before update or delete on test for each statement execute procedure zero_counter();create triggerforummon=# create trigger limit_modifications before update or delete on test for each row execute procedure limit_modifications();create triggerforummon=# \\d test table \public.test\ column | type | modifiers | storage | stats target | description -------- --------- ----------- ---------- -------------- ------------- id | integer | | plain | | pass | text | | extended | | triggers: limit_modifications before delete or update on test for each row execute procedure limit_modifications() zero_counter before delete or update on test for each statement execute procedure zero_counter()has oids: noforummon=# update test set pass = \'qq\' where id<5003 ;error: update of more than 5000 row is forbidden.forummon=# update test set pass = \'qq\' where id<5000 ;update 4999forummon=# drop table test ;drop table总结批量操作的数量限制可以在limit_modifications中修改,切记在update不确定时先select再update。
参考连接:http://www.depesz.com/2007/07/27/update-account-set-password-new_password-oops/

上一个:windows10文件后缀隐藏了怎么显示(window10文件后缀隐藏了怎么显示)
下一个:混合气体腐蚀试验箱的“外表”及气体浓度控制

雷蒙磨磨粉机在环保方面上的意义及磨粉生产线上的介绍
怎样使用花岗石方箱
良心挂机游戏(挂机游戏有哪些好玩)
园林植物景观设计
平板电脑与笔记本电脑的区别,ipad与笔记本电脑的区别
浅析复合气体检测仪报警值设定标准
北京夏天玩水的地方推荐
姜小白专栏|六大茶类基础知识大全(三)
机床* 美国哈斯新推数控车削、高速镗铣加工中心
电缆的内屏蔽与外屏蔽各起什么作用?