Skip to content

Conversation

@kevinmingtarja
Copy link
Collaborator

@kevinmingtarja kevinmingtarja commented Oct 20, 2025

Instead of making a separate query to get_user after add_or_update_user, we should return the user object directly from the upsert operation by using the RETURNING clause (when possible, see discussion below regarding sqlite-specific handling).

This reduces one RTT to the DB for every request execution.

Benchmarked with multitime -n 50 sky status --no-show-managed-jobs --no-show-services --no-show-pools:

  • Local API server
  • GCP Cloud SQL Postgres, in us-west1 (oregon)
  • 2k clusters, 100 users

Before:

            Mean        Std.Dev.    Min         Median      Max
real        7.913       1.838       6.061       7.608       16.661

After:

            Mean        Std.Dev.    Min         Median      Max
real        7.466       0.962       5.922       7.378       9.626

Mean and median decreases by around 1 RTT, also lower std dev and max, which is expected.

Tested (run the relevant ones):

  • Code formatting: install pre-commit (auto-check on commit) or bash format.sh
  • Any manual or new tests for this PR (please specify below)
  • All smoke tests: /smoke-test (CI) or pytest tests/test_smoke.py (local)
  • Relevant individual tests: /smoke-test -k test_name (CI) or pytest tests/test_smoke.py::test_name (local)
  • Backward compatibility: /quicktest-core (CI) or pytest tests/smoke_tests/test_backward_compat.py (local)

@kevinmingtarja
Copy link
Collaborator Author

/quicktest-core

@SeungjinYang
Copy link
Collaborator

I do think SQLite supports RETURNING, https://sqlite.org/lang_returning.html

@kevinmingtarja
Copy link
Collaborator Author

I do think SQLite supports RETURNING, https://sqlite.org/lang_returning.html

You're right, thanks for pointing it out! I just assumed there isn't since we weren't using it on the sqlite code path.

@kevinmingtarja
Copy link
Collaborator Author

I do think SQLite supports RETURNING, https://sqlite.org/lang_returning.html

Note: The RETURNING syntax has been supported by SQLite since version 3.35.0 (2021), but SQLAlchemy only added support for it since 2.0 (2023). In any case, I've added a check for both to fallback to doing an additional query in case the sqlite/sqlalchemy version being used does not support it yet.

References:

@kevinmingtarja
Copy link
Collaborator Author

I verified that the sqlite and sqlalchemy version that our docker image has by default does support RETURNING:

% docker run berkeleyskypilot/skypilot-nightly:latest pip show sqlalchemy | grep Version
Version: 2.0.44
% docker run berkeleyskypilot/skypilot-nightly:latest python -c 'import sqlite3; print(sqlite3.sqlite_version)'
3.46.1

@kevinmingtarja
Copy link
Collaborator Author

kevinmingtarja commented Oct 20, 2025

/smoke-test 🟢
/smoke-test --postgres -> Failures are unrelated it seems, test_nonexistent_bucket and test_upload_to_existing_bucket. It's only failing with --postgres, and is also failing on master.

@kevinmingtarja kevinmingtarja marked this pull request as ready for review October 20, 2025 21:15
Copy link
Collaborator

@SeungjinYang SeungjinYang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Couple of minor syntactic nits

@kevinmingtarja
Copy link
Collaborator Author

/smoke-test -k minimal --kubernetes

@kevinmingtarja kevinmingtarja merged commit d282999 into master Oct 21, 2025
21 checks passed
@kevinmingtarja kevinmingtarja deleted the status-optim-2 branch October 21, 2025 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants