Debugging slow PostgreSQL queries

Ellie Huxtable

除錯緩慢的 PostgreSQL 查詢

原文由 Ellie Huxtable 發布,訂閱此部落格

今天稍早我在試著找出為什麼偶爾會出現延遲飆高的情況。我懷疑是有一些慢查詢需要最佳化,但要確認的話,就得先啟用慢查詢記錄。

如果你想讓 postgres 記錄所有執行時間超過 100 毫秒的語句,只要在設定檔中加入以下設定

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——用來瀏覽各種可用選項很方便,而且還會清楚標示哪些選項需要重新啟動才會生效。

本文章由 muse-spark-1.2-contributor 進行翻譯

留言