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 1kkrHK-00025m-PK for pgsql-performance@arkaria.postgresql.org; Thu, 03 Dec 2020 16:16:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kkrHJ-000492-G0 for pgsql-performance@arkaria.postgresql.org; Thu, 03 Dec 2020 16:16:21 +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 1kkrHJ-00048u-8R for pgsql-performance@lists.postgresql.org; Thu, 03 Dec 2020 16:16:21 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kkrHH-00062n-5n for pgsql-performance@lists.postgresql.org; Thu, 03 Dec 2020 16:16:20 +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 0B3GGGlH1735186; Thu, 3 Dec 2020 11:16:16 -0500 From: Tom Lane To: Justin Pryzby cc: Ravikumar Reddy , Atul Kumar , pgsql-performance Subject: Re: time taking deletion on large tables In-reply-to: <20201203153612.GM24052@telsasoft.com> References: <20201203153612.GM24052@telsasoft.com> Comments: In-reply-to Justin Pryzby message dated "Thu, 03 Dec 2020 09:36:12 -0600" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1735184.1607012176.1@sss.pgh.pa.us> Date: Thu, 03 Dec 2020 11:16:16 -0500 Message-ID: <1735185.1607012176@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Justin Pryzby writes: > On Thu, Dec 03, 2020 at 08:43:57PM +0530, Ravikumar Reddy wrote: >> When I try to do a delete like this: it hangs for an entire day, so I >> need to kill it with pg_terminate_backend(pid). >> >> DELETE FROM feed_posts >> WHERE feed_definition_id = 'bf33573d-936e-4e55-8607-72b685d2cbae' >> AND created_at > '2020-05-11 00:00:00' >> AND created_at < '2020-05-12 00:00:00'; 90% of the "delete takes forever" complaints that we hear trace down to having a foreign key reference to the deletion-target table that's not backed by an index on the referencing column. Then you end up getting a seqscan on the referencing table to look for rows referencing a row-to-be-deleted. And then another one for the next row. Etc. You could try "explain analyze" on a query deleting just a single one of these rows and see if an RI enforcement trigger is what's eating the time. regards, tom lane