Skip to content

executor: give a partitioned table to attach the parent's indexes - #713

Draft
Kiran01bm wants to merge 1 commit into
kiran01bm/attach-partitioned-defaultfrom
kiran01bm/attach-partitioned-indexes
Draft

Kiran01bm wants to merge 1 commit into
kiran01bm/attach-partitioned-defaultfrom
kiran01bm/attach-partitioned-indexes

Conversation

@Kiran01bm

Copy link
Copy Markdown
Collaborator

The online attach's index step now gives a table that is itself partitioned the parent's indexes, built concurrently, so the attach of such a table builds no index.

Why

When pg-sprite attaches a partition online, it first gives the new table every index of the parent, built concurrently while writes go on, because the ATTACH would otherwise build them while it blocks the table. For a table that is itself partitioned, the ATTACH looks for a valid partitioned index on that table to pair with each parent index. If it finds none, it builds the index on every partition of the table, all under ACCESS EXCLUSIVE. Until now the index step refused a partitioned table outright. This PR gives it those indexes ahead of time, one step closer to attaching a sub-partitioned table online. The attach flow still refuses one until the foreign key step follows.

What

  • For each parent index that backs no constraint and that nothing on the table pairs with, the step runs the existing partitioned index build on the table itself, under the derived name <index>_p<table oid>. The build creates the table's index ON ONLY, builds every leaf's index concurrently, attaches each level bottom up, and returns once the table's index is valid. A rerun resumes an index a stop left invalid.
  • The pairing reads a partitioned table's own (partitioned) indexes too. Probes on PostgreSQL 14 and 18 confirm the ATTACH pairs a valid one and passes over an invalid one, building the index across the whole tree. The pairing already skipped invalid indexes.
  • A parent PRIMARY KEY or UNIQUE constraint is taken when the table already has the matching key. Otherwise it is refused with attach-partition-index-unsupported before anything is built; the key build follows in the next PR.
  • The partitioned build's refusals reach the step: a foreign table below the table, and a unique index that does not cover a partition key in the table's tree.
  • A standalone table's proof now checks the kind it was resolved as, so a partitioned table is never taken for a plain one.

Before / after

Take visits, indexed on id and lower(page), and visits_2027, partitioned by region into visits_2027_eu and visits_2027_us, which is split again by id. The index step runs on visits_2027 before the attach. Before, it refused the table; after, it builds both indexes across the tree without blocking writes, and the ATTACH takes them.

Before
  ensureAttachIndexes(visits → visits_2027)
    └─ visits_2027 is of kind 'p'  ──▶  refused, nothing built
  ATTACH visits_2027 run as written
    └─ builds visits_id_idx and visits_page_idx on all 3 leaves under ACCESS EXCLUSIVE

After
  ensureAttachIndexes(visits → visits_2027)
    ├─ visits_id_idx   ──▶  partitioned build of visits_id_idx_p<oid> on visits_2027
    │     ├─ CREATE INDEX ... ON ONLY visits_2027 and ON ONLY visits_2027_us
    │     ├─ CREATE INDEX CONCURRENTLY on _eu, _us_low, _us_high (writes go on)
    │     └─ ATTACH each level bottom up  ──▶  valid
    ├─ visits_page_idx ──▶  the same, for lower(page)
    └─ re-read: the ATTACH pairs both, builds nothing
  ATTACH visits_2027
    └─ every index on all 5 tables is the one the step gave it, now attached

The attach index step now gives a table that is itself partitioned a valid
partitioned index for each of the parent's plain indexes, by running the
partitioned index build on it, so the ATTACH pairs them and builds nothing.
A parent key constraint is taken only when the table already has it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant