Fix for failed jobs in postgresql. Generalization of SQL