Re: Incorrect behaviour when using a GiST index on points

Lists: pgsql-hackers
From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Incorrect behaviour when using a GiST index on points
Date: 2012-02-16 07:43:11
Message-ID: CAPpHfdvGticGniaj88VCHzHboXJobUhjLm6c09q_Op_u9EoBFg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi!

Oleg and me found an incorrect behaviour when using a GiST index on points.
It's quite easy to reproduce.

test=# create table test as values (0,0.0000001);
test=# insert into test values (0,0.0000001);
test=# select * from test where point(x,y) <@ box(point(0,0),point(0,0));
x | y
---+---
(0 rows)

test=# create index gist_test on test using gist (point(x,y));
test=# set enable_seqscan = off;
test=# select * from test where point(x,y) <@ box(point(0,0),point(0,0));
x | y
---+-------
0 | 1e-07
(1 row)

So, "point <@ box" operator don't thinks (0, 1e-07) to be equal to (0, 0),
while GiST index does. "point <@ box" operator uses on_ob function which
uses comparison operators of float numbers.

Datum
on_pb(PG_FUNCTION_ARGS)
{
Point *pt = PG_GETARG_POINT_P(0);
BOX *box = PG_GETARG_BOX_P(1);

PG_RETURN_BOOL(pt->x <= box->high.x && pt->x >= box->low.x &&
pt->y <= box->high.y && pt->y >= box->low.y);
}

GiST consistent method uses box_contained function, which uses FP* macros
for float comparison.

/* box_contained - is box1 contained by box2?
*/
Datum
box_contained(PG_FUNCTION_ARGS)
{
BOX *box1 = PG_GETARG_BOX_P(0);
BOX *box2 = PG_GETARG_BOX_P(1);

PG_RETURN_BOOL(FPle(box1->high.x, box2->high.x) &&
FPge(box1->low.x, box2->low.x) &&
FPle(box1->high.y, box2->high.y) &&
FPge(box1->low.y, box2->low.y));
}

Correspondingly, FP* compares numbers with some error.

#define EPSILON 1.0E-06

#ifdef EPSILON
#define FPzero(A) (fabs(A) <= EPSILON)
#define FPeq(A,B) (fabs((A) - (B)) <= EPSILON)
#define FPne(A,B) (fabs((A) - (B)) > EPSILON)
#define FPlt(A,B) ((B) - (A) > EPSILON)
#define FPle(A,B) ((A) - (B) <= EPSILON)
#define FPgt(A,B) ((A) - (B) > EPSILON)
#define FPge(A,B) ((B) - (A) <= EPSILON)
#else
#define FPzero(A) ((A) == 0)
#define FPeq(A,B) ((A) == (B))
#define FPne(A,B) ((A) != (B))
#define FPlt(A,B) ((A) < (B))
#define FPle(A,B) ((A) <= (B))
#define FPgt(A,B) ((A) > (B))
#define FPge(A,B) ((A) >= (B))
#endif

Described differences leads to incorrect behaviour of GiST index.
The question is: what is correct way to fix it? Should on_pb also use FP*
or consistent method should behave like on_pb?

------
With best regards,
Alexander Korotkov.


From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-02-20 08:44:29
Message-ID: CAPpHfdsyZYU7fV-Yp8uWbrOnxXL954rUA3Z3VqjoP=DyoqJdzg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Feb 16, 2012 at 11:43 AM, Alexander Korotkov
<aekorotkov(at)gmail(dot)com>wrote:

> Described differences leads to incorrect behaviour of GiST index.
> The question is: what is correct way to fix it? Should on_pb also use FP*
> or consistent method should behave like on_pb?
>

Any comments on this? Current behaviour definitely indicates a bug, and I'm
ready to fix it. The only question: is this bug in on_pb or gist?

------
With best regards,
Alexander Korotkov.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-02-20 15:22:50
Message-ID: 18849.1329751370@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Alexander Korotkov <aekorotkov(at)gmail(dot)com> writes:
> On Thu, Feb 16, 2012 at 11:43 AM, Alexander Korotkov
> <aekorotkov(at)gmail(dot)com>wrote:
>> Described differences leads to incorrect behaviour of GiST index.
>> The question is: what is correct way to fix it? Should on_pb also use FP*
>> or consistent method should behave like on_pb?

> Any comments on this? Current behaviour definitely indicates a bug, and I'm
> ready to fix it. The only question: is this bug in on_pb or gist?

I'm inclined to think the right answer is to make on_pb use the FP*
macros, for consistency with other geometric operators. But it's worth
asking whether that will actually fix the problem. I've thought for
some time that we'd eventually find cases where geo_ops' use of fuzzy
comparisons breaks index behavior entirely, because it destroys natural
assumptions like the transitive law. So that could eventually lead us
to rip out the FP* macros everywhere.

In any case, this doesn't seem like something we could back-patch;
it'd be a behavioral change in HEAD only.

regards, tom lane


From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-02-20 17:34:21
Message-ID: CAPpHfds-sGWBaRaaPB7HpjUYTV0TgkN2i+3O0oe8QqyWSR1CQA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, Feb 20, 2012 at 7:22 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> Alexander Korotkov <aekorotkov(at)gmail(dot)com> writes:
> > On Thu, Feb 16, 2012 at 11:43 AM, Alexander Korotkov
> > <aekorotkov(at)gmail(dot)com>wrote:
> >> Described differences leads to incorrect behaviour of GiST index.
> >> The question is: what is correct way to fix it? Should on_pb also use
> FP*
> >> or consistent method should behave like on_pb?
>
> > Any comments on this? Current behaviour definitely indicates a bug, and
> I'm
> > ready to fix it. The only question: is this bug in on_pb or gist?
>
> I'm inclined to think the right answer is to make on_pb use the FP*
> macros, for consistency with other geometric operators. But it's worth
> asking whether that will actually fix the problem. I've thought for
> some time that we'd eventually find cases where geo_ops' use of fuzzy
> comparisons breaks index behavior entirely, because it destroys natural
> assumptions like the transitive law. So that could eventually lead us
> to rip out the FP* macros everywhere.
>
> In any case, this doesn't seem like something we could back-patch;
> it'd be a behavioral change in HEAD only.
>

Analyzing GiST interface methods more detail I found one other place where
fuzzy comparison was used. It is gist_box_same function. And it's really
scary thing. It means that entry of internal page is not extended when
inserting index tuple extends it in less than EPSILON. Correspondingly
current GiST search behaviour is neither exact search or fuzzy search with
given EPSILON. It is something in the middle. See following example for
proof.

test=# create table test(a box);
CREATE TABLE
test=# insert into test (select (case when i%2= 0 then
box(point(1,1),point(1,1)) else box(point(2,2),point(2,2)) end) from
generate_series(1,300) i);
INSERT 0 300
test=# create index test_idx on test using gist(a);
CREATE INDEX
test=#
test=# insert into test values (box(point(1.0000009,1.0000009),
point(1.0000009,1.0000009)));
INSERT 0 1
test=# select * from test where a && box(point(1.0000018,1.0000018),
point(1.0000018,1.0000018));
a
---------------------------------------------
(1.0000009,1.0000009),(1.0000009,1.0000009)
(1 row)

