時間:2019-12-02來源:系統城作者:電腦系統城
在開發數據庫應用或者調試代碼時,經常需要獲取系統的當前日期和時間,我們來看一下 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 ) |
前面 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) |
以下示例使用 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() 等價于 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 |
當我們在存儲過程(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() 返回當前實際的時間,即使在同一個 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 ) |
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() 是 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 |
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_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' ); |
注意:使用這些延遲函數時,確保當前會話沒有鎖定過多的資源;否則,其他會話將會一直等待,導致系統性能的下降。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
2022-02-25
系統城教小白如何在Centos8-stream安裝PostgreSQL132021-04-22
自定義函數實現單詞排序并運用于PostgreSQL(實現代碼)2021-04-19
MySQL命令行操作時的編碼問題詳解玩游戲的時候最怕跳出什么程序來干擾游戲,很多玩家在玩游戲過程中會遇到輸入法彈出來,影響游戲體驗,Win10玩游戲老跳出來輸入法怎么辦?按照以下的方法操作就可以解決這個問題了...
2021-02-19