PostgreSQL 獲取當前日期時間及注意事項

作者: 不剪發(fā)的Tony老師
畢業(yè)于北京航空航天大學,十多年數(shù)據(jù)庫管理與開發(fā)經(jīng)驗,目前在一家全球性的金融公司從事數(shù)據(jù)庫架構設計。CSDN學院簽約講師以及GitChat專欄作者。csdn上的博客收藏于以下地址:https://tonydong.blog.csdn.net

文章目錄

        當前日期
            CURRENT_DATE
        當前事務開始時間
            CURRENT_TIME、LOCALTIME、CURRENT_TIMESTAMP、LOCALTIMESTAMP
        當前語句開始時間
            transaction_timestamp()
            statement_timestamp()
            clock_timestamp()
            timeofday()
            now()
        延遲執(zhí)行

在開發(fā)數(shù)據(jù)庫應用或者調試代碼時,經(jīng)常需要獲取系統(tǒng)的當前日期和時間,我們來看一下 PostgreSQL 中提供的相關函數(shù)。
當前日期
CURRENT_DATE

CURRENT_DATE 函數(shù)用于獲取數(shù)據(jù)庫服務器的當前日期:

postgres=# SELECT CURRENT_DATE;
 current_date
--------------
 2019-09-28
(1 row)

 
調用該函數(shù)時不需要在函數(shù)名后加括號。該日期是服務器的日期,不是客戶端的日期。
當前事務開始時間

以下函數(shù)可以用于獲取數(shù)據(jù)庫服務器的當前時間:

CURRENT_TIME
CURRENT_TIME(precision)
LOCALTIME
LOCALTIME(precision)

CURRENT_TIMESTAMP
CURRENT_TIMESTAMP(precision)
LOCALTIMESTAMP
LOCALTIMESTAMP(precision)

   

CURRENT_TIME、LOCALTIME、CURRENT_TIMESTAMP、LOCALTIMESTAMP

前面 4 個函數(shù)用于獲取時間,后面 4 個函數(shù)用于獲取時間戳;CURRENT_TIME 和 CURRENT_TIMESTAMP 包含時區(qū)信息,LOCALTIME 和 LOCALTIMESTAMP 不包含時區(qū)信息。precision 用于指定小數(shù)秒的位數(shù),取值為 0 - 6,默認為 6。

postgres=# SELECT CURRENT_TIME, LOCALTIME, CURRENT_TIMESTAMP, LOCALTIMESTAMP;
    current_time    |    localtime    |       current_timestamp       |       localtimestamp       
--------------------+-----------------+-------------------------------+----------------------------
 12:20:50.602412+08 | 12:20:50.602412 | 2019-09-28 12:20:50.602412+08 | 2019-09-28 12:20:50.602412
(1 row)

postgres=# SELECT CURRENT_TIME(3), LOCALTIME(3), CURRENT_TIMESTAMP(3), LOCALTIMESTAMP(3);
  current_time   |  localtime   |     current_timestamp      |     localtimestamp      
-----------------+--------------+----------------------------+-------------------------
 12:28:03.547+08 | 12:28:03.547 | 2019-09-28 12:28:03.547+08 | 2019-09-28 12:28:03.547
(1 row)



注意:上面所有的函數(shù),包括 CURRENT_DATE,返回的都是當前事務開始的時間。在同一個事務期間,多次調用相同的函數(shù)將會返回相同的值,結果不會隨著時間增加。這一點與其他數(shù)據(jù)庫的實現(xiàn)可能不同。

以下示例使用 pg_sleep 函數(shù)暫停 3 秒再次獲取當前時間:

postgres=# BEGIN;
BEGIN
postgres=# SELECT CURRENT_TIMESTAMP;
       current_timestamp       
-------------------------------
 2019-09-28 12:43:57.075609+08
(1 row)

postgres=# SELECT pg_sleep(3);
 pg_sleep
----------
(1 row)

postgres=# SELECT CURRENT_TIMESTAMP;
       current_timestamp       
-------------------------------
 2019-09-28 12:43:57.075609+08
(1 row)

postgres=# COMMIT;
COMMIT

 

在事務中兩次獲取的時間相同。
當前語句開始時間

PostgreSQL 還提供了其他獲取時間的函數(shù):

transaction_timestamp()
statement_timestamp()
clock_timestamp()
timeofday()
now()

transaction_timestamp()

transaction_timestamp() 等價于 CURRENT_TIMESTAMP,但是作用更加明確。
statement_timestamp()

statement_timestamp() 返回當前語句的開始時間,更準確地說,應該是接收到客戶端最新命令的時間。statement_timestamp() 和 transaction_timestamp() 對于事務中的第一個命令返回的結果相同,但隨后再執(zhí)行 statement_timestamp() 將會返回不同的值。

postgres=# BEGIN;
BEGIN
postgres=# SELECT statement_timestamp();
      statement_timestamp      
-------------------------------
 2019-09-28 13:11:14.497135+08
(1 row)

postgres=# SELECT pg_sleep(3);
 pg_sleep
----------
(1 row)

postgres=# SELECT statement_timestamp();
     statement_timestamp     
-----------------------------
 2019-09-28 13:11:17.5141+08
(1 row)

postgres=# COMMIT;
COMMIT



兩次執(zhí)行結果之間相差了 3 秒左右。

