Re: ALTER TABLE .. DETACH PARTITION CONCURRENTLY
Alvaro Herrera <alvherre@2ndquadrant.com>
From: Alvaro Herrera <alvherre@2ndquadrant.com>
To: Robert Haas <robertmhaas@gmail.com>
Cc: Pg Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2020-09-01T18:15:27Z
Lists: pgsql-hackers
On 2020-Aug-27, Robert Haas wrote: > On Wed, Aug 26, 2020 at 7:40 PM Alvaro Herrera <alvherre@2ndquadrant.com> wrote: > > To mark it detached means to set pg_inherits.inhdetached=true. That > > column name is a bit of a misnomer, since that column really means "is > > in the process of being detached"; the pg_inherits row goes away > > entirely once the detach process completes. This mark guarantees that > > everyone will see that row because the detaching session waits long > > enough after committing the first transaction and before deleting the > > pg_inherits row, until everyone else has moved on. > > OK. Do you just wait until the XID of the transaction that set > inhdetached is all-visible, or how do you do it? I'm just doing WaitForLockers( ... AccessExclusiveLock ...) on the partitioned table at the start of the second transaction. That will wait until all lockers that have obtained a partition descriptor with the old definition are gone. Note we don't actually lock the partitioned table with that lock level. In the second transaction we additionally obtain AccessExclusiveLock on the partition itself, but that's after nobody sees it as a partition anymore. That lock level is needed for some of the internal DDL changes, and should not cause problems. I thought about using WaitForOlderSnapshots() instead of waiting for lockers, but it didn't seem to solve any actual problem. Note that on closing the first transaction, the locks on both tables are released. This avoids the deadlock hazards because of the lock upgrades that would otherwise occur. This means that the tables could be dropped or changed in the meantime. The case where either relation is dropped is handled by using try_relation_open() in the second transaction; if either table is gone, then we can just mark the operation as completed. This part is a bit fuzzy. One thing that should probably be done is have a few operations (such as other ALTER TABLE) raise an error when run on a table with inhdetached=true, because that might get things out of step and potentially cause other problems. I've not done that yet. > So all the plans that were created before you set > inhdetached=true have to be guaranteed to be invaliated or gone > altogether before you can actually delete the pg_inherits row. It > seems like it ought to be possible to ensure that, though I am not > surely of the details exactly. Is it sufficient to wait for all > transactions that have locked the table to go away? I'm not sure > exactly how this stuff interacts with the plan cache. Hmm, any cached plan should be released with relcache inval events, per PlanCacheRelCallback(). There are some comments in plancache.h about "unsaved" cached plans that I don't really understand :-( -- Á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