Rebuild partition descriptors from the previous one

From: Manu <manuelreyesbravo(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Cc: Amit Langote <amitlangote09(at)gmail(dot)com>, Alvaro Herrera <alvherre(at)kurilemu(dot)de>, Robert Haas <robertmhaas(at)gmail(dot)com>, David Rowley <dgrowleyml(at)gmail(dot)com>, David Geier <geidav(dot)pg(at)gmail(dot)com>, Rahila Syed <rahilasyed90(at)gmail(dot)com>
Subject: Rebuild partition descriptors from the previous one
Date: 2026-12-06 05:01:36
Message-ID: 179653329617.2947974.9284587075136868741@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

With the attached series, rebuilding a partition descriptor after an
ATTACH, DETACH, CREATE ... PARTITION OF or DROP of a partition reads,
converts and sorts only the bounds of the partitions that changed,
not all of them. Restoring the schema of a table with 10,000 list
partitions on a text key goes from 271.1 s to 16.7 s. The price is a
per-backend copy of the bound info of each partitioned table in use,
with no limit; that is the open question below.

Today each of those commands invalidates the parent's relcache entry,
and the next use of the table in every backend rebuilds the
descriptor from scratch: it reads relpartbound of every partition,
converts it from text and sorts all the bounds. A dump restore of N
partitions thus does O(N^2) work, and every other session using the
table pays O(N) after each such command.

0001 keeps, per backend, a record of the last descriptor built for
each partitioned table: its partitions and a copy of its bound info.
A change of a partition's bounds or membership updates its pg_class
row; a relcache callback marks that partition dirty in the records.
A rebuild still reads pg_inherits, takes the clean partitions from the
record and reads only the others. The record is a private copy and
the relcache's descriptor is built and freed as today, so nothing
needs reference counting (a copy is one of the two ways Tom named in
[1]). A descriptor built while invalidations arrived is not
recorded, so the race of bug #19690 [2] does not get worse.
0002 sorts only the new bounds into the known ones.
0003 adds a test for a cache reset while records are released.

Times on master 7451263c13c, without assertions:
- restore, 10,000 partitions: list on text 271.1 s -> 16.7 s, range
on int 119.0 s -> 12.7 s, hash on int 30.4 s -> 11.5 s
- another session's first point query after one partition DDL, 10,000
partitions: list 11.31 ms -> 1.31 ms, range 13.21 ms -> 1.08 ms,
hash 3.91 ms -> 0.91 ms
The rebase to 8d90516d76d does not touch the measured code.

Memory: a record takes 1.24 to 1.26 times its descriptor, e.g.
461,544 against 371,400 bytes for 10,000 range partitions on int.
The record of a dropped table is released at the backend's next
commit; a backend that held the records of 200 tables dropped by
another session has 0 bytes left for them after its next statement.

Correctness: a lab build (not attached) compared every descriptor
built during check-world with one built from scratch, bounds, order
and the set of partitions: 0 differences. In that build, mutants that
never mark a partition dirty, take a wrong range bound, map a list
value to the wrong partition, list a partition twice or free a live
table's record each fail the isolation test of 0001. The test of
0003 crashes a version of 0001 that left the pending flag set (signal
11 at the next commit) and passes with this one. check-world passes
with each prefix of the series, with assertions, TAP and injection
points.

Open question: records of tables that still exist have no limit. A
backend keeps one per partitioned table it has used, for its life,
since that is what makes the next rebuild cheap; DISCARD ALL does not
release them either, as it does not flush the relcache. Is that
acceptable, or should there be a cap, at the cost of full rebuilds for
the tables that fall out of it?

CF #7483 (David) moves rd_partdesc into RelationPartitionInfo; this
series does not touch those fields, and both apply on 8d90516d76d.

[1] https://postgr.es/m/23284.1555263536@sss.pgh.pa.us
[2] https://postgr.es/m/19690-5619e7d182d18d5d@postgresql.org

Regards,
Manu

Attachment Content-Type Size
v4-0001-Rebuild-partition-descriptors-from-the-previous-o.patch text/x-patch 43.3 KB
v4-0002-Sort-only-the-new-partition-bounds-into-the-previ.patch text/x-patch 6.4 KB
v4-0003-Test-a-cache-reset-while-releasing-partition-desc.patch text/x-patch 6.2 KB
measurements.txt text/plain 2.2 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Manu 2026-12-06 05:45:33 Re: Progress reporting: a debug trace and a test framework
Previous Message Raja Sai pranav 2026-12-06 04:40:36 Re: pg_receivewal doesn't truncate compressed WAL files on restart