【问题标题】:PL/pgSQL Loops don't update current timestamp / now()PL/pgSQL 循环不更新当前时间戳 / now()
【发布时间】:2022-01-17 15:37:07
【问题描述】:

我想看看在 postgres 的 DO 块内循环迭代需要多长时间。基本布局如下:

DO $$
declare v_time timestamptz;
declare i record;
begin
for i in select generate_series(1, 5) t
loop

select current_timestamp into v_time;

perform pg_sleep(i.t);
-- something done here (pg_sleep to ensure some time passes)

raise notice '%', v_time - (select current_timestamp);
-- expect negative interval from RAISE. 
end loop;
end; $$;

但是,当我运行它时(已在 Postgres 13 和 9 上尝试过),我得到一个 0S 的间隔返回:

NOTICE:  00:00:00
NOTICE:  00:00:00
NOTICE:  00:00:00
NOTICE:  00:00:00
NOTICE:  00:00:00
DO

Query returned successfully in 15 secs 389 msec.

我以前做过这个,以前从来没有遇到过这个问题,所以我想我的问题是“这次我做错了什么?”而不是“为什么 postgres 行为异常?”

【问题讨论】:

    标签: postgresql plpgsql


    【解决方案1】:

    current_timestampdefined to be

    当前事务的开始时间,在事务期间它们的值不变

    您可能希望使用 clock_timestamp() 代替它返回在事务中更改的值,请参阅上面的链接以获取更完整的说明。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多