From: Alban Hertroys <haramrae@gmail.com>
To: Israel Brewster <ijbrewster@alaska.edu>
Cc: Christopher Browne <cbbrowne@gmail.com>
Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: UPDATE many records
Date: Mon, 6 Jan 2020 21:29:25 +0100
Message-ID: <91870B50-CDFB-489C-BE01-59B281A9CDCD@gmail.com> (raw)
In-Reply-To: <8AB099DA-99B4-421A-BD15-638A367EE722@alaska.edu>
References: <97518FCA-F286-4A6A-803F-46A109196C71@alaska.edu>
<CAFNqd5X5FXzFL+vhx=mg40gv0nPV9cJ6yJZU9f5hFYCUbkg31Q@mail.gmail.com>
<8AB099DA-99B4-421A-BD15-638A367EE722@alaska.edu>
> On 6 Jan 2020, at 21:15, Israel Brewster <ijbrewster@alaska.edu> wrote:
>
>> On Jan 6, 2020, at 10:08 AM, Christopher Browne <cbbrowne@gmail.com> wrote:
>>
>> On Mon, 6 Jan 2020 at 13:36, Israel Brewster <ijbrewster@alaska.edu> 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=new_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).
>>
>> Given that each row update is completely independent of any other row, I have the following questions:
>>
>> 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’t be too bad (there are a number of logical segregations I can apply), I’m just wondering if there is a more automatic option.
>>
>> Yeah, I'd be inclined to do this in batches.
I think you’re overcomplicating the matter.
I’d just do it as a single update in one transaction. It’s 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’re looking at a one-off situation that won’t require more than a few workarounds - after which you can just run the update again.
Ad 1). No harm has been done, it’s 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.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: haramrae@gmail.com, ijbrewster@alaska.edu, cbbrowne@gmail.com
Subject: Re: UPDATE many records
In-Reply-To: <91870B50-CDFB-489C-BE01-59B281A9CDCD@gmail.com>
References: <97518FCA-F286-4A6A-803F-46A109196C71@alaska.edu> <CAFNqd5X5FXzFL+vhx=mg40gv0nPV9cJ6yJZU9f5hFYCUbkg31Q@mail.gmail.com> <8AB099DA-99B4-421A-BD15-638A367EE722@alaska.edu> <91870B50-CDFB-489C-BE01-59B281A9CDCD@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox