From 85a30d624e96f6679e6d699f9a167b87c6beae2c Mon Sep 17 00:00:00 2001 From: Alexander Nestorov Date: Sun, 8 Nov 2026 00:24:30 +0100 Subject: [PATCH v8 2/4] Implement cross-type operators for GiST indexes btree_gist's GiST opclasses were same-type only: the planner could use an index only when the query value's type exactly matched the indexed column. A common case like an int8 column compared against an int4 literal (WHERE bigint_col = 42) therefore could not use the index unless the query was written with an explicit cast, which is easy to forget and which ORMs and parameter binding routinely get wrong. Add cross-type query operator support among the integer trio (int2, int4, int8) to the gist_int2_ops, gist_int4_ops and gist_int8_ops families. Each family gains the six B-tree comparison strategies (<, <=, =, >=, >, <>) and the <-> distance operator against the other two integer types. The additions are deliberately pg_amop-only: GiST's amvalidate requires support functions in a family to have matching left/right input types, so no cross-type consistent/distance functions are registered. Instead the existing support functions dispatch on the operator's subtype OID. Same-type queries take the normal path; cross-type queries select a comparison/distance callback that reads the query and key sides at their own widths, so out-of-range query constants compare by normal integer semantics without being narrowed to the column type. The cross-type path reuses gbt_num_consistent() and gbt_num_distance() through per-pair gbtree_ninfo structs. Their callbacks rely on being passed the index key first and the query value second, as both of those functions do. Note that the subtype is InvalidOid for exclusion-constraint and temporal PK/FK checks, which is handled as the same-type case. This is exposed as btree_gist version 1.10. Co-authored-by: Maxime Schoemans --- contrib/btree_gist/Makefile | 2 +- contrib/btree_gist/btree_gist--1.9--1.10.sql | 134 +++++++++++++++++++ contrib/btree_gist/btree_gist.control | 2 +- contrib/btree_gist/btree_int2.c | 131 ++++++++++++++++-- contrib/btree_gist/btree_int4.c | 131 ++++++++++++++++-- contrib/btree_gist/btree_int8.c | 131 ++++++++++++++++-- contrib/btree_gist/btree_utils_num.c | 4 + contrib/btree_gist/btree_utils_num.h | 39 ++++++ contrib/btree_gist/meson.build | 1 + src/tools/pgindent/typedefs.list | 1 + 10 files changed, 544 insertions(+), 32 deletions(-) create mode 100644 contrib/btree_gist/btree_gist--1.9--1.10.sql diff --git a/contrib/btree_gist/Makefile b/contrib/btree_gist/Makefile index fbbbca95598..380c7642fde 100644 --- a/contrib/btree_gist/Makefile +++ b/contrib/btree_gist/Makefile @@ -32,13 +32,13 @@ OBJS = \ EXTENSION = btree_gist DATA = btree_gist--1.0--1.1.sql \ btree_gist--1.1--1.2.sql btree_gist--1.2--1.3.sql \ btree_gist--1.3--1.4.sql btree_gist--1.4--1.5.sql \ btree_gist--1.5--1.6.sql btree_gist--1.6--1.7.sql \ btree_gist--1.7--1.8.sql btree_gist--1.8--1.9.sql \ - btree_gist--1.9.sql + btree_gist--1.9.sql btree_gist--1.9--1.10.sql PGFILEDESC = "btree_gist - B-tree equivalent GiST operator classes" REGRESS = init int2 int4 int8 float4 float8 cash oid timestamp timestamptz \ time timetz date interval macaddr macaddr8 inet cidr text varchar char \ bytea bit varbit numeric uuid not_equal enum bool partitions \ stratnum without_overlaps diff --git a/contrib/btree_gist/btree_gist--1.9--1.10.sql b/contrib/btree_gist/btree_gist--1.9--1.10.sql new file mode 100644 index 00000000000..9cd57455086 --- /dev/null +++ b/contrib/btree_gist/btree_gist--1.9--1.10.sql @@ -0,0 +1,134 @@ +/* contrib/btree_gist/btree_gist--1.9--1.10.sql */ + +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "ALTER EXTENSION btree_gist UPDATE TO '1.10'" to load this file. \quit + +-- Add cross-type operator support for the integer trio (int2, int4, int8) +-- to the existing GiST operator families. +-- +-- GiST's amvalidate requires support functions in a family to have matching +-- left/right input types, so the catalog additions below are deliberately +-- pg_amop-only. The existing consistent/distance support functions dispatch +-- on the subtype OID: same-type queries take the normal path, while mixed-width +-- integer queries select a cross-type comparison callback that reads the query +-- and key sides at their own widths (see btree_int{2,4,8}.c). + +CREATE FUNCTION int2_int4_dist(int2, int4) +RETURNS int4 +AS 'MODULE_PATHNAME' +LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + +CREATE FUNCTION int4_int2_dist(int4, int2) +RETURNS int4 +AS 'MODULE_PATHNAME' +LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + +CREATE FUNCTION int2_int8_dist(int2, int8) +RETURNS int8 +AS 'MODULE_PATHNAME' +LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + +CREATE FUNCTION int8_int2_dist(int8, int2) +RETURNS int8 +AS 'MODULE_PATHNAME' +LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + +CREATE FUNCTION int4_int8_dist(int4, int8) +RETURNS int8 +AS 'MODULE_PATHNAME' +LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + +CREATE FUNCTION int8_int4_dist(int8, int4) +RETURNS int8 +AS 'MODULE_PATHNAME' +LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + +CREATE OPERATOR <-> ( + LEFTARG = int2, + RIGHTARG = int4, + PROCEDURE = int2_int4_dist, + COMMUTATOR = '<->' +); + +CREATE OPERATOR <-> ( + LEFTARG = int4, + RIGHTARG = int2, + PROCEDURE = int4_int2_dist, + COMMUTATOR = '<->' +); + +CREATE OPERATOR <-> ( + LEFTARG = int2, + RIGHTARG = int8, + PROCEDURE = int2_int8_dist, + COMMUTATOR = '<->' +); + +CREATE OPERATOR <-> ( + LEFTARG = int8, + RIGHTARG = int2, + PROCEDURE = int8_int2_dist, + COMMUTATOR = '<->' +); + +CREATE OPERATOR <-> ( + LEFTARG = int4, + RIGHTARG = int8, + PROCEDURE = int4_int8_dist, + COMMUTATOR = '<->' +); + +CREATE OPERATOR <-> ( + LEFTARG = int8, + RIGHTARG = int4, + PROCEDURE = int8_int4_dist, + COMMUTATOR = '<->' +); + +ALTER OPERATOR FAMILY gist_int2_ops USING gist ADD + OPERATOR 1 < (int2, int4), + OPERATOR 2 <= (int2, int4), + OPERATOR 3 = (int2, int4), + OPERATOR 4 >= (int2, int4), + OPERATOR 5 > (int2, int4), + OPERATOR 6 <> (int2, int4), + OPERATOR 15 <-> (int2, int4) FOR ORDER BY pg_catalog.integer_ops, + OPERATOR 1 < (int2, int8), + OPERATOR 2 <= (int2, int8), + OPERATOR 3 = (int2, int8), + OPERATOR 4 >= (int2, int8), + OPERATOR 5 > (int2, int8), + OPERATOR 6 <> (int2, int8), + OPERATOR 15 <-> (int2, int8) FOR ORDER BY pg_catalog.integer_ops; + +ALTER OPERATOR FAMILY gist_int4_ops USING gist ADD + OPERATOR 1 < (int4, int2), + OPERATOR 2 <= (int4, int2), + OPERATOR 3 = (int4, int2), + OPERATOR 4 >= (int4, int2), + OPERATOR 5 > (int4, int2), + OPERATOR 6 <> (int4, int2), + OPERATOR 15 <-> (int4, int2) FOR ORDER BY pg_catalog.integer_ops, + OPERATOR 1 < (int4, int8), + OPERATOR 2 <= (int4, int8), + OPERATOR 3 = (int4, int8), + OPERATOR 4 >= (int4, int8), + OPERATOR 5 > (int4, int8), + OPERATOR 6 <> (int4, int8), + OPERATOR 15 <-> (int4, int8) FOR ORDER BY pg_catalog.integer_ops; + +ALTER OPERATOR FAMILY gist_int8_ops USING gist ADD + OPERATOR 1 < (int8, int2), + OPERATOR 2 <= (int8, int2), + OPERATOR 3 = (int8, int2), + OPERATOR 4 >= (int8, int2), + OPERATOR 5 > (int8, int2), + OPERATOR 6 <> (int8, int2), + OPERATOR 15 <-> (int8, int2) FOR ORDER BY pg_catalog.integer_ops, + OPERATOR 1 < (int8, int4), + OPERATOR 2 <= (int8, int4), + OPERATOR 3 = (int8, int4), + OPERATOR 4 >= (int8, int4), + OPERATOR 5 > (int8, int4), + OPERATOR 6 <> (int8, int4), + OPERATOR 15 <-> (int8, int4) FOR ORDER BY pg_catalog.integer_ops; diff --git a/contrib/btree_gist/btree_gist.control b/contrib/btree_gist/btree_gist.control index 69d9341a0ad..e606fa6551d 100644 --- a/contrib/btree_gist/btree_gist.control +++ b/contrib/btree_gist/btree_gist.control @@ -1,6 +1,6 @@ # btree_gist extension comment = 'support for indexing common datatypes in GiST' -default_version = '1.9' +default_version = '1.10' module_pathname = '$libdir/btree_gist' relocatable = true trusted = true diff --git a/contrib/btree_gist/btree_int2.c b/contrib/btree_gist/btree_int2.c index 6d5ca44fe8f..603813fcd19 100644 --- a/contrib/btree_gist/btree_int2.c +++ b/contrib/btree_gist/btree_int2.c @@ -2,12 +2,13 @@ * contrib/btree_gist/btree_int2.c */ #include "postgres.h" #include "btree_gist.h" #include "btree_utils_num.h" +#include "catalog/pg_type.h" #include "common/int.h" #include "utils/rel.h" #include "utils/sortsupport.h" typedef struct int16key { @@ -88,12 +89,82 @@ static const gbtree_ninfo tinfo = gbt_int2le, gbt_int2lt, gbt_int2key_cmp, gbt_int2_dist }; +/* + * Cross-type GiST callbacks: the indexed key is int2, the query value is int4 + * or int8. These tinfos let the cross-type cases reuse gbt_num_consistent() + * and gbt_num_distance(); their callbacks read the key (left argument) and the + * query value (right argument) each at its own width. f_cmp is not used on + * these paths and is left NULL. + */ +GBT_INT_CMP_FNS(gbt_int2_int4_, int16, int32) +GBT_INT_CMP_FNS(gbt_int2_int8_, int16, int64) + +static const gbtree_ninfo tinfo_int2_int4 = +{ + gbt_t_int2, + sizeof(int16), + 4, + gbt_int2_int4_gt, + gbt_int2_int4_ge, + gbt_int2_int4_eq, + gbt_int2_int4_le, + gbt_int2_int4_lt, + NULL, + gbt_int2_int4_dist +}; + +static const gbtree_ninfo tinfo_int2_int8 = +{ + gbt_t_int2, + sizeof(int16), + 4, + gbt_int2_int8_gt, + gbt_int2_int8_ge, + gbt_int2_int8_eq, + gbt_int2_int8_le, + gbt_int2_int8_lt, + NULL, + gbt_int2_int8_dist +}; + +/* + * Cross-type dispatch shared by gbt_int2_consistent and gbt_int2_distance: + * select the tinfo for the query subtype, read the query value at its own + * width into the caller's buffer, and point *query at it. + */ +static const gbtree_ninfo * +gbt_int2_crosstype(Oid subtype, Datum querydatum, gbt_intquery *buf, + const void **query) +{ + switch (subtype) + { + case InvalidOid: /* same-type: exclusion/temporal constraint + * checks pass the native type with subtype 0 */ + case INT2OID: + buf->i2 = DatumGetInt16(querydatum); + *query = &buf->i2; + return &tinfo; + case INT4OID: + buf->i4 = DatumGetInt32(querydatum); + *query = &buf->i4; + return &tinfo_int2_int4; + case INT8OID: + buf->i8 = DatumGetInt64(querydatum); + *query = &buf->i8; + return &tinfo_int2_int8; + default: + elog(ERROR, "unrecognized subtype %u for btree_gist int2 cross-type comparison", + subtype); + return NULL; /* keep compiler quiet */ + } +} + PG_FUNCTION_INFO_V1(int2_dist); Datum int2_dist(PG_FUNCTION_ARGS) { int16 a = PG_GETARG_INT16(0); @@ -109,12 +180,46 @@ int2_dist(PG_FUNCTION_ARGS) ra = abs(r); PG_RETURN_INT16(ra); } +PG_FUNCTION_INFO_V1(int2_int4_dist); +Datum +int2_int4_dist(PG_FUNCTION_ARGS) +{ + int32 a = (int32) PG_GETARG_INT16(0); + int32 b = PG_GETARG_INT32(1); + int32 r; + + if (pg_sub_s32_overflow(a, b, &r) || + r == PG_INT32_MIN) + ereport(ERROR, + (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), + errmsg("integer out of range"))); + + PG_RETURN_INT32(abs(r)); +} + +PG_FUNCTION_INFO_V1(int2_int8_dist); +Datum +int2_int8_dist(PG_FUNCTION_ARGS) +{ + int64 a = (int64) PG_GETARG_INT16(0); + int64 b = PG_GETARG_INT64(1); + int64 r; + + if (pg_sub_s64_overflow(a, b, &r) || + r == PG_INT64_MIN) + ereport(ERROR, + (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), + errmsg("bigint out of range"))); + + PG_RETURN_INT64(i64abs(r)); +} + /************************************************** * GiST support functions **************************************************/ Datum @@ -134,47 +239,53 @@ gbt_int2_fetch(PG_FUNCTION_ARGS) } Datum gbt_int2_consistent(PG_FUNCTION_ARGS) { GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); - int16 query = PG_GETARG_INT16(1); + Datum queryDatum = PG_GETARG_DATUM(1); StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); -#ifdef NOT_USED Oid subtype = PG_GETARG_OID(3); -#endif bool *recheck = (bool *) PG_GETARG_POINTER(4); int16KEY *kkk = (int16KEY *) DatumGetPointer(entry->key); + const gbtree_ninfo *ti; + gbt_intquery querybuf; + const void *query; GBT_NUMKEY_R key; /* All cases served by this function are exact */ *recheck = false; key.lower = (GBT_NUMKEY *) &kkk->lower; key.upper = (GBT_NUMKEY *) &kkk->upper; - PG_RETURN_BOOL(gbt_num_consistent(&key, &query, strategy, - GIST_LEAF(entry), &tinfo, fcinfo->flinfo)); + ti = gbt_int2_crosstype(subtype, queryDatum, &querybuf, &query); + + PG_RETURN_BOOL(gbt_num_consistent(&key, query, strategy, + GIST_LEAF(entry), ti, fcinfo->flinfo)); } Datum gbt_int2_distance(PG_FUNCTION_ARGS) { GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); - int16 query = PG_GETARG_INT16(1); -#ifdef NOT_USED + Datum queryDatum = PG_GETARG_DATUM(1); Oid subtype = PG_GETARG_OID(3); -#endif int16KEY *kkk = (int16KEY *) DatumGetPointer(entry->key); + const gbtree_ninfo *ti; + gbt_intquery querybuf; + const void *query; GBT_NUMKEY_R key; key.lower = (GBT_NUMKEY *) &kkk->lower; key.upper = (GBT_NUMKEY *) &kkk->upper; - PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry), - &tinfo, fcinfo->flinfo)); + ti = gbt_int2_crosstype(subtype, queryDatum, &querybuf, &query); + + PG_RETURN_FLOAT8(gbt_num_distance(&key, query, GIST_LEAF(entry), + ti, fcinfo->flinfo)); } Datum gbt_int2_union(PG_FUNCTION_ARGS) { GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0); diff --git a/contrib/btree_gist/btree_int4.c b/contrib/btree_gist/btree_int4.c index 8e0c010c633..87fc2200308 100644 --- a/contrib/btree_gist/btree_int4.c +++ b/contrib/btree_gist/btree_int4.c @@ -1,12 +1,13 @@ /* * contrib/btree_gist/btree_int4.c */ #include "postgres.h" #include "btree_gist.h" #include "btree_utils_num.h" +#include "catalog/pg_type.h" #include "common/int.h" #include "utils/rel.h" #include "utils/sortsupport.h" typedef struct int32key { @@ -86,12 +87,82 @@ static const gbtree_ninfo tinfo = gbt_int4le, gbt_int4lt, gbt_int4key_cmp, gbt_int4_dist }; +/* + * Cross-type GiST callbacks: the indexed key is int4, the query value is int2 + * or int8. These tinfos let the cross-type cases reuse gbt_num_consistent() + * and gbt_num_distance(); their callbacks read the key (left argument) and the + * query value (right argument) each at its own width. f_cmp is not used on + * these paths and is left NULL. + */ +GBT_INT_CMP_FNS(gbt_int4_int2_, int32, int16) +GBT_INT_CMP_FNS(gbt_int4_int8_, int32, int64) + +static const gbtree_ninfo tinfo_int4_int2 = +{ + gbt_t_int4, + sizeof(int32), + 8, + gbt_int4_int2_gt, + gbt_int4_int2_ge, + gbt_int4_int2_eq, + gbt_int4_int2_le, + gbt_int4_int2_lt, + NULL, + gbt_int4_int2_dist +}; + +static const gbtree_ninfo tinfo_int4_int8 = +{ + gbt_t_int4, + sizeof(int32), + 8, + gbt_int4_int8_gt, + gbt_int4_int8_ge, + gbt_int4_int8_eq, + gbt_int4_int8_le, + gbt_int4_int8_lt, + NULL, + gbt_int4_int8_dist +}; + +/* + * Cross-type dispatch shared by gbt_int4_consistent and gbt_int4_distance: + * select the tinfo for the query subtype, read the query value at its own + * width into the caller's buffer, and point *query at it. + */ +static const gbtree_ninfo * +gbt_int4_crosstype(Oid subtype, Datum querydatum, gbt_intquery *buf, + const void **query) +{ + switch (subtype) + { + case INT2OID: + buf->i2 = DatumGetInt16(querydatum); + *query = &buf->i2; + return &tinfo_int4_int2; + case InvalidOid: /* same-type: exclusion/temporal constraint + * checks pass the native type with subtype 0 */ + case INT4OID: + buf->i4 = DatumGetInt32(querydatum); + *query = &buf->i4; + return &tinfo; + case INT8OID: + buf->i8 = DatumGetInt64(querydatum); + *query = &buf->i8; + return &tinfo_int4_int8; + default: + elog(ERROR, "unrecognized subtype %u for btree_gist int4 cross-type comparison", + subtype); + return NULL; /* keep compiler quiet */ + } +} + PG_FUNCTION_INFO_V1(int4_dist); Datum int4_dist(PG_FUNCTION_ARGS) { int32 a = PG_GETARG_INT32(0); @@ -107,12 +178,46 @@ int4_dist(PG_FUNCTION_ARGS) ra = abs(r); PG_RETURN_INT32(ra); } +PG_FUNCTION_INFO_V1(int4_int2_dist); +Datum +int4_int2_dist(PG_FUNCTION_ARGS) +{ + int32 a = PG_GETARG_INT32(0); + int32 b = (int32) PG_GETARG_INT16(1); + int32 r; + + if (pg_sub_s32_overflow(a, b, &r) || + r == PG_INT32_MIN) + ereport(ERROR, + (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), + errmsg("integer out of range"))); + + PG_RETURN_INT32(abs(r)); +} + +PG_FUNCTION_INFO_V1(int4_int8_dist); +Datum +int4_int8_dist(PG_FUNCTION_ARGS) +{ + int64 a = (int64) PG_GETARG_INT32(0); + int64 b = PG_GETARG_INT64(1); + int64 r; + + if (pg_sub_s64_overflow(a, b, &r) || + r == PG_INT64_MIN) + ereport(ERROR, + (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), + errmsg("bigint out of range"))); + + PG_RETURN_INT64(i64abs(r)); +} + /************************************************** * GiST support functions **************************************************/ Datum @@ -132,47 +237,53 @@ gbt_int4_fetch(PG_FUNCTION_ARGS) } Datum gbt_int4_consistent(PG_FUNCTION_ARGS) { GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); - int32 query = PG_GETARG_INT32(1); + Datum queryDatum = PG_GETARG_DATUM(1); StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); -#ifdef NOT_USED Oid subtype = PG_GETARG_OID(3); -#endif bool *recheck = (bool *) PG_GETARG_POINTER(4); int32KEY *kkk = (int32KEY *) DatumGetPointer(entry->key); + const gbtree_ninfo *ti; + gbt_intquery querybuf; + const void *query; GBT_NUMKEY_R key; /* All cases served by this function are exact */ *recheck = false; key.lower = (GBT_NUMKEY *) &kkk->lower; key.upper = (GBT_NUMKEY *) &kkk->upper; - PG_RETURN_BOOL(gbt_num_consistent(&key, &query, strategy, - GIST_LEAF(entry), &tinfo, fcinfo->flinfo)); + ti = gbt_int4_crosstype(subtype, queryDatum, &querybuf, &query); + + PG_RETURN_BOOL(gbt_num_consistent(&key, query, strategy, + GIST_LEAF(entry), ti, fcinfo->flinfo)); } Datum gbt_int4_distance(PG_FUNCTION_ARGS) { GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); - int32 query = PG_GETARG_INT32(1); -#ifdef NOT_USED + Datum queryDatum = PG_GETARG_DATUM(1); Oid subtype = PG_GETARG_OID(3); -#endif int32KEY *kkk = (int32KEY *) DatumGetPointer(entry->key); + const gbtree_ninfo *ti; + gbt_intquery querybuf; + const void *query; GBT_NUMKEY_R key; key.lower = (GBT_NUMKEY *) &kkk->lower; key.upper = (GBT_NUMKEY *) &kkk->upper; - PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry), - &tinfo, fcinfo->flinfo)); + ti = gbt_int4_crosstype(subtype, queryDatum, &querybuf, &query); + + PG_RETURN_FLOAT8(gbt_num_distance(&key, query, GIST_LEAF(entry), + ti, fcinfo->flinfo)); } Datum gbt_int4_union(PG_FUNCTION_ARGS) { GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0); diff --git a/contrib/btree_gist/btree_int8.c b/contrib/btree_gist/btree_int8.c index ed55ac8dbfd..e37bca52905 100644 --- a/contrib/btree_gist/btree_int8.c +++ b/contrib/btree_gist/btree_int8.c @@ -2,12 +2,13 @@ * contrib/btree_gist/btree_int8.c */ #include "postgres.h" #include "btree_gist.h" #include "btree_utils_num.h" +#include "catalog/pg_type.h" #include "common/int.h" #include "utils/rel.h" #include "utils/sortsupport.h" typedef struct int64key { @@ -88,12 +89,82 @@ static const gbtree_ninfo tinfo = gbt_int8le, gbt_int8lt, gbt_int8key_cmp, gbt_int8_dist }; +/* + * Cross-type GiST callbacks: the indexed key is int8, the query value is int2 + * or int4. These tinfos let the cross-type cases reuse gbt_num_consistent() + * and gbt_num_distance(); their callbacks read the key (left argument) and the + * query value (right argument) each at its own width. f_cmp is not used on + * these paths and is left NULL. + */ +GBT_INT_CMP_FNS(gbt_int8_int2_, int64, int16) +GBT_INT_CMP_FNS(gbt_int8_int4_, int64, int32) + +static const gbtree_ninfo tinfo_int8_int2 = +{ + gbt_t_int8, + sizeof(int64), + 16, + gbt_int8_int2_gt, + gbt_int8_int2_ge, + gbt_int8_int2_eq, + gbt_int8_int2_le, + gbt_int8_int2_lt, + NULL, + gbt_int8_int2_dist +}; + +static const gbtree_ninfo tinfo_int8_int4 = +{ + gbt_t_int8, + sizeof(int64), + 16, + gbt_int8_int4_gt, + gbt_int8_int4_ge, + gbt_int8_int4_eq, + gbt_int8_int4_le, + gbt_int8_int4_lt, + NULL, + gbt_int8_int4_dist +}; + +/* + * Cross-type dispatch shared by gbt_int8_consistent and gbt_int8_distance: + * select the tinfo for the query subtype, read the query value at its own + * width into the caller's buffer, and point *query at it. + */ +static const gbtree_ninfo * +gbt_int8_crosstype(Oid subtype, Datum querydatum, gbt_intquery *buf, + const void **query) +{ + switch (subtype) + { + case INT2OID: + buf->i2 = DatumGetInt16(querydatum); + *query = &buf->i2; + return &tinfo_int8_int2; + case INT4OID: + buf->i4 = DatumGetInt32(querydatum); + *query = &buf->i4; + return &tinfo_int8_int4; + case InvalidOid: /* same-type: exclusion/temporal constraint + * checks pass the native type with subtype 0 */ + case INT8OID: + buf->i8 = DatumGetInt64(querydatum); + *query = &buf->i8; + return &tinfo; + default: + elog(ERROR, "unrecognized subtype %u for btree_gist int8 cross-type comparison", + subtype); + return NULL; /* keep compiler quiet */ + } +} + PG_FUNCTION_INFO_V1(int8_dist); Datum int8_dist(PG_FUNCTION_ARGS) { int64 a = PG_GETARG_INT64(0); @@ -109,12 +180,46 @@ int8_dist(PG_FUNCTION_ARGS) ra = i64abs(r); PG_RETURN_INT64(ra); } +PG_FUNCTION_INFO_V1(int8_int2_dist); +Datum +int8_int2_dist(PG_FUNCTION_ARGS) +{ + int64 a = PG_GETARG_INT64(0); + int64 b = (int64) PG_GETARG_INT16(1); + int64 r; + + if (pg_sub_s64_overflow(a, b, &r) || + r == PG_INT64_MIN) + ereport(ERROR, + (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), + errmsg("bigint out of range"))); + + PG_RETURN_INT64(i64abs(r)); +} + +PG_FUNCTION_INFO_V1(int8_int4_dist); +Datum +int8_int4_dist(PG_FUNCTION_ARGS) +{ + int64 a = PG_GETARG_INT64(0); + int64 b = (int64) PG_GETARG_INT32(1); + int64 r; + + if (pg_sub_s64_overflow(a, b, &r) || + r == PG_INT64_MIN) + ereport(ERROR, + (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), + errmsg("bigint out of range"))); + + PG_RETURN_INT64(i64abs(r)); +} + /************************************************** * GiST support functions **************************************************/ Datum @@ -134,47 +239,53 @@ gbt_int8_fetch(PG_FUNCTION_ARGS) } Datum gbt_int8_consistent(PG_FUNCTION_ARGS) { GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); - int64 query = PG_GETARG_INT64(1); + Datum queryDatum = PG_GETARG_DATUM(1); StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); -#ifdef NOT_USED Oid subtype = PG_GETARG_OID(3); -#endif bool *recheck = (bool *) PG_GETARG_POINTER(4); int64KEY *kkk = (int64KEY *) DatumGetPointer(entry->key); + const gbtree_ninfo *ti; + gbt_intquery querybuf; + const void *query; GBT_NUMKEY_R key; /* All cases served by this function are exact */ *recheck = false; key.lower = (GBT_NUMKEY *) &kkk->lower; key.upper = (GBT_NUMKEY *) &kkk->upper; - PG_RETURN_BOOL(gbt_num_consistent(&key, &query, strategy, - GIST_LEAF(entry), &tinfo, fcinfo->flinfo)); + ti = gbt_int8_crosstype(subtype, queryDatum, &querybuf, &query); + + PG_RETURN_BOOL(gbt_num_consistent(&key, query, strategy, + GIST_LEAF(entry), ti, fcinfo->flinfo)); } Datum gbt_int8_distance(PG_FUNCTION_ARGS) { GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); - int64 query = PG_GETARG_INT64(1); -#ifdef NOT_USED + Datum queryDatum = PG_GETARG_DATUM(1); Oid subtype = PG_GETARG_OID(3); -#endif int64KEY *kkk = (int64KEY *) DatumGetPointer(entry->key); + const gbtree_ninfo *ti; + gbt_intquery querybuf; + const void *query; GBT_NUMKEY_R key; key.lower = (GBT_NUMKEY *) &kkk->lower; key.upper = (GBT_NUMKEY *) &kkk->upper; - PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry), - &tinfo, fcinfo->flinfo)); + ti = gbt_int8_crosstype(subtype, queryDatum, &querybuf, &query); + + PG_RETURN_FLOAT8(gbt_num_distance(&key, query, GIST_LEAF(entry), + ti, fcinfo->flinfo)); } Datum gbt_int8_union(PG_FUNCTION_ARGS) { GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0); diff --git a/contrib/btree_gist/btree_utils_num.c b/contrib/btree_gist/btree_utils_num.c index 7429f1856c1..1896a76b57c 100644 --- a/contrib/btree_gist/btree_utils_num.c +++ b/contrib/btree_gist/btree_utils_num.c @@ -273,12 +273,16 @@ gbt_num_consistent(const GBT_NUMKEY_R *key, * On leaf pages we directly apply the check "key->lower OP query"; we * need not consider key->upper since it will be equal to key->lower. * * On internal pages we mostly need to check "is lower bound below query?" * and/or "is upper bound above query?", where we must allow equality in * both cases. + * + * The comparison callbacks are always passed the key first and the query + * value second, here and in gbt_num_distance(); the integer opclasses' + * cross-type callbacks rely on that order. */ #define lower_is_below_query() \ tinfo->f_le(key->lower, query, flinfo) #define upper_is_above_query() \ tinfo->f_ge(key->upper, query, flinfo) diff --git a/contrib/btree_gist/btree_utils_num.h b/contrib/btree_gist/btree_utils_num.h index 52f531f70e4..e90a5ce7baf 100644 --- a/contrib/btree_gist/btree_utils_num.h +++ b/contrib/btree_gist/btree_utils_num.h @@ -25,12 +25,25 @@ typedef struct typedef struct { int i; GBT_NUMKEY *t; } Nsrt; +/* + * Query-value storage for the integer opclasses' cross-type path. The caller + * owns one of these and passes its address to the per-type cross-type helper, + * which stores the query value at its own width and points the query pointer + * at it. + */ +typedef union +{ + int16 i2; + int32 i4; + int64 i8; +} gbt_intquery; + /* type description */ typedef struct { @@ -190,12 +203,38 @@ float_penalty_num_impl(double olower, double oupper, (ivp)->day * (24.0 * SECS_PER_HOUR) + \ (ivp)->month * (30.0 * SECS_PER_DAY)) /* This macro is not safe to use with actual float inputs, only integers */ #define GET_FLOAT_DISTANCE(t, arg1, arg2) fabs( ((float8) *((const t *) (arg1))) - ((float8) *((const t *) (arg2))) ) +/* Likewise, but the arguments may be of two different integer types */ +#define GET_FLOAT_DISTANCE2(t1, t2, arg1, arg2) fabs( ((float8) *((const t1 *) (arg1))) - ((float8) *((const t2 *) (arg2))) ) + +/* + * Generate the comparison/distance callbacks for a gbtree_ninfo whose key and + * query sides may be of different integer types. gbt_num_consistent() and + * gbt_num_distance() always invoke the callbacks as f_xx(key, query), so the + * first argument has the indexed key type KT and the second has the query + * type QT (the operator's right-hand type). Integer widening is + * value-preserving, so the comparisons need no explicit cast; the distance + * widens to float8 to avoid overflow in the subtraction. + */ +#define GBT_INT_CMP_FNS(prefix, KT, QT) \ +static bool prefix##gt(const void *a, const void *b, FmgrInfo *flinfo) \ +{ return *((const KT *) a) > *((const QT *) b); } \ +static bool prefix##ge(const void *a, const void *b, FmgrInfo *flinfo) \ +{ return *((const KT *) a) >= *((const QT *) b); } \ +static bool prefix##eq(const void *a, const void *b, FmgrInfo *flinfo) \ +{ return *((const KT *) a) == *((const QT *) b); } \ +static bool prefix##le(const void *a, const void *b, FmgrInfo *flinfo) \ +{ return *((const KT *) a) <= *((const QT *) b); } \ +static bool prefix##lt(const void *a, const void *b, FmgrInfo *flinfo) \ +{ return *((const KT *) a) < *((const QT *) b); } \ +static float8 prefix##dist(const void *a, const void *b, FmgrInfo *flinfo) \ +{ return GET_FLOAT_DISTANCE2(KT, QT, a, b); } + extern Interval *abs_interval(Interval *a); extern bool gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query, StrategyNumber strategy, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo); diff --git a/contrib/btree_gist/meson.build b/contrib/btree_gist/meson.build index 2b1a5463289..43ba130e8e4 100644 --- a/contrib/btree_gist/meson.build +++ b/contrib/btree_gist/meson.build @@ -49,12 +49,13 @@ install_data( 'btree_gist--1.4--1.5.sql', 'btree_gist--1.5--1.6.sql', 'btree_gist--1.6--1.7.sql', 'btree_gist--1.7--1.8.sql', 'btree_gist--1.8--1.9.sql', 'btree_gist--1.9.sql', + 'btree_gist--1.9--1.10.sql', kwargs: contrib_data_args, ) tests += { 'name': 'btree_gist', 'sd': meson.current_source_dir(), diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index 553162c0810..584edaf9da8 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -3794,12 +3794,13 @@ floating_decimal_32 floating_decimal_64 fmgr_hook_type foreign_glob_cxt foreign_loc_cxt freefunc fsec_t +gbt_intquery gbt_vsrt_arg gbtree_ninfo gbtree_vinfo generate_series_fctx generate_series_numeric_fctx generate_series_timestamp_fctx -- 2.54.0 (Apple Git-157)