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 1kkryx-0003cD-M6 for pgsql-performance@arkaria.postgresql.org; Thu, 03 Dec 2020 17:01:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kkryP-0002Fs-TK for pgsql-performance@arkaria.postgresql.org; Thu, 03 Dec 2020 17:00:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kkryP-0002Fl-Lz for pgsql-performance@lists.postgresql.org; Thu, 03 Dec 2020 17:00:53 +0000 Received: from relay7-d.mail.gandi.net ([217.70.183.200]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kkryM-0008MT-6a for pgsql-performance@lists.postgresql.org; Thu, 03 Dec 2020 17:00:52 +0000 X-Originating-IP: 99.10.92.30 Received: from [192.168.10.146] (99-10-92-30.lightspeed.rlghnc.sbcglobal.net [99.10.92.30]) (Authenticated sender: adsend@dunslane.net) by relay7-d.mail.gandi.net (Postfix) with ESMTPSA id 18B1B20004; Thu, 3 Dec 2020 17:00:45 +0000 (UTC) From: Andrew Dunstan Subject: Re: time taking deletion on large tables To: Tom Lane , Justin Pryzby Cc: Ravikumar Reddy , Atul Kumar , pgsql-performance References: <20201203153612.GM24052@telsasoft.com> <1735185.1607012176@sss.pgh.pa.us> Message-ID: Date: Thu, 3 Dec 2020 12:00:44 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.3.1 MIME-Version: 1.0 In-Reply-To: <1735185.1607012176@sss.pgh.pa.us> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12/3/20 11:16 AM, Tom Lane wrote: > 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. > > Yeah. IIRC some other RDBMS systems actually create such an index if it doesn't already exist. Maybe we should have a warning when setting up an FK constraint if the referencing fields aren't usefully indexed. cheers andrew -- Andrew Dunstan EDB: https://www.enterprisedb.com