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;
Item
Value
Syntax
GROUP BY (column, …) is required, then CHECK COUNT(*) or SUM(column), < or <=, and a non-negative integer literal
Check cost
O(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
Violation
ERR ASSERTION_VIOLATION retryable=0 …: reports the group and the enforced limit (4 for < 5)
At creation
Reads the table and fails if it already violates the rule. Waits for in-flight writes, then creates
With multiple cores
One 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)
Item
Value
State column
NOT NULL, integer, char(N), or varchar(N), not a tuple key. One per column
ON INSERT
States a row may be inserted with
ON UPDATE
Allowed previous => next pairs. Writing the same value again is allowed. An UPDATE that does not change the column is not checked
ON DELETE FROM
States a row may be deleted from. If omitted, every DELETE is allowed
Path property
With the declaration above, FINALIZED is reachable only through CHECKED, without storing any history
At creation
Rejected if an existing row has a state that the declaration does not list
PURGE
Rejected, because a released tuple key could make a row's state history start over from the initial state
Comparison with other DBMSs
Rule
KDBS
Other DBMSs
Upper bound on count per group
CREATE ASSERTION … COUNT(*) <=
The standard CREATE ASSERTION is effectively unimplemented. Use a trigger plus a lock, or a counter row with CHECK
State transition
CREATE TRANSITION
Trigger (compares OLD.status and NEW.status)
Concurrency
Guaranteed by the engine (one constraint even with multiple cores)
Triggers can leave race conditions depending on the isolation level