Add btree_gin support for enum types
Reviewed by Tom Lane and Anastasia Lubennikova Discussion: http://postgr.es/m/56EA8A71.8060107@dunslane.net
This commit is contained in:
parent
f7946a92b6
commit
4ad0f88c44
@ -4,13 +4,14 @@ MODULE_big = btree_gin
|
|||||||
OBJS = btree_gin.o $(WIN32RES)
|
OBJS = btree_gin.o $(WIN32RES)
|
||||||
|
|
||||||
EXTENSION = btree_gin
|
EXTENSION = btree_gin
|
||||||
DATA = btree_gin--1.0.sql btree_gin--1.0--1.1.sql btree_gin--unpackaged--1.0.sql
|
DATA = btree_gin--1.0.sql btree_gin--1.0--1.1.sql btree_gin--1.1--1.2.sql \
|
||||||
|
btree_gin--unpackaged--1.0.sql
|
||||||
PGFILEDESC = "btree_gin - B-tree equivalent GIN operator classes"
|
PGFILEDESC = "btree_gin - B-tree equivalent GIN operator classes"
|
||||||
|
|
||||||
REGRESS = install_btree_gin int2 int4 int8 float4 float8 money oid \
|
REGRESS = install_btree_gin int2 int4 int8 float4 float8 money oid \
|
||||||
timestamp timestamptz time timetz date interval \
|
timestamp timestamptz time timetz date interval \
|
||||||
macaddr macaddr8 inet cidr text varchar char bytea bit varbit \
|
macaddr macaddr8 inet cidr text varchar char bytea bit varbit \
|
||||||
numeric
|
numeric enum
|
||||||
|
|
||||||
ifdef USE_PGXS
|
ifdef USE_PGXS
|
||||||
PG_CONFIG = pg_config
|
PG_CONFIG = pg_config
|
||||||
|
47
contrib/btree_gin/btree_gin--1.1--1.2.sql
Normal file
47
contrib/btree_gin/btree_gin--1.1--1.2.sql
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
/* contrib/btree_gin/btree_gin--1.1--1.2.sql */
|
||||||
|
|
||||||
|
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
|
||||||
|
\echo Use "ALTER EXTENSION btree_gin UPDATE TO '1.1'" to load this file. \quit
|
||||||
|
|
||||||
|
--
|
||||||
|
--
|
||||||
|
--
|
||||||
|
-- enum ops
|
||||||
|
--
|
||||||
|
--
|
||||||
|
|
||||||
|
|
||||||
|
CREATE FUNCTION gin_extract_value_anyenum(anyenum, internal)
|
||||||
|
RETURNS internal
|
||||||
|
AS 'MODULE_PATHNAME'
|
||||||
|
LANGUAGE C STRICT IMMUTABLE;
|
||||||
|
|
||||||
|
CREATE FUNCTION gin_compare_prefix_anyenum(anyenum, anyenum, int2, internal)
|
||||||
|
RETURNS int4
|
||||||
|
AS 'MODULE_PATHNAME'
|
||||||
|
LANGUAGE C STRICT IMMUTABLE;
|
||||||
|
|
||||||
|
CREATE FUNCTION gin_extract_query_anyenum(anyenum, internal, int2, internal, internal)
|
||||||
|
RETURNS internal
|
||||||
|
AS 'MODULE_PATHNAME'
|
||||||
|
LANGUAGE C STRICT IMMUTABLE;
|
||||||
|
|
||||||
|
CREATE FUNCTION gin_enum_cmp(anyenum, anyenum)
|
||||||
|
RETURNS int4
|
||||||
|
AS 'MODULE_PATHNAME'
|
||||||
|
LANGUAGE C STRICT IMMUTABLE;
|
||||||
|
|
||||||
|
CREATE OPERATOR CLASS enum_ops
|
||||||
|
DEFAULT FOR TYPE anyenum USING gin
|
||||||
|
AS
|
||||||
|
OPERATOR 1 <,
|
||||||
|
OPERATOR 2 <=,
|
||||||
|
OPERATOR 3 =,
|
||||||
|
OPERATOR 4 >=,
|
||||||
|
OPERATOR 5 >,
|
||||||
|
FUNCTION 1 gin_enum_cmp(anyenum,anyenum),
|
||||||
|
FUNCTION 2 gin_extract_value_anyenum(anyenum, internal),
|
||||||
|
FUNCTION 3 gin_extract_query_anyenum(anyenum, internal, int2, internal, internal),
|
||||||
|
FUNCTION 4 gin_btree_consistent(internal, int2, anyelement, int4, internal, internal),
|
||||||
|
FUNCTION 5 gin_compare_prefix_anyenum(anyenum,anyenum,int2, internal),
|
||||||
|
STORAGE anyenum;
|
@ -25,7 +25,6 @@ typedef struct QueryInfo
|
|||||||
Datum (*typecmp) (FunctionCallInfo);
|
Datum (*typecmp) (FunctionCallInfo);
|
||||||
} QueryInfo;
|
} QueryInfo;
|
||||||
|
|
||||||
|
|
||||||
/*** GIN support functions shared by all datatypes ***/
|
/*** GIN support functions shared by all datatypes ***/
|
||||||
|
|
||||||
static Datum
|
static Datum
|
||||||
@ -112,13 +111,14 @@ gin_btree_compare_prefix(FunctionCallInfo fcinfo)
|
|||||||
int32 res,
|
int32 res,
|
||||||
cmp;
|
cmp;
|
||||||
|
|
||||||
cmp = DatumGetInt32(DirectFunctionCall2Coll(
|
cmp = DatumGetInt32(CallerFInfoFunctionCall2(
|
||||||
data->typecmp,
|
data->typecmp,
|
||||||
PG_GET_COLLATION(),
|
fcinfo->flinfo,
|
||||||
(data->strategy == BTLessStrategyNumber ||
|
PG_GET_COLLATION(),
|
||||||
data->strategy == BTLessEqualStrategyNumber)
|
(data->strategy == BTLessStrategyNumber ||
|
||||||
? data->datum : a,
|
data->strategy == BTLessEqualStrategyNumber)
|
||||||
b));
|
? data->datum : a,
|
||||||
|
b));
|
||||||
|
|
||||||
switch (data->strategy)
|
switch (data->strategy)
|
||||||
{
|
{
|
||||||
@ -426,3 +426,54 @@ leftmostvalue_numeric(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
GIN_SUPPORT(numeric, true, leftmostvalue_numeric, gin_numeric_cmp)
|
GIN_SUPPORT(numeric, true, leftmostvalue_numeric, gin_numeric_cmp)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Use a similar trick to that used for numeric for enums, since we don't
|
||||||
|
* actually know the leftmost value of any enum without knowing the concrete
|
||||||
|
* type, so we use a dummy leftmost value of InvalidOid.
|
||||||
|
*
|
||||||
|
* Note that we use CallerFInfoFunctionCall2 here so that enum_cmp
|
||||||
|
* gets a valid fn_extra to work with. Unlike most other type comparison
|
||||||
|
* routines it needs it, so we can't use DirectFunctionCall2.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#define ENUM_IS_LEFTMOST(x) ((x) == InvalidOid)
|
||||||
|
|
||||||
|
PG_FUNCTION_INFO_V1(gin_enum_cmp);
|
||||||
|
|
||||||
|
Datum
|
||||||
|
gin_enum_cmp(PG_FUNCTION_ARGS)
|
||||||
|
{
|
||||||
|
Oid a = PG_GETARG_OID(0);
|
||||||
|
Oid b = PG_GETARG_OID(1);
|
||||||
|
int res = 0;
|
||||||
|
|
||||||
|
if (ENUM_IS_LEFTMOST(a))
|
||||||
|
{
|
||||||
|
res = (ENUM_IS_LEFTMOST(b)) ? 0 : -1;
|
||||||
|
}
|
||||||
|
else if (ENUM_IS_LEFTMOST(b))
|
||||||
|
{
|
||||||
|
res = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
res = DatumGetInt32(CallerFInfoFunctionCall2(
|
||||||
|
enum_cmp,
|
||||||
|
fcinfo->flinfo,
|
||||||
|
PG_GET_COLLATION(),
|
||||||
|
ObjectIdGetDatum(a),
|
||||||
|
ObjectIdGetDatum(b)));
|
||||||
|
}
|
||||||
|
|
||||||
|
PG_RETURN_INT32(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Datum
|
||||||
|
leftmostvalue_enum(void)
|
||||||
|
{
|
||||||
|
return ObjectIdGetDatum(InvalidOid);
|
||||||
|
}
|
||||||
|
|
||||||
|
GIN_SUPPORT(anyenum, false, leftmostvalue_enum, gin_enum_cmp)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# btree_gin extension
|
# btree_gin extension
|
||||||
comment = 'support for indexing common datatypes in GIN'
|
comment = 'support for indexing common datatypes in GIN'
|
||||||
default_version = '1.1'
|
default_version = '1.2'
|
||||||
module_pathname = '$libdir/btree_gin'
|
module_pathname = '$libdir/btree_gin'
|
||||||
relocatable = true
|
relocatable = true
|
||||||
|
63
contrib/btree_gin/expected/enum.out
Normal file
63
contrib/btree_gin/expected/enum.out
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
set enable_seqscan=off;
|
||||||
|
CREATE TYPE rainbow AS ENUM ('r','o','y','g','b','i','v');
|
||||||
|
CREATE TABLE test_enum (
|
||||||
|
i rainbow
|
||||||
|
);
|
||||||
|
INSERT INTO test_enum VALUES ('v'),('y'),('r'),('g'),('o'),('i'),('b');
|
||||||
|
CREATE INDEX idx_enum ON test_enum USING gin (i);
|
||||||
|
SELECT * FROM test_enum WHERE i<'g'::rainbow ORDER BY i;
|
||||||
|
i
|
||||||
|
---
|
||||||
|
r
|
||||||
|
o
|
||||||
|
y
|
||||||
|
(3 rows)
|
||||||
|
|
||||||
|
SELECT * FROM test_enum WHERE i<='g'::rainbow ORDER BY i;
|
||||||
|
i
|
||||||
|
---
|
||||||
|
r
|
||||||
|
o
|
||||||
|
y
|
||||||
|
g
|
||||||
|
(4 rows)
|
||||||
|
|
||||||
|
SELECT * FROM test_enum WHERE i='g'::rainbow ORDER BY i;
|
||||||
|
i
|
||||||
|
---
|
||||||
|
g
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT * FROM test_enum WHERE i>='g'::rainbow ORDER BY i;
|
||||||
|
i
|
||||||
|
---
|
||||||
|
g
|
||||||
|
b
|
||||||
|
i
|
||||||
|
v
|
||||||
|
(4 rows)
|
||||||
|
|
||||||
|
SELECT * FROM test_enum WHERE i>'g'::rainbow ORDER BY i;
|
||||||
|
i
|
||||||
|
---
|
||||||
|
b
|
||||||
|
i
|
||||||
|
v
|
||||||
|
(3 rows)
|
||||||
|
|
||||||
|
explain (costs off) SELECT * FROM test_enum WHERE i>='g'::rainbow ORDER BY i;
|
||||||
|
QUERY PLAN
|
||||||
|
-----------------------------------------------
|
||||||
|
Sort
|
||||||
|
Sort Key: i
|
||||||
|
-> Bitmap Heap Scan on test_enum
|
||||||
|
Recheck Cond: (i >= 'g'::rainbow)
|
||||||
|
-> Bitmap Index Scan on idx_enum
|
||||||
|
Index Cond: (i >= 'g'::rainbow)
|
||||||
|
(6 rows)
|
||||||
|
|
||||||
|
-- make sure we handle the non-evenly-numbered oid case for enums
|
||||||
|
create type e as enum ('0', '2', '3');
|
||||||
|
alter type e add value '1' after '0';
|
||||||
|
create table t as select (i % 4)::text::e from generate_series(0, 100000) as i;
|
||||||
|
create index on t using gin (e);
|
26
contrib/btree_gin/sql/enum.sql
Normal file
26
contrib/btree_gin/sql/enum.sql
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
set enable_seqscan=off;
|
||||||
|
|
||||||
|
CREATE TYPE rainbow AS ENUM ('r','o','y','g','b','i','v');
|
||||||
|
|
||||||
|
CREATE TABLE test_enum (
|
||||||
|
i rainbow
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO test_enum VALUES ('v'),('y'),('r'),('g'),('o'),('i'),('b');
|
||||||
|
|
||||||
|
CREATE INDEX idx_enum ON test_enum USING gin (i);
|
||||||
|
|
||||||
|
SELECT * FROM test_enum WHERE i<'g'::rainbow ORDER BY i;
|
||||||
|
SELECT * FROM test_enum WHERE i<='g'::rainbow ORDER BY i;
|
||||||
|
SELECT * FROM test_enum WHERE i='g'::rainbow ORDER BY i;
|
||||||
|
SELECT * FROM test_enum WHERE i>='g'::rainbow ORDER BY i;
|
||||||
|
SELECT * FROM test_enum WHERE i>'g'::rainbow ORDER BY i;
|
||||||
|
|
||||||
|
explain (costs off) SELECT * FROM test_enum WHERE i>='g'::rainbow ORDER BY i;
|
||||||
|
|
||||||
|
|
||||||
|
-- make sure we handle the non-evenly-numbered oid case for enums
|
||||||
|
create type e as enum ('0', '2', '3');
|
||||||
|
alter type e add value '1' after '0';
|
||||||
|
create table t as select (i % 4)::text::e from generate_series(0, 100000) as i;
|
||||||
|
create index on t using gin (e);
|
Loading…
x
Reference in New Issue
Block a user