(From http://www.ualberta.ca/~dostatni/moodlemoot2013/mdl_log.html)
The following script is executed daily. It generates a CSV file with all the moodle activity for previous day. That file is saved together with apache access and error logs in a log server. This data is used to verify student challenges ( eg. "I couldn't submit my assignment because there was a problem with the system"), identify trends and trace the origin of errors (to a specific code deployment).
This level of archiving allows us to remove the mdl_log entries from the active database. Currently the logs take up about half of the size of the database on disk.
copy ( select * from mdl_log where time >= extract ( 'epoch' from (current_date - interval '1 day')) and time < extract ( 'epoch' from current_date ) ) to '/backups/dumps/mdl_log.csv' with CSV HEADER FORCE QUOTE url;
/usr/bin/psql -f /var/lib/postgresql/scripts/dump_yesterdays_logs.sql moodle >> /var/log/postgresql/mdl_log.log 2>&1