Tuple Key (Keystone)

Every KDBS row has a 48-bit identifier called the tuple key. It is the row's identity for as long as the row lives, and it cannot be updated. The design lets any structure refer to a row by this value instead of by an address, and KDBS calls it the Keystone. The manual does not call it a "primary key", because it is not a business key.

Key Specs

ItemValue
Range1 to 2^48 - 1 (281,474,976,710,655). 0, negative values, and larger values raise OutOfRange
LocationFor a BTREE table (the default), the first column. It must be an integer type
Who sets the valueDecided on each INSERT. If a value is given, that value is used; if omitted, the engine issues one (ascending)
UpdateNot allowed (UNSUPPORTED)
ReuseThe key of a committed row stays reserved even after DELETE. It is reused only after PURGE FROM t WHERE … releases it
Rolled-back keyReleased immediately
Read by namekeyidentity: reads the key of any table, including a hidden key

How It Works

A BTREE table is stored in tuple key order. Whatever order the keys arrive in, each row is placed where its key sorts, so the rows in every page are in key order. As a result:

  • WHERE cust_id = 42 is a single descent of the B+ tree (Lookup). There is no separate primary key index.
  • WHERE ord_id BETWEEN a AND b is a range scan (Range) that seeks to the low end and stops once it passes the high end.
  • ORDER BY <tuple key> (ascending) is free. The compiler removes the clause. Descending order with a LIMIT also reads from the high end and stops.
  • WHERE ord_id IN (1, 7, 42) is a key set (key_set=3) that descends once per value.

It resembles a SQL Server clustered index or an Oracle IOT, but differs in that the engine issues the key and forbids updates.

What happens to a declared primary key

When you declare a PRIMARY KEY, the engine decides once, based on the shape of the declaration.

Declared keyIn BTREEIn HEAP
A single first column of int64The tuple key itselfThe tuple key itself (a visible column)
Integer, date, and bool columns that fit in 48 bits combined (such as (int32, int16))Embedded key: the key is the row's identity, with no index. Stored in key orderA unique index beside a hidden tuple key
Anything else (strings, wide integer combinations, decimal)A unique index beside a hidden tuple key, and the row is placed by the hash of the keyA unique index beside a hidden tuple key

The sandbox's grade_cd(grade varchar(16) PRIMARY KEY, …) is the third case. WHERE grade = 'VIP' first looks at the position (home=) computed from the key hash.

step 0 IndexProbe grade_cd home=37877333572008
  filter 0:0.1 = 'VIP'

Why keys are not reused

If the key of a deleted row were given to a new row, other structures that still hold the old key (caches, external systems, Waystone traces) would point at the wrong row. KDBS removes this problem on the engine side by keeping a committed key reserved until PURGE releases it explicitly. A table with CREATE TRANSITION rejects PURGE, because a released key could restart a row's state history from the beginning.

HEAP Tables

CREATE TABLE … HEAP places rows in data pages in no key order, and every key access goes through an index. If the first column has an int64 PRIMARY KEY, that is the tuple key; otherwise the engine prepends a hidden int64 key (not visible in SELECT *). For tables with frequent row deletes or key changes, the manual recommends HEAP, which reclaims deleted space and dead index entries in the background. BTREE reclaims them only through PURGE and PURGE INDEX.

Comparison with Other DBMSs

KDBSSQL ServerOraclePostgreSQL
Default storageB+ tree clustered on the tuple keyClustered index (if present)Heap (IOT is optional)Heap
Row identifier48-bit tuple key, a valueRID or clustered keyROWID (an address)ctid (an address)
Key issuanceEngine, issued if omittedIDENTITYSequenceSERIAL/IDENTITY
Reuse of deleted keysNot until PURGEPossiblePossiblePossible

In the Articles

Related pages