ASSERTION and TRANSITION

Every application has a list of things that must never go wrong: never sell more seats than exist, never let a status skip a step. Usually each service that writes re-checks these rules and handles race conditions separately. KDBS lets you declare two kinds of these rules in the schema, and the engine checks them on every write, no matter which service, script, or agent writes.

CREATE ASSERTION: an upper bound per group

-- Never sell more than 180 seats per flight
CREATE TABLE bookings (id int64, flight_no int64, passenger varchar);
CREATE ASSERTION seat_cap ON bookings GROUP BY (flight_no) CHECK COUNT(*) <= 180;

CREATE ASSERTION risk_cap ON positions GROUP BY (desk) CHECK SUM(exposure) < 1000000;
ItemValue
SyntaxGROUP BY (column, …) is required, then CHECK COUNT(*) or SUM(column), < or <=, and a non-negative integer literal
Check costO(1) per write. The engine maintains the per-group aggregate
Rejected declarations>, >=, = (a lower bound would need checks on DELETE and on decreasing UPDATE), MIN/MAX/AVG, DISTINCT, DEFERRABLE, grouping or summing by a tuple key
ViolationERR ASSERTION_VIOLATION retryable=0 …: reports the group and the enforced limit (4 for < 5)
At creationReads the table and fails if it already violates the rule. Waits for in-flight writes, then creates
With multiple coresOne constraint for the whole instance. If two cores insert into the last open slot at the same time, only one succeeds

CREATE TRANSITION: state change rules

CREATE TABLE orders (id int64, status varchar(16));
CREATE TRANSITION order_status ON orders(status)
  ON INSERT ('NEW')
  ON UPDATE ('NEW' => 'CHECKED', 'CHECKED' => 'FINALIZED',
             'NEW' => 'CANCELLED', 'CHECKED' => 'CANCELLED')
  ON DELETE FROM ('NEW', 'CANCELLED');

UPDATE orders SET status = 'FINALIZED' WHERE status = 'NEW';
-- ERR TRANSITION_VIOLATION retryable=0 transition 'order_status' on column 'status':
--     'NEW' => 'FINALIZED' is not a declared edge, row id 1 (byte 27)
ItemValue
State columnNOT NULL, integer, char(N), or varchar(N), not a tuple key. One per column
ON INSERTStates a row may be inserted with
ON UPDATEAllowed previous => next pairs. Writing the same value again is allowed. An UPDATE that does not change the column is not checked
ON DELETE FROMStates a row may be deleted from. If omitted, every DELETE is allowed
Path propertyWith the declaration above, FINALIZED is reachable only through CHECKED, without storing any history
At creationRejected if an existing row has a state that the declaration does not list
PURGERejected, because a released tuple key could make a row's state history start over from the initial state

Comparison with other DBMSs

RuleKDBSOther DBMSs
Upper bound on count per groupCREATE ASSERTION … COUNT(*) <=The standard CREATE ASSERTION is effectively unimplemented. Use a trigger plus a lock, or a counter row with CHECK
State transitionCREATE TRANSITIONTrigger (compares OLD.status and NEW.status)
ConcurrencyGuaranteed by the engine (one constraint even with multiple cores)Triggers can leave race conditions depending on the isolation level

Related pages