> 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/internals/views/view-pg-backend-memory-contexts.md).

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

The view `pg_backend_memory_contexts` displays all the memory contexts of the server process attached to the current session.

`pg_backend_memory_contexts` contains one row for each memory context.

**Table 53.5. `pg_backend_memory_contexts` Columns**

| <p>Column Type</p><p>Description</p>                                                                                                                                                                                                                                                                                   |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>name</code> <code>text</code></p><p>Name of the memory context</p>                                                                                                                                                                                                                                            |
| <p><code>ident</code> <code>text</code></p><p>Identification information of the memory context. This field is truncated at 1024 bytes</p>                                                                                                                                                                              |
| <p><code>type</code> <code>text</code></p><p>Type of the memory context</p>                                                                                                                                                                                                                                            |
| <p><code>level</code> <code>int4</code></p><p>The 1-based level of the context in the memory context hierarchy. The level of a context also shows the position of that context in the <code>path</code> column.</p>                                                                                                    |
| <p><code>path</code> <code>int4\[]</code></p><p>Array of transient numerical identifiers to describe the memory context hierarchy. The first element is for <code>TopMemoryContext</code>, subsequent elements contain intermediate parents and the final element contains the identifier for the current context.</p> |
| <p><code>total\_bytes</code> <code>int8</code></p><p>Total bytes allocated for this memory context</p>                                                                                                                                                                                                                 |
| <p><code>total\_nblocks</code> <code>int8</code></p><p>Total number of blocks allocated for this memory context</p>                                                                                                                                                                                                    |
| <p><code>free\_bytes</code> <code>int8</code></p><p>Free space in bytes</p>                                                                                                                                                                                                                                            |
| <p><code>free\_chunks</code> <code>int8</code></p><p>Total number of free chunks</p>                                                                                                                                                                                                                                   |
| <p><code>used\_bytes</code> <code>int8</code></p><p>Used space in bytes</p>                                                                                                                                                                                                                                            |

<br>

By default, the `pg_backend_memory_contexts` view can be read only by superusers or roles with the privileges of the `pg_read_all_stats` role.

Since memory contexts are created and destroyed during the running of a query, the identifiers stored in the `path` column can be unstable between multiple invocations of the view in the same query. The example below demonstrates an effective usage of this column and calculates the total number of bytes used by `CacheMemoryContext` and all of its children:

```

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];
```

The [Common Table Expression](/the-sql-language/queries/queries-with.md) is used to ensure the context IDs in the `path` column match between both evaluations of the view.

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/view-pg-backend-memory-contexts.html)（英文原文，待翻譯）


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.postgresql.tw/internals/views/view-pg-backend-memory-contexts.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
