pgsql: Get rid of our dependency on type "long" for memory size calcula

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Get rid of our dependency on type "long" for memory size calcula
Date: 2025-01-31 18:52:52
Message-ID: E1tdw8K-004fdx-Jp@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Get rid of our dependency on type "long" for memory size calculations.

Consistently use "Size" (or size_t, or in some places int64 or double)
as the type for variables holding memory allocation sizes. In most
places variables' data types were fine already, but we had an ancient
habit of computing bytes from kilobytes-units GUCs with code like
"work_mem * 1024L". That risks overflow on Win64 where they did not
make "long" as wide as "size_t". We worked around that by restricting
such GUCs' ranges, so you couldn't set work_mem et al higher than 2GB
on Win64. This patch removes that restriction, after replacing such
calculations with "work_mem * (Size) 1024" or variants of that.

It should be noted that this patch was constructed by searching
outwards from the GUCs that have MAX_KILOBYTES as upper limit.
So I can't positively guarantee there are no other places doing
memory-size arithmetic in int or long variables. I do however feel
pretty confident that increasing MAX_KILOBYTES on Win64 is safe now.
Also, nothing in our code should be dealing in multiple-gigabyte
allocations without authorization from a relevant GUC, so it seems
pretty likely that this search caught everything that could be at
risk of overflow.

Author: Vladlen Popolitov <v(dot)popolitov(at)postgrespro(dot)ru>
Co-authored-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Discussion: https://postgr.es/m/1a01f0-66ec2d80-3b-68487680@27595217

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/041e8b95b8cd251bfec6a3c9c3dd6614de6a4c9b

Modified Files
--------------
src/backend/access/gin/ginfast.c | 8 ++++----
src/backend/access/gin/ginget.c | 2 +-
src/backend/access/gin/gininsert.c | 2 +-
src/backend/access/hash/hash.c | 6 +++---
src/backend/access/heap/vacuumlazy.c | 4 ++--
src/backend/access/nbtree/nbtpage.c | 9 +++++----
src/backend/commands/vacuumparallel.c | 2 +-
src/backend/executor/execUtils.c | 2 +-
src/backend/executor/nodeBitmapIndexscan.c | 2 +-
src/backend/executor/nodeBitmapOr.c | 2 +-
src/backend/nodes/tidbitmap.c | 12 ++++++------
src/backend/optimizer/path/costsize.c | 14 +++++++-------
src/backend/optimizer/plan/planner.c | 2 +-
src/backend/replication/logical/reorderbuffer.c | 7 +++----
src/backend/utils/sort/tuplestore.c | 2 +-
src/include/executor/hashjoin.h | 2 +-
src/include/nodes/tidbitmap.h | 4 ++--
src/include/utils/dsa.h | 2 +-
src/include/utils/guc.h | 10 +++++++---
src/test/modules/test_bloomfilter/test_bloomfilter.c | 2 +-
20 files changed, 50 insertions(+), 46 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2025-01-31 19:37:03 pgsql: Remove obsolete restriction on the range of log_rotation_size.
Previous Message Daniel Gustafsson 2025-01-31 15:07:24 pgsql: require_auth: prepare for multiple SASL mechanisms