【问题标题】:Operator does not exist: interval > integer运算符不存在:区间 > 整数
【发布时间】:2026-01-15 23:10:01
【问题描述】:

我有一个查询可以在 Postgresql 7.4 上运行,但不能在具有相同数据库的 Postgresql 8.3 上运行。

查询:

SELECT * FROM login_session WHERE (now()-modified) > timeout;

得到以下错误:

ERROR:  operator does not exist: interval > integer
LINE 1: ...ELECT * FROM login_session WHERE (now()-modified) > timeout ...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

modifiedtimestamptimeoutinteger

我需要在服务器上更改一些设置吗?

我正在新服务器 (ubuntu) 上为客户端安装应用程序,因此我无法更改应用程序中的查询。

【问题讨论】:

    标签: sql postgresql


    【解决方案1】:

    7.4 和 8.3 之间有很多变化。其中一些最激烈的是删除了一些自动演员表。

    我想“超时”以秒为单位?如果是这样,您可以将查询更改为:

    SELECT
        *
    FROM
        login_session
    WHERE
        (CURRENT_TIMESTAMP - modified) > (timeout * '1 sec'::interval);
    

    【讨论】:

      【解决方案2】:
      create or replace function int2interval (x integer) returns interval as $$ select $1*'1 sec'::interval $$ language sql;
      create cast (integer as interval) with function int2interval (integer) as implicit;
      

      应该这样做。

      【讨论】:

      • 优先于 pgsql 版本,因为它不需要 pgsql。我还运行了“乘法与 1 秒投射”与“连接到字符串并投射”的计时。这个版本(1 秒施法)快了 ~ 30%
      【解决方案3】:
      CREATE OR REPLACE FUNCTION intToInterval(arg integer)
        RETURNS interval AS
      $BODY$
         BEGIN      
            return CAST( arg || ' seconds' AS interval ); 
         END;
      $BODY$
        LANGUAGE 'plpgsql';
      
      CREATE CAST (integer AS interval)
      WITH FUNCTION intToInterval ( integer )
      AS IMPLICIT;
      

      (假设超时以秒为单位 - 否则相应更改)

      【讨论】: