適用於 PostgreSQL 的 Azure 資料庫彈性伺服器中的自適應自動清理

本文說明 adaptive_autovacuum 適用於 PostgreSQL 的 Azure 資料庫 靈活伺服器所支援的參數:

  • adaptive_autovacuum.optimize_configurations:可自動調整一系列自動吸塵設定。
  • adaptive_autovacuum.open_transaction_threshold:設定存留期閾值 (以秒為單位),以偵測並緩解舊的已準備交易。

每個參數的運作方式

adaptive_autovacuum.optimize_configurations

當你將此參數設定為 on時,調諧服務會定期:

  1. 收集並彙整工作負載與資料表統計訊號。
  2. 評估規則工作流程。
  3. 計算候選的自動真空參數更新。
  4. 套用更新並重新載入引擎設定。
  5. 撰寫審計記錄。

如果未符合任何規則條件,執行程序可以在沒有任何變更的情況下完成。

目前可調整參數:

  • autovacuum_vacuum_cost_limit
  • autovacuum_vacuum_threshold
  • autovacuum_vacuum_scale_factor
  • autovacuum_vacuum_cost_delay
  • autovacuum_analyze_scale_factor

注意

  • 如果你設 autovacuum_vacuum_cost_limit 為 -1,邏輯會從 vacuum_cost_limit衍生出來。
  • 調諧服務會把 autovacuum_freeze_max_age 它當作輸入訊號使用,但不會直接調諧。

微調參數的可見性與覆寫行為

當此功能改變五個目標參數(autovacuum_vacuum_cost_limit, , autovacuum_vacuum_thresholdautovacuum_vacuum_scale_factor, autovacuum_vacuum_cost_delay, autovacuum_analyze_scale_factor) 中的任一時:

  • 控制平面的 Configurations 端點(例如 GET Configurations API 或 az postgres flexible-server parameter CLI 指令群組)不會顯示這些有效執行時的變更。
  • 要查看有效值,請對 PostgreSQL 端點使用資料平面查詢(例如 SHOW <guc_name> ,或 SELECT name, setting FROM pg_settings WHERE name IN (...))。

使用者覆寫語意:

  • 你可以直接透過入口網站、REST API、CLI 或任何支援的 SDK 設定這五個參數中的任一。
  • 如果你將參數設為與控制平面 Configurations 端點目前回傳的值相同,該操作會被視為一個 no-op,且不會有新的有效變更。
  • 使用者設定的值會覆寫在套用時所套用的功能值。
  • 若 adaptive_autovacuum.optimize_configurations 保持啟用,後續的調整迭代可根據規則評估再次套用新值。

adaptive_autovacuum.open_transaction_threshold

此參數會控制孤立的已準備交易緩解:

  • 0 代表被停用。
  • 大於 0 表示已啟用,且門檻值以秒為單位。

啟用後,若最舊的已準備交易超過門檻,孤立交易處理器會評估資格並復原舊的已準備交易。 處理器會在參數變更時更新其記憶體內的閾值,因此行為會跟隨最新的值。

重要時間細節:

  • 此功能首先決定最早準備的交易時間戳記。
  • 這種偵測是基於投票的,而非連續的。
  • 系統每隔 1,800 秒輪詢一次 (也就是每 30 分鐘一次),因此只有在某次輪詢觀察到一筆已處於已準備狀態且存在時間夠久的交易後,才能開始進行緩解。

執行時與排程行為

  • optimize_configurations調音頻率為每30分鐘一次。
  • 當你開啟 optimize_configurations 時,系統會立即執行一次調校,之後會持續依排程執行。
  • 對 open_transaction_threshold 的處理是由對已準備交易的觀察結果所驅動。 緩解措施僅在年齡檢查超過門檻時啟動。
  • open_transaction_threshold 的已準備交易觀察資料每 300 秒更新一次。

啟用後,何時會建立 intelligentperformance 結構描述?

intelligentperformance結構描述建立於 azure_sys 資料庫之下。 它的建立並非立即完成,而是由負責保存自適應自動清理所用統計資料的功能來處理,而不是由初始微調執行程序本身完成。 通常,該結構會在啟用功能後的0至30分鐘內建立。

你可以透過將 adaptive_autovacuum.optimize_configurations 設為 on,或將 adaptive_autovacuum.open_transaction_threshold 設定為非零值(也就是 > 0)來啟用自適應 autovacuum。 當功能啟用並開始收集所需的統計資料時,架構便會被建立。

