Queryable Executables
This article unveils SELF, an executable format where the program itself is a SQLite database, radically collapsing traditional binary tooling into SQL. It demonstrates how a running program can store its entire state, content, and logs within its own file, simplifying deployment and enabling transactional live edits. This ingenious technical paradigm offers a 'magical' one-file solution, challenging conventional wisdom around application architecture.
The Lowdown
Building on a previous exploration, this article delves deeper into the concept of 'SELF': an executable format where the program is, surprisingly, a SQLite database. The author showcases how this innovative approach simplifies application development and deployment by leveraging SQLite's capabilities to manage not only the program's code but also its entire operational state.
- Core Mechanism: The executable itself is a SQLite database, interpreted via
binfmt_misc, which maps database rows (specifically asegmentstable) to executable code and jumps to an entry point. - Self-Contained State: A key breakthrough is that the running program can store all its state (logs, user data, website content) transactionally within the very SQLite file it executes from, negating the need for external filesystems like
/varor/tmp. - Proof-of-Concept: The
self-httpdwebserver exemplifies this by serving its website, routes, and visitor logs directly from the single executable SQLite file. - Inspiration & Distinction: While inspired by Justine Tunney's
redbean(an "Actually Portable Executable"), SELF distinguishes itself as an "Actually Queryable Executable" by using the database itself as the container, simplifying data manipulation through SQL instead of external archives. - Runtime Access: The program accesses its own database file at runtime by using
argv[0](the path to itself) to open a SQLite connection, allowing it to query and modify its own structure and data. - Tooling & Transactions: This paradigm grants programs immediate access to SQLite's rich feature set, including ACID transactions for live content editing,
sqldifffor auditing changes, and FTS5 for self-indexing without additional machinery. - Simplified Deployment: Deploying becomes as simple as
scping a single file. Redeploys are handled as data migrations, where data from the old executable's database isATTACHed andINSERTed into the new one.
Ultimately, this project demonstrates that re-envisioning an executable as a database fundamentally alters how we manage program code, data, and state, leading to a profound simplification of the application lifecycle and a return to the elegance of single-file deployments.