【问题标题】:Postgres: Save rows from temp table before rollbackPostgres:在回滚之前保存临时表中的行
【发布时间】:2020-12-23 14:26:21
【问题描述】:

我有一个主程序 (p_proc_a),我在其中创建用于日志记录的临时表 (tmp_log)。在主程序中,我调用了一些其他程序(p_proc_b,p_proc_c)。在每个这些过程中,我都将数据插入到 tmp_log 表中。 回滚前出现异常,如何将 tmp_log 中的行保存到物理表(日志)中?

create procedure p_proc_a
language plpgsql
as $body$
begin

  create temp table tmp_log (log_message text) on commit drop;

  call p_proc_b();
  call p_proc_c();

  insert into log (log_message) 
  select log_message from tmp_log;

  exception
    when others then
      begin
        get stacked diagnostics
          v_message_text = message_text;

        insert into log (log_message)
        values(v_message_text);
      end;
end;
  $body$

是否有任何解决方法可以将日志保存到表中并从 p_proc_b 和 p_proc_c 回滚更改?

感谢您的建议。

【问题讨论】:

    标签: sql postgresql logging transactions rollback


    【解决方案1】:

    这在 PostgreSQL 中是不可能的。

    典型的解决方法是使用dblink 连接到数据库本身并通过dblink 写入日志。

    【讨论】:

    • 感谢您的提示。我为每个过程使用 json (inout) 参数进行了解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 2017-02-19
    相关资源
    最近更新 更多