r/databasedevelopment • u/louischoi • Oct 17 '26
Tuple key (primary key) really sould be mutable? I dont think so,
Hi, Im UkJin CHOI from South Korea, Im really passinate for building database engine. Claude code makes it possible. and I found this channel. I worked as DBA and Data engineer for years.
While building many services with PostgreSQL, SQLite, Azure SQL. I found that there are rarely cases in which primary key were update in buisiness logic.
Mutable keys expand undo logs, deepen WAL record and plus complecity.
So Im building a database with immutable tuple key specialized in OLTP database for 2month with Claude code.
Minimum feature were finished like SQLs, Catalog, Transaction (ReadRepeatable, Read Committed), MVCC, WAL, Indexes, Forein Key. Dropping them keeps the engine simpler and faster.
What do you think?
Am I on the proper way? do you think that it will works and company adopt it rather than PostgreSQL if it is mature?
I really appreciate any advice and opinion!
Thank you.
3
u/Lemondifficult22 Oct 17 '26
It might help to think of primary keys not as a unique identifier of a record, but instead as a way of sorting data. Like, you may have events associated with users and you will want to process the oldest or latest ones first. In that situation, the key would be a tupple of (userid, timestamp, eventid). Then a range scan is very easy on the first 20 events for any user, and the tuples are likely to get updated.
Have a look at redis and Cassandra to get a better idea of how to think of that intuitively. Or make your own in memory string to string btreemap for this purpose.
2
u/Significant_Tune9219 Oct 17 '26
The rarity you saw is real, and it is mostly because apps already use a surrogate that nobody updates. A natural key that has to change, an account number or a SKU, still shows up, and every child row points at it. If the tuple key is that identity, forbidding the update does not remove the write. It turns it into a new parent plus a repoint of the children, which is more WAL than the in-place update, and it is the case ON UPDATE exists for. Keep the tuple identity immutable and put the business key in a secondary unique index that is allowed to move. Postgres users already do not update their primary keys, so that constraint is not the reason anyone leaves Postgres.
2
u/mamcx Oct 17 '26
This kind of limitations (that in part derive from SQL, not relational model) are part of why RDBMS are too anemic.
There is not reason to add it as part of the core. As you pointed: "immutable tuple key specialized" this is an engine/implementation specialization.
Now the main problem is that you are creating a "odd case" that go against the "normal case" and because is not a fundamental part of the model then anyway you are building all the machinery for the normal case, then say "but here is the other way!" somewhere.
That its: You build a coherent "all is immutable" or "all is mutable" as the base, THEN, you add "this are the rules to invert this", that is what Rust do.
What is trouble is making "edge cases", you will regret it and will be pain with the impl of all main components.
ie: If you idea is good, must be good not only for this case but most. If not, at best, is a hidden specialization on the query optimizer.
2
u/assface Oct 17 '26
Primary key updates are typically implemented as a delete followed by an update. They should be treated as unique logical tuples.