Skip to content

Commit

Permalink
[MERGE 15] YSQL: Fix name conflict in aset realloc
Browse files Browse the repository at this point in the history
Summary:
    In the master branch, for memory tracking, we added `oldsize` for the block.
    It overwrites the variable with the same name in the outer scope, defined for the chunk.
    However, in PG upstream, the `oldsize` in the outer scope is referenced in the inner scope.
    Because of the naming overwritting, after merging, the reference used becomes incorrect.

    **Solution**
    I basically "backport" how the upstream creates the variable for "block old size" and use
    it is for YB memory tracking.

    Along with this, it also fixes two potential overflow issues in the existing code.
    The fixes are needed in the master as well. So a backport to the PG13 branch will be committed later
    in a separate diff.

Original commit: 83c6b6a/D21128

Test Plan: Jenkins: rebase: pg15

Reviewers: neil, mbautin, telgersma

Reviewed By: telgersma

Subscribers: smishra, yql

Differential Revision: https://phabricator.dev.yugabyte.com/D24000
  • Loading branch information
SteveXiSong authored and nocaway committed Jul 20, 2023
1 parent 68f9934 commit 56401ef
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/postgres/src/backend/utils/mmgr/aset.c
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,6 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
AllocBlock block = (AllocBlock) (((char *) chunk) - ALLOC_BLOCKHDRSZ);
Size chksize;
Size blksize;
Size oldsize = block->endptr - ((char *) block);
Size oldblksize;

/*
Expand Down Expand Up @@ -1176,7 +1175,8 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
context->mem_allocated += blksize;

block->freeptr = block->endptr = ((char *) block) + blksize;
YbPgMemAddConsumption(blksize - oldsize);
YbPgMemSubConsumption(oldblksize);
YbPgMemAddConsumption(blksize);

/* Update pointers since block has likely been moved */
chunk = (AllocChunk) (((char *) block) + ALLOC_BLOCKHDRSZ);
Expand Down
4 changes: 3 additions & 1 deletion src/postgres/src/backend/utils/mmgr/mcxt.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ YbPgMemSubConsumption(Size sz)
return;

// Avoid overflow when subtracting sz.
PgMemTracker.pg_cur_mem_bytes = Max(PgMemTracker.pg_cur_mem_bytes - sz, 0);
PgMemTracker.pg_cur_mem_bytes = PgMemTracker.pg_cur_mem_bytes >= sz ?
PgMemTracker.pg_cur_mem_bytes - sz :
0;
// Only call release if pggate is alive, and update its liveness from the
// return value.
if (PgMemTracker.pggate_alive)
Expand Down

0 comments on commit 56401ef

Please sign in to comment.