From 47501c0eb29a5f6dd99f4fe64d40e35ca1862592 Mon Sep 17 00:00:00 2001 From: Ewan Young Date: Fri, 27 Nov 2026 21:31:51 +0800 Subject: [PATCH] btree_gist: avoid numeric overflow in gbt_numeric_penalty gbt_numeric_penalty() measured the key widths by subtracting numeric bounds with numeric_sub(). With extreme but valid values in the index (for example repeat('9', 131072) and its negation), the difference of the union's bounds overflows the numeric format, so gistchoose() threw ERROR: value overflows numeric format and the INSERT failed, once the index had internal pages. Compute the widths with numeric_sub_safe() and treat an overflowed width as infinite, as the other btree_gist penalty and distance functions do. A width can also be NaN or infinite because a bound is, so handle all three cases up front: an unmeasurable union is a cheap home for the new entry when the original key is just as wide, and the maximum penalty otherwise. The union contains the original key, so the remaining ds = us - os subtraction only runs on finite widths and cannot overflow or produce Inf - Inf. --- contrib/btree_gist/btree_numeric.c | 56 ++++++++++++++++++------- contrib/btree_gist/expected/numeric.out | 17 ++++++++ contrib/btree_gist/sql/numeric.sql | 13 ++++++ 3 files changed, 72 insertions(+), 14 deletions(-) diff --git a/contrib/btree_gist/btree_numeric.c b/contrib/btree_gist/btree_numeric.c index 7a75e2ed00d..2eac27bb830 100644 --- a/contrib/btree_gist/btree_numeric.c +++ b/contrib/btree_gist/btree_numeric.c @@ -10,6 +10,8 @@ #include "btree_gist.h" #include "btree_utils_var.h" #include "utils/builtins.h" +#include "utils/float.h" +#include "nodes/miscnodes.h" #include "utils/numeric.h" #include "utils/rel.h" #include "utils/sortsupport.h" @@ -166,6 +168,7 @@ gbt_numeric_penalty(PG_FUNCTION_ARGS) GBT_VARKEY_R rk, ok, uk; + ErrorSaveContext escontext = {T_ErrorSaveContext}; rk = gbt_var_key_readable(org); uni = PointerGetDatum(gbt_var_key_copy(&rk)); @@ -173,29 +176,54 @@ gbt_numeric_penalty(PG_FUNCTION_ARGS) ok = gbt_var_key_readable(org); uk = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(uni)); - us = DatumGetNumeric(DirectFunctionCall2(numeric_sub, - PointerGetDatum(uk.upper), - PointerGetDatum(uk.lower))); - - os = DatumGetNumeric(DirectFunctionCall2(numeric_sub, - PointerGetDatum(ok.upper), - PointerGetDatum(ok.lower))); + /* + * Compute the key widths with the soft-error subtraction. Extreme but + * valid bounds (e.g. repeat('9', 131072) and its negation) can make a + * width overflow the numeric format; treat such a width as infinite, as + * the other btree_gist penalty and distance functions do for an + * overflowed difference. A width can also be NaN or infinite because a + * bound is (both are valid stored values), so handle all three cases + * before the ratio below. + */ + us = numeric_sub_safe(DatumGetNumeric(PointerGetDatum(uk.upper)), + DatumGetNumeric(PointerGetDatum(uk.lower)), + (Node *) &escontext); + if (escontext.error_occurred) + { + us = DatumGetNumeric(DirectFunctionCall1(float8_numeric, + Float8GetDatum(get_float8_infinity()))); + escontext.error_occurred = false; + } - ds = DatumGetNumeric(DirectFunctionCall2(numeric_sub, - NumericGetDatum(us), - NumericGetDatum(os))); + os = numeric_sub_safe(DatumGetNumeric(PointerGetDatum(ok.upper)), + DatumGetNumeric(PointerGetDatum(ok.lower)), + (Node *) &escontext); + if (escontext.error_occurred) + os = DatumGetNumeric(DirectFunctionCall1(float8_numeric, + Float8GetDatum(get_float8_infinity()))); if (numeric_is_nan(us)) { - if (numeric_is_nan(os)) - *result = 0.0; - else - *result = 1.0; + *result = numeric_is_nan(os) ? 0.0 : 1.0; + } + else if (numeric_is_inf(us)) + { + /* + * The union is too wide to measure. If the original key is just as + * wide it is a cheap home for the new entry; otherwise charge the + * maximum. The union contains the original key, so os is finite + * whenever us is, and the subtraction below never sees an infinity. + */ + *result = numeric_is_inf(os) ? 0.0 : 1.0; } else { Numeric nul = int64_to_numeric(0); + ds = DatumGetNumeric(DirectFunctionCall2(numeric_sub, + NumericGetDatum(us), + NumericGetDatum(os))); + *result = 0.0; if (DatumGetBool(DirectFunctionCall2(numeric_gt, NumericGetDatum(ds), NumericGetDatum(nul)))) diff --git a/contrib/btree_gist/expected/numeric.out b/contrib/btree_gist/expected/numeric.out index 34c1e568063..f314684418f 100644 --- a/contrib/btree_gist/expected/numeric.out +++ b/contrib/btree_gist/expected/numeric.out @@ -205,3 +205,20 @@ SELECT * FROM numerictmp WHERE a BETWEEN 1 AND 300 ORDER BY a; 207.400532 (2 rows) +-- A width that overflows the numeric format, or is infinite because a bound +-- is, must not fail an insert once the index has internal pages. +CREATE TABLE numericwide (a numeric); +INSERT INTO numericwide SELECT g FROM generate_series(1, 1000) g; +CREATE INDEX numericwideidx ON numericwide USING gist (a); +INSERT INTO numericwide VALUES (('9' || repeat('9', 131071))::numeric), + (('-9' || repeat('9', 131071))::numeric), + ('infinity'), ('-infinity'); +SET enable_seqscan = off; +SELECT count(*) FROM numericwide WHERE a > 0; + count +------- + 1002 +(1 row) + +RESET enable_seqscan; +DROP TABLE numericwide; diff --git a/contrib/btree_gist/sql/numeric.sql b/contrib/btree_gist/sql/numeric.sql index dbb2f2f1838..5c9fe84ff55 100644 --- a/contrib/btree_gist/sql/numeric.sql +++ b/contrib/btree_gist/sql/numeric.sql @@ -81,3 +81,16 @@ SET enable_bitmapscan=off; EXPLAIN (COSTS OFF) SELECT * FROM numerictmp WHERE a BETWEEN 1 AND 300 ORDER BY a; SELECT * FROM numerictmp WHERE a BETWEEN 1 AND 300 ORDER BY a; + +-- A width that overflows the numeric format, or is infinite because a bound +-- is, must not fail an insert once the index has internal pages. +CREATE TABLE numericwide (a numeric); +INSERT INTO numericwide SELECT g FROM generate_series(1, 1000) g; +CREATE INDEX numericwideidx ON numericwide USING gist (a); +INSERT INTO numericwide VALUES (('9' || repeat('9', 131071))::numeric), + (('-9' || repeat('9', 131071))::numeric), + ('infinity'), ('-infinity'); +SET enable_seqscan = off; +SELECT count(*) FROM numericwide WHERE a > 0; +RESET enable_seqscan; +DROP TABLE numericwide; -- 2.47.3