Skip to content

Commit 6db4598

Browse files
committed
Add stratnum GiST support function
This is support function 12 for the GiST AM and translates "well-known" RT*StrategyNumber values into whatever strategy number is used by the opclass (since no particular numbers are actually required). We will use this to support temporal PRIMARY KEY/UNIQUE/FOREIGN KEY/FOR PORTION OF functionality. This commit adds two implementations, one for internal GiST opclasses (just an identity function) and another for btree_gist opclasses. It updates btree_gist from 1.7 to 1.8, adding the support function for all its opclasses. Author: Paul A. Jungwirth <[email protected]> Reviewed-by: Peter Eisentraut <[email protected]> Reviewed-by: jian he <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/CA+renyUApHgSZF9-nd-a0+OPGharLQLO=mDHcY4_qQ0+noCUVg@mail.gmail.com
1 parent b725b7e commit 6db4598

File tree

17 files changed

+273
-8
lines changed

17 files changed

+273
-8
lines changed

contrib/btree_gist/Makefile

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ 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 \
4041
time timetz date interval macaddr macaddr8 inet cidr text varchar char \
41-
bytea bit varbit numeric uuid not_equal enum bool partitions
42+
bytea bit varbit numeric uuid not_equal enum bool partitions \
43+
stratnum
4244

4345
SHLIB_LINK += $(filter -lm, $(LIBS))
4446

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
CREATE FUNCTION gist_stratnum_btree(smallint)
7+
RETURNS smallint
8+
AS 'MODULE_PATHNAME'
9+
LANGUAGE C IMMUTABLE PARALLEL SAFE STRICT;
10+
11+
ALTER OPERATOR FAMILY gist_oid_ops USING gist ADD
12+
FUNCTION 12 (oid, oid) gist_stratnum_btree (int2) ;
13+
14+
ALTER OPERATOR FAMILY gist_int2_ops USING gist ADD
15+
FUNCTION 12 (int2, int2) gist_stratnum_btree (int2) ;
16+
17+
ALTER OPERATOR FAMILY gist_int4_ops USING gist ADD
18+
FUNCTION 12 (int4, int4) gist_stratnum_btree (int2) ;
19+
20+
ALTER OPERATOR FAMILY gist_int8_ops USING gist ADD
21+
FUNCTION 12 (int8, int8) gist_stratnum_btree (int2) ;
22+
23+
ALTER OPERATOR FAMILY gist_float4_ops USING gist ADD
24+
FUNCTION 12 (float4, float4) gist_stratnum_btree (int2) ;
25+
26+
ALTER OPERATOR FAMILY gist_float8_ops USING gist ADD
27+
FUNCTION 12 (float8, float8) gist_stratnum_btree (int2) ;
28+
29+
ALTER OPERATOR FAMILY gist_timestamp_ops USING gist ADD
30+
FUNCTION 12 (timestamp, timestamp) gist_stratnum_btree (int2) ;
31+
32+
ALTER OPERATOR FAMILY gist_timestamptz_ops USING gist ADD
33+
FUNCTION 12 (timestamptz, timestamptz) gist_stratnum_btree (int2) ;
34+
35+
ALTER OPERATOR FAMILY gist_time_ops USING gist ADD
36+
FUNCTION 12 (time, time) gist_stratnum_btree (int2) ;
37+
38+
ALTER OPERATOR FAMILY gist_date_ops USING gist ADD
39+
FUNCTION 12 (date, date) gist_stratnum_btree (int2) ;
40+
41+
ALTER OPERATOR FAMILY gist_interval_ops USING gist ADD
42+
FUNCTION 12 (interval, interval) gist_stratnum_btree (int2) ;
43+
44+
ALTER OPERATOR FAMILY gist_cash_ops USING gist ADD
45+
FUNCTION 12 (money, money) gist_stratnum_btree (int2) ;
46+
47+
ALTER OPERATOR FAMILY gist_macaddr_ops USING gist ADD
48+
FUNCTION 12 (macaddr, macaddr) gist_stratnum_btree (int2) ;
49+
50+
ALTER OPERATOR FAMILY gist_text_ops USING gist ADD
51+
FUNCTION 12 (text, text) gist_stratnum_btree (int2) ;
52+
53+
ALTER OPERATOR FAMILY gist_bpchar_ops USING gist ADD
54+
FUNCTION 12 (bpchar, bpchar) gist_stratnum_btree (int2) ;
55+
56+
ALTER OPERATOR FAMILY gist_bytea_ops USING gist ADD
57+
FUNCTION 12 (bytea, bytea) gist_stratnum_btree (int2) ;
58+
59+
ALTER OPERATOR FAMILY gist_numeric_ops USING gist ADD
60+
FUNCTION 12 (numeric, numeric) gist_stratnum_btree (int2) ;
61+
62+
ALTER OPERATOR FAMILY gist_bit_ops USING gist ADD
63+
FUNCTION 12 (bit, bit) gist_stratnum_btree (int2) ;
64+
65+
ALTER OPERATOR FAMILY gist_vbit_ops USING gist ADD
66+
FUNCTION 12 (varbit, varbit) gist_stratnum_btree (int2) ;
67+
68+
ALTER OPERATOR FAMILY gist_inet_ops USING gist ADD
69+
FUNCTION 12 (inet, inet) gist_stratnum_btree (int2) ;
70+
71+
ALTER OPERATOR FAMILY gist_cidr_ops USING gist ADD
72+
FUNCTION 12 (cidr, cidr) gist_stratnum_btree (int2) ;
73+
74+
ALTER OPERATOR FAMILY gist_timetz_ops USING gist ADD
75+
FUNCTION 12 (timetz, timetz) gist_stratnum_btree (int2) ;
76+
77+
ALTER OPERATOR FAMILY gist_uuid_ops USING gist ADD
78+
FUNCTION 12 (uuid, uuid) gist_stratnum_btree (int2) ;
79+
80+
ALTER OPERATOR FAMILY gist_macaddr8_ops USING gist ADD
81+
FUNCTION 12 (macaddr8, macaddr8) gist_stratnum_btree (int2) ;
82+
83+
ALTER OPERATOR FAMILY gist_enum_ops USING gist ADD
84+
FUNCTION 12 (anyenum, anyenum) gist_stratnum_btree (int2) ;
85+
86+
ALTER OPERATOR FAMILY gist_bool_ops USING gist ADD
87+
FUNCTION 12 (bool, bool) gist_stratnum_btree (int2) ;

