Skip to content

Commit feafce1

Browse files
akorotkovCommitfest Bot
authored andcommitted
Move scalar distance operators from btree_gist to core
Currently btree_gist is the only way to have knn search for scalar datatypes. This is why distance operators for those types are defined inside btree_gist as well. Upcoming knn-btree needs these distance operators to be defined in core. This commit moves them from btree_gist to core. Assuming that extension shared library should still work with non-upgraded extension catalog, we btree_gist still provides wrappers over core functions. Extension upgrade scripts switch opclasses to core operators and drops extension operators. Extension upgrade script has to refer @extschema@ to distinguish between operators with same name. Have to mark btree_gist as non-relocatable in order to do that. Catversion is bumped. btree_gist extension version is bumped. Discussion: https://postgr.es/m/ce35e97b-cf34-3f5d-6b99-2c25bae49999%40postgrespro.ru Author: Nikita Glukhov Reviewed-by: Robert Haas, Tom Lane, Anastasia Lubennikova, Alexander Korotkov
1 parent c7b0013 commit feafce1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1882
-140
lines changed

contrib/btree_gist/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ EXTENSION = btree_gist
3333
DATA = btree_gist--1.0--1.1.sql \
3434
btree_gist--1.1--1.2.sql btree_gist--1.2.sql btree_gist--1.2--1.3.sql \
3535
btree_gist--1.3--1.4.sql btree_gist--1.4--1.5.sql \
36-
btree_gist--1.5--1.6.sql btree_gist--1.6--1.7.sql
36+
btree_gist--1.5--1.6.sql btree_gist--1.6--1.7.sql \
37+
btree_gist--1.7--1.8.sql
3738
PGFILEDESC = "btree_gist - B-tree equivalent GiST operator classes"
3839

3940
REGRESS = init int2 int4 int8 float4 float8 cash oid timestamp timestamptz \

contrib/btree_gist/btree_cash.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "btree_gist.h"
77
#include "btree_utils_num.h"
88
#include "common/int.h"
9+
#include "utils/builtins.h"
910
#include "utils/cash.h"
1011

1112
typedef struct
@@ -95,20 +96,7 @@ PG_FUNCTION_INFO_V1(cash_dist);
9596
Datum
9697
cash_dist(PG_FUNCTION_ARGS)
9798
{
98-
Cash a = PG_GETARG_CASH(0);
99-
Cash b = PG_GETARG_CASH(1);
100-
Cash r;
101-
Cash ra;
102-
103-
if (pg_sub_s64_overflow(a, b, &r) ||
104-
r == PG_INT64_MIN)
105-
ereport(ERROR,
106-
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
107-
errmsg("money out of range")));
108-
109-
ra = i64abs(r);
110-
111-
PG_RETURN_CASH(ra);
99+
return cash_distance(fcinfo);
112100
}
113101

114102
/**************************************************

contrib/btree_gist/btree_date.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,7 @@ PG_FUNCTION_INFO_V1(date_dist);
118118
Datum
119119
date_dist(PG_FUNCTION_ARGS)
120120
{
121-
/* we assume the difference can't overflow */
122-
Datum diff = DirectFunctionCall2(date_mi,
123-
PG_GETARG_DATUM(0),
124-
PG_GETARG_DATUM(1));
125-
126-
PG_RETURN_INT32(abs(DatumGetInt32(diff)));
121+
return date_distance(fcinfo);
127122
}
128123

129124

contrib/btree_gist/btree_float4.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "btree_gist.h"
77
#include "btree_utils_num.h"
88
#include "utils/float.h"
9+
#include "utils/builtins.h"
910

