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 1rPrjJ-00Emjw-Gb for pgsql-general@arkaria.postgresql.org; Tue, 16 Jan 2024 22:16:22 +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 1rPrjI-008cvd-Dd for pgsql-general@arkaria.postgresql.org; Tue, 16 Jan 2024 22:16:20 +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 1rPrjI-008cvV-2q for pgsql-general@lists.postgresql.org; Tue, 16 Jan 2024 22:16:20 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rPrjF-002BjY-Cl for pgsql-general@lists.postgresql.org; Tue, 16 Jan 2024 22:16:19 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 40GMGE3K1905753; Tue, 16 Jan 2024 17:16:14 -0500 From: Tom Lane To: Adrian Klaver cc: Jim Vanns , pgsql-general@lists.postgresql.org Subject: Re: Tips on troubleshooting slow DELETE (suspect cascades) In-reply-to: <566a6359-940c-457e-b4c6-02cdf00f54f0@aklaver.com> References: <566a6359-940c-457e-b4c6-02cdf00f54f0@aklaver.com> Comments: In-reply-to Adrian Klaver message dated "Tue, 16 Jan 2024 09:53:09 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <1905751.1705443374.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Tue, 16 Jan 2024 17:16:14 -0500 Message-ID: <1905752.1705443374@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Adrian Klaver writes: > On 1/16/24 09:45, Jim Vanns wrote: >> I have a slow (CPU bound) DELETE statement I'm attempting to debug and I >> suspect that its actually the ON DELETE CASCADE on the foreign key thats >> causing it. 99% of the time, the cause is lack of an index on the foreign key's referencing columns. We make you have a unique index on the referenced columns, because otherwise the FK constraint's semantics are unclear. But you're not required to make one on the other side. >> What I need is a way to see into this statement as it executes to >> confirm my suspicion - does anyone have any tips on that? > Explain: > https://www.postgresql.org/docs/current/sql-explain.html Specifically, if EXPLAIN ANALYZE shows a lot of time spent in the enforcement trigger for the FK, this is likely what's happening. regards, tom lane