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

# 53.5. pg\_backend\_memory\_contexts \#

### 53.5. `pg_backend_memory_contexts` [#](#view-pg-backend-memory-contexts) <a href="#view-pg-backend-memory-contexts" id="view-pg-backend-memory-contexts"></a>

檢視表 `pg_backend_memory_contexts` 顯示目前工作階段所連接之伺服器程序的所有記憶體上下文。

`pg_backend_memory_contexts` 中，每個記憶體上下文各占一筆資料列。

#### 表 53.5. `pg_backend_memory_contexts` 欄位 [#](#view-pg-backend-memory-contexts-columns) <a href="#view-pg-backend-memory-contexts-columns" id="view-pg-backend-memory-contexts-columns"></a>

| <p>欄位類型</p><p>說明</p>                                                                                                                                     |
| -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>name</code> <code>text</code></p><p>記憶體上下文的名稱</p>                                                                                               |
| <p><code>ident</code> <code>text</code></p><p>記憶體上下文的識別資訊。此欄位會截斷在 1024 位元組</p>                                                                           |
| <p><code>type</code> <code>text</code></p><p>記憶體上下文的類型</p>                                                                                               |
| <p><code>level</code> <code>int4</code></p><p>此上下文在記憶體上下文階層中的層級（從 1 起算）。上下文的層級也代表該上下文在 <code>path</code> 欄位中的位置。</p>                                     |
| <p><code>path</code> <code>int4\[]</code></p><p>由暫時性數字識別碼組成的陣列，用以描述記憶體上下文階層。第一個元素對應 <code>TopMemoryContext</code>，後續元素包含中間層的父上下文，最後一個元素則包含目前上下文的識別碼。</p> |
| <p><code>total\_bytes</code> <code>int8</code></p><p>為此記憶體上下文配置的總位元組數</p>                                                                                |
| <p><code>total\_nblocks</code> <code>int8</code></p><p>為此記憶體上下文配置的區塊總數</p>                                                                               |
| <p><code>free\_bytes</code> <code>int8</code></p><p>可用空間（位元組）</p>                                                                                        |
| <p><code>free\_chunks</code> <code>int8</code></p><p>可用區塊片段（chunk）的總數</p>                                                                                |
| <p><code>used\_bytes</code> <code>int8</code></p><p>已使用空間（位元組）</p>                                                                                       |

<br>

預設情況下，只有超級使用者或具有 `pg_read_all_stats` 角色權限的角色可以讀取 `pg_backend_memory_contexts` 檢視表。

由於記憶體上下文會在查詢執行期間建立與銷毀，因此在同一個查詢中多次叫用此檢視表時，`path` 欄位中儲存的識別碼可能不一致。下面的範例示範如何有效使用此欄位，計算 `CacheMemoryContext` 及其所有子上下文所使用的總位元組數：

```

WITH memory_contexts AS (
    SELECT * FROM pg_backend_memory_contexts
)
SELECT sum(c1.total_bytes)
FROM memory_contexts c1, memory_contexts c2
WHERE c2.name = 'CacheMemoryContext'
AND c1.path[c2.level] = c2.path[c2.level];
```

此處使用[通用資料表運算式](https://docs.postgresql.tw/19/the-sql-language/queries/queries-with.md)，以確保 `path` 欄位中的上下文 ID 在兩次評估此檢視表時一致。

***

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


---

# 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-backend-memory-contexts.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.
