Repository navigation
Conversation
Contributor
Author
|
This is a bigger change. In theory it should be worthwhile but I'll need to review it before it's ready to consider. |
elcritch
force-pushed
the
feature/owned-postgres-plan-cache
branch
from
November 13, 2026 15:12
56e87f1 to
8d7071c
Compare
elcritch
marked this pull request as ready for review
November 13, 2026 18:48
Contributor
Author
|
Got around to finishing this. The "ManagedDbConn" allows prepared statement sharing. Should help Postgres spend less time setting up queries. |
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.
What this solves
This PR lets an application reuse prepared queries in PostgreSQL and SQLite while keeping memory use and connection cleanup under control.
Imagine an app repeatedly asking, "Find the customer with this ID." The ID changes, but the query's structure stays the same. The database can save that structure as a prepared statement, which can reduce the work needed for later requests.
An earlier version of Ormin created a new saved PostgreSQL statement on every call. Those statements accumulated while the connection stayed open, causing memory use to grow. PR #87 fixed that by running PostgreSQL queries without keeping named statements. This PR adds controlled reuse back as an optional feature, and adds an owned connection with statement reuse for SQLite too.
How it works
Each backend provides a
ManagedDbConnthat owns one connection and manages its saved queries:maxPlans = 0turns caching off.The connection has one clear owner. You can transfer ownership, but you cannot copy it into two independent owners. Under ARC/ORC, when the owner leaves scope—or you explicitly call
close()—it releases its connection and saved statements. This helps prevent forgotten connections and accidental double cleanup.Inline
query:/query(T):blocks and generatedcreateProc/createItercalls accept the owned connection, including single-row queries andINSERT ... RETURNING. Existing raw PostgreSQL and SQLite connections remain available. Using a closed managed connection raisesDbError.SQLite behavior
Open an owned SQLite connection with
openManaged("app.sqlite")oropenManaged(":memory:").A SQLite statement can be used by only one active query at a time. If an outer iterator is still using a cached statement, a nested query with the same SQL gets a temporary statement. Temporary statements are finalized when the query exits; cached statements are reset and their bound values cleared. Cleanup also runs when a conversion raises or an iterator exits early.
db.reset()clears the cache without reopening the database, preserving in-memory data and transaction state. It raisesDbErrorwhile managed statements are active. SQLite'sprepare_v2can recompile statements after schema changes; execution errors are reported to the caller.Closing an owner invalidates its outstanding managed statements. SQLite's
close_v2allows outstanding temporary or borrowed handles to finish cleanup before the underlying connection is fully released.PostgreSQL behavior
Open an owned PostgreSQL connection with
openManaged("host=localhost dbname=app user=app").db.reset()reconnects and clears the cache.Saved PostgreSQL statements belong to one database session. Use a direct connection or a session pooler that keeps the same session. Transaction pooling, which can switch sessions between transactions, is unsuitable.
If a saved statement disappears or its result shape becomes invalid after a database change, Ormin reports the error and switches to ordinary execution for that session. Connection failures are also reported to the caller. Ormin does not automatically repeat a failed PostgreSQL query, because repeating something like an order insertion could create a duplicate. Callers can explicitly reset and retry when appropriate.
Ownership and interop
Use each owned connection from one thread at a time, and pass it as
var ManagedDbConnto query helpers.borrowRawprovides a temporary handle for other database APIs. Callers must not close that handle, reset a borrowed PostgreSQL session, or finalize statements owned by Ormin. Clean up any statements or handles created through the borrowed handle yourself.Validation
nim testandnim test_postgrespassed with Nim 2.2.12 against SQLite and an isolated PostgreSQL 14.25 instance.nim buildexamplespassed with the Atlas examples feature enabled.Local macOS PostgreSQL runs supplied Homebrew's libpq runtime path through compiler flags. This remains a draft for API and lifecycle review.