PostgreSQL Extension Architecture
The KalamDB PostgreSQL extension currently operates in remote mode: PostgreSQL hosts the extension entrypoints and relation bridge, while the actual table definition and execution live in a running KalamDB server.
Main Components
- PostgreSQL 16 session: owns SQL parsing, planning, and the visible PostgreSQL-side table surface.
pg_kalamextension: registers the bridge layer, DDL interception hook, helper functions, and thekalam.user_idsession setting.- Foreign server definition: stores the remote connection settings such as
host,port, TLS material, timeout, and optional auth header. - KalamDB server: receives gRPC scan and mutation requests plus mirrored SQL DDL.
Read Path
For SELECT statements:
- PostgreSQL plans the extension-managed relation scan.
pg_kalamresolves the relation identity and the foreign server connection settings.- The extension reads the current
kalam.user_idvalue, if present. - A remote scan request is sent to KalamDB.
- The returned Arrow batches are converted into PostgreSQL tuples.
The extension reads and writes through PostgreSQL relations that were created from CREATE TABLE ... USING kalamdb.
Write Path
For INSERT, UPDATE, and DELETE statements:
- PostgreSQL routes the operation through FDW modify callbacks.
pg_kalamresolves the target table and tenant context.- Rows are converted from PostgreSQL datums into KalamDB scalar values, while
_seqstays read-only and_useridis treated as an insert-only tenant override. - Mutations are sent to KalamDB over the remote executor.
Single-row inserts are buffered so PostgreSQL transactions can batch work more efficiently. Pending writes are flushed before scans and before transaction completion so read-your-writes behavior stays predictable inside the PostgreSQL session.
Explicit Transaction Flow
When a PostgreSQL session opens an explicit transaction with BEGIN or START TRANSACTION, the extension binds staged KalamDB writes to that pg session until COMMIT or ROLLBACK.
That transaction state is observable through two server views:
system.sessionsfor the live pg bridge session and its current remote transaction metadata.system.transactionsfor the active explicit transaction itself alongside other active transaction origins.
This makes PostgreSQL transaction behavior visible without materializing extra persisted bookkeeping on the server.
DDL Forwarding Path
When you run CREATE TABLE ... USING kalamdb, the extension intercepts the statement, mirrors the user-column DDL into KalamDB, and injects the visible system columns back into the local PostgreSQL schema. The flow is:
- resolve the PostgreSQL schema and table name as the KalamDB namespace and table name,
- create the namespace in KalamDB if needed,
- create the KalamDB table from the user-defined columns and forwarded table options,
- add
_seqto every local PostgreSQL table surface and_useridto user tables, - reject explicit declarations of
_seq,_userid, and_deletedin the originalCREATE TABLE ... USING kalamdbstatement.
This is the public authoring flow for the extension. PostgreSQL WITH (...) options are forwarded to KalamDB as the mirrored table options.
Direct Statement Passthrough
The extension also exposes a direct SQL bridge:
SELECT kalam_exec('SELECT * FROM system.tables');Use kalam_exec(...) when you want to send a statement directly to KalamDB without wrapping it in PostgreSQL syntax first. This is the simplest path for server-native DDL, system queries, or commands that do not need PostgreSQL-side relation mapping.
Namespace and Schema Mapping
KalamDB namespaces map naturally to PostgreSQL schemas.
- The PostgreSQL schema becomes the KalamDB namespace.
- The PostgreSQL relation name becomes the KalamDB table name.
That lets you organize PostgreSQL schemas and KalamDB namespaces with the same naming model.
Tenant Context
kalam.user_id is a PostgreSQL session setting registered by the extension.
SET kalam.user_id = 'user-alice';
SELECT kalam_user_id();The session value is attached to remote requests and is the main way user-scoped reads, updates, and deletes are isolated. On INSERT, a visible _userid column on user tables can override the session value for that inserted row batch.
Data Definition Details
The extension preserves KalamDB-focused DDL details where possible:
PRIMARY KEYandDEFAULT SNOWFLAKE_ID()are kept in the KalamDB definition.- PostgreSQL keeps a relation surface that matches the forwarded KalamDB table definition closely enough for normal DML while exposing the extension-managed system columns locally.
SERIAL,BIGSERIAL,SMALLSERIAL, andGENERATED ... AS IDENTITYare normalized into explicit integer columns withDEFAULT SNOWFLAKE_ID()before the mirrored KalamDB DDL is sent._seqis exposed as a read-only projected column and is not treated as a writable PostgreSQL application field.