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

# 52.51. pg\_statistic \#

### 52.51. `pg_statistic` [#](#catalog-pg-statistic) <a href="#catalog-pg-statistic" id="catalog-pg-statistic"></a>

目錄 `pg_statistic` 儲存關於資料庫內容的統計資料。項目由 [`ANALYZE`](https://docs.postgresql.tw/19/reference/sql-commands/sql-analyze.md) 建立，之後供查詢規劃器使用。請注意，即使假設統計資料是最新的，所有統計資料本質上都只是近似值。

通常，每個已分析過的資料表欄位都有一個 `stainherit` = `false` 的項目。若該資料表有繼承子資料表或分割區，還會另外建立第二個 `stainherit` = `true` 的項目。這個資料列代表該欄位在整個繼承樹上的統計資訊，也就是你用 `SELECT column FROM table*` 所看到之資料的統計資訊；而 `stainherit` = `false` 的資料列代表的則是 `SELECT column FROM ONLY table` 的結果。

`pg_statistic` 也儲存關於索引運算式之值的統計資料。這些資料的描述方式就如同它們是實際的資料欄位一樣；特別是，`starelid` 參照的是該索引。不過，一般的非運算式索引欄位不會建立項目，因為它會與底層資料表欄位的項目重複。目前，索引運算式的項目一律是 `stainherit` = `false`。

由於不同種類的資料可能適合不同種類的統計資訊，`pg_statistic` 在設計上不對它所儲存的統計資訊種類做太多假設。只有極為通用的統計資訊（例如是否為 null 的情形）才在 `pg_statistic` 中擁有專屬欄位。其他所有內容都儲存在「槽」（slot）中；槽是一組相關聯的欄位，其內容由該槽某一個欄位中的代碼編號來識別。詳細資訊請參閱 `src/include/catalog/pg_statistic.h`。

`pg_statistic` 不應讓所有人（public）都能讀取，因為即使只是資料表內容的統計資訊，也可能被視為敏感資訊。（例如：薪資欄位的最小值與最大值可能相當引人關注。）[`pg_stats`](https://docs.postgresql.tw/19/internals/views/view-pg-stats.md) 是建立在 `pg_statistic` 之上、可公開讀取的檢視表，它只公開目前使用者可讀取之資料表的相關資訊。

#### 表 52.51. `pg_statistic` 欄位 [#](#catalog-pg-statistic-columns) <a href="#catalog-pg-statistic-columns" id="catalog-pg-statistic-columns"></a>

| <p>欄位類型</p><p>說明</p>                                                                                                                                                                                                                                                                                                       |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>starelid</code> <code>oid</code> （參照 <a href="https://docs.postgresql.tw/19/internals/catalogs/catalog-pg-class.md"><code>pg\_class</code></a>.<code>oid</code>）</p><p>所描述欄位所屬的資料表或索引</p>                                                                                                                         |
| <p><code>staattnum</code> <code>int2</code> （參照 <a href="https://docs.postgresql.tw/19/internals/catalogs/catalog-pg-attribute.md"><code>pg\_attribute</code></a>.<code>attnum</code>）</p><p>所描述欄位的編號</p>                                                                                                                  |
| <p><code>stainherit</code> <code>bool</code></p><p>若為真，統計資訊包含子資料表中的值，而不只是指定關聯中的值</p>                                                                                                                                                                                                                                       |
| <p><code>stanullfrac</code> <code>float4</code></p><p>欄位項目中為 null 的比例</p>                                                                                                                                                                                                                                                  |
| <p><code>stawidth</code> <code>int4</code></p><p>非 null 項目的平均儲存寬度，以位元組為單位</p>                                                                                                                                                                                                                                              |
| <p><code>stadistinct</code> <code>float4</code></p><p>欄位中相異非 null 資料值的數目。 大於零的值就是相異值的實際數目。 小於零的值則是資料表資料列數之乘數的負值；例如，若某欄位約 80% 的值為非 null， 且每個非 null 值平均約出現兩次，就可以表示為 <code>stadistinct</code> = -0.4。 值為零表示相異值的數目未知。</p>                                                                                                      |
| <p><code>stakind</code><em><code>N</code></em> <code>int2</code></p><p>代碼編號，指出儲存在第 <em><code>N</code></em> 個“槽”中的統計資訊種類 （該槽位於 <code>pg\_statistic</code> 資料列中）。</p>                                                                                                                                                        |
| <p><code>staop</code><em><code>N</code></em> <code>oid</code> （參照 <a href="https://docs.postgresql.tw/19/internals/catalogs/catalog-pg-operator.md"><code>pg\_operator</code></a>.<code>oid</code>）</p><p>用來推導儲存在第 <em><code>N</code></em> 個“槽”中之統計資訊的運算子。例如， 直方圖槽會顯示定義資料排序順序的 <code><</code> 運算子。 若該統計資訊種類不需要運算子，則為零。</p> |
| <p><code>stacoll</code><em><code>N</code></em> <code>oid</code> （參照 <a href="https://docs.postgresql.tw/19/internals/catalogs/catalog-pg-collation.md"><code>pg\_collation</code></a>.<code>oid</code>）</p><p>用來推導儲存在第 <em><code>N</code></em> 個“槽”中之統計資訊的定序。例如， 可定序欄位的直方圖槽會顯示定義資料排序順序的定序。不可定序的資料則為零。</p>                  |
| <p><code>stanumbers</code><em><code>N</code></em> <code>float4\[]</code></p><p>適合第 <em><code>N</code></em> 個“槽”之種類的數值統計資訊； 若該槽的種類不涉及數值，則為 null</p>                                                                                                                                                                         |
| <p><code>stavalues</code><em><code>N</code></em> <code>anyarray</code></p><p>適合第 <em><code>N</code></em> 個“槽”之種類的欄位資料值； 若該槽的種類不儲存任何資料值，則為 null。每個陣列的元素值實際上 屬於該特定欄位的資料型別，或相關的型別（例如陣列的元素型別），因此無法 比 <code>anyarray</code> 更具體地定義這些欄位的型別。</p>                                                                                  |

<br>

***

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


---

# 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/catalogs/catalog-pg-statistic.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.
