Skip to content

add a sqlite history backend#2352

Merged
slingamn merged 4 commits intoergochat:masterfrom
slingamn:sqlite.6
Mar 13, 2026
Merged

add a sqlite history backend#2352
slingamn merged 4 commits intoergochat:masterfrom
slingamn:sqlite.6

Conversation

@slingamn
Copy link
Copy Markdown
Member

cc @felix @NyaaaWhatsUpDoc

  1. This uses modernc.org/sqlite via the standard database/sql API
  2. Performance is OK after I enabled WAL and synchronous(NORMAL)
  3. The recommended setting here is still db.SetMaxOpenConns(1). I wasn't really confident in an alternative strategy? I've found sources saying that it can make sense to run multiple concurrent readers and a single writer, but the workload here is write-heavy so it could lead to starvation?
  4. sqlite is compiled out by default, but I may reconsider (more discussion on figure out and document all build tag decisions #2348)

@slingamn slingamn added this to the v2.18 milestone Mar 11, 2026
@NyaaaWhatsUpDoc
Copy link
Copy Markdown

it is possible to set maxOpenConns to higher than that :) just a few things to keep in mind, all of which we've documented fairly extensively in our gotosocial sqlite driver initialization here: https://codeberg.org/superseriousbusiness/gotosocial/src/commit/a2cbe8cf28d3271c52c6032cb9ea1841ea606ada/internal/db/bundb/driver_sqlite.go#L44

@slingamn
Copy link
Copy Markdown
Member Author

@NyaaaWhatsUpDoc since I'm still using modernc.org/sqlite, what was the concurrency issue you experienced with it?

@slingamn
Copy link
Copy Markdown
Member Author

Merging this because I have other stuff pipelined on top, but comments are still welcome

@slingamn slingamn merged commit 768c01c into ergochat:master Mar 13, 2026
1 check passed
@NyaaaWhatsUpDoc
Copy link
Copy Markdown

NyaaaWhatsUpDoc commented Mar 13, 2026

@slingamn the issue is that the concurrent interrupt goroutine they use in their Query() and Exec() database/sql implementations can put their sqlite connection type into a bad state in which connections return "INTERRUPTED" forever and never recover. i've never been able to make a test case that reveals it in their repo, though i have tried. you can show it in gotosocial still though if you load our repo, remove the "replace modernc.org/sqlite" line in our go.mod to stop using my workaround, then run go run -tags='debug nowasm' ./cmd/gotosocial testrig start to start a test server, open up a web browser and spam refresh on "http://localhost:8080/@admin", you'll quickly see it come about in the logs.

you can find my 1 commit workaround for it here: https://gitlab.com/NyaaaWhatsUpDoc/sqlite/-/compare/master...workaround%2Fremove-concurrent-interrupt-calls?from_project_id=9241019

and as you can see from the tags here i've been maintaining additional ${version}-concurrency-workaround tags since 2024: https://gitlab.com/NyaaaWhatsUpDoc/sqlite/-/tags

you might want to keep an eye on https://github.com/ncruces/go-sqlite3, at the moment it relies on a WebAssembly runtime, but it's currently undergoing an underlying rewrite that uses transpiled WebAssembly to Go code as seen here which i've been helping a little with. in my opinion, the ncruces/go-sqlite3 code is a lot easier to reason with if you ever have issues to upstream fixes for, and doesn't use garbage-collector-opaque "uintptrs" all over the place like the transpiled modernc.org/sqlite does.

for the moment if you want i can submit a PR that switches you to my own fork without that issue? or i can just submit a PR when the WebAssembly-free release of https://github.com/ncruces/go-sqlite3 is ready :). or you can just stick with modernc.org/sqlite as it is too, obviously, these are all just my suggestions!

@slingamn
Copy link
Copy Markdown
Member Author

Thanks! I am planning to release Ergo v2.18 with modernc.org/sqlite and a recommended max of 1 connection. I would definitely consider ncruces/go-sqlite-3 for v2.19, since it appears to be API-compatible with database/sql and also compatible at a file format level with all other sqlite implementations.

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.

2 participants