Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 24ace40

Browse files
committed
Retry after buffer locking failure during SPGiST index creation.
The original coding thought this case was impossible, but it can happen if the bgwriter or checkpointer processes decide to write out an index page while creation is still proceeding, leading to a bogus "unexpected spgdoinsert() failure" error. Problem reported by Jonathan S. Katz. Teodor Sigaev
1 parent bffd1ce commit 24ace40

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/backend/access/spgist/spgdoinsert.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -1946,9 +1946,12 @@ spgdoinsert(Relation index, SpGistState *state,
19461946
* Attempt to acquire lock on child page. We must beware of
19471947
* deadlock against another insertion process descending from that
19481948
* page to our parent page (see README). If we fail to get lock,
1949-
* abandon the insertion and tell our caller to start over. XXX
1950-
* this could be improved; perhaps it'd be worth sleeping a bit
1951-
* before giving up?
1949+
* abandon the insertion and tell our caller to start over.
1950+
*
1951+
* XXX this could be improved, because failing to get lock on a
1952+
* buffer is not proof of a deadlock situation; the lock might be
1953+
* held by a reader, or even just background writer/checkpointer
1954+
* process. Perhaps it'd be worth retrying after sleeping a bit?
19521955
*/
19531956
if (!ConditionalLockBuffer(current.buffer))
19541957
{

src/backend/access/spgist/spginsert.c

+11-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,17 @@ spgistBuildCallback(Relation index, HeapTuple htup, Datum *values,
4545
/* Work in temp context, and reset it after each tuple */
4646
oldCtx = MemoryContextSwitchTo(buildstate->tmpCtx);
4747

48-
/* No concurrent insertions can be happening, so failure is unexpected */
49-
if (!spgdoinsert(index, &buildstate->spgstate, &htup->t_self,
50-
*values, *isnull))
51-
elog(ERROR, "unexpected spgdoinsert() failure");
48+
/*
49+
* Even though no concurrent insertions can be happening, we still might
50+
* get a buffer-locking failure due to bgwriter or checkpointer taking a
51+
* lock on some buffer. So we need to be willing to retry. We can flush
52+
* any temp data when retrying.
53+
*/
54+
while (!spgdoinsert(index, &buildstate->spgstate, &htup->t_self,
55+
*values, *isnull))
56+
{
57+
MemoryContextReset(buildstate->tmpCtx);
58+
}
5259

5360
MemoryContextSwitchTo(oldCtx);
5461
MemoryContextReset(buildstate->tmpCtx);

0 commit comments

Comments
 (0)