Skip to content

Commit f4cd710

Browse files
committed
Add support of bool, bpchar, name and uuid to btree_gin
Mostly for completeness, but I believe there are cases to use that in multicolumn GIN indexes. Bump btree_gin module version Author: Matheus Oliveira Reviewed by: Tomas Vondra Discussion: https://www.postgresql.org/message-id/flat/CAJghg4LMJf6Z13fnZD-MBNiGxzd0cA2=F3TDjNkX3eQH58hktQ@mail.gmail.com
1 parent 0a64b45 commit f4cd710

File tree

13 files changed

+690
-5
lines changed

13 files changed

+690
-5
lines changed

contrib/btree_gin/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ OBJS = btree_gin.o $(WIN32RES)
55

66
EXTENSION = btree_gin
77
DATA = btree_gin--1.0.sql btree_gin--1.0--1.1.sql btree_gin--1.1--1.2.sql \
8-
btree_gin--unpackaged--1.0.sql
8+
btree_gin--1.2--1.3.sql btree_gin--unpackaged--1.0.sql
99
PGFILEDESC = "btree_gin - B-tree equivalent GIN operator classes"
1010

1111
REGRESS = install_btree_gin int2 int4 int8 float4 float8 money oid \
1212
timestamp timestamptz time timetz date interval \
1313
macaddr macaddr8 inet cidr text varchar char bytea bit varbit \
14-
numeric enum
14+
numeric enum uuid name bool bpchar
1515

1616
ifdef USE_PGXS
1717
PG_CONFIG = pg_config
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/* contrib/btree_gin/btree_gin--1.2--1.3.sql */
2+
3+
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
4+
\echo Use "ALTER EXTENSION btree_gin UPDATE TO '1.3'" to load this file. \quit
5+
6+
-- uuid datatype support new in 1.3.
7+
CREATE FUNCTION gin_extract_value_uuid(uuid, internal)
8+
RETURNS internal
9+
AS 'MODULE_PATHNAME'
10+
LANGUAGE C STRICT IMMUTABLE;
11+
12+
CREATE FUNCTION gin_compare_prefix_uuid(uuid, uuid, int2, internal)
13+
RETURNS int4
14+
AS 'MODULE_PATHNAME'
15+
LANGUAGE C STRICT IMMUTABLE;
16+
17+
CREATE FUNCTION gin_extract_query_uuid(uuid, internal, int2, internal, internal)
18+
RETURNS internal
19+
AS 'MODULE_PATHNAME'
20+
LANGUAGE C STRICT IMMUTABLE;
21+
22+
CREATE OPERATOR CLASS uuid_ops
23+
DEFAULT FOR TYPE uuid USING gin
24+
AS
25+
OPERATOR 1 <,
26+
OPERATOR 2 <=,
27+
OPERATOR 3 =,
28+
OPERATOR 4 >=,
29+
OPERATOR 5 >,
30+
FUNCTION 1 uuid_cmp(uuid,uuid),
31+
FUNCTION 2 gin_extract_value_uuid(uuid, internal),
32+
FUNCTION 3 gin_extract_query_uuid(uuid, internal, int2, internal, internal),
33+
FUNCTION 4 gin_btree_consistent(internal, int2, anyelement, int4, internal, internal),
34+
FUNCTION 5 gin_compare_prefix_uuid(uuid,uuid,int2, internal),
35+
STORAGE uuid;
36+
37+
-- name datatype support new in 1.3.
38+
CREATE FUNCTION gin_extract_value_name(name, internal)
39+
RETURNS internal
40+
AS 'MODULE_PATHNAME'
41+
LANGUAGE C STRICT IMMUTABLE;
42+
43+
CREATE FUNCTION gin_compare_prefix_name(name, name, int2, internal)
44+
RETURNS int4
45+
AS 'MODULE_PATHNAME'
46+
LANGUAGE C STRICT IMMUTABLE;
47+
48+
CREATE FUNCTION gin_extract_query_name(name, internal, int2, internal, internal)
49+
RETURNS internal
50+
AS 'MODULE_PATHNAME'
51+
LANGUAGE C STRICT IMMUTABLE;
52+
53+
CREATE OPERATOR CLASS name_ops
54+
DEFAULT FOR TYPE name USING gin
55+
AS
56+
OPERATOR 1 <,
57+
OPERATOR 2 <=,
58+
OPERATOR 3 =,
59+
OPERATOR 4 >=,
60+
OPERATOR 5 >,
61+
FUNCTION 1 btnamecmp(name,name),
62+
FUNCTION 2 gin_extract_value_name(name, internal),
63+
FUNCTION 3 gin_extract_query_name(name, internal, int2, internal, internal),
64+
FUNCTION 4 gin_btree_consistent(internal, int2, anyelement, int4, internal, internal),
65+
FUNCTION 5 gin_compare_prefix_name(name,name,int2, internal),
66+
STORAGE name;
67+
68+
-- bool datatype support new in 1.3.
69+
CREATE FUNCTION gin_extract_value_bool(bool, internal)
70+
RETURNS internal
71+
AS 'MODULE_PATHNAME'
72+
LANGUAGE C STRICT IMMUTABLE;
73+
74+
CREATE FUNCTION gin_compare_prefix_bool(bool, bool, int2, internal)
75+
RETURNS int4
76+
AS 'MODULE_PATHNAME'
77+
LANGUAGE C STRICT IMMUTABLE;
78+
79+
CREATE FUNCTION gin_extract_query_bool(bool, internal, int2, internal, internal)
80+
RETURNS internal
81+
AS 'MODULE_PATHNAME'
82+
LANGUAGE C STRICT IMMUTABLE;
83+
84+
CREATE OPERATOR CLASS bool_ops
85+
DEFAULT FOR TYPE bool USING gin
86+
AS
87+
OPERATOR 1 <,
88+
OPERATOR 2 <=,
89+
OPERATOR 3 =,
90+
OPERATOR 4 >=,
91+
OPERATOR 5 >,
92+
FUNCTION 1 btboolcmp(bool,bool),
93+
FUNCTION 2 gin_extract_value_bool(bool, internal),
94+
FUNCTION 3 gin_extract_query_bool(bool, internal, int2, internal, internal),
95+
FUNCTION 4 gin_btree_consistent(internal, int2, anyelement, int4, internal, internal),
96+
FUNCTION 5 gin_compare_prefix_bool(bool,bool,int2, internal),
97+
STORAGE bool;
98+
99+
-- bpchar datatype support new in 1.3.
100+
CREATE FUNCTION gin_extract_value_bpchar(bpchar, internal)
101+
RETURNS internal
102+
AS 'MODULE_PATHNAME'
103+
LANGUAGE C STRICT IMMUTABLE;
104+
105+
CREATE FUNCTION gin_compare_prefix_bpchar(bpchar, bpchar, int2, internal)
106+
RETURNS int4
107+
AS 'MODULE_PATHNAME'
108+
LANGUAGE C STRICT IMMUTABLE;
109+
110+
CREATE FUNCTION gin_extract_query_bpchar(bpchar, internal, int2, internal, internal)
111+
RETURNS internal
112+
AS 'MODULE_PATHNAME'
113+
LANGUAGE C STRICT IMMUTABLE;
114+
115+
CREATE OPERATOR CLASS bpchar_ops
116+
DEFAULT FOR TYPE bpchar USING gin
117+
AS
118+
OPERATOR 1 <,
119+
OPERATOR 2 <=,
120+
OPERATOR 3 =,
121+
OPERATOR 4 >=,
122+
OPERATOR 5 >,
123+
FUNCTION 1 bpcharcmp(bpchar, bpchar),
124+
FUNCTION 2 gin_extract_value_bpchar(bpchar, internal),
125+
FUNCTION 3 gin_extract_query_bpchar(bpchar, internal, int2, internal, internal),
126+
FUNCTION 4 gin_btree_consistent(internal, int2, anyelement, int4, internal, internal),
127+
FUNCTION 5 gin_compare_prefix_bpchar(bpchar,bpchar,int2, internal),
128+
STORAGE bpchar;

