From 07d851ce6f6a736849b679da85cd8d355f15328f Mon Sep 17 00:00:00 2001 From: Ewan Young Date: Tue, 24 Nov 2026 23:27:39 +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 numeric 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. Use numeric_sub_safe() and, when a width is too large to represent, treat it as unmeasurable, taking the same branch already used for a NaN width (a NaN is a valid stored value that reaches this code without error). The union contains the original key, so the original width can only overflow when the union width does too; the remaining subtraction is then on finite values and cannot overflow. This is the numeric counterpart of the overflow fixes in the other btree_gist penalty and distance functions. --- contrib/btree_gist/btree_numeric.c | 46 +++++++++++++++++-------- contrib/btree_gist/expected/numeric.out | 16 +++++++++ contrib/btree_gist/sql/numeric.sql | 12 +++++++ 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/contrib/btree_gist/btree_numeric.c b/contrib/btree_gist/btree_numeric.c index 7a75e2ed00d..83c80d9f5b7 100644 --- a/contrib/btree_gist/btree_numeric.c +++ b/contrib/btree_gist/btree_numeric.c @@ -10,6 +10,7 @@ #include "btree_gist.h" #include "btree_utils_var.h" #include "utils/builtins.h" +#include "nodes/miscnodes.h" #include "utils/numeric.h" #include "utils/rel.h" #include "utils/sortsupport.h" @@ -166,6 +167,9 @@ gbt_numeric_penalty(PG_FUNCTION_ARGS) GBT_VARKEY_R rk, ok, uk; + ErrorSaveContext escontext = {T_ErrorSaveContext}; + bool us_unmeasurable, + os_unmeasurable; rk = gbt_var_key_readable(org); uni = PointerGetDatum(gbt_var_key_copy(&rk)); @@ -173,21 +177,31 @@ 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))); - - ds = DatumGetNumeric(DirectFunctionCall2(numeric_sub, - NumericGetDatum(us), - NumericGetDatum(os))); - - if (numeric_is_nan(us)) + /* + * 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; rather than failing the insert, we + * treat such a width as unmeasurable and fall into the same branch used + * for a NaN width below. (A NaN bound, which is a valid stored value, + * already reaches that branch: numeric_sub_safe returns NaN for it + * without error.) The union contains the original key, so if the + * original width overflows the union width does too; hence os is finite + * whenever us is, and the ds subtraction below cannot overflow. + */ + us = numeric_sub_safe(DatumGetNumeric(PointerGetDatum(uk.upper)), + DatumGetNumeric(PointerGetDatum(uk.lower)), + (Node *) &escontext); + us_unmeasurable = escontext.error_occurred; + escontext.error_occurred = false; + + os = numeric_sub_safe(DatumGetNumeric(PointerGetDatum(ok.upper)), + DatumGetNumeric(PointerGetDatum(ok.lower)), + (Node *) &escontext); + os_unmeasurable = escontext.error_occurred; + + if (us_unmeasurable || numeric_is_nan(us)) { - if (numeric_is_nan(os)) + if (os_unmeasurable || numeric_is_nan(os)) *result = 0.0; else *result = 1.0; @@ -196,6 +210,10 @@ gbt_numeric_penalty(PG_FUNCTION_ARGS) { 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..8d1ba5de697 100644 --- a/contrib/btree_gist/expected/numeric.out +++ b/contrib/btree_gist/expected/numeric.out @@ -205,3 +205,19 @@ SELECT * FROM numerictmp WHERE a BETWEEN 1 AND 300 ORDER BY a; 207.400532 (2 rows) +-- Extreme but valid values whose bound difference overflows the numeric +-- format 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); +SET enable_seqscan = off; +SELECT count(*) FROM numericwide WHERE a > 0; + count +------- + 1001 +(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..5712ce4c867 100644 --- a/contrib/btree_gist/sql/numeric.sql +++ b/contrib/btree_gist/sql/numeric.sql @@ -81,3 +81,15 @@ 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; + +-- Extreme but valid values whose bound difference overflows the numeric +-- format 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); +SET enable_seqscan = off; +SELECT count(*) FROM numericwide WHERE a > 0; +RESET enable_seqscan; +DROP TABLE numericwide; -- 2.47.3