contrib/btree_gist/btree_gist.c

+27
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
*/
44
#include "postgres.h"
55

6+
#include "access/stratnum.h"
67
#include "utils/builtins.h"
78

89
PG_MODULE_MAGIC;
910

1011
PG_FUNCTION_INFO_V1(gbt_decompress);
1112
PG_FUNCTION_INFO_V1(gbtreekey_in);
1213
PG_FUNCTION_INFO_V1(gbtreekey_out);
14+
PG_FUNCTION_INFO_V1(gist_stratnum_btree);
1315

1416
/**************************************************
1517
* In/Out for keys
@@ -51,3 +53,28 @@ gbt_decompress(PG_FUNCTION_ARGS)
5153
{
5254
PG_RETURN_POINTER(PG_GETARG_POINTER(0));
5355
}
56+
57+
/*
58+
* Returns the btree number for equals, otherwise invalid.
59+
*/
60+
Datum
61+
gist_stratnum_btree(PG_FUNCTION_ARGS)
62+
{
63+
StrategyNumber strat = PG_GETARG_UINT16(0);
64+
65+
switch (strat)
66+
{
67+
case RTEqualStrategyNumber:
68+
PG_RETURN_UINT16(BTEqualStrategyNumber);
69+
case RTLessStrategyNumber:
70+
PG_RETURN_UINT16(BTLessStrategyNumber);
71+
case RTLessEqualStrategyNumber:
72+
PG_RETURN_UINT16(BTLessEqualStrategyNumber);
73+
case RTGreaterStrategyNumber:
74+
PG_RETURN_UINT16(BTGreaterStrategyNumber);
75+
case RTGreaterEqualStrategyNumber:
76+
PG_RETURN_UINT16(BTGreaterEqualStrategyNumber);
77+
default:
78+
PG_RETURN_UINT16(InvalidStrategy);
79+
}
80+
}

contrib/btree_gist/btree_gist.control

+1-1
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'
55
relocatable = true
66
trusted = true
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- test stratnum support func
2+
SELECT gist_stratnum_btree(3::smallint);
3+
gist_stratnum_btree
4+
---------------------
5+
0
6+
(1 row)
7+
8+
SELECT gist_stratnum_btree(18::smallint);
9+
gist_stratnum_btree
10+
---------------------
11+
3
12+
(1 row)
13+

contrib/btree_gist/meson.build

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ install_data(
5050
'btree_gist--1.4--1.5.sql',
5151
'btree_gist--1.5--1.6.sql',
5252
'btree_gist--1.6--1.7.sql',
53+
'btree_gist--1.7--1.8.sql',
5354
kwargs: contrib_data_args,
5455
)
5556

@@ -89,6 +90,7 @@ tests += {
8990
'enum',
9091
'bool',
9192
'partitions',
93+
'stratnum',
9294
],
9395
},
9496
}

contrib/btree_gist/sql/stratnum.sql

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

doc/src/sgml/gist.sgml

+64-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ CREATE INDEX ON my_table USING GIST (my_inet_column inet_ops);
272272

273273
<para>
274274
There are five methods that an index operator class for
275-
<acronym>GiST</acronym> must provide, and six that are optional.
275+
<acronym>GiST</acronym> must provide, and seven that are optional.
276276
Correctness of the index is ensured
277277
by proper implementation of the <function>same</function>, <function>consistent</function>
278278
and <function>union</function> methods, while efficiency (size and speed) of the
@@ -295,6 +295,10 @@ CREATE INDEX ON my_table USING GIST (my_inet_column inet_ops);
295295
user-specified parameters.
296296
The optional eleventh method <function>sortsupport</function> is used to
297297
speed up building a <acronym>GiST</acronym> index.
298+
The optional twelfth method <function>stratnum</function> is used to
299+
translate well-known <literal>RT*StrategyNumber</literal>s (from
300+
<filename>src/include/access/stratnum.h</filename>) into strategy numbers
301+
used by the operator class.
298302
</para>
299303

300304
<variablelist>
@@ -1169,6 +1173,65 @@ my_sortsupport(PG_FUNCTION_ARGS)
11691173
</para>
11701174
</listitem>
11711175
</varlistentry>
1176+
1177+
<varlistentry>
1178+
<term><function>stratnum</function></term>
1179+
<listitem>
1180+
<para>
1181+
Given an <literal>RT*StrategyNumber</literal> value from
1182+
<filename>src/include/access/stratnum.h</filename>, returns a strategy
1183+
number used by this operator class for matching functionality. The
1184+
function should return <literal>InvalidStrategy</literal> if the
1185+
operator class has no matching strategy.
1186+
</para>
1187+
1188+
<para>
1189+
The <acronym>SQL</acronym> declaration of the function must look like
1190+
this:
1191+
1192+
<programlisting>
1193+
CREATE OR REPLACE FUNCTION my_stratnum(integer)
1194+
RETURNS integer
1195+
AS 'MODULE_PATHNAME'
1196+
LANGUAGE C STRICT;
1197+
</programlisting>
1198+
</para>
1199+
1200+
<para>
1201+
The matching code in the C module could then follow this skeleton:
1202+
1203+
<programlisting>
1204+
PG_FUNCTION_INFO_V1(my_stratnum);
1205+
1206+
Datum
1207+
my_stratnum(PG_FUNCTION_ARGS)
1208+
{
1209+
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(1);
1210+
StrategyNumber ret = InvalidStrategy;
1211+
1212+
switch (strategy)
1213+
{
1214+
case RTEqualStrategyNumber:
1215+
ret = BTEqualStrategyNumber;
1216+
}
1217+
1218+
PG_RETURN_UINT16(ret);
1219+
}
1220+
</programlisting>
1221+
</para>
1222+
1223+
<para>
1224+
One translation function is provided by
1225+
<productname>PostgreSQL</productname>:
1226+
<literal>gist_stratnum_identity</literal> is for operator classes that
1227+
already use the <literal>RT*StrategyNumber</literal> constants. It
1228+
returns whatever is passed to it. The <literal>btree_gist</literal>
1229+
extension defines a second translation function,
1230+
<literal>gist_stratnum_btree</literal>, for operator classes that use
1231+
the <literal>BT*StrategyNumber</literal> constants.
1232+
</para>
1233+
</listitem>
1234+
</varlistentry>
11721235
</variablelist>
11731236

11741237
<para>

doc/src/sgml/xindex.sgml

+7-1
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@
508508
</table>
509509

510510
<para>
511-
GiST indexes have eleven support functions, six of which are optional,
511+
GiST indexes have twelve support functions, seven of which are optional,
512512
as shown in <xref linkend="xindex-gist-support-table"/>.
513513
(For more information see <xref linkend="gist"/>.)
514514
</para>
@@ -590,6 +590,12 @@
590590
(optional)</entry>
591591
<entry>11</entry>
592592
</row>
593+
<row>
594+
<entry><function>stratnum</function></entry>
595+
<entry>translate well-known strategy numbers to ones
596+
used by the operator class (optional)</entry>
597+
<entry>12</entry>
598+
</row>
593599
</tbody>
594600
</tgroup>
595601
</table>

src/backend/access/gist/gistutil.c

+14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "storage/indexfsm.h"
2424
#include "storage/lmgr.h"
2525
#include "utils/float.h"
26+
#include "utils/fmgrprotos.h"
2627
#include "utils/lsyscache.h"
2728
#include "utils/snapmgr.h"
2829
#include "utils/syscache.h"
@@ -1056,3 +1057,16 @@ gistGetFakeLSN(Relation rel)
10561057
return GetFakeLSNForUnloggedRel();
10571058
}
10581059
}
1060+
1061+
/*
1062+
* Returns the same number that was received.
1063+
*
1064+
* This is for GiST opclasses that use the RT*StrategyNumber constants.
1065+
*/
1066+
Datum
1067+
gist_stratnum_identity(PG_FUNCTION_ARGS)
1068+
{
1069+
StrategyNumber strat = PG_GETARG_UINT16(0);
1070+
1071+
PG_RETURN_UINT16(strat);
1072+
}

src/backend/access/gist/gistvalidate.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ gistvalidate(Oid opclassoid)
147147
ok = check_amproc_signature(procform->amproc, VOIDOID, true,
148148
1, 1, INTERNALOID);
149149
break;
150+
case GIST_STRATNUM_PROC:
151+
ok = check_amproc_signature(procform->amproc, INT2OID, true,
152+
1, 1, INT2OID);
153+
break;
150154
default:
151155
ereport(INFO,
152156
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
@@ -267,7 +271,8 @@ gistvalidate(Oid opclassoid)
267271
continue; /* got it */
268272
if (i == GIST_DISTANCE_PROC || i == GIST_FETCH_PROC ||
269273
i == GIST_COMPRESS_PROC || i == GIST_DECOMPRESS_PROC ||
270-
i == GIST_OPTIONS_PROC || i == GIST_SORTSUPPORT_PROC)
274+
i == GIST_OPTIONS_PROC || i == GIST_SORTSUPPORT_PROC ||
275+
i == GIST_STRATNUM_PROC)
271276
continue; /* optional methods */
272277
ereport(INFO,
273278
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
@@ -339,6 +344,7 @@ gistadjustmembers(Oid opfamilyoid,
339344
case GIST_FETCH_PROC:
340345
case GIST_OPTIONS_PROC:
341346
case GIST_SORTSUPPORT_PROC:
347+
case GIST_STRATNUM_PROC:
342348
/* Optional, so force it to be a soft family dependency */
343349
op->ref_is_hard = false;
344350
op->ref_is_family = true;

src/include/access/gist.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
#define GIST_FETCH_PROC 9
3939
#define GIST_OPTIONS_PROC 10
4040
#define GIST_SORTSUPPORT_PROC 11
41-
#define GISTNProcs 11
41+
#define GIST_STRATNUM_PROC 12
42+
#define GISTNProcs 12
4243

4344
/*
4445
* Page opaque data in a GiST index page.

src/include/catalog/catversion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202401131
60+
#define CATALOG_VERSION_NO 202401191
6161

6262
#endif

0 commit comments

Comments
 (0)