Avoid quadratic behavior in MergeConstraintsIntoExisting()

Started by Manuabout 19 hours ago1 messageshackers
Jump to latest
#1Manu
manuelreyesbravo@gmail.com

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]/messages/by-id/CAApHDvqwNJMaQoWH31D8qow9PozivzOwD17VSgfpEZDmChTsqQ@mail.gmail.com, 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]: /messages/by-id/CAApHDvqwNJMaQoWH31D8qow9PozivzOwD17VSgfpEZDmChTsqQ@mail.gmail.com

Regards,
Manu

Attachments:

t260304_1
v1-0001-Avoid-quadratic-behavior-in-MergeConstraintsIntoE.patchtext/x-patchDownload+144-109
measurements.txttext/plainDownload
attach-bench.sh.txttext/plainDownload
pg_upgrade-bench.sh.txttext/plainDownload
restore-split.sh.txttext/plainDownload
oracle.py.txttext/plainDownload
patch-sha256.txttext/plainDownload
attach-curve.tsv.txttext/plainDownload
rowley-rounds.tsv.txttext/plainDownload