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 1vEuO8-003MVp-Vi for pgsql-hackers@arkaria.postgresql.org; Fri, 31 Oct 2025 19:02:16 +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 1vEuO5-00HSp8-QU for pgsql-hackers@arkaria.postgresql.org; Fri, 31 Oct 2025 19:02:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vEuO5-00HSoz-Gn for pgsql-hackers@lists.postgresql.org; Fri, 31 Oct 2025 19:02:12 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vEuO2-004may-2o for pgsql-hackers@postgresql.org; Fri, 31 Oct 2025 19:02:11 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-294df925292so23508575ad.1 for ; Fri, 31 Oct 2025 12:02:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761937330; x=1762542130; darn=postgresql.org; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=mir3NQlPaMrKMU+UFiDKK15EKY7MUg+FAU1ZR6Or1XY=; b=S+sWf5q6JcYkrUqXz7taU8ms57fLWWt+68xaa7Y71gklRqQ881kI98hSGFYA5erSzE S7Inz4Z/ZzcHjxEm9Nd9e752VIMvICi71vIjJe6cdwBupeOQLHp1l7fEoyuqZagtAVmp gEB0Ss5U40fmC4yJqRYvuCn84KiwYY8AqwXqH1TLFsicJWsQkuiMrqAiakgwKcI5Micc epR3S6Q0I5ZuqjhICIu0dIxEEQNZ0kokiXUCngiVH9DvzgcqkqpfDDn6ONIdKqo8D99O t2x8OlON/m5Fn5QflD2HngiVhDXm/Ww/Ajw+YYGTuqAw/xQp+qd7tvHBvaP2oQTbOxkt 1dug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761937330; x=1762542130; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=mir3NQlPaMrKMU+UFiDKK15EKY7MUg+FAU1ZR6Or1XY=; b=FyS7MzAiFtWn2g7N8WqyOPy9Lo+cOigFgHldzhEG7AMxOI/SLhAfqjaOq/V8j7KeKL T9EYyb8fSd/kuYmLK/YzFeqE4Ha0BoiXMiZdh1pQ+OyQJy/hbrz9B2wfjc9Kz8P0zIIo 0V3mAfcgfA5NMXEho3/3CXyfVXMKO3n88/LZrNyiR4Z5BudZ/UvKD/Dk+nI/YcFSLTPP 1cN/TLVZPqGGXHwMMWTZzj25KzA3Z55NPziPFzS9HPcJ20zq1Mpbq0xUXw1bJgCItjBl Wqp7H4TKFeSqi3zBYn/8bVF/rOHJXAw25Ro+2mFE8eN6bqEGeiGRkZMZO4vPocIn358e 3aDg== X-Forwarded-Encrypted: i=1; AJvYcCWNJv48GoccV2VjSpiAztb14vBHW7j6oJSgrgxTSLpRDI8bQ67kFIVvRKKISOzyVHfUo0wcyJ1UgxyuxKf5@postgresql.org X-Gm-Message-State: AOJu0YyHPzotXXEfIxPlO+P1fgc0SRCZGOJ4otgFiB90bdPlBAOWH+5k Axe9svIuVQoKXXlE2wbJzFWa2kkkhEd3GM0tTysfsBQ2mvYSnz4Kd1pu X-Gm-Gg: ASbGncsRneLDezd9xmyqgnzIYrygly2SJpjvykp7iy4k+1lJQ79+50XLdKHp9Fy2WLA cZmIKf2g9qYnJucMbcLzXPTfHNDpNr2tiYAojuEVVYNTLAEoBXwUWqF7h8ycGlUpXLR9+ecDLjL AkIvEJur2l+Bccyh1gpVf6AsTDZtrJvBTUw10poj/BPuKc4XsRvP34nOeBimo7DUVVkkGdW7uGW Z3AYTQFCl7b6J+a5G9M6A7PiIoAhHz4MMTdOYr12gt4mwUa61fRpqnDa1AzekQlRqmdyyuf98w+ IAUl+dEaAThmXlDNn7507Gv6Ag3HZDIVco+QK53Ixj55P8YkrQs5ILuffNx8ddHSJdyV3iO1GU8 Zhx6SX+fNzBeSmcgODULYIqQR275j8QA1co5Z3ZlpDLWgh3ZladcoeaPj8YcVfypYFieWVimgNg bmvNgWJ72ObqxJLve/vG/BOtiGDsE3IuWYBQ== X-Google-Smtp-Source: AGHT+IGjxxQi/tLQYH1eEG38z2gGdMhp+Q9ENh+aE4/6q5TogWBf8wKrnJ5gQPMR29BSVgGWQO4swQ== X-Received: by 2002:a17:903:2348:b0:267:a5df:9b07 with SMTP id d9443c01a7336-2951a36bff4mr56605405ad.12.1761937329835; Fri, 31 Oct 2025 12:02:09 -0700 (PDT) Received: from localhost ([2804:14d:328a:a59c:f817:1d4b:5dab:62d0]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29546185f17sm9697025ad.31.2025.10.31.12.02.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 31 Oct 2025 12:02:09 -0700 (PDT) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Fri, 31 Oct 2025 16:02:05 -0300 Message-Id: Cc: "Tomas Vondra" , Subject: Re: postgres_fdw: Use COPY to speed up batch inserts From: "Matheus Alcantara" To: "Andrew Dunstan" , "Matheus Alcantara" , "jian he" X-Mailer: aerc 0.21.0 References: <5c52f4c7-74fa-4594-8378-53fd49f39329@dunslane.net> In-Reply-To: <5c52f4c7-74fa-4594-8378-53fd49f39329@dunslane.net> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu Oct 30, 2025 at 1:32 PM -03, Andrew Dunstan wrote: >> It seems to me that we need to disable the COPY usage when the foreign >> table has triggers enabled. >> > > I think it's probably worth finding out why COPY is so much worse in the= =20 > presence of triggers. Is there something we can do to improve that, at=20 > least so it's no worse? > I did a bit of reading on CopyFrom() and found comments that clarifies why the use of COPY for batch inserts is slower when the target foreign table has triggers. /* * It's generally more efficient to prepare a bunch of tuples for * insertion, and insert them in one * table_multi_insert()/ExecForeignBatchInsert() call, than call * table_tuple_insert()/ExecForeignInsert() separately for every tuple. * However, there are a number of reasons why we might not be able to do * this. These are explained below. */ if (resultRelInfo->ri_TrigDesc !=3D NULL && (resultRelInfo->ri_TrigDesc->trig_insert_before_row || resultRelInfo->ri_TrigDesc->trig_insert_instead_row)) { /* * Can't support multi-inserts when there are any BEFORE/INSTEAD OF * triggers on the table. Such triggers might query the table we're * inserting into and act differently if the tuples that have already * been processed and prepared for insertion are not there. */ insertMethod =3D CIM_SINGLE; } It forces the use of CIM_SINGLE if a BEFORE or INSTEAD OF trigger is present. This method then relies on table_tuple_insert() or ExecForeignInsert() to insert one tuple at a time. IUUC we cannot determine during execute_foreign_modify() whether a foreign table has triggers enabled on the target remote table. This lack of information suggests that we would need to query the foreign server to find out. If it's the only viable path I think that we would have issues if the the user configured to access the foreign server don't have access on catalog tables. It's showing a bit complicated to decide at runtime if we should use the COPY or INSERT for batch insert into a foreign table. Perhaps we could add a new option on CREATE FOREIGN TABLE to enable this usage or not? We could document the performance improvements and the limitations so the user can decide if it should enable or not. -- Matheus Alcantara