Re: pg_dump --if-exists --clean when drop index that is partition of a partitioned index
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: jian he <jian.universality@gmail.com>
Cc: Pavel Stehule <pavel.stehule@gmail.com>,
PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2025-04-15T22:51:55Z
Lists: pgsql-hackers
Attachments
- v1-fix-pg_dump-index-drop-order.patch (text/x-diff) patch v1
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
Commits
-
Fix pg_dump --clean with partitioned indexes.
- 1fc3403626d4 18.0 landed
- a25f21d99adf 16.9 landed
- 7144cd53821f 15.13 landed
- 6a3e57865fc0 13.21 landed
- 3424c1075e4a 17.5 landed
- 148cc0105dce 14.18 landed