postgresql - "ERROR: invalid transaction termination"尝试使用嵌套事务控制执行过程时

标签 postgresql transactions plpgsql commit postgresql-11

根据文档 ( https://www.postgresql.org/docs/current/app-psql.html ),即使 AUTOCOMMIT 设置为关闭,PSQL 也会在任何尚未在事务中的命令之前发出隐式 BEGIN block ,它本身不是 BEGIN 或其他事务控制命令,也不是不能在事务 block 内执行的命令,例如 VACUUM。 (不幸的是,CALL 的处理方式与 VACCUM 不同)。而且,根据 Shaun Thomas ( https://blog.2ndquadrant.com/pg-phriday-stored-procedures-postgres-11/ ) 的说法,无效事务终止错误的发生是因为不可能从过程中关闭当前事务(在本例中是由 PSQL 启动的事务)。我尝试了所有与事务控制相关的 PSQL 设置,但所有设置都出现无效事务终止错误;即使 PSQL 处理的命令文件只包含 CALL 语句。

这是我调用的程序:

create or replace procedure producto$cargar_imagenes(_super$ bigint, _archivo$ character varying) as $$
declare
    _msg character varying;
    _log rastro_proceso%ROWTYPE;
begin
    perform rastro_proceso_temporal$insert(_super$);
    perform producto$cargar_imagenes$biz(_super$, _archivo$);
    if (_super$ is not null and _super$ > 0) then
        perform producto$cargar_imagenes$log(_super$, _archivo$);
    else
        perform tarea_usuario$private$update(6519204281880642486, null);
    end if;
    commit;
end;
$$ language plpgsql set search_path = public;

它在 commit 语句处失败;如果我将其注释掉,它就会起作用。

最佳答案

删除 SET 子句。每the documentation:

If a SET clause is attached to a procedure, then that procedure cannot execute transaction control statements (for example, COMMIT and ROLLBACK, depending on the language).

关于postgresql - "ERROR: invalid transaction termination"尝试使用嵌套事务控制执行过程时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53214740/

相关文章:

postgresql - 如何按标识符分组并在 postgreSQL 中创建一个 JSON 对象数组?

连接表的 SQL 约束

java - JPA 并发事务

python - Psycopg2 callproc 和 sql 参数

sql - 在触发之前不更新值postgresql

sql - Postgres LIMIT/OFFSET 奇怪的行为

sql - 如何迭代具有已知起始位置和混合列顺序的行,就像遍历相应的索引一样?

java - 如何在 Spring 5 中使用自定义 'TransactionAttributeSource'

django - 在 Django 中使用带有错误处理的原子 block

c# - 如何将复合类型传递给 PL/pgsql 函数?