current_timestamp
是一个系统函数,它返回一个时间戳,表示当前事务开始那一刻的时间点。
current_timestamp
在 PostgreSQL 6.3 中被添加。
用法
current_timestamp → timestamp with time zone
尽管它的名字是 current_timestamp
,但它返回的是当前事务开始的时间点,而不是函数实际执行的时间(关于这个,请参见 clock_timestamp()
)。函数 transaction_timestamp()
在 PostgreSQL 8.2 中被添加,以便提供一个明确的函数来检索当前事务的开始时间。
该函数有一个变体 current_timestamp()
,用于检索具有指定精度的事务时间戳。
current_timestamp (integer
) →timestamp with time zone
请注意,current_timestamp
不能以 current_timestamp()
的形式调用。
变更历史
- PostgreSQL 7.2
- 可以指定返回时间戳的精度(提交 bd97e4e7)
- PostgreSQL 6.2
- 已添加(提交 f10b6392)
示例
current_timestamp
的基本执行示例
postgres=# SELECT current_timestamp; current_timestamp ------------------------------- 2021-06-17 19:25:22.955989+01 (1 row)
current_timestamp
将始终报告与 transaction_timestamp() 相同的值,并且(如果在隐式事务中执行)与 statement_timestamp() 相同的值。
postgres=# SELECT current_timestamp, transaction_timestamp(), statement_timestamp(), clock_timestamp()\gx -[ RECORD 1 ]---------+------------------------------ current_timestamp | 2021-06-17 19:26:40.599694+01 transaction_timestamp | 2021-06-17 19:26:40.599694+01 statement_timestamp | 2021-06-17 19:26:40.599694+01 clock_timestamp | 2021-06-17 19:26:40.600033+01
精度(小数位数)可以根据需要指定。
postgres=# SELECT current_timestamp(3); current_timestamp ---------------------------- 2021-06-17 20:07:24.741+01 (1 row)
指定的精度必须在 0
和 6
之间;大于此值将被截断为 6
。
postgres=# SELECT current_timestamp(99); WARNING: TIMESTAMP(99) WITH TIME ZONE precision reduced to maximum allowed, 6 current_timestamp ------------------------------- 2021-06-17 20:08:32.147416+01 (1 row)
变体 current_timestamp()
是无效的。
postgres=# SELECT current_timestamp(); ERROR: syntax error at or near ")" LINE 1: SELECT current_timestamp();