Mark schema template replacements as Stable to enable Replacer cache#1242
Merged
brandur merged 3 commits intoriverqueue:masterfrom May 8, 2026
Merged
Conversation
brandur
reviewed
May 7, 2026
Contributor
brandur
left a comment
There was a problem hiding this comment.
Thanks Igor! Looks great.
Would you mind adding an entry under the "fixed" section in the CHANGELOG. Maybe just something like:
- Mark schema replacements as `Stable` in sqlc templates, preventing query SQL from having to be reallocated over and over again.. [PR #1242](https://github.com/riverqueue/river/pull/1242).
Contributor
Author
Done -> 6d208db |
Contributor
Author
|
I'm trying to run the tests locally to debug these failures. I see that one of them passed on the third try |
Contributor
|
@IgorDeo Actually, this looks like it revealed an sqlctemplate bug by accident. I'll take the fix. |
brandur
added a commit
that referenced
this pull request
May 8, 2026
Related to #1242. Activating caching everywhere revealed a bug in that we were adding named args using `maputil.Values`. The problem with this is that `maputil.Values` returns values in an arbitrary order, resulting in all kinds of odd things potentially happening during a query as args are switched around. Here, fix the problem by appending args in the same order as incoming map keys.
Contributor
|
Opened #1243. |
brandur
added a commit
that referenced
this pull request
May 8, 2026
Related to #1242. Activating caching everywhere revealed a bug in that we were adding named args using `maputil.Values`. The problem with this is that `maputil.Values` returns values in an arbitrary order, resulting in all kinds of odd things potentially happening during a query as args are switched around. Here, fix the problem by appending args in the same order as incoming map keys.
Contributor
schemaTemplateParam (and the schema replacement in ColumnExists) never set Stable: true, so the sqlctemplate.Replacer cache introduced in riverqueue#802 is bypassed on every query. This causes regexp.ReplaceAllStringFunc to run on every SQL query, allocating a new string each time. Under production throughput this creates enough allocation pressure to cause visible memory growth — we observed +29MB in 16 minutes on a worker running river.Start() with continuous job polling. The schema value comes from the client config and is constant for the lifetime of a River client, so marking it Stable is safe. With this change, the regex runs once per unique SQL string and then serves from cache. Fixes all three drivers: riverpgxv5, riverdatabasesql, riversqlite. Fixes riverqueue#1241
56118b5 to
41e5084
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.
schemaTemplateParam (and the schema replacement in ColumnExists) never set Stable: true, so the sqlctemplate.Replacer cache introduced in #802 is bypassed on every query. This causes regexp.ReplaceAllStringFunc to run on every SQL query, allocating a new string each time.
The schema value comes from the client config and is constant for the lifetime of a River client, so marking it Stable is safe. With this change, the regex runs once per unique SQL string and then serves from cache.
Fixes all three drivers: riverpgxv5, riverdatabasesql, riversqlite.
Fixes #1241