1011
typedef struct float4key
1112
{
@@ -94,15 +95,7 @@ PG_FUNCTION_INFO_V1(float4_dist);
9495
Datum
9596
float4_dist(PG_FUNCTION_ARGS)
9697
{
97-
float4 a = PG_GETARG_FLOAT4(0);
98-
float4 b = PG_GETARG_FLOAT4(1);
99-
float4 r;
100-
101-
r = a - b;
102-
if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
103-
float_overflow_error();
104-
105-
PG_RETURN_FLOAT4(fabsf(r));
98+
return float4dist(fcinfo);
10699
}
107100

108101

contrib/btree_gist/btree_float8.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "btree_gist.h"
77
#include "btree_utils_num.h"
88
#include "utils/float.h"
9+
#include "utils/builtins.h"
910

1011
typedef struct float8key
1112
{
@@ -102,15 +103,7 @@ PG_FUNCTION_INFO_V1(float8_dist);
102103
Datum
103104
float8_dist(PG_FUNCTION_ARGS)
104105
{
105-
float8 a = PG_GETARG_FLOAT8(0);
106-
float8 b = PG_GETARG_FLOAT8(1);
107-
float8 r;
108-
109-
r = a - b;
110-
if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
111-
float_overflow_error();
112-
113-
PG_RETURN_FLOAT8(fabs(r));
106+
return float8dist(fcinfo);
114107
}
115108

116109
/**************************************************
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/* contrib/btree_gist/btree_gist--1.7--1.8.sql */
2+
3+
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
4+
\echo Use "ALTER EXTENSION btree_gist UPDATE TO '1.8'" to load this file. \quit
5+
6+
-- drop btree_gist distance operators from opfamilies
7+
8+
ALTER OPERATOR FAMILY gist_int2_ops USING gist DROP OPERATOR 15 (int2, int2);
9+
ALTER OPERATOR FAMILY gist_int4_ops USING gist DROP OPERATOR 15 (int4, int4);
10+
ALTER OPERATOR FAMILY gist_int8_ops USING gist DROP OPERATOR 15 (int8, int8);
11+
ALTER OPERATOR FAMILY gist_float4_ops USING gist DROP OPERATOR 15 (float4, float4);
12+
ALTER OPERATOR FAMILY gist_float8_ops USING gist DROP OPERATOR 15 (float8, float8);
13+
ALTER OPERATOR FAMILY gist_oid_ops USING gist DROP OPERATOR 15 (oid, oid);
14+
ALTER OPERATOR FAMILY gist_cash_ops USING gist DROP OPERATOR 15 (money, money);
15+
ALTER OPERATOR FAMILY gist_date_ops USING gist DROP OPERATOR 15 (date, date);
16+
ALTER OPERATOR FAMILY gist_time_ops USING gist DROP OPERATOR 15 (time, time);
17+
ALTER OPERATOR FAMILY gist_timestamp_ops USING gist DROP OPERATOR 15 (timestamp, timestamp);
18+
ALTER OPERATOR FAMILY gist_timestamptz_ops USING gist DROP OPERATOR 15 (timestamptz, timestamptz);
19+
ALTER OPERATOR FAMILY gist_interval_ops USING gist DROP OPERATOR 15 (interval, interval);
20+
21+
-- add pg_catalog distance operators to opfamilies
22+
23+
ALTER OPERATOR FAMILY gist_int2_ops USING gist ADD OPERATOR 15 pg_catalog.<-> (int2, int2) FOR ORDER BY pg_catalog.integer_ops;
24+
ALTER OPERATOR FAMILY gist_int4_ops USING gist ADD OPERATOR 15 pg_catalog.<-> (int4, int4) FOR ORDER BY pg_catalog.integer_ops;
25+
ALTER OPERATOR FAMILY gist_int8_ops USING gist ADD OPERATOR 15 pg_catalog.<-> (int8, int8) FOR ORDER BY pg_catalog.integer_ops;
26+
ALTER OPERATOR FAMILY gist_float4_ops USING gist ADD OPERATOR 15 pg_catalog.<-> (float4, float4) FOR ORDER BY pg_catalog.float_ops;
27+
ALTER OPERATOR FAMILY gist_float8_ops USING gist ADD OPERATOR 15 pg_catalog.<-> (float8, float8) FOR ORDER BY pg_catalog.float_ops;
28+
ALTER OPERATOR FAMILY gist_oid_ops USING gist ADD OPERATOR 15 pg_catalog.<-> (oid, oid) FOR ORDER BY pg_catalog.oid_ops;
29+
ALTER OPERATOR FAMILY gist_cash_ops USING gist ADD OPERATOR 15 pg_catalog.<-> (money, money) FOR ORDER BY pg_catalog.money_ops;
30+
ALTER OPERATOR FAMILY gist_date_ops USING gist ADD OPERATOR 15 pg_catalog.<-> (date, date) FOR ORDER BY pg_catalog.integer_ops;
31+
ALTER OPERATOR FAMILY gist_time_ops USING gist ADD OPERATOR 15 pg_catalog.<-> (time, time) FOR ORDER BY pg_catalog.interval_ops;
32+
ALTER OPERATOR FAMILY gist_timestamp_ops USING gist ADD OPERATOR 15 pg_catalog.<-> (timestamp, timestamp) FOR ORDER BY pg_catalog.interval_ops;
33+
ALTER OPERATOR FAMILY gist_timestamptz_ops USING gist ADD OPERATOR 15 pg_catalog.<-> (timestamptz, timestamptz) FOR ORDER BY pg_catalog.interval_ops;
34+
ALTER OPERATOR FAMILY gist_interval_ops USING gist ADD OPERATOR 15 pg_catalog.<-> (interval, interval) FOR ORDER BY pg_catalog.interval_ops;
35+
36+
-- drop distance operators
37+
38+
ALTER EXTENSION btree_gist DROP OPERATOR @extschema@.<-> (int2, int2);
39+
ALTER EXTENSION btree_gist DROP OPERATOR @extschema@.<-> (int4, int4);
40+
ALTER EXTENSION btree_gist DROP OPERATOR @extschema@.<-> (int8, int8);
41+
ALTER EXTENSION btree_gist DROP OPERATOR @extschema@.<-> (float4, float4);
42+
ALTER EXTENSION btree_gist DROP OPERATOR @extschema@.<-> (float8, float8);
43+
ALTER EXTENSION btree_gist DROP OPERATOR @extschema@.<-> (oid, oid);
44+
ALTER EXTENSION btree_gist DROP OPERATOR @extschema@.<-> (money, money);
45+
ALTER EXTENSION btree_gist DROP OPERATOR @extschema@.<-> (date, date);
46+
ALTER EXTENSION btree_gist DROP OPERATOR @extschema@.<-> (time, time);
47+
ALTER EXTENSION btree_gist DROP OPERATOR @extschema@.<-> (timestamp, timestamp);
48+
ALTER EXTENSION btree_gist DROP OPERATOR @extschema@.<-> (timestamptz, timestamptz);
49+
ALTER EXTENSION btree_gist DROP OPERATOR @extschema@.<-> (interval, interval);
50+
51+
DROP OPERATOR @extschema@.<-> (int2, int2);
52+
DROP OPERATOR @extschema@.<-> (int4, int4);
53+
DROP OPERATOR @extschema@.<-> (int8, int8);
54+
DROP OPERATOR @extschema@.<-> (float4, float4);
55+
DROP OPERATOR @extschema@.<-> (float8, float8);
56+
DROP OPERATOR @extschema@.<-> (oid, oid);
57+
DROP OPERATOR @extschema@.<-> (money, money);
58+
DROP OPERATOR @extschema@.<-> (date, date);
59+
DROP OPERATOR @extschema@.<-> (time, time);
60+
DROP OPERATOR @extschema@.<-> (timestamp, timestamp);
61+
DROP OPERATOR @extschema@.<-> (timestamptz, timestamptz);
62+
DROP OPERATOR @extschema@.<-> (interval, interval);
63+
64+
-- drop distance functions
65+
66+
ALTER EXTENSION btree_gist DROP FUNCTION @extschema@.int2_dist(int2, int2);
67+
ALTER EXTENSION btree_gist DROP FUNCTION @extschema@.int4_dist(int4, int4);
68+
ALTER EXTENSION btree_gist DROP FUNCTION @extschema@.int8_dist(int8, int8);
69+
ALTER EXTENSION btree_gist DROP FUNCTION @extschema@.float4_dist(float4, float4);
70+
ALTER EXTENSION btree_gist DROP FUNCTION @extschema@.float8_dist(float8, float8);
71+
ALTER EXTENSION btree_gist DROP FUNCTION @extschema@.oid_dist(oid, oid);
72+
ALTER EXTENSION btree_gist DROP FUNCTION @extschema@.cash_dist(money, money);
73+
ALTER EXTENSION btree_gist DROP FUNCTION @extschema@.date_dist(date, date);
74+
ALTER EXTENSION btree_gist DROP FUNCTION @extschema@.time_dist(time, time);
75+
ALTER EXTENSION btree_gist DROP FUNCTION @extschema@.ts_dist(timestamp, timestamp);
76+
ALTER EXTENSION btree_gist DROP FUNCTION @extschema@.tstz_dist(timestamptz, timestamptz);
77+
ALTER EXTENSION btree_gist DROP FUNCTION @extschema@.interval_dist(interval, interval);
78+
79+
DROP FUNCTION @extschema@.int2_dist(int2, int2);
80+
DROP FUNCTION @extschema@.int4_dist(int4, int4);
81+
DROP FUNCTION @extschema@.int8_dist(int8, int8);
82+
DROP FUNCTION @extschema@.float4_dist(float4, float4);
83+
DROP FUNCTION @extschema@.float8_dist(float8, float8);
84+
DROP FUNCTION @extschema@.oid_dist(oid, oid);
85+
DROP FUNCTION @extschema@.cash_dist(money, money);
86+
DROP FUNCTION @extschema@.date_dist(date, date);
87+
DROP FUNCTION @extschema@.time_dist(time, time);
88+
DROP FUNCTION @extschema@.ts_dist(timestamp, timestamp);
89+
DROP FUNCTION @extschema@.tstz_dist(timestamptz, timestamptz);
90+
DROP FUNCTION @extschema@.interval_dist(interval, interval);

contrib/btree_gist/btree_gist.control

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# btree_gist extension
22
comment = 'support for indexing common datatypes in GiST'
3-
default_version = '1.7'
3+
default_version = '1.8'
44
module_pathname = '$libdir/btree_gist'
5-
relocatable = true
6-
trusted = true
5+
relocatable = false
6+
trusted = true

contrib/btree_gist/btree_int2.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "btree_gist.h"
77
#include "btree_utils_num.h"
88
#include "common/int.h"
9+
#include "utils/builtins.h"
910

1011
typedef struct int16key
1112
{
@@ -94,20 +95,7 @@ PG_FUNCTION_INFO_V1(int2_dist);
9495
Datum
9596
int2_dist(PG_FUNCTION_ARGS)
9697
{
97-
int16 a = PG_GETARG_INT16(0);
98-
int16 b = PG_GETARG_INT16(1);
99-
int16 r;
100-
int16 ra;
101-
102-
if (pg_sub_s16_overflow(a, b, &r) ||
103-
r == PG_INT16_MIN)
104-
ereport(ERROR,
105-
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
106-
errmsg("smallint out of range")));
107-
108-
ra = abs(r);
109-
110-
PG_RETURN_INT16(ra);
98+
return int2dist(fcinfo);
11199
}
112100

113101

contrib/btree_gist/btree_int4.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "btree_gist.h"
77
#include "btree_utils_num.h"
88
#include "common/int.h"
9+
#include "utils/builtins.h"
910

1011
typedef struct int32key
1112
{
@@ -95,20 +96,7 @@ PG_FUNCTION_INFO_V1(int4_dist);
9596
Datum
9697
int4_dist(PG_FUNCTION_ARGS)
9798
{
98-
int32 a = PG_GETARG_INT32(0);
99-
int32 b = PG_GETARG_INT32(1);
100-
int32 r;
101-
int32 ra;
102-
103-
if (pg_sub_s32_overflow(a, b, &r) ||
104-
r == PG_INT32_MIN)
105-
ereport(ERROR,
106-
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
107-
errmsg("integer out of range")));
108-
109-
ra = abs(r);
110-
111-
PG_RETURN_INT32(ra);
99+
return int4dist(fcinfo);
112100
}
113101

114102

contrib/btree_gist/btree_int8.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "btree_gist.h"
77
#include "btree_utils_num.h"
88
#include "common/int.h"
9+
#include "utils/builtins.h"
910

1011
typedef struct int64key
1112
{
@@ -95,20 +96,7 @@ PG_FUNCTION_INFO_V1(int8_dist);
9596
Datum
9697
int8_dist(PG_FUNCTION_ARGS)
9798
{
98-
int64 a = PG_GETARG_INT64(0);
99-
int64 b = PG_GETARG_INT64(1);
100-
int64 r;
101-
int64 ra;
102-
103-
if (pg_sub_s64_overflow(a, b, &r) ||
104-
r == PG_INT64_MIN)
105-
ereport(ERROR,
106-
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
107-
errmsg("bigint out of range")));
108-
109-
ra = i64abs(r);
110-
111-
PG_RETURN_INT64(ra);
99+
return int8dist(fcinfo);
112100
}
113101

114102

contrib/btree_gist/btree_interval.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,7 @@ PG_FUNCTION_INFO_V1(interval_dist);
128128
Datum
129129
interval_dist(PG_FUNCTION_ARGS)
130130
{
131-
Datum diff = DirectFunctionCall2(interval_mi,
132-
PG_GETARG_DATUM(0),
133-
PG_GETARG_DATUM(1));
134-
135-
PG_RETURN_INTERVAL_P(abs_interval(DatumGetIntervalP(diff)));
131+
return interval_distance(fcinfo);
136132
}
137133

138134

contrib/btree_gist/btree_oid.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "btree_gist.h"
77
#include "btree_utils_num.h"
8+
#include "utils/builtins.h"
89

910
typedef struct
1011
{
@@ -100,15 +101,7 @@ PG_FUNCTION_INFO_V1(oid_dist);
100101
Datum
101102
oid_dist(PG_FUNCTION_ARGS)
102103
{
103-
Oid a = PG_GETARG_OID(0);
104-
Oid b = PG_GETARG_OID(1);
105-
Oid res;
106-
107-
if (a < b)
108-
res = b - a;
109-
else
110-
res = a - b;
111-
PG_RETURN_OID(res);
104+
return oiddist(fcinfo);
112105
}
113106

114107

contrib/btree_gist/btree_time.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,7 @@ PG_FUNCTION_INFO_V1(time_dist);
141141
Datum
142142
time_dist(PG_FUNCTION_ARGS)
143143
{
144-
Datum diff = DirectFunctionCall2(time_mi_time,
145-
PG_GETARG_DATUM(0),
146-
PG_GETARG_DATUM(1));
147-
148-
PG_RETURN_INTERVAL_P(abs_interval(DatumGetIntervalP(diff)));
144+
return time_distance(fcinfo);
149145
}
150146

151147

contrib/btree_gist/btree_ts.c

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -146,48 +146,14 @@ PG_FUNCTION_INFO_V1(ts_dist);
146146
Datum
147147
ts_dist(PG_FUNCTION_ARGS)
148148
{
149-
Timestamp a = PG_GETARG_TIMESTAMP(0);
150-
Timestamp b = PG_GETARG_TIMESTAMP(1);
151-
Interval *r;
152-
153-
if (TIMESTAMP_NOT_FINITE(a) || TIMESTAMP_NOT_FINITE(b))
154-
{
155-
Interval *p = palloc(sizeof(Interval));
156-
157-
p->day = INT_MAX;
158-
p->month = INT_MAX;
159-
p->time = PG_INT64_MAX;
160-
PG_RETURN_INTERVAL_P(p);
161-
}
162-
else
163-
r = DatumGetIntervalP(DirectFunctionCall2(timestamp_mi,
164-
PG_GETARG_DATUM(0),
165-
PG_GETARG_DATUM(1)));
166-
PG_RETURN_INTERVAL_P(abs_interval(r));
149+
return timestamp_distance(fcinfo);
167150
}
168151

169152
PG_FUNCTION_INFO_V1(tstz_dist);
170153
Datum
171154
tstz_dist(PG_FUNCTION_ARGS)
172155
{
173-
TimestampTz a = PG_GETARG_TIMESTAMPTZ(0);
174-
TimestampTz b = PG_GETARG_TIMESTAMPTZ(1);
175-
Interval *r;
176-
177-
if (TIMESTAMP_NOT_FINITE(a) || TIMESTAMP_NOT_FINITE(b))
178-
{
179-
Interval *p = palloc(sizeof(Interval));
180-
181-
p->day = INT_MAX;
182-
p->month = INT_MAX;
183-
p->time = PG_INT64_MAX;
184-
PG_RETURN_INTERVAL_P(p);
185-
}
186-
187-
r = DatumGetIntervalP(DirectFunctionCall2(timestamp_mi,
188-
PG_GETARG_DATUM(0),
189-
PG_GETARG_DATUM(1)));
190-
PG_RETURN_INTERVAL_P(abs_interval(r));
156+
return timestamptz_distance(fcinfo);
191157
}
192158

193159

0 commit comments

Comments
 (0)