【问题标题】:postgresql two phase commit prepare transaction error: transactions can not be started in PL / pgSQLpostgresql 两阶段提交准备事务错误:事务不能在 PL/pgSQL 中启动
【发布时间】:2018-09-12 15:17:44
【问题描述】:

我想为 PostgreSQL 做一个准备事务的两阶段提交事务。

你能帮忙解决这个错误吗?

我不明白如何使用准备事务通过 dblick 连接到远程数据库?

create or replace function insert_into_table_a() returns void as $$
    declare 
        trnxprepare text;
        trnxcommit text;
        trnxrollback text;
        trnxid varchar;
begin

    select uuid_generate_v4() into trnxid;
    select 'prepare transaction ' || ' ''' || trnxid || ' ''' into trnxprepare;
    select 'commit prepared     ' || ' ''' || trnxid || ' ''' into trnxcommit;
    select 'rollback prepared   ' || ' ''' || trnxid || ' ''' into trnxrollback;

    insert into table_a values ('test');
    perform dblink_connect('cn','dbname=test2 user=test2user password=123456');
    perform dblink_exec('cn','insert into table_b values (''test 2'');');
    perform dblink_disconnect('cn');

    execute trnxprepare;
    execute trnxcommit;

    exception 
        when others then
            execute trnxrollback;
            perform dblink_disconnect('cn');
            raise notice '% %', sqlerrm, sqlstate;
end;
$$ language plpgsql;




select insert_into_table_a();

错误:错误:不能在 PL / pgSQL 中启动事务

提示:改用带有 EXCEPTION 子句的 BEGIN 块。

CONTEXT: insert_into_table_a () PL / pgSQL 函数,第 24 行,在 EXECUTE 中

SQL 状态:0A000

【问题讨论】:

  • 阅读错误信息和提示后还有哪些问题未解决?您只是无法管理 PL/pgSQL 中的事务。 PostgreSQL v11 将允许在 PL/pgSQL 中进行一些事务处理。
  • @LaurenzAlbe 我在使用 v10 时遇到问题
  • 也许这可以帮助你:wiki.postgresql.org/wiki/…

标签: postgresql plpgsql distributed-transactions dblink


【解决方案1】:

因此,在 Postgres 中,大部分情况下您无法从内部函数控制事务。如果它们没有被捕获,你可以引发错误以间接中止它们,但你不能像这样直接开始或结束它们。

要管理事务,您需要一个工作进程作为可加载模块,或者通过连接控制来自客户端的事务。

【讨论】:

  • 我需要样品告诉你
猜你喜欢
  • 2013-01-11
  • 1970-01-01
  • 2014-03-16
  • 1970-01-01
  • 1970-01-01
  • 2012-02-15
  • 2018-08-15
  • 2016-01-04
  • 1970-01-01
相关资源
最近更新 更多