From affda4db2f08e45c047935a1724c7f94bfd17d16 Mon Sep 17 00:00:00 2001 From: TatsuyaKawata Date: Wed, 18 Nov 2026 23:33:59 +0900 Subject: [PATCH v1] Report the peak total storage of Recursive Union nodes in EXPLAIN Recursive Union uses two tuplestores and reuses them across iterations with tuplestore_clear(). EXPLAIN ANALYZE showed the sum of each tuplestore's lifetime peak, which can add together peaks from different iterations that never existed at the same time. Add tuplestore_get_stats_since_clear(), which reports the statistics since the last tuplestore_clear(), and use it to record the total space used by both tuplestores just before they are cleared, keeping the maximum. The storage type is now Disk if either tuplestore spilled, as is done for Function Scan. --- src/backend/commands/explain.c | 34 +++++++++++--------- src/backend/executor/nodeRecursiveunion.c | 38 +++++++++++++++++++++++ src/backend/utils/sort/tuplestore.c | 35 ++++++++++++++++++--- src/include/nodes/execnodes.h | 3 ++ src/include/utils/tuplestore.h | 2 ++ 5 files changed, 93 insertions(+), 19 deletions(-) diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c index b8d1d0ec751..973acbc08ad 100644 --- a/src/backend/commands/explain.c +++ b/src/backend/commands/explain.c @@ -3668,29 +3668,33 @@ show_table_func_scan_info(TableFuncScanState *tscanstate, ExplainState *es) static void show_recursive_union_info(RecursiveUnionState *rstate, ExplainState *es) { - bool maxUsedDisk, - tempUsedDisk; - int64 maxSpaceUsed, - tempSpaceUsed; + Tuplestorestate *tables[2] = {rstate->working_table, + rstate->intermediate_table}; + bool totalDiskUsed = false; + int64 totalSpaceUsed = 0; if (!es->analyze) return; /* - * Recursive union node uses two tuplestores. We employ the storage type - * from one of them which consumed more memory/disk than the other. The - * storage size is sum of the two. + * Similar to nodeRecursiveunion.c's update_storage_stats() function, we + * need to fetch the stats from the final iteration and use those values + * if they're higher than what's already stored in RecursiveUnionState. */ - tuplestore_get_stats(rstate->working_table, &tempUsedDisk, - &tempSpaceUsed); - tuplestore_get_stats(rstate->intermediate_table, &maxUsedDisk, - &maxSpaceUsed); + for (int i = 0; i < 2; i++) + { + bool diskUsed; + int64 spaceUsed; + + tuplestore_get_stats_since_clear(tables[i], &diskUsed, &spaceUsed); + totalSpaceUsed += spaceUsed; + totalDiskUsed |= diskUsed; + } - if (tempSpaceUsed > maxSpaceUsed) - maxUsedDisk = tempUsedDisk; + rstate->maxSpaceUsed = Max(rstate->maxSpaceUsed, totalSpaceUsed); + rstate->usedDisk |= totalDiskUsed; - maxSpaceUsed += tempSpaceUsed; - show_storage_info(maxUsedDisk, maxSpaceUsed, es); + show_storage_info(rstate->usedDisk, rstate->maxSpaceUsed, es); } /* diff --git a/src/backend/executor/nodeRecursiveunion.c b/src/backend/executor/nodeRecursiveunion.c index 7166397e59b..e8a7b4f555d 100644 --- a/src/backend/executor/nodeRecursiveunion.c +++ b/src/backend/executor/nodeRecursiveunion.c @@ -24,6 +24,7 @@ #include "utils/memutils.h" #include "utils/tuplestore.h" +static void update_storage_stats(RecursiveUnionState *node); /* @@ -134,6 +135,8 @@ ExecRecursiveUnion(PlanState *pstate) * table. This saves a round of free/malloc from creating a new * tuple store. */ + if (node->ps.instrument) + update_storage_stats(node); tuplestore_clear(node->working_table); swaptemp = node->working_table; @@ -336,6 +339,41 @@ ExecReScanRecursiveUnion(RecursiveUnionState *node) /* reset processing state */ node->recursing = false; node->intermediate_empty = true; + if (node->ps.instrument) + update_storage_stats(node); tuplestore_clear(node->working_table); tuplestore_clear(node->intermediate_table); } + +/* + * update_storage_stats + * Update the storage statistics for EXPLAIN ANALYZE with the storage + * space used by the working and intermediate tables. + * + * We expect this to be called before clearing the tables so that the + * maxSpaceUsed and usedDisk fields are not forgotten between iterations and + * rescans. We calculate the total space used by both tables since they were + * last cleared and store that if it's higher than any previous iteration's. + * "usedDisk" is marked if either table spilled during any iteration. + */ +static void +update_storage_stats(RecursiveUnionState *node) +{ + Tuplestorestate *tables[2] = {node->working_table, + node->intermediate_table}; + int64 totalSpaceUsed = 0; + + /* Calculate the sum of the space used by both tables. */ + for (int i = 0; i < 2; i++) + { + bool usedDisk; + int64 spaceUsed; + + tuplestore_get_stats_since_clear(tables[i], &usedDisk, &spaceUsed); + totalSpaceUsed += spaceUsed; + node->usedDisk |= usedDisk; + } + + /* Record this one if it beats the previous best. */ + node->maxSpaceUsed = Max(node->maxSpaceUsed, totalSpaceUsed); +} diff --git a/src/backend/utils/sort/tuplestore.c b/src/backend/utils/sort/tuplestore.c index 610ddf00532..97c4d4a1b7b 100644 --- a/src/backend/utils/sort/tuplestore.c +++ b/src/backend/utils/sort/tuplestore.c @@ -110,6 +110,10 @@ struct Tuplestorestate bool truncated; /* tuplestore_trim has removed tuples? */ bool usedDisk; /* used by tuplestore_get_stats() */ int64 maxSpace; /* used by tuplestore_get_stats() */ + bool usedDiskSinceClear; /* used by + * tuplestore_get_stats_since_clear() */ + int64 maxSpaceSinceClear; /* used by + * tuplestore_get_stats_since_clear() */ int64 availMem; /* remaining memory available, in bytes */ int64 allowedMem; /* total memory allowed, in bytes */ int64 tuples; /* number of tuples added */ @@ -266,6 +270,8 @@ tuplestore_begin_common(int eflags, bool interXact, int maxKBytes) state->truncated = false; state->usedDisk = false; state->maxSpace = 0; + state->usedDiskSinceClear = false; + state->maxSpaceSinceClear = 0; state->allowedMem = maxKBytes * (int64) 1024; state->availMem = state->allowedMem; state->myfile = NULL; @@ -433,6 +439,8 @@ tuplestore_clear(Tuplestorestate *state) /* update the maxSpace before doing any USEMEM/FREEMEM adjustments */ tuplestore_updatemax(state); + state->usedDiskSinceClear = false; + state->maxSpaceSinceClear = 0; if (state->myfile) BufFileClose(state->myfile); @@ -1552,20 +1560,24 @@ tuplestore_trim(Tuplestorestate *state) static void tuplestore_updatemax(Tuplestorestate *state) { + int64 spaceUsed; + if (state->status == TSS_INMEM) - state->maxSpace = Max(state->maxSpace, - state->allowedMem - state->availMem); + spaceUsed = state->allowedMem - state->availMem; else { - state->maxSpace = Max(state->maxSpace, - BufFileSize(state->myfile)); + spaceUsed = BufFileSize(state->myfile); /* * usedDisk never gets set to false again after spilling to disk, even * if tuplestore_clear() is called and new tuples go to memory again. */ state->usedDisk = true; + state->usedDiskSinceClear = true; } + + state->maxSpace = Max(state->maxSpace, spaceUsed); + state->maxSpaceSinceClear = Max(state->maxSpaceSinceClear, spaceUsed); } /* @@ -1583,6 +1595,21 @@ tuplestore_get_stats(Tuplestorestate *state, bool *used_disk, int64 *max_space) *max_space = state->maxSpace; } +/* + * tuplestore_get_stats_since_clear + * Like tuplestore_get_stats(), but the statistics only cover the period + * since the tuplestore was created or last cleared by tuplestore_clear(). + */ +void +tuplestore_get_stats_since_clear(Tuplestorestate *state, bool *used_disk, + int64 *max_space) +{ + tuplestore_updatemax(state); + + *used_disk = state->usedDiskSinceClear; + *max_space = state->maxSpaceSinceClear; +} + /* * tuplestore_in_memory * diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index e4fd53c383b..bca145b07bd 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -1592,6 +1592,9 @@ typedef struct RecursiveUnionState bool intermediate_empty; Tuplestorestate *working_table; Tuplestorestate *intermediate_table; + /* storage statistics to report in EXPLAIN ANALYZE */ + int64 maxSpaceUsed; + bool usedDisk; /* Remaining fields are unused in UNION ALL case */ Oid *eqfuncoids; /* per-grouping-field equality fns */ FmgrInfo *hashfunctions; /* per-grouping-field hash fns */ diff --git a/src/include/utils/tuplestore.h b/src/include/utils/tuplestore.h index 12dc51b81d2..b612dd11515 100644 --- a/src/include/utils/tuplestore.h +++ b/src/include/utils/tuplestore.h @@ -68,6 +68,8 @@ extern void tuplestore_trim(Tuplestorestate *state); extern void tuplestore_get_stats(Tuplestorestate *state, bool *used_disk, int64 *max_space); +extern void tuplestore_get_stats_since_clear(Tuplestorestate *state, + bool *used_disk, int64 *max_space); extern bool tuplestore_in_memory(Tuplestorestate *state); -- 2.34.1