Refactor the stats consolidation scripts to work with postgresql