Skip to content

explain: rework peak memory usage calculation - #2277

Open
HustonMmmavr wants to merge 2 commits into
apache:mainfrom
HustonMmmavr:issue_2130-v1
Open

HustonMmmavr wants to merge 2 commits into
apache:mainfrom
HustonMmmavr:issue_2130-v1

Conversation

@HustonMmmavr

Copy link
Copy Markdown
Contributor

Current version of peak memory usage has several disadvantages:

  • it was implemented for AllocSet (Aset) context only, but now postgres has other context types (Generation/Slab), so unconditional type casts of generic MemoryContext to AllocSet may lead to a SIGSEGV. The parent context (Aset) may have Generation child for sort operator (select * from tbl order by column, context creates at tuplesort_begin_batch). These lines
    /*
    * Mixing AllocSetContexts and other contexts will lose the accounting info.
    */
    if (!IsA(context, AllocSetContext) || (new_parent != NULL && !IsA(new_parent, AllocSetContext)))
    return;
    check that both contexts are Aset contexts, but that is not enough - see issue Possible SIGSEGV durning aset memory tracking #2130
  • The peak usage consumption for group is calculated at AllocSetGetPeakUsage/AllocSetGetPeakUsage_recurse - it recurses by the context tree and, as I see it, should sum up all peak usage within current and descendant group. There are two problems with summing up peak values - it is not accurate and real peak value may be less than sum of distinct peak values. Moreover AllocSetGetPeakUsage_recurse does not use returned value of recursive call - so child subgroups are not taken into account.

This patch

  • reworks old behaviour by creating separate tracking groups within generic MemoryContext
  • main functionality was moved to MemoryContext level (like group reparenting and so on), thus there is no need to use context specific methods
  • increment/decrement of current allocated memory size is still located at context implementation.
  • The tracking groups implemented as a tree structure - group may belong to some parent group, so peak values updating for all parents.
  • The calculation of memory usage is based on MemoryContext.mem_allocated (this field was introduced in postgres). It's more accurate because real allocated OS memory is tracked now

Closes #2130.

Fixes #ISSUE_Number

What does this PR do?

Type of Change

  • Bug fix (non-breaking change)
  • New feature (non-breaking change)
  • Breaking change (fix or feature with breaking changes)
  • Documentation update

Breaking Changes

Test Plan

  • Unit tests added/updated
  • Integration tests added/updated
  • Passed make installcheck
  • Passed make -C src/test installcheck-cbdb-parallel

Impact

Performance:

User-facing changes:

Dependencies:

Checklist

Additional Context

CI Skip Instructions


