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: Handle numeric overflow in numeric_minmax_multi_ops
Date: Mon, 02 Nov 2026 20:44:07 +0000
Message-ID: <E1xNysx-00000001q94-1L4O@gemulon.postgresql.org> (raw)

Handle numeric overflow in numeric_minmax_multi_ops

When calculating the distance between numeric values, the code could
overflow in two ways. A subtraction of two finite values may still
overflow, or the result may not fit into float8.

The distances don't need to be perfectly accurate, although it may make
the index less efficient. And for most minmax-multi opclasses it works
like that, but for numeric the functions may error-out. That prevents
summarization, either when creating an index or later during vacuum.

Fix by using numeric_sub_safe() and numeric_float8_no_overflow(), and
returning infinity when either of those fails. The older releases need
to use numeric_sub_opt_error(), but the principle is the same.

Also, handle NaN values the same as for float4/float8. When both values
are NaN, return distance 0. When one value is NaN, return infinity.

While at it, free the detoasted copies of arguments, and the calculated
numeric distance. For small values it does not matter, but for extreme
numeric values it may make a difference.

Reported by Kirill Reshke, with a patch fixing the issue. Various
improvements and backpatching by me.

Backpatch to 14, where minmax-multi indexes were introduced.

Author: Kirill Reshke <reshkekirill@gmail.com>
Author: Tomas Vondra <tomas@vondra.me>
Reviewed-by: shihao zhong <zhong950419@gmail.com>
Backpatch-through: 14
Discussion: https://postgr.es/m/CALdSSPgBvXz-VSF0p-5k%3D_uFfN38cWrERmm_AC4K67N6MWQhmg%40mail.gmail.com

Branch
------
master

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

Modified Files
--------------
src/backend/access/brin/brin_minmax_multi.c | 40 ++++++++++++++++++++++++-----
src/test/regress/expected/brin_multi.out    | 10 ++++++++
src/test/regress/sql/brin_multi.sql         | 14 ++++++++++
3 files changed, 58 insertions(+), 6 deletions(-)



view thread (9+ messages)  latest in thread

Message-ID: <E1xNysx-00000001q94-1L4O@gemulon.postgresql.org>
Permalink:  ../E1xNysx-00000001q94-1L4O@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xNysx-00000001q94-1L4O@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: Handle numeric overflow in numeric_minmax_multi_ops
  In-Reply-To: <E1xNysx-00000001q94-1L4O@gemulon.postgresql.org>
  References: <E1xNysx-00000001q94-1L4O@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