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 1h7QP5-0000GJ-GN for pgsql-performance@arkaria.postgresql.org; Fri, 22 Mar 2019 20:04:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h7QP2-0006pC-UZ for pgsql-performance@arkaria.postgresql.org; Fri, 22 Mar 2019 20:04:32 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h7QP2-0006k7-B3 for pgsql-performance@lists.postgresql.org; Fri, 22 Mar 2019 20:04:32 +0000 Received: from gproxy7-pub.mail.unifiedlayer.com ([70.40.196.235]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h7QOz-0006Zj-0x for pgsql-performance@lists.postgresql.org; Fri, 22 Mar 2019 20:04:30 +0000 Received: from cmgw14.unifiedlayer.com (unknown [10.9.0.14]) by gproxy7.mail.unifiedlayer.com (Postfix) with ESMTP id C3917215FBD for ; Fri, 22 Mar 2019 14:01:42 -0600 (MDT) Received: from host214.hostmonster.com ([74.220.215.214]) by cmsmtp with ESMTP id 7QMIhin3OXFO57QMIhP0Fc; Fri, 22 Mar 2019 14:01:42 -0600 X-Authority-Reason: nr=8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=pragmaticdata.com; s=default; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:MIME-Version:Date:Message-ID:From:References:To:Subject:Sender: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=ayUrmAQGyfHKmq7OFb7fMkbc/z26FKrg5wflJGWfnhU=; b=IWKTczRsw2z6ZYD+PJ1cvl6NXB nqT2Ctfy2Coo7/V5obcRVvHQvhiEUIKvlcERTBdJLpYnO5YL4jKuB9w03LK8SnIW9UCeYfPqdQjXG o+JTqaIyLCKfhuABmljf7d1bK; Received: from [191.7.153.218] (port=49743 helo=[192.168.9.9]) by host214.hostmonster.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.91) (envelope-from ) id 1h7QMI-000suA-8L for pgsql-performance@lists.postgresql.org; Fri, 22 Mar 2019 14:01:42 -0600 Subject: Re: EXPLAIN PLAN for DELETE CASCADE or DELETE not using pkey index despite EXPLAINing that it would? To: pgsql-performance@lists.postgresql.org References: <189c6fff-0180-8179-d059-e222ec83bb1e@gusw.net> <16581.1553203016@sss.pgh.pa.us> From: Gunther Message-ID: <808a0d1a-9357-6bd7-bfe9-5f401fa48d5a@gusw.net> Date: Fri, 22 Mar 2019 16:01:38 -0400 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.5.3 MIME-Version: 1.0 In-Reply-To: <16581.1553203016@sss.pgh.pa.us> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - host214.hostmonster.com X-AntiAbuse: Original Domain - lists.postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - gusw.net X-BWhitelist: no X-Source-IP: 191.7.153.218 X-Source-L: No X-Exim-ID: 1h7QMI-000suA-8L X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([192.168.9.9]) [191.7.153.218]:49743 X-Source-Auth: gunther+pragmaticdata.com X-Email-Count: 1 X-Source-Cap: cHJhZ21hdDE7cHJhZ21hdDE7aG9zdDIxNC5ob3N0bW9uc3Rlci5jb20= X-Local-Domain: yes List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 3/21/2019 17:16, Tom Lane wrote: > 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 I had the same suspicion. But firstly my schema is generated automatically and all foreign keys have the indexes. But what is even more stunning is that the table where this massive read activity happens is the Foo heap table. I verified that by using strace where all the massive amounts of reads are on those files for the main Foo table. And this doesn't make sense, since any foreign key targets its primary key. The foreign keys of the child tables are also indexed and there is no io on the volumes that hold these child tables, nor is the io on the volume that holds the Foo_pkey.