GitHubWaitlist

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

LevelWhat a transaction readsConflicts
READ COMMITTED (the default)each statement reads what was committed before it began, and the transaction’s own writesa statement that waited on a row lock runs again on what is committed; never 40001 for that
REPEATABLE READthe database as it was at BEGINa row another transaction changed and committed is 40001 at the statement. Reads are not checked, so write skew can happen, as under Postgres
SERIALIZABLEthe database as it was at BEGINas 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, DELETE and SELECT ... FOR UPDATE | NO KEY UPDATE | SHARE | KEY SHARE hold their rows until the transaction ends; SKIP LOCKED and NOWAIT work, so the FOR UPDATE SKIP LOCKED LIMIT n job queue does too.
  • Some conflicts surface at COMMIT, not at the statement. Two transactions inserting the same key: the second fails with 23505 at its next statement or at COMMIT, where Postgres makes it wait at its INSERT. A child inserted under a parent another transaction deleted fails at COMMIT with 23503. Wrap the whole transaction, COMMIT included, 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 TABLE serializes other LOCK TABLEs but does not block ordinary writers (it warns).

SERIALIZABLE

write skew, refused
-- 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. COMMIT checks them; if a transaction that committed since it began changed any of it, COMMIT fails with 40001 and 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.

psql
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 ROLLBACK drops them all.
  • COMMIT checks 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 ONLY transaction.
  • UNDO AGENT puts 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 COMMIT in 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. A ROLLBACK there undoes what came before it; a BEGIN there makes the block the client’s.
  • What can’t run in a transaction block (VACUUM, CREATE or DROP DATABASE, CREATE INDEX CONCURRENTLY, DISCARD ALL, ALTER SYSTEM, ...) fails with 25001 among other statements; alone it runs as before. SAVEPOINT, RELEASE, ROLLBACK TO and COMMIT AND CHAIN there are 25P01.
  • 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 sql op (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:

psql
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 a READ ONLY transaction.
  • At COMMIT or ROLLBACK the connection goes back to its own world. From 0.9.2 a savepoint keeps where it goes back to: a SWITCH WORLD rolled back with ROLLBACK TO no longer changes it.
  • Outside a transaction it warns and does nothing, as any SET LOCAL.
  • Never use SWITCH WORLD or a plain SET chronos.world in 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’s max_idle_ms, or a session’s own idle_in_transaction_session_timeout, changes the hour. SHOW still says Postgres’s 0.
  • People and the guest have no such timeout unless the session sets idle_in_transaction_session_timeout or idle_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; 0 waits forever) changes it.

Not here

  • Two-phase commit. max_prepared_transactions is 0; PREPARE TRANSACTION rolls back (55000 inside a transaction), and COMMIT PREPARED or ROLLBACK PREPARED find 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.

Transactions and poolers · Chronos