find_all_inheritors() with NoLock fails on concurrently dropped sub-partitions

From: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: find_all_inheritors() with NoLock fails on concurrently dropped sub-partitions
Date: 2026-10-12 20:49:47
Message-ID: CAN4CZFMp2oRXb_AC6+6+NxdjPHE2DJHFz_5WTpm99ovjKD9xTA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello hackers

find_all_inheritors() with NoLock doesn't lock anything below the
given relation, but it still calls find_inheritance_children() on
every relation it found, which starts with has_subclass(), and that
errors out if the pg_class row is gone:

ERROR: cache lookup failed for relation 16398

This needs a sub-partitioned table below the first level (root -> p1
-> p2, we hold a lock on root only, DROP TABLE p2 only locks p1 and
p2) to be dropped after it was found, but before it is expanded.

There are 5 callers using NoLock: VACUUM, ANALYZE, REPACK/CLUSTER and
LOCK TABLE on a partitioned table, and pg_get_publication_tables().

Dropping old sub-partitions while something vacuums the root seems
like a realistic way to hit it, the window grows with the number of
partitions.

The attached patch makes find_inheritance_children_extended() return
an empty list if the relation is gone, instead of erroring out.
It also adds an injection point before the pg_inherits scan, and a TAP
test which fails without the fix.

Found while working on the REPACK foreign partition warning patch [1].

[1]: https://www.postgresql.org/message-id/CAN4CZFM1g0oDs5Ee_5OWzf3YF-01rQDAak6ut_OWxEJJ%2BUmnXA%40mail.gmail.com

Attachment Content-Type Size
v1-0001-Fix-find_all_inheritors-with-NoLock-against-concu.patch application/octet-stream 6.2 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message surya poondla 2026-10-12 20:50:15 Re: Incremental backups report progress as if they were full backups
Previous Message Zsolt Parragi 2026-10-12 20:28:09 Re: Proposal: new file format for hba/ident/hosts configuration?