pgsql: Add EXPLAIN (MEMORY) to report planner memory consumption

From: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Add EXPLAIN (MEMORY) to report planner memory consumption
Date: 2024-01-29 16:55:27
Message-ID: E1rUUut-003iEY-FZ@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers pgsql-hackers

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

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/5de890e3610d5a12cdaea36413d967cf5c544e20

Modified Files
--------------
contrib/auto_explain/auto_explain.c | 2 +
doc/src/sgml/ref/explain.sgml | 14 ++++
src/backend/commands/explain.c | 152 ++++++++++++++++++++++++++++------
src/backend/commands/prepare.c | 22 ++++-
src/backend/utils/mmgr/mcxt.c | 13 +++
src/include/commands/explain.h | 4 +-
src/include/utils/memutils.h | 2 +
src/test/regress/expected/explain.out | 76 +++++++++++++++++
src/test/regress/sql/explain.sql | 8 ++
9 files changed, 265 insertions(+), 28 deletions(-)

Responses

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2024-01-29 17:06:37 pgsql: Fix incompatibilities with libxml2 >= 2.12.0.
Previous Message Heikki Linnakangas 2024-01-29 11:52:37 pgsql: Fix locking when fixing an incomplete split of a GIN internal pa

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2024-01-29 17:05:42 Re: psql: add \create_function command
Previous Message Steve Chavez 2024-01-29 16:54:45 Re: psql: add \create_function command