← Offstack All Offstack Docs Topic & How-To Guides v15.0.0 · ~6 KB

SQL Backend Architecture

How Pro and Enterprise editions map key/value, relational, and multi-cloud state safely onto your existing SQLAlchemy-backed enterprise databases.

Current Status (v15.0.0): This file preserves the original pre-wire-in architectural design note. The wire-in is long complete: all 96 AWS services and multi-cloud stores run on SQLAlchemy Core, and 12 RDBMS backends (SQLite, DuckDB, PostgreSQL 16.15, MySQL 8.4, MariaDB 11.4, Dolt, CockroachDB, Firebird 4.0, MSSQL 2022, Oracle Free 23, IBM Db2 12.1.6, and SAP HANA 2.0 SPS08) are live-verified end to end. See docs/DATABASES.md for current backend rules and live matrices.

Status at time of writing: dynamodb/database.py is implemented and unit-verified. The storage engine still uses the SQLite DBAPI directly; wiring it to Database is what makes the "runs on any RDBMS" claim true end to end.

What database.py provides

PiecePurpose
Database(url)One engine + connection for any SQLAlchemy URL
Database.execute(sql, params)DBAPI-shaped; rewrites ? → :pN quote-aware
Database.execute_manysame, batched
Database.transaction()commit on success, rollback on raise
Database.upsert_sql(table)native upsert for SQLite / PostgreSQL / MySQL / MariaDB
upsert(db, table, values)portable; UPDATE-then-INSERT fallback for Oracle / SQL Server / Databricks
metadata_objtable definitions, so DDL is dialect-generated
url_from_path(path)maps the engine's historical db_path onto a URL

Dialect-specific tuning lives in Database._setup_dialect: WAL, busy timeout and foreign keys are applied only to SQLite.

Wire-in steps (dynamodb/storage_engine.py)

  1. Constructor. Replace

self.conn = sqlite3.connect(db_path, ...) with self.db = Database(url_from_path(db_path)). Keep self.conn = self.db so the 26 with contextlib.closing(conn.cursor()) sites keep working — Database has no cursor(), so either:

fetchall/close that delegates to Database.execute, or

Prefer the cursor() shim: it keeps the diff reviewable and leaves the query text untouched.

  1. PRAGMAs. Delete every PRAGMA / VACUUM / pool-read block from the

constructor (lines ~131-420). They are SQLite tuning and now belong to _setup_dialect. Keep the self.lock write lock — it is the engine's own serialisation and every dialect needs it.

  1. Read pool. Delete _read_conns / _read_avail / _borrow_read /

keep-fresh / _wal_truncate machinery. Server engines pool internally; SQLite's WAL is handled by the dialect. _borrow_read() becomes a context manager yielding self.db under self.lock.

  1. Upserts. Replace the 4 INSERT OR REPLACE INTO (items ~1017, ~1153,

~1578, ~1846, ~1883) and 1 INSERT OR IGNORE INTO (tables_meta ~725) with upsert(self.db, <table>, {...}) / a duplicate check for the IGNORE case. Real AWS CreateTable on an existing table is already rejected explicitly above the insert, so the IGNORE can become an explicit existence check.

  1. Schema. Delete _SCHEMA_TABLES_META / _SCHEMA_ITEMS /

_SCHEMA_GSI_INDEX and the CREATE INDEX block; metadata_obj.create_all in Database.__init__ replaces them.

  1. begins_with. The LIKE construction is dialect-fine but must escape

% and _ (also an AWS-correctness fix): build LIKE ? ESCAPE '\' and escape the prefix.

  1. Substring/contains (if added). LIKE '%x%' is portable; Postgres has

ILIKE but DynamoDB contains is case-sensitive, so plain LIKE is right.

Testing the portability claim

and must stay green.

OFFSTACK_TEST_DATABASE_URLS (comma-separated). With none set, it runs the SQLite case only. With a Postgres URL it runs the same CRUD/Query/Scan/ BatchWrite/Transact assertions against that database. This is how a contributor verifies a new dialect without a CI matrix.

"verified by dialect handling, unverified against a live instance".

Known per-dialect caveats

EngineCaveat
SQLiteSingle writer; WAL only helps readers. The engine's write lock is doing the real work.
PostgreSQLTEXT keys are fine; no varchar limit issues. Epoch-millisecond columns must be BigInteger — INTEGER is 32-bit and overflows.
MySQL/MariaDBVerified 2026-11-10 against MySQL 8.4. Five schema rules apply, all detailed in DATABASES.md: key columns carry an explicit width (never TEXT), every index must fit InnoDB's 3072-byte budget, LogGroupName columns declare the ascii charset, every service MetaData is registered with mark_binary_collation() because MySQL's default collation is case-insensitive and DynamoDB keys are not, and binary payloads declare package_binary() because LargeBinary renders as a 65,535-byte BLOB that cannot hold a Lambda package.
OracleEmpty string is NULL; sk default '' must become a sentinel or a nullable-with-coalesce column. Least-effort fix: store ''-keys as a single space or a sentinel string in the engine, consistently.
SQL ServerNo UPDATE ... FROM needed here; the TOP rewrite in limit_clause covers the one pagination site.
DatabricksNo multi-statement transactions on older runtimes; TransactWriteItems atomicity cannot be honoured. Document as read/write-compatible, not transaction-compatible.

SQLite, PostgreSQL and MySQL/MariaDB are verified end to end — every service boots, creates its tables and answers a round-trip on each. Oracle, SQL Server and Databricks remain best-effort: their dialect handling exists and upsert_sql raises NotImplementedError so upsert() takes the portable path, but none has been run against a live server.


Want a live walkthrough or evaluation setup for Offstack? Message George Vranjesevic or reach out at george@developmentsnapshot.com.