> 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/the-sql-language/functions/functions-admin/functions-admin-dbobject.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 9.28.7. 資料庫物件管理函式

## 9.28.7. 資料庫物件管理函式 [#](#functions-admin-dbobject) <a href="#functions-admin-dbobject" id="functions-admin-dbobject"></a>

[表 9.102](#functions-admin-dbsize) 所列的函式會計算資料庫物件的磁碟空間使用量，或協助呈現或理解使用量的結果。`bigint` 結果以位元組為單位。如果傳給這些函式的 OID 並不代表既有的物件，就會回傳 `NULL`。

### 表 9.102. 資料庫物件大小函式 [#](#functions-admin-dbsize) <a href="#functions-admin-dbsize" id="functions-admin-dbsize"></a>

| <p>函式</p><p>說明</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>pg\_column\_size</code> ( <code>"any"</code> ) → <code>integer</code></p><p>顯示儲存任一個別資料值所使用的位元組數。如果直接套用在資料表欄位值上，這會反映出所做的任何壓縮。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| <p><code>pg\_column\_compression</code> ( <code>"any"</code> ) → <code>text</code></p><p>顯示用來壓縮個別變動長度值的壓縮演算法。如果該值未經壓縮，就回傳 <code>NULL</code>。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| <p><code>pg\_column\_toast\_chunk\_id</code> ( <code>"any"</code> ) → <code>oid</code></p><p>顯示存放在磁碟上之 TOAST 化值的 <code>chunk\_id</code>。如果該值未經 TOAST 處理或不在磁碟上，就回傳 <code>NULL</code>。關於 TOAST 的更多資訊，請參閱<a href="https://docs.postgresql.tw/19/internals/storage/storage-toast.md">第 66.2 節</a>。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| <p><code>pg\_database\_size</code> ( <code>name</code> ) → <code>bigint</code></p><p><code>pg\_database\_size</code> ( <code>oid</code> ) → <code>bigint</code></p><p>計算具有指定名稱或 OID 之資料庫所使用的磁碟空間總量。要使用這個函式，你必須擁有指定資料庫的 <code>CONNECT</code> 權限（預設即授予），或具有 <code>pg\_read\_all\_stats</code> 角色的權限。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| <p><code>pg\_indexes\_size</code> ( <code>regclass</code> ) → <code>bigint</code></p><p>計算附屬於指定資料表之索引所使用的磁碟空間總量。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| <p><code>pg\_relation\_size</code> ( <em><code>relation</code></em> <code>regclass</code> \[, <em><code>fork</code></em> <code>text</code> ] ) → <code>bigint</code></p><p>計算指定關聯的一個「fork」所使用的磁碟空間。（請注意，在大多數情況下，使用較高階的函式 <code>pg\_total\_relation\_size</code> 或 <code>pg\_table\_size</code> 會更方便，它們會加總所有 fork 的大小。）只有一個引數時，這會回傳該關聯主要資料 fork 的大小。可以提供第二個引數來指定要檢查哪一個 fork：</p><ul><li><code>main</code> 回傳該關聯主要資料 fork 的大小。</li><li><code>fsm</code> 回傳與該關聯相關聯之空閒空間對應表（Free Space Map，請參閱<a href="https://docs.postgresql.tw/19/internals/storage/storage-fsm.md">第 66.3 節</a>）的大小。</li><li><code>vm</code> 回傳與該關聯相關聯之可見性對應表（Visibility Map，請參閱<a href="https://docs.postgresql.tw/19/internals/storage/storage-vm.md">第 66.4 節</a>）的大小。</li><li><code>init</code> 回傳與該關聯相關聯之初始化 fork（如果有的話）的大小。</li></ul> |
| <p><code>pg\_size\_bytes</code> ( <code>text</code> ) → <code>bigint</code></p><p>將人類易讀格式的大小（如 <code>pg\_size\_pretty</code> 所回傳的）轉換為位元組數。有效的單位為 <code>bytes</code>、<code>B</code>、<code>kB</code>、<code>MB</code>、<code>GB</code>、<code>TB</code> 與 <code>PB</code>。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| <p><code>pg\_size\_pretty</code> ( <code>bigint</code> ) → <code>text</code></p><p><code>pg\_size\_pretty</code> ( <code>numeric</code> ) → <code>text</code></p><p>將以位元組表示的大小，轉換為附帶大小單位（視情況使用 bytes、kB、MB、GB、TB 或 PB）、較容易讓人閱讀的格式。請注意，這些單位是 2 的次方而不是 10 的次方，所以 1kB 是 1024 個位元組，1MB 是 1024<sup>2</sup> = 1048576 個位元組，依此類推。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| <p><code>pg\_table\_size</code> ( <code>regclass</code> ) → <code>bigint</code></p><p>計算指定資料表所使用的磁碟空間，不包括索引（但包括其 TOAST 資料表（如果有的話）、空閒空間對應表與可見性對應表）。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| <p><code>pg\_tablespace\_size</code> ( <code>name</code> ) → <code>bigint</code></p><p><code>pg\_tablespace\_size</code> ( <code>oid</code> ) → <code>bigint</code></p><p>計算具有指定名稱或 OID 之資料表空間所使用的磁碟空間總量。要使用這個函式，你必須擁有指定資料表空間的 <code>CREATE</code> 權限，或具有 <code>pg\_read\_all\_stats</code> 角色的權限，除非它是目前資料庫的預設資料表空間。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| <p><code>pg\_total\_relation\_size</code> ( <code>regclass</code> ) → <code>bigint</code></p><p>計算指定資料表所使用的磁碟空間總量，包括所有索引與 TOAST 資料。結果等同於 <code>pg\_table\_size</code> <code>+</code> <code>pg\_indexes\_size</code>。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |

<br>

上述對資料表或索引進行操作的函式接受 `regclass` 引數，它就是該資料表或索引在 `pg_class` 系統目錄中的 OID。不過，你不必手動查詢 OID，因為 `regclass` 資料型別的輸入轉換器會替你完成這項工作。詳情請參閱[第 8.19 節](https://docs.postgresql.tw/19/the-sql-language/datatype/datatype-oid.md)。

[表 9.103](#functions-admin-dblocation) 所列的函式可協助識別與資料庫物件相關聯的特定磁碟檔案。

### 表 9.103. 資料庫物件位置函式 [#](#functions-admin-dblocation) <a href="#functions-admin-dblocation" id="functions-admin-dblocation"></a>

| <p>函式</p><p>說明</p>                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>pg\_relation\_filenode</code> ( <em><code>relation</code></em> <code>regclass</code> ) → <code>oid</code></p><p>回傳目前指派給指定關聯的「filenode」（檔案節點）編號。filenode 是該關聯所使用之檔案名稱的基本組成部分（更多資訊請參閱<a href="https://docs.postgresql.tw/19/internals/storage/storage-file-layout.md">第 66.1 節</a>）。對大多數關聯而言，結果與 <code>pg\_class</code>.<code>relfilenode</code> 相同，但對某些系統目錄而言，<code>relfilenode</code> 為零，必須使用這個函式才能取得正確的值。如果傳入的是沒有儲存空間的關聯（例如檢視表），函式會回傳 NULL。</p> |
| <p><code>pg\_relation\_filepath</code> ( <em><code>relation</code></em> <code>regclass</code> ) → <code>text</code></p><p>回傳該關聯完整的檔案路徑名稱（相對於資料庫叢集的資料目錄 <code>PGDATA</code>）。</p>                                                                                                                                                                                                                                                                          |
| <p><code>pg\_filenode\_relation</code> ( <em><code>tablespace</code></em> <code>oid</code>, <em><code>filenode</code></em> <code>oid</code> ) → <code>regclass</code></p><p>依據資料表空間 OID 與關聯所存放的 filenode，回傳該關聯的 OID。這基本上是 <code>pg\_relation\_filepath</code> 的反向對應。對於位於資料庫預設資料表空間中的關聯，資料表空間可以指定為零。如果目前資料庫中沒有任何關聯與指定的值相關聯，或處理的是暫時關聯，就回傳 <code>NULL</code>。</p>                                                                                            |

<br>

[表 9.104](#functions-admin-collation) 列出用於管理定序的函式。

### 表 9.104. 定序管理函式 [#](#functions-admin-collation) <a href="#functions-admin-collation" id="functions-admin-collation"></a>

| <p>函式</p><p>說明</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| <p><code>pg\_collation\_actual\_version</code> ( <code>oid</code> ) → <code>text</code></p><p>回傳該定序物件目前安裝在作業系統中的實際版本。如果這與 <code>pg\_collation</code>.<code>collversion</code> 中的值不同，那麼相依於該定序的物件可能需要重建。另請參閱 <a href="https://docs.postgresql.tw/19/reference/sql-commands/sql-altercollation.md">ALTER COLLATION</a>。</p>                                                                                                                                                                                                                                                                               |
| <p><code>pg\_database\_collation\_actual\_version</code> ( <code>oid</code> ) → <code>text</code></p><p>回傳資料庫的定序目前安裝在作業系統中的實際版本。如果這與 <code>pg\_database</code>.<code>datcollversion</code> 中的值不同，那麼相依於該定序的物件可能需要重建。另請參閱 <a href="https://docs.postgresql.tw/19/reference/sql-commands/sql-alterdatabase.md">ALTER DATABASE</a>。</p>                                                                                                                                                                                                                                                                    |
| <p><code>pg\_import\_system\_collations</code> ( <em><code>schema</code></em> <code>regnamespace</code> ) → <code>integer</code></p><p>依據在作業系統中找到的所有語系，將定序加入系統目錄 <code>pg\_collation</code>。這就是 <code>initdb</code> 所使用的方式；詳情請參閱<a href="https://docs.postgresql.tw/19/server-administration/charset/collation.md#collation-managing">第 23.2.2 節</a>。如果之後在作業系統中安裝了其他語系，可以再次執行這個函式，為新的語系加入定序。與 <code>pg\_collation</code> 中既有項目相符的語系會被略過。（但以已不再存在於作業系統中之語系為基礎的定序物件，並不會被這個函式移除。）<em><code>schema</code></em> 參數通常會是 <code>pg\_catalog</code>，但這並非必要條件；定序也可以安裝到其他綱要中。函式會回傳它所建立的新定序物件數量。這個函式只限超級使用者使用。</p> |

<br>

[表 9.105](#functions-admin-statsmod) 列出用於操作統計資訊的函式。這些函式不能在復原期間執行。

### 警告

這些統計資訊操作函式所做的變更，很可能會被 [autovacuum](https://docs.postgresql.tw/19/server-administration/maintenance/routine-vacuuming.md#autovacuum)（或手動執行的 `VACUUM` 或 `ANALYZE`）覆寫，因此應視為暫時性的。

### 表 9.105. 資料庫物件統計資訊操作函式 [#](#functions-admin-statsmod) <a href="#functions-admin-statsmod" id="functions-admin-statsmod"></a>

<table data-header-hidden><thead><tr><th></th></tr></thead><tbody><tr><td><p>函式</p><p>說明</p></td></tr><tr><td><p><code>pg_restore_relation_stats</code> ( <code>VARIADIC</code> <em><code>kwargs</code></em> <code>"any"</code> ) → <code>boolean</code></p><p>更新資料表層級的統計資訊。通常這些統計資訊會自動收集，或作為 <a href="https://docs.postgresql.tw/19/reference/sql-commands/sql-vacuum.md">VACUUM</a> 或 <a href="https://docs.postgresql.tw/19/reference/sql-commands/sql-analyze.md">ANALYZE</a> 的一部分而更新，所以不需要呼叫這個函式。不過，在還原之後如果尚未執行 <code>ANALYZE</code>，這個函式可以讓最佳化器選擇較好的執行計畫。</p><p>所追蹤的統計資訊可能會隨版本而改變，因此引數是以 <em><code>argname</code></em> 與 <em><code>argvalue</code></em> 成對的方式傳入，形式如下：</p><pre><code>SELECT pg_restore_relation_stats(
    'arg1name', 'arg1value'::arg1type,
    'arg2name', 'arg2value'::arg2type,
    'arg3name', 'arg3value'::arg3type);
</code></pre><p>例如，要設定 <code>relpages</code> 與 <code>reltuples</code> 在資料表 <code>mytable</code> 上的值：</p><pre><code>SELECT pg_restore_relation_stats(
    'schemaname', 'myschema',
    'relname',    'mytable',
    'relpages',   173::integer,
    'reltuples',  10000::real);
</code></pre><p>引數 <code>schemaname</code> 與 <code>relname</code> 是必要的，用來指定資料表。其他引數則是對應於 <a href="https://docs.postgresql.tw/19/internals/catalogs/catalog-pg-class.md"><code>pg_class</code></a> 中特定欄位之統計資訊的名稱與值。目前支援的關聯統計資訊有：<code>relpages</code>（值為 <code>integer</code> 型別）、<code>reltuples</code>（值為 <code>real</code> 型別）、<code>relallvisible</code>（值為 <code>integer</code> 型別），以及 <code>relallfrozen</code>（值為 <code>integer</code> 型別）。</p><p>此外，這個函式還接受名為 <code>version</code>、型別為 <code>integer</code> 的引數，用來指定統計資訊來源的伺服器版本。這預期有助於從較舊版本的 PostgreSQL 移植統計資訊。</p><p>輕微的錯誤會以 <code>WARNING</code> 回報並予以忽略，其餘的統計資訊仍會被還原。如果所有指定的統計資訊都成功還原，就回傳 <code>true</code>，否則回傳 <code>false</code>。</p><p>呼叫者必須擁有該資料表的 <code>MAINTAIN</code> 權限，或是資料庫的擁有者。</p></td></tr><tr><td><p><code>pg_clear_relation_stats</code> ( <em><code>schemaname</code></em> <code>text</code>, <em><code>relname</code></em> <code>text</code> ) → <code>void</code></p><p>清除指定關聯的資料表層級統計資訊，就如同該資料表是新建立的一樣。</p><p>呼叫者必須擁有該資料表的 <code>MAINTAIN</code> 權限，或是資料庫的擁有者。</p></td></tr><tr><td><p><code>pg_restore_attribute_stats</code> ( <code>VARIADIC</code> <em><code>kwargs</code></em> <code>"any"</code> ) → <code>boolean</code></p><p>建立或更新欄位層級的統計資訊。通常這些統計資訊會自動收集，或作為 <a href="https://docs.postgresql.tw/19/reference/sql-commands/sql-vacuum.md">VACUUM</a> 或 <a href="https://docs.postgresql.tw/19/reference/sql-commands/sql-analyze.md">ANALYZE</a> 的一部分而更新，所以不需要呼叫這個函式。不過，在還原之後如果尚未執行 <code>ANALYZE</code>，這個函式可以讓最佳化器選擇較好的執行計畫。</p><p>所追蹤的統計資訊可能會隨版本而改變，因此引數是以 <em><code>argname</code></em> 與 <em><code>argvalue</code></em> 成對的方式傳入，形式如下：</p><pre><code>SELECT pg_restore_attribute_stats(
    'arg1name', 'arg1value'::arg1type,
    'arg2name', 'arg2value'::arg2type,
    'arg3name', 'arg3value'::arg3type);
</code></pre><p>例如，要設定 <code>avg_width</code> 與 <code>null_frac</code> 在屬性 <code>col1</code> 上的值，該屬性屬於資料表 <code>mytable</code>：</p><pre><code>SELECT pg_restore_attribute_stats(
    'schemaname', 'myschema',
    'relname',    'mytable',
    'attname',    'col1',
    'inherited',  false,
    'avg_width',  125::integer,
    'null_frac',  0.5::real);
</code></pre><p>必要的引數有：<code>schemaname</code> 與 <code>relname</code>（值為 <code>text</code> 型別），用來指定資料表；<code>attname</code>（值為 <code>text</code> 型別）或 <code>attnum</code>（值為 <code>smallint</code> 型別）二者之一，用來指定欄位；以及 <code>inherited</code>，用來指定統計資訊是否包含子資料表的值。其他引數則是對應於 <a href="https://docs.postgresql.tw/19/internals/views/view-pg-stats.md"><code>pg_stats</code></a> 中欄位之統計資訊的名稱與值。</p><p>此外，這個函式還接受名為 <code>version</code>、型別為 <code>integer</code> 的引數，用來指定統計資訊來源的伺服器版本。這預期有助於從較舊版本的 PostgreSQL 移植統計資訊。</p><p>輕微的錯誤會以 <code>WARNING</code> 回報並予以忽略，其餘的統計資訊仍會被還原。如果所有指定的統計資訊都成功還原，就回傳 <code>true</code>，否則回傳 <code>false</code>。</p><p>呼叫者必須擁有該資料表的 <code>MAINTAIN</code> 權限，或是資料庫的擁有者。</p></td></tr><tr><td><p><code>pg_clear_attribute_stats</code> ( <em><code>schemaname</code></em> <code>text</code>, <em><code>relname</code></em> <code>text</code>, <em><code>attname</code></em> <code>text</code>, <em><code>inherited</code></em> <code>boolean</code> ) → <code>void</code></p><p>清除指定關聯與屬性的欄位層級統計資訊，就如同該資料表是新建立的一樣。</p><p>呼叫者必須擁有該資料表的 <code>MAINTAIN</code> 權限，或是資料庫的擁有者。</p></td></tr></tbody></table>

<br>

[表 9.106](#functions-info-partition) 列出提供分割資料表結構相關資訊的函式。

### 表 9.106. 分割資訊函式 [#](#functions-info-partition) <a href="#functions-info-partition" id="functions-info-partition"></a>

| <p>函式</p><p>說明</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>pg\_partition\_tree</code> ( <code>regclass</code> ) → <code>setof record</code> ( <em><code>relid</code></em> <code>regclass</code>, <em><code>parentrelid</code></em> <code>regclass</code>, <em><code>isleaf</code></em> <code>boolean</code>, <em><code>level</code></em> <code>integer</code> )</p><p>列出指定的分割資料表或分割索引之分割樹中的資料表或索引，每個分割區一筆資料列。所提供的資訊包括分割區的 OID、其直接父層的 OID、表示該分割區是否為葉節點的布林值，以及表示其在階層中所在層級的整數。輸入的資料表或索引的層級值為 0，其直接子分割區為 1，再下一層的分割區為 2，依此類推。如果該關聯不存在，或者既不是分割區也不是分割資料表，就不回傳任何資料列。</p> |
| <p><code>pg\_partition\_ancestors</code> ( <code>regclass</code> ) → <code>setof regclass</code></p><p>列出指定分割區的上層祖先關聯，包括該關聯本身。如果該關聯不存在，或者既不是分割區也不是分割資料表，就不回傳任何資料列。</p>                                                                                                                                                                                                                                                                                                                                         |
| <p><code>pg\_partition\_root</code> ( <code>regclass</code> ) → <code>regclass</code></p><p>回傳指定關聯所屬之分割樹最頂層的父層。如果該關聯不存在，或者既不是分割區也不是分割資料表，就回傳 <code>NULL</code>。</p>                                                                                                                                                                                                                                                                                                                                            |

<br>

例如，要檢查分割資料表 `measurement` 所含資料的總大小，可以使用下列查詢：

```

SELECT pg_size_pretty(sum(pg_relation_size(relid))) AS total_size
  FROM pg_partition_tree('measurement');
```

[返回 9.28. 系統管理函式](https://docs.postgresql.tw/19/the-sql-language/functions/functions-admin.md#functions-admin)

***

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


---

# 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/the-sql-language/functions/functions-admin/functions-admin-dbobject.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.
