Posting this to elaborate a bit on what Zegarek already highlighted.
If we add included columns to the BTREE then BRIN can actually outperfom BTREE.
What was interesting is to compare how performance changes when we make those included columns wider. So I ran below test with the width 100, 200 and 500.
set max_parallel_workers_per_gather=0;
drop table if exists ttt0;
create table ttt0 as
select i::int, i::text || repeat('x', 100) x
from generate_series(1, 2e6::int) i;
create index btree_x on ttt0(i) include(x);
create index brin_x on ttt0 using brin(i);
analyse ttt0;
reset enable_seqscan;
reset enable_indexscan;
set enable_bitmapscan = off;
explain (analyze, buffers)
select count(x) from ttt0 where i < 4e5::int;
set enable_seqscan = off;
set enable_indexscan = off;
reset enable_bitmapscan;
explain (analyze, buffers)
select count(x) from ttt0 where i < 4e5::int;
JIT starts to kick in with the width 500 and it makes BRIN performance much worse. If we disable JIT then things are back to normal and BRIN continues to outperform BTREE.
-- !!!!!!!!!! 100
RESET
RESET
SET
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=42186.93..42186.94 rows=1 width=8) (actual time=131.530..131.531 rows=1 loops=1)
Buffers: shared hit=13679
-> Index Only Scan using btree_x on ttt0 (cost=0.43..41185.36 rows=400625 width=107) (actual time=0.020..106.956 rows=399999 loops=1)
Index Cond: (i < 400000)
Heap Fetches: 399999
Buffers: shared hit=13679
Planning:
Buffers: shared hit=1
Planning Time: 0.090 ms
Execution Time: 131.557 ms
(10 rows)
SET
SET
RESET
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=40703.95..40703.96 rows=1 width=8) (actual time=90.339..90.342 rows=1 loops=1)
Buffers: shared hit=6914
-> Bitmap Heap Scan on ttt0 (cost=113.92..39702.39 rows=400625 width=107) (actual time=0.185..60.036 rows=399999 loops=1)
Recheck Cond: (i < 400000)
Rows Removed by Index Recheck: 897
Heap Blocks: lossy=6912
Buffers: shared hit=6914
-> Bitmap Index Scan on brin_x (cost=0.00..13.76 rows=407398 width=0) (actual time=0.170..0.171 rows=69120 loops=1)
Index Cond: (i < 400000)
Buffers: shared hit=2
Planning:
Buffers: shared hit=1
Planning Time: 0.089 ms
Execution Time: 90.380 ms
(14 rows)
-- !!!!!!!!!! 200
RESET
RESET
SET
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=70148.37..70148.38 rows=1 width=8) (actual time=218.600..218.601 rows=1 loops=1)
Buffers: shared hit=1 read=12502
-> Index Only Scan using btree_x on ttt0 (cost=0.43..69150.62 rows=399097 width=210) (actual time=0.612..193.148 rows=399999 loops=1)
Index Cond: (i < 400000)
Heap Fetches: 0
Buffers: shared hit=1 read=12502
Planning:
Buffers: shared hit=1
Planning Time: 0.111 ms
Execution Time: 218.628 ms
(10 rows)
SET
SET
RESET
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=66734.94..66734.95 rows=1 width=8) (actual time=143.774..143.775 rows=1 loops=1)
Buffers: shared hit=1 read=12162 written=5
-> Bitmap Heap Scan on ttt0 (cost=118.81..65737.20 rows=399097 width=210) (actual time=1.072..112.776 rows=399999 loops=1)
Recheck Cond: (i < 400000)
Rows Removed by Index Recheck: 1281
Heap Blocks: lossy=12160
Buffers: shared hit=1 read=12162 written=5
-> Bitmap Index Scan on brin_x (cost=0.00..19.04 rows=400831 width=0) (actual time=0.761..0.761 rows=121600 loops=1)
Index Cond: (i < 400000)
Buffers: shared hit=1 read=2
Planning:
Buffers: shared hit=1
Planning Time: 0.080 ms
Execution Time: 143.807 ms
(14 rows)
-- !!!!!!!!!! 500
RESET
RESET
SET
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=102648.83..102648.84 rows=1 width=8) (actual time=628.547..628.549 rows=1 loops=1)
Buffers: shared read=30774 written=3
-> Index Only Scan using btree_x on ttt0 (cost=0.55..101653.92 rows=397964 width=510) (actual time=1.454..595.347 rows=399999 loops=1)
Index Cond: (i < 400000)
Heap Fetches: 0
Buffers: shared read=30774 written=3
Planning:
Buffers: shared hit=1
Planning Time: 0.116 ms
JIT:
Functions: 3
Options: Inlining false, Optimization false, Expressions true, Deforming true
Timing: Generation 0.229 ms (Deform 0.000 ms), Inlining 0.000 ms, Optimization 0.252 ms, Emission 2.779 ms, Total 3.260 ms
Execution Time: 628.854 ms
(14 rows)
SET
SET
RESET
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=164379.97..164379.98 rows=1 width=8) (actual time=1080.068..1080.069 rows=1 loops=1)
Buffers: shared read=28548 written=2
-> Bitmap Heap Scan on ttt0 (cost=126.63..163385.06 rows=397964 width=510) (actual time=5.875..1045.626 rows=399999 loops=1)
Recheck Cond: (i < 400000)
Rows Removed by Index Recheck: 284
Heap Blocks: lossy=28544
Buffers: shared read=28548 written=2
-> Bitmap Index Scan on brin_x (cost=0.00..27.14 rows=399627 width=0) (actual time=1.043..1.044 rows=285440 loops=1)
Index Cond: (i < 400000)
Buffers: shared read=4
Planning:
Buffers: shared read=1
Planning Time: 0.108 ms
JIT:
Functions: 5
Options: Inlining false, Optimization false, Expressions true, Deforming true
Timing: Generation 0.360 ms (Deform 0.110 ms), Inlining 0.000 ms, Optimization 0.342 ms, Emission 4.077 ms, Total 4.778 ms
Execution Time: 1080.523 ms
(18 rows)
-- !!!!!!!!!! 500
-- set jit=off;
RESET
RESET
SET
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=103063.75..103063.76 rows=1 width=8) (actual time=414.979..414.980 rows=1 loops=1)
Buffers: shared read=59297
-> Index Only Scan using btree_x on ttt0 (cost=0.55..102064.73 rows=399610 width=510) (actual time=7.033..386.442 rows=399999 loops=1)
Index Cond: (i < 400000)
Heap Fetches: 399999
Buffers: shared read=59297
Planning:
Buffers: shared hit=1
Planning Time: 0.093 ms
Execution Time: 415.006 ms
(10 rows)
SET
SET
RESET
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=164386.88..164386.89 rows=1 width=8) (actual time=217.692..217.693 rows=1 loops=1)
Buffers: shared read=28548
-> Bitmap Heap Scan on ttt0 (cost=127.04..163387.85 rows=399610 width=510) (actual time=0.778..186.223 rows=399999 loops=1)
Recheck Cond: (i < 400000)
Rows Removed by Index Recheck: 284
Heap Blocks: lossy=28544
Buffers: shared read=28548
-> Bitmap Index Scan on brin_x (cost=0.00..27.14 rows=399674 width=0) (actual time=0.748..0.748 rows=285440 loops=1)
Index Cond: (i < 400000)
Buffers: shared read=4
Planning:
Buffers: shared read=1
Planning Time: 0.087 ms
Execution Time: 217.725 ms
(14 rows)