postgresql - 不一致日期时间 PostgreSQL 默认值 now()

标签 postgresql

我有这种情况,我有这个名为 balance 的表:

id      -> Integer Auto Increment not null
balance -> numeric(19) not null
date    -> datetime default now() not null


id   |   balance    |            date
1          100         2019-09-12 16:15:29.091720
2          99          2019-09-12 16:15:33.404119
3          98          2019-09-12 16:15:33.412087
4          97          2019-09-12 16:15:33.425252
5          96          2019-09-12 16:15:33.442137    
6          95          2019-09-12 16:15:33.513825    -> this time A
7          94          2019-09-12 16:15:33.444407    -> this time B

然后我只插入表中的 1 列,是平衡的。使用线程过程插入示例:

INSERT INTO balance(balance) VALUES(100)

日期B低于A,id增量为6后7,是否按id进行处理?

例如,id 6 是第一次处理然后插入 7。那么 6 的时间必须小于 7 吗?

知道为什么会这样吗?

最佳答案

如果插入 id = 7 行的事务开始早于插入 id = 6 的行,那么这是可能的。请注意,事务开始的时间很重要,而不是 insert 作为该事务的一部分执行的时间。

As documented in the manual , now() 返回交易开始的时间(与 transaction_timestamp() 相同,不是“当前”时间。

如果需要,您应该将默认值更改为 clock_timestamp()

关于postgresql - 不一致日期时间 PostgreSQL 默认值 now(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57904256/

相关文章:

python - psycopg2 找不到符号 _PQbackendPID

ruby-on-rails - 如何在 Rails Active Record 中实现 "Write to both columns"(正如 Strong 迁移所建议的那样)?

postgresql - 优化大型 ST_Intersect 查询,将 1.8m OSM 街道匹配到 317k 多边形

PostgreSQL 如何查找最近 n 分钟内的任何更改

python - 使用 psycopg3 在服务器之间复制表

sql - 如何通过 id 和自定义类型的下一行进行选择?

postgresql - 更新 golang 中的 Jsonb 列

postgresql - 如何在带有 where 子句的 sequelize 中使用嵌套包含?

database - 如何 pg_dump RDS Postgres 数据库?

postgresql - 重命名存储函数的列名