After a bit more polish I landed with the attached, which I most likely will go ahead with after another round in CI.
Thank you for refining the code. The changes look good to me. Regression tests ran smoothly in parallel with the memory monitoring function, pgbench results with the following custom script also shows good performance. ```
SELECT * FROM pg_get_process_memory_contexts( (SELECT pid FROM pg_stat_activity ORDER BY random() LIMIT 1) , false, 5); ```