Make levels 1-based in pg_log_backend_memory_contexts()
authorDavid Rowley <drowley@postgresql.org>
Thu, 17 Apr 2025 21:04:28 +0000 (09:04 +1200)
committerDavid Rowley <drowley@postgresql.org>
Thu, 17 Apr 2025 21:04:28 +0000 (09:04 +1200)
Both pg_get_process_memory_contexts() and pg_backend_memory_contexts
have 1-based levels, whereas pg_log_backend_memory_contexts() was using
0-based levels.  Align these.

This results in slightly saner behavior from MemoryContextStatsDetail()
in regards to the max_level.  Previously it would stop at 1 level before
the maximum requested level rather than at that level.

Reported-by: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Author: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Author: David Rowley <drowleyml@gmail.com
Reviewed-by: Melih Mutlu <m.melihmutlu@gmail.com>
Reviewed-by: Rahila Syed <rahilasyed90@gmail.com>
Discussion: https://postgr.es/m/395ea5d4fe190480efa95bf533485c70@oss.nttdata.com

src/backend/utils/mmgr/mcxt.c

index 1f5ebf2e1242183fc1826db0ca6a5e384a7187fe..e9aab36d110666e7579ba883be5a51dbf7363e26 100644 (file)
@@ -873,7 +873,7 @@ MemoryContextStatsDetail(MemoryContext context,
        print_location = PRINT_STATS_TO_LOGS;
 
    /* num_contexts report number of contexts aggregated in the output */
-   MemoryContextStatsInternal(context, 0, max_level, max_children,
+   MemoryContextStatsInternal(context, 1, max_level, max_children,
                               &grand_totals, print_location, &num_contexts);
 
    if (print_to_stderr)
@@ -968,7 +968,7 @@ MemoryContextStatsInternal(MemoryContext context, int level,
     */
    child = context->firstchild;
    ichild = 0;
-   if (level < max_level && !stack_is_too_deep())
+   if (level <= max_level && !stack_is_too_deep())
    {
        for (; child != NULL && ichild < max_children;
             child = child->nextchild, ichild++)
@@ -1003,7 +1003,7 @@ MemoryContextStatsInternal(MemoryContext context, int level,
 
        if (print_location == PRINT_STATS_TO_STDERR)
        {
-           for (int i = 0; i <= level; i++)
+           for (int i = 0; i < level; i++)
                fprintf(stderr, "  ");
            fprintf(stderr,
                    "%d more child contexts containing %zu total in %zu blocks; %zu free (%zu chunks); %zu used\n",
@@ -1104,7 +1104,7 @@ MemoryContextStatsPrint(MemoryContext context, void *passthru,
 
    if (print_to_stderr)
    {
-       for (i = 0; i < level; i++)
+       for (i = 1; i < level; i++)
            fprintf(stderr, "  ");
        fprintf(stderr, "%s: %s%s\n", name, stats_string, truncated_ident);
    }
@@ -1585,12 +1585,11 @@ ProcessGetMemoryContextInterrupt(void)
        {
            MemoryContextCounters grand_totals;
            int         num_contexts = 0;
-           int         level = 0;
 
            path = NIL;
            memset(&grand_totals, 0, sizeof(grand_totals));
 
-           MemoryContextStatsInternal(c, level, 100, 100, &grand_totals,
+           MemoryContextStatsInternal(c, 1, 100, 100, &grand_totals,
                                       PRINT_STATS_NONE, &num_contexts);
 
            path = compute_context_path(c, context_id_lookup);