Running queries and calling PostgreSQL functions