How PostGIS Finds Your Nearest Neighbor (Without Scanning Every Row)

Finding the nearest X is easy to ask for. Getting it right at scale is another matter. A lot of &ldquo;find the nearest X&rdquo; features (the coffee shop around the corner, the available driver closest to your pickup, the hospital within reach during an emergency) use a k-nearest neighbor (KNN) query under the hood. In PostGIS, that&rsquo;s the <-> distance operator. On a small table it works…