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 1uMi6Y-001e4o-LV for pgsql-performance@arkaria.postgresql.org; Wed, 04 Jun 2025 07:00:06 +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 1uMi6W-008zRA-J3 for pgsql-performance@arkaria.postgresql.org; Wed, 04 Jun 2025 07:00:05 +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 1uMi6W-008zR2-8h for pgsql-performance@lists.postgresql.org; Wed, 04 Jun 2025 07:00:04 +0000 Received: from mail1.dalibo.net ([51.159.93.128] helo=mail.dalibo.com) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uMi6U-0008F4-31 for pgsql-performance@lists.postgresql.org; Wed, 04 Jun 2025 07:00:04 +0000 Received: from [192.168.0.7] (5-49-10-71.hfc.dyn.abo.bbox.fr [5.49.10.71]) by mail.dalibo.com (Postfix) with ESMTPSA id C63D327330; Wed, 4 Jun 2025 09:00:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=dalibo.com; s=a; t=1749020400; bh=J2dmGG8qQDtUcwNONgppaLVkMnsU5MCP722TgeluURQ=; h=Date:Subject:To:References:From:In-Reply-To:From; b=J31aVGD3H42nRUarM32nMSUODnnEnNCzEXVKEBexbK+mfCiu1NysNWtXEhSl/Z/QJ Hm2/QRqDDovOqxfxh0VgrVDvwyD9rm3t6buOswnflXtuWXdbewxEf0g3v2gVgqJe8P PSPATWKeNqz9WP+3yY5HOv/9Spbe763ynAJAY7b0= Message-ID: Date: Wed, 4 Jun 2025 09:00:00 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: ALTER TABLE ADD FOREIGN KEY to partitioned table, is not parallelized To: Dimitrios Apostolou , pgsql-performance@lists.postgresql.org References: <5f002654-92e9-8032-3e1a-134461b4e819@gmx.net> From: =?UTF-8?Q?Fr=C3=A9d=C3=A9ric_Yhuel?= Content-Language: fr, en-US Autocrypt: addr=frederic.yhuel@dalibo.com; keydata= xjMEXn3bgxYJKwYBBAHaRw8BAQdA6tX5FT/n5ztMWIoBdl6k5avvu65fv6ryfVzIx/aH3V3N JEYuIFlodWVsIDxmcmVkZXJpYy55aHVlbEBkYWxpYm8uY29tPsKWBBMWCAA+FiEEi7OTyf6H ePS+C6n11XGWhN8zGNUFAmZDVJ8CGwMFCQtfi90FCwkIBwIGFQoJCAsCBBYCAwECHgECF4AA CgkQ1XGWhN8zGNV65gEA6o+UrebFjn2CIAOYwP3CiiJugiM5TMH9yJyehQua5ZoA/2ChxiVn 8OUaAw/ErxJmQi1mSN0tNZBL/KhkUPBDUDQNzjgEXn3bgxIKKwYBBAGXVQEFAQEHQCuh5cLQ AtEBXHa2Fmtp0kduBu7msM7qO/gaEcdjFUdiAwEIB8J+BBgWCAAmAhsMFiEEi7OTyf6HePS+ C6n11XGWhN8zGNUFAmZDVJ8FCQtfi90ACgkQ1XGWhN8zGNWOOAEArSqePBqfd4Kx4ulACaWO 6fM+XSDfUlBAQXx9rU6DR4MBALwozl9g91tRysim6lKxggvBfp/PvbpTZNrxYLWB2fYP In-Reply-To: <5f002654-92e9-8032-3e1a-134461b4e819@gmx.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 6/3/25 17:34, Dimitrios Apostolou wrote: > The backend process for each of the above ALTER TABLE commands, does not >   parallelize the foreign key checks for the different partitions. I >   know, because in the logs I see gigabytes of temporary files being >   written, with the CONTEXT showing queries issued incrementally on >   all the different partitions: > >   :LOG:      temporary file: path "pg_tblspc/16390/PG_17_202406281/ > pgsql_tmp/pgsql_tmp3363462.579", size 1073741824 >   :CONTEXT:  SQL statement "SELECT fk."columnX" FROM ONLY > "public"."table_partition_214" fk >              LEFT OUTER JOIN ONLY "public"."another_table" pk >                  ON ( pk."columnX" OPERATOR(pg_catalog.=) fk."columnX") >              WHERE pk."columnX" IS NULL AND (fk."columnX" IS NOT NULL)" > >   Why can't the backend issue these queries in parallel workers? This has been discussed here: https://www.postgresql.org/message-id/flat/0d21e3b4-dcde-290c-875e-6ed5013e8e52%40dalibo.com Perhaps we should exhume this patch, but I believe the optimal strategy is to perform a VACUUM between the data and post-data to build the visibility map. The anti-join can then use an efficient index-only scan. Best regards, Frédéric