contrib/btree_gin/btree_gin.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "utils/numeric.h"
1515
#include "utils/timestamp.h"
1616
#include "utils/varbit.h"
17+
#include "utils/uuid.h"
1718

1819
PG_MODULE_MAGIC;
1920

@@ -350,6 +351,8 @@ leftmostvalue_text(void)
350351

351352
GIN_SUPPORT(text, true, leftmostvalue_text, bttextcmp)
352353

354+
GIN_SUPPORT(bpchar, true, leftmostvalue_text, bpcharcmp)
355+
353356
static Datum
354357
leftmostvalue_char(void)
355358
{
@@ -437,7 +440,6 @@ GIN_SUPPORT(numeric, true, leftmostvalue_numeric, gin_numeric_cmp)
437440
* routines it needs it, so we can't use DirectFunctionCall2.
438441
*/
439442

440-
441443
#define ENUM_IS_LEFTMOST(x) ((x) == InvalidOid)
442444

443445
PG_FUNCTION_INFO_V1(gin_enum_cmp);
@@ -477,3 +479,30 @@ leftmostvalue_enum(void)
477479
}
478480

479481
GIN_SUPPORT(anyenum, false, leftmostvalue_enum, gin_enum_cmp)
482+
483+
static Datum
484+
leftmostvalue_uuid(void)
485+
{
486+
/* palloc0 will create the UUID with all zeroes: "00000000-0000-0000-0000-000000000000" */
487+
pg_uuid_t *retval = (pg_uuid_t *) palloc0(sizeof(pg_uuid_t));
488+
return UUIDPGetDatum(retval);
489+
}
490+
491+
GIN_SUPPORT(uuid, false, leftmostvalue_uuid, uuid_cmp)
492+
493+
static Datum
494+
leftmostvalue_name(void)
495+
{
496+
NameData* result = (NameData *) palloc0(NAMEDATALEN);
497+
return NameGetDatum(result);
498+
}
499+
500+
GIN_SUPPORT(name, false, leftmostvalue_name, btnamecmp)
501+
502+
static Datum
503+
leftmostvalue_bool(void)
504+
{
505+
return BoolGetDatum(false);
506+
}
507+
508+
GIN_SUPPORT(bool, false, leftmostvalue_bool, btboolcmp)

