Skip to content

Commit 7406ab6

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. (previously committed as 6db4598, reverted by 8aee330; this is essentially unchanged from those) Author: Paul A. Jungwirth <pj@illuminatedcomputing.com> Reviewed-by: Peter Eisentraut <peter@eisentraut.org> Reviewed-by: jian he <jian.universality@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/CA+renyUApHgSZF9-nd-a0+OPGharLQLO=mDHcY4_qQ0+noCUVg@mail.gmail.com
1 parent 95d6e9a commit 7406ab6

File tree

17 files changed

+273
-8
lines changed

17 files changed

+273
-8
lines changed

contrib/btree_gist/Makefile

Lines changed: 4 additions & 2 deletions
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

Lines changed: 87 additions & 0 deletions
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

Lines changed: 27 additions & 0 deletions
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 supported operators, 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

Lines changed: 1 addition & 1 deletion
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
Lines changed: 13 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ CREATE INDEX ON my_table USING GIST (my_inet_column inet_ops);
266266

267267
<para>
268268
There are five methods that an index operator class for
269-
<acronym>GiST</acronym> must provide, and six that are optional.
269+
<acronym>GiST</acronym> must provide, and seven that are optional.
270270
Correctness of the index is ensured
271271
by proper implementation of the <function>same</function>, <function>consistent</function>
272272
and <function>union</function> methods, while efficiency (size and speed) of the
@@ -289,6 +289,10 @@ CREATE INDEX ON my_table USING GIST (my_inet_column inet_ops);
289289
user-specified parameters.
290290
The optional eleventh method <function>sortsupport</function> is used to
291291
speed up building a <acronym>GiST</acronym> index.
292+
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
295+
used by the operator class.
292296
</para>
293297

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

11681231
<para>

doc/src/sgml/xindex.sgml

Lines changed: 7 additions & 1 deletion
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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "common/pg_prng.h"
2222
#include "storage/indexfsm.h"
2323
#include "utils/float.h"
24+
#include "utils/fmgrprotos.h"
2425
#include "utils/lsyscache.h"
2526
#include "utils/rel.h"
2627
#include "utils/snapmgr.h"
@@ -1055,3 +1056,16 @@ gistGetFakeLSN(Relation rel)
10551056
return GetFakeLSNForUnloggedRel();
10561057
}
10571058
}
1059+
1060+
/*
1061+
* Returns the same number that was received.
1062+
*
1063+
* This is for GiST opclasses that use the RT*StrategyNumber constants.
1064+
*/
1065+
Datum
1066+
gist_stratnum_identity(PG_FUNCTION_ARGS)
1067+
{
1068+
StrategyNumber strat = PG_GETARG_UINT16(0);
1069+
1070+
PG_RETURN_UINT16(strat);
1071+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy