mirror of
https://github.com/postgres/postgres.git
synced 2025-07-30 11:03:19 +03:00
Allow extensions to generate lossy index conditions.
For a long time, indxpath.c has had the ability to extract derived (lossy) index conditions from certain operators such as LIKE. For just as long, it's been obvious that we really ought to make that capability available to extensions. This commit finally accomplishes that, by adding another API for planner support functions that lets them create derived index conditions for their functions. As proof of concept, the hardwired "special index operator" code formerly present in indxpath.c is pushed out to planner support functions attached to LIKE and other relevant operators. A weak spot in this design is that an extension needs to know OIDs for the operators, datatypes, and opfamilies involved in the transformation it wants to make. The core-code prototypes use hard-wired OID references but extensions don't have that option for their own operators etc. It's usually possible to look up the required info, but that may be slow and inconvenient. However, improving that situation is a separate task. I want to do some additional refactorization around selfuncs.c, but that also seems like a separate task. Discussion: https://postgr.es/m/15193.1548028093@sss.pgh.pa.us
This commit is contained in:
@ -105,6 +105,15 @@ SELECT b.*
|
||||
set enable_seqscan to false;
|
||||
set enable_indexscan to true;
|
||||
set enable_bitmapscan to false;
|
||||
explain (costs off)
|
||||
select proname from pg_proc where proname like E'RI\\_FKey%del' order by 1;
|
||||
QUERY PLAN
|
||||
------------------------------------------------------------------------------
|
||||
Index Only Scan using pg_proc_proname_args_nsp_index on pg_proc
|
||||
Index Cond: ((proname >= 'RI_FKey'::text) AND (proname < 'RI_FKez'::text))
|
||||
Filter: (proname ~~ 'RI\_FKey%del'::text)
|
||||
(3 rows)
|
||||
|
||||
select proname from pg_proc where proname like E'RI\\_FKey%del' order by 1;
|
||||
proname
|
||||
------------------------
|
||||
@ -115,8 +124,42 @@ select proname from pg_proc where proname like E'RI\\_FKey%del' order by 1;
|
||||
RI_FKey_setnull_del
|
||||
(5 rows)
|
||||
|
||||
explain (costs off)
|
||||
select proname from pg_proc where proname ilike '00%foo' order by 1;
|
||||
QUERY PLAN
|
||||
--------------------------------------------------------------------
|
||||
Index Only Scan using pg_proc_proname_args_nsp_index on pg_proc
|
||||
Index Cond: ((proname >= '00'::text) AND (proname < '01'::text))
|
||||
Filter: (proname ~~* '00%foo'::text)
|
||||
(3 rows)
|
||||
|
||||
select proname from pg_proc where proname ilike '00%foo' order by 1;
|
||||
proname
|
||||
---------
|
||||
(0 rows)
|
||||
|
||||
explain (costs off)
|
||||
select proname from pg_proc where proname ilike 'ri%foo' order by 1;
|
||||
QUERY PLAN
|
||||
-----------------------------------------------------------------
|
||||
Index Only Scan using pg_proc_proname_args_nsp_index on pg_proc
|
||||
Filter: (proname ~~* 'ri%foo'::text)
|
||||
(2 rows)
|
||||
|
||||
set enable_indexscan to false;
|
||||
set enable_bitmapscan to true;
|
||||
explain (costs off)
|
||||
select proname from pg_proc where proname like E'RI\\_FKey%del' order by 1;
|
||||
QUERY PLAN
|
||||
------------------------------------------------------------------------------------------
|
||||
Sort
|
||||
Sort Key: proname
|
||||
-> Bitmap Heap Scan on pg_proc
|
||||
Filter: (proname ~~ 'RI\_FKey%del'::text)
|
||||
-> Bitmap Index Scan on pg_proc_proname_args_nsp_index
|
||||
Index Cond: ((proname >= 'RI_FKey'::text) AND (proname < 'RI_FKez'::text))
|
||||
(6 rows)
|
||||
|
||||
select proname from pg_proc where proname like E'RI\\_FKey%del' order by 1;
|
||||
proname
|
||||
------------------------
|
||||
@ -127,6 +170,34 @@ select proname from pg_proc where proname like E'RI\\_FKey%del' order by 1;
|
||||
RI_FKey_setnull_del
|
||||
(5 rows)
|
||||
|
||||
explain (costs off)
|
||||
select proname from pg_proc where proname ilike '00%foo' order by 1;
|
||||
QUERY PLAN
|
||||
--------------------------------------------------------------------------------
|
||||
Sort
|
||||
Sort Key: proname
|
||||
-> Bitmap Heap Scan on pg_proc
|
||||
Filter: (proname ~~* '00%foo'::text)
|
||||
-> Bitmap Index Scan on pg_proc_proname_args_nsp_index
|
||||
Index Cond: ((proname >= '00'::text) AND (proname < '01'::text))
|
||||
(6 rows)
|
||||
|
||||
select proname from pg_proc where proname ilike '00%foo' order by 1;
|
||||
proname
|
||||
---------
|
||||
(0 rows)
|
||||
|
||||
explain (costs off)
|
||||
select proname from pg_proc where proname ilike 'ri%foo' order by 1;
|
||||
QUERY PLAN
|
||||
-----------------------------------------------------------------
|
||||
Index Only Scan using pg_proc_proname_args_nsp_index on pg_proc
|
||||
Filter: (proname ~~* 'ri%foo'::text)
|
||||
(2 rows)
|
||||
|
||||
reset enable_seqscan;
|
||||
reset enable_indexscan;
|
||||
reset enable_bitmapscan;
|
||||
--
|
||||
-- Test B-tree page deletion. In particular, deleting a non-leaf page.
|
||||
--
|
||||
|
@ -3201,6 +3201,24 @@ explain (costs off)
|
||||
Index Cond: (b = false)
|
||||
(3 rows)
|
||||
|
||||
explain (costs off)
|
||||
select * from boolindex where b is true order by i desc limit 10;
|
||||
QUERY PLAN
|
||||
----------------------------------------------------------------
|
||||
Limit
|
||||
-> Index Scan Backward using boolindex_b_i_key on boolindex
|
||||
Index Cond: (b = true)
|
||||
(3 rows)
|
||||
|
||||
explain (costs off)
|
||||
select * from boolindex where b is false order by i desc limit 10;
|
||||
QUERY PLAN
|
||||
----------------------------------------------------------------
|
||||
Limit
|
||||
-> Index Scan Backward using boolindex_b_i_key on boolindex
|
||||
Index Cond: (b = false)
|
||||
(3 rows)
|
||||
|
||||
--
|
||||
-- Test for multilevel page deletion
|
||||
--
|
||||
|
@ -242,6 +242,15 @@ SELECT '' AS ten, set_masklen(inet(text(i)), 24) FROM INET_TBL;
|
||||
-- check that btree index works correctly
|
||||
CREATE INDEX inet_idx1 ON inet_tbl(i);
|
||||
SET enable_seqscan TO off;
|
||||
EXPLAIN (COSTS OFF)
|
||||
SELECT * FROM inet_tbl WHERE i<<'192.168.1.0/24'::cidr;
|
||||
QUERY PLAN
|
||||
-------------------------------------------------------------------------------
|
||||
Index Scan using inet_idx1 on inet_tbl
|
||||
Index Cond: ((i > '192.168.1.0/24'::inet) AND (i <= '192.168.1.255'::inet))
|
||||
Filter: (i << '192.168.1.0/24'::inet)
|
||||
(3 rows)
|
||||
|
||||
SELECT * FROM inet_tbl WHERE i<<'192.168.1.0/24'::cidr;
|
||||
c | i
|
||||
----------------+------------------
|
||||
@ -250,6 +259,15 @@ SELECT * FROM inet_tbl WHERE i<<'192.168.1.0/24'::cidr;
|
||||
192.168.1.0/26 | 192.168.1.226
|
||||
(3 rows)
|
||||
|
||||
EXPLAIN (COSTS OFF)
|
||||
SELECT * FROM inet_tbl WHERE i<<='192.168.1.0/24'::cidr;
|
||||
QUERY PLAN
|
||||
--------------------------------------------------------------------------------
|
||||
Index Scan using inet_idx1 on inet_tbl
|
||||
Index Cond: ((i >= '192.168.1.0/24'::inet) AND (i <= '192.168.1.255'::inet))
|
||||
Filter: (i <<= '192.168.1.0/24'::inet)
|
||||
(3 rows)
|
||||
|
||||
SELECT * FROM inet_tbl WHERE i<<='192.168.1.0/24'::cidr;
|
||||
c | i
|
||||
----------------+------------------
|
||||
@ -261,6 +279,43 @@ SELECT * FROM inet_tbl WHERE i<<='192.168.1.0/24'::cidr;
|
||||
192.168.1.0/26 | 192.168.1.226
|
||||
(6 rows)
|
||||
|
||||
EXPLAIN (COSTS OFF)
|
||||
SELECT * FROM inet_tbl WHERE '192.168.1.0/24'::cidr >>= i;
|
||||
QUERY PLAN
|
||||
--------------------------------------------------------------------------------
|
||||
Index Scan using inet_idx1 on inet_tbl
|
||||
Index Cond: ((i >= '192.168.1.0/24'::inet) AND (i <= '192.168.1.255'::inet))
|
||||
Filter: ('192.168.1.0/24'::inet >>= i)
|
||||
(3 rows)
|
||||
|
||||
SELECT * FROM inet_tbl WHERE '192.168.1.0/24'::cidr >>= i;
|
||||
c | i
|
||||
----------------+------------------
|
||||
192.168.1.0/24 | 192.168.1.0/24
|
||||
192.168.1.0/24 | 192.168.1.226/24
|
||||
192.168.1.0/24 | 192.168.1.255/24
|
||||
192.168.1.0/24 | 192.168.1.0/25
|
||||
192.168.1.0/24 | 192.168.1.255/25
|
||||
192.168.1.0/26 | 192.168.1.226
|
||||
(6 rows)
|
||||
|
||||
EXPLAIN (COSTS OFF)
|
||||
SELECT * FROM inet_tbl WHERE '192.168.1.0/24'::cidr >> i;
|
||||
QUERY PLAN
|
||||
-------------------------------------------------------------------------------
|
||||
Index Scan using inet_idx1 on inet_tbl
|
||||
Index Cond: ((i > '192.168.1.0/24'::inet) AND (i <= '192.168.1.255'::inet))
|
||||
Filter: ('192.168.1.0/24'::inet >> i)
|
||||
(3 rows)
|
||||
|
||||
SELECT * FROM inet_tbl WHERE '192.168.1.0/24'::cidr >> i;
|
||||
c | i
|
||||
----------------+------------------
|
||||
192.168.1.0/24 | 192.168.1.0/25
|
||||
192.168.1.0/24 | 192.168.1.255/25
|
||||
192.168.1.0/26 | 192.168.1.226
|
||||
(3 rows)
|
||||
|
||||
SET enable_seqscan TO on;
|
||||
DROP INDEX inet_idx1;
|
||||
-- check that gist index works correctly
|
||||
|
@ -294,6 +294,105 @@ order by thousand, tenthous;
|
||||
999 | 9999
|
||||
(25 rows)
|
||||
|
||||
explain (costs off)
|
||||
select thousand, tenthous, four from tenk1
|
||||
where (thousand, tenthous, four) > (998, 5000, 3)
|
||||
order by thousand, tenthous;
|
||||
QUERY PLAN
|
||||
-----------------------------------------------------------------------
|
||||
Sort
|
||||
Sort Key: thousand, tenthous
|
||||
-> Bitmap Heap Scan on tenk1
|
||||
Filter: (ROW(thousand, tenthous, four) > ROW(998, 5000, 3))
|
||||
-> Bitmap Index Scan on tenk1_thous_tenthous
|
||||
Index Cond: (ROW(thousand, tenthous) >= ROW(998, 5000))
|
||||
(6 rows)
|
||||
|
||||
select thousand, tenthous, four from tenk1
|
||||
where (thousand, tenthous, four) > (998, 5000, 3)
|
||||
order by thousand, tenthous;
|
||||
thousand | tenthous | four
|
||||
----------+----------+------
|
||||
998 | 5998 | 2
|
||||
998 | 6998 | 2
|
||||
998 | 7998 | 2
|
||||
998 | 8998 | 2
|
||||
998 | 9998 | 2
|
||||
999 | 999 | 3
|
||||
999 | 1999 | 3
|
||||
999 | 2999 | 3
|
||||
999 | 3999 | 3
|
||||
999 | 4999 | 3
|
||||
999 | 5999 | 3
|
||||
999 | 6999 | 3
|
||||
999 | 7999 | 3
|
||||
999 | 8999 | 3
|
||||
999 | 9999 | 3
|
||||
(15 rows)
|
||||
|
||||
explain (costs off)
|
||||
select thousand, tenthous from tenk1
|
||||
where (998, 5000) < (thousand, tenthous)
|
||||
order by thousand, tenthous;
|
||||
QUERY PLAN
|
||||
----------------------------------------------------------
|
||||
Index Only Scan using tenk1_thous_tenthous on tenk1
|
||||
Index Cond: (ROW(thousand, tenthous) > ROW(998, 5000))
|
||||
(2 rows)
|
||||
|
||||
select thousand, tenthous from tenk1
|
||||
where (998, 5000) < (thousand, tenthous)
|
||||
order by thousand, tenthous;
|
||||
thousand | tenthous
|
||||
----------+----------
|
||||
998 | 5998
|
||||
998 | 6998
|
||||
998 | 7998
|
||||
998 | 8998
|
||||
998 | 9998
|
||||
999 | 999
|
||||
999 | 1999
|
||||
999 | 2999
|
||||
999 | 3999
|
||||
999 | 4999
|
||||
999 | 5999
|
||||
999 | 6999
|
||||
999 | 7999
|
||||
999 | 8999
|
||||
999 | 9999
|
||||
(15 rows)
|
||||
|
||||
explain (costs off)
|
||||
select thousand, hundred from tenk1
|
||||
where (998, 5000) < (thousand, hundred)
|
||||
order by thousand, hundred;
|
||||
QUERY PLAN
|
||||
-----------------------------------------------------------
|
||||
Sort
|
||||
Sort Key: thousand, hundred
|
||||
-> Bitmap Heap Scan on tenk1
|
||||
Filter: (ROW(998, 5000) < ROW(thousand, hundred))
|
||||
-> Bitmap Index Scan on tenk1_thous_tenthous
|
||||
Index Cond: (thousand >= 998)
|
||||
(6 rows)
|
||||
|
||||
select thousand, hundred from tenk1
|
||||
where (998, 5000) < (thousand, hundred)
|
||||
order by thousand, hundred;
|
||||
thousand | hundred
|
||||
----------+---------
|
||||
999 | 99
|
||||
999 | 99
|
||||
999 | 99
|
||||
999 | 99
|
||||
999 | 99
|
||||
999 | 99
|
||||
999 | 99
|
||||
999 | 99
|
||||
999 | 99
|
||||
999 | 99
|
||||
(10 rows)
|
||||
|
||||
-- Test case for bug #14010: indexed row comparisons fail with nulls
|
||||
create temp table test_table (a text, b text);
|
||||
insert into test_table values ('a', 'b');
|
||||
|
@ -59,11 +59,29 @@ SELECT b.*
|
||||
set enable_seqscan to false;
|
||||
set enable_indexscan to true;
|
||||
set enable_bitmapscan to false;
|
||||
explain (costs off)
|
||||
select proname from pg_proc where proname like E'RI\\_FKey%del' order by 1;
|
||||
select proname from pg_proc where proname like E'RI\\_FKey%del' order by 1;
|
||||
explain (costs off)
|
||||
select proname from pg_proc where proname ilike '00%foo' order by 1;
|
||||
select proname from pg_proc where proname ilike '00%foo' order by 1;
|
||||
explain (costs off)
|
||||
select proname from pg_proc where proname ilike 'ri%foo' order by 1;
|
||||
|
||||
set enable_indexscan to false;
|
||||
set enable_bitmapscan to true;
|
||||
explain (costs off)
|
||||
select proname from pg_proc where proname like E'RI\\_FKey%del' order by 1;
|
||||
select proname from pg_proc where proname like E'RI\\_FKey%del' order by 1;
|
||||
explain (costs off)
|
||||
select proname from pg_proc where proname ilike '00%foo' order by 1;
|
||||
select proname from pg_proc where proname ilike '00%foo' order by 1;
|
||||
explain (costs off)
|
||||
select proname from pg_proc where proname ilike 'ri%foo' order by 1;
|
||||
|
||||
reset enable_seqscan;
|
||||
reset enable_indexscan;
|
||||
reset enable_bitmapscan;
|
||||
|
||||
--
|
||||
-- Test B-tree page deletion. In particular, deleting a non-leaf page.
|
||||
|
@ -1135,6 +1135,10 @@ explain (costs off)
|
||||
select * from boolindex where b = true order by i desc limit 10;
|
||||
explain (costs off)
|
||||
select * from boolindex where not b order by i limit 10;
|
||||
explain (costs off)
|
||||
select * from boolindex where b is true order by i desc limit 10;
|
||||
explain (costs off)
|
||||
select * from boolindex where b is false order by i desc limit 10;
|
||||
|
||||
--
|
||||
-- Test for multilevel page deletion
|
||||
|
@ -65,8 +65,18 @@ SELECT '' AS ten, set_masklen(inet(text(i)), 24) FROM INET_TBL;
|
||||
-- check that btree index works correctly
|
||||
CREATE INDEX inet_idx1 ON inet_tbl(i);
|
||||
SET enable_seqscan TO off;
|
||||
EXPLAIN (COSTS OFF)
|
||||
SELECT * FROM inet_tbl WHERE i<<'192.168.1.0/24'::cidr;
|
||||
SELECT * FROM inet_tbl WHERE i<<'192.168.1.0/24'::cidr;
|
||||
EXPLAIN (COSTS OFF)
|
||||
SELECT * FROM inet_tbl WHERE i<<='192.168.1.0/24'::cidr;
|
||||
SELECT * FROM inet_tbl WHERE i<<='192.168.1.0/24'::cidr;
|
||||
EXPLAIN (COSTS OFF)
|
||||
SELECT * FROM inet_tbl WHERE '192.168.1.0/24'::cidr >>= i;
|
||||
SELECT * FROM inet_tbl WHERE '192.168.1.0/24'::cidr >>= i;
|
||||
EXPLAIN (COSTS OFF)
|
||||
SELECT * FROM inet_tbl WHERE '192.168.1.0/24'::cidr >> i;
|
||||
SELECT * FROM inet_tbl WHERE '192.168.1.0/24'::cidr >> i;
|
||||
SET enable_seqscan TO on;
|
||||
DROP INDEX inet_idx1;
|
||||
|
||||
|
@ -119,6 +119,33 @@ select thousand, tenthous from tenk1
|
||||
where (thousand, tenthous) >= (997, 5000)
|
||||
order by thousand, tenthous;
|
||||
|
||||
explain (costs off)
|
||||
select thousand, tenthous, four from tenk1
|
||||
where (thousand, tenthous, four) > (998, 5000, 3)
|
||||
order by thousand, tenthous;
|
||||
|
||||
select thousand, tenthous, four from tenk1
|
||||
where (thousand, tenthous, four) > (998, 5000, 3)
|
||||
order by thousand, tenthous;
|
||||
|
||||
explain (costs off)
|
||||
select thousand, tenthous from tenk1
|
||||
where (998, 5000) < (thousand, tenthous)
|
||||
order by thousand, tenthous;
|
||||
|
||||
select thousand, tenthous from tenk1
|
||||
where (998, 5000) < (thousand, tenthous)
|
||||
order by thousand, tenthous;
|
||||
|
||||
explain (costs off)
|
||||
select thousand, hundred from tenk1
|
||||
where (998, 5000) < (thousand, hundred)
|
||||
order by thousand, hundred;
|
||||
|
||||
select thousand, hundred from tenk1
|
||||
where (998, 5000) < (thousand, hundred)
|
||||
order by thousand, hundred;
|
||||
|
||||
-- Test case for bug #14010: indexed row comparisons fail with nulls
|
||||
create temp table test_table (a text, b text);
|
||||
insert into test_table values ('a', 'b');
|
||||
|
Reference in New Issue
Block a user