Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 4300643CEBA for ; Thu, 19 Jan 2012 04:55:06 -0400 (AST) Received: from exprod7og109.obsmtp.com ([64.18.2.171]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rnnlt-0003Zk-9m for pgsql-bugs@postgresql.org; Thu, 19 Jan 2012 08:55:06 +0000 Received: from mail-ey0-f176.google.com ([209.85.215.176]) (using TLSv1) by exprod7ob109.postini.com ([64.18.6.12]) with SMTP ID DSNKTxfaVgkQ2KuMIXT/ZZcZ/E06j7qcf3q7@postini.com; Thu, 19 Jan 2012 00:55:01 PST Received: by eaak11 with SMTP id k11so141981eaa.21 for ; Thu, 19 Jan 2012 00:54:45 -0800 (PST) Received: by 10.213.5.14 with SMTP id 14mr3710726ebt.121.1326963285406; Thu, 19 Jan 2012 00:54:45 -0800 (PST) Received: from [192.168.1.101] (dsl-hkibrasgw2-ff6cc300-200.dhcp.inet.fi. [88.195.108.200]) by mx.google.com with ESMTPS id n17sm35602677eei.3.2012.01.19.00.54.43 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 19 Jan 2012 00:54:44 -0800 (PST) Message-ID: <4F17DA52.9080803@enterprisedb.com> Date: Thu, 19 Jan 2012 10:54:42 +0200 From: Heikki Linnakangas User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20120104 Icedove/8.0 MIME-Version: 1.0 To: YAMAMOTO Takashi CC: pgsql-bugs@postgresql.org, oleg@sai.msu.su, teodor@sigaev.ru Subject: Re: BUG #6399: knngist sometimes returns tuples in incorrect order References: <4F16D93F.7090907@enterprisedb.com> <20120119043517.6B48F14A158@mail.netbsd.org> In-Reply-To: <20120119043517.6B48F14A158@mail.netbsd.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -4.2 (----) X-Archive-Number: 201201/134 X-Sequence-Number: 32201 On 19.01.2012 06:35, YAMAMOTO Takashi wrote: >> On 18.01.2012 14:07, Heikki Linnakangas wrote: >>> For 9.2, I think we should change gist so >>> that the user-defined distance function can return any scalar data type, >>> not just float8 (as long as it has b-tree comparison operators). >> >> I took a shot at doing that. Patch attached. It needs some cleanup; I >> think we'll need to bump the version of the btree_gist extension, and I >> only changed the btree_int8 distance function to do that, even though it >> would now make a lot of sense to adjust the others, too. Also, I think >> it'll leak memory (or crash..) if the distance data type is pass-by-ref. >> >> If someone has a test suite at hand for performance testing this, that >> would be good. Now that I'm calling the b-tree comparison function for >> every comparison operation in the red-black tree, instead of a direct >> float8<= instruction, this could be quite a bit slower. That could be >> mitigated somewhat by using the sort-support stuff Tom committed recently. >> >> If we bend things a bit, this might be back-patchable to 9.1. We can't >> add a new am support function easily, that would require a catalog >> update to increment pg_am.amsupport entry, but we could hardwire the >> support for int8 distances, like I hardwired the knowledge of float8's >> comparsion function in the attached patch. > > thanks for taking a look quickly. > > i have some questions: > > doesn't gbt_int8_distance overflow? Yes, good point. > probably result += INT64_MIN so that it fits to int8 keeping the order? It can still overflow. The maximum distance between two int64s is (2^63 - 1) - (-2^63) = 2^64 - 1. That's the distance between the smallest possible int64 (-2^63) and the greatest one (2^63 - 1) . The int8_dist function, which implements the <-> operator, can also overflow, but it checks for it and throws an error: postgres=# SELECT 9223372036854775807 <-> -9223372036854775808; ERROR: bigint out of range We could add such an overflow check to gbt_int8_distance(), but I wonder if it would have some surprising effects. GiST might calculate distance to a value that's not in the final result set, so you'd get an error that you wouldn't get without the index. Then again, the alternative plan is to fetch the matching rows and sort, and the distance calculated for the sort will also overflow. Another solution for gbt_int8_distance() would be to treat the distance as an unsigned value, changing the comparison operation accordingly. I see that the distance functions for int4 and int2 can also overflow. The gist support function doesn't have the same problem with float8 representation, but I think it would be good idea to change int4_dist to return a bigint, so that it couldn't overflow (and int2_dist to return an int4). > isn't strategy number necessary to find out the sorting semantics for > the operator's sortfamily? (your patch doesn't change it, but...) Sorry, I didn't understand that. Can you elaborate? -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com