gpt4 book ai didi

postgresql - 具有指定事务级别的 Postgres dblink 存储过程调用

转载 作者:行者123 更新时间:2023-11-29 12:33:13 25 4
gpt4 key购买 nike

我想以这种方式在 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');

但我在运行时遇到错误:

ERROR:  statement returning results not allowed
CONTEXT: SQL statement "SELECT dblink_exec('myconn', 'select another_stored_procedure()')"

所以执行失败了,虽然我尝试用不同的方式得到想要的结果。

更新 1:

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

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

所以我需要在具有显式事务级别指定的自治事务中执行存储过程。

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

最佳答案

我假设,您已经连接到另一端的另一个 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');

关于postgresql - 具有指定事务级别的 Postgres dblink 存储过程调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28299150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com