Skip to content
 

KNN GIST Index in Postgresql

With version 9.1 of PostgreSQL and above, the KNN GIST Index is available.

There are two new operators defined:  <-> and <#> 

With version 2.0 of PostGIS and above, the KNN GIST Index functionality is exposed for PostGIS geometry types.

<-> - Returns the distance between two points. For point / point checks it uses floating point accuracy (as opposed to the double precision accuracy of the underlying point geometry). For other geometry types the distance between the floating point bounding box centroids is returned. Useful for doing distance ordering and nearest neighbor limits using KNN gist functionality.

<#> - Returns the distance between bounding box of 2 geometries. For point / point checks it's almost the same as distance (though may be different since the bounding box is at floating point accuracy and geometries are double precision). Useful for doing distance ordering and nearest neighbor limits using KNN gist functionality.

Let’s look at how they operators affect search results in a real-world example – proximity of parcels from a point on the street. Note that one of the geometries in this example is a very thin polygon just inside of a larger one against the street.
This first example shows bounding box ordered distance <#>