postgresql function事务

原来对function的理解是有误的,以为functioin 是作为一个 事务的(另一本书上写的Begin-end 包含的语句块作为一个事务,搞不清楚了),最近看了书《Postgresql 服务器编程》-戚长松 译 第32页有这样一段话:“我们需要特别注意的是,当非事务性的代码块(BEGIN...END)被定义的时候,Postresql会像一个私人事务一样对待每一个独立的语句,并且在语句完成的时候立即执行他们。这样的操作就可以让其他事务有机会插入到你的语句中。

做了个实验理解具体一点:function发生错误会回滚相关的所有事务,begin-end不能作为事务标志。异常捕获可以保证正常执行。

理解多了一点。

select * from public.student;
insert into public.student values('1001','tom');
insert into public.student values('1002','lucy');

show transaction_isolation;

create or replace function func() returns void as $$
begin
	update student set name = 'sun' where id = '1001';
    update student set name = 'poli' where id = 1/0;
	exception when others then
		raise notice '美丽的意外';
	update student set name = 'jim' where id = '1001';
end;
$$ language plpgsql;

select func();

猜你喜欢

转载自blog.csdn.net/u010933982/article/details/82818051