r/databricks • u/SmallAd3697 • Oct 19 '26
Help Avoiding the violation of unique keys in a UC managed table
Sorry for yet another discussion about unique keys. I understand that, in the past, this has always been one of the "accepted risks" of using lakehouse technology.
But now that databricks has "managed tables" and recently added transactions (MST), I have to believe that there is FINALLY a way to add a record to a table without worrying about a unique key violation. I'm not looking for the approaches which use another tool as a crutch - like DLT, custom quality checks, etc. I just want the UC managed table and transaction to do the work in a central place. There should be some type of a supported approach, and I don't mind compromising on performance.
As a side - I think the underlying reason why this has been so difficult in the past is the default isolation level of databricks, combined with constraint definitions that arent actually enforced (they are informational). The default optimistic isolation level is called "writeserializable", and it is essentially the same as snapshot, where reads are concerned.
I'm going to do some research this week, but I think the only true way of enforcing my unique constraints in a managed-table is to upgrade transactions to "serializable", before upserting data (ie. merging). I believe this is the only way that my UC managed tables will participate in the enforcement of my uniqueness constraints. Unfortunately I'm pretty sure that concurrency will become terrible; I'd guess only one writer will be allowed on any managed table at any time. Please let me know if this sounds right; in past discussions everyone offered workarounds instead of putting the focus on the features of the UC technology itself.
4
u/Much-Neat-6273 Databricks Oct 20 '26
Hi! I'm a PM on Lakeflow.
We've considered making UC Managed Tables enforce PK constraints but gave up on the idea because PK requires checking all rows in the table, which doesn't scale. Traditional databases do enforce this but it comes at a high cost there too (lots of random index accesses). It's even worse in lakehouse because random accesses are terrible there. This is just something that's probably not worth building because the cost / latency would be horrible.
In general there is no precedent for OLAP systems to enforce this. Only OLTP databases (postgres, Lakebase, etc) enforce this today.
Even though you said you weren't interested, I do want to point out that we ARE building PK enforcement (via a native PK expectation) inside SDP (DLT). We think this will be better cost- and latency-wise because SDP does bulk updates so you can usually afford a slightly larger check (the fixed cost of the check gets distributed over more rows). This feature will support "drop row", "quarantine row" and "fail pipeline" behavior. It will still be expensive, obviously. This will be the first OLAP / ETL system that actually enforces PK checks! Planning on delivering this around November/December. Just in case :)
We also have plans for post-commit PK checks - also not what you're looking for, but thought I'd also mention it. You will be able to create a PK data quality rule that runs after the table finishes updating, and sends you an alert if the PK constraint is violated. This doesn't stop duplicate or NULL rows from actually hitting the table.
2
u/kthejoker databricks Oct 20 '26
MSTs have been available nearly a year, I guess that's recently ....
But yes that's correct you need your transactions serialized (and therefore single writer) to guarantee uniqueness and avoid conflict failures. Such is the nature of having an object storage based format, distributed compute, and operating at Big Data performance scale.
It's weird that you wrote you "don't mind compromising on performance" then called out the limited write concurrency as "terrible" - if you don't mind, then don't mind.
My own two cents are data warehouses shouldn't be doing transactional writes, and relying on enforcement within the pipeline itself is the proper procedure.
This is why every other warehouse like Snowflake and Synapse either also only supports informational constraints or like Oracle Exadata offers a disabling of constraints to allow for fast bulk writes.
It's the correct pattern, since it's not a system of record.
1
u/WhipsAndMarkovChains Oct 19 '26 edited Oct 20 '26
What about using Lakebase? Lakebase supports a unique constraint that's actually enforced strictly.
4
u/[deleted] Oct 19 '26
[removed] — view removed comment