Skip to content
Advertisement

Gist index in PostgreSQL only works on order, but not on where predicate

I’ve got a plain table with LatLon column containing a point with object location in space with GiST index created on this column:

I’ve got a query on selecting only points within some distance to the point with fixed coordinates:

Although explain on such query shows the index is not being used during execution:

At the same time, executing a canonical order by query against LatLon column shows the index works perfectly:

Why the GiST index doesn’t apply on where statement with distance operator <->?

Advertisement

Answer

Because someone implemented one, and not the other.

Note that this is not simply an operator, and that no other operator pairings (<-> and <, here) use indexes, either. So you would have to introduce the infrastructure to support such indexes, as well as this specific implementation.

If you just want the close thing with an index, you need to pull the distance into the point to get a circle. Then you have a nice binary operator for the index to grab onto:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement