Fix query-cancel handling in spgdoinsert().
authorTom Lane <[email protected]>
Fri, 14 May 2021 17:26:55 +0000 (13:26 -0400)
committerTom Lane <[email protected]>
Fri, 14 May 2021 17:26:55 +0000 (13:26 -0400)
commitfe64adc9317740569c18e948d0d838d46696c5d3
tree1309e5a3368952a350189a4959c472ae90638b4e
parent39b8ccb08619cb3e4c97d90134fcb7fb48c79bac
Fix query-cancel handling in spgdoinsert().

Knowing that a buggy opclass could cause an infinite insertion loop,
spgdoinsert() intended to allow its loop to be interrupted by query
cancel.  However, that never actually worked, because in iterations
after the first, we'd be holding buffer lock(s) which would cause
InterruptHoldoffCount to be positive, preventing servicing of the
interrupt.

To fix, check if an interrupt is pending, and if so fall out of
the insertion loop and service the interrupt after we've released
the buffers.  If it was indeed a query cancel, that's the end of
the matter.  If it was a non-canceling interrupt reason, make use
of the existing provision to retry the whole insertion.  (This isn't
as wasteful as it might seem, since any upper-level index tuples we
already created should be usable in the next attempt.)

While there's no known instance of such a bug in existing release
branches, it still seems like a good idea to back-patch this to
all supported branches, since the behavior is fairly nasty if a
loop does happen --- not only is it uncancelable, but it will
quickly consume memory to the point of an OOM failure.  In any
case, this code is certainly not working as intended.

Per report from Dilip Kumar.

Discussion: https://postgr.es/m/CAFiTN-uxP_soPhVG840tRMQTBmtA_f_Y8N51G7DKYYqDh7XN-A@mail.gmail.com
src/backend/access/spgist/spgdoinsert.c