pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Handle numeric overflow in numeric_minmax_multi_ops
9+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2026-11-12 52ecf2af06 165af118c5  Silence compiler warnings about SOFT_ERROR_OCCURRED usage.
* pgsql: Handle numeric overflow in numeric_minmax_multi_ops
@ 2026-11-02 20:44  Tomas Vondra <tomas.vondra@postgresql.org>
  0 siblings, 1 reply; 9+ messages in thread

From: Tomas Vondra @ 2026-11-02 20:44 UTC (permalink / raw)
  To: pgsql-committers@lists.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: 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(-)



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* pgsql: Handle numeric overflow in numeric_minmax_multi_ops
@ 2026-11-02 20:44  Tomas Vondra <tomas.vondra@postgresql.org>
  0 siblings, 0 replies; 9+ messages in thread

From: Tomas Vondra @ 2026-11-02 20:44 UTC (permalink / raw)
  To: pgsql-committers@lists.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: https://postgr.es/m/CALdSSPgBvXz-VSF0p-5k%3D_uFfN38cWrERmm_AC4K67N6MWQhmg%40mail.gmail.com

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/78721a206dc6123e47de17c522b650201de4ed0c

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(-)



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* pgsql: Handle numeric overflow in numeric_minmax_multi_ops
@ 2026-11-02 20:44  Tomas Vondra <tomas.vondra@postgresql.org>
  0 siblings, 0 replies; 9+ messages in thread

From: Tomas Vondra @ 2026-11-02 20:44 UTC (permalink / raw)
  To: pgsql-committers@lists.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: https://postgr.es/m/CALdSSPgBvXz-VSF0p-5k%3D_uFfN38cWrERmm_AC4K67N6MWQhmg%40mail.gmail.com

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/5c8629bb58957ef552e22680cf20b5214f40328e

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



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* pgsql: Handle numeric overflow in numeric_minmax_multi_ops
@ 2026-11-02 20:44  Tomas Vondra <tomas.vondra@postgresql.org>
  0 siblings, 0 replies; 9+ messages in thread

From: Tomas Vondra @ 2026-11-02 20:44 UTC (permalink / raw)
  To: pgsql-committers@lists.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: https://postgr.es/m/CALdSSPgBvXz-VSF0p-5k%3D_uFfN38cWrERmm_AC4K67N6MWQhmg%40mail.gmail.com

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/9ab2a624ebe92431d19a92367e8a99dccfc5e10a

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



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* pgsql: Handle numeric overflow in numeric_minmax_multi_ops
@ 2026-11-02 20:44  Tomas Vondra <tomas.vondra@postgresql.org>
  0 siblings, 0 replies; 9+ messages in thread

From: Tomas Vondra @ 2026-11-02 20:44 UTC (permalink / raw)
  To: pgsql-committers@lists.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: https://postgr.es/m/CALdSSPgBvXz-VSF0p-5k%3D_uFfN38cWrERmm_AC4K67N6MWQhmg%40mail.gmail.com

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/444b85032a2a699816c772191f322b3fd16fe70d

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



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* pgsql: Handle numeric overflow in numeric_minmax_multi_ops
@ 2026-11-02 20:44  Tomas Vondra <tomas.vondra@postgresql.org>
  0 siblings, 0 replies; 9+ messages in thread

From: Tomas Vondra @ 2026-11-02 20:44 UTC (permalink / raw)
  To: pgsql-committers@lists.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: https://postgr.es/m/CALdSSPgBvXz-VSF0p-5k%3D_uFfN38cWrERmm_AC4K67N6MWQhmg%40mail.gmail.com

Branch
------
REL_15_STABLE

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

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



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* pgsql: Handle numeric overflow in numeric_minmax_multi_ops
@ 2026-11-02 20:45  Tomas Vondra <tomas.vondra@postgresql.org>
  0 siblings, 0 replies; 9+ messages in thread

From: Tomas Vondra @ 2026-11-02 20:45 UTC (permalink / raw)
  To: pgsql-committers@lists.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: https://postgr.es/m/CALdSSPgBvXz-VSF0p-5k%3D_uFfN38cWrERmm_AC4K67N6MWQhmg%40mail.gmail.com

Branch
------
REL_14_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/174b605f0e3a3e6a01a137ea401733b699937cad

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



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: pgsql: Handle numeric overflow in numeric_minmax_multi_ops
@ 2026-11-06 03:07  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Tomas Vondra <tomas.vondra@postgresql.org>
  0 siblings, 1 reply; 9+ messages in thread

From: Tom Lane @ 2026-11-06 03:07 UTC (permalink / raw)
  To: Tomas Vondra <tomas.vondra@postgresql.org>; +Cc: pgsql-committers@lists.postgresql.org

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

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






^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: pgsql: Handle numeric overflow in numeric_minmax_multi_ops
@ 2026-11-12 16:33  Tomas Vondra <tomas@vondra.me>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 9+ messages in thread

From: Tomas Vondra @ 2026-11-12 16:33 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; Tomas Vondra <tomas.vondra@postgresql.org>; +Cc: pgsql-committers@lists.postgresql.org


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







^ permalink  raw  reply  [nested|flat] 9+ messages in thread


end of thread, other threads:[~2026-11-12 16:33 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-02 20:44 pgsql: Handle numeric overflow in numeric_minmax_multi_ops Tomas Vondra <tomas.vondra@postgresql.org>
2026-11-06 03:07 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-11-12 16:33   ` Tomas Vondra <tomas@vondra.me>
2026-11-02 20:44 pgsql: Handle numeric overflow in numeric_minmax_multi_ops Tomas Vondra <tomas.vondra@postgresql.org>
2026-11-02 20:44 pgsql: Handle numeric overflow in numeric_minmax_multi_ops Tomas Vondra <tomas.vondra@postgresql.org>
2026-11-02 20:44 pgsql: Handle numeric overflow in numeric_minmax_multi_ops Tomas Vondra <tomas.vondra@postgresql.org>
2026-11-02 20:44 pgsql: Handle numeric overflow in numeric_minmax_multi_ops Tomas Vondra <tomas.vondra@postgresql.org>
2026-11-02 20:44 pgsql: Handle numeric overflow in numeric_minmax_multi_ops Tomas Vondra <tomas.vondra@postgresql.org>
2026-11-02 20:45 pgsql: Handle numeric overflow in numeric_minmax_multi_ops Tomas Vondra <tomas.vondra@postgresql.org>

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