Execute several statements each as a single DB transaction.

def execute_transactions(conn, statements: Iterable):
    """Execute several statements each as a single DB transaction."""

    with conn.cursor() as cursor:
        for statement in statements:
            try:
                cursor.execute(statement)
                conn.commit()
            except psycopg2.ProgrammingError:
                conn.rollback()