PostgreSQL partial/sub commit within function
来源:互联网 发布:unity3d so库 调用 编辑:程序博客网 时间:2024/06/04 19:30
Postgres2015全国用户大会将于11月20至21日在北京丽亭华苑酒店召开。本次大会嘉宾阵容强大,国内顶级PostgreSQL数据库专家将悉数到场,并特邀欧洲、俄罗斯、日本、美国等国家和地区的数据库方面专家助阵:
- Postgres-XC项目的发起人铃木市一(SUZUKI Koichi)
- Postgres-XL的项目发起人Mason Sharp
- pgpool的作者石井达夫(Tatsuo Ishii)
- PG-Strom的作者海外浩平(Kaigai Kohei)
- Greenplum研发总监姚延栋
- 周正中(德哥), PostgreSQL中国用户会创始人之一
- 汪洋,平安科技数据库技术部经理
- ……
PostgreSQL的函数是原子操作,所以不能像Oracle那样在函数中实现分段提交。
但是如果你要从Oracle迁移到PostgreSQL的话,必然会面临这样的问题,那么怎么办呢?
有几种方法可以来实现,下面是例子:
1. 通过exception来实现分段提交。
create table tbl (id int primary key, info text);create or replace function func1() returns void as $$declarev_stat int := 0;begininsert into tbl values(1);v_stat := 1;insert into tbl values(2);v_stat := 2;insert into tbl values(3);v_stat := 3;insert into tbl values(3);v_stat := 4;return;exception when others thencasewhen v_stat = 1 theninsert into tbl values(1);when v_stat = 2 theninsert into tbl values(1);insert into tbl values(2);when v_stat = 3 theninsert into tbl values(1);insert into tbl values(2);insert into tbl values(3);when v_stat = 4 theninsert into tbl values(1);insert into tbl values(2);insert into tbl values(3);insert into tbl values(3);elsereturn;end case;end;$$ language plpgsql;postgres=# select func1();func1-------(1 row)postgres=# select ctid,* from tbl;ctid | id | info-------+----+------(0,5) | 1 |(0,6) | 2 |(0,7) | 3 |(3 rows)
这样做的弊端很明显,需要重新插入,相当于前面的操作全部回滚,产生了双份数据,其中前面插入的一份是垃圾。
另外,代码会变的很繁琐。
所以我们可以略微改进一下。
2. 把每个分段作为一个子函数,正常返回true,异常返回false,在函数中调用这些子函数来实现分段来规避上面的问题。
create or replace function subf1() returns boolean as $$declarebegininsert into tbl values(1);-- 以及其他需要放一起提交的SQL和逻辑return true;exception when others thenreturn false;end;$$ language plpgsql strict;create or replace function subf2() returns boolean as $$declarebegininsert into tbl values(2);-- 以及其他需要放一起提交的SQL和逻辑return true;exception when others thenreturn false;end;$$ language plpgsql strict;create or replace function subf3() returns boolean as $$declarebegininsert into tbl values(3);-- 以及其他需要放一起提交的SQL和逻辑return true;exception when others thenreturn false;end;$$ language plpgsql strict;create or replace function subf4() returns boolean as $$declarebegininsert into tbl values(3);-- 以及其他需要放一起提交的SQL和逻辑return true;exception when others thenreturn false;end;$$ language plpgsql strict;create or replace function func1() returns void as $$declarev_stat boolean;begin-- 模拟分段1select subf1() into v_stat;if not v_stat thenreturn;end if;-- 模拟分段2select subf2() into v_stat;if not v_stat thenreturn;end if;-- 模拟分段3select subf3() into v_stat;if not v_stat thenreturn;end if;-- 模拟分段4select subf4() into v_stat;if not v_stat thenreturn;end if;return;end;$$ language plpgsql;postgres=# truncate tbl;TRUNCATE TABLEpostgres=# select func1();func1-------(1 row)postgres=# select ctid,* from tbl;ctid | id | info-------+----+------(0,1) | 1 |(0,2) | 2 |(0,3) | 3 |(3 rows)
现在正常了,不会产生双份垃圾。
0 0
- PostgreSQL partial/sub commit within function
- Partial-Function
- python partial function
- about function and sub
- sub/function:VBScript 过程
- 说说Sub和Function
- Postgresql function
- postgresql Two-phase commit
- 偏函数(Partial-Function)
- currying vs partial function application
- sub和function的区别
- Sub 和 Function 的应用
- Difference of Function and Sub
- VBScript Sub and Function Demo
- sub 与 function 的区别
- PostgreSQL function里面调用function
- PostgreSQL window function
- PostgreSQL aggregate function customize
- day2_Android下单元测试相关
- JAVA IO类
- 文件I/O实践(3) --文件共享与fcntl
- 一个 IT 青年编程四年的感悟
- Java进程监控与分析
- PostgreSQL partial/sub commit within function
- 模式识别之身份证识别
- 找出一个二维数组中的“鞍点”,即该位置上的元素在该行中 最大,在该列中最小(也可能没有“鞍点”),打印有关信息。(提示:注意特 殊情况:没鞍点或多个鞍点)
- linux下基于smtp协议的C++客户端debug经验
- use PostgreSQL async Notification as a chat group
- 日经春秋 20151014
- 交换排序——快速排序
- Scala的安装
- 第117讲:Hadoop集群之安装IP配置、Slaves、namenode和secondarynamenode的配置学习笔记