Skip to content

Commit 630f9a4

Browse files
committed
Change gist stratnum function to use CompareType
This changes commit 7406ab6 in that the gist strategy number mapping support function is changed to use the CompareType enum as input, instead of the "well-known" RT*StrategyNumber strategy numbers. This is a bit cleaner, since you are not dealing with two sets of strategy numbers. Also, this will enable us to subsume this system into a more general system of using CompareType to define operator semantics across index methods. Discussion: https://www.postgresql.org/message-id/flat/E72EAA49-354D-4C2E-8EB9-255197F55330@enterprisedb.com
1 parent 6339f64 commit 630f9a4

File tree

19 files changed

+145
-126
lines changed

19 files changed

+145
-126
lines changed

contrib/btree_gist/btree_gist--1.7--1.8.sql

+27-27
Original file line numberDiff line numberDiff line change
@@ -3,85 +3,85 @@
33
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
44
\echo Use "ALTER EXTENSION btree_gist UPDATE TO '1.8'" to load this file. \quit
55

6-
CREATE FUNCTION gist_stratnum_btree(smallint)
6+
CREATE FUNCTION gist_stratnum_btree(int)
77
RETURNS smallint
88
AS 'MODULE_PATHNAME'
99
LANGUAGE C IMMUTABLE PARALLEL SAFE STRICT;
1010

1111
ALTER OPERATOR FAMILY gist_oid_ops USING gist ADD
12-
FUNCTION 12 (oid, oid) gist_stratnum_btree (int2) ;
12+
FUNCTION 12 (oid, oid) gist_stratnum_btree (int) ;
1313

1414
ALTER OPERATOR FAMILY gist_int2_ops USING gist ADD
15-
FUNCTION 12 (int2, int2) gist_stratnum_btree (int2) ;
15+
FUNCTION 12 (int2, int2) gist_stratnum_btree (int) ;
1616

1717
ALTER OPERATOR FAMILY gist_int4_ops USING gist ADD
18-
FUNCTION 12 (int4, int4) gist_stratnum_btree (int2) ;
18+
FUNCTION 12 (int4, int4) gist_stratnum_btree (int) ;
1919

2020
ALTER OPERATOR FAMILY gist_int8_ops USING gist ADD
21-
FUNCTION 12 (int8, int8) gist_stratnum_btree (int2) ;
21+
FUNCTION 12 (int8, int8) gist_stratnum_btree (int) ;
2222

2323
ALTER OPERATOR FAMILY gist_float4_ops USING gist ADD
24-
FUNCTION 12 (float4, float4) gist_stratnum_btree (int2) ;
24+
FUNCTION 12 (float4, float4) gist_stratnum_btree (int) ;
2525

2626
ALTER OPERATOR FAMILY gist_float8_ops USING gist ADD
27-
FUNCTION 12 (float8, float8) gist_stratnum_btree (int2) ;
27+
FUNCTION 12 (float8, float8) gist_stratnum_btree (int) ;
2828

2929
ALTER OPERATOR FAMILY gist_timestamp_ops USING gist ADD
30-
FUNCTION 12 (timestamp, timestamp) gist_stratnum_btree (int2) ;
30+
FUNCTION 12 (timestamp, timestamp) gist_stratnum_btree (int) ;
3131

3232
ALTER OPERATOR FAMILY gist_timestamptz_ops USING gist ADD
33-
FUNCTION 12 (timestamptz, timestamptz) gist_stratnum_btree (int2) ;
33+
FUNCTION 12 (timestamptz, timestamptz) gist_stratnum_btree (int) ;
3434

3535
ALTER OPERATOR FAMILY gist_time_ops USING gist ADD
36-
FUNCTION 12 (time, time) gist_stratnum_btree (int2) ;
36+
FUNCTION 12 (time, time) gist_stratnum_btree (int) ;
3737

3838
ALTER OPERATOR FAMILY gist_date_ops USING gist ADD
39-
FUNCTION 12 (date, date) gist_stratnum_btree (int2) ;
39+
FUNCTION 12 (date, date) gist_stratnum_btree (int) ;
4040

