[Django] #27639: Add a chunk size argument to QuerySet.iterator()

62 views
Skip to first unread message

Django

unread,
Dec 25, 2016, 3:55:37 PM12/25/16
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François | Owner: nobody
Freitag |
Type: New | Status: new
feature |
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords: cursors database
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Django currently fetches results from the database in batches of
{{{GET_ITERATOR_CHUNK_SIZE}}} (currently 100). When {{{.iterator()}}} is
used, usually for a large query, specifying the batch size would allow
more control over the number of back-and-forth communications between
Django and the database.

[https://www.python.org/dev/peps/pep-0249/#fetchmany PEP249] define the
{{{size}}} argument for the {{{.fetchmany()}}} method.

* MySQL-python provides the [https://dev.mysql.com/doc/connector-python/en
/connector-python-api-mysqlcursor-fetchmany.html size] argument for
{{{.fetchmany()}}}.
* SQLite provides the
[https://docs.python.org/3.6/library/sqlite3.html#sqlite3.Cursor.fetchmany
size] argument for {{{.fetchmany()}}}.
* cx_Oracle provides [http://cx-
oracle.readthedocs.io/en/latest/cursor.html?highlight=arraysize#Cursor.arraysize
arraysize] attribute on the cursor and the [http://cx-
oracle.readthedocs.io/en/latest/cursor.html#Cursor.fetchmany numRows]
argument for {{{.fetchmany()}}}.
* Psycopg2 (used for PostgreSQL) provides the
[http://initd.org/psycopg/docs/cursor.html?highlight=itersize#cursor.itersize
itersize] attribute (used by server-side cursors, see discussion on
[https://github.com/django/django/pull/7587 This PR -- Added server side
cursors support for QuerySet iteration in PostgreSQL].

Note:
In [https://code.djangoproject.com/ticket/26530#comment:3 #26530], Anssi
Kääriäinen proposed the name {{{cursor_size}}} for this argument.

--
Ticket URL: <https://code.djangoproject.com/ticket/27639>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 26, 2016, 8:30:56 AM12/26/16
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:1>

Django

unread,
Dec 26, 2016, 3:09:29 PM12/26/16
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Adam Chainz):

* cc: me@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:2>

Django

unread,
Jan 3, 2017, 12:28:12 AM1/3/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by François Freitag):

* owner: nobody => François Freitag
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:3>

Django

unread,
Jan 11, 2017, 2:12:57 PM1/11/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by François Freitag):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/7836/ PR #7836]

--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:4>

Django

unread,
Jan 12, 2017, 8:37:45 PM1/12/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:5>

Django

unread,
Jan 13, 2017, 10:02:40 AM1/13/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* needs_better_patch: 0 => 1
* stage: Ready for checkin => Accepted


Comment:

Some test failures on Oracle must be fixed.

--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:6>

Django

unread,
Jan 15, 2017, 2:14:55 PM1/15/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by François Freitag):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:7>

Django

unread,
Jan 16, 2017, 9:08:48 AM1/16/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:8>

Django

unread,
Feb 17, 2017, 3:14:27 PM2/17/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* needs_better_patch: 0 => 1

Django

unread,
Feb 21, 2017, 11:37:19 PM2/21/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by François Freitag):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:9>

Django

unread,
Mar 13, 2017, 12:20:54 PM3/13/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by François Freitag):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:10>

Django

unread,
Mar 21, 2017, 9:26:17 PM3/21/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by François Freitag):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:11>

Django

unread,
Mar 22, 2017, 5:04:51 PM3/22/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:12>

Django

unread,
Mar 22, 2017, 5:09:59 PM3/22/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by François Freitag):

* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:13>

Django

unread,
Mar 23, 2017, 4:11:13 AM3/23/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:14>

Django

unread,
May 10, 2017, 1:44:53 PM5/10/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* needs_better_patch: 0 => 1

* stage: Ready for checkin => Accepted


Comment:

Since it was marked as RFC, the PR received some updates to allow
`chunk_size=None` to disable server-side cursors (use case in #28062). A
few review comments remain.

--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:15>

Django

unread,
May 14, 2017, 9:53:08 PM5/14/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by François Freitag):

After reworking this PR to allow {{{chunk_size=None}}}, I'm not convinced
with {{{chunk_size=None}}}:

- It is introduced to workaround an issue with PostgreSQL, I do not
believe iterator should change for that reason.
- It's a PostgreSQL-only parameter, because server-side cursors cannot be
disabled on Oracle, and it'll be ignored on databases that don't support
server-side cursors.
- Its meaning is ambiguous. I would expect chunk_size=None to signify "do
not use chunked fetch and fetch all the results at once", i.e. use
{{{fetchall}}}. Django uses {{{fetchmany}}}.
- In most use cases I can think of, server-side cursors need to be either
globally enabled/disabled, not on a per-query basis. If a end-user really
want to do so, it's possible to setup an other connection and use
{{{using()}}} to disable server-side cursors for that query. I have a hard
time coming up with a use-case where third party libraries want to use
iterator, but not with a server-side cursor.

--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:16>

Django

unread,
May 15, 2017, 7:16:57 AM5/15/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

Until a use case arises, omitting support for `chunk_size=None` is fine
with me.

--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:17>

Django

unread,
May 24, 2017, 10:39:37 PM5/24/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: cursors database | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by François Freitag):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:18>

Django

unread,
Jun 1, 2017, 6:59:09 PM6/1/17
to django-...@googlegroups.com
#27639: Add a chunk size argument to QuerySet.iterator()
-------------------------------------+-------------------------------------
Reporter: François Freitag | Owner: François
| Freitag
Type: New feature | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: cursors database | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"edee5a8de6afb34a9247de2bb73ab66397a6e573" edee5a8d]:
{{{
#!CommitTicketReference repository=""
revision="edee5a8de6afb34a9247de2bb73ab66397a6e573"
Fixed #27639 -- Added chunk_size parameter to QuerySet.iterator().
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27639#comment:19>

Reply all
Reply to author
Forward
0 new messages