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 1kyj04-0007BY-DI for pgsql-hackers@arkaria.postgresql.org; Sun, 10 Jan 2021 22:15:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kyj02-0004nR-3x for pgsql-hackers@arkaria.postgresql.org; Sun, 10 Jan 2021 22:15:50 +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 1kyj01-0004nK-QE for pgsql-hackers@lists.postgresql.org; Sun, 10 Jan 2021 22:15:49 +0000 Received: from mail-io1-xd34.google.com ([2607:f8b0:4864:20::d34]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kyizx-0001O9-1Y for pgsql-hackers@lists.postgresql.org; Sun, 10 Jan 2021 22:15:48 +0000 Received: by mail-io1-xd34.google.com with SMTP id q137so15861592iod.9 for ; Sun, 10 Jan 2021 14:15:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=JwH4G91hTZnyLLb2enP6emwGgpn1lBVjphegj4B4z30=; b=RyEazInOO/tGklEjHUH0RFELZAKIR0RiUyB9oS+fJCNTAkRES7toC2NAevVs8TD3tD dtNDAgyZ7d9U1pEZHGTP+W+KV3OV+DKmT9HAucwkB9G8iON0/kSY1vpkxQrUeCJfeY1e ujXf/++bz4hmkLdVGDiOzvuDzJyopsbqf5C0Ca0PbuQE8LpzFO4d+tivjgT8nqqoXEW0 Y47xt4Q0yJO5D6c7mRqdNPTTU8QfWibFCIwss6uTLq5FOECs3abERvkGs9SbB7PL1/54 00rUwYaKkKWbseHI2EDF/ZslfZmOh96OF9P8MCffeassEMcrs+tA1go2dR7twjn0Umzo MYeQ== 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:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=JwH4G91hTZnyLLb2enP6emwGgpn1lBVjphegj4B4z30=; b=NaWtXF/TKAf5f9WMle7WGevpfKfr5pRy/qiDVS3bkPgmXJ5fVRN1egVeLvlSyvcEL+ t4Hcx6nTpwFU99r7RfkHT/CFgMIGWGMPuRZojnHmyXIN8ntOFf9QxEG052NxfOOiOB0h /ZzvUmU7URKmgmb82hOwUS5TE7S7Qi+m8J3O5TQLNju6n1axM/HTaTNMJMF7ondNawdB gI4277YwOFu7bFw6347EuVSnpko41/t1hEc16HJrgFV5arLzMWRNy2NtfdYyIUd8oEfD mzLUMxEnpkaxcHOTNvDoHuNJMJ/7+DzfvABsgfA4/oTOK9uTNlwzEsMXHbLEUNxLFSw7 lq8A== X-Gm-Message-State: AOAM532Gme99cQGr+Z90RfpJ4tTq2pqVZWABJ7jYeTJkQQGRgCmsegu0 GezvHqblHJTIwrEunNOLQqRPCQ== X-Google-Smtp-Source: ABdhPJw72pgZN+1J9x3GLs2G9pg+ULPNeE6iL+Ld2g3gwBI/zTElDApRIRMrQltGMMTKWc26Dfha7w== X-Received: by 2002:a02:c7d1:: with SMTP id s17mr12186672jao.142.1610316943161; Sun, 10 Jan 2021 14:15:43 -0800 (PST) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id n16sm13598120ilj.19.2021.01.10.14.15.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 10 Jan 2021 14:15:42 -0800 (PST) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 3C1FD800A3C; Sun, 10 Jan 2021 16:15:41 -0600 (CST) Date: Sun, 10 Jan 2021 16:15:41 -0600 From: Justin Pryzby To: Alvaro Herrera Cc: Amit Langote , pgsql-hackers@lists.postgresql.org Subject: Re: ALTER TABLE .. DETACH PARTITION CONCURRENTLY Message-ID: <20210110221541.GP1849@telsasoft.com> References: <20201201152519.GA17514@alvherre.pgsql> <20210108191433.GA14549@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="kR3zbvD4cgoYnS/6" Content-Disposition: inline In-Reply-To: <20210108191433.GA14549@alvherre.pgsql> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --kR3zbvD4cgoYnS/6 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Fri, Jan 08, 2021 at 04:14:33PM -0300, Alvaro Herrera wrote: > > > I ended up with apparently broken constraint when running multiple loops around > > > a concurrent detach / attach: > > > > > > while psql -h /tmp postgres -c "ALTER TABLE p ATTACH PARTITION p1 FOR VALUES FROM (1)TO(2)" -c "ALTER TABLE p DETACH PARTITION p1 CONCURRENTLY"; do :; done& > > > while psql -h /tmp postgres -c "ALTER TABLE p ATTACH PARTITION p1 FOR VALUES FROM (1)TO(2)" -c "ALTER TABLE p DETACH PARTITION p1 CONCURRENTLY"; do :; done& > > > > > > "p1_check" CHECK (true) > > > "p1_i_check" CHECK (i IS NOT NULL AND i >= 1 AND i < 2) > > > > Not good. > > Haven't had time to investigate this problem yet. I guess it's because you commited the txn and released lock in the middle of the command. -- Justin --kR3zbvD4cgoYnS/6 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-fix.notapxtch" From e18c11fd5bcc4f5cd981a3219383265b55974f34 Mon Sep 17 00:00:00 2001 From: Justin Pryzby Date: Sun, 10 Jan 2021 15:41:43 -0600 Subject: [PATCH] fix --- src/backend/commands/tablecmds.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index d7b9c63e5f..144c27c303 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -17131,6 +17131,7 @@ ATExecDetachPartition(List **wqueue, AlteredTableInfo *tab, Relation rel, Oid partrelid, parentrelid; LOCKTAG tag; + LockRelId partlockrelid; char *parentrelname; char *partrelname; @@ -17162,6 +17163,10 @@ ATExecDetachPartition(List **wqueue, AlteredTableInfo *tab, Relation rel, table_close(rel, NoLock); tab->rel = NULL; + partlockrelid.relId = parentrelid; + partlockrelid.dbId = MyDatabaseId; + LockRelationIdForSession(&partlockrelid, ShareUpdateExclusiveLock); + /* Make updated catalog entry visible */ PopActiveSnapshot(); CommitTransactionCommand(); @@ -17204,7 +17209,7 @@ ATExecDetachPartition(List **wqueue, AlteredTableInfo *tab, Relation rel, errmsg("partition \"%s\" was removed concurrently", partrelname))); tab->rel = rel; - + UnlockRelationIdForSession(&partlockrelid, ShareUpdateExclusiveLock); } /* Do the final part of detaching */ @@ -17444,7 +17449,10 @@ DetachAddConstraintIfNeeded(List **wqueue, Relation partRel) TupleDesc td = RelationGetDescr(partRel); Constraint *n; - constraintExpr = make_ands_explicit(RelationGetPartitionQual(partRel)); + List *l = RelationGetPartitionQual(partRel); + Assert(partRel->rd_rel->relispartition); + Assert(l != NIL); + constraintExpr = make_ands_explicit(l); /* If an identical constraint exists, we don't need to create one */ if (td->constr && td->constr->num_check > 0) -- 2.17.0 --kR3zbvD4cgoYnS/6--