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

# F.33.1. 函式

## F.33.1. 函式 [#](#pgstattuple-funcs) <a href="#pgstattuple-funcs" id="pgstattuple-funcs"></a>

`pgstattuple(regclass) returns record`

`pgstattuple` 會傳回關聯的實體長度、「死亡」資料列的百分比，以及其他資訊。這可以協助使用者判斷是否需要清理（vacuum）。引數是目標關聯的名稱（可選擇以綱要限定）或 OID。 例如：

```

    test=> SELECT * FROM pgstattuple('pg_catalog.pg_proc');
    -[ RECORD 1 ]------+-------
    table_len          | 458752
    tuple_count        | 1470
    tuple_len          | 438896
    tuple_percent      | 95.67
    dead_tuple_count   | 11
    dead_tuple_len     | 3157
    dead_tuple_percent | 0.69
    free_space         | 8932
    free_percent       | 1.95
```

輸出欄位說明請參閱[表 F.24](#pgstattuple-columns).

### 表 F.24. `pgstattuple` 輸出欄位 [#](#pgstattuple-columns) <a href="#pgstattuple-columns" id="pgstattuple-columns"></a>

| 欄位                   | 型別       | 說明                |
| -------------------- | -------- | ----------------- |
| `table_len`          | `bigint` | 關聯的實體長度，以位元組為單位   |
| `tuple_count`        | `bigint` | 存活資料列數            |
| `tuple_len`          | `bigint` | 存活資料列的總長度，以位元組為單位 |
| `tuple_percent`      | `float8` | 存活資料列的百分比         |
| `dead_tuple_count`   | `bigint` | 死亡資料列數            |
| `dead_tuple_len`     | `bigint` | 死亡資料列的總長度，以位元組為單位 |
| `dead_tuple_percent` | `float8` | 死亡資料列的百分比         |
| `free_space`         | `bigint` | 可用空間總量，以位元組為單位    |
| `free_percent`       | `float8` | 可用空間的百分比          |

<br>

### 注意

`table_len` 一定會大於 `tuple_len`、`dead_tuple_len` 與 `free_space` 的總和。差額來自固定的頁面額外負擔、每個頁面中指向元組的指標表，以及確保元組正確對齊的填補。

`pgstattuple` 只會對關聯取得讀取鎖定。因此結果並不反映瞬間的快照；並行的更新會影響結果。

若 `HeapTupleSatisfiesDirty` 傳回 false， `pgstattuple` 就判定該元組為「死亡」。

`pgstattuple(text) returns record` : 此函式與 `pgstattuple(regclass)` 相同，只是目標關聯以 TEXT 指定。此函式目前是為了向後相容性而保留，並將在未來的某個版本中棄用。

`pgstatindex(regclass) returns record`

`pgstatindex` 會傳回一筆紀錄，顯示 B-tree 索引的相關資訊。例如：

```

    test=> SELECT * FROM pgstatindex('pg_cast_oid_index');
    -[ RECORD 1 ]------+------
    version            | 2
    tree_level         | 0
    index_size         | 16384
    root_block_no      | 1
    internal_pages     | 0
    leaf_pages         | 1
    empty_pages        | 0
    deleted_pages      | 0
    avg_leaf_density   | 54.27
    leaf_fragmentation | 0
```

輸出欄位如下：

| 欄位                   | 型別        | 說明            |
| -------------------- | --------- | ------------- |
| `version`            | `integer` | B-tree 版本編號   |
| `tree_level`         | `integer` | 根頁面的樹層級       |
| `index_size`         | `bigint`  | 索引總大小，以位元組為單位 |
| `root_block_no`      | `bigint`  | 根頁面的位置（若無則為零） |
| `internal_pages`     | `bigint`  | 「內部」（上層）頁面數   |
| `leaf_pages`         | `bigint`  | 葉頁面數          |
| `empty_pages`        | `bigint`  | 空頁面數          |
| `deleted_pages`      | `bigint`  | 已刪除頁面數        |
| `avg_leaf_density`   | `float8`  | 葉頁面的平均密度      |
| `leaf_fragmentation` | `float8`  | 葉頁面的碎片化程度     |

回報的 `index_size` 通常會比 `internal_pages + leaf_pages + empty_pages + deleted_pages` 所計入的頁面多一個頁面，因為它還包含索引的中繼頁面（metapage）。

與 `pgstattuple` 相同，結果是逐頁累計的，不應預期它代表整個索引的瞬間快照。

`pgstatindex(text) returns record` : 此函式與 `pgstatindex(regclass)` 相同，只是目標索引以 TEXT 指定。此函式目前是為了向後相容性而保留，並將在未來的某個版本中棄用。

`pgstatginindex(regclass) returns record`

`pgstatginindex` 會傳回一筆紀錄，顯示 GIN 索引的相關資訊。例如：

```

    test=> SELECT * FROM pgstatginindex('test_gin_index');
    -[ RECORD 1 ]--+--
    version        | 1
    pending_pages  | 0
    pending_tuples | 0
```

輸出欄位如下：

| 欄位               | 型別        | 說明         |
| ---------------- | --------- | ---------- |
| `version`        | `integer` | GIN 版本編號   |
| `pending_pages`  | `integer` | 待處理清單中的頁面數 |
| `pending_tuples` | `bigint`  | 待處理清單中的元組數 |

`pgstathashindex(regclass) returns record`

`pgstathashindex` 會傳回一筆紀錄，顯示 HASH 索引的相關資訊。例如：

```

    test=> select * from pgstathashindex('con_hash_index');
    -[ RECORD 1 ]--+-----------------
    version        | 4
    bucket_pages   | 33081
    overflow_pages | 0
    bitmap_pages   | 1
    unused_pages   | 32455
    live_items     | 10204006
    dead_items     | 0
    free_percent   | 61.8005949100872
```

輸出欄位如下：

| 欄位               | 型別        | 說明           |
| ---------------- | --------- | ------------ |
| `version`        | `integer` | HASH 版本編號    |
| `bucket_pages`   | `bigint`  | 桶（bucket）頁面數 |
| `overflow_pages` | `bigint`  | 溢位頁面數        |
| `bitmap_pages`   | `bigint`  | 點陣圖頁面數       |
| `unused_pages`   | `bigint`  | 未使用頁面數       |
| `live_items`     | `bigint`  | 存活資料列數       |
| `dead_tuples`    | `bigint`  | 死亡資料列數       |
| `free_percent`   | `float`   | 可用空間的百分比     |

譯註：上表中第七個欄位的名稱寫作 dead\_tuples，但上方範例輸出顯示的是 dead\_items。經核對 REL\_18\_STABLE 的 contrib/pgstattuple/pgstattuple--1.4--1.5.sql，pgstathashindex 實際傳回的欄位名稱為 dead\_items（bigint），表中 dead\_tuples 為原文誤植；表格內容依原文保留。

`pg_relpages(regclass) returns bigint` : `pg_relpages` 會傳回關聯中的頁面數。

`pg_relpages(text) returns bigint` : 此函式與 `pg_relpages(regclass)` 相同，只是目標關聯以 TEXT 指定。此函式目前是為了向後相容性而保留，並將在未來的某個版本中棄用。

`pgstattuple_approx(regclass) returns record`

`pgstattuple_approx` 是 `pgstattuple` 的較快替代方案，會傳回近似的結果。 引數是目標關聯的名稱或 OID。 例如：

```

    test=> SELECT * FROM pgstattuple_approx('pg_catalog.pg_proc'::regclass);
    -[ RECORD 1 ]--------+-------
    table_len            | 573440
    scanned_percent      | 2
    approx_tuple_count   | 2740
    approx_tuple_len     | 561210
    approx_tuple_percent | 97.87
    dead_tuple_count     | 0
    dead_tuple_len       | 0
    dead_tuple_percent   | 0
    approx_free_space    | 11996
    approx_free_percent  | 2.09
```

輸出欄位說明請參閱[表 F.25](#pgstatapprox-columns).

`pgstattuple` 一律執行全資料表掃描，並傳回存活與死亡資料列的精確數量（及其大小）與可用空間；`pgstattuple_approx` 則會盡量避免全資料表掃描，傳回精確的死亡資料列統計資訊，以及存活資料列數量與大小和可用空間的近似值。

它的做法是依據可見性對應表略過只包含可見元組的頁面（若某頁面已設定對應的 VM 位元，就假設它不包含任何死亡資料列）。對於這類頁面，它會從可用空間對應表取得可用空間的值，並假設頁面上其餘的空間都由存活資料列占用。

對於無法略過的頁面，它會掃描每個元組，將其存在與大小記錄到相應的計數器中，並累加頁面上的可用空間。最後，它會根據已掃描的頁面數與元組數估計存活資料列的總數（方式與 VACUUM 估計 pg\_class.reltuples 相同）。

### 表 F.25. `pgstattuple_approx` 輸出欄位 [#](#pgstatapprox-columns) <a href="#pgstatapprox-columns" id="pgstatapprox-columns"></a>

| 欄位                     | 型別       | 說明                     |
| ---------------------- | -------- | ---------------------- |
| `table_len`            | `bigint` | 關聯的實體長度，以位元組為單位（精確值）   |
| `scanned_percent`      | `float8` | 已掃描資料表的百分比             |
| `approx_tuple_count`   | `bigint` | 存活資料列數（估計值）            |
| `approx_tuple_len`     | `bigint` | 存活資料列的總長度，以位元組為單位（估計值） |
| `approx_tuple_percent` | `float8` | 存活資料列的百分比              |
| `dead_tuple_count`     | `bigint` | 死亡資料列數（精確值）            |
| `dead_tuple_len`       | `bigint` | 死亡資料列的總長度，以位元組為單位（精確值） |
| `dead_tuple_percent`   | `float8` | 死亡資料列的百分比              |
| `approx_free_space`    | `bigint` | 可用空間總量，以位元組為單位（估計值）    |
| `approx_free_percent`  | `float8` | 可用空間的百分比               |

<br>

在上述輸出中，可用空間的數字可能與 `pgstattuple` 的輸出不完全相符，因為可用空間對應表雖然提供確切的數字，但不保證精確到位元組。

[返回 F.33. pgstattuple — 取得元組層級的統計資訊](https://docs.postgresql.tw/19/appendixes/contrib/pgstattuple.md#pgstattuple)

***

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


---

# 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/appendixes/contrib/pgstattuple/pgstattuple-funcs.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.
