Postgresql 8.4更新plpgsql函数中的查询语法错误

时间:2017-12-12 00:55:13

标签: postgresql plpgsql postgresql-8.4

我正在使用PostgreSQL 8.4并创建一个plpgsql函数。在这个函数的主体中,我有一个查询来更新记录。

...
  UPDATE device_syncfiles SET 
  state_code = 1, updated_at = NOW() at time zone 'UTC'
  WHERE 
  ((state_code = 2 AND EXTRACT(EPOCH FROM (NOW() at time zone 'UTC' - updated_at::timestamp without time zone)) > 3600) OR 
  (state_code = 3 AND EXTRACT(EPOCH FROM (NOW() at time zone 'UTC' - updated_at::timestamp without time zone)) > 3600));
...

当我将此函数加载到数据库中时,会出现语法错误

ERROR:  syntax error at or near "$1"
LINE 1: UPDATE device_syncfiles SET  $1  = 1,  $2  = NOW() at time z...
                                     ^
QUERY:  UPDATE device_syncfiles SET  $1  = 1,  $2  = NOW() at time zone 'UTC' WHERE (( $1  = 2 AND EXTRACT(EPOCH FROM (NOW() at time zone 'UTC' -  $2 ::timestamp without time zone)) >  $3 ) OR ( $1  = 3 AND EXTRACT(EPOCH FROM (NOW() at time zone 'UTC' -  $2 ::timestamp without time zone)) >  $4 ))
CONTEXT:  SQL statement in PL/PgSQL function "ig_syncfile_get" near line 19

我发现此查询没有任何问题。这里有什么问题?

更新:(缺少信息)

表:device_syncfiles

  id PK integer auto inc
  user_id integer FK
  file_name character varying(255) NOT NULL,
  state_code integer NOT NULL FK,
  md5 character varying(255) NOT NULL,
  msg character varying(255),
  created_at timestamp without time zone,
  updated_at timestamp without time zone

功能:ig_syncfile_get()

CREATE OR REPLACE FUNCTION ig_syncfile_get()
  RETURNS TABLE(id integer, user_id integer, file_name character varying, state_code integer, md5 character varying, created_at timestamp without time zone, updated_at timestamp without time zone) AS
$BODY$
      DECLARE
        _device_syncfile_id integer;
        _download_timeout integer;
        _processing_timeout integer;
      BEGIN
        -- GET all timeout info
        SELECT state_timeout INTO _download_timeout FROM device_syncfile_states 
        WHERE state_name = 'downloading';
        SELECT state_timeout INTO _processing_timeout FROM device_syncfile_states 
        WHERE state_name = 'processing';
        -- GET syncfile id
        _device_syncfile_id = NULL;

        -- Reset timed out file to idel state
        UPDATE device_syncfiles SET 
        state_code = 1, updated_at = NOW() at time zone 'UTC'
        WHERE 
        ((state_code = 2 AND EXTRACT(EPOCH FROM (NOW() at time zone 'UTC' - updated_at::timestamp without time zone)) > _download_timeout) OR 
        (state_code = 3 AND EXTRACT(EPOCH FROM (NOW() at time zone 'UTC' - updated_at::timestamp without time zone)) > _processing_timeout));

        -- GET the id of one idel/timed out file => result could be a integer or NULL
        SELECT device_syncfiles.id INTO _device_syncfile_id FROM device_syncfiles 
        WHERE 
        device_syncfiles.state_code = 1 OR 
        (device_syncfiles.state_code = 2 AND EXTRACT(EPOCH FROM (NOW() at time zone 'UTC' - device_syncfiles.updated_at::timestamp without time zone)) > _download_timeout) OR
        (device_syncfiles.state_code = 3 AND EXTRACT(EPOCH FROM (NOW() at time zone 'UTC' - device_syncfiles.updated_at::timestamp without time zone)) > _processing_timeout) 
        LIMIT 1;

        -- WHEN NULL skip state update and return empty set of record
        -- Otherwise return the set of record with the id found in last step
        IF _device_syncfile_id IS NOT NULL THEN
          PERFORM ig_syncfile_update(_device_syncfile_id, 2, NULL);
        END IF;

        RETURN QUERY SELECT 
        device_syncfiles.id,
        device_syncfiles.user_id ,
        device_syncfiles.file_name ,
        device_syncfiles.state_code ,
        device_syncfiles.md5 ,
        device_syncfiles.created_at ,
        device_syncfiles.updated_at 
        FROM device_syncfiles WHERE device_syncfiles.id = _device_syncfile_id;
      END;
      $BODY$
  LANGUAGE plpgsql VOLATILE

1 个答案:

答案 0 :(得分:3)

许多 问题。

0。

  

我正在使用Postgresql 8.4

Postgres 8.4于2014年7月达到EOL。Consider upgrading to a current version.迫切需要。

1。

您的问题没有透露完整的功能(至少是页眉和页脚),也没有任何表格定义和一些示例数据来帮助我们。

我必须做出假设,我的有根据的猜测是你有一个名为state_code的函数参数,它与相同的列名冲突。在 三个 位置。基本信息:

您必须知道RETURNS TABLE子句中声明的所有字段都是有效的OUT参数。 (如第一个链接的第一句所述。)所以你的Q更新证实了我的假设。

2

您的错误消息会在此处报告这些实例的

UPDATE device_syncfiles SET 
  state_code = 1 ...

这是 0。 的结果。您正在绊倒Postgres的长期遗忘版本,其中在函数创建时检查表面语法,用于检测UPDATE语句和函数参数的目标列之间的命名冲突。这很愚蠢,后来被删除了:那些目标列不能与主体上的函数参数冲突。

您在Postgres 8.4中重现的错误: dbfiddle here

Postgres 9.4中没有发生同样的情况: dbfiddle here

修复,最好重命名函数参数以避免冲突。相关:

3

还有两个实例:

WHERE 
  ((state_code = 2 AND EXTRACT(EPOCH FROM (NOW() at time zone 'UTC' - updated_at::timestamp without time zone)) > 3600) OR 
   (state_code = 3 AND EXTRACT(EPOCH FROM (NOW() at time zone 'UTC' - updated_at::timestamp without time zone)) > 3600));

在任何版本中都需要修复。 Postgres无法判断是否要解析为函数参数或表列。最佳表 - 限定所有列以避免与参数名称先验可能发生冲突。 (UPDATE目标列除外,它们不需要也不允许表限定。)

4

这仍然是猪的口红。像这样改进查询:

UPDATE device_syncfiles d
SET    state_code = 1
     , updated_at = NOW() AT TIME ZONE 'UTC'
WHERE  d.state_code IN (2, 3)
AND    d.updated_at < (now() - interval '1 hour') AT TIME ZONE 'UTC';

更短,更快,可以使用updated_at上的索引。

5

最后考虑使用timestamp with time zone代替timestamp without time zone开头: