You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 6, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Mar 6, 2026. It is now read-only.
Make max_stream_count configurable when using Bigquery Storage API #2030
Currently, for API that can use BQ Storage Client to fetch data like to_dataframe_iterable or to_arrow_iterable, the client library always uses the maximum number of read streams recommended by BQ server.
This behavior has the advantage of maximizing throughput but can lead to out-of-memory issue when there are too many streams being opened and result are not read fast enough: we've encountered queries that open hundreds of streams and consuming GBs of memory.
BQ Storage Client API also suggests capping max_stream_count when resource is constrained
Typically, clients should either leave this unset to let the system to determine an upper bound OR set this a size for the maximum "units of work" it can gracefully handle.
This problem has been encountered by others before and can be worked-around by monkey-patching the create_read_session on the BQ Client object: #1292
However, it should really be fixed by allowing the max_stream_count parameter to be set through public API.
I am putting together a PR for this.
It is not as simple as just adding a max_stream_count argument because historically the argument preserve_order has been used to make some choices regarding the number of streams to use.
We have some logic to allow those two to interact in a way that makes sense and hopefully doesn't break backward compatibility.
We are also issuing a docstring to explain the method as a whole (this feels necessary since we have a new argument that may interact with another argument).
@kien-truong let me know if the updates meet the need.
The code is available in main and will included in the next release. (I don't have a date yet for the next release.)
The original changes to this issue have been merged to python-bigquery version 3.27.0.
Not sure when 3.27.0 will make it to PyPi, but as of this moment, it is available github.
I have not had the bandwidth to take the additional requests here.
Activity
kien-truong commentedon Sep 24, 2024
Using the default setting, in the worst-case scenario, for n-download streams, we would have to store
2nresult pages in memory:1result page inside each download thread, timesnthreadsnresult page in the transfer queue between download threads and main threadchalmerlowe commentedon Oct 4, 2024
I am putting together a PR for this.
It is not as simple as just adding a
max_stream_countargument because historically the argumentpreserve_orderhas been used to make some choices regarding the number of streams to use.We have some logic to allow those two to interact in a way that makes sense and hopefully doesn't break backward compatibility.
We are also issuing a docstring to explain the method as a whole (this feels necessary since we have a new argument that may interact with another argument).
More to come, soon.
kien-truong commentedon Oct 4, 2024
Right, you can't use more than 1 stream if you need to preserve row order when using ORDER BY, the concurrency will screw up the output.
chalmerlowe commentedon Oct 4, 2024
Alright. Got a draft PR. Still working on the unit tests.
But I need to focus on something else right now. Will come back to this with a clear head.
feat: updates to allow users to set max_stream_count (googleapis#2039)
chalmerlowe commentedon Oct 10, 2024
@kien-truong let me know if the updates meet the need.
The code is available in
mainand will included in the next release. (I don't have a date yet for the next release.)kien-truong commentedon Oct 10, 2024
Thanks @chalmerlowe, can you also add the
max_stream_countargument to 2 methodsto_dataframe_iterableandto_arrow_iterable?python-bigquery/google/cloud/bigquery/table.py
Line 1811 in 7372ad6
python-bigquery/google/cloud/bigquery/table.py
Line 1976 in 7372ad6
Those 2 methods are where this is most useful to support incremental fetching use cases.
chalmerlowe commentedon Oct 10, 2024
I can give it a try, but I won't be able to tackle it today. I have a few other things on my plate. Will keep you posted.
feat: updates to allow users to set max_stream_count (#2039)
chalmerlowe commentedon Nov 7, 2024
The original changes to this issue have been merged to python-bigquery version 3.27.0.
Not sure when 3.27.0 will make it to PyPi, but as of this moment, it is available github.
I have not had the bandwidth to take the additional requests here.
chalmerlowe commentedon Nov 13, 2024
The original changes to this have been released to PYPI as 3.27.0. Will see about adding the other changes when my schedule opens up.
kien-truong commentedon Nov 16, 2024
Cheer, I've already created the PR for that here #2051