4141
ALTER OPERATOR FAMILY gist_interval_ops USING gist ADD
42-
FUNCTION 12 (interval, interval) gist_stratnum_btree (int2) ;
42+
FUNCTION 12 (interval, interval) gist_stratnum_btree (int) ;
4343

4444
ALTER OPERATOR FAMILY gist_cash_ops USING gist ADD
45-
FUNCTION 12 (money, money) gist_stratnum_btree (int2) ;
45+
FUNCTION 12 (money, money) gist_stratnum_btree (int) ;
4646

4747
ALTER OPERATOR FAMILY gist_macaddr_ops USING gist ADD
48-
FUNCTION 12 (macaddr, macaddr) gist_stratnum_btree (int2) ;
48+
FUNCTION 12 (macaddr, macaddr) gist_stratnum_btree (int) ;
4949

5050
ALTER OPERATOR FAMILY gist_text_ops USING gist ADD
51-
FUNCTION 12 (text, text) gist_stratnum_btree (int2) ;
51+
FUNCTION 12 (text, text) gist_stratnum_btree (int) ;
5252

5353
ALTER OPERATOR FAMILY gist_bpchar_ops USING gist ADD
54-
FUNCTION 12 (bpchar, bpchar) gist_stratnum_btree (int2) ;
54+
FUNCTION 12 (bpchar, bpchar) gist_stratnum_btree (int) ;
5555

5656
ALTER OPERATOR FAMILY gist_bytea_ops USING gist ADD
57-
FUNCTION 12 (bytea, bytea) gist_stratnum_btree (int2) ;
57+
FUNCTION 12 (bytea, bytea) gist_stratnum_btree (int) ;
5858

5959
ALTER OPERATOR FAMILY gist_numeric_ops USING gist ADD
60-
FUNCTION 12 (numeric, numeric) gist_stratnum_btree (int2) ;
60+
FUNCTION 12 (numeric, numeric) gist_stratnum_btree (int) ;
6161

6262
ALTER OPERATOR FAMILY gist_bit_ops USING gist ADD
63-
FUNCTION 12 (bit, bit) gist_stratnum_btree (int2) ;
63+
FUNCTION 12 (bit, bit) gist_stratnum_btree (int) ;
6464

6565
ALTER OPERATOR FAMILY gist_vbit_ops USING gist ADD
66-
FUNCTION 12 (varbit, varbit) gist_stratnum_btree (int2) ;
66+
FUNCTION 12 (varbit, varbit) gist_stratnum_btree (int) ;
6767

6868
ALTER OPERATOR FAMILY gist_inet_ops USING gist ADD
69-
FUNCTION 12 (inet, inet) gist_stratnum_btree (int2) ;
69+
FUNCTION 12 (inet, inet) gist_stratnum_btree (int) ;
7070

7171
ALTER OPERATOR FAMILY gist_cidr_ops USING gist ADD
72-
FUNCTION 12 (cidr, cidr) gist_stratnum_btree (int2) ;
72+
FUNCTION 12 (cidr, cidr) gist_stratnum_btree (int) ;
7373

7474
ALTER OPERATOR FAMILY gist_timetz_ops USING gist ADD
75-
FUNCTION 12 (timetz, timetz) gist_stratnum_btree (int2) ;
75+
FUNCTION 12 (timetz, timetz) gist_stratnum_btree (int) ;
7676

7777
ALTER OPERATOR FAMILY gist_uuid_ops USING gist ADD
78-
FUNCTION 12 (uuid, uuid) gist_stratnum_btree (int2) ;
78+
FUNCTION 12 (uuid, uuid) gist_stratnum_btree (int) ;
7979

8080
ALTER OPERATOR FAMILY gist_macaddr8_ops USING gist ADD
81-
FUNCTION 12 (macaddr8, macaddr8) gist_stratnum_btree (int2) ;
81+
FUNCTION 12 (macaddr8, macaddr8) gist_stratnum_btree (int) ;
8282