test=# set enable_seqscan = off;
SET
test=# select * from test where a && box(point(1.0000018,1.0000018),
point(1.0000018,1.0000018));
a
---
(0 rows)

But, I believe we still can bachpatch it in one of following ways:
1) Fix consistent and same functions. Add to release note notice that users
should rebuild indexes if they want correct behaviour.
2) Leave same function as is. Add kluges to consistent functions which
provides correct search on current not truly correct trees.

But anyway +1 for rip out the FP* macros everywhere in HEAD. Because I
really dislike the way FP* are currently implemented. Why EPSILON
is 1.0E-06? We don't know anything about nature of data that users store in
geometrical datatypes. The lesser double precision value is 5E-324. For
some datasets EPSILON can easily cover whole range.

------
With bestregards,
Alexander Korotkov.


From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-02-22 13:56:03
Message-ID: CAPpHfdubmSkB_4HvWPfEGBcBe4fMCdo-9XZbSbLWGhC+r6rPSw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Attached patch fixes GiST behaviour without altering operators behaviour.

------
With best regards,
Alexander Korotkov.

Attachment Content-Type Size
gistproc_fix.patch text/x-patch 2.4 KB

From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-03-12 11:50:33
Message-ID: CAPpHfdsgk+N=kf+HUn7CUtg6ThNQcKH6q3VZZwUzsB3zpjpRfA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

I believe that attached version of patch can be backpatched. It fixes this
problem without altering of index building procedure. It just makes checks
in internal pages softener enough to compensate effect of gist_box_same
implementation.

------
With best regards,
Alexander Korotkov.

Attachment Content-Type Size
gistproc_backpatch.patch text/x-patch 6.7 KB

From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-04-09 08:52:12
Message-ID: CAPpHfdvJKy0oDywXvz5M-c3ZHpebELbHMJ8VRzZtOBZFGGpUYQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, Mar 12, 2012 at 3:50 PM, Alexander Korotkov <aekorotkov(at)gmail(dot)com>wrote:

> I believe that attached version of patch can be backpatched. It fixes this
> problem without altering of index building procedure. It just makes checks
> in internal pages softener enough to compensate effect of gist_box_same
> implementation.
>

Any comments about this?

------
With best regards,
Alexander Korotkov.


From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-06-21 18:53:43
Message-ID: CAPpHfdvbkS6DAbM9uOJLUY6dNVZpunLTF8G1Yoj5ruKiVMPXcg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Feb 22, 2012 at 5:56 PM, Alexander Korotkov <aekorotkov(at)gmail(dot)com>wrote:

> Attached patch fixes GiST behaviour without altering operators behaviour.
>

I think we definitely should apply this patch before 9.2 release, because
it is a bug fix. Otherwise people will continue produce incorrect GiST
indexes with in-core geometrical opclasses until 9.3. Patch is very simple
and only changes few lines of code.

Any thoughts?

------
With best regards,
Alexander Korotkov.

Attachment Content-Type Size
gistproc_fix.patch application/octet-stream 2.4 KB

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-07-03 15:28:50
Message-ID: CA+TgmoafwqpO1_iNDLmUdn-j_HeunF_kzWy_1ricW7DoNjEmaA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Jun 21, 2012 at 2:53 PM, Alexander Korotkov
<aekorotkov(at)gmail(dot)com> wrote:
> On Wed, Feb 22, 2012 at 5:56 PM, Alexander Korotkov <aekorotkov(at)gmail(dot)com>
> wrote:
>>
>> Attached patch fixes GiST behaviour without altering operators behaviour.
>
>
> I think we definitely should apply this patch before 9.2 release, because it
> is a bug fix. Otherwise people will continue produce incorrect GiST indexes
> with in-core geometrical opclasses until 9.3. Patch is very simple and only
> changes few lines of code.
>
> Any thoughts?

Do we need to apply this patch to 9.2?

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Alexander Korotkov <aekorotkov(at)gmail(dot)com>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-07-03 15:34:24
Message-ID: 7948.1341329664@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> On Thu, Jun 21, 2012 at 2:53 PM, Alexander Korotkov
> <aekorotkov(at)gmail(dot)com> wrote:
>> I think we definitely should apply this patch before 9.2 release, because it
>> is a bug fix. Otherwise people will continue produce incorrect GiST indexes
>> with in-core geometrical opclasses until 9.3. Patch is very simple and only
>> changes few lines of code.
>>
>> Any thoughts?

> Do we need to apply this patch to 9.2?

It's been like that all along, no? I'm feeling hesitant to shove it
into 9.2 at this late date. But we should review it and get it into
9.3 early if possible.

regards, tom lane


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Alexander Korotkov <aekorotkov(at)gmail(dot)com>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-07-03 15:44:13
Message-ID: CA+Tgmoa-3s3YpeR8VhAPu1CeoWJKEsQeQA-HZ=F3yPxQMpsYMg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Jul 3, 2012 at 11:34 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>> On Thu, Jun 21, 2012 at 2:53 PM, Alexander Korotkov
>> <aekorotkov(at)gmail(dot)com> wrote:
>>> I think we definitely should apply this patch before 9.2 release, because it
>>> is a bug fix. Otherwise people will continue produce incorrect GiST indexes
>>> with in-core geometrical opclasses until 9.3. Patch is very simple and only
>>> changes few lines of code.
>>>
>>> Any thoughts?
>
>> Do we need to apply this patch to 9.2?
>
> It's been like that all along, no?

