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 1lBjCd-0004A6-Br for pgsql-general@arkaria.postgresql.org; Mon, 15 Feb 2021 19:06:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lBjC6-0006Jv-Nq for pgsql-general@arkaria.postgresql.org; Mon, 15 Feb 2021 19:06:02 +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 1lBjC6-0006Jn-CP for pgsql-general@lists.postgresql.org; Mon, 15 Feb 2021 19:06:02 +0000 Received: from mail-il1-x130.google.com ([2607:f8b0:4864:20::130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lBjC3-00009Q-D8 for pgsql-general@lists.postgresql.org; Mon, 15 Feb 2021 19:06:01 +0000 Received: by mail-il1-x130.google.com with SMTP id o7so6354411ils.2 for ; Mon, 15 Feb 2021 11:05:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=f8WnExPsr9RHRNi6OoW0yeX6V15UA40XdwvvOQ4168g=; b=oA5FOJHooe42gIpexUcMW2CWKAeQfc4mXb2tCVrLm+RzGosFu/RDR6hAyGSTjnOa9U 3Tk2hWEGnRQXe9KKDqvSy8CRZV/p7Y3ToIGtuhX5/znhytwNKKZ8x/tkkbZ06iW0ElXW /XBLdGqj7cHpRSWDwJX7PBA0/2agfQ8VAIzCnz/HN8r6fGZQDL1ftTt/Jg4QPce4hHPx bADhdAVKhNl8WarOHMif5CJmX+VCcphmXIkbuutZl2VL5Qoiy+3jAzH54u5wEcheY8Nm I7PSaTYK88hFZVzRMy+eVUmWeszV+g1x5rRP5KCeCFSwmNG0iKnHfIVbmT9DMdYpDSbp dk3w== 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:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=f8WnExPsr9RHRNi6OoW0yeX6V15UA40XdwvvOQ4168g=; b=q6N83FtcdmQG2ZGHSkjgCo7D/kU0mO4PoeujPid48Ty8P3BcDuSY7tVo1LJGo+gCaL 6QoobjrevcFltzaEHikUmvwNaFkoQsZaNSYhxrUH9v9lp4Lz+Gr2W525RcVfbUxmNi0F pdH3XDfaj/kAJmODPX/X3vsFKutx/TdNuSbcMxtHdylNyKeWe1BrRp34gu8knxAkte03 au8acxaN9wXSnLmMUMZsH/Xaoq0i8r6HrHXPqe7oXsPIw+nAwjMKmgK6ryvxueFkkxKT XGIEucez4/fw9L0XzjxJ6zeumbO+x8eI8XN908uXsIzdaukPgN0HbH8QM4LjWYk0V8NO oYQg== X-Gm-Message-State: AOAM531erYAk3AC5bF3ZQ0TzC866JqXcdm6B0C3ObPhcOhJyGVpIMICw 1k/mdLISGNbTHVSK+fLgkPSgu8f7ur4= X-Google-Smtp-Source: ABdhPJyy2QjRx9vqfvIc1C+bQ4UuBvkwQ/LzVVK1GRUJB562zjOP2akl7J7xwgShJStPruJlG+ADnw== X-Received: by 2002:a92:2c04:: with SMTP id t4mr14116177ile.99.1613415958670; Mon, 15 Feb 2021 11:05:58 -0800 (PST) Received: from ?IPv6:2601:681:5500:dde0:216a:ac73:ce4:9476? ([2601:681:5500:dde0:216a:ac73:ce4:9476]) by smtp.gmail.com with ESMTPSA id u7sm9812830ilb.61.2021.02.15.11.05.58 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 15 Feb 2021 11:05:58 -0800 (PST) Subject: Re: Insert into on conflict, data size upto 3 billion records To: pgsql-general@lists.postgresql.org References: <2a876340-e227-c96a-1d79-f8a74aa45e4a@gmail.com> From: Rob Sargent Message-ID: <5ebe9fe2-3546-14f4-cd59-0b7335e26a40@gmail.com> Date: Mon, 15 Feb 2021 12:05:57 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-CA Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2/15/21 11:41 AM, Karthik K wrote: > exactly, for now, what I did was, as the table is already partitioned, I > created 50 different connections and tried updating the target table by > directly querying from the source partition tables. Are there any other > techniques that I can use to speed this up? also when we use on conflict > statement for both insert and update does Postgres uses batching > internally (committing for every 10000 records etc) or will it update > all records at once, in that case, does it create a version for each > record and do swap all at once? I'm wondering how atomicity is > guaranteed, also if I have to do batching other than selecting from > individual partitions does doing it batches of 10000 records help? > > What is your ratio of inserts versus update? Can you separate the inserts and updates? Is the target table indexed other than on primary key? If so can they be dropped? Assuming you use \copy to load the batch tables I've found this strategy to be effective: index batch on id --update first begin update target t set "all fields" from batch b where t.id = b.id and b.id between "hi" and "low" commit increment hi low, avoid overlap; repeat --insert begin; insert into target as select b.* from from batch b where not exists (select 1 from target v where b.id = v.id) and b.id between "hi" and "low" commit increment hi, low, avoid overlap; repeat