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 1h73Sg-00013j-Ud for pgsql-performance@arkaria.postgresql.org; Thu, 21 Mar 2019 19:34:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h73Se-0000JI-Gx for pgsql-performance@arkaria.postgresql.org; Thu, 21 Mar 2019 19:34:44 +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 1h73Se-0000JB-1S for pgsql-performance@lists.postgresql.org; Thu, 21 Mar 2019 19:34:44 +0000 Received: from gproxy5-pub.mail.unifiedlayer.com ([67.222.38.55]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h73ST-0007Tb-Gx for pgsql-performance@lists.postgresql.org; Thu, 21 Mar 2019 19:34:41 +0000 Received: from cmgw12.unifiedlayer.com (unknown [10.9.0.12]) by gproxy5.mail.unifiedlayer.com (Postfix) with ESMTP id 17B00140CDC for ; Thu, 21 Mar 2019 13:31:47 -0600 (MDT) Received: from host214.hostmonster.com ([74.220.215.214]) by cmsmtp with ESMTP id 73PmheX5Amds973PmhNz17; Thu, 21 Mar 2019 13:31:47 -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: MIME-Version:Date:Message-ID:To:Subject:From:Sender:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=oQA4RUNxePfV/9tAp6Y/EYWXmW9clJZiwjxHQJ+ndLU=; b=DV8jXzLuYXj5Lu2EmQOoM+y1Hd TnPz6T1m582ZOPFXYr/WqleaptComeV/XVsCQn6jMhHWEahWlNmAfglTVA0mnbkjQuP2Z72KRGA/J 8nwX2p9HePhbkleaJYmkgpSdP; Received: from [191.7.153.218] (port=62023 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 1h73Pl-002wFJ-Q6 for pgsql-performance@lists.postgresql.org; Thu, 21 Mar 2019 13:31:46 -0600 From: Gunther Subject: EXPLAIN PLAN for DELETE CASCADE or DELETE not using pkey index despite EXPLAINing that it would? To: pgsql-performance@lists.postgresql.org Message-ID: <189c6fff-0180-8179-d059-e222ec83bb1e@gusw.net> Date: Thu, 21 Mar 2019 15:31:42 -0400 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.5.3 MIME-Version: 1.0 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: 1h73Pl-002wFJ-Q6 X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([192.168.9.9]) [191.7.153.218]:62023 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 Hi, 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 see the read activity with iostat, reading heavily at 130 MB/s for a long time until my burst balance is used up, then continuing to churn with 32 MB/s. I also see the read activity with iotop, that tells me that it is that postgres backend running the DELETE query that is doing this, not some autovacuum nor anything else. It looks to me that in actuality it is doing a sequential scan for each of the 250 rows, despite it EPLAINing to me that it was going to use that index. It would really be good to know what it is churning so heavily? I have seen some ways of using dtrace or things like that to do some measurement points. But I haven't seen how this is done to inspect the effective execution plan and where in that plan it is, i.e., which iteration. It would be nice if there was some way of doing a "deep explain plan" or even better, having an idea of the execution plan which the executor is actually following, and a way to report on the current status of work according to this plan. How else do I figure out what causes this heavy read activity on the main Foo table? This is something I might even want to contribute. For many years I am annoyed by this waiting for long running statement without any idea where it is and how much is there still to go. If I have a plan structure and an executor that follows the plan structure, there must be a way to dump it out. The pg_stat_activity table might contain a current statement id, and then a superuser might ask EXPLAIN STATEMENT :statementId. Or just a pg_plantrace command which would dump the current plan with an indication of completion % of each step. But also delete cascades and triggers should be viewable from this, they should be traced, I am sure that somewhere inside there is some data structure representing this activity and all it would take is to dump it? regards, -Gunther