在開發(fā)數(shù)據(jù)庫應(yīng)用或者調(diào)試代碼時(shí),經(jīng)常需要獲取系統(tǒng)的當(dāng)前日期和時(shí)間,我們來看一下 PostgreSQL 中提供的相關(guān)函數(shù)。
當(dāng)前日期
CURRENT_DATE
CURRENT_DATE 函數(shù)用于獲取數(shù)據(jù)庫服務(wù)器的當(dāng)前日期:
postgres=# SELECT CURRENT_DATE; current_date -------------- 2019-09-28 (1 row)
調(diào)用該函數(shù)時(shí)不需要在函數(shù)名后加括號(hào)。該日期是服務(wù)器的日期,不是客戶端的日期。
當(dāng)前事務(wù)開始時(shí)間
以下函數(shù)可以用于獲取數(shù)據(jù)庫服務(wù)器的當(dāng)前時(shí)間:
CURRENT_TIME CURRENT_TIME(precision) LOCALTIME LOCALTIME(precision) CURRENT_TIMESTAMP CURRENT_TIMESTAMP(precision) LOCALTIMESTAMP LOCALTIMESTAMP(precision)
CURRENT_TIME、LOCALTIME、CURRENT_TIMESTAMP、LOCALTIMESTAMP
前面 4 個(gè)函數(shù)用于獲取時(shí)間,后面 4 個(gè)函數(shù)用于獲取時(shí)間戳;CURRENT_TIME 和 CURRENT_TIMESTAMP 包含時(shí)區(qū)信息,LOCALTIME 和 LOCALTIMESTAMP 不包含時(shí)區(qū)信息。precision 用于指定小數(shù)秒的位數(shù),取值為 0 - 6,默認(rèn)為 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,返回的都是當(dāng)前事務(wù)開始的時(shí)間。在同一個(gè)事務(wù)期間,多次調(diào)用相同的函數(shù)將會(huì)返回相同的值,結(jié)果不會(huì)隨著時(shí)間增加。這一點(diǎn)與其他數(shù)據(jù)庫的實(shí)現(xiàn)可能不同。
以下示例使用 pg_sleep 函數(shù)暫停 3 秒再次獲取當(dāng)前時(shí)間:
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
在事務(wù)中兩次獲取的時(shí)間相同。
當(dāng)前語句開始時(shí)間
PostgreSQL 還提供了其他獲取時(shí)間的函數(shù):
transaction_timestamp() statement_timestamp() clock_timestamp() timeofday() now()
transaction_timestamp()
transaction_timestamp() 等價(jià)于 CURRENT_TIMESTAMP,但是作用更加明確。
statement_timestamp()
statement_timestamp() 返回當(dāng)前語句的開始時(shí)間,更準(zhǔn)確地說,應(yīng)該是接收到客戶端最新命令的時(shí)間。statement_timestamp() 和 transaction_timestamp() 對于事務(wù)中的第一個(gè)命令返回的結(jié)果相同,但隨后再執(zhí)行 statement_timestamp() 將會(huì)返回不同的值。
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í)行結(jié)果之間相差了 3 秒左右。
當(dāng)我們在存儲(chǔ)過程(Stored Procedure)中進(jìn)行調(diào)試時(shí),通常需要打印不同語句消耗的時(shí)間;此時(shí)就需要使用 statement_timestamp(),而不能使用 CURRENT_TIMESTAMP 或者 transaction_timestamp():
CREATE OR REPLACE sp_test ... DECLARE lts_systimestamp timest BEGIN; lts_systimestamp := statement_timestamp(); ... RAISE NOTICE 'Step 1 take time: %', statement_timestamp() - lts_systimest ... END;
clock_timestamp()
clock_timestamp() 返回當(dāng)前實(shí)際的時(shí)間,即使在同一個(gè) 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)生的時(shí)間都不相同。
timeofday()
timeofday() 是 PostgreSQL 中一個(gè)歷史遺留函數(shù)。它與 clock_timestamp() 一樣返回當(dāng)前實(shí)際時(shí)間,但是返回類型是一個(gè)格式化的字符串,而不是 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() 等價(jià)的一個(gè)傳統(tǒng)函數(shù),同一個(gè)事務(wù)中的結(jié)果不會(huì)改變:
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í)間數(shù)據(jù)類型都支持使用字面值'now'指定當(dāng)前日期和時(shí)間(當(dāng)前事務(wù)開始時(shí)間)。因此,以下語句效果相同:
SELECT CURRENT_TIMESTAMP; SELECT now(); SELECT TIMESTAMP 'now'; -- 不要用于字段的 DEFAULT 值
順便說一下,PostgreSQL 還提供了其他幾個(gè)特殊的日期和時(shí)間字面值:
-- 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 日零點(diǎn)、今天午夜、明天午夜、昨天午夜以及 UTC 零點(diǎn)。
延遲執(zhí)行
以下函數(shù)可以用于延遲服務(wù)器進(jìn)行的操作:
pg_sleep(seconds) pg_sleep_for(interval) pg_sleep_until(timestamp with time zone)
pg_sleep 將當(dāng)前會(huì)話的進(jìn)行暫停指定的秒數(shù)。seconds 的類型為 double precision,所以支持小數(shù)秒。我們在面前使用了該函數(shù)。
pg_sleep_for 執(zhí)行一個(gè)延遲的時(shí)間間隔,通常用于指定一個(gè)較大的延遲。
pg_sleep_until 可以用于指定一個(gè)進(jìn)程的喚醒時(shí)間。
以下示例分別暫停 1.5 秒、5 分鐘以及直到明天 3 點(diǎn):
SELECT pg_sleep(1.5); SELECT pg_sleep_for('5 minutes'); SELECT pg_sleep_until('tomorrow 03:00');
暫停時(shí)間的精度取決于不同平臺(tái)的實(shí)現(xiàn),通??梢赃_(dá)到 0.01 秒。延遲效果最少會(huì)滿足指定的值,但有可能由于其他因素導(dǎo)致更長,例如服務(wù)器負(fù)載過高。尤其對于 pg_sleep_until,不能保證在完全準(zhǔn)確的指定時(shí)間喚醒進(jìn)程,但是也不會(huì)提前喚醒。
注意:使用這些延遲函數(shù)時(shí),確保當(dāng)前會(huì)話沒有鎖定過多的資源;否則,其他會(huì)話將會(huì)一直等待,導(dǎo)致系統(tǒng)性能的下降。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
標(biāo)簽:衡陽 來賓 蚌埠 晉城 烏海 株洲 珠海 錦州
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PostgreSQL 如何獲取當(dāng)前日期時(shí)間及注意事項(xiàng)》,本文關(guān)鍵詞 PostgreSQL,如何,獲取,當(dāng)前,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。