Postgres:在回滚之前保存临时表中的行 [英] Postgres: Save rows from temp table before rollback

查看:52
本文介绍了Postgres:在回滚之前保存临时表中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a main procedure (p_proc_a) in which i create temp table for logging (tmp_log). In main procedure i call some other procedures (p_proc_b, p_proc_c). In every of these procedures i insert data into tmp_log table. How to save rows from tmp_log into physical table (log) in case of exeption before rollback?

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 回滚更改?

Is there any workround to save logs into table and rollback changes from p_proc_b and p_proc_c ?

感谢您的建议.

推荐答案

这在 PostgreSQL 中是不可能的.

That is not possible in PostgreSQL.

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

The typical workaround is to use dblink to connect to the database itself and write the logs via dblink.

这篇关于Postgres:在回滚之前保存临时表中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