【发布时间】:2021-07-17 00:36:05
【问题描述】:
我在 PostgreSQL 11 上有一个 PL/pgSQL 函数如下:
create or replace function public.process_single_usage(usage_p "UsagePs")
returns text
language plpgsql
as $function$
declare
return_msg text;
-- Variables used for business logic
begin
-- Business logic and error handling, return_msg variable is assigned to an error message on failure
-- or to an empty string on success.
-- A bunch of insert statements if no error arises.
return return_msg;
end
$function$;
我是否可以使用第二个函数在表的每一行上运行第一个函数,并在任何一行出现错误消息时回滚每个插入,或者我是否需要使用单个函数来控制事务?
【问题讨论】:
-
该函数始终是调用者事务的一部分。而且你不能在函数中使用提交或回滚
-
那么,既然一个函数正在调用,那是否意味着在给定时间有两个事务,一个嵌套另一个?
-
不,一切都在由第一个函数的调用者定义的单个事务中运行
-
@a_horse_with_no_name:虽然这是真的,但我认为这不会妨碍 OP 试图实现的目标。我添加了一个答案。
-
那么你有答案了吗?
标签: postgresql function transactions plpgsql commit