> 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/internals/views/view-pg-stats.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 53.29. pg\_stats \#

### 53.29. `pg_stats` [#](#view-pg-stats) <a href="#view-pg-stats" id="view-pg-stats"></a>

檢視表 `pg_stats` 提供對儲存在 [`pg_statistic`](https://docs.postgresql.tw/19/internals/catalogs/catalog-pg-statistic.md) 系統目錄中之資訊的存取。此檢視表只允許存取 [`pg_statistic`](https://docs.postgresql.tw/19/internals/catalogs/catalog-pg-statistic.md) 中對應於使用者有權讀取之資料表的資料列，因此允許公開讀取此檢視表是安全的。

`pg_stats` 也被設計為以比底層系統目錄更易讀的格式呈現資訊——代價是每當為 [`pg_statistic`](https://docs.postgresql.tw/19/internals/catalogs/catalog-pg-statistic.md) 定義新的槽位（slot）類型時，就必須擴充此檢視表的結構。

#### 表 53.29. `pg_stats` 欄位 [#](#view-pg-stats-columns) <a href="#view-pg-stats-columns" id="view-pg-stats-columns"></a>

| <p>欄位類型</p><p>說明</p>                                                                                                                                                                                                               |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>schemaname</code> <code>name</code> （參照 <a href="https://docs.postgresql.tw/19/internals/catalogs/catalog-pg-namespace.md"><code>pg\_namespace</code></a>.<code>nspname</code>）</p><p>包含資料表之綱要的名稱</p>                     |
| <p><code>tablename</code> <code>name</code> （參照 <a href="https://docs.postgresql.tw/19/internals/catalogs/catalog-pg-class.md"><code>pg\_class</code></a>.<code>relname</code>）</p><p>資料表名稱</p>                                    |
| <p><code>attname</code> <code>name</code> （參照 <a href="https://docs.postgresql.tw/19/internals/catalogs/catalog-pg-attribute.md"><code>pg\_attribute</code></a>.<code>attname</code>）</p><p>此資料列所描述之欄位的名稱</p>                      |
| <p><code>inherited</code> <code>bool</code></p><p>若為 true，此資料列包含來自子資料表的值，而不只是指定資料表中的值</p>                                                                                                                                          |
| <p><code>null\_frac</code> <code>float4</code></p><p>欄位項目中為 null 的比例</p>                                                                                                                                                           |
| <p><code>avg\_width</code> <code>int4</code></p><p>欄位項目的平均寬度（以位元組為單位）</p>                                                                                                                                                          |
| <p><code>n\_distinct</code> <code>float4</code></p><p>若大於零，表示欄位中相異值的估計數量。若小於零，則為相異值數量除以資料列數量所得之值的負數。（當 <code>ANALYZE</code> 認為相異值的數量可能會隨著資料表成長而增加時，使用負值形式；當欄位看起來只有固定數量的可能值時，使用正值形式。）例如，-1 表示一個唯一的欄位，其相異值數量與資料列數量相同。</p>            |
| <p><code>most\_common\_vals</code> <code>anyarray</code></p><p>欄位中最常見之值的清單。（若沒有任何值看起來比其他值更常見，則為 null。）</p>                                                                                                                         |
| <p><code>most\_common\_freqs</code> <code>float4\[]</code></p><p>最常見之值的頻率清單，亦即每個值的出現次數除以資料列總數。（當 <code>most\_common\_vals</code> 為 null 時，此欄位也為 null。）</p>                                                                         |
| <p><code>histogram\_bounds</code> <code>anyarray</code></p><p>將欄位的值劃分為數量大約相等之各組的值清單。若有 <code>most\_common\_vals</code>，其中的值不納入此直方圖的計算。（若該欄位的資料型別沒有 <code><</code> 運算子，或 <code>most\_common\_vals</code> 清單已涵蓋全部母體，則此欄位為 null。）</p> |
| <p><code>correlation</code> <code>float4</code></p><p>資料列的實體排列順序與欄位值的邏輯順序之間的統計相關性。其範圍從 -1 到 +1。當此值接近 -1 或 +1 時，由於減少了對磁碟的隨機存取，該欄位上的索引掃描會被估計為比此值接近零時更便宜。（若該欄位的資料型別沒有 <code><</code> 運算子，則此欄位為 null。）</p>                             |
| <p><code>most\_common\_elems</code> <code>anyarray</code></p><p>在欄位的值之中最常出現的非 null 元素值清單。（純量型別則為 null。）</p>                                                                                                                        |
| <p><code>most\_common\_elem\_freqs</code> <code>float4\[]</code></p><p>最常見之元素值的頻率清單，亦即包含至少一個該值實例之資料列的比例。在各元素的頻率之後，還會接著兩個或三個額外的值；它們是前述各元素頻率的最小值與最大值，以及可選的 null 元素頻率。（當 <code>most\_common\_elems</code> 為 null 時，此欄位也為 null。）</p>   |
| <p><code>elem\_count\_histogram</code> <code>float4\[]</code></p><p>欄位的各個值之中相異非 null 元素值個數的直方圖，其後接著相異非 null 元素的平均個數。（純量型別則為 null。）</p>                                                                                             |
| <p><code>range\_length\_histogram</code> <code>anyarray</code></p><p>範圍型別欄位中非空且非 null 之範圍值長度的直方圖。（非範圍型別則為 null。）</p><p>無論範圍的邊界是否包含端點，此直方圖都是使用 <code>subtype\_diff</code> 範圍函式計算的。</p>                                              |
| <p><code>range\_empty\_frac</code> <code>float4</code></p><p>欄位項目中值為空範圍的比例。（非範圍型別則為 null。）</p>                                                                                                                                     |
| <p><code>range\_bounds\_histogram</code> <code>anyarray</code></p><p>非空且非 null 之範圍值的下界與上界的直方圖。（非範圍型別則為 null。）</p><p>這兩個直方圖以單一的範圍陣列表示，其中各範圍的下界代表下界的直方圖，上界則代表上界的直方圖。</p>                                                             |

<br>

陣列欄位中的項目數上限，可以使用 [`ALTER TABLE SET STATISTICS`](https://docs.postgresql.tw/19/reference/sql-commands/sql-altertable.md) 命令逐欄控制，或透過設定 [default\_statistics\_target](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-query.md#GUC-DEFAULT-STATISTICS-TARGET) 執行期參數進行全域控制。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/view-pg-stats.html)（原文版本：18.6；核對日期：2026-10-10）


---

# 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/internals/views/view-pg-stats.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.
