Skip to content

Instantly share code, notes, and snippets.

@tudormunteanu
Created November 2, 2020 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tudormunteanu/3df78d255e46f6a15b1f89a119ab26f6 to your computer and use it in GitHub Desktop.
Save tudormunteanu/3df78d255e46f6a15b1f89a119ab26f6 to your computer and use it in GitHub Desktop.
Batch delete database rows with transactions
begin;
\timing on
DELETE FROM
account AS a
USING
member AS m
WHERE
m.territory='XY' AND
m.member_id >= (:v1 + 0) * 1000 AND
m.member_id < (:v1 + 1) * 1000 AND
a.member_id = m.member_id
;
\timing off
commit;
#!/bin/bash
set -euo pipefail
URL='postgres://...'
SQL_FILE='sample.sql'
# Batches of 1000
for i in {0..100000}; do
id_=$((i*1000))
echo "=== Batch #${i}. IDs > ${id_}."
psql -v v1="${i}" -f $SQL_FILE $URL
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment