Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h753v-0006Yc-K5 for pgsql-performance@arkaria.postgresql.org; Thu, 21 Mar 2019 21:17:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h753t-0003gx-HB for pgsql-performance@arkaria.postgresql.org; Thu, 21 Mar 2019 21:17:17 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h753t-0003gq-9N for pgsql-performance@lists.postgresql.org; Thu, 21 Mar 2019 21:17:17 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h753n-0001VV-16 for pgsql-performance@lists.postgresql.org; Thu, 21 Mar 2019 21:17:16 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x2LLGuw5016582; Thu, 21 Mar 2019 17:16:56 -0400 From: Tom Lane To: Gunther cc: pgsql-performance@lists.postgresql.org Subject: Re: EXPLAIN PLAN for DELETE CASCADE or DELETE not using pkey index despite EXPLAINing that it would? In-reply-to: <189c6fff-0180-8179-d059-e222ec83bb1e@gusw.net> References: <189c6fff-0180-8179-d059-e222ec83bb1e@gusw.net> Comments: In-reply-to Gunther message dated "Thu, 21 Mar 2019 15:31:42 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <16580.1553203016.1@sss.pgh.pa.us> Date: Thu, 21 Mar 2019 17:16:56 -0400 Message-ID: <16581.1553203016@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Gunther writes: > I have 250 rows to delete, but they are a target to a bunch of child > tables with foreign key on delete cascade. > EXPLAIN DELETE FROM Foo WHERE id = (SELECT fooId FROM Garbage); > shows me that it uses the nested loop by Foo_pkey index to find the 250 > items from Garbage to be deleted. > But once that starts, I see HUGE amount of read activity from the > tablespace Foo_main that contains the Foo table, and only the Foo table, > not the Foo_pkey, not any other index, not any other child table, not > even the toast table for Foo is contained in that tablespace (I have the > toast table diverted with symlinks to another volume). I'm betting you neglected to index the referencing column for one or more of the foreign keys involved. You can get away with that as long as you're not concerned with the speed of DELETE ... regards, tom lane