在開發數據庫應用或者調試代碼時,經常需要獲取系統的當前日期和時間,我們來看一下 PostgreSQL 中提供的相關函數。
當前日期
CURRENT_DATE
CURRENT_DATE 函數用于獲取數據庫服務器的當前日期:
1
2
3
4
5
|
postgres=# SELECT CURRENT_DATE ; current_date -------------- 2019-09-28 (1 row) |
調用該函數時不需要在函數名后加括號。該日期是服務器的日期,不是客戶端的日期。
當前事務開始時間
以下函數可以用于獲取數據庫服務器的當前時間:
1
2
3
4
5
6
7
8
9
|
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。
1
2
3
4
5
6
7
8
9
10
11
|
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 秒再次獲取當前時間:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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 還提供了其他獲取時間的函數:
1
2
3
4
5
|
transaction_timestamp() statement_timestamp() clock_timestamp() timeofday() now() |
transaction_timestamp()
transaction_timestamp() 等價于 CURRENT_TIMESTAMP,但是作用更加明確。
statement_timestamp()
statement_timestamp() 返回當前語句的開始時間,更準確地說,應該是接收到客戶端最新命令的時間。statement_timestamp() 和 transaction_timestamp() 對于事務中的第一個命令返回的結果相同,但隨后再執行 statement_timestamp() 將會返回不同的值。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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():
1
2
3
4
5
6
7
8
9
10
|
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 語句中也可能返回不同的值:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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() 等價的一個傳統函數,同一個事務中的結果不會改變:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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'指定當前日期和時間(當前事務開始時間)。因此,以下語句效果相同:
1
2
3
|
SELECT CURRENT_TIMESTAMP ; SELECT now(); SELECT TIMESTAMP 'now' ; -- 不要用于字段的 DEFAULT 值 |
順便說一下,PostgreSQL 還提供了其他幾個特殊的日期和時間字面值:
1
2
3
4
5
6
|
-- 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 零點。
延遲執行
以下函數可以用于延遲服務器進行的操作:
1
2
3
|
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 點:
1
2
3
|
SELECT pg_sleep(1.5); SELECT pg_sleep_for( '5 minutes' ); SELECT pg_sleep_until( 'tomorrow 03:00' ); |
暫停時間的精度取決于不同平臺的實現,通常可以達到 0.01 秒。延遲效果最少會滿足指定的值,但有可能由于其他因素導致更長,例如服務器負載過高。尤其對于 pg_sleep_until,不能保證在完全準確的指定時間喚醒進程,但是也不會提前喚醒。
注意:使用這些延遲函數時,確保當前會話沒有鎖定過多的資源;否則,其他會話將會一直等待,導致系統性能的下降。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/horses/article/details/102581249