I'm using psql to extract data from a redshift (based on postgres) instance, but psql/libpq collects the result in memory before writing it to a file and causes out of memory problems for large results. Using COPY TO STDOUT or FETCH_COUNT isn't an option since redshift doesn't support those.