Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k30Es-0002nw-8a for pgsql-hackers@arkaria.postgresql.org; Tue, 04 Aug 2020 16:56:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k30Eq-00069V-Ty for pgsql-hackers@arkaria.postgresql.org; Tue, 04 Aug 2020 16:56:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k30Eq-00069O-NM for pgsql-hackers@lists.postgresql.org; Tue, 04 Aug 2020 16:56:32 +0000 Received: from mail-qt1-x844.google.com ([2607:f8b0:4864:20::844]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1k30En-0001Ik-VM for pgsql-hackers@lists.postgresql.org; Tue, 04 Aug 2020 16:56:32 +0000 Received: by mail-qt1-x844.google.com with SMTP id v22so25519505qtq.8 for ; Tue, 04 Aug 2020 09:56:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=Igs5PtWkguG5EkoF3rByDpMrT17C1hhlnZOBA5qMevo=; b=BjVZ1ZEegRkW7QbSCGktf1OCwOel42msROmpbo9A8DNmV8eQzrLZgBpnnxpo6o8WWo EUG4TTulyvvZUv24Ob/lc6Ejwm3RTns8mzpasrGdHuHcKH/LOaF/Swscdd+UqYla8Oub i9muHzMRANI5zqPfDbxkN3gzb/b6F0bX6u/lIC1tbl0V7ccJoB11B0EfUMo52BvWeVFq FLl0+yQwzUJ21mOonr/whJs75yKACiyS2JERd/6JEaTVekeYvO26KhreQObPkuYof/FZ fdyYLjy9cg0SpI4DonJ7kSsUR+az/Syp0/1JnOf3eipePwPLW+NNE9832wjfiDRkh7Xo Aojw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=Igs5PtWkguG5EkoF3rByDpMrT17C1hhlnZOBA5qMevo=; b=s2+GXNI8gVMN7MYHcAfVWVFChCDrprTBw6gwEEkXD/uzTLpHJxkiupzKIVoEo43rBG t2REjrKGkDgDDsIUxsTHi1nuMsn8N3Hvd/yUFt6nUmynNGgDVb1q4ZeP0MXIe5GzR7FC DU4ceQm9gICvv8P8ea6kkwCXtTd8mPi04I5SJa2UcJ0WE5+UcyGd9BRxZ3J9aoa4qzCx KIl9jJiQ1esiqxztDTPwhEHojXGUaoUDs5aK8/Gr8A19L05J8cRuaxSLBBscrNccZ5XN f0lVltq3oV11FMLtcYvaiparL2kMx081AE2Bosp4aGepzeygi4PJnRaOequyM/0DtaSO 4JXw== X-Gm-Message-State: AOAM531LWCtZn6Glbb7O3vnXe8uHCvgjQ7kSJtLtrZQkMeqXewEXV4if K0RKqmh0MnLelxRcyFaSgDo48porS37DGfc5hUTVHLxNYkebi9jECz5iXUXB14o67HsHcMUOMvf nBKPPnHVfz1qSHrVLuRH5ukV0W7UHHunHBpL43sZr7J9AO148utSEW62SEF4BukrzUNF3x/js4G jadWsY8G4Dv1RvNQ== X-Google-Smtp-Source: ABdhPJxCeNZ5s6gF4kKnpq+ti8c1HDWNFPVrdtYwwWtvxtBbFhFj1vqzYaGkrDkPCAxRr5oAaaN3Ow== X-Received: by 2002:ac8:470e:: with SMTP id f14mr22774572qtp.380.1596560187904; Tue, 04 Aug 2020 09:56:27 -0700 (PDT) Received: from perhan.alvh.no-ip.org ([190.95.19.47]) by smtp.gmail.com with ESMTPSA id k24sm25816699qtb.26.2020.08.04.09.56.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Aug 2020 09:56:27 -0700 (PDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 80F302A0C96; Tue, 4 Aug 2020 12:56:25 -0400 (-04) Date: Tue, 4 Aug 2020 12:56:25 -0400 From: Alvaro Herrera To: Pg Hackers Subject: Re: ALTER TABLE .. DETACH PARTITION CONCURRENTLY Message-ID: <20200804165625.GA18486@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20200803234854.GA24158@alvherre.pgsql> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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