PostgreSQL 如何獲取當前日期時間及注意事項
在開發資料庫應用或者除錯程式碼時,經常需要獲取系統的當前日期和時間,我們來看一下 PostgreSQL 中提供的相關函式。
當前日期
CURRENT_DATE
CURRENT_DATE 函式用於獲取資料庫伺服器的當前日期:
postgres=# SELECT CURRENT_DATE; current_date -------------- 2019-09-28 (1 row)
呼叫該函式時不需要在函式名後加括號。該日期是伺服器的日期,不是客戶端的日期。
當前事務開始時間
以下函式可以用於獲取資料庫伺服器的當前時間:
CURRENT_TIME CURRENT_TIME(precision) LOCALTIME LOCALTIME(precision) CURRENT_TIMESTAMP CURRENT_TIMESTAMP(precision) LOCALTIMESTAMP LOCALTIMESTAMP(precision)
CURRENT_TIME、LOCALTIME、CURRENT_TIMESTAMP、LOCALTIMESTAMP
前面 4 個函式用於獲取時間,後面 4 個函式用於獲取時間戳;CURRENT_TIME 和 CURRENT_TIMESTAMP 包含時區資訊,LOCALTIME 和 LOCALTIMESTAMP 不包含時區資訊。precision 用於指定小數秒的位數,取值為 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)
注意:上面所有的函式,包括 CURRENT_DATE,返回的都是當前事務開始的時間。在同一個事務期間,多次呼叫相同的函式將會返回相同的值,結果不會隨著時間增加。這一點與其他資料庫的實現可能不同。
以下示例使用 pg_sleep 函式暫停 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 還提供了其他獲取時間的函式:
transaction_timestamp() statement_timestamp() clock_timestamp() timeofday() now()
transaction_timestamp()
transaction_timestamp() 等價於 CURRENT_TIMESTAMP,但是作用更加明確。
statement_timestamp()
statement_timestamp() 返回當前語句的開始時間,更準確地說,應該是接收到客戶端最新命令的時間。statement_timestamp() 和 transaction_timestamp() 對於事務中的第一個命令返回的結果相同,但隨後再執行 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
兩次執行結果之間相差了 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 秒鐘內返回了 10 條記錄,但是每條記錄產生的時間都不相同。
timeofday()
timeofday() 是 PostgreSQL 中一個歷史遺留函式。它與 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() 等價的一個傳統函式,同一個事務中的結果不會改變:
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
另外,所有的日期/時間資料型別都支援使用字面值'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)
以上函式分別返回 UTC 1970 年 1 月 1 日零點、今天午夜、明天午夜、昨天午夜以及 UTC 零點。
延遲執行
以下函式可以用於延遲伺服器進行的操作:
pg_sleep(seconds) pg_sleep_for(interval) pg_sleep_until(timestamp with time zone)
pg_sleep 將當前會話的進行暫停指定的秒數。seconds 的型別為 double precision,所以支援小數秒。我們在面前使用了該函式。
pg_sleep_for 執行一個延遲的時間間隔,通常用於指定一個較大的延遲。
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');
暫停時間的精度取決於不同平臺的實現,通常可以達到 0.01 秒。延遲效果最少會滿足指定的值,但有可能由於其他因素導致更長,例如伺服器負載過高。尤其對於 pg_sleep_until,不能保證在完全準確的指定時間喚醒程序,但是也不會提前喚醒。
注意:使用這些延遲函式時,確保當前會話沒有鎖定過多的資源;否則,其他會話將會一直等待,導致系統性能的下降。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。