Refactored postgres pub sub logic into a separate function.