Patch
Avoid assertion failure with inconsistent range subtype_diff
- Branch
- master
- Risk
- 12
- Base commit
- 458c77d74d2bab2014e97f01df19dc805dc7b42a
To apply it to a checkout of master at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a23315-11d0-7820-9c36-c826e04b32c5 | jq -r .body | git am
From 3cde62a47bf874f30936e217fa511fdd5e480c4a Mon Sep 17 00:00:00 2001
From: Claude <noreply@anthropic.com>
Date: Tue, 1 Dec 2026 11:28:26 -0800
Subject: [PATCH] Avoid assertion failure with inconsistent range subtype_diff
When estimating the selectivity of "<@" and "@>" for range and
multirange columns, calc_length_hist_frac() computes the average of
the length histogram's cumulative fraction over an interval whose
endpoints are distances obtained from the range type's subtype_diff
function. It asserted that the endpoints arrive in ascending order,
which is true only if subtype_diff is consistent with the sort
ordering of the subtype. A user-defined subtype_diff function that
is not, for example one that is not monotonic, therefore caused an
assertion failure while planning such a query. In a non-assert
build the function carried on with the endpoints reversed, which
could extrapolate outside the histogram bin it was looking at.
The rest of this code takes some trouble to tolerate silly results
from subtype_diff, such as NaN or negative values, so do likewise
here: if the endpoints are out of order, swap them. The result is
then a valid average over the interval, though of course the
estimate cannot be expected to be accurate with such a function.
The range and multirange estimators each have a copy of this
function; fix both.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
---
.../utils/adt/multirangetypes_selfuncs.c | 16 ++++++-
src/backend/utils/adt/rangetypes_selfuncs.c | 16 ++++++-
src/test/regress/expected/rangetypes.out | 47 +++++++++++++++++++
src/test/regress/sql/rangetypes.sql | 29 ++++++++++++
4 files changed, 106 insertions(+), 2 deletions(-)
diff --git a/src/backend/utils/adt/multirangetypes_selfuncs.c b/src/backend/utils/adt/multirangetypes_selfuncs.c
index 533111445e7..be271446a61 100644
--- a/src/backend/utils/adt/multirangetypes_selfuncs.c
+++ b/src/backend/utils/adt/multirangetypes_selfuncs.c
@@ -975,7 +975,21 @@ calc_length_hist_frac(const Datum *length_hist_values, int length_hist_nvalues,
int i;
double area;
- Assert(length2 >= length1);
+ /*
+ * The callers obtain the interval's endpoints from the range type's
+ * subdiff function, and expect length2 >= length1. That holds if subdiff
+ * is consistent with the subtype's sort ordering, but we don't want to
+ * crash or produce nonsense if it isn't. So if the endpoints are out of
+ * order, just swap them: the average of P(x) over the interval is
+ * well-defined either way.
+ */
+ if (length2 < length1)
+ {
+ double tmp = length1;
+
+ length1 = length2;
+ length2 = tmp;
+ }
if (length2 < 0.0)
return 0.0; /* shouldn't happen, but doesn't hurt to check */
diff --git a/src/backend/utils/adt/rangetypes_selfuncs.c b/src/backend/utils/adt/rangetypes_selfuncs.c
index 75f1e7567d5..88202dcd005 100644
--- a/src/backend/utils/adt/rangetypes_selfuncs.c
+++ b/src/backend/utils/adt/rangetypes_selfuncs.c
@@ -864,7 +864,21 @@ calc_length_hist_frac(const Datum *length_hist_values, int length_hist_nvalues,
int i;
double area;
- Assert(length2 >= length1);
+ /*
+ * The callers obtain the interval's endpoints from the range type's
+ * subdiff function, and expect length2 >= length1. That holds if subdiff
+ * is consistent with the subtype's sort ordering, but we don't want to
+ * crash or produce nonsense if it isn't. So if the endpoints are out of
+ * order, just swap them: the average of P(x) over the interval is
+ * well-defined either way.
+ */
+ if (length2 < length1)
+ {
+ double tmp = length1;
+
+ length1 = length2;
+ length2 = tmp;
+ }
if (length2 < 0.0)
return 0.0; /* shouldn't happen, but doesn't hurt to check */
diff --git a/src/test/regress/expected/rangetypes.out b/src/test/regress/expected/rangetypes.out
index fb1bb30b7ea..ef6c113717a 100644
--- a/src/test/regress/expected/rangetypes.out
+++ b/src/test/regress/expected/rangetypes.out
@@ -1639,6 +1639,53 @@ select * from float8range_test;
drop table float8range_test;
--
+-- Test selectivity estimation with a subtype_diff function that is not
+-- consistent with the subtype's sort ordering. The estimates are not
+-- expected to be any good, but the planner should not fall over.
+-- (The multirange estimator has the same logic, so test it here too.)
+--
+create function inconsistent_int4_diff(int4, int4) returns float8
+ language sql immutable strict
+ return (($1::int8 - $2::int8) % 7)::float8;
+create type inconsistent_int4range as range
+ (subtype = int4, subtype_diff = inconsistent_int4_diff);
+create table inconsistent_range_test as
+ select r, inconsistent_int4multirange(r) as mr
+ from (select inconsistent_int4range(i, i + (i * 37) % 40) as r
+ from generate_series(1, 1000) i) ss;
+analyze inconsistent_range_test;
+select count(*) from inconsistent_range_test
+ where r <@ inconsistent_int4range(100, 200);
+ count
+-------
+ 104
+(1 row)
+
+select count(*) from inconsistent_range_test
+ where r @> inconsistent_int4range(100, 102);
+ count
+-------
+ 19
+(1 row)
+
+select count(*) from inconsistent_range_test
+ where mr <@ inconsistent_int4range(100, 200);
+ count
+-------
+ 104
+(1 row)
+
+select count(*) from inconsistent_range_test
+ where mr @> inconsistent_int4range(100, 102);
+ count
+-------
+ 19
+(1 row)
+
+drop table inconsistent_range_test;
+drop type inconsistent_int4range;
+drop function inconsistent_int4_diff(int4, int4);
+--
-- Test range types over domains
--
create domain mydomain as int4;
diff --git a/src/test/regress/sql/rangetypes.sql b/src/test/regress/sql/rangetypes.sql
index e4062c3e6a0..9924f3450fa 100644
--- a/src/test/regress/sql/rangetypes.sql
+++ b/src/test/regress/sql/rangetypes.sql
@@ -472,6 +472,35 @@ insert into float8range_test values(float8range(-100.00007, '1.111113e9'), 42);
select * from float8range_test;
drop table float8range_test;
+--
+-- Test selectivity estimation with a subtype_diff function that is not
+-- consistent with the subtype's sort ordering. The estimates are not
+-- expected to be any good, but the planner should not fall over.
+-- (The multirange estimator has the same logic, so test it here too.)
+--
+
+create function inconsistent_int4_diff(int4, int4) returns float8
+ language sql immutable strict
+ return (($1::int8 - $2::int8) % 7)::float8;
+create type inconsistent_int4range as range
+ (subtype = int4, subtype_diff = inconsistent_int4_diff);
+create table inconsistent_range_test as
+ select r, inconsistent_int4multirange(r) as mr
+ from (select inconsistent_int4range(i, i + (i * 37) % 40) as r
+ from generate_series(1, 1000) i) ss;
+analyze inconsistent_range_test;
+select count(*) from inconsistent_range_test
+ where r <@ inconsistent_int4range(100, 200);
+select count(*) from inconsistent_range_test
+ where r @> inconsistent_int4range(100, 102);
+select count(*) from inconsistent_range_test
+ where mr <@ inconsistent_int4range(100, 200);
+select count(*) from inconsistent_range_test
+ where mr @> inconsistent_int4range(100, 102);
+drop table inconsistent_range_test;
+drop type inconsistent_int4range;
+drop function inconsistent_int4_diff(int4, int4);
+
--
-- Test range types over domains
--
base-commit: 458c77d74d2bab2014e97f01df19dc805dc7b42a
--
2.53.0