Yeah, but it seems an awful lot like a bug. In fact... it's hard to
imagine how it could be any more of a bug than this.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Oleg Bartunov <obartunov(at)gmail(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Alexander Korotkov <aekorotkov(at)gmail(dot)com>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-07-03 15:48:41
Message-ID: CAF4Au4yhNu_HCFAP=57ur_frdTUMD3tKH_gF3T79RXBpDJgEVA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Yes, it's a bug and it needs to be applied !

On Tue, Jul 3, 2012 at 7:44 PM, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
> On Tue, Jul 3, 2012 at 11:34 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>>> On Thu, Jun 21, 2012 at 2:53 PM, Alexander Korotkov
>>> <aekorotkov(at)gmail(dot)com> wrote:
>>>> I think we definitely should apply this patch before 9.2 release, because it
>>>> is a bug fix. Otherwise people will continue produce incorrect GiST indexes
>>>> with in-core geometrical opclasses until 9.3. Patch is very simple and only
>>>> changes few lines of code.
>>>>
>>>> Any thoughts?
>>
>>> Do we need to apply this patch to 9.2?
>>
>> It's been like that all along, no?
>
> Yeah, but it seems an awful lot like a bug.  In fact... it's hard to
> imagine how it could be any more of a bug than this.
>
> --
> Robert Haas
> EnterpriseDB: http://www.enterprisedb.com
> The Enterprise PostgreSQL Company


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: obartunov(at)gmail(dot)com
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Alexander Korotkov <aekorotkov(at)gmail(dot)com>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-07-03 15:58:01
Message-ID: 8549.1341331081@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Oleg Bartunov <obartunov(at)gmail(dot)com> writes:
> Yes, it's a bug and it needs to be applied !

Well, it needs to be *reviewed* first, and nobody's done that ...

regards, tom lane


From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: obartunov(at)gmail(dot)com, Robert Haas <robertmhaas(at)gmail(dot)com>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-07-03 21:46:26
Message-ID: CAPpHfdt2FZ=DBd_OX11Pa0Rk=cC34w_q3W_OB8p_ERkAsqDK=Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Jul 3, 2012 at 7:58 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> Oleg Bartunov <obartunov(at)gmail(dot)com> writes:
> > Yes, it's a bug and it needs to be applied !
>
> Well, it needs to be *reviewed* first, and nobody's done that ...
>

I've discussed it with Teodor privately and he has verified by thoughts. I
think if he'll verify it in this thread, it should be enough for review of
few lines bugfix.

If we say about bugfix I provided for backpatch, it need more detailed
review. I was a miss that I didn't add it to current commitfest, will add
it to the next one.

------
With best regards,
Alexander Korotkov.


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-08-27 22:43:56
Message-ID: 20120827224356.GB2487@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


I need someone to review this patch for 9.3. We have already missed
fixing this for 9.2.

---------------------------------------------------------------------------

On Thu, Jun 21, 2012 at 10:53:43PM +0400, Alexander Korotkov wrote:
> On Wed, Feb 22, 2012 at 5:56 PM, Alexander Korotkov <aekorotkov(at)gmail(dot)com>
> wrote:
>
> Attached patch fixes GiST behaviour without altering operators behaviour.
>
>
> I think we definitely should apply this patch before 9.2 release, because it is
> a bug fix. Otherwise people will continue produce incorrect GiST indexes with
> in-core geometrical opclasses until 9.3. Patch is very simple and only changes
> few lines of code.
>
> Any thoughts?
>
> ------
> With best regards,
> Alexander Korotkov.

> *** a/src/backend/access/gist/gistproc.c
> --- b/src/backend/access/gist/gistproc.c
> ***************
> *** 836,842 **** gist_box_picksplit(PG_FUNCTION_ARGS)
> }
>
> /*
> ! * Equality method
> *
> * This is used for both boxes and points.
> */
> --- 836,843 ----
> }
>
> /*
> ! * Equality method. Returns true only when boxes are exact same. We can't
> ! * ignore small extents because of index consistency.
> *
> * This is used for both boxes and points.
> */
> ***************
> *** 848,856 **** gist_box_same(PG_FUNCTION_ARGS)
> bool *result = (bool *) PG_GETARG_POINTER(2);
>
> if (b1 && b2)
> ! *result = DatumGetBool(DirectFunctionCall2(box_same,
> ! PointerGetDatum(b1),
> ! PointerGetDatum(b2)));
> else
> *result = (b1 == NULL && b2 == NULL) ? TRUE : FALSE;
> PG_RETURN_POINTER(result);
> --- 849,857 ----
> bool *result = (bool *) PG_GETARG_POINTER(2);
>
> if (b1 && b2)
> ! *result = (b1->low.x == b2->low.x && b1->low.y == b2->low.y &&
> ! b1->high.x == b2->high.x && b1->high.y == b2->high.y)
> ! ? TRUE : FALSE;
> else
> *result = (b1 == NULL && b2 == NULL) ? TRUE : FALSE;
> PG_RETURN_POINTER(result);
> ***************
> *** 1326,1331 **** gist_point_consistent(PG_FUNCTION_ARGS)
> --- 1327,1333 ----
> bool *recheck = (bool *) PG_GETARG_POINTER(4);
> bool result;
> StrategyNumber strategyGroup = strategy / GeoStrategyNumberOffset;
> + BOX *query, *key;
>
> switch (strategyGroup)
> {
> ***************
> *** 1337,1348 **** gist_point_consistent(PG_FUNCTION_ARGS)
> *recheck = false;
> break;
> case BoxStrategyNumberGroup:
> ! result = DatumGetBool(DirectFunctionCall5(
> ! gist_box_consistent,
> ! PointerGetDatum(entry),
> ! PG_GETARG_DATUM(1),
> ! Int16GetDatum(RTOverlapStrategyNumber),
> ! 0, PointerGetDatum(recheck)));
> break;
> case PolygonStrategyNumberGroup:
> {
> --- 1339,1356 ----
> *recheck = false;
> break;
> case BoxStrategyNumberGroup:
> ! /*
> ! * This code repeats logic of on_ob which uses simple comparison
> ! * rather than FP* functions.
> ! */
> ! query = PG_GETARG_BOX_P(1);
> ! key = DatumGetBoxP(entry->key);
> !
> ! *recheck = false;
> ! result = key->high.x >= query->low.x &&
> ! key->low.x <= query->high.x &&
> ! key->high.y >= query->low.y &&
> ! key->low.y <= query->high.y;
> break;
> case PolygonStrategyNumberGroup:
> {

>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ It's impossible for everything to be true. +


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: Alexander Korotkov <aekorotkov(at)gmail(dot)com>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-08-27 23:43:49
Message-ID: 12549.1346111029@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Bruce Momjian <bruce(at)momjian(dot)us> writes:
> I need someone to review this patch for 9.3. We have already missed
> fixing this for 9.2.

So put it in the next commitfest.

FWIW, I looked at this last week, and concluded I didn't have enough
confidence in it to push it into 9.2 at the last minute.

There's also the big-picture question of whether we should just get rid
of fuzzy comparisons in the geometric types instead of trying to hack
indexes to work around them. I'd really rather have us debate that
question and resolve it one way or the other before spending time on the
details of patches that take the second approach.

regards, tom lane


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Alexander Korotkov <aekorotkov(at)gmail(dot)com>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-08-28 02:20:29
Message-ID: 20120828022029.GD6786@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, Aug 27, 2012 at 07:43:49PM -0400, Tom Lane wrote:
> Bruce Momjian <bruce(at)momjian(dot)us> writes:
> > I need someone to review this patch for 9.3. We have already missed
> > fixing this for 9.2.
>
> So put it in the next commitfest.
>
> FWIW, I looked at this last week, and concluded I didn't have enough
> confidence in it to push it into 9.2 at the last minute.
>
> There's also the big-picture question of whether we should just get rid
> of fuzzy comparisons in the geometric types instead of trying to hack
> indexes to work around them. I'd really rather have us debate that
> question and resolve it one way or the other before spending time on the
> details of patches that take the second approach.

Done.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ It's impossible for everything to be true. +


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Alexander Korotkov <aekorotkov(at)gmail(dot)com>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-08-28 02:21:27
Message-ID: 20120828022127.GE6786@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, Aug 27, 2012 at 07:43:49PM -0400, Tom Lane wrote:
> Bruce Momjian <bruce(at)momjian(dot)us> writes:
> > I need someone to review this patch for 9.3. We have already missed
> > fixing this for 9.2.
>
> So put it in the next commitfest.

Done. I have linked to your comment below too.

---------------------------------------------------------------------------

> FWIW, I looked at this last week, and concluded I didn't have enough
> confidence in it to push it into 9.2 at the last minute.
>
> There's also the big-picture question of whether we should just get rid
> of fuzzy comparisons in the geometric types instead of trying to hack
> indexes to work around them. I'd really rather have us debate that
> question and resolve it one way or the other before spending time on the
> details of patches that take the second approach.
>
> regards, tom lane

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ It's impossible for everything to be true. +


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Bruce Momjian <bruce(at)momjian(dot)us>, Alexander Korotkov <aekorotkov(at)gmail(dot)com>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-08-28 20:38:36
Message-ID: CA+TgmoauSzBBbzzW3XOUkS9hcuyG2XcBhOatLsSnMtCoDt+p3Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, Aug 27, 2012 at 7:43 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> There's also the big-picture question of whether we should just get rid
> of fuzzy comparisons in the geometric types instead of trying to hack
> indexes to work around them.

+1 for that approach, but only if I don't have to do the work.
Otherwise, +1 for doing the simplest thing that we're sure will
eliminate wrong answers.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Bruce Momjian <bruce(at)momjian(dot)us>, Alexander Korotkov <aekorotkov(at)gmail(dot)com>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-08-28 21:04:09
Message-ID: 9804.1346187849@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> On Mon, Aug 27, 2012 at 7:43 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> There's also the big-picture question of whether we should just get rid
>> of fuzzy comparisons in the geometric types instead of trying to hack
>> indexes to work around them.

> +1 for that approach, but only if I don't have to do the work.
> Otherwise, +1 for doing the simplest thing that we're sure will
> eliminate wrong answers.

What we're forced to speculate about here is how many applications out
there are relying on fuzzy comparison to get answers they like, versus
how many are getting answers they don't like because of it. The fact
that the underlying storage is float8 not numeric suggests there are
probably some cases where fuzzy is helpful.

Another issue here is that even if we agree that simple comparisons
(operator complexity up to about the limit of what an index might
support) should be exact, there's something to be said for fuzzy
computations for operators like whether a point falls on a line.
Internal roundoff error makes that problematic even if you assume
that the inputs are exact.

I've never cared for the particulars of the way the fuzzy comparisons
are done, in any case: using an absolute rather than relative error
threshold is wrong according to every numerical analysis principle
I know.

The long and the short of it is that it will probably take a significant
investment of work to make something that's clearly better. If that
weren't the case, we'd have done something long ago.

regards, tom lane


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Bruce Momjian <bruce(at)momjian(dot)us>, Alexander Korotkov <aekorotkov(at)gmail(dot)com>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-08-29 03:08:45
Message-ID: CA+TgmoaNRU8dWwkaFrhuJMyMfgFD+asVZ2e9fuDdf9wJiR=c9g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Aug 28, 2012 at 5:04 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>> On Mon, Aug 27, 2012 at 7:43 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>>> There's also the big-picture question of whether we should just get rid
>>> of fuzzy comparisons in the geometric types instead of trying to hack
>>> indexes to work around them.
>
>> +1 for that approach, but only if I don't have to do the work.
>> Otherwise, +1 for doing the simplest thing that we're sure will
>> eliminate wrong answers.
>
> What we're forced to speculate about here is how many applications out
> there are relying on fuzzy comparison to get answers they like, versus
> how many are getting answers they don't like because of it. The fact
> that the underlying storage is float8 not numeric suggests there are
> probably some cases where fuzzy is helpful.

I figured it mostly ended up that way because most of the geometic
datatypes are built on top of float8s, and most of the GiST distance
metrics are therefore a float8 distance. But I must be confused,
because surely we don't need to remove the option to express the
penalty as a float8, only the prohibition on using anything else. In
which case this next part seems like a non-issue:

> Another issue here is that even if we agree that simple comparisons
> (operator complexity up to about the limit of what an index might
> support) should be exact, there's something to be said for fuzzy
> computations for operators like whether a point falls on a line.
> Internal roundoff error makes that problematic even if you assume
> that the inputs are exact.

> I've never cared for the particulars of the way the fuzzy comparisons
> are done, in any case: using an absolute rather than relative error
> threshold is wrong according to every numerical analysis principle
> I know.

Yeah, that seemed odd to me, too.

> The long and the short of it is that it will probably take a significant
> investment of work to make something that's clearly better. If that
> weren't the case, we'd have done something long ago.

Perhaps, but this patch has been kicking around for 7 months without
any on-list review, so there might also be a lack of interest in
fixing the problem. :-(

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Noah Misch <noah(at)leadboat(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Bruce Momjian <bruce(at)momjian(dot)us>, Alexander Korotkov <aekorotkov(at)gmail(dot)com>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-10-02 17:58:40
Message-ID: 20121002175840.GB7382@tornado.leadboat.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Aug 28, 2012 at 05:04:09PM -0400, Tom Lane wrote:
> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> > On Mon, Aug 27, 2012 at 7:43 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> >> There's also the big-picture question of whether we should just get rid
> >> of fuzzy comparisons in the geometric types instead of trying to hack
> >> indexes to work around them.
>
> > +1 for that approach, but only if I don't have to do the work.

I agree in the abstract; why should a point (position on a 2D plane) compare
fuzzily while a float8 (position on a 1D number line) does not? But ...

> > Otherwise, +1 for doing the simplest thing that we're sure will
> > eliminate wrong answers.
>
> What we're forced to speculate about here is how many applications out
> there are relying on fuzzy comparison to get answers they like, versus
> how many are getting answers they don't like because of it. The fact
> that the underlying storage is float8 not numeric suggests there are
> probably some cases where fuzzy is helpful.

... yes. Having never used these types in practice, I won't venture a guess.
Anyone else?

> I've never cared for the particulars of the way the fuzzy comparisons
> are done, in any case: using an absolute rather than relative error
> threshold is wrong according to every numerical analysis principle
> I know.

Definitely.

> The long and the short of it is that it will probably take a significant
> investment of work to make something that's clearly better. If that
> weren't the case, we'd have done something long ago.

In any event, I think we should entertain a patch to make the GiST operator
class methods bug-compatible with corresponding operators. Even if we decide
to change operator behavior in HEAD, the back branches could use it.

Thanks,
nm


From: Noah Misch <noah(at)leadboat(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Bruce Momjian <bruce(at)momjian(dot)us>, Alexander Korotkov <aekorotkov(at)gmail(dot)com>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-10-11 11:17:28
Message-ID: 20121011111728.GE29677@tornado.leadboat.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Oct 02, 2012 at 01:58:40PM -0400, Noah Misch wrote:
> > > On Mon, Aug 27, 2012 at 7:43 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> > >> There's also the big-picture question of whether we should just get rid
> > >> of fuzzy comparisons in the geometric types instead of trying to hack
> > >> indexes to work around them.

> In any event, I think we should entertain a patch to make the GiST operator
> class methods bug-compatible with corresponding operators. Even if we decide
> to change operator behavior in HEAD, the back branches could use it.

We have broad agreement that the specific implementation of fuzz in geometric
comparison operators is shoddy, but nobody has voiced interest in designing a
concrete improvement. I propose adding a TODO item "Remove or improve
rounding in geometric comparison operators", endorsing Alexander's design, and
reviewing his patch. Objections?


From: Noah Misch <noah(at)leadboat(dot)com>
To: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Robert Haas <robertmhaas(at)gmail(dot)com>, Bruce Momjian <bruce(at)momjian(dot)us>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-10-18 19:18:28
Message-ID: 20121018191828.GB10844@tornado.leadboat.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Oct 11, 2012 at 07:17:28AM -0400, Noah Misch wrote:
> On Tue, Oct 02, 2012 at 01:58:40PM -0400, Noah Misch wrote:
> > > > On Mon, Aug 27, 2012 at 7:43 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> > > >> There's also the big-picture question of whether we should just get rid
> > > >> of fuzzy comparisons in the geometric types instead of trying to hack
> > > >> indexes to work around them.
>
> > In any event, I think we should entertain a patch to make the GiST operator
> > class methods bug-compatible with corresponding operators. Even if we decide
> > to change operator behavior in HEAD, the back branches could use it.
>
> We have broad agreement that the specific implementation of fuzz in geometric
> comparison operators is shoddy, but nobody has voiced interest in designing a
> concrete improvement. I propose adding a TODO item "Remove or improve
> rounding in geometric comparison operators", endorsing Alexander's design, and
> reviewing his patch. Objections?

TODO added, and here's a review:

The patch adds no regression tests; it should add tests illustrating the
problems it fixes.

I audited the other indexable geometric operators for similar problems. This
passage in gist_point_consistent_internal(), which handles (point,point)
operators, caught my suspicion:

case RTSameStrategyNumber:
if (isLeaf)
{
result = FPeq(key->low.x, query->x)
&& FPeq(key->low.y, query->y);
}
else
{
result = (query->x <= key->high.x && query->x >= key->low.x &&
query->y <= key->high.y && query->y >= key->low.y);
}
break;

A leaf entry reachable from an internal entry may fall exactly on the
internal-entry bounding box. Since we would accept a fuzzy match at the leaf
level, I think we must also accept a fuzzy match at the internal level.

> *** a/src/backend/access/gist/gistproc.c
> --- b/src/backend/access/gist/gistproc.c

> ***************
> *** 1326,1331 **** gist_point_consistent(PG_FUNCTION_ARGS)
> --- 1327,1333 ----
> bool *recheck = (bool *) PG_GETARG_POINTER(4);
> bool result;
> StrategyNumber strategyGroup = strategy / GeoStrategyNumberOffset;
> + BOX *query, *key;

This function now has "query" variables within subsidiary blocks redundant
with and masking this one. Avoid doing that.

>
> switch (strategyGroup)
> {
> ***************
> *** 1337,1348 **** gist_point_consistent(PG_FUNCTION_ARGS)
> *recheck = false;
> break;
> case BoxStrategyNumberGroup:
> ! result = DatumGetBool(DirectFunctionCall5(
> ! gist_box_consistent,
> ! PointerGetDatum(entry),
> ! PG_GETARG_DATUM(1),
> ! Int16GetDatum(RTOverlapStrategyNumber),
> ! 0, PointerGetDatum(recheck)));
> break;
> case PolygonStrategyNumberGroup:
> {
> --- 1339,1356 ----
> *recheck = false;
> break;
> case BoxStrategyNumberGroup:
> ! /*
> ! * This code repeats logic of on_ob which uses simple comparison
> ! * rather than FP* functions.
> ! */
> ! query = PG_GETARG_BOX_P(1);
> ! key = DatumGetBoxP(entry->key);
> !
> ! *recheck = false;
> ! result = key->high.x >= query->low.x &&
> ! key->low.x <= query->high.x &&
> ! key->high.y >= query->low.y &&
> ! key->low.y <= query->high.y;

For leaf entries, this correctly degenerates to on_pb(). For internal
entries, it must, but does not, implement box_overlap(). (The fuzzy
box_overlap() would be fine.) I recommend making gist_point_consistent()'s
treatment of boxes resemble its treatment of circles and polygons; that eases
verifying their correctness. Call gist_box_consistent. Then, for leaf
entries, call box_contain_pt().

GiST "consistent" functions often validate the strategy number, but the
circle, polygon and box branches of gist_point_consistent silently assume
strategy % GeoStrategyNumberOffset == RTContainedByStrategyNumber. Should
they verify that assumption? I raise this as an incidental question; it is
not new with your patch.

Thanks,
nm


From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Oleg Bartunov <obartunov(at)gmail(dot)com>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Alexander Korotkov <aekorotkov(at)gmail(dot)com>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-10-23 14:18:48
Message-ID: 20121023141848.GB4971@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Oleg Bartunov escribió:
> Yes, it's a bug and it needs to be applied !

Oleg,

This patch has been waiting a long time for some review and commit.
Since it fixes existing bugs, it should be backpatched; or at least some
people believe it needs to be.

Please see downthread -- there is some commentary from Noah ([1] and
others) about the patch itself. As far I understand, some changes are
still needed, and I don't know if the last version submitted is the
version that should be backpatched. But *something* needs to be done
about this patch. Since you and Teodor are the guys mostly in charge of
GiST, could you please see about finalizing and committing it?

Thanks.

[1] http://archives.postgresql.org/message-id/20121018191828.GB10844@tornado.leadboat.com

> On Tue, Jul 3, 2012 at 7:44 PM, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
> > On Tue, Jul 3, 2012 at 11:34 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> >> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> >>> On Thu, Jun 21, 2012 at 2:53 PM, Alexander Korotkov
> >>> <aekorotkov(at)gmail(dot)com> wrote:
> >>>> I think we definitely should apply this patch before 9.2 release, because it
> >>>> is a bug fix. Otherwise people will continue produce incorrect GiST indexes
> >>>> with in-core geometrical opclasses until 9.3. Patch is very simple and only
> >>>> changes few lines of code.
> >>>>
> >>>> Any thoughts?
> >>
> >>> Do we need to apply this patch to 9.2?
> >>
> >> It's been like that all along, no?
> >
> > Yeah, but it seems an awful lot like a bug.  In fact... it's hard to
> > imagine how it could be any more of a bug than this.

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services


From: Noah Misch <noah(at)leadboat(dot)com>
To: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: Oleg Bartunov <obartunov(at)gmail(dot)com>, Robert Haas <robertmhaas(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Alexander Korotkov <aekorotkov(at)gmail(dot)com>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-10-23 16:35:15
Message-ID: 20121023163515.GA18141@tornado.leadboat.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Oct 23, 2012 at 11:18:48AM -0300, Alvaro Herrera wrote:
> Please see downthread -- there is some commentary from Noah ([1] and
> others) about the patch itself. As far I understand, some changes are
> still needed, and I don't know if the last version submitted is the
> version that should be backpatched.

We'd best use the same patch for both HEAD and back branches; pg_upgrade would
carry forward faulty indexes. pg_upgrade could instead invalidate them, but I
don't think the gravity of the problem calls for it. A release note
suggesting a REINDEX of affected indexes ought to suffice.


From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: Noah Misch <noah(at)leadboat(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Robert Haas <robertmhaas(at)gmail(dot)com>, Bruce Momjian <bruce(at)momjian(dot)us>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-11-02 12:05:30
Message-ID: CAPpHfduzPjwO-bUkJdniQ-K-c3x-uh9ZhtFf1PLr_6hKstn7fQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Oct 18, 2012 at 11:18 PM, Noah Misch <noah(at)leadboat(dot)com> wrote:

> On Thu, Oct 11, 2012 at 07:17:28AM -0400, Noah Misch wrote:
> > On Tue, Oct 02, 2012 at 01:58:40PM -0400, Noah Misch wrote:
> > > > > On Mon, Aug 27, 2012 at 7:43 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
> wrote:
> > > > >> There's also the big-picture question of whether we should just
> get rid
> > > > >> of fuzzy comparisons in the geometric types instead of trying to
> hack
> > > > >> indexes to work around them.
> >
> > > In any event, I think we should entertain a patch to make the GiST
> operator
> > > class methods bug-compatible with corresponding operators. Even if we
> decide
> > > to change operator behavior in HEAD, the back branches could use it.
> >
> > We have broad agreement that the specific implementation of fuzz in
> geometric
> > comparison operators is shoddy, but nobody has voiced interest in
> designing a
> > concrete improvement. I propose adding a TODO item "Remove or improve
> > rounding in geometric comparison operators", endorsing Alexander's
> design, and
> > reviewing his patch. Objections?
>
> TODO added, and here's a review:
>
> The patch adds no regression tests; it should add tests illustrating the
> problems it fixes.
>

I've added some tests to points.sql.

> I audited the other indexable geometric operators for similar problems.
> This
> passage in gist_point_consistent_internal(), which handles (point,point)
> operators, caught my suspicion:
>
> case RTSameStrategyNumber:
> if (isLeaf)
> {
> result = FPeq(key->low.x, query->x)
> && FPeq(key->low.y, query->y);
> }
> else
> {
> result = (query->x <= key->high.x &&
> query->x >= key->low.x &&
> query->y <= key->high.y
> && query->y >= key->low.y);
> }
> break;
>
> A leaf entry reachable from an internal entry may fall exactly on the
> internal-entry bounding box. Since we would accept a fuzzy match at the
> leaf
> level, I think we must also accept a fuzzy match at the internal level.

Good catch, fixed.

> > *** a/src/backend/access/gist/gistproc.c
> > --- b/src/backend/access/gist/gistproc.c
>
> > ***************
> > *** 1326,1331 **** gist_point_consistent(PG_FUNCTION_ARGS)
> > --- 1327,1333 ----
> > bool *recheck = (bool *) PG_GETARG_POINTER(4);
> > bool result;
> > StrategyNumber strategyGroup = strategy / GeoStrategyNumberOffset;
> > + BOX *query, *key;
>
> This function now has "query" variables within subsidiary blocks redundant
> with and masking this one. Avoid doing that.

Fixed.

>
> > switch (strategyGroup)
> > {
> > ***************
> > *** 1337,1348 **** gist_point_consistent(PG_FUNCTION_ARGS)
> > *recheck = false;
> > break;
> > case BoxStrategyNumberGroup:
> > ! result = DatumGetBool(DirectFunctionCall5(
> > !
> gist_box_consistent,
> > !
> PointerGetDatum(entry),
> > !
> PG_GETARG_DATUM(1),
> > !
> Int16GetDatum(RTOverlapStrategyNumber),
> > !
> 0, PointerGetDatum(recheck)));
> > break;
> > case PolygonStrategyNumberGroup:
> > {
> > --- 1339,1356 ----
> > *recheck = false;
> > break;
> > case BoxStrategyNumberGroup:
> > ! /*
> > ! * This code repeats logic of on_ob which uses
> simple comparison
> > ! * rather than FP* functions.
> > ! */
> > ! query = PG_GETARG_BOX_P(1);
> > ! key = DatumGetBoxP(entry->key);
> > !
> > ! *recheck = false;
> > ! result = key->high.x >= query->low.x &&
> > ! key->low.x <= query->high.x &&
> > ! key->high.y >= query->low.y &&
> > ! key->low.y <= query->high.y;
>
> For leaf entries, this correctly degenerates to on_pb(). For internal
> entries, it must, but does not, implement box_overlap(). (The fuzzy
> box_overlap() would be fine.) I recommend making gist_point_consistent()'s
> treatment of boxes resemble its treatment of circles and polygons; that
> eases
> verifying their correctness. Call gist_box_consistent. Then, for leaf
> entries, call box_contain_pt().
>

I have two objections on doing that:
1) It's not evident for me that fuzzy comparison in internal pages is fine.
Obviously, it depends on data distribution. It's easy to provide an example
when fuzzy comparison will lead to significant performance degradation.
2) With PolygonStrategyNumberGroup CircleStrategyNumberGroup it's faster to
do simple box comparison than doing calculation for exact circle and
especially polygon check. In this case previous filtering in leaf pages
looks reasonable. With BoxStrategyNumberGroup exact calculation is simpler
than gist_box_consistent.

------
With best regards,
Alexander Korotkov.

Attachment Content-Type Size
gistproc_fix-2.patch application/octet-stream 6.7 KB

From: Noah Misch <noah(at)leadboat(dot)com>
To: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Robert Haas <robertmhaas(at)gmail(dot)com>, Bruce Momjian <bruce(at)momjian(dot)us>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-11-02 12:46:41
Message-ID: 20121102124641.GA14496@tornado.leadboat.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Nov 02, 2012 at 04:05:30PM +0400, Alexander Korotkov wrote:
> On Thu, Oct 18, 2012 at 11:18 PM, Noah Misch <noah(at)leadboat(dot)com> wrote:

> > > --- 1339,1356 ----
> > > *recheck = false;
> > > break;
> > > case BoxStrategyNumberGroup:
> > > ! /*
> > > ! * This code repeats logic of on_ob which uses
> > simple comparison
> > > ! * rather than FP* functions.
> > > ! */
> > > ! query = PG_GETARG_BOX_P(1);
> > > ! key = DatumGetBoxP(entry->key);
> > > !
> > > ! *recheck = false;
> > > ! result = key->high.x >= query->low.x &&
> > > ! key->low.x <= query->high.x &&
> > > ! key->high.y >= query->low.y &&
> > > ! key->low.y <= query->high.y;
> >
> > For leaf entries, this correctly degenerates to on_pb(). For internal
> > entries, it must, but does not, implement box_overlap(). (The fuzzy
> > box_overlap() would be fine.) I recommend making gist_point_consistent()'s
> > treatment of boxes resemble its treatment of circles and polygons; that
> > eases
> > verifying their correctness. Call gist_box_consistent. Then, for leaf
> > entries, call box_contain_pt().
> >
>
> I have two objections on doing that:
> 1) It's not evident for me that fuzzy comparison in internal pages is fine.
> Obviously, it depends on data distribution. It's easy to provide an example
> when fuzzy comparison will lead to significant performance degradation.
> 2) With PolygonStrategyNumberGroup CircleStrategyNumberGroup it's faster to
> do simple box comparison than doing calculation for exact circle and
> especially polygon check. In this case previous filtering in leaf pages
> looks reasonable. With BoxStrategyNumberGroup exact calculation is simpler
> than gist_box_consistent.

That's fair; I withdraw the recommendation to use gist_box_consistent(). It
remains that the code here must somehow implement a box_overlap()-style
calculation for internal pages.


From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: Noah Misch <noah(at)leadboat(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Robert Haas <robertmhaas(at)gmail(dot)com>, Bruce Momjian <bruce(at)momjian(dot)us>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-11-02 17:01:17
Message-ID: CAPpHfdv3p8pOU5-uy7swHXqve8EwHvbCs8Xwmjf-CXGDQbyzbQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Nov 2, 2012 at 4:46 PM, Noah Misch <noah(at)leadboat(dot)com> wrote:

> On Fri, Nov 02, 2012 at 04:05:30PM +0400, Alexander Korotkov wrote:
> > On Thu, Oct 18, 2012 at 11:18 PM, Noah Misch <noah(at)leadboat(dot)com> wrote:
>
> > > > --- 1339,1356 ----
> > > > *recheck = false;
> > > > break;
> > > > case BoxStrategyNumberGroup:
> > > > ! /*
> > > > ! * This code repeats logic of on_ob which uses
> > > simple comparison
> > > > ! * rather than FP* functions.
> > > > ! */
> > > > ! query = PG_GETARG_BOX_P(1);
> > > > ! key = DatumGetBoxP(entry->key);
> > > > !
> > > > ! *recheck = false;
> > > > ! result = key->high.x >= query->low.x &&
> > > > ! key->low.x <= query->high.x &&
> > > > ! key->high.y >= query->low.y &&
> > > > ! key->low.y <= query->high.y;
> > >
> > > For leaf entries, this correctly degenerates to on_pb(). For internal
> > > entries, it must, but does not, implement box_overlap(). (The fuzzy
> > > box_overlap() would be fine.) I recommend making
> gist_point_consistent()'s
> > > treatment of boxes resemble its treatment of circles and polygons; that
> > > eases
> > > verifying their correctness. Call gist_box_consistent. Then, for leaf
> > > entries, call box_contain_pt().
> > >
> >
> > I have two objections on doing that:
> > 1) It's not evident for me that fuzzy comparison in internal pages is
> fine.
> > Obviously, it depends on data distribution. It's easy to provide an
> example
> > when fuzzy comparison will lead to significant performance degradation.
> > 2) With PolygonStrategyNumberGroup CircleStrategyNumberGroup it's faster
> to
> > do simple box comparison than doing calculation for exact circle and
> > especially polygon check. In this case previous filtering in leaf pages
> > looks reasonable. With BoxStrategyNumberGroup exact calculation is
> simpler
> > than gist_box_consistent.
>
> That's fair; I withdraw the recommendation to use gist_box_consistent().
> It
> remains that the code here must somehow implement a box_overlap()-style
> calculation for internal pages.
>

Sorry, didn't understand this point. What exactly do you mean by
box_overlap()-style?

------
With best regards,
Alexander Korotkov.


From: Noah Misch <noah(at)leadboat(dot)com>
To: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Robert Haas <robertmhaas(at)gmail(dot)com>, Bruce Momjian <bruce(at)momjian(dot)us>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-11-03 00:23:56
Message-ID: 20121103002356.GA28197@tornado.leadboat.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Nov 02, 2012 at 09:01:17PM +0400, Alexander Korotkov wrote:
> On Fri, Nov 2, 2012 at 4:46 PM, Noah Misch <noah(at)leadboat(dot)com> wrote:
> > On Fri, Nov 02, 2012 at 04:05:30PM +0400, Alexander Korotkov wrote:
> > > On Thu, Oct 18, 2012 at 11:18 PM, Noah Misch <noah(at)leadboat(dot)com> wrote:
> >
> > > > > --- 1339,1356 ----
> > > > > *recheck = false;
> > > > > break;
> > > > > case BoxStrategyNumberGroup:
> > > > > ! /*
> > > > > ! * This code repeats logic of on_ob which uses
> > > > simple comparison
> > > > > ! * rather than FP* functions.
> > > > > ! */
> > > > > ! query = PG_GETARG_BOX_P(1);
> > > > > ! key = DatumGetBoxP(entry->key);
> > > > > !
> > > > > ! *recheck = false;
> > > > > ! result = key->high.x >= query->low.x &&
> > > > > ! key->low.x <= query->high.x &&
> > > > > ! key->high.y >= query->low.y &&
> > > > > ! key->low.y <= query->high.y;
> > > >
> > > > For leaf entries, this correctly degenerates to on_pb(). For internal
> > > > entries, it must, but does not, implement box_overlap(). (The fuzzy
> > > > box_overlap() would be fine.)

> > It
> > remains that the code here must somehow implement a box_overlap()-style
> > calculation for internal pages.
> >
>
> Sorry, didn't understand this point. What exactly do you mean by
> box_overlap()-style?

point_ops index entries have type "box". On leaf pages, the box for each
entry is trivial, having high == low. At leaf pages, gist_point_consistent()
should implement "point <@ box" with an algorithm equivalent to on_pb(); your
latest code achieves that. In internal pages, the box for each entry is
rarely trivial; it spans all points stored on the leaf page reachable through
its downlink. At internal pages, gist_point_consistent() should implement
"point <@ box" with an algorithm near-equivalent to box_overlap(). (As an
optional deviation, it may use exact comparisons despite box_overlap() using
fuzzy comparisons.) Looking at the math again, your latest code does achieve
that, too. I was thrown off by your use of a different, albeit mathematically
equivalent, algorithm from the one used in box_overlap(). Please don't do
that; either use box_overlap()'s algorithm here, or change box_overlap() to
use the shorter algorithm you have introduced. Formulating the same
calculation differently in related code is a recipe for confusion. (Then
again, perhaps the equivalence of the algorithms is obvious to everyone
entitled to travel within 1 km of the geometric type implementation.)

Thanks,
nm


From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: Noah Misch <noah(at)leadboat(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Robert Haas <robertmhaas(at)gmail(dot)com>, Bruce Momjian <bruce(at)momjian(dot)us>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-11-03 21:53:19
Message-ID: CAPpHfdvJz-PsAqLJg_nTLF32cN6KwL8+-_YvuwY3=N5TmfqGqQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sat, Nov 3, 2012 at 4:23 AM, Noah Misch <noah(at)leadboat(dot)com> wrote:

> On Fri, Nov 02, 2012 at 09:01:17PM +0400, Alexander Korotkov wrote:
> > On Fri, Nov 2, 2012 at 4:46 PM, Noah Misch <noah(at)leadboat(dot)com> wrote:
> > > On Fri, Nov 02, 2012 at 04:05:30PM +0400, Alexander Korotkov wrote:
> > > > On Thu, Oct 18, 2012 at 11:18 PM, Noah Misch <noah(at)leadboat(dot)com>
> wrote:
> > >
> > > > > > --- 1339,1356 ----
> > > > > > *recheck = false;
> > > > > > break;
> > > > > > case BoxStrategyNumberGroup:
> > > > > > ! /*
> > > > > > ! * This code repeats logic of on_ob which
> uses
> > > > > simple comparison
> > > > > > ! * rather than FP* functions.
> > > > > > ! */
> > > > > > ! query = PG_GETARG_BOX_P(1);
> > > > > > ! key = DatumGetBoxP(entry->key);
> > > > > > !
> > > > > > ! *recheck = false;
> > > > > > ! result = key->high.x >= query->low.x &&
> > > > > > ! key->low.x <=
> query->high.x &&
> > > > > > ! key->high.y >=
> query->low.y &&
> > > > > > ! key->low.y <=
> query->high.y;
> > > > >
> > > > > For leaf entries, this correctly degenerates to on_pb(). For
> internal
> > > > > entries, it must, but does not, implement box_overlap(). (The
> fuzzy
> > > > > box_overlap() would be fine.)
>
> > > It
> > > remains that the code here must somehow implement a box_overlap()-style
> > > calculation for internal pages.
> > >
> >
> > Sorry, didn't understand this point. What exactly do you mean by
> > box_overlap()-style?
>
> point_ops index entries have type "box". On leaf pages, the box for each
> entry is trivial, having high == low. At leaf pages,
> gist_point_consistent()
> should implement "point <@ box" with an algorithm equivalent to on_pb();
> your
> latest code achieves that. In internal pages, the box for each entry is
> rarely trivial; it spans all points stored on the leaf page reachable
> through
> its downlink. At internal pages, gist_point_consistent() should implement
> "point <@ box" with an algorithm near-equivalent to box_overlap(). (As an
> optional deviation, it may use exact comparisons despite box_overlap()
> using
> fuzzy comparisons.) Looking at the math again, your latest code does
> achieve
> that, too. I was thrown off by your use of a different, albeit
> mathematically
> equivalent, algorithm from the one used in box_overlap(). Please don't do
> that; either use box_overlap()'s algorithm here, or change box_overlap() to
> use the shorter algorithm you have introduced. Formulating the same
> calculation differently in related code is a recipe for confusion. (Then
> again, perhaps the equivalence of the algorithms is obvious to everyone
> entitled to travel within 1 km of the geometric type implementation.)
>

I've added comment for clarifying this situation.

------
With best regards,
Alexander Korotkov.

Attachment Content-Type Size
gistproc_fix-3.patch application/octet-stream 7.1 KB

From: Noah Misch <noah(at)leadboat(dot)com>
To: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Robert Haas <robertmhaas(at)gmail(dot)com>, Bruce Momjian <bruce(at)momjian(dot)us>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2012-11-10 15:54:55
Message-ID: 20121110155455.GA7538@tornado.leadboat.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sun, Nov 04, 2012 at 01:53:19AM +0400, Alexander Korotkov wrote:
> On Sat, Nov 3, 2012 at 4:23 AM, Noah Misch <noah(at)leadboat(dot)com> wrote:
> > I was thrown off by your use of a different, albeit
> > mathematically
> > equivalent, algorithm from the one used in box_overlap(). Please don't do
> > that; either use box_overlap()'s algorithm here, or change box_overlap() to
> > use the shorter algorithm you have introduced. Formulating the same
> > calculation differently in related code is a recipe for confusion. (Then
> > again, perhaps the equivalence of the algorithms is obvious to everyone
> > entitled to travel within 1 km of the geometric type implementation.)
> >
>
> I've added comment for clarifying this situation.

Good enough.

> ! * This code repeats logic of on_ob which checks if point is

Typo: the function is on_pb().

> + -- Testing GiST indexes provides same behaviour as sequential scan
> + SET enable_seqscan TO false;
> + CREATE TABLE POINT_GIST_TBL(f1 point);
> + INSERT INTO POINT_GIST_TBL (SELECT '(0,0)' FROM generate_series(0,1000));
> + CREATE INDEX POINT_GIST_TBL_INDEX ON POINT_GIST_TBL USING gist (f1);
> + INSERT INTO POINT_GIST_TBL VALUES ('(0.0000009,0.0000009)');
> + SELECT COUNT(*) FROM POINT_GIST_TBL WHERE f1 ~= '(0.0000009,0.0000009)'::point;
> + SELECT COUNT(*) FROM POINT_GIST_TBL WHERE f1 <@ '(0.0000009,0.0000009),(0.0000009,0.0000009)'::box;
> + SELECT COUNT(*) FROM POINT_GIST_TBL WHERE f1 ~= '(0.0000018,0.0000018)'::point;

Do a "RESET enable_seqscan;" at the end. The omission has no consequence here
since these are the last commands of the test file.

Neither of those things are important enough to call for a new version; I'm
leaving the patch Ready for Committer.

Thanks,
nm


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Cc: Noah Misch <noah(at)leadboat(dot)com>, Robert Haas <robertmhaas(at)gmail(dot)com>, Bruce Momjian <bruce(at)momjian(dot)us>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Incorrect behaviour when using a GiST index on points
Date: 2013-02-08 23:32:53
Message-ID: 25625.1360366373@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Alexander Korotkov <aekorotkov(at)gmail(dot)com> writes:
> On Sat, Nov 3, 2012 at 4:23 AM, Noah Misch <noah(at)leadboat(dot)com> wrote:
>> ... At internal pages, gist_point_consistent() should implement
>> "point <@ box" with an algorithm near-equivalent to box_overlap(). (As an
>> optional deviation, it may use exact comparisons despite box_overlap() using
>> fuzzy comparisons.) Looking at the math again, your latest code does achieve
>> that, too. I was thrown off by your use of a different, albeit mathematically
>> equivalent, algorithm from the one used in box_overlap(). Please don't do
>> that; either use box_overlap()'s algorithm here, or change box_overlap() to
>> use the shorter algorithm you have introduced. Formulating the same
>> calculation differently in related code is a recipe for confusion. (Then
>> again, perhaps the equivalence of the algorithms is obvious to everyone
>> entitled to travel within 1 km of the geometric type implementation.)

> I've added comment for clarifying this situation.

Applied and back-patched with some cosmetic changes (mostly the
comments) and a better version of the regression test.

As a separate commit, I also simplified box_overlap() to match this
logic, since I agree with Noah that it's not good for them to look so
different. Besides, it should be at least a bit faster this way.

regards, tom lane