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 1lBjdn-0005Vx-N7 for pgsql-general@arkaria.postgresql.org; Mon, 15 Feb 2021 19:34:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lBjdm-00085C-Kk for pgsql-general@arkaria.postgresql.org; Mon, 15 Feb 2021 19:34:38 +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 1lBjdm-00084l-Ah for pgsql-general@lists.postgresql.org; Mon, 15 Feb 2021 19:34:38 +0000 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lBjdk-0000N5-34 for pgsql-general@lists.postgresql.org; Mon, 15 Feb 2021 19:34:37 +0000 Received: by mail-pf1-x42d.google.com with SMTP id z15so4760881pfc.3 for ; Mon, 15 Feb 2021 11:34:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:subject:date:in-reply-to:message-id :mime-version; bh=WD0c8drng8njetybFlWgd4TBN5nOFyzRMOclaDcrjVQ=; b=tiV9o7sX4vgbtwJzl5xeqQRdfFT7iAXd1P1GbExf+GOFvFCeuwpfcTGYVCrvAUFIKc CMQF581vnE9tPMDEgA0pJ9ih7U49x5KjCFcpwGG3EcOfMYkNYg9UmdKhs8PvayCUrrgZ YduxsGW8ZNwwQwtyaLkQ7xBc2zLj83vnooZpZ4SKwEa04VNB+X9qj5BY62e3OdalI8Mp LvTn0Nkrv88zIE8qvZ1ykUURkjo17uK43W6qpxW19bMvBKeCsBXo/NIW3OkIuGWcSEBj S71OnUfgvLhgQY6q46/5Ma5y9sW8ORN6MCWaH7/5CPt8k0Pm3uxiO5oXX7u8xrG0TshD 9X9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:subject:date :in-reply-to:message-id:mime-version; bh=WD0c8drng8njetybFlWgd4TBN5nOFyzRMOclaDcrjVQ=; b=hH6bn4UMi49fR7hPipz9mclR3l5RzX2iJ6+ocg49RKLMczX/+X84qv2OaPFIu+oCFO WC4EHBTuGXFLd8SYbMZw4ccwjnJj8xP79zzqfNGNueNaK8ah0ErKlasGA2IT6xVt+Zj1 8VgaRdnBtb0Fk8P7LRMcWiLa98402QZJ5rwIE5kWiCmOPdCW6xerzjrtBeclWv+1pBYc NiEVeI/iOqBitGD/sWJ8/FT9Dyk6VRtryLnMifdNnx9Yr9orL4uFInvf2kq/D79xtdha hjX5J04KIdMi0JnQPa4BZzNY4Xv6Cpnw6M2AYzQrNF2R80bdQQm3MsCFKieCvrkI6YWD RAXQ== X-Gm-Message-State: AOAM531DXqipq6mQl0TnZy6b7eML35yH4+mQO+PWRRyCKr3CynoKgkMQ pKs6UKyXQulSTW2dM1gBv0e0va8Pa/4= X-Google-Smtp-Source: ABdhPJxdiZmPfAKQDvV8TloHSpiC4UGAQrtRA+Jim8hYzY7Qe8BGqkqwU/Snz9+gHmcKBCOCaxFFhA== X-Received: by 2002:a63:da46:: with SMTP id l6mr16462577pgj.134.1613417673878; Mon, 15 Feb 2021 11:34:33 -0800 (PST) Received: from tim-desktop (220-235-6-168.dyn.iinet.net.au. [220.235.6.168]) by smtp.gmail.com with ESMTPSA id u142sm18523899pfc.37.2021.02.15.11.34.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Feb 2021 11:34:33 -0800 (PST) References: <2a876340-e227-c96a-1d79-f8a74aa45e4a@gmail.com> User-agent: mu4e 1.5.8; emacs 27.1.91 From: Tim Cross To: pgsql-general@lists.postgresql.org Subject: Re: Insert into on conflict, data size upto 3 billion records Date: Tue, 16 Feb 2021 06:12:49 +1100 In-reply-to: Message-ID: <87blclazw9.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Karthik K writes: > 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? > I have had pretty good success with the following strategy. However, you will need to profile/test each assumption as things vary greatly depending on data and table structure. A bit of trial and error is usually required. 1. Use the \COPY command to upload the batch data into a temporary table or at least a table with logging turned off 2. Run a stored procedure which first does updates for existing rows then one which does inserts for non-existing rows into your final table 3. If your table is partitioned, pre-process your batches into separate batches that are divided by the partition key, so instead of one big batch, multiple smaller batches. If this is not possible, break your upload up into multiple batches rather than one huge batch. 4. Optimise the update/insert statement to suit your data and table structure, dropping any unnecessary indexes and re-building them once finished (the whole upload). Note that this will need profiling as depending on the index and index structure, dropping and re-creating can be overall slower than leaving index in place. 5. Determine best times to run analyze to update table stats. Probably want to do it after each update and insert run, but sometimes, may be overall faster to just do it after each 'job' (update + insert). 6. don't forget to check the logs and watch for WAL writes being too frequent etc. Often things are tuned for 'normal' (outside bulk uploads) and are very poor for the bulk uploads. Need to make sure it is the right balance.