Repository navigation
perf: Stop capturing a stack trace for every database query - #6003
Open
abdelaziz-mahdy wants to merge 5 commits into
Open
abdelaziz-mahdy wants to merge 5 commits into
abdelaziz-mahdy wants to merge 5 commits into
Conversation
Every successful query captured StackTrace.current and passed it to session.logQuery, which drops it unless the query is logged (all queries, slow or failed). Walking the async stack was about 2% of CPU on a read workload. Successful queries now pass StackTrace.empty. logQuery is called synchronously from the query, so the session log manager captures the trace itself when it records the query and keeps the caller's frames.
The driver captured StackTrace.current for every statement, so that an error could carry the caller's frames. In an AOT build that was the largest single cost of a query: about 15% of the CPU of a findById call, because every capture walks the async awaiter chain. The pool now sets ignoreStackTraces, and _query takes StackTrace.current in its catch blocks instead. Taken there, in the awaiting async function, the trace is the awaiter chain of the code that ran the query, so errors keep the caller's frames and only failed queries pay for it. Requires postgres 3.5.19, which added the setting.
1 task done
The pubspec check requires every package to use the same postgres constraint. Raise serverpod_embedded_postgres, serverpod_test and serverpod_cli to ^3.5.19 to match serverpod and serverpod_database.
Resolve pubspec conflicts with the 4.1.2 version bump by regenerating the pubspecs from the templates.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every successful query paid for two
StackTrace.currentcaptures it almostnever used:
case the statement failed.
PostgresDatabaseConnection._logQuery(and the SQLite one) captured onefor
session.logQuery, which drops it unless the query is logged (allqueries, slow, or failed).
In an AOT build a capture walks the async awaiter chain. In a profile of
Note.db.findByIdthe captures were ~15% of the request's CPU.This PR:
ignoreStackTraces(postgres >= 3.5.19,constraint bumped), and takes
StackTrace.currentin_query's catch blocksinstead. Taken there, in the awaiting async function, the trace is the
awaiter chain of the code that ran the query. Errors keep the caller's
frames, and only failed queries pay.
StackTrace.emptyfor successful queries tologQuery. The sessionlog manager captures the trace itself when it records the query. It runs
synchronously from the query, so the logged trace keeps the caller's frames.
LogQueryFunction's docs describe this.Part of #6002
Measurements
Benchmark app with
findById,find(limit: 20),insertRowand a no-DBendpoint
(backend-benchmark),
AOT, macOS arm64. Retired CPU instructions per request, mean of 3 runs in
shuffled order:
findByIdfind, 20 rowsinsertRowEvery run of this PR was below every run of
mainfor the three DBrequests.
Throughput in Docker (app on 2 vCPUs, Postgres 16, k6 open model; highest
rate held at p99 < 100 ms and < 1% errors), 3 rounds:
findByIdinsertRowMedians; for the three DB scenarios every round of this PR held a higher rate
than every round of
main. The harness steps in about 4-8% increments, sothese are step counts, not exact percentages.
How it was measured: the app is compiled AOT and run natively; for each request type, k6 sends 15 s at 1,000 req/s, and the server process's retired instructions (
proc_pid_rusage(RUSAGE_INFO_V4)) before and after are divided by the number of requests. Builds ofmainand this PR were run alternately in shuffled order. I can share the scripts.Tests
test_integration/database_operations/exceptions/query_error_stack_trace_test.dart.A failed query's stack trace still includes the function that ran the
query.
endpoint_method_call_logging_test.dart. A logged query's stacktrace still includes the calling endpoint method.
util/run_tests_integration_concurrently) pass;dart analyzeis clean on this branch.Pre-launch Checklist
///).Breaking changes
None for apps. Code that implements
DatabaseSession.logQueryitself nowgets
StackTrace.emptyfor successful queries. It can callStackTrace.current, which is equivalent because it runs synchronously fromthe query. The minimum
postgresversion is now 3.5.19.