r/databasedevelopment • • 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.

0 Upvotes

12 comments sorted by

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.

3

u/Lemondifficult22 Oct 17 '26

Regarding the purpose, it's not clear to me why I would use it. A database is fundamentally an index and a transaction manager. Those paradigms are the largest differentiators between products, everything else is just integration and convenience.

2

u/louischoi Oct 17 '26

Thank you for your thoughtful feedbacks.

Yes, Primary key is best key for ordering. That is a use case I had not considered at all. with composite primary key and very big volume. What a new perspectives.

A database Im working on (KDBS) aims to provide functionality for medium size database like user, items, purchases, not like user action logs with 10M+ rows per a table.

big-scale data processing and data warehousing are intentionally out of scope.

By forcing orcing PK a word (64bit) it could gain a performance: 6x faster at pk-point look up than PostgreSQL, 2.4x faster at pk(not composite one)-range-scan, 1.7 faster than pk-point-join with 3 tables in my benchmarks. (Yes, I know almost latency is about Networks, Disk I/O though)

And I think about a memozation and optimizing physical data layout per a query patterns. plus, immutable key make it enables to implement Assertion feature (SQL-92) - constraint for a group by an expression -

your explanation very helful Thank you!

1

u/linearizable Oct 17 '26

If you force a large integer as a record identifier, isn’t that just a heap table?

1

u/louischoi Oct 21 '26

yes in some ways. but kdbs still keeps clustered index for row-id. and data page aligns with this row-id, dont need to scan full pages to access by key.

2

u/mamcx Oct 17 '26

Yes! even better is to think of data projected as indexed:

[c, a, b]:
   AsStored: [c, a, b]
   AsSortedAsc: [a, b, c]
   AsSortedDesc: [c, b, a]
   AsSlicesStored: [c, a], [b]
   ....

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.

1

u/louischoi Oct 17 '26

Yes, KDBS’s philosophy is along the same lines. It encourages separating the identity key from business meaning. By making the tuple key immutable at the engine level, the engine can gain real advantages, so immutability is a means rather than a goal in itself.

When a key must change, KDBS provides a PURGE API, (almost same way delete-and insert). It is somewhat expensive, but it should be rare.

I agree with much of what you said.

1

u/louischoi Oct 17 '26

Thank you for your feedbacks and advice!

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.

1

u/louischoi Oct 18 '26

Thank you for your advice! yes as you said, it is a rust version of DBMS. and I noticed that many limitation come from constrations of expression (SQL) not engine.

I think I will approach some other way with not giving up functionality for edge case.

RDBMS should be good not in most ways, but in all ways.

I will come back again with V2 version!

really appreciate your opinion!

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.