【问题标题】:Postgres query which runs itself after every 1 hour每 1 小时运行一次的 Postgres 查询
【发布时间】:2021-05-28 13:13:47
【问题描述】:

我正在使用带有 Dbeaver 工具的 Postgres。 Dbeaver 在一段时间后使连接失效,并且在很长一段时间后触发查询时,执行时间会更长。

我需要编写一个每 1 小时后自行运行的查询,并对数据库中的任何表执行查询,这样我的连接就不会进入空闲阶段。

任务调度程序在 Dbeaver EE 中可用,它可以完成这项工作,但是当我使用它的社区版时,我正在考虑按照上面解释的方式来做。

我尝试编写以下查询。它有一个循环,在循环内部,我试图使用 pg_sleep 保持查询执行,但这不起作用。它给了我以下错误。另外,我正在考虑无限运行这个循环。

你能帮忙吗?

do $$
declare 
   counter integer := 0;
begin
   while counter < 24 loop --need to change it to infinite loop
    pg_sleep(3600);   --wait for 1 hour
    perform 'select * from user_roles ur';  --any query to run
    counter := counter + 1;
   end loop;
end$$;

错误:

SQL Error [42601]: ERROR: syntax error at or near "pg_sleep"

【问题讨论】:

  • 不要那样做。相反,请使用损坏较少的工具。

标签: sql database postgresql dbeaver


【解决方案1】:

您可以在 linux 上使用 cron 或在 windows 上使用 windows task scheduler , 或者您可以使用pgagent 附带的pgadmin

但是在您的查询中,您需要select sleep(second)

do $$
declare 
   counter integer := 0;
begin
   while counter < 24 loop --need to change it to infinite loop
    select pg_sleep(3600);   --wait for 1 hour
    perform 'select * from a_files af ';  --any query to run
    counter := counter + 1;
   end loop;
end$$;

【讨论】:

    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 2017-01-10
    • 2014-10-28
    相关资源
    最近更新 更多