@HustonMmmavr
HustonMmmavr marked this pull request as draft November 9, 2026 13:44
@HustonMmmavr
HustonMmmavr marked this pull request as ready for review November 9, 2026 16:15
@HustonMmmavr HustonMmmavr changed the title peak mem usage (#2130): rework peak usage mechanics explain: rework peak memoy usage calculation Nov 9, 2026
@HustonMmmavr HustonMmmavr changed the title explain: rework peak memoy usage calculation explain: rework peak memory usage calculation Nov 9, 2026
@HustonMmmavr

Copy link
Copy Markdown
Contributor Author

@Alena0704 may I ask you for advice?
During rebase I've found that patch was in conflict with 3fb5b97, because of renaming MemoryContextGetPeakSpace to MemoryContextGetPeakUsage, I've resolved conflict only by changing the function call. Or, maybe it's worth to restore previous behavior (without TUPLESORT_ALLOWBOUNDED checks), because current approach allows to track peak usage for different context types?

@Alena0704
Alena0704 self-requested a review November 11, 2026 10:48
@Alena0704

Alena0704 commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

@Alena0704 may I ask you for advice? During rebase I've found that patch was in conflict with 3fb5b97, because of renaming MemoryContextGetPeakSpace to MemoryContextGetPeakUsage, I've resolved conflict only by changing the function call. Or, maybe it's worth to restore previous behavior (without TUPLESORT_ALLOWBOUNDED checks), because current approach allows to track peak usage for different context types?

My PR restored sort memory reporting, and during development I also fixed peak memory accounting for unbounded sorts that use Generation contexts. With your generic tracking, I think we can remove these checks and replace peakTupleMemory, provided the new accounting also includes the tuple array. memtuples is allocated in the parent maincontext, so reading the peak only from sortcontext would miss that memory. We need to include it too. Please also check that the statistics remain correct across Incremental Sort batches.

@HustonMmmavr

Copy link
Copy Markdown
Contributor Author

@Alena0704 may I ask you for advice? During rebase I've found that patch was in conflict with 3fb5b97, because of renaming MemoryContextGetPeakSpace to MemoryContextGetPeakUsage, I've resolved conflict only by changing the function call. Or, maybe it's worth to restore previous behavior (without TUPLESORT_ALLOWBOUNDED checks), because current approach allows to track peak usage for different context types?

My PR restored sort memory reporting, and during development I also fixed peak memory accounting for unbounded sorts that use Generation contexts. With your generic tracking, I think we can remove these checks and replace peakTupleMemory, provided the new accounting also includes the tuple array. memtuples is allocated in the parent maincontext, so reading the peak only from sortcontext would miss that memory. We need to include it too. Please also check that the statistics remain correct across Incremental Sort batches.

memtuples is allocated in the parent maincontext, so reading the peak only from sortcontext would miss that memory

So, as I understand, we should get the peak usage from the main context, because it would be a more accurate statistic, right?

Please also check that the statistics remain correct across Incremental Sort batches.

I've changed peak memory tracking at tuplesort.c in my repo - the CI passed, is this sufficient?

Comment thread src/include/utils/memutils.h Outdated
extern Size MemoryContextSetPeakSpace(MemoryContext context, Size nbytes);
extern Size MemoryContextGetCurrentUsage(MemoryContext context);
extern Size MemoryContextGetPeakUsage(MemoryContext context);
extern bool MemoryContextStartPeakUsageTracking(MemoryContext context);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returns bool, but all production callers discard the result. The only consumer is a test assertion, which can check the resulting tracker state instead. Could we make this function return void and keep the call outside Assert? This would simplify the API and remove dependency on Assert.

@Alena0704

Copy link
Copy Markdown
Collaborator

@Alena0704 may I ask you for advice? During rebase I've found that patch was in conflict with 3fb5b97, because of renaming MemoryContextGetPeakSpace to MemoryContextGetPeakUsage, I've resolved conflict only by changing the function call. Or, maybe it's worth to restore previous behavior (without TUPLESORT_ALLOWBOUNDED checks), because current approach allows to track peak usage for different context types?

My PR restored sort memory reporting, and during development I also fixed peak memory accounting for unbounded sorts that use Generation contexts. With your generic tracking, I think we can remove these checks and replace peakTupleMemory, provided the new accounting also includes the tuple array. memtuples is allocated in the parent maincontext, so reading the peak only from sortcontext would miss that memory. We need to include it too. Please also check that the statistics remain correct across Incremental Sort batches.

memtuples is allocated in the parent maincontext, so reading the peak only from sortcontext would miss that memory

So, as I understand, we should get the peak usage from the main context, because it would be a more accurate statistic, right?

Please also check that the statistics remain correct across Incremental Sort batches.

I've changed peak memory tracking at tuplesort.c in my repo - the CI passed, is this sufficient?

My commit focused on restoring correct memory reporting for sorts. I added a local solution for unbounded sorts because the existing peak memory accounting did not support the Generation context they use. The broader accounting problem in a mixed tree of memory contexts was outside the scope of that commit.

Yes, with the new generic tracker, reading the peak from maincontext is the right approach. The structure here is:

maincontext — AllocSet
├── memtuples[]
└── sortcontext — AllocSet
    └── tuplecontext — AllocSet or Generation

memtuples is allocated in maincontext, so reading the peak only from sortcontext misses the array's memory. The new tracker propagates changes in current usage up the hierarchy and records the peak of the combined current usage. Therefore, switching to maincontext, removing peakTupleMemory, and removing the separate tracker for sortcontext look appropriate here.

The guards around the choice of memory reporting mechanism can be removed. The TUPLESORT_ALLOWBOUNDED check that selects the allocator for tuplecontext should remain.

By your changes, do you mean the issue_2130-r branch, including commit 72e33132? That is the version I reviewed.

Passing CI is a good signal, but it only checks scenarios covered by the tests. I would add targeted regression tests for batches of different sizes, cases where memtuples accounts for a substantial share of memory, in-memory sorting, and spilling. These tests should check that the reported memory usage includes the tuple array and that the peak is tracked correctly across batches, including when sorting spills to disk.

Keeping the peak across batches does not necessarily require a reset: the current consumer of workmemused takes the maximum. For example, if the batch peaks are 100, 20, and 40 KB, a final value of 100 KB is correct. The Sort Space statistics should be checked separately.

Also, please have a look at the existing Peak < Peak check: it seems that it compares a value with itself and can never detect an error?

It would also be useful to run benchmarks to assess the overhead of updating the tracker along the chain of parent groups.

Current version of peak memory usage has several disadvantages:
- it was implemented for AllocSet (Aset) context only, but now postgres
  has other context types (Generation/Slab), so unconditional
  type casts of generic MemoryContext to AllocSet may lead to a SIGSEGV.
  The parent context (Aset) may have Generation child for sort operator
  (`select * from tbl order by column`, context creates at
  tuplesort_begin_batch).
  These lines https://github.com/apache/cloudberry/blob/c2baef068a6f9f8b3c8110f9fdf5758306207693/src/backend/utils/mmgr/aset.c#L1833-L1837
  check that both contexts are Aset contexts, but that is not enough -
  see issue apache#2130
- The peak usage consumption for group is calculated at
  AllocSetGetPeakUsage/AllocSetGetPeakUsage_recurse - it recurses
  by the context tree and, as I see it, should sum up all peak usage
  within current and descendant group. There are two problems with
  summing up peak values - it is not accurate and real peak value may
  be less than sum of distinct peak values. Moreover
  AllocSetGetPeakUsage_recurse does not use returned value of recursive
  call - so child subgroups are not taken into account.

This patch
- reworks old behaviour by creating separate tracking groups
within generic MemoryContext
- main functionality was moved to MemoryContext level (like group
  reparenting and so on), thus there is no need to use context
  specific methods
- increment/decrement of current allocated memory size is still
  located at context implementation.
- The tracking groups implemented as a tree structure - group
  may belong to some parent group, so peak values updating for all parents.
- The calculation of memory usage is based on MemoryContext.mem_allocated
  (this field was introduced in postgres). It's more accurate because real
  allocated OS memory is tracked now

Closes apache#2130.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possible SIGSEGV durning aset memory tracking

3 participants