Documentation
¶
Overview ¶
Package profile provides pre-configured database profiles for different workload types. Each profile optimises SQLite settings for specific use cases like read-heavy operations, write-intensive workloads, or mixed scenarios.
Pre-configured profiles:
- ReadLight, ReadBalanced, ReadHeavy for reader connection pools
- WriteLight, WriteBalanced, WriteHeavy for the serialised writer
- Attached for per-schema PRAGMAs on attached databases
Use New to build a custom profile from scratch.
Index ¶
- type AutoVacuum
- type JournalMode
- type LockingMode
- type Pragma
- type Profile
- func (p *Profile) Apply(db *sql.DB) error
- func (p *Profile) ApplyPool(db *sql.DB)
- func (p *Profile) Clone() *Profile
- func (p *Profile) Custom(name Pragma, value any) *Profile
- func (p *Profile) DSNPragmas() []string
- func (p *Profile) SchemaStatements(schema string) []string
- func (p *Profile) String() string
- func (p *Profile) WithAutoVacuum(mode AutoVacuum) *Profile
- func (p *Profile) WithBusyTimeout(ms int) *Profile
- func (p *Profile) WithCacheSize(kibibytes int) *Profile
- func (p *Profile) WithConnMaxLifetime(d time.Duration) *Profile
- func (p *Profile) WithForeignKeys(enabled bool) *Profile
- func (p *Profile) WithJournalMode(mode JournalMode) *Profile
- func (p *Profile) WithLockingMode(mode LockingMode) *Profile
- func (p *Profile) WithMMapSize(bytes int64) *Profile
- func (p *Profile) WithMaxIdleConns(n int) *Profile
- func (p *Profile) WithMaxOpenConns(n int) *Profile
- func (p *Profile) WithPageSize(bytes int) *Profile
- func (p *Profile) WithQueryOnly(enabled bool) *Profile
- func (p *Profile) WithRecursiveTriggers(enabled bool) *Profile
- func (p *Profile) WithSecureDelete(enabled bool) *Profile
- func (p *Profile) WithSynchronous(mode SynchronousMode) *Profile
- func (p *Profile) WithTempStore(store TempStore) *Profile
- type SynchronousMode
- type TempStore
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AutoVacuum ¶
type AutoVacuum string
AutoVacuum defines available auto_vacuum modes
const ( AutoVacuumNone AutoVacuum = "NONE" AutoVacuumFull AutoVacuum = "FULL" AutoVacuumIncremental AutoVacuum = "INCREMENTAL" )
type JournalMode ¶
type JournalMode string
JournalMode defines available journal modes for SQLite
const ( JournalDelete JournalMode = "DELETE" JournalTruncate JournalMode = "TRUNCATE" JournalPersist JournalMode = "PERSIST" JournalMemory JournalMode = "MEMORY" JournalWal JournalMode = "WAL" JournalOff JournalMode = "OFF" )
type LockingMode ¶
type LockingMode string
LockingMode defines available locking modes
const ( LockingNormal LockingMode = "NORMAL" LockingExclusive LockingMode = "EXCLUSIVE" )
type Pragma ¶ added in v0.3.0
type Pragma string
Pragma identifies a SQLite PRAGMA by name.
const ( PragmaJournalMode Pragma = "journal_mode" PragmaSynchronous Pragma = "synchronous" PragmaForeignKeys Pragma = "foreign_keys" PragmaCacheSize Pragma = "cache_size" PragmaMMapSize Pragma = "mmap_size" PragmaTempStore Pragma = "temp_store" PragmaAutoVacuum Pragma = "auto_vacuum" PragmaPageSize Pragma = "page_size" PragmaBusyTimeout Pragma = "busy_timeout" PragmaLockingMode Pragma = "locking_mode" PragmaRecursiveTriggers Pragma = "recursive_triggers" PragmaSecureDelete Pragma = "secure_delete" PragmaQueryOnly Pragma = "query_only" )
Known pragma names for use with the typed With* methods. Use Profile.Custom to set pragmas not listed here.
type Profile ¶
type Profile struct {
MaxOpenConns int
MaxIdleConns int
ConnMaxLifetime time.Duration
// contains filtered or unexported fields
}
Profile holds configuration for database connections
func Attached ¶ added in v0.3.0
func Attached() *Profile
Attached creates a profile for an attached database. Only PRAGMA settings are meaningful for attached databases - connection pool parameters (MaxOpenConns, MaxIdleConns, ConnMaxLifetime) are ignored because attached databases share the main connection pool.
Use the With* methods to configure per-schema PRAGMAs:
profile.Attached(). WithJournalMode(profile.JournalWal). WithCacheSize(-30720)
func ReadBalanced ¶
func ReadBalanced() *Profile
ReadBalanced is the recommended default for most applications. 10 connections with a 75MB cache handles moderate concurrency without excessive memory use.
func ReadHeavy ¶
func ReadHeavy() *Profile
ReadHeavy targets high-concurrency workloads where many goroutines read simultaneously. 25 connections and a 150MB cache reduce contention at the cost of higher memory use. Only worthwhile when read concurrency is the bottleneck.
func ReadLight ¶
func ReadLight() *Profile
ReadLight targets low-memory environments or applications with infrequent reads. 5 connections and a 30MB cache keep the footprint small at the cost of reduced concurrency under load.
func WriteBalanced ¶
func WriteBalanced() *Profile
WriteBalanced is the recommended default for most applications. Uses 8192 page size for better write throughput and disables auto-vacuum to avoid write amplification. The larger page size is the primary performance differentiator over WriteLight.
func WriteHeavy ¶
func WriteHeavy() *Profile
WriteHeavy targets high-volume write workloads. Uses the same 8192 page size as Balanced but doubles the cache and mmap sizes, and increases busy_timeout. The extra memory helps when the working set exceeds Balanced's cache - otherwise performance is identical.
func WriteLight ¶
func WriteLight() *Profile
WriteLight targets low-memory environments or applications with infrequent writes. Uses 4096 page size and incremental auto-vacuum, trading write throughput for a smaller on-disk footprint.
func (*Profile) Apply ¶
Apply configures a database connection with this profile. Sets pool parameters and runs PRAGMAs via db.Exec. Note that PRAGMAs applied this way only reach one connection in the pool. For reliable per-connection PRAGMAs, prefer DSNPragmas with a custom connector.
func (*Profile) ApplyPool ¶ added in v0.3.0
ApplyPool sets connection pool parameters on the database handle without running any PRAGMAs. Use this with sql.OpenDB when PRAGMAs are handled by the DSN or a custom connector.
func (*Profile) Custom ¶ added in v0.3.0
Custom sets a PRAGMA not covered by the typed With* methods. The name must be a valid SQLite identifier. Invalid names are rejected by the consumption methods (Apply, DSNPragmas, SchemaStatements).
Example:
profile.New().Custom(profile.Pragma("wal_autocheckpoint"), 1000)
func (*Profile) DSNPragmas ¶ added in v0.3.0
DSNPragmas returns the profile's PRAGMAs formatted for the modernc.org/sqlite DSN query string. Each entry is a key=value pair like "_pragma=journal_mode(WAL)". The driver applies these on every new connection, solving the per-connection PRAGMA problem that Apply has with pooled connections.
func (*Profile) SchemaStatements ¶ added in v0.3.0
SchemaStatements returns the profile's PRAGMAs as schema-qualified SQL statements for an attached database. For example, with schema "analytics", a cache_size pragma becomes "PRAGMA analytics.cache_size = -30720".
func (*Profile) WithAutoVacuum ¶
func (p *Profile) WithAutoVacuum(mode AutoVacuum) *Profile
WithAutoVacuum sets the auto_vacuum pragma
func (*Profile) WithBusyTimeout ¶
WithBusyTimeout sets the busy_timeout pragma for database lock retries. Specifies how long (in milliseconds) to wait for locks before returning SQLITE_BUSY. Higher values reduce lock contention errors but may increase latency. Common values: 5000 (5 seconds, default), 10000 (high contention), 1000 (low latency)
func (*Profile) WithCacheSize ¶
WithCacheSize sets the cache_size pragma for SQLite's page cache. Positive values specify cache size in KiB, negative values specify number of pages. Larger cache improves read performance but uses more memory. Example: -102400 = 100MB cache (recommended for most applications)
func (*Profile) WithConnMaxLifetime ¶
WithConnMaxLifetime sets the maximum amount of time a connection may be reused. Prevents accumulation of connection-specific state and handles server restarts. Common values: time.Hour (default), time.Minute*30 (high churn), 0 (unlimited)
func (*Profile) WithForeignKeys ¶
WithForeignKeys sets the foreign_keys pragma
func (*Profile) WithJournalMode ¶
func (p *Profile) WithJournalMode(mode JournalMode) *Profile
WithJournalMode sets the journal_mode pragma
func (*Profile) WithLockingMode ¶
func (p *Profile) WithLockingMode(mode LockingMode) *Profile
WithLockingMode sets the locking_mode pragma
func (*Profile) WithMMapSize ¶
WithMMapSize sets the mmap_size pragma for memory-mapped I/O. Specifies the maximum size in bytes that SQLite will use for memory-mapped files. Larger values can improve performance for read-heavy workloads. Common values: 268435456 (256MB), 536870912 (512MB), 0 (disable mmap)
func (*Profile) WithMaxIdleConns ¶
WithMaxIdleConns sets the maximum number of idle connections in the pool. Should be less than or equal to MaxOpenConns. Higher values reduce connection overhead but use more resources. Typical values: 2-12 depending on workload.
func (*Profile) WithMaxOpenConns ¶
WithMaxOpenConns sets the maximum number of open connections to the database. For read profiles: higher values allow more concurrent queries (5-25 typical). For write profiles: should be 1 due to SQLite's single-writer constraint.
func (*Profile) WithPageSize ¶
WithPageSize sets the page_size pragma for database pages. Must be a power of 2 between 512 and 65536 bytes. Larger pages reduce overhead for large records but use more memory. Common values: 4096 (default), 8192 (good for write-heavy), 16384 (large records)
func (*Profile) WithQueryOnly ¶
WithQueryOnly sets the query_only pragma
func (*Profile) WithRecursiveTriggers ¶
WithRecursiveTriggers sets the recursive_triggers pragma
func (*Profile) WithSecureDelete ¶
WithSecureDelete sets the secure_delete pragma
func (*Profile) WithSynchronous ¶
func (p *Profile) WithSynchronous(mode SynchronousMode) *Profile
WithSynchronous sets the synchronous pragma
func (*Profile) WithTempStore ¶
WithTempStore sets the temp_store pragma
type SynchronousMode ¶
type SynchronousMode string
SynchronousMode defines available synchronous levels for SQLite
const ( SyncOff SynchronousMode = "0" SyncNormal SynchronousMode = "1" SyncFull SynchronousMode = "2" SyncExtra SynchronousMode = "3" )