pgsql: Handle numeric overflow in numeric_minmax_multi_ops

Started by Tomas Vondra20 days ago3 messagescommitters
Jump to latest
#1Tomas Vondra
tomas.vondra@postgresql.org

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: /messages/by-id/CALdSSPgBvXz-VSF0p-5k=_uFfN38cWrERmm_AC4K67N6MWQhmg@mail.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(-)

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tomas Vondra (#1)
Re: pgsql: Handle numeric overflow in numeric_minmax_multi_ops

Tomas Vondra <tomas.vondra@postgresql.org> writes:

Handle numeric overflow in numeric_minmax_multi_ops

Hi, this commit is making a few buildfarm members unhappy,
eg at [1]https://buildfarm.postgresql.org/cgi-bin/show_stage_log.pl?nm=chimaera&dt=2026-11-05%2012%3A14%3A14&stg=build:

In file included from brin_minmax_multi.c:72:0:
brin_minmax_multi.c: In function \342\200\230brin_minmax_multi_distance_numeric\342\200\231:
../../../../src/include/nodes/miscnodes.h:54:15: warning: the comparison will always evaluate as \342\200\230true\342\200\231 for the address of \342\200\230escontext\342\200\231 will never be NULL [-Waddress]
((escontext) != NULL && IsA(escontext, ErrorSaveContext) && \\
^
brin_minmax_multi.c:2070:6: note: in expansion of macro \342\200\230SOFT_ERROR_OCCURRED\342\200\231
if (SOFT_ERROR_OCCURRED(&escontext))
^~~~~~~~~~~~~~~~~~~

We have seen this before, and the way to silence it is

-	if (SOFT_ERROR_OCCURRED(&escontext))
+	if (escontext.error_occurred)

regards, tom lane

[1]: https://buildfarm.postgresql.org/cgi-bin/show_stage_log.pl?nm=chimaera&dt=2026-11-05%2012%3A14%3A14&stg=build

#3Tomas Vondra
tomas@vondra.me
In reply to: Tom Lane (#2)
Re: pgsql: Handle numeric overflow in numeric_minmax_multi_ops

On 11/6/26 04:07, Tom Lane wrote:

Tomas Vondra <tomas.vondra@postgresql.org> writes:

Handle numeric overflow in numeric_minmax_multi_ops

Hi, this commit is making a few buildfarm members unhappy,
eg at [1]:

In file included from brin_minmax_multi.c:72:0:
brin_minmax_multi.c: In function \342\200\230brin_minmax_multi_distance_numeric\342\200\231:
../../../../src/include/nodes/miscnodes.h:54:15: warning: the comparison will always evaluate as \342\200\230true\342\200\231 for the address of \342\200\230escontext\342\200\231 will never be NULL [-Waddress]
((escontext) != NULL && IsA(escontext, ErrorSaveContext) && \\
^
brin_minmax_multi.c:2070:6: note: in expansion of macro \342\200\230SOFT_ERROR_OCCURRED\342\200\231
if (SOFT_ERROR_OCCURRED(&escontext))
^~~~~~~~~~~~~~~~~~~

We have seen this before, and the way to silence it is

-	if (SOFT_ERROR_OCCURRED(&escontext))
+	if (escontext.error_occurred)

regards, tom lane

Uh, I completely missed this report, and only noticed it now when I saw
the commit fixing it. Apologies and thanks for taking care of it!

Thanks

--
Tomas Vondra