Server side cursor psycopg2. forward-only cursors, and that’s what you need.