saop_patch_test.sql

application/octet-stream

Filename: saop_patch_test.sql
Type: application/octet-stream
Part: 0
Message: Re: POC, WIP: OR-clause support for indexes
drop table if exists multi_test;
create table multi_test(a int, b int);
create index multi_test_idx on multi_test (a,b);
insert into multi_test select j, case when i < 14 then 0 else 1 end from generate_series(1,14) i, generate_series(1,400) j order by j,i;
vacuum analyze multi_test;

-- ┌───┬───────┬───────┬────────┬────────┬────────────┬───────┬───────┬───────────────────┬─────────┬───────────┬──────────────┐
-- │ i │ blkno │ flags │ nhtids │ nhblks │ ndeadhblks │ nlive │ ndead │ nhtidschecksimple │ avgsize │ freespace │   highkey    │
-- ├───┼───────┼───────┼────────┼────────┼────────────┼───────┼───────┼───────────────────┼─────────┼───────────┼──────────────┤
-- │ 1 │     1 │     1 │    854 │      4 │          0 │   123 │     0 │                 0 │      55 │       808 │ (a, b)=(62)  │
-- │ 2 │     2 │     1 │    854 │      5 │          0 │   123 │     0 │                 0 │      55 │       808 │ (a, b)=(123) │
-- │ 3 │     4 │     1 │    854 │      5 │          0 │   123 │     0 │                 0 │      55 │       808 │ (a, b)=(184) │
-- │ 4 │     5 │     1 │    854 │      5 │          0 │   123 │     0 │                 0 │      55 │       808 │ (a, b)=(245) │
-- │ 5 │     6 │     1 │    854 │      4 │          0 │   123 │     0 │                 0 │      55 │       808 │ (a, b)=(306) │
-- │ 6 │     7 │     1 │    854 │      5 │          0 │   123 │     0 │                 0 │      55 │       808 │ (a, b)=(367) │
-- │ 7 │     8 │     1 │    476 │      3 │          0 │    80 │     0 │                 0 │      49 │     3,908 │ ∅            │
-- └───┴───────┴───────┴────────┴────────┴────────────┴───────┴───────┴───────────────────┴─────────┴───────────┴──────────────┘

set enable_bitmapscan to on;
set enable_indexonlyscan to off;
set enable_indexscan to off;
set random_page_cost=0.1;

-- Should only need to scan root page (3) plus a single leaf page (4) exactly
-- once:
EXPLAIN (ANALYZE, COSTS OFF, BUFFERS, TIMING OFF, SUMMARY OFF)
select * from multi_test where a in (183) and b in (1,2,3,4,5,6,7,8,9,10,11,12);

-- On HEAD:
--┌───────────────────────────────────────────────────────────────────────────────────────────┐
--│                                        QUERY PLAN                                         │
--├───────────────────────────────────────────────────────────────────────────────────────────┤
--│ Bitmap Heap Scan on multi_test (actual rows=1 loops=1)                                    │
--│   Recheck Cond: ((a = 183) AND (b = ANY ('{1,2,3,4,5,6,7,8,9,10,11,12}'::integer[])))     │
--│   Heap Blocks: exact=1                                                                    │
--│   Buffers: shared hit=25                                                                  │
--│   ->  Bitmap Index Scan on multi_test_idx (actual rows=1 loops=1)                         │
--│         Index Cond: ((a = 183) AND (b = ANY ('{1,2,3,4,5,6,7,8,9,10,11,12}'::integer[]))) │
--│         Buffers: shared hit=24                                                            │
--└───────────────────────────────────────────────────────────────────────────────────────────┘
--(7 rows)

-- Patch:
--┌───────────────────────────────────────────────────────────────────────────────────────────┐
--│                                        QUERY PLAN                                         │
--├───────────────────────────────────────────────────────────────────────────────────────────┤
--│ Bitmap Heap Scan on multi_test (actual rows=1 loops=1)                                    │
--│   Recheck Cond: ((a = 183) AND (b = ANY ('{1,2,3,4,5,6,7,8,9,10,11,12}'::integer[])))     │
--│   Heap Blocks: exact=1                                                                    │
--│   Buffers: shared hit=3                                                                   │
--│   ->  Bitmap Index Scan on multi_test_idx (actual rows=1 loops=1)                         │
--│         Index Cond: ((a = 183) AND (b = ANY ('{1,2,3,4,5,6,7,8,9,10,11,12}'::integer[]))) │
--│         Buffers: shared hit=2                                                             │
--└───────────────────────────────────────────────────────────────────────────────────────────┘
--(7 rows)