繁体   English   中英

Postgresql 触发/交易行为

[英]Postgresql Trigger / Transaction Behaviour

为了帮助我调试数据库中的另一个问题,我在 postgresql 中编写了以下 function 作为触发器:

CREATE OR REPLACE FUNCTION stage.triggerlogfunction()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
begin
    insert into stage.temptriggerlog_complete
    values
    (current_timestamp, 'Hello');
    perform pg_sleep(5);
    insert into stage.temptriggerlog_complete
    values
    (current_timestamp, 'Did');
    perform pg_sleep(5);
    insert into stage.temptriggerlog_complete
    values
    (current_timestamp, 'This Work?');
    return null;
END;
$function$
;

当一个新行插入到我的数据库中的一个表中时,这个触发器被设置为触发。 这按预期工作,但我期望在运行时看到的是三行,时间戳相隔 5 秒(按顺序运行,每行之间有 5 秒的延迟),但我返回的实际结果是三行,全部带有相同的时间戳。

为什么会这样? 有什么方法可以强制执行我期望的行为吗?

有什么办法可以在每个插入语句运行时获取当前时间戳?

您正在寻找statement_timestamp() (这将是导致触发器触发的语句的时间戳)甚至clock_timestamp()然后。

请参阅文档以了解它们的作用和替代方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM