Waiting for PostgreSQL 18 – Add function to get memory context stats for processes

On 8th of April 2025, Daniel Gustafsson committed patch:

Add function to get memory context stats for processes
 
This adds a function for retrieving memory context statistics
and information from backends as well as auxiliary processes.
The intended usecase is cluster debugging when under memory
pressure or unanticipated memory usage characteristics.
 
When calling the function it sends a signal to the specified
process to submit statistics regarding its memory contexts
into dynamic shared memory.  Each memory context is returned
in detail, followed by a cumulative total in case the number
of contexts exceed the max allocated amount of shared memory.
Each process is limited to use at most 1Mb memory for this.
 
A summary can also be explicitly requested by the user, this
will return the TopMemoryContext and a cumulative total of
all lower contexts.
 
In order to not block on busy processes the caller specifies
the number of seconds during which to retry before timing out.
In the case where no statistics are published within the set
timeout,  the last known statistics are returned, or NULL if
no previously published statistics exist.  This allows dash-
board type queries to continually publish even if the target
process is temporarily congested.  Context records contain a
timestamp to indicate when they were submitted.
 
Author: Rahila Syed <rahilasyed90@gmail.com>
Reviewed-by: Daniel Gustafsson <daniel@yesql.se>
Reviewed-by: Andres Freund <andres@anarazel.de>
Reviewed-by: Tomas Vondra <tomas@vondra.me>
Reviewed-by: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Reviewed-by: Fujii Masao <masao.fujii@oss.nttdata.com>
Reviewed-by: Alexander Korotkov <aekorotkov@gmail.com>
Discussion: https://postgr.es/m/CAH2L28v8mc9HDt8QoSJ8TRmKau_8FM_HKS41NeO9-6ZAkuZKXw@mail.gmail.com

This is something that I don't really think will be useful for many people, but it will definitely help some.

Ever since PostgreSQL 14 we have pg_backend_memory_contexts view, that shows memory usage information for current backend. It looks like this:

=$ select * from pg_backend_memory_contexts order by path limit 5;
           name           │    ident     │   type   │ level │   path   │ total_bytes │ total_nblocks │ free_bytes │ free_chunks │ used_bytes
──────────────────────────┼──────────────┼──────────┼───────┼──────────┼─────────────┼───────────────┼────────────┼─────────────┼────────────
 TopMemoryContext         │ [null]       │ AllocSet │     1{1}1405446308814137456
 collation cache          │ [null]       │ AllocSet │     2{1,2}81921678401408
 TableSpace cache         │ [null]       │ AllocSet │     2{1,3}81921211206080
 RegexpCacheMemoryContext │ [null]       │ AllocSet │     2{1,4}102417840240
 RegexpMemoryContext      │ ^(.*cont.*)$ │ AllocSet │     3{1,4,30}1337656992106384
(5 rows)

Now, we can also get such information for other Pg processes, including non-backend ones (like wal writer):

=$ \! ps -u pgdba u | grep walwriter
pgdba       1420  0.0  0.0 225636  9536 ?        Ss   May01   0:05 postgres: walwriter
 
=$ select * from pg_get_process_memory_contexts( 1253, false, 1 ) order by path limit 5^J;
                 name                  │ ident  │   type   │   path   │ level │ total_bytes │ total_nblocks │ free_bytes │ free_chunks │ used_bytes │ num_agg_contexts │        stats_timestamp
───────────────────────────────────────┼────────┼──────────┼──────────┼───────┼─────────────┼───────────────┼────────────┼─────────────┼────────────┼──────────────────┼───────────────────────────────
 TopMemoryContext                      │ [null] │ AllocSet │ {1}11026644278409988012025-05-05 15:02:59.764672+02
 smgr relation table[null] │ AllocSet │ {1,2}23276831684881592012025-05-05 15:02:59.764672+02
 Background Writer                     │ [null] │ AllocSet │ {1,3}2245762226488192812025-05-05 15:02:59.764672+02
 pg_get_remote_backend_memory_contexts │ [null] │ AllocSet │ {1,3,11}38192114720672012025-05-05 15:02:59.764672+02
 LOCALLOCK hash                        │ [null] │ AllocSet │ {1,4}2819215760761612025-05-05 15:02:59.764672+02
(5 rows)

First argument is pid of process.

Second argument, if it's true – it will just show contexts that are directly below TopMemoryContext. If it's false, it generates much more data, because it shows data about everything that is there (will show example at the end).

Third argument is timeout in seconds. If getting data from backend takes longer than this, previous data will be returned, or NULL if there is no previous data.

So this means we can easily build some kind of “show-memory-usage-in-time" dashboards/graphs/tables, without overwhelming server.

Very, very nice. Not really for everyday use, but if you'll see Pg process that eats surprising amount of memory, you can at least see why, and what is it using it for.

Thanks a lot, to everyone involved.

And this is promised example of how it looks for backend. It wasn't doing much, but still something (pgbench):

=$ select * from pg_get_process_memory_contexts( 649620, false, 100 ) order by path ;
                 name                  │                     ident                      │   type   │    path    │ level │ total_bytes │ total_nblocks │ free_bytes │ free_chunks │ used_bytes │ num_agg_contexts │        stats_timestamp
───────────────────────────────────────┼────────────────────────────────────────────────┼──────────┼────────────┼───────┼─────────────┼───────────────┼────────────┼─────────────┼────────────┼──────────────────┼───────────────────────────────
 TopMemoryContext                      │ [null]                                         │ AllocSet │ {1}1152904731441114976012025-05-05 15:09:57.330051+02
 TableSpace cache                      │ [null]                                         │ AllocSet │ {1,2}28192121120608012025-05-05 15:09:57.330051+02
 Operator lookup cache                 │ [null]                                         │ AllocSet │ {1,3}22457621077631380012025-05-05 15:09:57.330051+02
 search_path processing cache          │ [null]                                         │ AllocSet │ {1,4}28192156168257612025-05-05 15:09:57.330051+02
 RowDescriptionContext                 │ [null]                                         │ AllocSet │ {1,5}28192169120128012025-05-05 15:09:57.330051+02
 MessageContext                        │ [null]                                         │ AllocSet │ {1,6}23276831373631903212025-05-05 15:09:57.330051+02
 Operator class cache                  │ [null]                                         │ AllocSet │ {1,7}2819215760761612025-05-05 15:09:57.330051+02
 smgr relation table[null]                                         │ AllocSet │ {1,8}23276831684881592012025-05-05 15:09:57.330051+02
 PgStat Shared Ref Hash                │ [null]                                         │ AllocSet │ {1,9}2928027040857612025-05-05 15:09:57.330051+02
 PgStat Shared Ref[null]                                         │ AllocSet │ {1,10}28192435122468012025-05-05 15:09:57.330051+02
 PgStat Pending                        │ [null]                                         │ AllocSet │ {1,11}21638451435246203212025-05-05 15:09:57.330051+02
 TopTransactionContext                 │ [null]                                         │ AllocSet │ {1,12}28192156484254412025-05-05 15:09:57.330051+02
 pg_get_remote_backend_memory_contexts │ [null]                                         │ AllocSet │ {1,12,25}316384265042988012025-05-05 15:09:57.330051+02
 TransactionAbortContext               │ [null]                                         │ AllocSet │ {1,13}232768132528024012025-05-05 15:09:57.330051+02
 Portal hash                           │ [null]                                         │ AllocSet │ {1,14}2819215760761612025-05-05 15:09:57.330051+02
 TopPortalContext                      │ [null]                                         │ AllocSet │ {1,15}2819217952124012025-05-05 15:09:57.330051+02
 Relcache by OID                       │ [null]                                         │ AllocSet │ {1,16}2163842352821285612025-05-05 15:09:57.330051+02
 CacheMemoryContext                    │ [null]                                         │ AllocSet │ {1,17}2524288734344048994412025-05-05 15:09:57.330051+02
 index info                            │ pgbench_branches_pkey                          │ AllocSet │ {1,17,26}3204826721137612025-05-05 15:09:57.330051+02
 index info                            │ pgbench_tellers_pkey                           │ AllocSet │ {1,17,27}3204825921145612025-05-05 15:09:57.330051+02
 index info                            │ pg_statistic_ext_relid_index                   │ AllocSet │ {1,17,28}3204826721137612025-05-05 15:09:57.330051+02
 index info                            │ pgbench_accounts_pkey                          │ AllocSet │ {1,17,29}3204825921145612025-05-05 15:09:57.330051+02
 index info                            │ pg_index_indrelid_index                        │ AllocSet │ {1,17,30}3204825921145612025-05-05 15:09:57.330051+02
 index info                            │ pg_constraint_conrelid_contypid_conname_index  │ AllocSet │ {1,17,31}3307229281214412025-05-05 15:09:57.330051+02
 index info                            │ pg_db_role_setting_databaseid_rol_index        │ AllocSet │ {1,17,32}33072210321204012025-05-05 15:09:57.330051+02
 index info                            │ pg_opclass_am_name_nsp_index                   │ AllocSet │ {1,17,33}33072211201195212025-05-05 15:09:57.330051+02
 index info                            │ pg_foreign_data_wrapper_name_index             │ AllocSet │ {1,17,34}3204827203132812025-05-05 15:09:57.330051+02
 index info                            │ pg_enum_oid_index                              │ AllocSet │ {1,17,35}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_class_relname_nsp_index                     │ AllocSet │ {1,17,36}3307229842208812025-05-05 15:09:57.330051+02
 index info                            │ pg_foreign_server_oid_index                    │ AllocSet │ {1,17,37}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_publication_pubname_index                   │ AllocSet │ {1,17,38}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_statistic_relid_att_inh_index               │ AllocSet │ {1,17,39}3307227681230412025-05-05 15:09:57.330051+02
 index info                            │ pg_cast_source_target_index                    │ AllocSet │ {1,17,40}3307229842208812025-05-05 15:09:57.330051+02
 index info                            │ pg_language_name_index                         │ AllocSet │ {1,17,41}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_transform_oid_index                         │ AllocSet │ {1,17,42}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_collation_oid_index                         │ AllocSet │ {1,17,43}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_amop_fam_strat_index                        │ AllocSet │ {1,17,44}3326437362252812025-05-05 15:09:57.330051+02
 index info                            │ pg_index_indexrelid_index                      │ AllocSet │ {1,17,45}3204825922145612025-05-05 15:09:57.330051+02
 index info                            │ pg_ts_template_tmplname_index                  │ AllocSet │ {1,17,46}33072212243184812025-05-05 15:09:57.330051+02
 index info                            │ pg_ts_config_map_index                         │ AllocSet │ {1,17,47}33072211201195212025-05-05 15:09:57.330051+02
 index info                            │ pg_opclass_oid_index                           │ AllocSet │ {1,17,48}3204825922145612025-05-05 15:09:57.330051+02
 index info                            │ pg_foreign_data_wrapper_oid_index              │ AllocSet │ {1,17,49}3204827203132812025-05-05 15:09:57.330051+02
 index info                            │ pg_publication_namespace_oid_index             │ AllocSet │ {1,17,50}3204827203132812025-05-05 15:09:57.330051+02
 index info                            │ pg_event_trigger_evtname_index                 │ AllocSet │ {1,17,51}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_statistic_ext_name_index                    │ AllocSet │ {1,17,52}33072212243184812025-05-05 15:09:57.330051+02
 index info                            │ pg_publication_oid_index                       │ AllocSet │ {1,17,53}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_ts_dict_oid_index                           │ AllocSet │ {1,17,54}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_event_trigger_oid_index                     │ AllocSet │ {1,17,55}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_conversion_default_index                    │ AllocSet │ {1,17,56}3224021520208812025-05-05 15:09:57.330051+02
 index info                            │ pg_operator_oprname_l_r_n_index                │ AllocSet │ {1,17,57}3326438162244812025-05-05 15:09:57.330051+02
 index info                            │ pg_trigger_tgrelid_tgname_index                │ AllocSet │ {1,17,58}33072212243184812025-05-05 15:09:57.330051+02
 index info                            │ pg_extension_oid_index                         │ AllocSet │ {1,17,59}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_enum_typid_label_index                      │ AllocSet │ {1,17,60}33072212243184812025-05-05 15:09:57.330051+02
 index info                            │ pg_ts_config_oid_index                         │ AllocSet │ {1,17,61}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_user_mapping_oid_index                      │ AllocSet │ {1,17,62}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_opfamily_am_name_nsp_index                  │ AllocSet │ {1,17,63}33072211201195212025-05-05 15:09:57.330051+02
 index info                            │ pg_foreign_table_relid_index                   │ AllocSet │ {1,17,64}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_type_oid_index                              │ AllocSet │ {1,17,65}3204825922145612025-05-05 15:09:57.330051+02
 index info                            │ pg_aggregate_fnoid_index                       │ AllocSet │ {1,17,66}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_constraint_oid_index                        │ AllocSet │ {1,17,67}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_rewrite_rel_rulename_index                  │ AllocSet │ {1,17,68}33072212243184812025-05-05 15:09:57.330051+02
 index info                            │ pg_ts_parser_prsname_index                     │ AllocSet │ {1,17,69}33072212243184812025-05-05 15:09:57.330051+02
 index info                            │ pg_ts_config_cfgname_index                     │ AllocSet │ {1,17,70}33072212243184812025-05-05 15:09:57.330051+02
 index info                            │ pg_ts_parser_oid_index                         │ AllocSet │ {1,17,71}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_publication_rel_prrelid_prpubid_index       │ AllocSet │ {1,17,72}33072211121196012025-05-05 15:09:57.330051+02
 index info                            │ pg_operator_oid_index                          │ AllocSet │ {1,17,73}3204825922145612025-05-05 15:09:57.330051+02
 index info                            │ pg_namespace_nspname_index                     │ AllocSet │ {1,17,74}3204825922145612025-05-05 15:09:57.330051+02
 index info                            │ pg_ts_template_oid_index                       │ AllocSet │ {1,17,75}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_amop_opr_fam_index                          │ AllocSet │ {1,17,76}3307229601211212025-05-05 15:09:57.330051+02
 index info                            │ pg_default_acl_role_nsp_obj_index              │ AllocSet │ {1,17,77}33072210882198412025-05-05 15:09:57.330051+02
 index info                            │ pg_collation_name_enc_nsp_index                │ AllocSet │ {1,17,78}33072211201195212025-05-05 15:09:57.330051+02
 index info                            │ pg_publication_rel_oid_index                   │ AllocSet │ {1,17,79}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_range_rngtypid_index                        │ AllocSet │ {1,17,80}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_ts_dict_dictname_index                      │ AllocSet │ {1,17,81}33072212243184812025-05-05 15:09:57.330051+02
 index info                            │ pg_type_typname_nsp_index                      │ AllocSet │ {1,17,82}33072212243184812025-05-05 15:09:57.330051+02
 index info                            │ pg_opfamily_oid_index                          │ AllocSet │ {1,17,83}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_statistic_ext_oid_index                     │ AllocSet │ {1,17,84}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_statistic_ext_data_stxoid_inh_index         │ AllocSet │ {1,17,85}33072211922188012025-05-05 15:09:57.330051+02
 index info                            │ pg_class_oid_index                             │ AllocSet │ {1,17,86}3204825922145612025-05-05 15:09:57.330051+02
 index info                            │ pg_proc_proname_args_nsp_index                 │ AllocSet │ {1,17,87}33072211201195212025-05-05 15:09:57.330051+02
 index info                            │ pg_partitioned_table_partrelid_index           │ AllocSet │ {1,17,88}3204827203132812025-05-05 15:09:57.330051+02
 index info                            │ pg_range_rngmultitypid_index                   │ AllocSet │ {1,17,89}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_transform_type_lang_index                   │ AllocSet │ {1,17,90}33072212243184812025-05-05 15:09:57.330051+02
 index info                            │ pg_attribute_relid_attnum_index                │ AllocSet │ {1,17,91}3307229842208812025-05-05 15:09:57.330051+02
 index info                            │ pg_proc_oid_index                              │ AllocSet │ {1,17,92}3204825922145612025-05-05 15:09:57.330051+02
 index info                            │ pg_language_oid_index                          │ AllocSet │ {1,17,93}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_namespace_oid_index                         │ AllocSet │ {1,17,94}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_amproc_fam_proc_index                       │ AllocSet │ {1,17,95}3326438162244812025-05-05 15:09:57.330051+02
 index info                            │ pg_foreign_server_name_index                   │ AllocSet │ {1,17,96}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_attribute_relid_attnam_index                │ AllocSet │ {1,17,97}33072212243184812025-05-05 15:09:57.330051+02
 index info                            │ pg_publication_namespace_pnnspid_pnpubid_index │ AllocSet │ {1,17,98}33072211121196012025-05-05 15:09:57.330051+02
 index info                            │ pg_conversion_oid_index                        │ AllocSet │ {1,17,99}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_user_mapping_user_server_index              │ AllocSet │ {1,17,100}33072211922188012025-05-05 15:09:57.330051+02
 index info                            │ pg_subscription_rel_srrelid_srsubid_index      │ AllocSet │ {1,17,101}33072211922188012025-05-05 15:09:57.330051+02
 index info                            │ pg_sequence_seqrelid_index                     │ AllocSet │ {1,17,102}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_extension_name_index                        │ AllocSet │ {1,17,103}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_conversion_name_nsp_index                   │ AllocSet │ {1,17,104}33072212243184812025-05-05 15:09:57.330051+02
 index info                            │ pg_authid_oid_index                            │ AllocSet │ {1,17,105}3204825922145612025-05-05 15:09:57.330051+02
 index info                            │ pg_auth_members_member_role_index              │ AllocSet │ {1,17,106}33072210882198412025-05-05 15:09:57.330051+02
 index info                            │ pg_subscription_oid_index                      │ AllocSet │ {1,17,107}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_parameter_acl_oid_index                     │ AllocSet │ {1,17,108}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_tablespace_oid_index                        │ AllocSet │ {1,17,109}3204825922145612025-05-05 15:09:57.330051+02
 index info                            │ pg_parameter_acl_parname_index                 │ AllocSet │ {1,17,110}3204827522129612025-05-05 15:09:57.330051+02
 index info                            │ pg_shseclabel_object_index                     │ AllocSet │ {1,17,111}33072211201195212025-05-05 15:09:57.330051+02
 index info                            │ pg_replication_origin_roname_index             │ AllocSet │ {1,17,112}3204827203132812025-05-05 15:09:57.330051+02
 index info                            │ pg_database_datname_index                      │ AllocSet │ {1,17,113}3204825922145612025-05-05 15:09:57.330051+02
 index info                            │ pg_subscription_subname_index                  │ AllocSet │ {1,17,114}33072212243184812025-05-05 15:09:57.330051+02
 index info                            │ pg_replication_origin_roiident_index           │ AllocSet │ {1,17,115}3204827203132812025-05-05 15:09:57.330051+02
 index info                            │ pg_auth_members_role_member_index              │ AllocSet │ {1,17,116}33072210882198412025-05-05 15:09:57.330051+02
 index info                            │ pg_database_oid_index                          │ AllocSet │ {1,17,117}3204825922145612025-05-05 15:09:57.330051+02
 index info                            │ pg_authid_rolname_index                        │ AllocSet │ {1,17,118}3204825922145612025-05-05 15:09:57.330051+02
 LOCALLOCK hash                        │ [null]                                         │ AllocSet │ {1,18}2819215760761612025-05-05 15:09:57.330051+02
 WAL record construction               │ [null]                                         │ AllocSet │ {1,19}2502162638404383212025-05-05 15:09:57.330051+02
 PrivateRefCount                       │ [null]                                         │ AllocSet │ {1,20}28192126400555212025-05-05 15:09:57.330051+02
 MdSmgr                                │ [null]                                         │ AllocSet │ {1,21}2819217440075212025-05-05 15:09:57.330051+02
 GUCMemoryContext                      │ [null]                                         │ AllocSet │ {1,22}2245762844811612812025-05-05 15:09:57.330051+02
 GUC hash table[null]                                         │ AllocSet │ {1,22,119}33276831155262121612025-05-05 15:09:57.330051+02
 Timezones                             │ [null]                                         │ AllocSet │ {1,23}210412822640010148812025-05-05 15:09:57.330051+02
 ErrorContext                          │ [null]                                         │ AllocSet │ {1,24}2819217952524012025-05-05 15:09:57.330051+02
(119 rows)

One thought on “Waiting for PostgreSQL 18 – Add function to get memory context stats for processes”

  1. I am really, really excited about this. I think it will actually be extremely useful for getting better visibility into postgres memory usage – and hopefully help us continue to find and fix more code paths in postgres that allocate big chunks of memory without spilling to temp.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.