current_time 是一个系统函数,它返回当前事务开始时的 time。
current_time 添加于 PostgreSQL 6.3。
用法
current_time → time with time zone
尽管其名称是 current_time,但它返回的是当前事务开始的时间,而不是函数实际执行的时间(后者可以通过 clock_timestamp() 来获取)。
该函数提供了一个变体 current_time(),用于检索具有指定精度的事务时间。
current_time (integer) →timestamp with time zone
请注意,current_time 不能以 current_time() 的方式调用。
变更历史
- PostgreSQL 7.2
- 可以指定返回的
time的精度(提交 bd97e4e7)
- 可以指定返回的
- PostgreSQL 6.2
- 已添加(提交 f10b6392)
示例
current_time 的基本执行示例
postgres=# SELECT current_time;
current_time
--------------------
20:22:38.562496+01
(1 row)
current_time 始终报告与 transaction_timestamp() 相同的时间,并且(如果在隐式事务中执行)与 statement_timestamp() 相同的时间。
postgres=# SELECT
current_time,
transaction_timestamp(),
statement_timestamp(),
clock_timestamp()\gx
-[ RECORD 1 ]---------+------------------------------
current_time | 20:26:13.979644+01
transaction_timestamp | 2021-06-17 20:26:13.979644+01
statement_timestamp | 2021-06-17 20:26:13.979644+01
clock_timestamp | 2021-06-17 20:26:13.980394+01
可以(可选地)指定精度(小数位数)。
postgres=# SELECT current_time(3); current_time ----------------- 20:30:13.684+01 (1 row)
指定的精度必须在 0 和 6 之间;大于 6 的值将被减小到 6。
postgres=# SELECT current_time(99);
WARNING: TIME(99) WITH TIME ZONE precision reduced to maximum allowed, 6
current_time
--------------------
20:31:02.153981+01
(1 row)
current_time() 变体无效。
postgres=# SELECT current_time();
ERROR: syntax error at or near ")"
LINE 1: SELECT current_time();
^
