Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ioZ0K-00023A-4B for pgsql-general@arkaria.postgresql.org; Mon, 06 Jan 2020 20:29:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ioZ0H-0006fX-IA for pgsql-general@arkaria.postgresql.org; Mon, 06 Jan 2020 20:29:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ioZ0H-0006e4-64 for pgsql-general@lists.postgresql.org; Mon, 06 Jan 2020 20:29:33 +0000 Received: from mail-wr1-x42f.google.com ([2a00:1450:4864:20::42f]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ioZ0E-0007MK-JK for pgsql-general@lists.postgresql.org; Mon, 06 Jan 2020 20:29:32 +0000 Received: by mail-wr1-x42f.google.com with SMTP id c14so51247489wrn.7 for ; Mon, 06 Jan 2020 12:29:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=2kW4k2GFa5mjCDa2BERJ5XiUk+lXTf4LdILdLjLSDMs=; b=K71pjJjx1KQy19WVdTIDCguYVJDpo+iwdYvTZCWLyVDzhch/yMbNqHUEISUdsyAtAC 3kuzLTans/z5RT87fUSBEek5CsFHz0+UWVc6mYxdtj4SZrhknbPZthEC1flB5s46IREM qssYG8fpUDC3SSIlCuRsGRsUsolegBi8gEou4OHWwzS2G04K9P1JXmTZnH95QueNzhLC Vahd9fuEGkqEa+dvgWU/gHuB+RIrOXb+0ZgdnqeJgpH/RiS4pfTqOWZiBmzmDXXFiFnz /8D7sx+ZnP3TxH5b5WGqjbpULAcoS9sRfSjFJjGWTnNh2kK7bw+DlKDIDneJ2nLSh6Bw yGaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=2kW4k2GFa5mjCDa2BERJ5XiUk+lXTf4LdILdLjLSDMs=; b=U0EGNTrNHk45CC+B/JzT9Us3yLPZRSQ32YeFoeVtf66PU1idva80bRTM4Dw2h9NRBC 4kwj9fHTb5IG7rf+x0PsmRBvrCAe4RvZDDJpCaoHmaj+RX2OZnrli7Xoo7ODLMSaK0z7 KCQyTganuYxStc5lM8elz8L59AAr/2HmYQ+DR/01Q1yppVwctNgCuwd4kQOXVdFERL6F XvNh7YRoWT9VAG+1kBnXNmohGiQz/vL2U04TTE6AR0T3uJBkfnXfRs11C1RXg8fwBXV+ 1uzMtF68Zl8XZN4+2Gmwb9mcnYxO9PXDTjJGuLiNxzLlZ1YVoCYwtqTRJ2sYmyOxiQnH gulw== X-Gm-Message-State: APjAAAWEZvKWHOVd1yuXcSkDMxZHj4bRJsu2B3GUYbUApGwicxXrVDfD vQ90mQaqgOl8aQ+P8nLxqms= X-Google-Smtp-Source: APXvYqyQTKTJKfYSv6L1hm/PuPNix2nIfpl8+AQmPUyfDuFBUm1TWAOp+BghKJ3gjwwmF/V8RMbylg== X-Received: by 2002:adf:fc0c:: with SMTP id i12mr110845262wrr.74.1578342567419; Mon, 06 Jan 2020 12:29:27 -0800 (PST) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id h66sm25332636wme.41.2020.01.06.12.29.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Jan 2020 12:29:26 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: Re: UPDATE many records From: Alban Hertroys In-Reply-To: <8AB099DA-99B4-421A-BD15-638A367EE722@alaska.edu> Date: Mon, 6 Jan 2020 21:29:25 +0100 Cc: Christopher Browne , "pgsql-generallists.postgresql.org" Content-Transfer-Encoding: quoted-printable Message-Id: <91870B50-CDFB-489C-BE01-59B281A9CDCD@gmail.com> References: <97518FCA-F286-4A6A-803F-46A109196C71@alaska.edu> <8AB099DA-99B4-421A-BD15-638A367EE722@alaska.edu> To: Israel Brewster X-Mailer: Apple Mail (2.3445.104.11) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On 6 Jan 2020, at 21:15, Israel Brewster = wrote: >=20 >> On Jan 6, 2020, at 10:08 AM, Christopher Browne = wrote: >>=20 >> On Mon, 6 Jan 2020 at 13:36, Israel Brewster = wrote: >> Thanks to a change in historical data, I have a need to update a = large number of records (around 50 million). The update itself is = straight forward, as I can just issue an "UPDATE table_name SET = changed_field=3Dnew_value();" (yes, new_value is the result of a stored = procedure, if that makes a difference) command via psql, and it should = work. However, due to the large number of records this command will = obviously take a while, and if anything goes wrong during the update = (one bad value in row 45 million, lost connection, etc), all the work = that has been done already will be lost due to the transactional nature = of such commands (unless I am missing something). >>=20 >> Given that each row update is completely independent of any other = row, I have the following questions: >>=20 >> 1) Is there any way to set the command such that each row change is = committed as it is calculated? >> 2) Is there some way to run this command in parallel in order to = better utilize multiple processor cores, other than manually breaking = the data into chunks and running a separate psql/update process for each = chunk? Honestly, manual parallelizing wouldn=E2=80=99t be too bad (there = are a number of logical segregations I can apply), I=E2=80=99m just = wondering if there is a more automatic option. >>=20 >> Yeah, I'd be inclined to do this in batches. I think you=E2=80=99re overcomplicating the matter. I=E2=80=99d just do it as a single update in one transaction. It=E2=80=99s= only 50M rows. It may take half an hour or so on decent hardware, = depending on how resource-intensive your function is. If that fails[1], only then would I start looking into batching things. = But then you still need to figure out why it fails and what to do about = that; if it fails it will probably fail fast, and if not, then you=E2=80=99= re looking at a one-off situation that won=E2=80=99t require more than a = few workarounds - after which you can just run the update again. Ad 1). No harm has been done, it=E2=80=99s a single transaction that = rolled back. Alban Hertroys -- If you can't see the forest for the trees, cut the trees and you'll find there is no forest.