Database backends
The database layer gives each Surfpool workspace a common key-value persistence service. It supports temporary in-memory state, file-backed SQLite state, and shared PostgreSQL state so a surfnet can keep accounts, blocks, transactions, and related data across its lifetime or across restarts.
Persistence role
The database layer sits below the surfnet state system. A SurfnetSvm selects a storage backend when it starts, then opens named stores through one common storage contract. This lets higher layers use the same persistence model without choosing SQL dialects.
The database layer supports these workspace modes:
| Mode | Purpose | State lifetime |
|---|---|---|
| Memory | Fast, isolated runs with no database URL | Process and surfnet lifetime |
| SQLite | Local persistence for a surfnet | Survives restart when a file path is used |
| PostgreSQL | Shared persistence for multiple surfnet instances or processes | Survives restart in the database server |
The state and Surfnet page explains how this persistence fits into the wider workspace state model. Account storage describes the account-focused use of the same storage service, while Surfnet virtual networking describes the runtime that consumes the persisted workspace state.
Backend selection
The StorageBackend chooses the persistence mode from the optional database URL. No URL selects memory. PostgreSQL URL schemes select PostgreSQL. Other values select SQLite, including :memory: and local file paths. The selected backend then creates named stores for the surfnet. crates/…/backend.rs• L8-44 and crates/…/svm.rs• L806-827.
This choice is tied to the surfnet instance. Each backend carries the surfnet identifier so shared database records remain separated between surfnets. A storage backend reports whether its data can survive a process restart, which allows callers to distinguish durable state from in-process state. crates/…/backend.rs• L46-107.
References: crates/…/backend.rs• L8-44 crates/…/backend.rs• L93-107 crates/…/svm.rs• L806-827
Store contract
Each named store presents a key-value view to the rest of Surfpool. It supports storing, reading, taking, clearing, listing, counting, and iterating over values. Keys and values use the caller's Rust types while the database backends use a shared JSON representation. crates/…/mod.rs• L177-205 and crates/…/diesel_common.rs• L47-93.
The surfnet opens separate stores for major state groups such as accounts, blocks, transactions, token data, and bundles. These stores share the backend's database resources, while the surfnet identifier keeps their records scoped to the correct workspace. crates/…/svm.rs• L1225-1249.
References: crates/…/mod.rs• L177-205 crates/…/diesel_common.rs• L1-12 crates/…/svm.rs• L1225-1249
Database crate
The dedicated surfpool-db crate is the database dependency boundary for the workspace. It exposes Diesel and dynamic-schema support, plus the SQLite and PostgreSQL connection types when their build features are enabled. It keeps database support optional so a build can include only the engines it needs. crates/…/lib.rs• L1-6 and crates/…/Cargo.toml• L12-45.
The crate does not define the surfnet's persistence policy. surfpool-core owns backend selection and the store behavior. The database crate supplies the shared database API and driver feature wiring that those modules use. crates/…/Cargo.toml• L98-107 and crates/…/Cargo.toml• L134-143.
Feature boundary
SQLite is enabled by default in the core crate. PostgreSQL is an opt-in feature that passes through to surfpool-db. If a URL requests an engine that is not present in the build, backend selection returns a clear feature-disabled error instead of silently changing the requested mode. crates/…/Cargo.toml• L134-143 and crates/…/mod.rs• L48-70.
References: crates/…/lib.rs• L1-6 crates/…/Cargo.toml• L12-45 crates/…/Cargo.toml• L134-143 crates/…/mod.rs• L48-70
SQLite backend
The SQLite backend provides local persistence for one surfnet. A file path gives restart-safe state. :memory: gives a database that is shared by that backend's stores but isolated from other backend instances. crates/…/sqlite.rs• L46-109.
The backend creates a connection pool for its stores and prepares each database table for the selected store. File-backed databases use write-ahead logging, then checkpoint that log during shutdown so pending data is consolidated into the main database file. crates/…/sqlite.rs• L84-128 and crates/…/sqlite.rs• L131-177.
SQLite workspace flow
The flow below shows how a local database URL becomes a set of surfnet stores.
flowchart LR
A[Surfnet starts] --> B[SQLite backend opens]
B --> C[Shared pool for the surfnet]
C --> D[Named stores open]
D --> E[JSON key-value records]
E --> F[Shutdown checkpoint]
F --> G[Local database file]
The backend gives all stores in one surfnet access to the same local database. A separate backend receives a separate in-memory database, which prevents one surfnet's temporary state from leaking into another. crates/…/sqlite.rs• L69-123 and crates/…/sqlite.rs• L551-588.
References: crates/…/sqlite.rs• L46-109 crates/…/sqlite.rs• L112-128 crates/…/sqlite.rs• L131-177 crates/…/sqlite.rs• L551-588
PostgreSQL backend
The PostgreSQL backend provides durable state in a database server. It shares a connection pool by database URL, so several surfnet instances can reuse the same server resources. Each store still filters its records by surfnet identifier. crates/…/postgres.rs• L24-29 and crates/…/postgres.rs• L61-100.
The backend creates a store table when needed. A transaction-scoped database lock protects concurrent attempts to create the same table, so parallel surfnet starts do not turn a normal table-creation race into a startup failure. crates/…/postgres.rs• L119-149.
PostgreSQL workspace flow
The flow below shows how several surfnets can use one PostgreSQL database while keeping their logical state separate.
flowchart LR
A[Surfnet URL] --> B[Shared PostgreSQL pool]
B --> C[Surfnet-scoped backend]
C --> D[Named stores]
D --> E[Rows keyed by surfnet identifier]
The shared pool reduces duplicate server connections, while the surfnet identifier provides the data boundary. This allows PostgreSQL to serve as a common persistence service without merging the state of separate surfnets. crates/…/postgres.rs• L24-29 and crates/…/postgres.rs• L61-100.
References: crates/…/postgres.rs• L24-29 crates/…/postgres.rs• L61-100 crates/…/postgres.rs• L119-149
Lifecycle and isolation
Storage begins when SurfnetSvm builds its state. The backend creates the stores needed by the virtual machine, and later operations read or update those stores through the common contract. On shutdown, the SVM records its latest checkpoint and asks the backend to perform engine-specific cleanup. crates/…/svm.rs• L813-827 and crates/…/svm.rs• L1193-1249.
The isolation rule is simple: memory is isolated by backend instance, SQLite is isolated by its database, and PostgreSQL is isolated by the surfnet identifier within a shared database. This rule is central to safe parallel workspaces and to restoring a workspace from durable state.
References: crates/…/svm.rs• L813-827 crates/…/svm.rs• L1193-1249 crates/…/sqlite.rs• L551-588 crates/…/postgres.rs• L61-100
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the URL-to-backend rules in
StorageBackendaligned with the feature flags in crates/…/Cargo.toml and crates/…/Cargo.toml. - Preserve the surfnet identifier in every durable store operation. Removing that scope can mix data between workspaces that share a database.
- Keep SQLite shutdown handling aligned with the file-backed WAL lifecycle. A change to startup or shutdown can affect restart recovery.
- Keep SQLite and PostgreSQL store behavior aligned with the
Storagetrait so higher-level state code remains backend-independent. - Keep shared PostgreSQL pool reuse aligned with the database URL. A change to the pool key can increase server sessions or split stores unexpectedly.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| PostgreSQL URL used without the PostgreSQL build feature | Surfnet creation fails with a feature-disabled error | Enable the PostgreSQL feature for the build |
| SQLite file cannot provide a pooled connection | Store creation or an operation fails before state is read or written | Surface the storage error and check the local database path and access |
| Key or value cannot use the shared JSON format | The store operation fails during serialization or read-back | Use types with compatible Serde support |
| Surfnet scope is missing or wrong in a shared database | One workspace can read or overwrite another workspace's records | Preserve the configured surfnet identifier for every backend and store |
| File-backed SQLite stops before its shutdown cleanup | WAL data or side files can remain until SQLite recovers them | Call the SVM shutdown path before normal process exit |
Key files
| File | Why to read it |
|---|---|
| crates/…/backend.rs | Shows backend selection, store creation, persistence status, and shutdown behavior. |
| crates/…/mod.rs | Defines the common storage contract and storage errors. |
| crates/…/sqlite.rs | Explains local SQLite persistence and its surfnet isolation model. |
| crates/…/postgres.rs | Explains shared PostgreSQL persistence and concurrent store setup. |
| crates/…/diesel_common.rs | Shows the shared key-value data shape used by both SQL backends. |
| crates/…/lib.rs | Shows the database crate's public driver boundary. |
| crates/…/Cargo.toml | Shows which database engines are optional and how features enable them. |
| crates/…/svm.rs | Shows where a surfnet opens stores and completes backend shutdown. |