如何使用 NOW() 函数作为范围的上限?

How to use the NOW() function as upper bound of a range?

我在 Postgres 10.6 数据库中有一个 table,其中有一列类型为 tstzrange。 我需要插入/更新具有已定义下限的行,但当前时间的值是范围的上限,因此 NOW() 作为上限值。

尝试过类似的东西:

UPDATE table_name
SET date_range = ['2018-03-23 00:00:00-05', now())
WHERE id = 3;

有没有办法使用内置函数或子查询?

使用范围构造器The manual:

Each range type has a constructor function with the same name as the range type. Using the constructor function is frequently more convenient than writing a range literal constant, since it avoids the need for extra quoting of the bound values. The constructor function accepts two or three arguments. The two-argument form constructs a range in standard form (lower bound inclusive, upper bound exclusive), while the three-argument form constructs a range with bounds of the form specified by the third argument. The third argument must be one of the strings “()”, “(]”, “[)”, or “[]”.

所以:

UPDATE table_name
SET    date_range = tstzrange('2018-03-23 00:00:00-05', now())
WHERE  id = 3;

我假设您知道 now() 解析为事务的开始时间。