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 1kiKkO-0003NS-E3 for pgsql-general@arkaria.postgresql.org; Thu, 26 Nov 2020 17:07:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kiKkM-0000N7-OO for pgsql-general@arkaria.postgresql.org; Thu, 26 Nov 2020 17:07:54 +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 1kiKkL-0000Mz-BK for pgsql-general@lists.postgresql.org; Thu, 26 Nov 2020 17:07:54 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kiKkD-0001Il-CO for pgsql-general@lists.postgresql.org; Thu, 26 Nov 2020 17:07:51 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 227835C0110; Thu, 26 Nov 2020 12:07:44 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Thu, 26 Nov 2020 12:07:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=P lbGW2IEp32cNgtrgieXsB5KVpS7IS/YJtX+tcvtg7I=; b=DXmY716sN8wodck5b Ejy7nNIVaZFtdi+6ZptpNeBx7VGvSgv/YSaFbnaoj1zOUpDKqOwjFqxr8do0Nt3G BXEw/KUo02tSiBOlgdnAfBfBYO2cDz2sJ8fIoGj5+a5yhDFtU0Mhvr+m4BmddN/a yE3MMDli/orx4B5/JiTz9Q+visda7WLfWW8rVY0APqlf86mKRrbAkNCpx0yXmf1M RgC5h7e6fLEblJhYkok+Bt1zlos7VMA0VfQ/XUnsxC2acJKUrp9Od76QSAEkcw31 DFGvWNCjz+gvpoEEYp0zbogVyK3JvdwgRCw48FB+dMLv0lDbkYNOJFMNj2RFQZEO 5AHkA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=PlbGW2IEp32cNgtrgieXsB5KVpS7IS/YJtX+tcvtg 7I=; b=lTvbh8qWx3vOaEHSkY9MmlGIPyG8af2IAltUhPNEhSFfPQtcOgVtDUCxL cXmFFF4osixIUYbuNQHV/mUJUx3C63VjDWNlMXZRqfX2VI/KjprwEDzb3otj4fCN YF2NeFopC+Bh1L6ko0Z/QkufCFG8xzvOgjoCtBFIMPr1LNU+A9b+yBwhp3GQmWmI xXKUgcxgV4Sjx5MFkdIvI52Y/tm+Y/ShYDrnnXqC3gwQlf4CFL8Yj8bGpbn8ymzx Kyo2YWuGTd7n9ImHlMPRpiAVAAne7zmxRjOktlweIO57ueUNyoN39vKyWYyulXKp hVzMhBb5CfWl1hnUYZWQLDNbmdxLQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudehvddgleekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthejredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpeetjedvteffjeeuvdelieeffeehuedthffgvdfhhffh kedvudevgedvjedufefgveenucfkphepjeehrddujedvrdduledrudefleenucevlhhush htvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: from [192.168.1.4] (75-172-19-139.tukw.qwest.net [75.172.19.139]) by mail.messagingengine.com (Postfix) with ESMTPA id 79C6F3280065; Thu, 26 Nov 2020 12:07:42 -0500 (EST) Subject: Re: postgres_fdw insert extremely slow To: Tom Lane , Mats Julian Olsen Cc: pgsql-general@lists.postgresql.org, wilhelm@duneanalytics.com References: <3862aa4d-01c7-c76b-4497-ebce1af702cc@aklaver.com> <10df210c-a1bb-f909-9a48-61ac54880df0@duneanalytics.com> <493978.1606323509@sss.pgh.pa.us> <494596.1606324345@sss.pgh.pa.us> <56a2c3cf-374d-893b-2feb-964805ff0884@duneanalytics.com> <500774.1606331937@sss.pgh.pa.us> From: Adrian Klaver Message-ID: Date: Thu, 26 Nov 2020 09:07:41 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 In-Reply-To: <500774.1606331937@sss.pgh.pa.us> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 11/25/20 11:18 AM, Tom Lane wrote: > Mats Julian Olsen writes: >> I've got some more numbers here: >> ... >> To me this does indicate some sort of networking issue, but I'm >> wondering if INSERTs are treated differently than SELECTs in >> postgres_fdw? The only feasibly explanation I have is that postgres_fdw >> does many more network calls for INSERT than for SELECT, e.g. something >> like 1 for SELECT and `n` for INSERT? > > I don't have the code in front of me, but from memory, postgres_fdw > will issue an INSERT statement to the remote for each row it has to > insert. Maybe you are indeed just dealing with spectacularly bad > network round trip times. > > You could try turning on log_statement and/or log_duration on the > remote to see if that sheds any more light about how much time is > spent executing each insertion vs. the network delay. > > There's been some recent discussion about teaching postgres_fdw to > batch insertions, which would likely be helpful in your situation. > I don't know how close that is to committable, but in any case > it couldn't see the light of day earlier than v14. In the meantime, > if you're sufficiently desperate maybe you could switch to using > dblink with manually-issued multi-row INSERTs. (This needn't > preclude continuing to access the table with postgres_fdw when > that's suitable.) So even if Mats where to break this query: INSERT INTO foreign.labels (address, labels) SELECT address_id, ARRAY_AGG(name) AS labels FROM labels GROUP BY 1 LIMIT 100; down into something like this: INSERT INTO foreign.labels (address, labels) VALUES (), (), (), (); postgres_fdw would send it as individual INSERTs? > > regards, tom lane > -- Adrian Klaver adrian.klaver@aklaver.com