W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
auto_explain
模塊提供了一種方式來自動記錄慢速語句的執(zhí)行計劃,而不需 要手工運行EXPLAIN。這在大型應(yīng)用中追蹤未被優(yōu)化的查詢時有用。
該模塊沒有提供 SQL 可訪問的函數(shù)。要使用它,簡單地將它載入服務(wù)器。你可以把它載入到一個單獨的 會話:
LOAD 'auto_explain';
(你必須作為超級用戶來這樣做)。更典型的用法是通過在postgresql.conf
的session_preload_libraries或shared_preload_libraries參數(shù)中包括
auto_explain
將它預(yù)先 載入到某些或者所有會話中。然后你就可以追蹤那些出乎意料地慢的查詢,而不管它們何時發(fā)生。當(dāng)然為 此會付出一些額外的負荷作為代價。
有幾個配置參數(shù)用來控制auto_explain
的行為。注意默認行為是什么也不做,因此如果你想要任何結(jié)果就必須至少設(shè)置auto_explain.log_min_duration
。
auto_explain.log_min_duration
(integer
)
auto_explain.log_min_duration
是最小語句執(zhí)行時間(以毫秒計),這將導(dǎo)致語句的計劃被記錄。這個參數(shù)設(shè)置為0
時將記錄所有計劃,設(shè)置為-1
(默認值)時禁用記錄計劃。例如,如果你將它設(shè)置為250ms
,則所有運行時間等于或超過 250ms 的語句將被記錄。只有超級用戶能夠改變這個設(shè)置。
auto_explain.log_analyze
(boolean
)
當(dāng)一個執(zhí)行計劃被記錄時,auto_explain.log_analyze
導(dǎo)致EXPLAIN ANALYZE
輸出(而不僅僅是EXPLAIN
輸出)被打印。默認情況下這個參數(shù)是關(guān)閉的。只有超級用戶能夠改變這個設(shè)置。
當(dāng)這個參數(shù)為打開時,對所有被執(zhí)行的語句將引起對每個計劃節(jié)點的計時,不管它們是否運行得足夠長以至于被記錄。這可能對性能有極度負面的影響。
auto_explain.log_buffers
(boolean
)
當(dāng)一個執(zhí)行計劃被記錄時,auto_explain.log_buffers
控制是否打印 緩沖區(qū)使用統(tǒng)計信息;它等效于EXPLAIN
的BUFFERS
選項。除非 auto_explain.log_analyze
參數(shù)被設(shè)置,否則這個參數(shù)沒有效果。這個參數(shù)默 認情況下是關(guān)閉的。只有超級用戶能夠改變這個設(shè)置。
auto_explain.log_wal
(boolean
)
auto_explain.log_wal
控制當(dāng)執(zhí)行計劃被記錄時是否打印WAL使用情況統(tǒng)計信息; 它相當(dāng)于EXPLAIN
的 WAL
選項。 除非啟用了auto_explain.log_analyze
,否則這個參數(shù)不生效。 這個參數(shù)默認是關(guān)閉狀態(tài)。只有超級用戶能夠改變這個設(shè)置。
auto_explain.log_timing
(boolean
)
當(dāng)一個執(zhí)行計劃被記錄時,auto_explain.log_timing
控制是否打印每 個結(jié)點上的計時信息;它等效于EXPLAIN
的TIMING
選項。重復(fù)讀取 系統(tǒng)鎖的開銷在某些系統(tǒng)上可能會顯著地拖慢查詢,因此當(dāng)只需要實際行計數(shù)而非確切時間時,關(guān)閉 這個參數(shù)將會很有幫助。只有當(dāng)auto_explain.log_analyze
也被啟用
時這個參數(shù)才有效。這個參數(shù)默認情況下是打開的。只有超級用戶能夠改變這個設(shè)置。
auto_explain.log_triggers
(boolean
)
當(dāng)一個執(zhí)行計劃被記錄時,auto_explain.log_triggers
會導(dǎo)致觸發(fā) 器執(zhí)行統(tǒng)計信息被包括在內(nèi)。只有當(dāng)auto_explain.log_analyze
也被 啟用時這個參數(shù)才有效。這個參數(shù)默認情況下是關(guān)閉的。只有超級用戶能夠改變這個設(shè)置。
auto_explain.log_verbose
(boolean
)
當(dāng)一個執(zhí)行計劃被記錄時,auto_explain.log_verbose
控制是否打印很長的詳細信息;它等效于EXPLAIN
的VERBOSE
選項。這個參數(shù)默認情況下是關(guān)閉的。只有超級用戶能夠改變這個設(shè)置。
auto_explain.log_settings
(boolean
)
auto_explain.log_settings
控制執(zhí)行計劃被日志記錄時是否打印關(guān)于已修改的配置選項的信息。輸出中僅包含影響查詢計劃的選項,其值與內(nèi)置缺省值不同。該參數(shù)默認關(guān)閉。僅超級用戶可修改該設(shè)置。
auto_explain.log_format
(enum
)
auto_explain.log_format
選擇要使用的EXPLAIN
輸出格式。允許的值是text
、xml
、json
和yaml
。默認是文本形式。只有超級用戶能夠改變這個設(shè)置。
auto_explain.log_level
(enum
)
auto_explain.log_level
選擇日志的級別,在此級別上,auto_explain會記錄查詢計劃。有效值為DEBUG5
, DEBUG4
,DEBUG3
, DEBUG2
, DEBUG1
,
INFO
, NOTICE
, WARNING
和LOG
。缺省為LOG
。僅超級用戶可修改該設(shè)置。
auto_explain.log_nested_statements
(boolean
)
auto_explain.log_nested_statements
導(dǎo)致嵌套語句(在一個函數(shù)內(nèi)執(zhí)行的語句)會被考慮在記錄范圍之內(nèi)。當(dāng)它被關(guān)閉時,只有頂層查詢計劃被記錄。這個參數(shù)默認情況下是關(guān)閉的。只有超級用戶能夠改變這個設(shè)置。
auto_explain.sample_rate
(real
)
auto_explain.sample_rate
會讓 auto_explain 只解釋每個會話中的一部分語句。默認值為 1,表示解釋所有的查詢。在嵌套 語句的情況下,要么所有語句都被解釋,要么一個也不被解釋。只有超級用戶 能夠更改這個設(shè)置。
在普通用法中,這些參數(shù)都在postgresql.conf
中設(shè)置,不過超級用戶可以在他們自己的會話中隨時修改這些參數(shù)。典型的用法可能是:
# postgresql.conf
session_preload_libraries = 'auto_explain'
auto_explain.log_min_duration = '3s'
postgres=# LOAD 'auto_explain';
postgres=# SET auto_explain.log_min_duration = 0;
postgres=# SET auto_explain.log_analyze = true;
postgres=# SELECT count(*)
FROM pg_class, pg_index
WHERE oid = indrelid AND indisunique;
這可能會產(chǎn)生這樣的日志輸出:
LOG: duration: 3.651 ms plan:
Query Text: SELECT count(*)
FROM pg_class, pg_index
WHERE oid = indrelid AND indisunique;
Aggregate (cost=16.79..16.80 rows=1 width=0) (actual time=3.626..3.627 rows=1 loops=1)
-> Hash Join (cost=4.17..16.55 rows=92 width=0) (actual time=3.349..3.594 rows=92 loops=1)
Hash Cond: (pg_class.oid = pg_index.indrelid)
-> Seq Scan on pg_class (cost=0.00..9.55 rows=255 width=4) (actual time=0.016..0.140 rows=255 loops=1)
-> Hash (cost=3.02..3.02 rows=92 width=4) (actual time=3.238..3.238 rows=92 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 4kB
-> Seq Scan on pg_index (cost=0.00..3.02 rows=92 width=4) (actual time=0.008..3.187 rows=92 loops=1)
Filter: indisunique
Takahiro Itagaki <itagaki.takahiro@oss.ntt.co.jp>
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: