add: log schema cache queries' timings - #4805
Merged
Merged
Conversation
steve-chavez
commented
Apr 11, 2026
Comment on lines
+355
to
+357
| (loadTime, summary) <- timeItT (evaluate $ showSummary sCache) | ||
| observer $ SchemaCacheQueriedObs resultTime $ dbQueryTimings sCache | ||
| observer $ SchemaCacheLoadedObs loadTime summary |
Member
Author
There was a problem hiding this comment.
Had to change the order of these statements otherwise this test failed
Lines 1247 to 1269 in c9adaed
steve-chavez
force-pushed
the
log-scache-qtimes
branch
4 times, most recently
from
April 15, 2026 20:38
8d69dc3 to
84ee742
Compare
steve-chavez
commented
Apr 15, 2026
steve-chavez
force-pushed
the
log-scache-qtimes
branch
from
April 15, 2026 21:17
84ee742 to
0c2af42
Compare
steve-chavez
marked this pull request as ready for review
April 15, 2026 21:19
steve-chavez
force-pushed
the
log-scache-qtimes
branch
from
April 15, 2026 21:21
0c2af42 to
e7d8381
Compare
laurenceisla
approved these changes
Apr 16, 2026
This adds a new log line that shows each schema cache query time individually, only on `log-level=debug`. Like so: ``` $ PGRST_LOG_LEVEL=debug postgrest-with-pg-17 -f test/spec/fixtures/load.sql postgrest-run .... 10/Apr/2026:21:48:45 -0500: Schema cache queried in 192.2 milliseconds 10/Apr/2026:21:48:45 -0500: tables: 72.027 ms, keydeps: 20.118 ms, rels: 6.189 ms, funcs: 35.010 ms, comprels: 4.319 ms, dreps: 1.614 ms, mhandlers: 7.419 ms, tzones: 43.025 ms ``` This helps debug specific schema cache queries being slow like on PostgREST#4613 (comment) and PostgREST#3046 (comment). It also closes PostgREST#3215, which main motivation was to find out which query is slow. Implementation details --------------------- To time each query inside a transaction in pure SQL, we do: ```sql -- start timer select set_config('pgrst.tmp_x', clock_timestamp()::text, false); -- run the query select <query> -- end timer select set_config('pgrst.tmp_x', (clock_timestamp() - current_setting('pgrst.tmp_x', false)::timestamptz)::text, false); -- .... repeated for every query -- at the end we capture all the timings with select extract('milliseconds' from current_setting('pgrst.tmp_x', false)::interval), extract(..; ``` Considerations -------------- Only added this on `log-level=debug` because while the queries are fast and the data is valuable, it triples the amount of queries we run during schema cache refresh, which could be troublesome on slow networks. It's possible to reduce the amount of queries by starting and stopping timers in one statement, but this would still double the amount of queries and makes the code messy, doesn't seem worth it. Also it would pollute pg_stat_statements, it's only required to debug certain extreme cases anyway.
steve-chavez
force-pushed
the
log-scache-qtimes
branch
from
April 16, 2026 17:49
e7d8381 to
04ee629
Compare
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.
This adds a new log line that shows each schema cache query time individually, only on
log-level=debug. Like so:This helps debug specific schema cache queries being slow like on #4613 (comment) and #3046 (comment). It also closes #3215, which main motivation was to find out which query is slow.
Implementation details
To time each query inside a transaction in pure SQL, we do:
Considerations
Only added this on
log-level=debugbecause while the queries are fast and the data is valuable, it triples the amount of queries we run during schema cache refresh, which could be troublesome on slow networks. It's possible to reduce the amount of queries by starting and stopping timers in one statement, but this would still double the amount of queries and makes the code messy, doesn't seem worth it.TODO