contrib/btree_gin/btree_gin.control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# btree_gin extension
22
comment = 'support for indexing common datatypes in GIN'
3-
default_version = '1.2'
3+
default_version = '1.3'
44
module_pathname = '$libdir/btree_gin'
55
relocatable = true

contrib/btree_gin/expected/bool.out

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
set enable_seqscan=off;
2+
CREATE TABLE test_bool (
3+
i boolean
4+
);
5+
INSERT INTO test_bool VALUES (false),(true),(null);
6+
CREATE INDEX idx_bool ON test_bool USING gin (i);
7+
SELECT * FROM test_bool WHERE i<true ORDER BY i;
8+
i
9+
---
10+
f
11+
(1 row)
12+
13+
SELECT * FROM test_bool WHERE i<=true ORDER BY i;
14+
i
15+
---
16+
f
17+
t
18+
(2 rows)
19+
20+
SELECT * FROM test_bool WHERE i=true ORDER BY i;
21+
i
22+
---
23+
t
24+
(1 row)
25+
26+
SELECT * FROM test_bool WHERE i>=true ORDER BY i;
27+
i
28+
---
29+
t
30+
(1 row)
31+
32+
SELECT * FROM test_bool WHERE i>true ORDER BY i;
33+
i
34+
---
35+
(0 rows)
36+
37+
SELECT * FROM test_bool WHERE i<false ORDER BY i;
38+
i
39+
---
40+
(0 rows)
41+
42+
SELECT * FROM test_bool WHERE i<=false ORDER BY i;
43+
i
44+
---
45+
f
46+
(1 row)
47+
48+
SELECT * FROM test_bool WHERE i=false ORDER BY i;
49+
i
50+
---
51+
f
52+
(1 row)
53+
54+
SELECT * FROM test_bool WHERE i>=false ORDER BY i;
55+
i
56+
---
57+
f
58+
t
59+
(2 rows)
60+
61+
SELECT * FROM test_bool WHERE i>false ORDER BY i;
62+
i
63+
---
64+
t
65+
(1 row)
66+
67+
EXPLAIN (COSTS OFF) SELECT * FROM test_bool WHERE i<true ORDER BY i;
68+
QUERY PLAN
69+
-------------------------------------------
70+
Sort
71+
Sort Key: i
72+
-> Bitmap Heap Scan on test_bool
73+
Recheck Cond: (i < true)
74+
-> Bitmap Index Scan on idx_bool
75+
Index Cond: (i < true)
76+
(6 rows)
77+
78+
EXPLAIN (COSTS OFF) SELECT * FROM test_bool WHERE i<=true ORDER BY i;
79+
QUERY PLAN
80+
-------------------------------------------
81+
Sort
82+
Sort Key: i
83+
-> Bitmap Heap Scan on test_bool
84+
Recheck Cond: (i <= true)
85+
-> Bitmap Index Scan on idx_bool
86+
Index Cond: (i <= true)
87+
(6 rows)
88+
89+
EXPLAIN (COSTS OFF) SELECT * FROM test_bool WHERE i=true ORDER BY i;
90+
QUERY PLAN
91+
-----------------------------
92+
Sort
93+
Sort Key: i
94+
-> Seq Scan on test_bool
95+
Filter: i
96+
(4 rows)
97+
98+
EXPLAIN (COSTS OFF) SELECT * FROM test_bool WHERE i>=true ORDER BY i;
99+
QUERY PLAN
100+
-------------------------------------------
101+
Sort
102+
Sort Key: i
103+
-> Bitmap Heap Scan on test_bool
104+
Recheck Cond: (i >= true)
105+
-> Bitmap Index Scan on idx_bool
106+
Index Cond: (i >= true)
107+
(6 rows)
108+
109+
EXPLAIN (COSTS OFF) SELECT * FROM test_bool WHERE i>true ORDER BY i;
110+
QUERY PLAN
111+
-------------------------------------------
112+
Sort
113+
Sort Key: i
114+
-> Bitmap Heap Scan on test_bool
115+
Recheck Cond: (i > true)
116+
-> Bitmap Index Scan on idx_bool
117+
Index Cond: (i > true)
118+
(6 rows)
119+

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