@@ -28663,6 +28663,144 @@ acl | {postgres=arwdDxtm/postgres,foo=r/postgres}
28663
28663
</para></entry>
28664
28664
</row>
28665
28665
28666
+ <row>
28667
+ <entry role="func_table_entry"><para role="func_signature">
28668
+ <indexterm>
28669
+ <primary>pg_get_process_memory_contexts</primary>
28670
+ </indexterm>
28671
+ <function>pg_get_process_memory_contexts</function> ( <parameter>pid</parameter> <type>integer</type>, <parameter>summary</parameter> <type>boolean</type>, <parameter>timeout</parameter> <type>float</type> )
28672
+ <returnvalue>setof record</returnvalue>
28673
+ ( <parameter>name</parameter> <type>text</type>,
28674
+ <parameter>ident</parameter> <type>text</type>,
28675
+ <parameter>type</parameter> <type>text</type>,
28676
+ <parameter>path</parameter> <type>integer[]</type>,
28677
+ <parameter>level</parameter> <type>integer</type>,
28678
+ <parameter>total_bytes</parameter> <type>bigint</type>,
28679
+ <parameter>total_nblocks</parameter> <type>bigint</type>,
28680
+ <parameter>free_bytes</parameter> <type>bigint</type>,
28681
+ <parameter>free_chunks</parameter> <type>bigint</type>,
28682
+ <parameter>used_bytes</parameter> <type>bigint</type>,
28683
+ <parameter>num_agg_contexts</parameter> <type>integer</type>,
28684
+ <parameter>stats_timestamp</parameter> <type>timestamptz</type> )
28685
+ </para>
28686
+ <para>
28687
+ This function handles requests to display the memory contexts of a
28688
+ <productname>PostgreSQL</productname> process with the specified
28689
+ process ID. The function can be used to send requests to backends as
28690
+ well as <glossterm linkend="glossary-auxiliary-proc">auxiliary processes</glossterm>.
28691
+ </para>
28692
+ <para>
28693
+ The returned record contains extended statistics per each memory
28694
+ context:
28695
+ <itemizedlist spacing="compact">
28696
+ <listitem>
28697
+ <para>
28698
+ <parameter>name</parameter> - The name of the memory context.
28699
+ </para>
28700
+ </listitem>
28701
+ <listitem>
28702
+ <para>
28703
+ <parameter>ident</parameter> - Memory context ID (if any).
28704
+ </para>
28705
+ </listitem>
28706
+ <listitem>
28707
+ <para>
28708
+ <parameter>type</parameter> - The type of memory context, possible
28709
+ values are: AllocSet, Generation, Slab and Bump.
28710
+ </para>
28711
+ </listitem>
28712
+ <listitem>
28713
+ <para>
28714
+ <parameter>path</parameter> - Memory contexts are organized in a
28715
+ tree model with TopMemoryContext as the root, and all other memory
28716
+ contexts as nodes in the tree. The <parameter>path</parameter>
28717
+ displays the path from the root to the current memory context. The
28718
+ path is limited to 100 children per node, which each node limited
28719
+ to a max depth of 100, to preserve memory during reporting. The
28720
+ printed path will also be limited to 100 nodes counting from the
28721
+ TopMemoryContext.
28722
+ </para>
28723
+ </listitem>
28724
+ <listitem>
28725
+ <para>
28726
+ <parameter>level</parameter> - The level in the tree of the current
28727
+ memory context.
28728
+ </para>
28729
+ </listitem>
28730
+ <listitem>
28731
+ <para>
28732
+ <parameter>total_bytes</parameter> - The total number of bytes
28733
+ allocated to this memory context.
28734
+ </para>
28735
+ </listitem>
28736
+ <listitem>
28737
+ <para>
28738
+ <parameter>total_nblocks</parameter> - The total number of blocks
28739
+ used for the allocated memory.
28740
+ </para>
28741
+ </listitem>
28742
+ <listitem>
28743
+ <para>
28744
+ <parameter>free_bytes</parameter> - The amount of free memory in
28745
+ this memory context.
28746
+ </para>
28747
+ </listitem>
28748
+ <listitem>
28749
+ <para>
28750
+ <parameter>free_chunks</parameter> - The number of chunks that
28751
+ <parameter>free_bytes</parameter> corresponds to.
28752
+ </para>
28753
+ </listitem>
28754
+ <listitem>
28755
+ <para>
28756
+ <parameter>used_bytes</parameter> - The total number of bytes
28757
+ currently occupied.
28758
+ </para>
28759
+ </listitem>
28760
+ <listitem>
28761
+ <para>
28762
+ <parameter>num_agg_contexts</parameter> - The number of memory
28763
+ contexts aggregated in the displayed statistics.
28764
+ </para>
28765
+ </listitem>
28766
+ <listitem>
28767
+ <para>
28768
+ <parameter>stats_timestamp</parameter> - When the statistics were
28769
+ extracted from the process.
28770
+ </para>
28771
+ </listitem>
28772
+ </itemizedlist>
28773
+ </para>
28774
+ <para>
28775
+ When <parameter>summary</parameter> is <literal>true</literal>, statistics
28776
+ for memory contexts at levels 1 and 2 are displayed, with level 1
28777
+ representing the root node (i.e., <literal>TopMemoryContext</literal>).
28778
+ Statistics for contexts on level 2 and below are aggregates of all
28779
+ child contexts' statistics, where <literal>num_agg_contexts</literal>
28780
+ indicate the number aggregated child contexts. When
28781
+ <parameter>summary</parameter> is <literal>false</literal>,
28782
+ <literal>the num_agg_contexts</literal> value is <literal>1</literal>,
28783
+ indicating that individual statistics are being displayed. The levels
28784
+ are limited to the first 100 contexts.
28785
+ </para>
28786
+ <para>
28787
+ Busy processes can delay reporting memory context statistics,
28788
+ <parameter>timeout</parameter> specifies the number of seconds
28789
+ to wait for updated statistics. <parameter>timeout</parameter> can be
28790
+ specified in fractions of a second.
28791
+ </para>
28792
+ <para>
28793
+ After receiving memory context statistics from the target process, it
28794
+ returns the results as one row per context. If all the contexts don't
28795
+ fit within the pre-determined size limit, the remaining context
28796
+ statistics are aggregated and a cumulative total is displayed. The
28797
+ <literal>num_agg_contexts</literal> column indicates the number of
28798
+ contexts aggregated in the displayed statistics. When
28799
+ <literal>num_agg_contexts</literal> is <literal>1</literal> is means
28800
+ that the context statistics are displayed separately.
28801
+ </para></entry>
28802
+ </row>
28803
+
28666
28804
<row>
28667
28805
<entry role="func_table_entry"><para role="func_signature">
28668
28806
<indexterm>
@@ -28802,6 +28940,39 @@ LOG: Grand total: 1651920 bytes in 201 blocks; 622360 free (88 chunks); 1029560
28802
28940
because it may generate a large number of log messages.
28803
28941
</para>
28804
28942
28943
+ <para>
28944
+ <function>pg_get_process_memory_contexts</function> can be used
28945
+ to request the memory contexts statistics of any postgres process. For example:
28946
+ <programlisting>
28947
+ postgres=# SELECT * FROM pg_get_process_memory_contexts(
28948
+ (SELECT pid FROM pg_stat_activity
28949
+ WHERE backend_type = 'checkpointer'),
28950
+ false, 0.5) LIMIT 1;
28951
+ -[ RECORD 1 ]----+------------------------------
28952
+ name | TopMemoryContext
28953
+ ident |
28954
+ type | AllocSet
28955
+ path | {1}
28956
+ level | 1
28957
+ total_bytes | 90304
28958
+ total_nblocks | 3
28959
+ free_bytes | 2880
28960
+ free_chunks | 1
28961
+ used_bytes | 87424
28962
+ num_agg_contexts | 1
28963
+ stats_timestamp | 2025-03-24 13:55:47.796698+01
28964
+ </programlisting>
28965
+ <note>
28966
+ <para>
28967
+ While <function>pg_get_process_memory_contexts</function> can be used to
28968
+ query memory contexts of the local backend,
28969
+ <structname>pg_backend_memory_contexts</structname>
28970
+ (see <xref linkend="view-pg-backend-memory-contexts"/> for more details)
28971
+ will be less resource intensive when only the local backend is of interest.
28972
+ </para>
28973
+ </note>
28974
+ </para>
28975
+
28805
28976
</sect2>
28806
28977
28807
28978
<sect2 id="functions-admin-backup">
0 commit comments