Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tADHZ-00ENv1-NM for pgsql-general@arkaria.postgresql.org; Sun, 10 Nov 2024 19:07:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tADHX-008JlT-4A for pgsql-general@arkaria.postgresql.org; Sun, 10 Nov 2024 19:07:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tADHW-008JlK-5D for pgsql-general@lists.postgresql.org; Sun, 10 Nov 2024 19:07:31 +0000 Received: from fhigh-b5-smtp.messagingengine.com ([202.12.124.156]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tADHR-001EpA-TK for pgsql-general@lists.postgresql.org; Sun, 10 Nov 2024 19:07:30 +0000 Received: from phl-compute-08.internal (phl-compute-08.phl.internal [10.202.2.48]) by mailfhigh.stl.internal (Postfix) with ESMTP id 5AE9E2540061; Sun, 10 Nov 2024 14:07:24 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-08.internal (MEProxy); Sun, 10 Nov 2024 14:07:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1731265644; x=1731352044; bh=8mXANLzUKj+XmdOuk+KElYKQRF9HSlk73XM9moBjS/U=; b= aE7NjlAWDf/f+wT/3dp4s6aWRPsr6WVeLfTHUiHRPBHhTcSX2+x/Z4kdeMHeaMfW y9TPrvUmDkofRZNfqa7Qri+ggYYeXrU/D5SuydrE5JY+DYVNXN0vP1lGpWXbNfVc qME0xGM+N0+U17fLiJS2Yngfbb5cNsjKLZ9Ub7hjLYzN5YFZG02m9oLBBKLPKFMR 4KPXb8wsKWp9XHibXyMf89R9SghTr2fIm1FqPyQoP9rK6XinGd7VoheWvm7QMgHt v0xv1CR0LGyA24pP5xwysbr6kknbRKs+y8nLuMtvOeul9hzy5tET4RQABcDnNsom swVH+/+7x5d/BrMhFP53Jw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1731265644; x=1731352044; bh=8 mXANLzUKj+XmdOuk+KElYKQRF9HSlk73XM9moBjS/U=; b=K8QSkUMwdmB4tjuWe 1Y4vNbOXzo3caaj3zVGVUz5d66+/JgdloLtL04M2y5EdxHCQiLqJTERMFZCMe5yA Zq/CnhTDBjvowPQ64BA0PvFC+v58LKMEuZcFgRHGuQxjfdxCjT/ap9Qco/P6TB2p MLg4UETzFN95g8aTAa6/AuZFoFk6rtj5e+eurNq+eX4BozsONw+E4kVFiHQIKYiE 8GOxzDUPzaC9nNRWeIfXbWOZMvTYfWh8MTMKCOjHESHURtEO0p5LGAe4t8RffckB d101ZleNRGwAgqEyXKtT6YbqegiVLdVlcLB2BVAI+qW3b14bTUnRtS0z7eGsA4pl w15DA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddruddtgdduudelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefkffggfg fuvfhfhfgjtgfgsehtkeertddtvdejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghr uceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuggftrfgrth htvghrnhepgfevtdejvddtudetkeeuudffgfegleduhfekteeugfefhffhteekveffffdu veeknecuffhomhgrihhnpehpghhlohgtkhhsrdhorhhgnecuvehluhhsthgvrhfuihiivg eptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghk lhgrvhgvrhdrtghomhdpnhgspghrtghpthhtohepvddpmhhouggvpehsmhhtphhouhhtpd hrtghpthhtohepuhhsvghrsehpihguuhdruggvvhdprhgtphhtthhopehpghhsqhhlqdhg vghnvghrrghlsehlihhsthhsrdhpohhsthhgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 10 Nov 2024 14:07:23 -0500 (EST) Message-ID: Date: Sun, 10 Nov 2024 11:07:22 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Fwd: Fwd: Postgres attach partition: AccessExclusive lock set on different tables depending on how attaching is performed To: user@pidu.dev, pgsql-general@lists.postgresql.org References: <254c31c2-ff15-4720-a4fc-f0969d677d72@aklaver.com> <4ebbbcf5-9c35-4711-bd7c-06a56a31aeff@aklaver.com> Content-Language: en-US From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 11/10/24 05:18, user wrote: > Hello, > Sorry for nagging, but I would really like to find some answers. > So, to reiterate. Experiment done as follows: > """""""""""""""""""""' > CREATE TABLE refs ( >                     id integer primary key, >                     did integer >                 ); > CREATE TABLE films ( >                     id integer, >                     code char(5) , >                     title varchar(40) NOT NULL, >                     did integer NOT NULL references refs(id) >                     ) >                     partition by list (code); > > insert into refs values (5, 5) > create table films_partition (LIKE films INCLUDING ALL) > insert into films_partition values (1, 'dr', 'musician',5) > alter table films_partition add constraint check_code check (code = 'dr'); > alter table films_partition ADD CONSTRAINT fk_did FOREIGN KEY (did) > REFERENCES refs (id); > """"""""""""""""""""""""" > Then, when we open a transaction and try to attach: > """"""""""""""""""""""""" >  BEGIN; > ALTER TABLE films ATTACH PARTITION films_partition for values in ('dr') > keep the transaction running.. > """""""""""""""""""""""" > Once we check a locks, we will see that there is AccessExclusiveLock on > table refs. > """""" > select relname, mode >                 from pg_locks l >                     join pg_class c on (relation = c.oid) >                     join pg_namespace nsp on (c.relnamespace = nsp.oid); > """"" > My questions are: > 1. Why is postgres adding again a constraint? Can't it detect that > foreign key already exists? I want to avoid locking partitioned table > for too long. I see, I missed it my previous post: alter table films_partition ADD CONSTRAINT fk_did FOREIGN KEY (did) REFERENCES refs (id); \d films_partition Foreign-key constraints: "fk_did" FOREIGN KEY (did) REFERENCES refs(id) ALTER TABLE films ATTACH PARTITION films_partition for values in ('dr') \d films_partition Foreign-key constraints: TABLE "films" CONSTRAINT "films_did_fkey" FOREIGN KEY (did) REFERENCES refs(id) The FK constraint changes from being films_partition <--> refs to films <--> refs > 2. Even when attach is adding a foreign key again, why is there > AccessExclusiveLock on refs table? foreign key constraint addition does > not require it. > https://pglocks.org/?pgcommand=ALTER%20TABLE%20ADD%20FOREIGN%20KEY%20(CHILD) > 3. If I repeat the steps listed above, but do not add foreign key > manually, then attach partition does not hold AccessExclusive lock on > table refs. It still needs to add a foreign key, as "films" table has > that constraint. Why is the AccessExclusive lock missing from "refs" > table now? Best guess because the FK is changing referencing table and in: ~//src/backend/commands/tablecmds.c "CloneFkReferencing For each FK constraint of the parent relation in the given list, find an equivalent constraint in its partition relation that can be reparented; if one cannot be found, create a new constraint in the partition as its child." [...] addFkRecurseReferencing(wqueue, fkconstraint, partRel, pkrel, indexOid, constrOid, numfks, confkey, mapped_conkey, conpfeqop, conppeqop, conffeqop, numfkdelsetcols, confdelsetcols, false, /* no old check exists */ AccessExclusiveLock, insertTriggerOid, updateTriggerOid); > > Regards! -- Adrian Klaver adrian.klaver@aklaver.com