Transactions and poolers
Isolation levels with SERIALIZABLE, a transaction across worlds, several statements as one transaction, and SET LOCAL chronos.world behind PgBouncer, Supavisor or RDS Proxy.
Chronos DB is one binary you run yourself. Releases and install script. To put agents on a database you already run, with approvals: Chronos Gateway
Which release
Isolation levels
| Level | What a transaction reads | Conflicts |
|---|---|---|
| READ COMMITTED (the default) | each statement reads what was committed before it began, and the transaction’s own writes | a statement that waited on a row lock runs again on what is committed; never 40001 for that |
| REPEATABLE READ | the database as it was at BEGIN | a row another transaction changed and committed is 40001 at the statement. Reads are not checked, so write skew can happen, as under Postgres |
| SERIALIZABLE | the database as it was at BEGIN | as REPEATABLE READ, and COMMIT checks what it read: changed by a commit since it began, the commit is 40001 |
BEGIN ISOLATION LEVEL ..., SET TRANSACTION, SET SESSION CHARACTERISTICS AS TRANSACTION and default_transaction_isolation all work, as in Postgres, and READ ONLY is enforced as Postgres enforces it (25006).
- Row locks wait, as Postgres’s do.
UPDATE,DELETEandSELECT ... FOR UPDATE | NO KEY UPDATE | SHARE | KEY SHAREhold their rows until the transaction ends;SKIP LOCKEDandNOWAITwork, so theFOR UPDATE SKIP LOCKED LIMIT njob queue does too. - Some conflicts surface at
COMMIT, not at the statement. Two transactions inserting the same key: the second fails with23505at its next statement or atCOMMIT, where Postgres makes it wait at itsINSERT. A child inserted under a parent another transaction deleted fails atCOMMITwith23503. Wrap the whole transaction,COMMITincluded, in the retry loop. - A merged world or a write through the HTTP API takes no row locks: a transaction that wrote the same row fails with
40001. LOCK TABLEserializes otherLOCK TABLEs but does not block ordinary writers (it warns).
SERIALIZABLE
-- session 1 -- session 2begin isolation level serializable; begin isolation level serializable;select count(*) from doctors where on_call; select count(*) from doctors where on_call;update doctors set on_call = false where id = 1; update doctors set on_call = false where id = 2;commit; commit; -- ERROR: could not serialize access due to -- read/write dependencies among transactions (40001)- It runs on REPEATABLE READ’s snapshot and keeps what the transaction reads: rows, key ranges, scan filters and whole tables.
COMMITchecks them; if a transaction that committed since it began changed any of it,COMMITfails with40001and nothing lands. - Write skew and Hermitage’s G2-item and G2 are refused as in Postgres.
- A transaction that wrote nothing commits unchecked.
- It can refuse more than Postgres’s SSI, never fewer. Any read/write dependency on a committed write refuses, where Postgres looks for a dangerous structure of two. Retry on
40001, as with Postgres.
A transaction across worlds
Inside a transaction, SWITCH WORLD, USE WORLD and SET [LOCAL] chronos.world move it to the other world instead of failing.
begin;insert into orders (id, status) values (42, 'new'); -- in the session's worldswitch world staging;update prices set amount = amount * 1.1; -- in stagingcommit; -- both worlds' changes land together- Each world gets its own fork of the transaction when it is first reached. Savepoints span every world, and
ROLLBACKdrops them all. COMMITchecks every world’s changes, then lands them in one step: no other commit comes between them, and after a crash all of them replay or none.- An agent may switch only to worlds it may change, or read in a
READ ONLYtransaction. UNDO AGENTputs back such a commit in every world it touched (from 0.9.2; 0.8.0 to 0.9.1 left all but the last world’s rows).
Other world statements (CREATE WORLD, MERGE WORLD, DROP WORLD, ...) still can’t run inside a transaction: 25001, with a hint naming each driver’s autocommit setting (psycopg, JDBC, SQLAlchemy) and SET LOCAL chronos.world.
Several statements in one string
A string of several statements sent in one simple Query (psql -c, a driver’s batch_execute, a migration tool) runs as Postgres runs it: as one implicit transaction.
insert ...; insert ...; insert ...: a failure in the third undoes the first two, and the rest don’t run.- A
COMMITin the string commits what came before it (with Postgres’s warning that no transaction was in progress), and what comes after is a block of its own. AROLLBACKthere undoes what came before it; aBEGINthere makes the block the client’s. - What can’t run in a transaction block (
VACUUM,CREATEorDROP DATABASE,CREATE INDEX CONCURRENTLY,DISCARD ALL,ALTER SYSTEM, ...) fails with25001among other statements; alone it runs as before.SAVEPOINT,RELEASE,ROLLBACK TOandCOMMIT AND CHAINthere are25P01. - A world statement commits what came before it and runs on its own, and the statements after it are a block of their own, so
create world x; use world x; ...works as before. - Over HTTP too: the
sqlop (POST /v1/sql) runs several statements as one transaction, and a failure undoes the ones before it.
Behind a connection pooler
The world a connection works in is session state, as search_path is. A pooler hands one server connection to many clients, so a world one client switched to must not reach the next.
Session mode
PgBouncer’s pool_mode = session: a client keeps the connection until it leaves, and the pooler sends its server_reset_query between clients. Keep it DISCARD ALL (PgBouncer’s default): it puts the connection back in the world it logged in to, with its settings, prepared statements and temporary tables.
Transaction mode
pool_mode = transaction (Supavisor’s and RDS Proxy’s default): every transaction may go out on another connection, and no reset runs between them. Name the world in each transaction:
begin;set local chronos.world = 'agent_7'; -- best first; anywhere in the transaction worksupdate orders set status = 'shipped' where id = 42;commit; -- the connection is back in its own world- The transaction runs in that world, with the same checks as
SWITCH WORLD: CONNECT on it, and for an agent the right to change it, or only to read it in aREAD ONLYtransaction. - At
COMMITorROLLBACKthe connection goes back to its own world. From 0.9.2 a savepoint keeps where it goes back to: aSWITCH WORLDrolled back withROLLBACK TOno longer changes it. - Outside a transaction it warns and does nothing, as any
SET LOCAL. - Never use
SWITCH WORLDor a plainSET chronos.worldin transaction mode: they last past the transaction, on a connection someone else gets next.
One pool per world
Works in any mode: put the world in the connection string’s database name (postgres://app@db:5433/agent_7).
Idle transactions
- An agent’s abandoned transaction lets go after an hour. An agent’s Postgres connection idle inside a transaction for an hour is dropped (FATAL
25P03) and its transaction rolled back, letting its row intents and locks go. The agent’smax_idle_ms, or a session’s ownidle_in_transaction_session_timeout, changes the hour.SHOWstill says Postgres’s0. - People and the guest have no such timeout unless the session sets
idle_in_transaction_session_timeoutoridle_session_timeout, as in Postgres. - A client that stops reading holds its locks only as long as one write may wait on it: 60 seconds by default, then the connection ends and its transaction rolls back.
CHRONOS_WRITE_TIMEOUT(seconds, from 0.9.2;0waits forever) changes it.
Not here
- Two-phase commit.
max_prepared_transactionsis 0;PREPARE TRANSACTIONrolls back (55000inside a transaction), andCOMMIT PREPAREDorROLLBACK PREPAREDfind no such transaction (42704). - A transaction holding a connection while a model thinks is still a connection held. Point agents at HTTP or MCP where you can.
Every isolation and lock detail of the SQL: SQL. Limits for the whole server: Operate and secure.