| From: | Manu <manuelreyesbravo(at)gmail(dot)com> |
|---|---|
| To: | Nathan Bossart <nathandbossart(at)gmail(dot)com> |
| Cc: | Alvaro Herrera <alvherre(at)kurilemu(dot)de>, pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Subject: | Internal errors when a concurrent DROP or DETACH wins the race in RangeVar callbacks |
| Date: | 2026-12-05 23:09:24 |
| Message-ID: | 179651216486.703690.16317689448096429205@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
Several RangeVarGetRelidExtended() callbacks look the object up in the
catalog before the lock is taken, without missing_ok. If another
session drops or detaches it in that window, the statement fails with
an internal error instead of the error a serial run gives. The six
cases of the attached script reproduce deterministically: it stops the
first session in its callback with gdb (no code change) and runs the
second statement while it is stopped:
DROP TABLE p1, against DETACH PARTITION p1 of its parent:
ERROR: could not find tuple for parent of relation 16387
(serially: the DROP succeeds)
DROP TABLE p1, against DROP TABLE p1, or DROP TABLE of the parent:
ERROR: could not find tuple for parent of relation 16387
(serially: table "p1" does not exist)
REINDEX INDEX i, against DROP INDEX i:
ERROR: cache lookup failed for index 16387
or, if the DROP lands before the relkind lookup:
ERROR: "i" is not an index
(serially: relation "i" does not exist)
ALTER INDEX pi ATTACH PARTITION ci, against DROP INDEX ci:
ERROR: cache lookup failed for index 16391
(serially: relation "ci" does not exist)
The DROP TABLE cases are in RangeVarCallbackForDropRelation(): it reads
relispartition from the syscache and then calls get_partition_parent(),
which raises elog(ERROR) when the pg_inherits row is gone, although the
caller tests the result with OidIsValid() as if it could be invalid.
Two sessions dropping the same partition at the same time are enough;
the window is narrow, and I have only hit it with gdb. The index
case right above it already passes missing_ok = true to
IndexGetRelation().
Alvaro, c5a0f8332ec does not cover the last case: it rechecks in
DefineIndex() after locking, but this window is in
RangeVarCallbackForAttachIndex(), before the index is locked; it still
fails on master 91d71a71cbe.
Nathan, the REINDEX INDEX case is new in v19. 079480dc202 replaced the
early returns in RangeVarCallbackForReindexIndex() with errors that
should be unreachable; a concurrent DROP INDEX reaches them. Before
that commit, 15 to 18 do not fail there but emit
WARNING: you don't own a lock of type ShareLock
before the right error, because on the retry the callback unlocks a
table it never locked, which I think is the subtle breakage your
commit message warns about.
The attached patch:
- adds get_partition_parent_extended(relid, even_if_detached,
missing_ok), with get_partition_parent() as a wrapper, and uses it
with missing_ok in RangeVarCallbackForDropRelation();
- RangeVarCallbackForReindexIndex(): returns again when the index is
gone (relkind or parent table lookup), and only unlocks the
previous table if one was locked;
- RangeVarCallbackForAttachIndex(): IndexGetRelation(relOid, true).
get_partition_ancestors() would avoid the new function, but it stops
at a pending detach, and DROP of a partition pending detach must still
lock the parent.
With the patch every case gives the serial error, the serial controls
are unchanged, and make check (243 tests) and the isolation suite
(143) pass, on master 91d71a71cbe with --enable-cassert. Without the
patch, the DROP TABLE and ATTACH PARTITION cases fail as above on that
master and on 15.19, 16.15, 17.11, 18.6 and 19rc1, and the REINDEX
cases as described above; measurements-v2.txt has every run.
The patch applies to master and REL_19_STABLE. In 15 to 18, which
lack 079480dc202, two of its three hunks in indexcmds.c do not apply;
I have not prepared versions for those branches yet. I have not
written a regression test either: it would need an injection point in
each callback, and I can add those if that is wanted.
The script's header has its usage; NOGDB=1 runs the serial control.
Regards,
Manu
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Tolerate-concurrently-dropped-objects-in-RangeVar.patch | text/x-patch | 7.0 KB |
| concurrent-drop-race.sh.txt | text/plain | 4.0 KB |
| measurements-v2.txt | text/plain | 4.3 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Tomas Vondra | 2026-12-05 23:45:23 | Re: Making type Datum be 8 bytes everywhere |
| Previous Message | Zsolt Parragi | 2026-12-05 23:04:55 | Re: Standby writes pages past minRecoveryPoint after crash recovery |