| From: | Manu <manuelreyesbravo(at)gmail(dot)com> |
|---|---|
| To: | pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Cc: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, David Rowley <dgrowleyml(at)gmail(dot)com>, Nathan Bossart <nathandbossart(at)gmail(dot)com> |
| Subject: | Avoid quadratic behavior in MergeConstraintsIntoExisting() |
| Date: | 2026-12-05 05:21:23 |
| Message-ID: | 179644808358.206883.15402438759779843470@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
The attached v1 reduces the median ATTACH PARTITION time from
276.664 ms to 29.837 ms with 1,600 NOT NULL columns.
ALTER TABLE ... INHERIT and ALTER TABLE ... ATTACH PARTITION call
MergeConstraintsIntoExisting(). For each CHECK or NOT NULL constraint
on the parent, the function scans the child's pg_constraint rows.
Since 14e87ffa5c5, each NOT NULL column has a pg_constraint row,
which makes this scan expensive on wide partitioned tables.
The patch looks up CHECK constraints by name using the existing
pg_constraint index. It reads the child's NOT NULL constraints once
and indexes them by the parent's attribute number.
I measured ATTACH with a child created using LIKE, a warm-up ATTACH,
synchronous_commit=off, and 20 rounds per width with alternating
build order. The baseline was c1dd701fb5f.
NOT NULL columns | Master (ms) | v1 (ms)
25 | 0.620 | 0.565
50 | 1.443 | 1.149
100 | 2.907 | 1.868
200 | 8.491 | 4.252
400 | 23.502 | 7.434
800 | 77.871 | 15.603
1600 | 276.664 | 29.837
Doubling the width from 800 to 1,600 columns increased the median
by 3.55x on master and 1.91x with v1.
For pg_upgrade --copy --no-sync from 18.6, v1 reduced total user-space
instructions by 15.1% on a synthetic schema with 1,000 partitions and
200 NOT NULL columns. On the smaller tier of David's schema [1], the
median paired reduction was 0.04% across three rounds (0.02% to 0.22%).
In a separate schema-restore profile per build, the inclusive share
of instruction samples in MergeConstraintsIntoExisting() went from
1.15% to 0.72% on that schema.
I also compared master and v1 on 25,000 generated inheritance and
partitioning cases. The error messages and resulting catalog rows
matched in every case, including 5,000 with ignore_system_indexes=on.
The attachments include the patch, its SHA-256 checksum, benchmark
scripts, and per-run data for the ATTACH curve and the four-build
comparison on David's schema.
[1] https://postgr.es/m/CAApHDvqwNJMaQoWH31D8qow9PozivzOwD17VSgfpEZDmChTsqQ@mail.gmail.com
Regards,
Manu
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Avoid-quadratic-behavior-in-MergeConstraintsIntoE.patch | text/x-patch | 13.8 KB |
| measurements.txt | text/plain | 4.9 KB |
| attach-bench.sh.txt | text/plain | 3.7 KB |
| pg_upgrade-bench.sh.txt | text/plain | 3.3 KB |
| restore-split.sh.txt | text/plain | 2.6 KB |
| oracle.py.txt | text/plain | 11.3 KB |
| patch-sha256.txt | text/plain | 130 bytes |
| attach-curve.tsv.txt | text/plain | 12.1 KB |
| rowley-rounds.tsv.txt | text/plain | 620 bytes |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | shihao zhong | 2026-12-05 05:22:27 | Re: [PATCH] Allow NOTIFY without cross-transaction commit ordering |
| Previous Message | shihao zhong | 2026-12-05 05:20:00 | Re: PostgreSQL crash on reference integrity check with concurrent DDL |