Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 函数内的时间戳比较_Postgresql_Timezone - Fatal编程技术网

Postgresql 函数内的时间戳比较

Postgresql 函数内的时间戳比较,postgresql,timezone,Postgresql,Timezone,要防止旧版本覆盖新版本,请在此简单函数中: create function myupdate(paramts timestamp without time zone, ...) language plpgsql AS $$ begin -- step 1 compare current record timestamp vs. supplied timestamp if exists (select 1 from record where ts <> paramts and ..

要防止旧版本覆盖新版本,请在此简单函数中:

create function myupdate(paramts timestamp without time zone, ...)
  language plpgsql AS
$$
begin
-- step 1 compare current record timestamp vs. supplied timestamp
if exists (select 1 from record where ts <> paramts and ...) then
    raise exception 'A newer version exists, cannot update.';
end if;
...
end
$$;
create function myfetch(...)
  language plpgsql AS
$$
begin
    return query select ts, ... from record where ...;
end
$$;
节点API和Angular客户端UI得到的是2021-04-16T21:37:35.878Z,提交给myupdate的值也是如此。然而,在我们西海岸的一台服务器上,在myupdate内部执行期间,ts自动转换为PST 2021-04-16 14:37:35.878694,并且在右侧有3个额外的数字

如何在UTC和相同精度下进行比较?

您应该使用带时区的时间戳时间戳,而不是不带时区的时间戳时间戳,以避免任何时区混淆。在餐桌上,在功能上,在整个食物链上。然后,值在内部始终存储为UTC时间,并且比较可以正确地自动比较不受时区影响的时间点

见:


无论哪种方式,这两种类型都具有微秒分辨率,即6位小数位数。您的第一个示例以某种方式被截断,可能是由于您的客户机在显示器上截短的。

以下是完整的答案:

使用tz=带时区的时间戳进行列定义。 tz总是向客户提供UTC值,例如,我使用的是node/pg promise,前端是Angular/TS,它们都获得类似2021-04-17T22:42:57.610Z的值。 UTC输出,UTC提交回,假设您不触摸时间戳,是否足够好?是和否。如果您的客户端支持6位十进制秒,则为是,否则JavaScript和变体为否,因为它只支持3位。 解决方案是四舍五入到2位数,然后进行比较 如果从mytable中选择tableTimestamp::Timestamp 2=参数::Timestamp 2,其中。。。然后更新mytable。。。;否则会引起例外。。。;如果结束


另外,带时区和不带时区都令人困惑。

谢谢欧文的建议,尽管答案并不能回答我所有的问题,但我还是接受它,因为tz是最好的解决方案。我将在下面发布我的完整答案和解决方案。