use pg subquery for psycopg2