gpt4 book ai didi

函数内部的 postgresql 案例不会触发

转载 作者:行者123 更新时间:2023-11-29 14:34:03 24 4
gpt4 key购买 nike

所以,我的数据库中有几个函数。

当特定表中的数据超过 5 分钟时,需要运行一个函数。

我试过这样做:

PERFORM case when now() - '5 minutes'::interval > (select end_time from x order by end_route desc limit 1) then update_x() else null end;

当我将命令作为常规选择查询运行时,它运行正常。但是当我把它放在另一个函数中时(被调用的函数返回不超过 5 分钟的更新表),它永远不会运行。另外,如果我只放置 update_x(),那么它运行正常(但每次调用该函数时)。

有人知道我该如何解决这个问题吗?一个想法是设置一个 cron 以每 5 分钟独立运行一次该函数,但我宁愿服务器处于空闲状态,因为该函数非常耗费资源,并且不会经常被调用。

我使用的是 8.4 版(由于我的 ISP,所以无法更改,尽管我正在考虑迁移到 VPS,所以如果这可以在 9.5 和更新版本上运行,我可以等待)。

最佳答案

函数now()给出了当前交易的开始时间,在里面是不变的。使用 clock_timestamp(), 示例:

do $$
begin
for i in 1..3 loop
perform pg_sleep(1);
raise notice 'now(): % clock_timestamp(): %', now(), clock_timestamp();
end loop;
end $$;

NOTICE: now(): 2017-12-06 10:22:40.422683+01 clock_timestamp(): 2017-12-06 10:22:41.437099+01
NOTICE: now(): 2017-12-06 10:22:40.422683+01 clock_timestamp(): 2017-12-06 10:22:42.452456+01
NOTICE: now(): 2017-12-06 10:22:40.422683+01 clock_timestamp(): 2017-12-06 10:22:43.468124+01

根据 the documentation:

clock_timestamp() returns the actual current time, and therefore its value changes even within a single SQL command (...)

now() is a traditional PostgreSQL equivalent to transaction_timestamp().

关于函数内部的 postgresql 案例不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47670255/

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