Created
September 16, 2015 22:31
-
-
Save pguelpa/2f47a8679a560eac6009 to your computer and use it in GitHub Desktop.
Postgres limited delete with filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export BATCH_SIZE=100000 | |
export PGPASSWORD=xyz | |
export PGHOST=x.y.z | |
export PGUSER=abc | |
export PGDATABASE=def | |
count=0 | |
until psql -c "DELETE FROM <table> WHERE id IN (SELECT id FROM <table> WHERE <time filter> <= NOW() - INTERVAL '24 hours' LIMIT ${BATCH_SIZE});" | grep -v "DELETE ${BATCH_SIZE}" | |
do | |
count=$((count + BATCH_SIZE)) | |
echo "Deleted: ${count}" | |
sleep 5 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment