This project provides an LD_PRELOAD shared library that intercepts SQLite API calls and transparently redirects them to a PostgreSQL backend. It's designed to allow applications hard-coded for SQLite (like Plex Media Server) to utilize the scalability, concurrency, and features of PostgreSQL without modification.
- C Hook Library (
sqlite_hook.so): Intercepts C-level SQLite calls (sqlite3_open,sqlite3_prepare_v2,sqlite3_step, etc.) usingdlfcn.h. It manages connection state and statement lifecycle. - Rust Translation Engine (
sql_translator): A high-performance Rust library linked into the C hook. It usessqlparserand regex-based preprocessing to translate SQLite-specific SQL dialects and functions into PostgreSQL-compatible syntax. - PostgreSQL Bridge: Uses
libpqto communicate with the target PostgreSQL database, handling data type mapping and error code translation.
- Transparent Interception: Works with existing binaries using
LD_PRELOAD. - Dialect Translation:
INSERT OR REPLACE->INSERT ... ON CONFLICT DO UPDATE(Schema-Aware).AUTOINCREMENT/INTEGER PRIMARY KEY->SERIAL.- SQLite functions (
strftime,ifnull,instr,last_insert_rowid,changes).
- Rich Data Types: Transparent mapping for
TEXT,INTEGER,REAL, andBLOB(BYTEA). - Transaction Support: Full support for
BEGIN,COMMIT, andROLLBACK. - Metadata Discovery: Maps
PRAGMA table_infoto PostgreSQL catalog queries. - Case Insensitivity: Maps
COLLATE NOCASEto PostgreSQL ICU collations. - Error Mapping: Translates PostgreSQL constraint violations to SQLite error codes.
- OS: Linux (tested on Ubuntu/WSL2).
- Build Tools:
gcc,make,pkg-config. - Dependencies:
libsqlite3-dev,libpq-dev. - Rust:
rustcandcargo(installed via rustup.rs).
-
Install System Dependencies:
sudo apt update sudo apt install -y build-essential libsqlite3-dev libpq-dev pkg-config
-
Build the Project:
make
This will compile the Rust engine and the C shared library.
Set the PG_CONNINFO environment variable to your PostgreSQL connection string and use LD_PRELOAD to launch your application.
# Using URI format (recommended)
export PG_CONNINFO="postgresql://user:password@localhost:5432/dbname"
# OR use standard Postgres environment variables
export PGHOST=localhost
export PGUSER=postgres
LD_PRELOAD=./sqlite_hook.so ./your_sqlite_applicationThe project includes both Rust unit tests and a comprehensive C integration test.
npm test
# OR
make test| Feature | Status |
|---|---|
Basic Interception (LD_PRELOAD) |
✅ Done |
Postgres Connectivity (libpq) |
✅ Done |
Statement Lifecycle (prepare, step) |
✅ Done |
Transaction Support (BEGIN/COMMIT) |
✅ Done |
| SQL Dialect Translation (UPSERT, etc.) | ✅ Done |
| Data Type Mapping (Blobs, Numerics) | ✅ Done |
Parameter Binding (bind_*) |
✅ Done |
Schema Discovery (PRAGMA table_info) |
✅ Done |
Global Functions (strftime, rowid) |
✅ Done |
Case Insensitivity (NOCASE) |
✅ Done |
| Error Code Mapping | ✅ Done |
-
PRAGMA foreign_key_listandPRAGMA index_list. - Support for Virtual Tables and FTS5 mapping.
- Connection pooling for high-concurrency applications.
- Support for custom SQLite extensions.