我有这个 CREATE TABLE
例如有两列 DEFAULT NOW()
这样当创建新行时,它会自动设置为当前 TIMESTAMP
:
create table threads(threadid uuid PRIMARY KEY default uuid_generate_v4(), reply_to uuid REFERENCES threads(threadid), comment text not null, created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), modified_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW());
并插入一些示例:insert into threads (threadid, reply_to, comment) values ('40ad95dd-d64e-421a-a4f3-bf7b7017d99a',null,'WOW');
insert into threads (threadid, reply_to, comment) values ('5fe62a5d-e8de-4d3a-935c-a97087168e60','40ad95dd-d64e-421a-a4f3-bf7b7017d99a','MUCH WOW');
insert into threads (threadid, reply_to, comment) values ('f2e41c30-49d5-4420-9aac-00cf98b3d696','5fe62a5d-e8de-4d3a-935c-a97087168e60','WHAT?');
insert into threads (threadid, reply_to, comment) values ('e3f41c30-49d5-4420-9aac-00cf98b3d696','5fe62a5d-e8de-4d3a-935c-a97087168e60','MUCH WOW TO WHAT?');
insert into threads (threadid, reply_to, comment) values ('3c6eccbf-fca4-4c53-9d33-034a32e9b6eb','40ad95dd-d64e-421a-a4f3-bf7b7017d99a','0_0');
insert into threads (threadid, reply_to, comment) values ('ad5d7fc3-fc3a-48d6-ae3c-db8a37ba3464','40ad95dd-d64e-421a-a4f3-bf7b7017d99a','WOW WHAT??');
insert into threads (threadid, reply_to, comment) values ('32ad95dd-d64e-421a-a4f3-bf7b7017d99a',null,'Awesome sauce!');
是否保证两列具有完全相同的时间戳? (显然,只要该列未更新以触发更新 modified_at
)。从我到目前为止的简单测试来看,它们似乎总是相等的,但我想要更好的确认:
select created_at, modified_at from threads ;
created_at | modified_at
-------------------------------+-------------------------------
2021-01-30 06:38:07.676708-05 | 2021-01-30 06:38:07.676708-05
2021-01-30 06:38:07.678601-05 | 2021-01-30 06:38:07.678601-05
2021-01-30 06:38:07.678977-05 | 2021-01-30 06:38:07.678977-05
2021-01-30 06:38:07.679341-05 | 2021-01-30 06:38:07.679341-05
2021-01-30 06:38:07.679547-05 | 2021-01-30 06:38:07.679547-05
2021-01-30 06:38:07.679734-05 | 2021-01-30 06:38:07.679734-05
2021-01-30 06:38:36.878027-05 | 2021-01-30 06:38:36.878027-05
最佳答案
我对文档的理解是,如果 now()
的两种用法在同一个命令中,他们将在同一时间返回。
这是文档 - 阅读最后一行。
transaction_timestamp() is equivalent to CURRENT_TIMESTAMP, but is named to clearly reflect what it returns. statement_timestamp() returns the start time of the current statement (more specifically, the time of receipt of the latest command message from the client). statement_timestamp() and transaction_timestamp() return the same value during the first command of a transaction, but might differ during subsequent commands. clock_timestamp() returns the actual current time, and therefore its value changes even within a single SQL command. timeofday() is a historical PostgreSQL function. Like clock_timestamp(), it returns the actual current time, but as a formatted text string rather than a timestamp with time zone value. now() is a traditional PostgreSQL equivalent to transaction_timestamp().
来自 https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT
关于sql - Postgres : are two columns of TIMESTAMP type with DEFAULT NOW() guaranteed to be equal on row creation (unless updated)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65967564/