十月 25, 2023
摘要:PostgreSQL 的statement_timestamp()
函数,用于返回当前带时区的日期和时间,即当前语句开始的时间。
目录
一个返回当前查询开始的时间点的函数。
statement_timestamp()
是一个系统函数,它返回一个表示当前语句开始的时间点的时间戳。
用法
statement_timestamp ( ) → timestamp with time zone
返回值是客户端收到最新命令消息时的时间。如果它是事务中的第一个且唯一的语句,则返回的值会与transaction_timestamp()
或current_timestamp
返回的值相同。
示例
基本的statement_timestamp()
执行示例:
postgres=# SELECT statement_timestamp();
statement_timestamp
-------------------------------
2021-06-17 17:51:41.791603+01
(1 row)
对于在事务块以外执行的语句,statement_timestamp()
返回的时间戳与transaction_timestamp()
返回的时间戳相同:
postgres=# SELECT
transaction_timestamp(),
statement_timestamp(),
clock_timestamp(),
clock_timestamp()\gx
-[ RECORD 1 ]---------+------------------------------
transaction_timestamp | 2021-06-17 18:06:45.891294+01
statement_timestamp | 2021-06-17 18:06:45.891294+01
clock_timestamp | 2021-06-17 18:06:45.891497+01
clock_timestamp | 2021-06-17 18:06:45.891497+01
在一个事务块中执行相同的查询:
postgres=# BEGIN;
BEGIN
postgres=# SELECT
transaction_timestamp(),
statement_timestamp(),
clock_timestamp(),
clock_timestamp()\gx
-[ RECORD 1 ]---------+------------------------------
transaction_timestamp | 2021-06-17 18:09:28.23169+01
statement_timestamp | 2021-06-17 18:09:30.726471+01
clock_timestamp | 2021-06-17 18:09:30.726765+01
clock_timestamp | 2021-06-17 18:09:30.726766+01
了解更多
PostgreSQL 教程:日期函数
PostgreSQL 文档:时间/日期函数和操作符