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 1vEGX7-008cqH-0X for pgsql-hackers@arkaria.postgresql.org; Thu, 30 Oct 2025 00:28:52 +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 1vEGX5-004sSo-Vr for pgsql-hackers@arkaria.postgresql.org; Thu, 30 Oct 2025 00:28:51 +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 1vEGX5-004sSg-Md for pgsql-hackers@lists.postgresql.org; Thu, 30 Oct 2025 00:28:50 +0000 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vEGX2-004yZ3-21 for pgsql-hackers@postgresql.org; Thu, 30 Oct 2025 00:28:50 +0000 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-7a59ec9bef4so699498b3a.2 for ; Wed, 29 Oct 2025 17:28:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761784127; x=1762388927; darn=postgresql.org; h=in-reply-to:references:cc:to:from:subject:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=cKdinJ+OvVMFdDJ9G+0U0+S2EARhtu5p7euvQ01e+2o=; b=T3ELTgnaA58iXFMnQ3PIUVu1RJHtQaj2T0/E5B7nj+y5QR2i7ocUtvB3F/Y3XHNf8u SAMYMsD08FZJrLpiZCfqtvIjnu3OMU86zdkxwiH4rc8XcWb5tMEmgDWUrd7tZMA6Mn87 rQs9e7KMSYqF8hSNMKY/CbLzH3zvm8QBq4ogqJXZWkocn9BlVczfgcE1XWgGTEH2NCd/ 0tIFH/rUuDmFMGTtghtNrPOGtMZ1U1ZmdM7Cz14ZJ1Nt76/JKxy6ubDV3LGZpCnfluQ1 fcub9WSoLOtvtPoxKBmagUoIc3mKPwiRpa6FhSL6xE9KjpweMaGw0JzNthSZV4toCoSo HSCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761784127; x=1762388927; h=in-reply-to:references:cc:to:from:subject:message-id:date :content-transfer-encoding:mime-version:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=cKdinJ+OvVMFdDJ9G+0U0+S2EARhtu5p7euvQ01e+2o=; b=OntBw78xgfGGA39qjOKgPdt2uNzGOyxkPA81UlMQqg5/gcK5Zo9Xd+vDAbSVYeEt4K dObFAE0elSSbWsX4DDmOPfY/+ICma1FEiQmT73Wt6L/kREp3y8v0GOsK2IfNxEOhhsXB OvYKbD7h/DGtF+n77UVQi9JF2MfF971t6IaOSDV+nn9T5mCvO9UA4YV9+OPvIADFDyqh TNPC9EwDawxejRrj4bspJHuA5F0sgGD1nHAtaQU20gXXLqxKpo8NaYbrw4ucufl59tK6 NoDxxCKMQ2yY8JJmb36jcASuRSUy9cr8EIEq30b7ZGEa/wS2LrUJtZ+9hiU97BIfuBip XMrw== X-Forwarded-Encrypted: i=1; AJvYcCV2gBO7+cugsTZocQxx7s+FkN+svwwJ+3Xq6TR4tUSzVqPMHwHRAOIrMSKKTyxdvBNPjcmZr8FkjGzxi2yM@postgresql.org X-Gm-Message-State: AOJu0YxJI76mR5w8JhB/rZEV6Tc/jcv0wlA5Nd+umW6JE+nIgHTYL2RU jMPnbXx/LD7WUVPw8SuOQxyExPFMu1oNtgjoAvgWrpe9Q+seyf54GUTi X-Gm-Gg: ASbGncskH2GufxPue9HHSPUxyNrzwDYDDQzo2mHfGiYhQuDowKzLvr61j+QQGjKaVoy T32KISi55FA7ND68jqX4FfGRASmoxXf514N3+F5OIihTr5noU+W+COieYXNbVba+v8DO9yy6udD PVDhX+hFjphJoPc9P2W1xrqFS47F7ptc+AByixhi97Meo/ttOMzU7GZoGj+C4MwLeRMJbd5zSP9 heevccRbgksqKrbo3T4Mhhxj5SrTjTpckBT8iT0GgEL944h2fvPqXTpNEI/wDjbsqyTXx3pgLRF sFLAKSMwpd52YX7sztMfcl5esavq6JLrdxpHOaB2CT6wDB/sJEUHsm+jkFfgUNOZIMCJfkdEN0W jl5xADUWBgxhRbNnidltEYfzhn0DWBFR3ePlHvpeNdbviH3CSuhatcnUvf5UEnzpkNpXZOdOsTu YZIhA7jdB0Vv2YyR3JhzjWXJ4= X-Google-Smtp-Source: AGHT+IHuf9yh+J6mVvYo9vN9PYo8Ev6xCZCdc1c39pWIerrcIMI9VNG8E4noY5U2QQfilXLCZvaocQ== X-Received: by 2002:a05:6a20:6a0c:b0:33e:6885:2bd0 with SMTP id adf61e73a8af0-34659c3494cmr5837255637.41.1761784126573; Wed, 29 Oct 2025 17:28:46 -0700 (PDT) Received: from localhost ([2804:14d:328a:a59c:f817:1d4b:5dab:62d0]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a414065418sm16793968b3a.41.2025.10.29.17.28.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Oct 2025 17:28:45 -0700 (PDT) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Wed, 29 Oct 2025 21:28:41 -0300 Message-Id: Subject: Re: postgres_fdw: Use COPY to speed up batch inserts From: "Matheus Alcantara" To: "jian he" , "Matheus Alcantara" Cc: "Tomas Vondra" , X-Mailer: aerc 0.21.0 References: In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed Oct 29, 2025 at 12:10 AM -03, jian he wrote: > On Sat, Oct 25, 2025 at 2:27=E2=80=AFAM Matheus Alcantara > wrote: >> >> On this new version I also added some regress tests on postgres_fdw.sql >> > > In the CopyFrom function, we have the CopyInsertMethod, CIM_SINGLE is slo= wer > than CIM_MULTI, I think. > We should do performance tests for the case where the COPY statement is l= imited > to use CIM_SINGLE. > > You can use triggers to make COPY can only use the CIM_SINGLE copymethod. > for example: > create function dummy() returns trigger as $$ begin return new; end $$ > language plpgsql; > create trigger dummy > before insert or update on batch_table_3 > for each row execute procedure dummy(); > > My local tests show that when batch_size is greater than 2, COPY performs= faster > than batch inserts into a foreign table, even though COPY can only use > CIM_SINGLE. > However, my tests were done with an enable-assert build, since I > encountered issues compiling the release build. > > anyway, I am sharing my test script. >=20 I've benchmarked using buildtype=3Drelease with Dcassert=3Dfalse and buildtype=3Ddebug with Dcassert=3Dtrue and in both cases I've got a worst performance when using the COPY for batching insert into a a foreign table with a trigger. See the results (best of 4 runs). Batch using INSERT batch_size: 100 buildtype=3Ddebug=20 Dcassert=3Dtrue tps =3D 13.596754 Batch using COPY batch_size: 100 buildtype=3Ddebug=20 Dcassert=3Dtrue tps =3D 11.650642=20 -------------------- Batch using INSERT batch_size: 100 buildtype=3Drelease=20 Dcassert=3Dfalse tps =3D 28.333161=20 Batch using COPY batch_size: 100 buildtype=3Drelease=20 Dcassert=3Dfalse tps =3D 18.499420 It seems to me that we need to disable the COPY usage when the foreign table has triggers enabled. -- Matheus Alcantara