除錯 PostgreSQL 慢查詢
今天稍早,我一直在試圖找出偶爾出現延遲突波的原因。我懷疑有一些慢查詢需要最佳化,但為了確認,我需要啟用慢查詢日誌功能。
如果你想在 postgres 中記錄所有執行時間超過 100ms 的陳述式,請在設定檔中加入以下內容:
log_min_duration_statement = 100然後使用下列指令重新載入 postgres 設定:
select pg_reload_conf();無需重新啟動伺服器!
另外,下列選項也可能有所幫助:
# Log all statements, not just the slow ones
# This may be very chatty, so make sure your log partition doesn't fill up
log_statement = all/ddl/none# Log durations alongside statements
log_duration = on在之前的工作中,我發現 pganalyze 對於分析 postgres 效能非常有幫助。它確實需要一些額外的設定步驟,而且不是免費的。如果你高度依賴 postgres,我非常推薦它!
另外,我最喜歡用來查詢 postgres 設定選項的資源是 postgresqlco.nf——它很適合用來瀏覽可用的選項,而且會清楚標示哪些選項需要重新啟動。
隨機一篇部落格