summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorsumau <soumaya.mauthoor@gmail.com>2019-10-28 15:22:08 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2019-10-30 10:50:44 -0400
commitd36b1f7f03841b9b346a6fd3395dd29333dce588 (patch)
tree507f1cca6605dcb767074f0adfdf883fa2ace164 /lib/sqlalchemy
parent12623517def001caa275c2af172405e336c731ab (diff)
downloadsqlalchemy-d36b1f7f03841b9b346a6fd3395dd29333dce588.tar.gz
Use simple growth scale with any max size for BufferedRowResultProxy
The maximum buffer size for the :class:`.BufferedRowResultProxy`, which is used by dialects such as PostgreSQL when ``stream_results=True``, can now be set to a number greater than 1000 and the buffer will grow to that size. Previously, the buffer would not go beyond 1000 even if the value were set larger. The growth of the buffer is also now based on a simple multiplying factor currently set to 5. Pull request courtesy Soumaya Mauthoor. Fixes: #4914 Closes: #4930 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/4930 Pull-request-sha: 66841f56e967c784f7078a787cec5129462006c8 Change-Id: I6286220bd9d488027fadc444039421a410e19a19
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/dialects/postgresql/psycopg2.py3
-rw-r--r--lib/sqlalchemy/engine/result.py33
2 files changed, 11 insertions, 25 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg2.py b/lib/sqlalchemy/dialects/postgresql/psycopg2.py
index 1a4db1108..14d49ee15 100644
--- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py
+++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py
@@ -137,7 +137,8 @@ The following DBAPI-specific options are respected when used with
interpreted by the :class:`.BufferedRowResultProxy`, and if omitted the
buffer will grow to ultimately store 1000 rows at a time.
- .. versionadded:: 1.0.6
+ .. versionchanged:: 1.4 The ``max_row_buffer`` size can now be greater than
+ 1000, and the buffer will grow to that size.
.. _psycopg2_executemany_mode:
diff --git a/lib/sqlalchemy/engine/result.py b/lib/sqlalchemy/engine/result.py
index 733bd6f6a..004a84da5 100644
--- a/lib/sqlalchemy/engine/result.py
+++ b/lib/sqlalchemy/engine/result.py
@@ -1486,10 +1486,8 @@ class BufferedRowResultProxy(ResultProxy):
The pre-fetching behavior fetches only one row initially, and then
grows its buffer size by a fixed amount with each successive need
- for additional rows up to a size of 1000.
-
- The size argument is configurable using the ``max_row_buffer``
- execution option::
+ for additional rows up the ``max_row_buffer`` size, which defaults
+ to 1000::
with psycopg2_engine.connect() as conn:
@@ -1497,7 +1495,7 @@ class BufferedRowResultProxy(ResultProxy):
stream_results=True, max_row_buffer=50
).execute("select * from table")
- .. versionadded:: 1.0.6 Added the ``max_row_buffer`` option.
+ .. versionadded:: 1.4 ``max_row_buffer`` may now exceed 1000 rows.
.. seealso::
@@ -1506,34 +1504,21 @@ class BufferedRowResultProxy(ResultProxy):
def _init_metadata(self):
self._max_row_buffer = self.context.execution_options.get(
- "max_row_buffer", None
+ "max_row_buffer", 1000
)
+ self._growth_factor = 5
self.__buffer_rows()
super(BufferedRowResultProxy, self)._init_metadata()
- # this is a "growth chart" for the buffering of rows.
- # each successive __buffer_rows call will use the next
- # value in the list for the buffer size until the max
- # is reached
- size_growth = {
- 1: 5,
- 5: 10,
- 10: 20,
- 20: 50,
- 50: 100,
- 100: 250,
- 250: 500,
- 500: 1000,
- }
-
def __buffer_rows(self):
if self.cursor is None:
return
size = getattr(self, "_bufsize", 1)
self.__rowbuffer = collections.deque(self.cursor.fetchmany(size))
- self._bufsize = self.size_growth.get(size, size)
- if self._max_row_buffer is not None:
- self._bufsize = min(self._max_row_buffer, self._bufsize)
+ if size < self._max_row_buffer:
+ self._bufsize = min(
+ self._max_row_buffer, size * self._growth_factor
+ )
def _soft_close(self, **kw):
self.__rowbuffer.clear()