asyncpg uses async for for streaming database results.
1import asyncpg2import asyncio34async def stream_users():5 conn = await asyncpg.connect("postgresql://localhost/db")67 # Cursor-based streaming8 async with conn.transaction():9 async for record in conn.cursor("SELECT * FROM users"):10 process(record) # Process one row at a time1112 await conn.close()1314# With SQLAlchemy15from sqlalchemy.ext.asyncio import AsyncSession1617async def stream_products(db: AsyncSession):18 result = await db.stream(select(Product))19 async for row in result:20 yield row2122# Batch processing23async def process_large_dataset():24 conn = await asyncpg.connect("postgresql://localhost/db")2526 batch_size = 100027 offset = 02829 while True:30 records = await conn.fetch(31 "SELECT * FROM large_table OFFSET $1 LIMIT $2",32 offset, batch_size33 )34 if not records:35 break3637 for record in records:38 process(record)3940 offset += batch_size4142 await conn.close()4344asyncio.run(stream_users())
Benefits: