You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PostgreSQL中两个设为DEFAULT NOW()的TIMESTAMP列在行创建时是否保证值相等(未更新时)

Will created_at and modified_at (both defaulting to NOW()) be identical on row creation?

Absolutely—you can rely on these two columns being exactly equal when a row is first inserted, without any manual updates to modified_at. Here's why:

How NOW() works in PostgreSQL

NOW() (which is equivalent to CURRENT_TIMESTAMP) returns the start time of the current transaction, not the exact moment the function is called. When you insert a row, all column defaults are evaluated within the same transaction context. That means both created_at and modified_at will use the same transaction timestamp—down to the microsecond (or higher precision, depending on your PostgreSQL configuration).

Your test results confirm this

Looking at your query output, every row has matching created_at and modified_at values. This isn't a coincidence: each INSERT statement runs in its own transaction (unless you wrap multiple inserts in a BEGIN/COMMIT block), and within each transaction, NOW() resolves to a single fixed timestamp for all columns using it as a default.

What about future updates?

The only time these columns will diverge is if you explicitly update modified_at (e.g., setting it to NOW() when editing the row's comment). But on initial row creation, they will always be identical.

内容的提问来源于stack exchange,提问作者sudoExclamationExclamation

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 05:52:34