Re: BUG #18129: GiST index produces incorrect query results

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: exclusion(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18129: GiST index produces incorrect query results
Date: 2023-09-23 22:06:35
Message-ID: 80e4ef74-4a00-4631-963f-3dd11e64c6fe@iki.fi
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On 22/09/2023 09:00, PG Bug reporting form wrote:
> First bad commit is 9155580fd, on which three different outcomes are
> possible: test passes/test fails/assertion fails.
> Assertion failures, that we had observed after 9155580fd, were fixed by
> a7ee7c851 and 741b88435, but apparently there is another defect induced
> by that gist implementation change.

This is indeed a similar bug to that fixed in 741b88435 and a7ee7c851.
In those commits, we fixed the insertion code so that whenever we insert
a downlink to the parent page and that causes the parent page to split,
we invalidate the 'downlinkoffnum' we had memorized for the child.
Because when the parent page is split, the downlink for the child likely
moves.

However, the downlink can move even when we *don't* split the parent.
Splitting a page not only inserts the new tuple for the new right page,
but also updates the old downlink, because the key representing what is
now the left page covers less keys than the original page. And updating
a tuple is implemented as delete+insert. The delete moves existing
tuples on the page! In other words, this code and comment in
gistfinishsplit():

> if (gistinserttuples(state, stack->parent, giststate,
> tuples, 2,
> stack->downlinkoffnum,
> left->buf, right->buf,
> true, /* Unlock parent */
> unlockbuf /* Unlock stack->buffer if caller wants
> * that */
> ))
> {
> /*
> * If the parent page was split, the downlink might have moved.
> */
> stack->downlinkoffnum = InvalidOffsetNumber;
> }

is wrong. We must invalidate 'downlinkoffnum' whether or not the parent
page was split, because updating an existing tuple can also move other
tuples on the page.

Attached is a set of patches to fix this:

1. To catch the problem earlier - already during the index build - I
modified getFindCorrectParent() to always find the parent the hard way
by scanning the parent page, even if 'downlinkoffnum' is set. Once the
downlink is found, it compares it with the 'downlinkoffnum', and PANICs
if it wasn't where we expected. That defeats the point of memorizing the
'downlinkoffnum' in the first place, but it's a useful cross-check.

I don't intend to commit this, but it was useful during debugging.

2. A straightforward fix for the bug.

3. Looking at gistFindCorrectParent, when it steps right, it also
doesn't update 'downlinkoffnum' of the parent page. Surely if we step to
a different block altogether, the memorized downlink position of the
previous block no longer applies.

Worryingly, I haven't been able to get a test failure caused by that.
Maybe there are some reasons why we never need to split the parent after
that, not sure. But it sure looks wrong.

4. Given how many bugs we've already had with this, I'd like to make
this more robust. Currently, we meticulously track if we have made any
inserts that invalidate the memorized location of the downlink. Instead,
gistFindCorrectParent() could treat the memorized location as just a
hint, and always check if the downlink is still at the memorized
location. If it is, great, and if it's not, find it the hard way. That
makes it unnecessary to clear 'downlinkoffnum' at the right places. We
still need the 'retry_from_parent' flag, though.

5. Re-indent the code after previous commit. Separated for easier review.

I'd also like to add something to our regression test suite for better
coverage of this. The intarray test case is very useful, but it'd be
nice to have something in the main regression suite, as this is a
general GiST issue, not related to intarray. I haven't spent any time on
that yet.

--
Heikki Linnakangas
Neon (https://neon.tech)

Attachment Content-Type Size
0001-Add-assertion-that-downlinkoffnum-is-set-correctly.patch text/x-patch 1.5 KB
0002-Fix-another-bug-in-GiST-index-build.patch text/x-patch 3.3 KB
0003-Also-clear-downlinkoffnum-when-the-parent-block-is-c.patch text/x-patch 1.0 KB
0004-Treat-downlinkoffnum-only-as-a-hint-in-gistFindCorre.patch text/x-patch 5.9 KB
0005-Re-indent-the-code.patch text/x-patch 4.3 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Michael Paquier 2023-09-23 23:23:43 Re: BUG #17928: Standby fails to decode WAL on termination of primary
Previous Message Thomas Munro 2023-09-23 20:48:42 Re: BUG #17928: Standby fails to decode WAL on termination of primary