8383
ALTER OPERATOR FAMILY gist_enum_ops USING gist ADD
84-
FUNCTION 12 (anyenum, anyenum) gist_stratnum_btree (int2) ;
84+
FUNCTION 12 (anyenum, anyenum) gist_stratnum_btree (int) ;
8585

8686
ALTER OPERATOR FAMILY gist_bool_ops USING gist ADD
87-
FUNCTION 12 (bool, bool) gist_stratnum_btree (int2) ;
87+
FUNCTION 12 (bool, bool) gist_stratnum_btree (int) ;

contrib/btree_gist/btree_gist.c

+8-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "postgres.h"
55

66
#include "access/stratnum.h"
7+
#include "nodes/primnodes.h"
78
#include "utils/builtins.h"
89

910
PG_MODULE_MAGIC;
@@ -60,19 +61,19 @@ gbt_decompress(PG_FUNCTION_ARGS)
6061
Datum
6162
gist_stratnum_btree(PG_FUNCTION_ARGS)
6263
{
63-
StrategyNumber strat = PG_GETARG_UINT16(0);
64+
CompareType cmptype = PG_GETARG_INT32(0);
6465

65-
switch (strat)
66+
switch (cmptype)
6667
{
67-
case RTEqualStrategyNumber:
68+
case COMPARE_EQ:
6869
PG_RETURN_UINT16(BTEqualStrategyNumber);
69-
case RTLessStrategyNumber:
70+
case COMPARE_LT:
7071
PG_RETURN_UINT16(BTLessStrategyNumber);
71-
case RTLessEqualStrategyNumber:
72+
case COMPARE_LE:
7273
PG_RETURN_UINT16(BTLessEqualStrategyNumber);
73-
case RTGreaterStrategyNumber:
74+
case COMPARE_GT:
7475
PG_RETURN_UINT16(BTGreaterStrategyNumber);
75-
case RTGreaterEqualStrategyNumber:
76+
case COMPARE_GE:
7677
PG_RETURN_UINT16(BTGreaterEqualStrategyNumber);
7778
default:
7879
PG_RETURN_UINT16(InvalidStrategy);

contrib/btree_gist/expected/stratnum.out

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
-- test stratnum support func
2-
SELECT gist_stratnum_btree(3::smallint);
2+
SELECT gist_stratnum_btree(7);
33
gist_stratnum_btree
44
---------------------
55
0
66
(1 row)
77

8-
SELECT gist_stratnum_btree(18::smallint);
8+
SELECT gist_stratnum_btree(3);
99
gist_stratnum_btree
1010
---------------------
1111
3

contrib/btree_gist/sql/stratnum.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
-- test stratnum support func
2-
SELECT gist_stratnum_btree(3::smallint);
3-
SELECT gist_stratnum_btree(18::smallint);
2+
SELECT gist_stratnum_btree(7);
3+
SELECT gist_stratnum_btree(3);

doc/src/sgml/gist.sgml

+12-12
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ CREATE INDEX ON my_table USING GIST (my_inet_column inet_ops);
290290
The optional eleventh method <function>sortsupport</function> is used to
291291
speed up building a <acronym>GiST</acronym> index.
292292
The optional twelfth method <function>stratnum</function> is used to
293-
translate well-known <literal>RT*StrategyNumber</literal>s (from
294-
<filename>src/include/access/stratnum.h</filename>) into strategy numbers
293+
translate compare types (from
294+
<filename>src/include/nodes/primnodes.h</filename>) into strategy numbers
295295
used by the operator class. This lets the core code look up operators for
296296
temporal constraint indexes.
297297
</para>
@@ -1173,8 +1173,8 @@ my_sortsupport(PG_FUNCTION_ARGS)
11731173
<term><function>stratnum</function></term>
11741174
<listitem>
11751175
<para>
1176-
Given an <literal>RT*StrategyNumber</literal> value from
1177-
<filename>src/include/access/stratnum.h</filename>, returns a strategy
1176+
Given a <literal>CompareType</literal> value from
1177+
<filename>src/include/nodes/primnodes.h</filename>, returns a strategy
11781178
number used by this operator class for matching functionality. The
11791179
function should return <literal>InvalidStrategy</literal> if the
11801180
operator class has no matching strategy.
@@ -1184,7 +1184,7 @@ my_sortsupport(PG_FUNCTION_ARGS)
11841184
This is used for temporal index constraints (i.e., <literal>PRIMARY
11851185
KEY</literal> and <literal>UNIQUE</literal>). If the operator class
11861186
provides this function and it returns results for
1187-
<literal>RTEqualStrategyNumber</literal>, it can be used in the
1187+
<literal>COMPARE_EQ</literal>, it can be used in the
11881188
non-<literal>WITHOUT OVERLAPS</literal> part(s) of an index constraint.
11891189
</para>
11901190

@@ -1194,7 +1194,7 @@ my_sortsupport(PG_FUNCTION_ARGS)
11941194

11951195
<programlisting>
11961196
CREATE OR REPLACE FUNCTION my_stratnum(integer)
1197-
RETURNS integer
1197+
RETURNS smallint
11981198
AS 'MODULE_PATHNAME'
11991199
LANGUAGE C STRICT;
12001200
</programlisting>
@@ -1209,12 +1209,12 @@ PG_FUNCTION_INFO_V1(my_stratnum);
12091209
Datum
12101210
my_stratnum(PG_FUNCTION_ARGS)
12111211
{
1212-
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(1);
1212+
CompareType cmptype = PG_GETARG_INT32(0);
12131213
StrategyNumber ret = InvalidStrategy;
12141214

1215-
switch (strategy)
1215+
switch (cmptype)
12161216
{
1217-
case RTEqualStrategyNumber:
1217+
case COMPARE_EQ:
12181218
ret = BTEqualStrategyNumber;
12191219
}
12201220

@@ -1226,9 +1226,9 @@ my_stratnum(PG_FUNCTION_ARGS)
12261226
<para>
12271227
One translation function is provided by
12281228
<productname>PostgreSQL</productname>:
1229-
<literal>gist_stratnum_identity</literal> is for operator classes that
1230-
already use the <literal>RT*StrategyNumber</literal> constants. It
1231-
returns whatever is passed to it. The <literal>btree_gist</literal>
1229+
<literal>gist_stratnum_common</literal> is for operator classes that
1230+
use the <literal>RT*StrategyNumber</literal> constants.
1231+
The <literal>btree_gist</literal>
12321232
extension defines a second translation function,
12331233
<literal>gist_stratnum_btree</literal>, for operator classes that use
12341234
the <literal>BT*StrategyNumber</literal> constants.

doc/src/sgml/xindex.sgml

+1-1
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@
592592
</row>
593593
<row>
594594
<entry><function>stratnum</function></entry>
595-
<entry>translate well-known strategy numbers to ones
595+
<entry>translate compare types to strategy numbers
596596
used by the operator class (optional)</entry>
597597
<entry>12</entry>
598598
</row>

src/backend/access/gist/gistutil.c

+26-9
Original file line numberDiff line numberDiff line change
@@ -1058,27 +1058,44 @@ gistGetFakeLSN(Relation rel)
10581058
}
10591059

10601060
/*
1061-
* Returns the same number that was received.
1062-
*
1063-
* This is for GiST opclasses that use the RT*StrategyNumber constants.
1061+
* This is a stratnum support function for GiST opclasses that use the
1062+
* RT*StrategyNumber constants.
10641063
*/
10651064
Datum
1066-
gist_stratnum_identity(PG_FUNCTION_ARGS)
1065+
gist_stratnum_common(PG_FUNCTION_ARGS)
10671066
{
1068-
StrategyNumber strat = PG_GETARG_UINT16(0);
1067+
CompareType cmptype = PG_GETARG_INT32(0);
10691068

1070-
PG_RETURN_UINT16(strat);
1069+
switch (cmptype)
1070+
{
1071+
case COMPARE_EQ:
1072+
PG_RETURN_UINT16(RTEqualStrategyNumber);
1073+
case COMPARE_LT:
1074+
PG_RETURN_UINT16(RTLessStrategyNumber);
1075+
case COMPARE_LE:
1076+
PG_RETURN_UINT16(RTLessEqualStrategyNumber);
1077+
case COMPARE_GT:
1078+
PG_RETURN_UINT16(RTGreaterStrategyNumber);
1079+
case COMPARE_GE:
1080+
PG_RETURN_UINT16(RTGreaterEqualStrategyNumber);
1081+
case COMPARE_OVERLAP:
1082+
PG_RETURN_UINT16(RTOverlapStrategyNumber);
1083+
case COMPARE_CONTAINED_BY:
1084+
PG_RETURN_UINT16(RTContainedByStrategyNumber);
1085+
default:
1086+
PG_RETURN_UINT16(InvalidStrategy);
1087+
}
10711088
}
10721089

10731090
/*
1074-
* Returns the opclass's private stratnum used for the given strategy.
1091+
* Returns the opclass's private stratnum used for the given compare type.
10751092
*
10761093
* Calls the opclass's GIST_STRATNUM_PROC support function, if any,
10771094
* and returns the result.
10781095
* Returns InvalidStrategy if the function is not defined.
10791096
*/
10801097
StrategyNumber
1081-
GistTranslateStratnum(Oid opclass, StrategyNumber strat)
1098+
GistTranslateStratnum(Oid opclass, CompareType cmptype)
10821099
{
10831100
Oid opfamily;
10841101
Oid opcintype;
@@ -1095,6 +1112,6 @@ GistTranslateStratnum(Oid opclass, StrategyNumber strat)
10951112
return InvalidStrategy;
10961113

10971114
/* Ask the translation function */
1098-
result = OidFunctionCall1Coll(funcid, InvalidOid, UInt16GetDatum(strat));
1115+
result = OidFunctionCall1Coll(funcid, InvalidOid, Int32GetDatum(cmptype));
10991116
return DatumGetUInt16(result);
11001117
}

src/backend/access/gist/gistvalidate.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ gistvalidate(Oid opclassoid)
148148
break;
149149
case GIST_STRATNUM_PROC:
150150
ok = check_amproc_signature(procform->amproc, INT2OID, true,
151-
1, 1, INT2OID);
151+
1, 1, INT4OID);
152152
break;
153153
default:
154154
ereport(INFO,

src/backend/catalog/pg_constraint.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -1647,22 +1647,22 @@ FindFKPeriodOpers(Oid opclass,
16471647
* of the old value, then we can treat the attribute as if it didn't
16481648
* change, and skip the RI check.
16491649
*/
1650-
strat = RTContainedByStrategyNumber;
1651-
GetOperatorFromWellKnownStrategy(opclass,
1652-
InvalidOid,
1653-
containedbyoperoid,
1654-
&strat);
1650+
GetOperatorFromCompareType(opclass,
1651+
InvalidOid,
1652+
COMPARE_CONTAINED_BY,
1653+
containedbyoperoid,
1654+
&strat);
16551655

16561656
/*
16571657
* Now look up the ContainedBy operator. Its left arg must be the type of
16581658
* the column (or rather of the opclass). Its right arg must match the
16591659
* return type of the support proc.
16601660
*/
1661-
strat = RTContainedByStrategyNumber;
1662-
GetOperatorFromWellKnownStrategy(opclass,
1663-
ANYMULTIRANGEOID,
1664-
aggedcontainedbyoperoid,
1665-
&strat);
1661+
GetOperatorFromCompareType(opclass,
1662+
ANYMULTIRANGEOID,
1663+
COMPARE_CONTAINED_BY,
1664+
aggedcontainedbyoperoid,
1665+
&strat);
16661666
}
16671667

16681668
/*

0 commit comments

Comments
 (0)