指定事务级别的Postgres dblink存储过程调用

Fro*_*art 1 postgresql dblink plpgsql

我想以这种方式在PL / pgSQL存储过程中使用dblink:

PERFORM dblink_exec('myconn', 'BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE');
PERFORM dblink_exec('myconn', 'SELECT another_stored_procedure()');
PERFORM dblink_exec('myconn', 'COMMIT');
Run Code Online (Sandbox Code Playgroud)

但是在运行时出现错误:

ERROR:  statement returning results not allowed
CONTEXT:  SQL statement "SELECT dblink_exec('myconn', 'select another_stored_procedure()')"
Run Code Online (Sandbox Code Playgroud)

因此执行失败,尽管我尝试以其他方式获得所需的结果。

更新1:

我知道Postgresql中的存储过程是事务性的。我将dblink用于自治事务功能以在同一服务器上使用它。

问题是服务器上的默认事务级别是“读已提交”,但有时我需要从另一个级别开始事务,例如“可序列化”。

因此,我需要在具有明确事务级别指定的自主事务中执行存储过程。

据我所知dblink允许这样做,但是我找不到适合我情况的有关dblink或dblink_exec函数的任何有用信息。

poz*_*ozs 5

我认为,您已经在另一端连接了另一个PostgreSQL服务器。

您需要调用该dblink()函数以执行具有结果(而不是结果)的语句dblink_exec()。(即使另一端的函数具有returns void-在这种情况下,您也可以通过NULL在中调用该函数来获得单个函数SELECT。)

另外,您可能不需要事务管理:

简而言之,您需要执行:

-- PERFORM dblink_exec('myconn', 'BEGIN ...');
--    if you need explicit transaction management
PERFORM * FROM dblink('myconn', 'SELECT another_stored_procedure()') alias(col text);
-- PERFORM dblink_exec('myconn', 'COMMIT');
Run Code Online (Sandbox Code Playgroud)