为什么我的表达式索引不接受与查询相同的语法?

Joh*_*hir 1 postgresql

此查询按预期工作:

# select start_date + (duration * (interval '1 second')) from mytable;
      ?column?       
---------------------
 2016-06-30 19:00:00
(1 row)
Run Code Online (Sandbox Code Playgroud)

但是当尝试用它创建表达式索引时,出现语法错误:

# create index on mytable (start_date + (duration * (interval '1 second')));
ERROR:  syntax error at or near "+"
LINE 1: ...reate index on mytable (start_date + (duratio...
Run Code Online (Sandbox Code Playgroud)

jme*_*sky 5

您需要一组额外的括号。这有点尴尬,但这就是 postgres 知道将其解析为表达式的方式:

# create index on mytable (start_date + (duration * (interval '1 second')));
ERROR:  syntax error at or near "+"
# create index on mytable ((start_date + (duration * (interval '1 second'))));
CREATE INDEX
Run Code Online (Sandbox Code Playgroud)