Skip to content

Unexpected reindex when altering column types for partitioned tables - #411

Open
pg-hub-mirror[bot] wants to merge 3 commits into
masterfrom
pg-hub/mirror-patch-ac4f1d5417ee2755
Open

pg-hub-mirror[bot] wants to merge 3 commits into
masterfrom
pg-hub/mirror-patch-ac4f1d5417ee2755

Conversation

@pg-hub-mirror

@pg-hub-mirror pg-hub-mirror Bot commented Sep 17, 2026

Copy link
Copy Markdown

Read-only mirror. Reply and review on pgsql-hackers; activity here is not sent upstream.

  • Original author: Álvaro Rodríguez <alvaro(at)datadoghq(dot)com>
  • Mailing list: pgsql-hackers
  • Message-ID: CA+C_kKUgzQaq=ngDy0cJDBW1XVTPgB5yGqF5cjP7Dk=VTA=sRw@mail.gmail.com
  • Original email

Patch files:


Hey Alberto,

Besides this specific problem: there are currently multiple active
threads related to bugs with recursive alter table. I would suggest
specifically to read through [0], all of it because v19 is very
different than v1. The patch coming out of that thread partially
overlaps with this one. It tries to fix properties being lost in
recursive alter table operations that cause index rebuilds, both
properties which could be recreated with alter table commands and
properties which couldn't.
Thanks for the review, and for the suggestion, I'll definitely take a
look at that discussion. At first glance it looks very relevant!
Maybe a good solution here would be to try and process the tables in the
opposite order when recreating object than the order in which we deleted
them? In which case we (possibly) wouldn't even need a new separate
pass. But I didn't look further into this. What do you think?
This was my original idea. I kind of discarded it originally because
the two pass approach seemed easier to implement with the current
logic, and I didn't want to mess up too much with the code to minimize
potential side effects. But I forgot that multi-level partitions were
a thing, so we might need to go back and revisit that. It definitely
looked like a valid approach too. I'll see if I can come up with
something.
I forgot to mention: the patch doesn't apply using git am and needs a
rebase (I applied it using patch -p1 after editing the diff). If you're
still interested in working on this, please provide a rebased patch, as
it makes the process smoother :)
Yup! I was aware of that but forgot to update the thread, please find
the rebased versions attached.
Thanks!
Álvaro

…fe' alter column types on partitioned tables
…or partitioned tables

During ALTER TABLE ALTER COLUMN TYPE for a partitioned table with an index, the old deletion behavior:
1. Processes the parent table in ATRewriteCatalogs before processing any of its child tables
2. Runs ATPostAlterTypeCleanup for the table, which drops the partitioned index at the end. That
drop cascades to the child indexes.
3. Processes the child tables in ATRewriteCatalogs. However, the child indexes were already dropped,
so we never remember them in RememberAllDependentForRebuilding.
4. When indexes are eventually recreated, they always need to be reindexed.

This change batches the deletions in ATRewriteCatalogs so that they happen at the end of a processing
step and not in each call to ATPostAlterTypeCleanup (which happens for each processed table). This
ensures that child indexes are tracked when ATExecAlterColumnType calls RememberAllDependentForRebuilding
for each child table.

After the change, child indexes are now recreated twice: once when the parent index is created, and once
by themselves. We need for the child indexes to be created first. That way, they use the remembered
information to decide whether a reindex is needed. When the parent index is created, it will automatically
use existing child indexes. To enforce this behavior, we add a new AT_PASS_OLD_PARTITIONED_INDEX that
recreates the parent indexes.
… TABLE ALTER COLUMN TYPE for partitioned tables

This is a follow-up to the same fix for non-constraint indexes. Just as in there, the parent constraint is dropped
first, and cascades to the child constraints, so that the associated indexes cannot be recreated properly.

We remove the condition that preented child constraints from being re-added. However, this causes the constraints
to be added twice (once when the parent is recreated, and once when the constraints themselves are recreated). We
need to make sure that this happens in the right order, so we first create the child constraints (reusing the index
if possible), and then the parent constraint, which should automatically reuse all child constraints and indexes.
@pg-hub-mirror

pg-hub-mirror Bot commented Sep 17, 2026

Copy link
Copy Markdown
Author

Earlier design discussion: Discussion #317

@pg-hub-mirror pg-hub-mirror Bot added area:storage Storage, access methods, buffers, or I/O source:pgsql-hackers Mirrored from pgsql-hackers type:patch Mail thread contains a PostgreSQL patch area:sql SQL language or commands labels Sep 17, 2026
@pg-hub-mirror pg-hub-mirror Bot locked and limited conversation to collaborators Sep 17, 2026
@pg-hub-mirror pg-hub-mirror Bot added cf:pg20-2 PostgreSQL CommitFest type:performance Performance-related patch status:waiting-on-author CommitFest: Waiting on Author labels Sep 17, 2026
@pg-hub-mirror pg-hub-mirror Bot added this to the PG20-2 milestone Sep 17, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area:sql SQL language or commands area:storage Storage, access methods, buffers, or I/O cf:pg20-2 PostgreSQL CommitFest source:pgsql-hackers Mirrored from pgsql-hackers status:waiting-on-author CommitFest: Waiting on Author type:patch Mail thread contains a PostgreSQL patch type:performance Performance-related patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant