Add EXPLAIN (MEMORY) to report planner memory consumption
commit5de890e3610d5a12cdaea36413d967cf5c544e20
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Mon, 29 Jan 2024 16:53:03 +0000 (29 17:53 +0100)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Mon, 29 Jan 2024 16:53:03 +0000 (29 17:53 +0100)
treefa419c222f2f3c59c2e478ea305e21e6f1d05c66
parent6a1ea02c491d16474a6214603dce40b5b122d4d1
Add EXPLAIN (MEMORY) to report planner memory consumption

This adds a new "Memory:" line under the "Planning:" group (which
currently only has "Buffers:") when the MEMORY option is specified.

In order to make the reporting reasonably accurate, we create a separate
memory context for planner activities, to be used only when this option
is given.  The total amount of memory allocated by that context is
reported as "allocated"; we subtract memory in the context's freelists
from that and report that result as "used".  We use
MemoryContextStatsInternal() to obtain the quantities.

The code structure to show buffer usage during planning was not in
amazing shape, so I (Álvaro) modified the patch a bit to clean that up
in passing.

Author: Ashutosh Bapat
Reviewed-by: David Rowley, Andrey Lepikhov, Jian He, Andy Fan
Discussion: https://www.postgresql.org/message-id/CAExHW5sZA=5LJ_ZPpRO-w09ck8z9p7eaYAqq3Ks9GDfhrxeWBw@mail.gmail.com
contrib/auto_explain/auto_explain.c
doc/src/sgml/ref/explain.sgml
src/backend/commands/explain.c
src/backend/commands/prepare.c
src/backend/utils/mmgr/mcxt.c
src/include/commands/explain.h
src/include/utils/memutils.h
src/test/regress/expected/explain.out
src/test/regress/sql/explain.sql