Batch update million records in prd DB

From: Yi Sun <yinan81(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Batch update million records in prd DB
Date: 2021-02-24 13:01:24
Message-ID: CABWY_HDoCs1JpUAvnhu6RFMxY-QZ=42xJd_nPBum=aUsJMUK7A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,

Now need to update several million records in a table in prd DB, if can use
batch update 1000 records and commit each time, if it will affect prd
application like below sample script please?

Sample script:

DO $MAIN$
DECLARE
affect_count integer;
chunk_size CONSTANT integer :=1000;
sleep_sec CONSTANT numeric :=0.1;
BEGIN

loop

exit when affect_count=0;

UPDATE tbl a
SET name = ''
WHERE a.id IN (SELECT id
FROM tbl b
WHERE name IS NULL
LIMIT chunk_size);

GET DIAGNOSTICS affect_count = ROW_COUNT;

commit;

PERFORM pg_sleep(sleep_sec);

end loop;
END;
$MAIN$;

Thanks and best regards

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Atul Kumar 2021-02-24 14:16:51 Re: getting tables list of other schema too
Previous Message Julien Rouhaud 2021-02-24 12:00:50 Re: Different query result, maybe bad index