Re: ALTER TABLE .. DETACH PARTITION CONCURRENTLY
Alvaro Herrera <alvherre@2ndquadrant.com>
From: Alvaro Herrera <alvherre@2ndquadrant.com>
To: Pg Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2020-08-04T16:56:25Z
Lists: pgsql-hackers
On 2020-Aug-03, Alvaro Herrera wrote: > Why two transactions? The reason is that in order for this to work, we > make a catalog change (mark it detached), and commit so that all > concurrent transactions can see the change. A second transaction waits > for anybody who holds any lock on the partitioned table and grabs Access > Exclusive on the partition (which now no one cares about, if they're > looking at the partitioned table), where the DDL action on the partition > can be completed. I forgot to mention. If for whatever reason the second transaction fails, (say the user aborts it or there is a crash), then the partition is still marked as detached, so no queries would see it; but all the ancillary catalog data remains. Just like when CREATE INDEX CONCURRENTLY fails, you keep an invalid index that must be dropped; in this case, the changes to do are much more extensive, so manual action is out of the question. So there's another DDL command to be invoked, ALTER TABLE parent DETACH PARTITION part FINALIZE; which will complete the detach action. If we had UNDO then perhaps it would be possible to register an action so that the detach is completed automatically. But for now this seems sufficient. Another aspect worth mentioning is constraints. In the patch, I create a CHECK constraint to stand for the partition constraint that's going to logically disappear. This was mentioned as a potential problem in one of Robert's emails (I didn't actually verify that this is a problem). However, a funny thing is that if a constraint already exists, you get a dupe, so after a few rounds of attach/detach you can see them pile up. I'll have to fix this at some point. But also, I need to think about whether foreign keys have similar problems, since they are also used by the optimizer. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Commits
-
Make detach-partition-concurrently-4 less timing sensitive
- eb43bdbf5104 14.0 landed
-
Improve documentation on DETACH PARTITION lock levels
- db6e1aeb952e 14.0 landed
-
Track detached partitions more accurately in partdescs
- 3fe773b14975 14.0 landed
-
Allow a partdesc-omitting-partitions to be cached
- d6b8d29419df 14.0 landed
-
Fix relcache inconsistency hazard in partition detach
- 8aba9322511f 14.0 landed
-
Don't add a redundant constraint when detaching a partition
- 7b357cc6ae55 14.0 landed
-
ALTER TABLE ... DETACH PARTITION ... CONCURRENTLY
- 71f4c8c6f74b 14.0 landed
-
Let ALTER TABLE Phase 2 routines manage the relation pointer
- cd03c6e94b09 14.0 landed
-
Check default partitions constraints while descending
- f481d2823297 14.0 landed
- ef1e1250e716 12.5 landed
- d0230a43fcae 13.0 landed