← SQLite with Go

SQLITE GO TUTORIAL · DATABASE/SQL · 7 LESSONS

Transactions, concurrency and queries

Transactions and RETURNING, UPSERT, WAL and busy_timeout, BEGIN IMMEDIATE, JSON functions, window functions and CTEs, and query plans.

  1. Transactions and RETURNING: all-or-nothing changes that hand back what they wrote

    Group related writes in a database/sql transaction that always rolls back on failure, read generated values with RETURNING, and classify a constraint failure by its extended SQLite result code.

    4 min · Worked lesson

  2. UPSERT with ON CONFLICT: counters, insert-if-absent and why INSERT OR REPLACE deletes rows

    Write ON CONFLICT DO UPDATE and DO NOTHING statements that use excluded values and RETURNING, and explain why INSERT OR REPLACE changes rowids and fires cascading deletes.

    4 min · Worked lesson

  3. WAL mode, synchronous and busy_timeout: readers that never wait and writers that queue

    Switch a database to write-ahead logging, explain which settings persist in the file and which are per connection, and turn SQLITE_BUSY into a bounded wait with busy_timeout.

    4 min · Worked lesson

  4. BEGIN IMMEDIATE and one writer: avoid SQLITE_BUSY_SNAPSHOT that no timeout can fix

    Explain why a deferred transaction that reads and then writes can fail with SQLITE_BUSY_SNAPSHOT regardless of busy_timeout, and prevent it with BEGIN IMMEDIATE through _txlock and a single-writer pool.

    4 min · Worked lesson

  5. JSON in SQLite: json_extract, -> and ->>, json_each and indexed generated columns

    Store JSON documents with a validity check, query paths with json_extract, -> and ->>, expand arrays with json_each and index a field through a generated column.

    4 min · Worked lesson

  6. Window functions and CTEs: running totals, top N per group and recursive queries

    Compute running totals, differences and shares with window functions, select the top rows per group with row_number, and walk series and trees with recursive common table expressions.

    4 min · Worked lesson

  7. Read EXPLAIN QUERY PLAN: SCAN, SEARCH, covering indexes, sorts and ANALYZE

    Read EXPLAIN QUERY PLAN output from Go, add indexes that turn scans into searches and remove temporary sorts, use expression indexes for computed filters, and keep statistics fresh with ANALYZE and PRAGMA optimize.

    5 min · Worked lesson