This project is archived and is in readonly mode.
2.4beta: server-side ("named") cursors: fetchone should really just fetch one
-
Jon
I think I may have goofed. I'm not sure, now, under what circumstances this comes into play. I don't see cursor.next() being used in SQLAlchemy. However, I still feel as though it is unwise to arbitrarily assume that 2,000 rows is an appropriate value when people are /expecting/ single-row iteration from a server-side cursor.
-
Daniele Varrazzo
- State changed from new to invalid
- Tag set to named cursor, iter, rel-2.4.0-beta2
Hello Jon,
If you want single rows from the database, you can use cur.fetchone(). The roundtrip optimization only involves iter(cur) (unless I've broken fetchone, which would be a bug indeed). This is not different from iter(file) that doesn't read the file by line but by blocks, even if iteration is then per line: this kind of buffering is not even disabled running "python -u" (unbuffered): the only way to iterate line-by-line on a file is "while 1: line = file.readline(), if not line break".
I think it is easier for an application really requiring record-by-record iteration to switch from a pattern "for record in cur:" to a pattern "while 1: record = cur.fetchone(); if not record: break" whereas the other way round is clumsy. Before 2.4 switching to a named cursor in an iter pattern implied a hidden heavy cost, requiring the clumsy workaround of the fetchmany and raising the usage bar for a feature otherwise very interesting.
I have detailed the rationale here: http://archives.postgresql.org/psycopg/2011-01/msg00020.php. I would appreciate if you could raise your opinion to the ML to allow other people to consider the option. I'm not convinced that 2000 (settable using arraysize) is a perfect default, so ideas are welcome. I'm also interested in any issue you may know of about the possible interaction of this feature with other libraries.
Thank you very much.