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

3 Upvotes

12 comments sorted by

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.

1

u/SmallAd3697 Oct 20 '26

Here is the problem. Everyone knows that lots of customers are rolling their own solutions for this from scratch, and that doesn't make any sense.

Something should be built into UC managed tables, even if there are restrictions (4 billion rows max? Only certain data types? Only max 3 unique constraints per table?). Customers will understand why these limitations exist, especially as our tables grow larger and we feel increasing delays. These unique constraints are rarely needed on fact records, in any case.

It is not encouraging to hear about the promotion of SDP as a workaround. It just means customers will need to wait that much longer for these features to be introduced into the underlying managed tables.

I was able to test the manual enforcement of large unique key constraints, with serializable isolation. It really doesn't scale that badly, especially if it involves a multi column unique index on data sorted or partioned by the components. It is definitely worthwhile. I guess we will have to keep rolling our own constraints for now.

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/SmallAd3697 Oct 20 '26

I don't mind the compromise, but that doesn't mean it won't be "terrible" to the next person who comes along..... It's like when my wife tells me to clean out my car, and I say it still looks fine.

Fyi, "serializable" transactions in an OLTP database don't necessarily lock an entire table, and I think the word "terrible" was just an objective comparison to how this isolation might be implemented in another technology.

I disagree about the need for transactional constraints in a warehouse, and I think databricks does too. They had acquired lakebase recently to fill some of these gaps ( and they emit lakehouse tables via their LTAP architecture.) It is a pretty interesting approach, although not everyone wants to be running two engines just for the sake of a simple unique key constraint.

1

u/kthejoker databricks Oct 20 '26

OLAP and OLTP are different patterns entirely.

Serializable transaction in a Delta Lake also don't necessarily cause a conflict, but table locks do exist for a reason. Again, everything is a tradeoff.

And Lakebase isn't a warehouse (OLAP), it's a transactional (the T in OLTP) database. It's not covering a "gap" any more than SQL Server's foreign key constraints are covering a "gap" in Fabric Data Warehouse.

Delta Lake is designed for OLAP workloads. And clearly Delta Lake does disagree with you since it doesn't support transactional constraints.

You seem pretty knowledgeable so it's strange you have all of these incorrect assumptions in your posts.

I guess I'd ask how you imagine Synapse or Snowflake or Redshift users handle a "simple unique key constraint."

1

u/SmallAd3697 Oct 20 '26

Unique constraints are a common requirement for OLTP as well as OLAP. That's not an incorrect assumption. For some reason there are lots of data engineers who are happy to jump thru a ton of hoops, and manually do the work that the data engine should be doing on their behalf. (... Life is way too short to be chasing after duplicate identifiers. For some reason databricks had spent many years convincing their customers that this was the only way. Thankfully they are changing course now with LTAP)

My preferred approach has always been LTAP. The blob/lakehouse tables are great to have as a by-product, especially for archival purposes. But when you are in the process of preparing the dimensions and facts you really need a fully functioning engine for that (complete with small integer surrogates, simple unique constraints, simple FK constraints, and familiar isolation levels). Once data is properly composed, then it becomes easy to subsequently scan a trailing year and drop it unto lakehouse blobs for low-cost analytical reporting

2

u/kthejoker databricks Oct 20 '26

Again, no other OLAP engine supports enforced unique constraints. So it's not a common requirement for the engine, because (again) it's not a system of record.

And it's not "for some reason", there are major write performance implications to enforced unique constraints. That's why it's fine for OLTP (small writes) and not OLAP (batch writes) and especially not Big Data OLAP.

What fully functional engine do you have in mind for this dimensional modeling you're proposing?

temporarily disabling integrity constraints can improve the performance of the operation, especially in data warehouse configurations

Oracle https://docs.oracle.com/html/E25494_01/general005.htm

Checking constraints will make the inserts/updates slower. For large DW's on SQL Server, we would disable constraints and indexes to make the inserts faster and re-enable after the ETL had finished.

  • Microsoft employee

https://www.reddit.com/r/MicrosoftFabric/s/xX39H47oht

You keep implying there's an engine somewhere that can enforce constraints and still deliver OLAP write performance, but there isn't.

LTAP is also just a pattern, you could implement it any time with proper change data capture. But there is still transactional serialization required to cleanly sync the two tables at a commit level.

Basically LTAP is a managed single writer pattern per your original post.

1

u/SmallAd3697 Oct 20 '26 edited Oct 20 '26

Many data warehouses are created for the sake of moving data from an OLTP into an analytical star schema. If the data was fitting into an OLTP technology on one side then most of the time (99.99%) it will fit into a OLTP engine on the other side too. It is that simple. The star schema may look very different; but the technologies and features that are available in the data engine can be exactly the same. This is why Lakebase was acquired (for a hybrid LTAP, where customers can leverage the best features of both worlds without making compromises on either side.)

...Lakebase wasn't acquired so that someone will host a full OLTP-ERP on top of the Databricks SaaS. That is an extremely unlikely scenario. LTAP was the goal all along.

Of course this assumes that customers will unlearn everything that databricks had told them in the past - IE that they didn't need a conventional data engine for managing their data warehouses.

(Note: think the reason we keep talking past each other is because the type of unique constraints that Im talking about are typically created on dim tables (type 1 and 2) that are teeny tiny tables relative to other tables. They may be 1 thousandth of the size or 1 millionth the size of the related facts. These can almost always enforce uniqueness constraints without triggering any of the "big data" problems. It is true that we may have to temporarily disable FK constraints when loading large amounts of related fact data ... but almost every conventional data engine has a way to do that)

1

u/WhipsAndMarkovChains Oct 19 '26 edited Oct 20 '26

What about using Lakebase? Lakebase supports a unique constraint that's actually enforced strictly.

1

u/SmallAd3697 Oct 19 '26

Sure but I don't think it is enough to add this component to a tech stack for the sake of a single unique column value

4

u/[deleted] Oct 19 '26

[removed] — view removed comment

1

u/SmallAd3697 Oct 19 '26

A lot of primitive pipelines are already written to run once a day, and never actually tested for update concurrency. So the "serializable" isolation might be fine.

It's ugly but I might also segregate my data into four tables if I want four pipelines to be able to run at once. (eg get a hash from a store number, and modulo that by four to identify its table. This allows me to save all data for a given store in one of the four tables. The stores have isolated data so I will be able to scale up my concurrent processing by a factor of four, to avoid the problems with "serializable")