Reference

SQL & Redis

SQLite, PostgreSQL, Snowflake and Redis syntax, queries and everyday operations.

Scope and conventions

SQL examples name their database and assume the sample schema where indicated. Redis examples are native redis-cli commands, with optional Search/JSON features labeled. Run independent examples in your own scratch database; driver binding syntax and transaction guarantees differ.

Transactions & conflicts

Group writes and choose commit or rollback

SQLite

BEGIN is deferred by default; BEGIN IMMEDIATE attempts to acquire write capability earlier. SQLite permits one writer at a time, subject to connection/journal/busy settings. Commit/rollback and statement errors need explicit caller handling.

Fragment

SQLite · Group writes and choose commit or rollback
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
BEGIN IMMEDIATE;
UPDATE items SET qty = qty + 1 WHERE item_id = 1;
ROLLBACK;

SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.

Make the write check the current value

SQLite

Put the eligibility condition in the write and inspect the affected-row result. A zero-row update is a meaningful outcome, not proof that a stale earlier read was still valid. Wider invariants, isolation failures and retries need their own contract.

Fragment

SQLite · Make the write check the current value
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
UPDATE items
SET qty = qty - 1
WHERE item_id = 1 AND qty >= 1;

SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.

Recognize contention and retry boundaries

SQLite

A busy result can reflect the single-writer or snapshot-upgrade boundary. A busy timeout waits in supported cases; it does not repair a stale decision. Retrying may require rolling back and re-reading. These settings apply to the current local SQLite connection.

Fragment

SQLite · Recognize contention and retry boundaries
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
PRAGMA busy_timeout = 1000;
BEGIN IMMEDIATE;
ROLLBACK;

Local SQLite connection example; journal mode/session schedule must be specified separately.

Roll back part of a transaction with savepoints

SQLite

A savepoint marks a nested rollback point. ROLLBACK TO undoes later changes but keeps the named savepoint active; RELEASE removes it. Releasing an inner savepoint is not an independent durable commit.

Fragment

SQLite · Roll back part of a transaction with savepoints
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
BEGIN;
SAVEPOINT before_change;
UPDATE items SET qty = qty + 1 WHERE item_id = 1;
ROLLBACK TO before_change;
RELEASE before_change;
COMMIT;

Run the complete example in a scratch database.