PG中的时间current_timestamp、now()和clock_timestamp()
Tags: clock_timestampGreenPlumPG
简介
同一个事务内的当前时间相同:current_timestamp、now();
在事务中随时间的前进而变化:clock_timestamp();
通过now()获取的时间是最完整的时间,包括时区,秒也保留到了6位小数。
CURRENT_TIME和CURRENT_TIMESTAMP传递带有时区的值;
LOCALTIME和LOCALTIMESTAMP传递的值不带时区。
CURRENT_TIME、CURRENT_TIMESTAMP、LOCALTIME和 LOCALTIMESTAMP可以有选择地接受一个精度参数, 该精度导致结果的秒域被园整为指定小数位。如果没有精度参数,结果将被给予所能得到的全部精度。
transaction_timestamp()等价于CURRENT_TIMESTAMP,但是其命名清楚地反映了它的返回值。
statement_timestamp()返回当前语句的开始时刻(更准确的说是收到 客户端最后一条命令的时间)。
statement_timestamp()和transaction_timestamp()在一个事务的第一条命令期间返回值相同,但是在随后的命令中却不一定相同。
clock_timestamp()返回真正的当前时间,因此它的值甚至在同一条 SQL 命令中都会变化。
timeofday()是一个有历史原因的PostgreSQL函数。和clock_timestamp()相似,timeofday()也返回真实的当前时间,但是它的结果是一个格式化的text串,而不是timestamp with time zone值。
now()是PostgreSQL的一个传统,等效于transaction_timestamp()。
示例
1 2 3 4 5 6 7 8 9 10 11 | do $$ begin raise notice 'current_timestamp1:%',current_timestamp; raise notice 'clock_timestamp1:%',clock_timestamp(); raise notice 'now1:%',now(); perform pg_sleep(2); raise notice 'current_timestamp2:%',current_timestamp; raise notice 'clock_timestamp2:%',clock_timestamp(); raise notice 'now2:%',now(); end$$; |
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | do $$ begin raise notice 'current_timestamp1:%',current_timestamp; raise notice 'clock_timestamp1:%',clock_timestamp(); raise notice 'now1:%',now(); perform pg_sleep(2); raise notice 'current_timestamp2:%',current_timestamp; raise notice 'clock_timestamp2:%',clock_timestamp(); raise notice 'now2:%',now(); end$$ > NOTICE: current_timestamp1:2023-02-13 10:11:46.121968+08 > NOTICE: clock_timestamp1:2023-02-13 10:11:46.123773+08 > NOTICE: now1:2023-02-13 10:11:46.121968+08 > NOTICE: current_timestamp2:2023-02-13 10:11:46.121968+08 > NOTICE: clock_timestamp2:2023-02-13 10:11:48.126419+08 > NOTICE: now2:2023-02-13 10:11:46.121968+08 > OK > 查询时间: 2.066s |
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | select now(); select CURRENT_DATE; select CURRENT_TIME; select CURRENT_TIMESTAMP; select CURRENT_TIME(precision); select CURRENT_TIMESTAMP(precision); select LOCALTIME; select LOCALTIMESTAMP; select LOCALTIME(precision); select LOCALTIMESTAMP(precision); --示例 SELECT CURRENT_TIME; 结果:14:39:53.662522-05 SELECT CURRENT_DATE; 结果:2001-12-23 SELECT CURRENT_TIMESTAMP; 结果:2001-12-23 14:39:53.662522-05 SELECT CURRENT_TIMESTAMP(2); 结果:2001-12-23 14:39:53.66-05 SELECT LOCALTIMESTAMP; 结果:2001-12-23 14:39:53.662522 |