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 1mYCSV-0002V2-6A for pgsql-performance@arkaria.postgresql.org; Wed, 06 Oct 2021 19:20:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mYCST-0000Sf-WB for pgsql-performance@arkaria.postgresql.org; Wed, 06 Oct 2021 19:20:06 +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 1mYCST-0000SA-GD for pgsql-performance@lists.postgresql.org; Wed, 06 Oct 2021 19:20:05 +0000 Received: from mail-il1-x132.google.com ([2607:f8b0:4864:20::132]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mYCSR-0000Cx-1e for pgsql-performance@lists.postgresql.org; Wed, 06 Oct 2021 19:20:04 +0000 Received: by mail-il1-x132.google.com with SMTP id w11so699339ilv.6 for ; Wed, 06 Oct 2021 12:20:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=byVt5ivDKGKjaB88Jik1uNJDWSbGnG81+HOyBq0/su8=; b=i/OTrEhbG+HQ0tmgrGmKI5xKAEYLTwWJRVKMwQpdUGkD/BkD+Zn3kcczFAyvykh2wA 07gGDSw0V8xV0wJrszgRfBFMDHg3n0zNQ0UXD8EX8T2q1bk+mwi+EsEtR3Ab8p+KOAzf m0+rfxH2Yqc2LGJuLuwJ7Gf8ygyYWCsak0PemdXE3g/UbBUAi2cHxV1Xi0IqfGyscy/P UQSDYTV/ybu5kHgBF2TCJJyf0kI42iZK9F5ATi9BVcZihfybdmpStiuVkzBc9d3tAakr F+wWiDvkSRZ5nlYbNQj8UIGMnGE/IlStWB6NXSUyBKAFpTMFR3EvLSwmANuLV9HVFIjj uisQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=byVt5ivDKGKjaB88Jik1uNJDWSbGnG81+HOyBq0/su8=; b=aiJ2kGe0EZJ8eMWyfo0pMu7ak9hb2q+Yz1wMPy3GXg/cEFPZZhbsiQteDKB5ULDY9d u6Qy3PO2bxmmyVYnegx1zRuTg9WUp+dD7E70LGG3m4Y4au01i5HoRm/agxMy759GsJUr bMa8jLRnznR91XwTzZDv6XgU14ka1XU4wlOMzk41VzCiLaX/OOhg2fGyxIMHbpBSCJ6k 9Ea3JNy/SFRM82IRk0nAS6vkcG4q59E4SX/6XgXjRKFmdN6qi1ZJldfUO05rIsp8WvIY dBnzW88fDvgPgiLdWvU+SWNweBXvx7SJgHzJEG2DX4ZEHo+9EUOirEDo+kovHcvN7OUa LUuQ== X-Gm-Message-State: AOAM530Xit8h/XYoX1fEgM/Xf9AZ9QJ+JZK+2Z93bbXfJxVyMYq93k6A 5nraGjpVQ8bPyE229DQ8/1NaSmIbNpYRYg== X-Google-Smtp-Source: ABdhPJz94hEAGKD2HTblFpDFf95IuLdyUXWOkkVSGqOsOr65rmEeZ36tTkbVBVw6zaAAWW2t6yzsJQ== X-Received: by 2002:a05:6e02:12e9:: with SMTP id l9mr5952830iln.43.1633548001955; Wed, 06 Oct 2021 12:20:01 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id n17sm2055825ile.76.2021.10.06.12.20.01 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Oct 2021 12:20:01 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id C8A6C80090D; Wed, 6 Oct 2021 14:20:00 -0500 (CDT) Date: Wed, 6 Oct 2021 14:20:00 -0500 From: Justin Pryzby To: "Dirschel, Steve" Cc: pgsql-performance@lists.postgresql.org Subject: Re: Troubleshooting a long running delete statement Message-ID: <20211006192000.GJ27491@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Oct 06, 2021 at 06:00:07PM +0000, Dirschel, Steve wrote: > • When I did an explain on the delete I could see it was full scanning the table. I did a full scan of the table interactively in less than 1 second so the long runtime was not due to the full tablescan. > I started looking at table definitions (indexes, FK's, etc.) and comparing to Oracle and noticed some indexes missing. I then could see the table being deleted from was a child table with a FK pointing to a parent table. Finally I was able to see that the parent table was missing an index on the FK column so for every row being deleted from the child it was full scanning the parent. All makes sense after the fact but I'm looking for a more methodical way to come to that conclusion by looking at database statistics. > > Are there other statistics in Postgres I may have looked at to methodically come to the conclusion that the problem was the missing index on the parent FK column? I think explain (analyze on) would've helped you. If I understand your scenario, it'd look like this: |postgres=# explain (analyze) delete from t; | Delete on t (cost=0.00..145.00 rows=10000 width=6) (actual time=10.124..10.136 rows=0 loops=1) | -> Seq Scan on t (cost=0.00..145.00 rows=10000 width=6) (actual time=0.141..2.578 rows=10000 loops=1) | Planning Time: 0.484 ms | Trigger for constraint u_i_fkey: time=4075.123 calls=10000 | Execution Time: 4087.764 ms You can see the query plan used for the FK trigger with autoexplain. postgres=*# SET auto_explain.log_min_duration='0s'; SET client_min_messages=debug; SET auto_explain.log_nested_statements=on; postgres=*# explain (analyze) delete from t; |... |Query Text: DELETE FROM ONLY "public"."u" WHERE $1 OPERATOR(pg_catalog.=) "i" |Delete on u (cost=0.00..214.00 rows=1 width=6) (actual rows=0 loops=1) | Buffers: shared hit=90 | -> Seq Scan on u (cost=0.00..214.00 rows=1 width=6) (actual rows=1 loops=1) | Filter: ($1 = i) | Rows Removed by Filter: 8616 | Buffers: shared hit=89 |...