Skip to content

fix(sqllab): release DB connection before fetching query results from backend - #43371

Open
justinpark wants to merge 3 commits into
apache:masterfrom
justinpark:fix/sqllab-results-release-db-connection-early
Open

fix(sqllab): release DB connection before fetching query results from backend#43371
justinpark wants to merge 3 commits into
apache:masterfrom
justinpark:fix/sqllab-results-release-db-connection-early

Conversation

@justinpark

Copy link
Copy Markdown
Member

SUMMARY

SqlExecutionResultsCommand (used by the SQL Lab results endpoint) kept the SQLAlchemy session/connection open for the entire duration of the S3/results-backend fetch and the CPU-bound decompress → deserialize → row-expansion work that follows. None of that work touches the database, but holding a connection open for it — which can run well past this endpoint's client-side timeout for large results — was exhausting the small per-worker SQLAlchemy connection pool whenever several large-result downloads landed concurrently on the same gunicorn worker.

This change warms the query.database relationship (needed later by _deserialize_results_payload for db_engine_spec) while the session is still open, then closes the session before fetching from the results backend, releasing the connection back to the pool for the remainder of the request.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

N/A - backend-only change, no UI impact.

TESTING INSTRUCTIONS

  1. Run a SQL Lab query that returns a large result set.
  2. Fetch results from /api/v1/sqllab/results/ while other large-result requests are in flight on the same worker.
  3. Confirm the request completes without exhausting the DB connection pool (e.g. no QueuePool limit ... overflow errors), and that results are still returned correctly.
  4. Existing unit tests for superset/commands/sql_lab/results.py should continue to pass.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

… backend

Close the SQLAlchemy session before the S3 fetch and the CPU-bound
decompress/deserialize/expand work in SqlExecutionResultsCommand. That
work doesn't need the DB, but holding a connection for its duration
(which can outlast this endpoint's client-side timeout on large
results) was exhausting the small per-worker connection pool when
several large-result downloads land concurrently on the same gunicorn
worker.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@dosubot dosubot Bot added change:backend Requires changing the backend sqllab Namespace | Anything related to the SQL Lab labels Aug 20, 2026
@bito-code-review

bito-code-review Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Code Review Agent Run #86b21a

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: 0dee95e..0dee95e
    • superset/commands/sql_lab/results.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

@netlify

netlify Bot commented Aug 20, 2026

Copy link
Copy Markdown

Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit 0dee95e
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/6a87855c5755e00008f71881
😎 Deploy Preview https://deploy-preview-43371--superset-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

Add two regression tests for SqlExecutionResultsCommand.validate():
- the DB session is closed before the results-backend fetch, so a slow
  S3 fetch does not hold a connection out of the pool
- the query's database relationship is warmed (loaded) before that
  close, so accessing it later (as run() -> _deserialize_results_payload
  does) works on the now-detached instance without needing a live
  session

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@pull-request-size pull-request-size Bot added size/M and removed size/S labels Aug 20, 2026
@codecov

codecov Bot commented Aug 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.80%. Comparing base (42ba2a4) to head (7fddf4d).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #43371      +/-   ##
==========================================
+ Coverage   66.73%   66.80%   +0.07%     
==========================================
  Files        2876     2876              
  Lines      164278   164062     -216     
  Branches    37900    37860      -40     
==========================================
- Hits       109636   109609      -27     
+ Misses      52479    52280     -199     
- Partials     2163     2173      +10     
Flag Coverage Δ
hive 38.10% <0.00%> (-0.01%) ⬇️
mysql 57.83% <100.00%> (+0.06%) ⬆️
postgres 57.86% <100.00%> (+0.07%) ⬆️
presto 40.04% <0.00%> (-0.01%) ⬇️
python 59.30% <100.00%> (+0.12%) ⬆️
sqlite 57.55% <100.00%> (+0.11%) ⬆️
unit 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

raise_for_access() reads g.user, so the two new tests need the same
test_request_context()/override_user() wrapping test_run_succeeds
already uses.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@bito-code-review

bito-code-review Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Code Review Agent Run #d58580

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: 0dee95e..7fddf4d
    • tests/integration_tests/sql_lab/commands_tests.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:backend Requires changing the backend size/M sqllab Namespace | Anything related to the SQL Lab

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant