From: Heikki Linnakangas <hlinnaka@iki.fi>
To: Andrey Borodin <x4mmm@yandex-team.ru>
To: Duncan Sands <duncan.sands@deepbluecap.com>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: Reproducible GIST index corruption under concurrent updates
Date: Wed, 20 Jan 2021 00:31:30 +0200
Message-ID: <907491b3-87c7-aadd-3fb1-be3c542b7fb7@iki.fi> (raw)
In-Reply-To: <132baf64-3764-0351-750e-0443e74f1052@iki.fi>
References: <a9690483-6c6c-3c82-c8ba-dc1a40848f11@deepbluecap.com>
<28663562-8489-4680-BC26-CAEADEB258D1@yandex-team.ru>
<132baf64-3764-0351-750e-0443e74f1052@iki.fi>
On 19/01/2021 23:53, Heikki Linnakangas wrote:
> On 19/01/2021 19:12, Andrey Borodin wrote:
>>> 19 янв. 2021 г., в 16:15, Duncan Sands <duncan.sands@deepbluecap.com> написал(а):
>>>
>>> Enjoy!
>>
>> Thanks!
>> It reproduces on my machine. Seems like all tuples are indexed, but some root downlinks are not adjusted properly. Probably, after concurrent split.
>
> This is reproducable down to v12. I bisected it to this commit:
>
> commit e2e992c93145cfc0e3563fb84efd25b390a84bb9 (refs/bisect/bad)
> Author: Heikki Linnakangas <heikki.linnakangas@iki.fi>
> Date: Wed Jul 24 20:24:05 2019 +0300
>
> Refactor checks for deleted GiST pages.
>
> The explicit check in gistScanPage() isn't currently really
> necessary, as
> a deleted page is always empty, so the loop would fall through without
> doing anything, anyway. But it's a marginal optimization, and it
> gives a
> nice place to attach a comment to explain how it works.
>
> Backpatch to v12, where GiST page deletion was introduced.
>
> Reviewed-by: Andrey Borodin
> Discussion:
> https://www.postgresql.org/message-id/835A15A5-F1B4-4446-A711-BF48357EB602%40yandex-team.ru
>
> I'll dig deeper tomorrow.
The culprit is this change:
> @@ -858,12 +856,13 @@ gistdoinsert(Relation r, IndexTuple itup, Size freespace,
> * leaf/inner is enough to recognize split for root
> */
> }
> - else if (GistFollowRight(stack->page) ||
> - stack->parent->lsn < GistPageGetNSN(stack->page))
> + else if ((GistFollowRight(stack->page) ||
> + stack->parent->lsn < GistPageGetNSN(stack->page)) &&
> + GistPageIsDeleted(stack->page))
> {
> /*
> - * The page was split while we momentarily unlocked the
> - * page. Go back to parent.
> + * The page was split or deleted while we momentarily
> + * unlocked the page. Go back to parent.
> */
> UnlockReleaseBuffer(stack->buffer);
> xlocked = false;
The comment change was correct, but the condition used &&. Should've
been ||. There is another copy of basically the same condition earlier
in the function that was changed correctly, but I blundered this one. Oops.
The attached patch fixes this. I also added an assertion to the
gistplacetopage() function, to check that we never try to insert on a
deleted page. This bug could've made that happen too, although in this
case the problem was a concurrent split, not a deletion. I'll backpatch
and push this tomorrow.
Many thanks for the easy reproducer script, Duncan!
- Heikki
Attachments:fix-gist-corruption-bug.patch (text/x-patch, 806B) downloaddiff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.cindex 79030e7715..d94d9d33a6 100644--- a/src/backend/access/gist/gist.c+++ b/src/backend/access/gist/gist.c@@ -242,6 +242,9 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate,
if (GistFollowRight(page))
elog(ERROR, "concurrent GiST page split was incomplete");
+ /* should never try to insert to a deleted page */+ Assert(!GistPageIsDeleted(page));+
*splitinfo = NIL;
/*
@@ -857,7 +860,7 @@ gistdoinsert(Relation r, IndexTuple itup, Size freespace,
*/
}
else if ((GistFollowRight(stack->page) ||
- stack->parent->lsn < GistPageGetNSN(stack->page)) &&+ stack->parent->lsn < GistPageGetNSN(stack->page)) ||
GistPageIsDeleted(stack->page))
{
/*
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: hlinnaka@iki.fi, x4mmm@yandex-team.ru, duncan.sands@deepbluecap.com
Subject: Re: Reproducible GIST index corruption under concurrent updates
In-Reply-To: <907491b3-87c7-aadd-3fb1-be3c542b7fb7@iki.fi>
References: <a9690483-6c6c-3c82-c8ba-dc1a40848f11@deepbluecap.com> <28663562-8489-4680-BC26-CAEADEB258D1@yandex-team.ru> <132baf64-3764-0351-750e-0443e74f1052@iki.fi> <907491b3-87c7-aadd-3fb1-be3c542b7fb7@iki.fi>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox