简体   繁体   English

PostgreSQL:CURRENT_TIMESTAMP和CLOCK_TIMESTAMP解决方案:Windows与Linux?

[英]PostgreSQL: CURRENT_TIMESTAMP AND CLOCK_TIMESTAMP Resolution: Windows vs. Linux?

So I have this interesting problem with the PostgreSQL timing functions. 所以我对PostgreSQL计时功能有这个有趣的问题。

Here's the situation. 这是情况。 We have a pre-production server (Linux) that we house our in-development applications. 我们有一个预生产服务器(Linux),用于存放开发中的应用程序。 I also do some work on a local copy of that DB (Windows) in case there is some more important work going on with the server. 如果该服务器正在进行一些更重要的工作,我还将对该数据库(Windows)的本地副本进行一些工作。 I recently ran into an issue where I started to get primary key violations on a logging table on my local DB copy. 最近,我遇到一个问题,即我开始在本地数据库副本的日志记录表上遇到主键冲突。 I thought this would be impossible since I was using the CLOCK_TIMESTAMP (current system time) as the primary key. 我以为这是不可能的,因为我使用CLOCK_TIMESTAMP(当前系统时间)作为主键。 In addition, I tested on the pre-prod server and it worked fine. 另外,我在预生产服务器上进行了测试,并且工作正常。 So I did some investigating. 所以我做了一些调查。 I eventually found out that if I run 'SELECT CLOCK_TIMESTAMP()' on the server, it returns the time down to the microsecond. 我最终发现,如果我在服务器上运行“ SELECT CLOCK_TIMESTAMP()”,它将使时间降低到微秒。 If I run it on my localhost, it only goes down to the millisecond. 如果我在本地主机上运行它,它只会下降到毫秒。 So the issue occurs when more than one update occurs before the timer gets to the next millisecond, which is definitely possible given some of our processes. 因此,当计时器到达下一毫秒之前发生了多个更新时,就会发生此问题,这在我们的某些流程中绝对是可能的。

So my question is this. 所以我的问题是这个。 Why is this happening and how can I fix it? 为什么会发生这种情况,我该如何解决? Is this some obscure setting I haven't been able to find yet? 这是我无法找到的一些晦涩的设置吗? Or is it a different in the timer resolutions of Windows vs Linux? 还是Windows与Linux的计时器分辨率不同?

Edit: The same thing happens with CURRENT_TIMESTAMP, NOW(), and all other timestamp-returning built-in functions. 编辑:CURRENT_TIMESTAMP,NOW()和所有其他返回时间戳的内置函数也会发生相同的情况。

Thanks 谢谢

Quoting Tom Lane on this pg_hackers thread: 在此pg_hackers线程上引用Tom Lane:

http://www.postgresql.org/message-id/9699.1262011789@sss.pgh.pa.us http://www.postgresql.org/message-id/9699.1262011789@sss.pgh.pa.us

I suppose what you're really asking about is not the precision of the datatype but the precision of now() readings. 我想您真正要问的不是数据类型的精度,而是now()读数的精度。 You're out of luck --- Windows just doesn't expose a call to get the wall clock time to better than 1 msec. 您真不走运-Windows并未公开将墙上时钟时间缩短至1毫秒以上的电话。

Keep in mind that whatever the Linux machine is returning might be largely fantasy in the low-order bits, too. 请记住,无论Linux计算机返回什么,在低阶位也可能是幻想。

To sort out your problem, consider using a serial as the primary key. 要解决您的问题,请考虑使用序列号作为主键。 (Assuming, of course, that you actually need a primary key in the first place for the log file.) (当然,假定您实际上首先需要一个主键来保存日志文件。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM