gpt4 book ai didi

postgresql - PL/pgSQL 循环不更新当前时间戳/now()

转载 作者:行者123 更新时间:2023-12-01 23:08:30 25 4
gpt4 key购买 nike

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

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 表现异常?”

最佳答案

current_timestampdefined to be :

the start time of the current transaction, their values do not change during the transaction

您可能想使用 clock_timestamp(),它会返回一个在交易中发生变化的值,请参阅上面的链接以获得更完整的说明。

关于postgresql - PL/pgSQL 循环不更新当前时间戳/now(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70346945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com