【问题标题】:Check time difference between timestamp and now in postgres在 postgres 中检查时间戳和现在之间的时间差
【发布时间】:2020-07-29 01:25:11
【问题描述】:

health_check_at 上次某些服务回调的时间。如果 now 和 health_check_at 之间的差异,我需要更改行中的状态列。

我的查询:

UPDATE instances
SET status = 'STOPPED'
WHERE id = 1
    AND health_check_at IS NOT NULL
    AND localtimestamp - health_check_at > # somehow check if diff more then n minutes 

例如,我如何实现current_timestamp - health_check_at 和 10 分钟之间的比较?

周末不应该算

【问题讨论】:

    标签: postgresql timestamp


    【解决方案1】:

    减去时间戳给出一个间隔。所以你需要一个间隔或N分钟。只需更改最后一行。

    UPDATE instances
    SET status = 'STOPPED'
    WHERE id = 1
        AND health_check_at IS NOT NULL
        AND localtimestamp - health_check_at > &N * interval '1 min';
    

    【讨论】:

      【解决方案2】:

      使用extract(t1,t2)age(t1, now()) 等函数。

      Extract

      【讨论】:

        猜你喜欢
        • 2021-09-09
        • 2021-03-22
        • 2022-06-12
        • 2022-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-22
        相关资源
        最近更新 更多