Re: pg_dump --if-exists --clean when drop index that is partition of a partitioned index

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: jian he <jian(dot)universality(at)gmail(dot)com>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: pg_dump --if-exists --clean when drop index that is partition of a partitioned index
Date: 2025-04-15 22:51:55
Message-ID: 1795813.1744757515@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

After thinking about this a bit more: the problem is basically one
of things being done in the wrong order. And the way to ensure
that things get done in the right order is to set up the object
dependencies correctly. So what we need to do is to switch the
dependency direction between the child and parent index objects,
as attached.

The effect of this is to switch both the creation order and the
drop order of the child and parent indexes. Normally, flipping
the creation order would be problematic. But for partitioned
indexes it doesn't matter because there's not actually any
connection between the child and parent indexes until we issue
INDEX ATTACH. The INDEX ATTACH object has dependencies on both,
so it doesn't come out till after both, so all that still works.

However ... this is still only a partial fix. It fixes the
problem as-presented, where you used both --clean and --if-exists.
But if you just say --clean, then the DROP command for the child
index fails because it's already gone thanks to dropping the
parent index first. Possibly the least messy way to deal with
that is to always put IF EXISTS into the DROP command for a
child index. This isn't totally problem-free, because then we
would have to teach RestoreArchive to not insert IF EXISTS if
one is already there, and that probably makes the fix not
back-patchable for fear of compatibility problems with unpatched
copies of pg_restore.

Or we could do what Jian suggested and just not emit any dropStmt
for child indexes. I continue to fear that that will have
undesirable side-effects, but I have to admit that I'm not sure
what. The fact that the backend will automatically drop the
child index if we drop either its parent index or its table
seems to cover a lot of the edge cases ... but does it cover
all of them?

regards, tom lane

Attachment Content-Type Size
v1-fix-pg_dump-index-drop-order.patch text/x-diff 1.4 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jeff Davis 2025-04-15 23:08:55 Re: [18] Unintentional behavior change in commit e9931bfb75
Previous Message Nikolay Samokhvalov 2025-04-15 22:27:56 Re: Built-in Raft replication