1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-02 04:21:28 +03:00

Add path column to pg_backend_memory_contexts view

"path" provides a reliable method of determining the parent/child
relationships between memory contexts.  Previously this could be done in
a non-reliable way by writing a recursive query and joining the "parent"
and "name" columns.  This wasn't reliable as the names were not unique,
which could result in joining to the wrong parent.

To make this reliable, "path" stores an array of numerical identifiers
starting with the identifier for TopLevelMemoryContext.  It contains an
element for each intermediate parent between that and the current context.

Incompatibility: Here we also adjust the "level" column to make it
1-based rather than 0-based.  A 1-based level provides a convenient way
to access elements in the "path" array. e.g. path[level] gives the
identifier for the current context.

Identifiers are not stable across multiple evaluations of the view.  In
an attempt to make these more stable for ad-hoc queries, the identifiers
are assigned breadth-first.  Contexts closer to TopLevelMemoryContext
are less likely to change between queries and during queries.

Author: Melih Mutlu <m.melihmutlu@gmail.com>
Discussion: https://postgr.es/m/CAGPVpCThLyOsj3e_gYEvLoHkr5w=tadDiN_=z2OwsK3VJppeBA@mail.gmail.com
Reviewed-by: Andres Freund, Stephen Frost, Atsushi Torikoshi,
Reviewed-by: Michael Paquier, Robert Haas, David Rowley
This commit is contained in:
David Rowley
2024-07-25 15:03:28 +12:00
parent 64c39bd504
commit 32d3ed8165
8 changed files with 225 additions and 40 deletions

View File

@@ -504,7 +504,22 @@
<structfield>level</structfield> <type>int4</type>
</para>
<para>
Distance from TopMemoryContext in context tree
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
<structfield>path</structfield> column.
</para></entry>
</row>
<row>
<entry role="catalog_table_entry"><para role="column_definition">
<structfield>path</structfield> <type>int4[]</type>
</para>
<para>
Array of transient numerical identifiers to describe the memory
context hierarchy. The first element is for
<literal>TopMemoryContext</literal>, subsequent elements contain
intermediate parents and the final element contains the identifier for
the current context.
</para></entry>
</row>
@@ -561,6 +576,29 @@
read only by superusers or roles with the privileges of the
<literal>pg_read_all_stats</literal> role.
</para>
<para>
Since memory contexts are created and destroyed during the running of a
query, the identifiers stored in the <structfield>path</structfield> 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
<literal>CacheMemoryContext</literal> and all of its children:
<programlisting>
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];
</programlisting>
The <link linkend="queries-with">Common Table Expression</link> is used
to ensure the context IDs in the <structfield>path</structfield> column
match between both evaluations of the view.
</para>
</sect1>
<sect1 id="view-pg-config">