postgres.git / summary / log / commit / refs
commit 8a043da94728821e1dfc6758cd883bafb57bfac7
Author: Tomas Vondra <tomas.vondra@postgresql.org>
Date: Thu Nov 05 12:00:43 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 18.7, 19.0
Handle numeric overflow when handling range values
The numrange_subdiff() function was calculating the difference simply by
calling numeric_sub and numeric_float8, but those functions are not
overflow-safe. It's possible to insert valid numeric ranges for which
the difference overflows either numeric or float8.
The overflow may affect various operations on numrange columns. It may
cause failures during ANALYZE on the table, inserts into GiST indexes,
selectivity estimation, and possibly other operations.
Fixed by handling overflows in numrange_subdiff() - extreme differences
are treated as infinity.
Author: Kirill Reshke <reshkekirill@gmail.com>
Reviewed-by: shihao zhong <zhong950419@gmail.com>
Backpatch-through: 14
Discussion: https://postgr.es/m/CALdSSPgBvXz-VSF0p-5k%3D_uFfN38cWrERmm_AC4K67N6MWQhmg%40mail.gmail.com
src/backend/utils/adt/rangetypes.c | 37 ++++++++++++++++++++++-----
src/test/regress/expected/multirangetypes.out | 7 +++++
src/test/regress/sql/multirangetypes.sql | 9 +++++++
3 files changed, 46 insertions(+), 7 deletions(-)
diff --git a/src/backend/utils/adt/rangetypes.c b/src/backend/utils/adt/rangetypes.c
index 66cc0acf4a7..ca0422c8141 100644
--- a/src/backend/utils/adt/rangetypes.c
+++ b/src/backend/utils/adt/rangetypes.c
@@ -41,7 +41,9 @@
#include "optimizer/optimizer.h"
#include "utils/builtins.h"
#include "utils/date.h"
+#include "utils/float.h"
#include "utils/lsyscache.h"
+#include "utils/numeric.h"
#include "utils/rangetypes.h"
#include "utils/sortsupport.h"
#include "utils/timestamp.h"
@@ -1677,7 +1679,6 @@ daterange_canonical(PG_FUNCTION_ARGS)
*
* Note that subtype_diff does return the difference, not the absolute value
* of the difference, and it must take care to avoid overflow.
- * (numrange_subdiff is at some risk there ...)
*----------------------------------------------------------
*/
@@ -1702,15 +1703,37 @@ int8range_subdiff(PG_FUNCTION_ARGS)
Datum
numrange_subdiff(PG_FUNCTION_ARGS)
{
- Datum v1 = PG_GETARG_DATUM(0);
- Datum v2 = PG_GETARG_DATUM(1);
- Datum numresult;
+ Numeric v1 = PG_GETARG_NUMERIC(0);
+ Numeric v2 = PG_GETARG_NUMERIC(1);
+ Numeric numresult;
float8 floatresult;
+ bool have_error = false;
- numresult = DirectFunctionCall2(numeric_sub, v1, v2);
+ /*
+ * The difference can exceed numeric's range, or be unrepresentable in
+ * float8. None of the callers is prepared to handle an error, so
+ * saturate to infinity (or round to zero) instead.
+ */
+ numresult = numeric_sub_opt_error(v1, v2, &have_error);
+ if (have_error)
+ {
+ /* The values v1 and v2 may be in arbitrary order. */
+ if (DatumGetBool(DirectFunctionCall2(numeric_lt, NumericGetDatum(v1),
+ NumericGetDatum(v2))))
+ floatresult = -get_float8_infinity();
+ else
+ floatresult = get_float8_infinity();
+ }
+ else
+ {
+ floatresult
+ = DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow,
+ NumericGetDatum(numresult)));
+ pfree(numresult);
+ }
- floatresult = DatumGetFloat8(DirectFunctionCall1(numeric_float8,
- numresult));
+ PG_FREE_IF_COPY(v1, 0);
+ PG_FREE_IF_COPY(v2, 1);
PG_RETURN_FLOAT8(floatresult);
}
diff --git a/src/test/regress/expected/multirangetypes.out b/src/test/regress/expected/multirangetypes.out
index 76ab1430c4c..f576d1c229d 100644
--- a/src/test/regress/expected/multirangetypes.out
+++ b/src/test/regress/expected/multirangetypes.out
@@ -3410,3 +3410,10 @@ create function mr_table_fail(i anyelement) returns table(i anyelement, r anymul
as $$ select $1, '[1,10]' $$ language sql;
ERROR: cannot determine result data type
DETAIL: A result of type anymultirange requires at least one input of type anyrange or anymultirange.
+--
+-- Check that numeric overflow during analyze does not make it fail
+--
+create table multirange_analyze_test (r nummultirange);
+insert into multirange_analyze_test values ('{[1e1000, 2e1000], [-9e131071, 9e131071]}'::nummultirange);
+analyze multirange_analyze_test;
+drop table multirange_analyze_test;
diff --git a/src/test/regress/sql/multirangetypes.sql b/src/test/regress/sql/multirangetypes.sql
index c7953aad295..7e368978bfc 100644
--- a/src/test/regress/sql/multirangetypes.sql
+++ b/src/test/regress/sql/multirangetypes.sql
@@ -901,3 +901,12 @@ create function mr_inoutparam_fail(inout i anyelement, out r anymultirange)
--should fail
create function mr_table_fail(i anyelement) returns table(i anyelement, r anymultirange)
as $$ select $1, '[1,10]' $$ language sql;
+
+--
+-- Check that numeric overflow during analyze does not make it fail
+--
+
+create table multirange_analyze_test (r nummultirange);
+insert into multirange_analyze_test values ('{[1e1000, 2e1000], [-9e131071, 9e131071]}'::nummultirange);
+analyze multirange_analyze_test;
+drop table multirange_analyze_test;
[parent: 8e54a4145700]