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 1khyBG-000840-Vo for pgsql-general@arkaria.postgresql.org; Wed, 25 Nov 2020 17:02:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khyBF-0004d6-NV for pgsql-general@arkaria.postgresql.org; Wed, 25 Nov 2020 17:02:09 +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 1khyBF-0004cz-Fs for pgsql-general@lists.postgresql.org; Wed, 25 Nov 2020 17:02:09 +0000 Received: from mail-lf1-x144.google.com ([2a00:1450:4864:20::144]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1khyB9-0006K1-5C for pgsql-general@lists.postgresql.org; Wed, 25 Nov 2020 17:02:08 +0000 Received: by mail-lf1-x144.google.com with SMTP id j205so4114866lfj.6 for ; Wed, 25 Nov 2020 09:02:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=duneanalytics-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:organization:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding:content-language; bh=7D36MzFWH9awBzBzkL07dyjlfwm+qC3J9cby5n5nR/Q=; b=iSjd6N5rcbpCCUELyTccLcU8ZG+YGDNXY26x/RJQLKhYGJSM/MKIg/iwkwJTfXsHQr gx/OSyEYQDnHosQuDON69J5rtRwf3Vcj89+wNKdQkiB2gSO1gn307ytm6hh2cRheqwJl HohfmzBoPb6v7iY2AxEWMVDihmzp5L0KVRZ+obHreOa5zeC9w+Vfu1uRfsq+9HHGuVcT KLgb8kXVd1BW4XhqWvN/OO9wVgaM120aJnpaedcbhXr3YY+XnTx4J1arX1OTkZO7subO gXgNx1e27f/PzqMgUPgivR7CLWQBlfSlADLqW+Ejtt4kHjaEhfm7ou0CEOZkP+K8kDSc mCfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:organization :message-id:date:user-agent:mime-version:in-reply-to :content-transfer-encoding:content-language; bh=7D36MzFWH9awBzBzkL07dyjlfwm+qC3J9cby5n5nR/Q=; b=VH1T3jnAbVGR+FM0a9sWVSzTOaF6m6YY9KwOjKVVF/Vz+d4HE6fq1G1i7mNjmMeQl7 xA1Tx5EPiKrvnXvlsGfjFlXYMsJYSA7DAQCuOvWZ/7ZDI8+JoTveyX2V9XnrnDOPjTGS qcL6KcdhCti4oBCwmATF6DUn6HBEgEpXWluFGJKBmJeEVALnmMboSIN3QlcAqxqMxpgx UHHZOOEMJgIIz5jMA2Wdm/46bd9sK3OD/h7W/62FnLe4jBEFhF+qAsz2d0PoOrNG+o5b qC7onzdiDxXGk+gPP8rJtmUi4+vWrYHmWDTqm3tWH4EPPjoHbXMrnVLxhPSn7rzNw+a1 cctQ== X-Gm-Message-State: AOAM532ytauq7YTwdi9mbL8wCJLr3seZx6QBWvYCcFftAmw0cjVtustd a8D+4v0QfYpVmpM5uWLZbaNzsFZj+PmD9N61XmI= X-Google-Smtp-Source: ABdhPJyFpObOq8uMFpzlYzUBoZwZpp17dWLn7ZtFE5+qxArY5hzIijKLsZf9TrveYJS0c+Jzv37lwQ== X-Received: by 2002:a05:6512:3137:: with SMTP id p23mr1628870lfd.67.1606323719577; Wed, 25 Nov 2020 09:01:59 -0800 (PST) Received: from [192.168.1.149] (host-37-191-231-244.lynet.no. [37.191.231.244]) by smtp.gmail.com with ESMTPSA id m16sm323543lfa.57.2020.11.25.09.01.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 25 Nov 2020 09:01:58 -0800 (PST) Subject: Re: postgres_fdw insert extremely slow To: Adrian Klaver , pgsql-general@lists.postgresql.org References: <3862aa4d-01c7-c76b-4497-ebce1af702cc@aklaver.com> <10df210c-a1bb-f909-9a48-61ac54880df0@duneanalytics.com> <1e78ec1e-ae8a-4d20-ea22-cc9a965ce98a@aklaver.com> From: Mats Julian Olsen Organization: Dune Analytics Message-ID: Date: Wed, 25 Nov 2020 18:01:57 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.3.2 MIME-Version: 1.0 In-Reply-To: <1e78ec1e-ae8a-4d20-ea22-cc9a965ce98a@aklaver.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 25.11.2020 17:57, Adrian Klaver wrote: > On 11/25/20 8:48 AM, Mats Julian Olsen wrote: >> Apologies for the sloppiness! >>> Postgres version(s)? >> >> x: Aurora PostgreSQL 11.7 on x86_64-pc-linux-gnu, compiled by gcc >> (GCC) 4.9.3, 64-bit (RDS) >> >> y: PostgreSQL 12.2 (Ubuntu 12.2-2.pgdg19.10+1) on >> x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.2.1-9ubuntu2) 9.2.1 >> 20191008, 64-bit (GCP) > > So they are separated by what network distance? > By quite a lot! One server is in eu-west-1 and the other in gcp us-central1-a. Note however, that SELECTs across the network are very fast, so it doesn't seem like this is a network issue. >> >>> >>> Without the actual query and EXPLAIN ANALYZE on it this will be >>> difficult to answer with any detail. >>> >>> Also would be nice to have the table schema. >>> >> >> Here's the query, schema and the EXPLAIN ANALYZE > > FYI, you will get more responses by including below information > directly in the email. To that end: > > On x > > CREATE TABLE labels ( >     id integer NOT NULL, >     address_id bytea NOT NULL, >     name text NOT NULL, >     author character varying(50) NOT NULL, >     type text NOT NULL, >     source text, >     updated_at timestamp with time zone DEFAULT now() NOT NULL, >     CONSTRAINT lowercase_name CHECK ((name = lower(name))), >     CONSTRAINT lowercase_type CHECK ((type = lower(type))), >     CONSTRAINT whitespace_name CHECK ((regexp_replace(btrim(name, ' > '::text), '(\s+)'::text, ' '::text, 'g'::text) = name)), >     CONSTRAINT whitespace_type CHECK ((regexp_replace(btrim(type, ' > '::text), '(\s+)'::text, ' '::text, 'g'::text) = type)) > ); > CREATE INDEX labels_label ON labels USING BTREE (name); > CREATE INDEX labels_label_type ON labels USING BTREE (type); > CREATE INDEX labels_address ON labels USING HASH (address_id); > CREATE INDEX labels_source ON labels USING BTREE (source); > > On y > > CREATE TABLE labels.labels ( >     address     bytea PRIMARY KEY, >     labels      text[] > ); > >> >> query: https://gist.github.com/mewwts/704663e19a21bc51864f3298f09a18c0 > > INSERT INTO foreign.labels (address, labels) > SELECT address_id, ARRAY_AGG(name) AS labels > FROM labels > GROUP BY 1 > LIMIT 100; > >> >> plan: https://explain.depesz.com/s/RQFQ > > Insert on labels  (cost=0.42..26.19 rows=100 width=53) (actual > time=11541.205..11541.205 rows=0 loops=1) >    ->  Limit  (cost=0.42..25.19 rows=100 width=53) (actual > time=0.068..3.549 rows=100 loops=1) >          ->  GroupAggregate  (cost=0.42..26502.02 rows=106996 > width=53) (actual time=0.066..3.449 rows=100 loops=1) >                Group Key: labels_1.address_id >                ->  Index Only Scan using > labels_address_id_type_name_key on labels labels_1 > (cost=0.42..24068.85 rows=219145 width=31) (actual time=0.054..0.414 > rows=201 loops=1) >                      Heap Fetches: 0 >  Planning Time: 0.102 ms >  Execution Time: 12797.143 ms > > >> >> >> Best, Thank you for inlining this! -- Mats CTO @ Dune Analytics We're hiring: https://careers.duneanalytics.com