若未達成某些前提條件,製作可能會延遲或跳過。

限制與前提條件

兩種管制均須遵守以下要求:

  • 執行個體必須是主要執行個體。
  • PostgreSQL 目前沒有恢復模式。
  • 伺服器的運算至少有 4 個 vCorre。
  • 伺服器是一般的彈性伺服器,而非彈性叢集。 這個功能在彈性叢集上不支援。
  • adaptive_autovacuum.optimize_configurations 支援大於或等於 14 的主要版本。
  • adaptive_autovacuum.open_transaction_threshold 支援大於或等於 13 的主要版本。

審計與可觀察性

系統會以一個名為 intelligentperformance.adaptive_tuning_events的稽核檢視方式記錄兩個控制項的操作。

視圖結構

預期邏輯形狀:

  • intelligentperformance.adaptive_tuning_events
    • 事件詳細資料
    • 優化器類型
    • applied_at

查詢近期活動

要查詢近期活動,請使用以下查詢:

SELECT
    applied_at,
    optimizer_type,
    event_details
FROM intelligentperformance.adaptive_tuning_events
ORDER BY applied_at DESC
LIMIT 100;
SELECT
    optimizer_type,
    COUNT(*) AS events
FROM intelligentperformance.adaptive_tuning_events
WHERE applied_at >= now() - interval '7 days'
GROUP BY optimizer_type
ORDER BY events DESC;

事件類型與詮釋

欄位 optimizer_type 表示動作來源。

自適應自動清理設定已變更

來源:

  • optimize_configurations 調音路徑。

酬載形狀:

  • JSON 參數變更記錄陣列,通常包括:
    • server_parameter_name
    • 先前值
    • updated_tuned_value

解讀方式:

  • 表示自動清理微調已變更伺服器設定。
  • 當後續工作負載指標改善(死元組壓力、真空踏頻、延遲、CPU/IO)時,為正數。
  • 當變化頻繁且振盪,且隨後出現迴歸時,則可能為負值。
SELECT
    applied_at,
    elem->>'server_parameter_name' AS parameter_name,
    elem->>'previous_value' AS previous_value,
    elem->>'updated_tuned_value' AS updated_value
FROM intelligentperformance.adaptive_tuning_events e
CROSS JOIN LATERAL jsonb_array_elements(e.event_details) AS elem
WHERE e.optimizer_type = 'adaptive_autovacuum_configuration_changed'
ORDER BY applied_at DESC;

orphan_transaction_rollback

來源:

  • open_transaction_threshold 緩解路徑。

酬載形狀:

  • 以資料庫為索引鍵的 JSON 物件,包含復原結果的詳細資料 (例如嘗試與成功的 GID 復原資訊)。
{
    "<example-database-name>": {
        "Timestamp": "2026-03-22T18:20:22.0000000Z", 
        "RollbackGids": ["<example-global-identifier-one>", "<example-global-identifier-two>"], 
        "OperationSuccessful": true
    }
}

解讀方式:

  • 表示已準備交易在超過閾值後被強制復原。
  • 偶爾發生的事件可以是健康的保護行為。
  • 頻繁事件通常表示應用程式交易生命週期問題,應予調查。
SELECT
    applied_at,
    jsonb_pretty(event_details) AS rollback_details
FROM intelligentperformance.adaptive_tuning_events
WHERE optimizer_type = 'orphan_transaction_rollback'
ORDER BY applied_at DESC
LIMIT 50;

判斷影響是正面還是負面

針對 optimize_configurations:

  1. 從 adaptive_autovacuum_configuration_changed 找出變更時間戳記。
  2. 針對無效元組、清理/分析頻率、延遲及 CPU/IO,比較變更後 30 分鐘與 120 分鐘的時間範圍。

針對 open_transaction_threshold:

  1. 識別 orphan_transaction_rollback 事件。
  2. 確認回滾頻率是否低且穩定。
  3. 如果回滾事件持續發生,請調查應用程式行為。
SELECT
    applied_at,
    optimizer_type,
    event_details
FROM intelligentperformance.adaptive_tuning_events
WHERE applied_at >= now() - interval '7 days'
ORDER BY applied_at DESC;

保留與管理

管務部門會使用固定且不可設定的90天保留期限,從稽核儲存中移除舊紀錄。

若要進行長期趨勢分析,請將稽核資料匯出到你自己的可觀察性儲存庫。