GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Make `max_stream_count` configurable when using Bigquery Storage API · Issue #2030 · googleapis/python-bigquery

This repository was archived by the owner on Mar 6, 2026. It is now read-only.
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

Copy link
Copy link
Closed
#2051
#2039
Closed
Make max_stream_count configurable when using Bigquery Storage API#2030
#2051
#2039
Copy link
Assignees
chalmerlowe
Labels
api: bigqueryIssues related to the googleapis/python-bigquery API.

Description

@kien-truong
kien-truong
opened on Sep 24, 2024
Issue body actions

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.

python-bigquery/google/cloud/bigquery/_pandas_helpers.py

Line 840 in ef8e927

requested_streams = 1 if preserve_order else 0

python-bigquery/google/cloud/bigquery/_pandas_helpers.py

Lines 854 to 858 in ef8e927

session = bqstorage_client.create_read_session(
parent="projects/{}".format(project_id),
read_session=requested_session,
max_stream_count=requested_streams,
)

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

https://cloud.google.com/bigquery/docs/reference/storage/rpc/google.cloud.bigquery.storage.v1#createreadsessionrequest

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.

Activity

  1. product-auto-label
    added
    api: bigqueryIssues related to the googleapis/python-bigquery API.
    on Sep 24, 2024
  2. blunderbuss-gcf
    assigned
    PhongChuong
    on Sep 24, 2024
  3. kien-truong commented on Sep 24, 2024

    @kien-truong
    kien-truong
    on Sep 24, 2024
    ContributorAuthor
    More actions

    Using the default setting, in the worst-case scenario, for n-download streams, we would have to store 2n result pages in memory:

    • 1 result page inside each download thread, times n threads
    • n result page in the transfer queue between download threads and main thread
  4. PhongChuong
    assigned
    chalmerlowe
    and unassigned
    PhongChuong
    on Sep 26, 2024
  5. chalmerlowe commented on Oct 4, 2024

    @chalmerlowe
    chalmerlowe
    on Oct 4, 2024
    Collaborator
    More actions

    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).

    More to come, soon.

    Reacted by Kien Truong
  6. kien-truong commented on Oct 4, 2024

    @kien-truong
    kien-truong
    on Oct 4, 2024
    ContributorAuthor
    More actions

    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.

  7. chalmerlowe commented on Oct 4, 2024

    @chalmerlowe
    chalmerlowe
    on Oct 4, 2024
    Collaborator
    More actions

    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.

  8. gcf-merge-on-green
    closed this as completedin #2039on Oct 10, 2024
  9. gcf-merge-on-green
    closed this as completedin 7372ad6on Oct 10, 2024
  10. renovate-bot
    added a commit that references this issue on Oct 10, 2024

    feat: updates to allow users to set max_stream_count (googleapis#2039)

    This issue will close once commit 7372ad6 is merged into the 'main' branch.
    7372ad6
  11. chalmerlowe commented on Oct 10, 2024

    @chalmerlowe
    chalmerlowe
    on Oct 10, 2024
    Collaborator
    More actions

    @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.)

  12. kien-truong commented on Oct 10, 2024

    @kien-truong
    kien-truong
    on Oct 10, 2024
    ContributorAuthor
    More actions

    Thanks @chalmerlowe, can you also add the max_stream_count argument to 2 methods to_dataframe_iterable and to_arrow_iterable?

    python-bigquery/google/cloud/bigquery/table.py

    Line 1811 in 7372ad6

    def to_arrow_iterable(

    python-bigquery/google/cloud/bigquery/table.py

    Line 1976 in 7372ad6

    def to_dataframe_iterable(

    Those 2 methods are where this is most useful to support incremental fetching use cases.

  13. chalmerlowe commented on Oct 10, 2024

    @chalmerlowe
    chalmerlowe
    on Oct 10, 2024
    Collaborator
    More actions

    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.

  14. chalmerlowe
    reopened this on Oct 10, 2024
  15. chalmerlowe
    added a commit that references this issue on Nov 1, 2024

    feat: updates to allow users to set max_stream_count (#2039)

    This issue will close once commit 6d5e765 is merged into the 'main' branch.
    6d5e765
  16. chalmerlowe commented on Nov 7, 2024

    @chalmerlowe
    chalmerlowe
    on Nov 7, 2024
    Collaborator
    More actions

    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.

  17. chalmerlowe commented on Nov 13, 2024

    @chalmerlowe
    chalmerlowe
    on Nov 13, 2024
    Collaborator
    More actions

    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.

  18. kien-truong commented on Nov 16, 2024

    @kien-truong
    kien-truong
    on Nov 16, 2024
    ContributorAuthor
    More actions

    Cheer, I've already created the PR for that here #2051

  19. Linchin
    closed this as completedin #2051on Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @chalmerlowe
    chalmerlowe

Labels

api: bigqueryIssues related to the googleapis/python-bigquery API.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.