當我們在存儲過程(Stored Procedure)中進行調試時,通常需要打印不同語句消耗的時間;此時就需要使用 statement_timestamp(),而不能使用 CURRENT_TIMESTAMP 或者 transaction_timestamp():

CREATE OR REPLACE sp_test
...
DECLARE
  lts_systimestamp timestamp;
BEGIN;
  lts_systimestamp := statement_timestamp();
  ...
  RAISE NOTICE 'Step 1 take time: %', statement_timestamp() - lts_systimestamp;
  ...
END;

    
clock_timestamp()

clock_timestamp() 返回當前實際的時間,即使在同一個 SQL 語句中也可能返回不同的值:

postgres=# SELECT clock_timestamp() FROM generate_series(1,10);
        clock_timestamp        
-------------------------------
 2019-09-28 13:18:55.659778+08
 2019-09-28 13:18:55.659786+08
 2019-09-28 13:18:55.659788+08
 2019-09-28 13:18:55.65979+08
 2019-09-28 13:18:55.659791+08
 2019-09-28 13:18:55.659793+08
 2019-09-28 13:18:55.659795+08
 2019-09-28 13:18:55.659797+08
 2019-09-28 13:18:55.659799+08
 2019-09-28 13:18:55.659801+08
(10 rows)



查詢語句在 1 秒鐘內(nèi)返回了 10 條記錄,但是每條記錄產(chǎn)生的時間都不相同。
timeofday()

imeofday() 是 PostgreSQL 中一個歷史遺留函數(shù)。它與 clock_timestamp() 一樣返回當前實際時間,但是返回類型是一個格式化的字符串,而不是 timestamp with time zone:

postgres=# SELECT timeofday() FROM generate_series(1,10);
              timeofday              
-------------------------------------
 Sat Sep 28 13:23:05.068541 2019 CST
 Sat Sep 28 13:23:05.068570 2019 CST
 Sat Sep 28 13:23:05.068577 2019 CST
 Sat Sep 28 13:23:05.068584 2019 CST
 Sat Sep 28 13:23:05.068591 2019 CST
 Sat Sep 28 13:23:05.068598 2019 CST
 Sat Sep 28 13:23:05.068605 2019 CST
 Sat Sep 28 13:23:05.068612 2019 CST
 Sat Sep 28 13:23:05.068619 2019 CST
 Sat Sep 28 13:23:05.068626 2019 CST
(10 rows)


now()

now() 是 PostgreSQL 中與 transaction_timestamp() 等價的一個傳統(tǒng)函數(shù),同一個事務中的結果不會改變:

postgres=# BEGIN;
BEGIN
postgres=# SELECT now();
              now              
-------------------------------
 2019-09-28 13:27:26.831492+08
(1 row)

postgres=# SELECT pg_sleep(3);
 pg_sleep
----------
(1 row)

postgres=# SELECT now();
              now              
-------------------------------
 2019-09-28 13:27:26.831492+08
(1 row)

postgres=# COMMIT;
COMMIT


另外,所有的日期/時間數(shù)據(jù)類型都支持使用字面值'now'指定當前日期和時間(當前事務開始時間)。因此,以下語句效果相同:

SELECT CURRENT_TIMESTAMP;
SELECT now();
SELECT TIMESTAMP 'now';  -- 不要用于字段的 DEFAULT 值

    
順便說一下,PostgreSQL 還提供了其他幾個特殊的日期和時間字面值:

-- SELECT timestamp 'epoch', timestamp 'today', timestamp 'tomorrow', timestamp 'yesterday', TIME 'allballs';
postgres=# SELECT DATE 'epoch', DATE 'today',DATE 'tomorrow', DATE 'yesterday', TIME 'allballs';
    date    |    date    |    date    |    date    |   time   
------------+------------+------------+------------+----------
 1970-01-01 | 2019-09-28 | 2019-09-29 | 2019-09-27 | 00:00:00
(1 row)

    
以上函數(shù)分別返回 UTC 1970 年 1 月 1 日零點、今天午夜、明天午夜、昨天午夜以及 UTC 零點。
延遲執(zhí)行

以下函數(shù)可以用于延遲服務器進行的操作:

pg_sleep(seconds)
pg_sleep_for(interval)
pg_sleep_until(timestamp with time zone)

   
pg_sleep 將當前會話的進行暫停指定的秒數(shù)。seconds 的類型為 double precision,所以支持小數(shù)秒。我們在面前使用了該函數(shù)。

pg_sleep_for 執(zhí)行一個延遲的時間間隔,通常用于指定一個較大的延遲。

pg_sleep_until 可以用于指定一個進程的喚醒時間。

以下示例分別暫停 1.5 秒、5 分鐘以及直到明天 3 點:

SELECT pg_sleep(1.5);
SELECT pg_sleep_for('5 minutes');
SELECT pg_sleep_until('tomorrow 03:00');

暫停時間的精度取決于不同平臺的實現(xiàn),通??梢赃_到 0.01 秒。延遲效果最少會滿足指定的值,但有可能由于其他因素導致更長,例如服務器負載過高。尤其對于 pg_sleep_until,不能保證在完全準確的指定時間喚醒進程,但是也不會提前喚醒。

注意:使用這些延遲函數(shù)時,確保當前會話沒有鎖定過多的資源;否則,其他會話將會一直等待,導致系統(tǒng)性能的下降。