> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-statistics.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 19.9. 執行時期統計資訊 \#

### 19.9. 執行時期統計資訊 [#](#runtime-config-statistics) <a href="#runtime-config-statistics" id="runtime-config-statistics"></a>

[19.9.1. 累計查詢與索引統計資訊](#runtime-config-cumulative-statistics)

[19.9.2. 統計資訊監控](#runtime-config-statistics-monitor)

#### 19.9.1. 累計查詢與索引統計資訊 [#](#runtime-config-cumulative-statistics) <a href="#runtime-config-cumulative-statistics" id="runtime-config-cumulative-statistics"></a>

這些參數控制伺服器層級的累計統計系統。 啟用後，收集到的資料可以透過 `pg_stat` 與 `pg_statio` 系列的系統檢視表存取。詳情請參閱[第 27 章](https://docs.postgresql.tw/19/server-administration/monitoring.md)。

`track_activities` (`boolean`)[#](#GUC-TRACK-ACTIVITIES) : 啟用對每個工作階段目前執行中命令的資訊收集， 包括其識別碼以及該命令開始執行的時間。此參數預設為 開啟。請注意，即使啟用此參數，這項資訊也僅對超級使用者、 具備 `pg_read_all_stats` 角色權限的角色， 以及擁有被回報之工作階段的使用者（包括屬於該使用者所具權限之角色的工作階段） 可見，因此不應構成安全性風險。 只有超級使用者以及具備相應 `SET` 權限的使用者可以變更此設定。

`track_activity_query_size` (`integer`)[#](#GUC-TRACK-ACTIVITY-QUERY-SIZE) : 指定為每個作用中工作階段保留、用於儲存目前執行中命令文字的 記憶體量，供 `pg_stat_activity`.`query` 欄位使用。 若此值指定時未帶單位，則以位元組為單位。 預設值為 1024 位元組。 此參數只能在伺服器啟動時設定。

`track_counts` (`boolean`)[#](#GUC-TRACK-COUNTS) : 啟用資料庫活動統計資訊的收集。 此參數預設為開啟，因為 autovacuum 守護程序需要用到收集到的資訊。 只有超級使用者以及具備相應 `SET` 權限的使用者可以變更此設定。

`track_cost_delay_timing` (`boolean`)[#](#GUC-TRACK-COST-DELAY-TIMING) : 啟用以成本為基礎的 vacuum 延遲計時（參閱 [19.10.2 節](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-vacuum.md#runtime-config-resource-vacuum-cost)）。此參數 預設為關閉，因為它會反覆向作業系統查詢 目前時間，在某些平台上可能造成顯著額外負擔。 你可以使用 [pg\_test\_timing](https://docs.postgresql.tw/19/reference/reference-server/pgtesttiming.md) 工具 來測量你的系統上計時功能的額外負擔。以成本為基礎的 vacuum 延遲 計時資訊會顯示在 [`pg_stat_progress_vacuum`](https://docs.postgresql.tw/19/server-administration/monitoring/progress-reporting/vacuum-progress-reporting.md#vacuum-progress-reporting)、 [`pg_stat_progress_analyze`](https://docs.postgresql.tw/19/server-administration/monitoring/progress-reporting/analyze-progress-reporting.md#analyze-progress-reporting)、 在使用 `VERBOSE` 選項時 [VACUUM](https://docs.postgresql.tw/19/reference/sql-commands/sql-vacuum.md) 與 [ANALYZE](https://docs.postgresql.tw/19/reference/sql-commands/sql-analyze.md) 的輸出中， 以及在設定 [log\_autovacuum\_min\_duration](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-logging.md#GUC-LOG-AUTOVACUUM-MIN-DURATION) 時，由 autovacuum 針對自動 vacuum 與自動 analyze 輸出。 只有超級使用者以及具備相應 `SET` 權限的使用者可以變更此設定。

`track_io_timing` (`boolean`)[#](#GUC-TRACK-IO-TIMING) : 啟用資料庫 I/O 等待的計時。此參數預設為 關閉，因為它會反覆向作業系統查詢 目前時間，在某些平台上可能造成顯著額外負擔。 你可以使用 [pg\_test\_timing](https://docs.postgresql.tw/19/reference/reference-server/pgtesttiming.md) 工具 來測量你的系統上計時功能的額外負擔。 I/O 計時資訊會顯示在 [`pg_stat_database`](https://docs.postgresql.tw/19/server-administration/monitoring/monitoring-stats/monitoring-pg-stat-database-view.md#monitoring-pg-stat-database-view)、 [`pg_stat_io`](https://docs.postgresql.tw/19/server-administration/monitoring/monitoring-stats.md#monitoring-pg-stat-io-view)（若 `object` 不是 `wal`）、在使用 [`pg_stat_get_backend_io()`](https://docs.postgresql.tw/19/server-administration/monitoring/monitoring-stats.md#PG-STAT-GET-BACKEND-IO) 函式的輸出中（若 `object` 不是 `wal`）、在使用 `BUFFERS` 選項時 [EXPLAIN](https://docs.postgresql.tw/19/reference/sql-commands/sql-explain.md) 的輸出中、在使用 `VERBOSE` 選項時 [VACUUM](https://docs.postgresql.tw/19/reference/sql-commands/sql-vacuum.md) 的輸出中、 在設定 [log\_autovacuum\_min\_duration](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-logging.md#GUC-LOG-AUTOVACUUM-MIN-DURATION) 時由 autovacuum 針對自動 vacuum 與自動 analyze 輸出， 以及在 [pg\_stat\_statements](https://docs.postgresql.tw/19/appendixes/contrib/pgstatstatements.md) 中。 只有超級使用者以及具備相應 `SET` 權限的使用者可以變更此設定。

`track_wal_io_timing` (`boolean`)[#](#GUC-TRACK-WAL-IO-TIMING) : 啟用 WAL I/O 等待的計時。此參數預設為關閉， 因為它會反覆向作業系統查詢目前時間， 在某些平台上可能造成顯著額外負擔。 你可以使用 pg\_test\_timing 工具 來測量你的系統上計時功能的額外負擔。 I/O 計時資訊會顯示在 `object` 為 `wal` 的 [`pg_stat_io`](https://docs.postgresql.tw/19/server-administration/monitoring/monitoring-stats.md#monitoring-pg-stat-io-view) 中，以及 `object` 為 `wal` 時 [`pg_stat_get_backend_io()`](https://docs.postgresql.tw/19/server-administration/monitoring/monitoring-stats.md#PG-STAT-GET-BACKEND-IO) 函式的輸出中。 只有超級使用者以及具備相應 `SET` 權限的使用者可以變更此設定。

`track_functions` (`enum`)[#](#GUC-TRACK-FUNCTIONS)

啟用函式呼叫次數與使用時間的追蹤。指定 `pl` 僅追蹤程序語言（procedural-language）函式， `all` 則同時追蹤 SQL 與 C 語言函式。 預設值為 `none`，即停用函式 統計追蹤。 只有超級使用者以及具備相應 `SET` 權限的使用者可以變更此設定。

#### 注意 [#](#runtime-config-statistics-restored-note-1) <a href="#runtime-config-statistics-restored-note-1" id="runtime-config-statistics-restored-note-1"></a>

簡單到可以被「內聯（inline）」進呼叫查詢的 SQL 語言函式， 無論此設定為何，都不會被追蹤。

`stats_fetch_consistency` (`enum`)[#](#GUC-STATS-FETCH-CONSISTENCY)

決定在同一交易中多次存取累計統計資訊時的行為。 當設為 `none` 時，每次存取都會重新從 共享記憶體擷取計數器。當設為 `cache` 時， 首次存取某物件的統計資訊時，會快取該統計資訊直到交易結束， 除非呼叫了 `pg_stat_clear_snapshot()`。 當設為 `snapshot` 時，首次存取統計資訊時， 會快取目前資料庫中可存取的所有統計資訊，直到 交易結束，除非呼叫了 `pg_stat_clear_snapshot()`。在交易中變更此 參數會捨棄該統計資訊快照。 預設值為 `cache`。

#### 注意 [#](#runtime-config-statistics-restored-note-2) <a href="#runtime-config-statistics-restored-note-2" id="runtime-config-statistics-restored-note-2"></a>

`none` 最適合用於監控系統。若 數值僅會被存取一次，此選項是最有 效率的。`cache` 可確保重複存取時取得 相同的數值，這對於涉及例如自我聯結（self-join）的查詢很重要。 `snapshot` 在互動式檢視統計資訊時很有用， 但額外負擔較高，尤其是在存在許多資料庫物件時。

#### 19.9.2. 統計資訊監控 [#](#runtime-config-statistics-monitor) <a href="#runtime-config-statistics-monitor" id="runtime-config-statistics-monitor"></a>

`compute_query_id` (`enum`)[#](#GUC-COMPUTE-QUERY-ID)

啟用核心內建的查詢識別碼計算功能。 查詢識別碼可以顯示在 [`pg_stat_activity`](https://docs.postgresql.tw/19/server-administration/monitoring/monitoring-stats.md#monitoring-pg-stat-activity-view) 檢視表中、透過 `EXPLAIN` 顯示，或若透過 [log\_line\_prefix](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-logging.md#GUC-LOG-LINE-PREFIX) 參數設定，也可以輸出到日誌中。 [pg\_stat\_statements](https://docs.postgresql.tw/19/appendixes/contrib/pgstatstatements.md) 延伸模組同樣需要 計算查詢識別碼。請注意，如果核心內建的查詢識別碼 計算方式不被接受，也可以改用外部模組。在這種情況下， 必須永遠停用核心內建的計算功能。 合法的值有 `off`（永遠停用）、 `on`（永遠啟用）、`auto` （讓 [pg\_stat\_statements](https://docs.postgresql.tw/19/appendixes/contrib/pgstatstatements.md) 等模組 自動啟用此功能），以及 `regress`（效果 與 `auto` 相同，只是為了方便自動化迴歸測試， 查詢識別碼不會顯示在 `EXPLAIN` 輸出中）。 預設值為 `auto`。

#### 注意 [#](#runtime-config-statistics-restored-note-3) <a href="#runtime-config-statistics-restored-note-3" id="runtime-config-statistics-restored-note-3"></a>

為確保只計算並顯示一個查詢識別碼， 計算查詢識別碼的延伸模組在查詢識別碼已經 被計算過的情況下應該要拋出錯誤。

`log_statement_stats` (`boolean`)\
`log_parser_stats` (`boolean`)\
`log_planner_stats` (`boolean`)\
`log_executor_stats` (`boolean`)[#](#GUC-LOG-STATEMENT-STATS) : 針對每個查詢，將對應模組的效能統計資訊輸出到伺服器日誌。 這是一種簡陋的效能剖析工具，類似 Unix 的 `getrusage()` 作業系統機制。`log_statement_stats` 回報整體陳述式統計資訊，而其他選項則回報各模組的統計資訊。 `log_statement_stats` 無法與任何各模組選項 同時啟用。這些選項預設皆為停用。 只有超級使用者以及具備相應 `SET` 權限的使用者可以變更這些設定。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/runtime-config-statistics.html)（原文版本：18.6；核對日期：2026-09-22）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-statistics.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
