Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XYQW6-0004Gc-Tu for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Sep 2014 02:16:47 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XYQW5-0005i6-CX for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Sep 2014 02:16:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XYQW3-0005hx-Js for pgsql-hackers@postgresql.org; Mon, 29 Sep 2014 02:16:43 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XYQVu-0000f7-Q1 for pgsql-hackers@postgresql.org; Mon, 29 Sep 2014 02:16:42 +0000 Received: from bruce by momjian.us with local (Exim 4.72) (envelope-from ) id 1XYQVr-0001DO-Bb; Sun, 28 Sep 2014 22:16:31 -0400 Date: Sun, 28 Sep 2014 22:16:31 -0400 From: Bruce Momjian To: Alexander Korotkov Cc: Emre Hasegeli , PostgreSQL Hackers , Heikki Linnakangas Subject: Re: KNN-GiST with recheck Message-ID: <20140929021631.GG12447@momjian.us> References: <20140803134809.GA58181@hasegeli-2.local> <20140914180936.GA5084@hasegeli.com> <20140926011800.GA8130@momjian.us> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) X-Pg-Spam-Score: -2.5 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org On Fri, Sep 26, 2014 at 10:49:42AM +0400, Alexander Korotkov wrote: > Does this also fix the identical PostGIS problem or is there something > PostGIS needs to do? > > > This patch provides general infrastructure for recheck in KNN-GiST. PostGIS > need corresponding change in its GiST opclass. Since PostGIS already define <-> > and <#> operators as distance to bounding box border and bounding box center, > it can't change their behaviour. > it has to support new operator "exact distance" in opclass.  Ah, OK, so they just need something that can be used for the recheck. I think they currently use ST_Distance() for that. Does it have to be an operator? If they defined an operator for ST_Distance(), would ST_Distance() work too for KNN-GiST? In summary, you still create a normal GiST index on the column: http://shisaa.jp/postset/postgis-postgresqls-spatial-partner-part-3.html CREATE INDEX planet_osm_line_ref_index ON planet_osm_line(ref); which indexes by the bounding box. The new code will allow ordered index hits to be filtered by something like ST_Distance(), rather than having to a LIMIT 50 in a CTE, then call ST_Distance(), like this: EXPLAIN ANALYZE WITH distance AS ( SELECT way AS road, ref AS route FROM planet_osm_line WHERE highway = 'secondary' ORDER BY ST_GeomFromText('POLYGON((14239931.42 3054117.72,14239990.49 3054224.25,14240230.15 3054091.38,14240171.08 3053984.84,14239931.42 3054117.72))', 900913) <#> way LIMIT 50 ) SELECT ST_Distance(ST_GeomFromText('POLYGON((14239931.42 3054117.72,14239990.49 3054224.25,14240230.15 3054091.38,14240171.08 3053984.84,14239931.42 3054117.72))', 900913), road) AS true_distance, route FROM distance ORDER BY true_distance LIMIT 1; Notice the CTE uses <#> (bounding box center), and then the outer query uses ST_Distance and LIMIT 1 to find the closest item. Excellent! -- Bruce Momjian http://momjian.us EnterpriseDB http://enterprisedb.com + Everyone has their own god. + -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers