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 1nYsYd-0003ae-3a for pgsql-general@arkaria.postgresql.org; Mon, 28 Mar 2022 16:49:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nYsYa-0004Wk-U1 for pgsql-general@arkaria.postgresql.org; Mon, 28 Mar 2022 16:49:28 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nYsYZ-0004Wb-Ut for pgsql-general@lists.postgresql.org; Mon, 28 Mar 2022 16:49:28 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nYsYW-0003aU-Tu for pgsql-general@postgresql.org; Mon, 28 Mar 2022 16:49:26 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id D0CD53201E88; Mon, 28 Mar 2022 12:49:23 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Mon, 28 Mar 2022 12:49:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; bh=LcyUCbO19U9/3O KchZh7z+BRJjDXxx330ISlW9QqxHg=; b=dcx70vcADsllNelhBzQCwVQFnVhI0l EQRT9NCp+QMxr7+duLMBY/I4UFteO4aUKqdm4i2Xty3IwgXxl0eCnnwIlezzuILe Qlo96RDKhw1+VmSVxtcefkX1K9CZD+6AE6K8VMGk/uD9ESfobVjc0rYU/KhRUWOS m/IYkXZ15HmFY3fq7iJubHZ4TuLmRElGMrXCoUCWehZhrCWzIQ6bammv4b1d8fcM fgR65jtz35Ck/3yv3X/fr3OnWsFRZsLQFn1p1X3z+rWaSL6QGKuH2d8OquTesAYO eq2Hou6+l+4Mlj4LQf+S8xZFfgWup7wUJ+8a24XTwZsRwc2RqWpmAw1w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:sender:subject:subject:to:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=LcyUCbO19U9/3OKchZh7z+BRJjDXxx330ISlW9QqxHg=; b=CGc6QZDm USUsEV+DMhAxob87XMdr9qSv7kkD3LPVqnMNnFfBYgsddOb6B/sE496BKeQm/G76 nLvcPS8OgncLo+qvz2JX5jh8dfmZlIOlYNnu712+cXHwxA0wR5t3eGxsvBQeUQpj Bb3sK3UBG+htZ9th9wzuCKJoBbbpxLD8E2sQbqgPyjMf4ZxPV4Sx+73VYmWZjlnU xTv9CJX8/Z3cIFtMVfKbdoKLD7rk8FRzcV2KcCvO8n6B5Dfq2zv/jz48KvzMzftd ymWObM8vhdJmrb5n8f700cr0tT81KkQ0YN68NwU0+mrqgCl9Ruv/mCWvDfWRSpVF GJgwV5aD76fvuQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrudehjedguddthecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefkffggfgfuvfhfhfgjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuggftrfgrthhtvghrnhepvedvkeeihfduhffhtdekleegffduiefgteejgeff gffhteefleeludfgleelgeevnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 28 Mar 2022 12:49:22 -0400 (EDT) Message-ID: <04bce16b-7bf4-e74a-e3c7-f6e369592cca@aklaver.com> Date: Mon, 28 Mar 2022 09:49:22 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: Performance issues on FK Triggers after replacing a primary column Content-Language: en-US To: Per Kaminsky , "pgsql-general@postgresql.org" References: <8ed0c2ae-8a35-2628-4b41-68f9cb734297@aklaver.com> From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 3/27/22 23:53, Per Kaminsky wrote: > The table structure looks (roughly) like this: > > * Table "Base": (id, created, deleted, origin, ...) ~3m rows > * Table "A": (id as FK on "Base", ...) ~400k rows > * Table "B": (id, ref_a as FK on "A", type, ...) ~2m rows > > Swapping the PK of "A" happens as following, the FK is dropped during > the process since otherwise the performance issues also happen here when > updating the PK. The update calls do normally utilize a file based > import into a temporary table from which i do the actual update: > > ALTER TABLE "B" DROP CONSTRAINT "B_to_A_fkey"; > ALTER TABLE "A" ADD COLUMN id_temp BIGINT; > // fill id_temp with new IDs > UPDATE "B" SET ref_a = "A".id_temp WHERE "B".ref_a= "A".id; > UPDATE "A" SET id = id_temp; > ALTER TABLE "B" ADD CONSTRAINT "B_to_A_fkey" FOREIGN KEY (ref_a) > REFERENCES A(id); Now that the morning coffee has taken effect, I'm wondering why the above is necessary at all? If "B_to_A_fkey" where to have ON UPDATE CASCADE then you could eliminate the dropping/adding back of the FK. The process would be: ALTER TABLE "A" ADD COLUMN id_temp BIGINT; // fill id_temp with new IDs UPDATE "A" SET id = id_temp; UPDATE "B" SET type = 2 WHERE type ISNULL; It might even be possible to further simplify depending on what '// fill id_temp with new IDs' actually does? > > And then the new occuring step, in the same transaction, which then also > has shown the performance issues described if i would not remove the FK > temporarily: > > ALTER TABLE "B" DROP CONSTRAINT "B_to_A_fkey"; > UPDATE "B" SET type = 2 WHERE type ISNULL; > ALTER TABLE "B" ADD CONSTRAINT "B_to_A_fkey" FOREIGN KEY (ref_a) > REFERENCES A(id); > > > > ** > > -- Adrian Klaver adrian.klaver@aklaver.com