MagicStack / asyncpg

A fast PostgreSQL Database Client Library for Python/asyncio.
Apache License 2.0
6.95k stars 402 forks source link

Explain iterable cursors in the documentation #677

Open wrobell opened 3 years ago

wrobell commented 3 years ago

When running

await conn.cursor(sql, prefetch=100)

I am getting error

asyncpg.exceptions._base.InterfaceError: prefetch argument can only be specified for iterable cursor

Looking into the documentation, I cannot find anything about iterable cursors. How do I declare them?

elprans commented 3 years ago

Here's the relevant part of the documentation: https://magicstack.github.io/asyncpg/current/api/index.html#cursors.

Iterable cursors are cursors that are used directly as iterator rather than an object that calling fetch(), so

# cursor with manual control
# prefetch doesn't make sense, because
# you specify the number of rows fetched explicitly
# every time
cursor = await conn.cursor(sql)
rows = await cursor.fetch(100)
# iterable cursor
# runs "fetch(100)" behind the scenes on every
# 100th iteration.
async for record in conn.cursor(sql, prefetch=100):
   ...