Sample Header Ad - 728x90

Routinely deleting aged postgresql rows via cron

1 vote
2 answers
4296 views
I have a Django website with a postgresql 9.3.10 backend. The website contains dynamic user-generated content (think of it as a form of 9gag). There are some tables from which I routinely delete aged rows. The database is called dbname and I log into it via the user postgres. The following are two examples of such routine deletion: begin; DELETE FROM links_groupseen WHERE which_reply_id IN (SELECT id FROM links_reply where "submitted_on" < now() - interval '7 days'); DELETE FROM links_reply WHERE "submitted_on" < now() - interval '7 days'; commit; begin; DELETE FROM links_vote WHERE link_id IN (SELECT id FROM links_link WHERE id NOT IN (select answer_to_id from links_publicreply) AND "submitted_on" < now() - interval '1 hour'); DELETE FROM links_photoobjectsubscription WHERE which_link_id IN (SELECT id FROM links_link WHERE id NOT IN (select answer_to_id from links_publicreply) AND "submitted_on" < now() - interval '1 hour'); DELETE FROM links_link WHERE id NOT IN (select answer_to_id from links_publicreply) AND "submitted_on" < now() - interval '1 hour'; commit; There are ~10 other such operations I run. At the moment, I do everything manually - I want to automate these to run at a certain low-utlization time of day. How do I do that? Can someone give me an illustrative example? ------------------- As per my own rudimentary knowledge, I'm thinking I should first save the following shell script to /etc/cron.daily: #!/bin/sh dbname="dbname" username="postgres" psql $dbname $username << EOF begin; DELETE FROM links_groupseen WHERE which_reply_id IN (SELECT id FROM links_reply where "submitted_on" < now() - interval '7 days'); DELETE FROM links_reply WHERE "submitted_on" < now() - interval '7 days'; commit; begin; DELETE FROM links_vote WHERE link_id IN (SELECT id FROM links_link WHERE id NOT IN (select answer_to_id from links_publicreply) AND "submitted_on" < now() - interval '1 hour'); DELETE FROM links_photoobjectsubscription WHERE which_link_id IN (SELECT id FROM links_link WHERE id NOT IN (select answer_to_id from links_publicreply) AND "submitted_on" < now() - interval '1 hour'); DELETE FROM links_link WHERE id NOT IN (select answer_to_id from links_publicreply) AND "submitted_on" < now() - interval '1 hour'; commit; EOF And then add the said script to crontab like so: 30 4 * * * /etc/cron.daily/mypgscript And then service crond restart
Asked by Hassan Baig (2079 rep)
Jun 24, 2016, 09:39 AM
Last activity: Jun 28, 2016, 05:15 PM