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 1oP4gz-0005Yo-Th for pgsql-bugs@arkaria.postgresql.org; Fri, 19 Aug 2022 16:17:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oP4gy-0006XP-DP for pgsql-bugs@arkaria.postgresql.org; Fri, 19 Aug 2022 16:17:52 +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 1oP4gy-0006XG-5I for pgsql-bugs@lists.postgresql.org; Fri, 19 Aug 2022 16:17:52 +0000 Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oP4gu-0006MW-UX for pgsql-bugs@lists.postgresql.org; Fri, 19 Aug 2022 16:17:51 +0000 Received: by mail-wm1-x32d.google.com with SMTP id n23-20020a7bc5d7000000b003a62f19b453so1265395wmk.3 for ; Fri, 19 Aug 2022 09:17:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc; bh=j5ZRGMdffiulBxRoFlm/yxgGUTrA/InI1L0o1A7BZJc=; b=ls9XKsQRxpDc/LK/rUQus78zM7SB2EM9N9bL+lotbaD74UtgWxEwIvOa8aX4T2tIKQ wRA4JqEjivR/9F/af1sma4vMNChv/US0JXlepPm9MPVzCJVT4YZUiJRt9Gn4fRO3wFNz nmx9njCqXuWspc2wi0vWL7GF+iAo6SyTwNeEJp6YfZEW6Y18hLx5JCLKJEZ4VgGtm4MO 3waFERykvGKDjK3Ar0CErq7PCYiOYT7l1dD51+Gvgp3DsRtmXMy7SfPghG0rokHF3el8 ZQKacqimI4cqc+ECr30I3DFq0uuqW42stoTq8y4xKYqnstq3zPt3IN9229pvq+VeeNQR esqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc; bh=j5ZRGMdffiulBxRoFlm/yxgGUTrA/InI1L0o1A7BZJc=; b=qddB095tTky4aDXhAfikbo/4rjrS604ux+zDnQ6TEwUSQ+GxxXIRk34fCB4GQvEVm/ rCiXjCc5Ug7SHpvn4YsFM9WftsGy5AHbjtjrEZbcQMBWO2Duhs+irz+duaus7asexdSy MDpKog0PFx+SnV8kp113D1TkCFcLDD2a5oyRlNx2m8Ly6PNVFY1ZTJhgVpFNxcl8YMjL +rGM5rqEGWKPBYZK0l4yHwFyCRz3vTnBbUQus5c2hI7ZLOnGpU4g1C00CCk20UnxeS4G Azoy8FoBRKJKhpkUPPYKYamFqbDYMqmcRd3bTixZQnYe8IYVvhG/7mIjk/dANztxobM2 4Uag== X-Gm-Message-State: ACgBeo0w2pNwUMmXCAWJ7tdli5krFWSeq9y2bK/xG4jg8z7dV/Q7GYLr FtZq4LCljl8WijbyNDvwVhUuLADFNupNQ4szUsFmUgIMPkgRuyJGJ9UaAou87DxeJjWHoos3t/i uoeP1QEIL5qGfQ2G2USec9QkFPqHZBJsaPJwEwQtWpwmcwgaB8uWI63J5Hx58bTrjcHCLfCyFc/ zp3GkOZL8L7pFpfNEgWcVTnB71KPc6HnBUZbonmedFMWnF X-Google-Smtp-Source: AA6agR6UC7ewhyDYH1M0RVItDiuVTHV0dELSjBG7GBRrwmRjDFF4Ke2sMSd1NauytlxGfQszqksutQ== X-Received: by 2002:a7b:c3c3:0:b0:3a5:a783:e37d with SMTP id t3-20020a7bc3c3000000b003a5a783e37dmr5350047wmj.80.1660925867327; Fri, 19 Aug 2022 09:17:47 -0700 (PDT) Received: from [10.137.0.17] (ip-86-49-229-52.bb.vodafone.cz. [86.49.229.52]) by smtp.gmail.com with ESMTPSA id s4-20020a1cf204000000b003a5f812635asm5424207wmc.39.2022.08.19.09.17.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 19 Aug 2022 09:17:46 -0700 (PDT) Message-ID: <5b0e5bab-08ac-0942-3bad-9b122b513f94@enterprisedb.com> Date: Fri, 19 Aug 2022 18:17:45 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: BUG #17590: [OPTIMIZER] DELETE never ends on a small table, found a workaround which makes it instant Content-Language: en-US To: g.fouet@gmail.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form References: <17590-80fab927fee1ddf2@postgresql.org> From: Tomas Vondra In-Reply-To: <17590-80fab927fee1ddf2@postgresql.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 8/19/22 17:13, PG Bug reporting form wrote: > The following bug has been logged on the website: > > Bug reference: 17590 > Logged by: Guillaume FOUET > Email address: g.fouet@gmail.com > PostgreSQL version: 14.5 > Operating system: Windows 10 - 64x > Description: > > Tested with Postgres 13.5, 13.8 and 14.5 (I updated to see if that was a > bug). > > I have four tables: ADDRESS, COMPANY, CONTACT, TRIP > ADDRESS has ~130000 rows > COMPANY has a FK toward ADDRESS and ~16000 rows > CONTACT has a FK toward ADDRESS and ~12000 rows > TRIP has a FK toward ADDRESS and ~137500 rows > > We wanted to purge the address table from old, unused addresses: > DELETE FROM address WHERE address_id NOT IN ( > SELECT DISTINCT address_id FROM company > UNION > SELECT DISTINCT address_id FROM contact > UNION > SELECT DISTINCT address_id FROM trip > ); > > This query above never ends (I waited 15 minutes and it was still going, HDD > doing nothing, one core CPU used). > The EXPLAIN says it materializes the address_ids aggregates then scans > ADDRESS for deletion. It's probably better to include the query plan. Anyway, the union essentially creates a new relation, making indexes (on the base relations unusable). > > After many tries, I made this query instead: > CREATE TEMPORARY TABLE used_address_id AS ( > SELECT DISTINCT address_id FROM company > UNION > SELECT DISTINCT address_id FROM contact > UNION > SELECT DISTINCT address_id FROM trip > ); > CREATE UNIQUE INDEX ON used_address_id (address_id); > DELETE FROM used_address_id WHERE address_id IS NULL; > DELETE FROM address WHERE address_id NOT IN (SELECT address_id FROM > used_address_id); > > This was resolved in 500ms (basically instantly). > I have the feeling there's an O(n²) somewhere in the first query. Yeah. The temporary table means we can use the indexes again. I'm not sure I'd call this a bug, it's simply how we deal with this sort of queries. Maybe try splitting the one "NOT IN" condition into a separate condition per table. I mean, something like DELETE FROM address WHERE address_id NOT IN (SELECT address_id FROM company) AND address_id NOT IN (SELECT address_id FROM contact) AND address_id NOT IN (SELECT address_id FROM trip) or something like that. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company