pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tomas Vondra <tomas.vondra@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Use Size instead of int64 to track allocated memory
Date: Fri, 04 Oct 2019 14:27:09 +0000
Message-ID: <E1iGOY1-0006d2-EP@gemulon.postgresql.org> (raw)

Use Size instead of int64 to track allocated memory

Commit 5dd7fc1519 added block-level memory accounting, but used int64 variable to
track the amount of allocated memory. That is incorrect, because we have Size for
exactly these purposes, but it was mostly harmless until c477f3e449 which changed
how we handle with repalloc() when downsizing the chunk. Previously we've ignored
these cases and just kept using the original chunk, but now we need to update the
accounting, and the code was doing this:

    context->mem_allocated += blksize - oldblksize;

Both blksize and oldblksize are Size (so unsigned) which means the subtraction
underflows, producing a very high positive value. On 64-bit platforms (where Size
has the same size as mem_alllocated) this happens to work because the result wraps
to the right value, but on (some) 32-bit platforms this fails.

This fixes two things - it changes mem_allocated (and related variables) to Size,
and it splits the update to two separate steps, to prevent any underflows.

Discussion: https://www.postgresql.org/message-id/15151.1570163761%40sss.pgh.pa.us

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/f2369bc610a19563cc00054ccfe9089fac469641

Modified Files
--------------
src/backend/utils/mmgr/aset.c       | 6 ++++--
src/backend/utils/mmgr/generation.c | 2 +-
src/include/nodes/memnodes.h        | 2 +-
3 files changed, 6 insertions(+), 4 deletions(-)



Message-ID: <E1iGOY1-0006d2-EP@gemulon.postgresql.org>
Permalink:  ../E1iGOY1-0006d2-EP@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1iGOY1-0006d2-EP@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: tomas.vondra@postgresql.org
  Subject: Re: pgsql: Use Size instead of int64 to track allocated memory
  In-Reply-To: <E1iGOY1-0006d2-EP@gemulon.postgresql.org>
  References: <E1iGOY1-0006d2-EP@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox