Re: Parent/child context relation in pg_get_backend_memory_contexts()
Robert Haas <robertmhaas@gmail.com>
From: Robert Haas <robertmhaas@gmail.com>
To: David Rowley <dgrowleyml@gmail.com>
Cc: Melih Mutlu <m.melihmutlu@gmail.com>,
Michael Paquier <michael@paquier.xyz>, torikoshia <torikoshia@oss.nttdata.com>, Andres Freund <andres@anarazel.de>, Stephen Frost <sfrost@snowman.net>, PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2024-07-11T20:09:31Z
Lists: pgsql-hackers
On Wed, Jul 10, 2024 at 9:16 PM David Rowley <dgrowleyml@gmail.com> wrote: > Melih and I talked about this in a meeting yesterday evening. I think > I'm about on the fence about having the IDs in leaf-to-root or > root-to-leaf. My main concern about which order is chosen is around > how easy it is to write hierarchical queries. I think I'd feel better > about having it in root-to-leaf order if "level" was 1-based rather > than 0-based. That would allow querying CacheMemoryContext and all of > its descendants with: > > WITH c AS (SELECT * FROM pg_backend_memory_contexts) > SELECT c1.* > FROM c c1, c c2 > WHERE c2.name = 'CacheMemoryContext' > AND c1.path[c2.level] = c2.path[c2.level]; I don't object to making it 1-based. > Ideally, no CTE would be needed here, but unfortunately, there's no > way to know the CacheMemoryContext's ID beforehand. We could make the > ID more stable if we did a breadth-first traversal of the context. > i.e., assign IDs in level order. This would stop TopMemoryContext's > 2nd child getting a different ID if its first child became a parent > itself. Do we ever have contexts with the same name at the same level? Could we just make the path an array of strings, so that you could then say something like this... SELECT * FROM pg_backend_memory_contexts where path[2] = 'CacheMemoryContext' ...and get all the things with that in the path? > select * from pg_backend_memory_contexts; > -- Observe that CacheMemoryContext has ID=22 and level=2. Get the > total of that and all of its descendants. > select sum(total_bytes) from pg_backend_memory_contexts where path[2] = 22; > -- or just it and direct children > select sum(total_bytes) from pg_backend_memory_contexts where path[2] > = 22 and level <= 3; I'm doubtful about this because nothing prevents the set of memory contexts from changing between one query and the next. We should try to make it so that it's easy to get what you want in a single query. -- Robert Haas EDB: http://www.enterprisedb.com
Commits
-
Add missing pointer dereference in pg_backend_memory_contexts view
- da87dc07f16e 18.0 landed
-
Add path column to pg_backend_memory_contexts view
- 32d3ed8165f8 18.0 landed
-
Allow memory contexts to have both fixed and variable ident strings.
- 442accc3fe0c 11.0 cited