I know that index internally is B-tree or similar tree structure.
Suppose index is built for 3 columns (a,b,c)
, I want Postgres to:
- find key [a=10, b=20, c=30] in that B-tree,
- scan next 10 entries and return them.
If the index has only one column the solution is obvious:
select * from table1
where a >= 10
order by a limit 10
But if there are more columns the solution becomes much more complex. For 2 columns:
select * from table1
where a > 10 or (a = 10 and b >= 20)
order by a, b limit 10
3 columns:
select * from table1
where a > 10 or (a = 10 and (b > 20 or b = 20 and c >= 30))
order by a, b, c limit 10
Note that query:
select * from table1
where a >= 10 and b >= 20 and c >= 30
order by a, b, c limit 10
is incorrect, since it will filter out for example [a = 11, b = 10, c=1].
How can I tell Postgres that I want this operation?
And can I be sure that even for those complex queries for 2+ columns the optimizer will always understand that he should perform range-scan? Why?