这个问题以前曾被问过:
Finding COMMIT impossible in a Postgres Housekeeping Procedure个
Postgresql invalid transaction termination when calling procedure
...但没有一个答案对我有用
我的存储过程中有一个循环.循环包含INSERTs,这需要是循环的written to the output table at the end of each pass.
- 如果不使用COMMIT,则需要等到存储过程结束时才能访问结果.
- 当我插入COMMIT(就在"end loop"之前)时,我收到以下错误消息:
ERROR: invalid transaction termination
CONTEXT: PL/pgSQL function [...] at COMMIT
SQL state: 2D000
我try 了官方文档中的第一个例子: https://www.postgresql.org/docs/12/plpgsql-transactions.html
我还是收到了同样的错误信息.
事实上,我举了一个更简单的例子:
CREATE TABLE test1 (a INTEGER);
CREATE PROCEDURE test_commit()
LANGUAGE plpgsql
AS $$
BEGIN
FOR i IN 0..9 LOOP
INSERT INTO test1 (a) VALUES (i);
COMMIT;
END LOOP;
END;
$$;
CALL test_commit();
相同的错误消息:
ERROR: invalid transaction termination
CONTEXT: PL/pgSQL function test_commit() line 5 at COMMIT
SQL state: 2D000
我正在运行pgAdmin 4的代码,我试着打开或关闭"自动提交".同样的结果.
我做错了什么?