r/SQL • u/Ambitious-Hornet-841 • Apr 16 '26
PostgreSQL Cross database join keys are a silent failure mode in multi DB agents
Cross-database join keys are a silent failure mode in multi DB agents
Post:
We hit a recurring issue while building a multi database data agent for PostgreSQL + MongoDB: joins could return zero rows with no error even when the logic looked right.
The issue was format mismatch, not missing data.
Example:
PG subscriber_id: 1234567
Mongo subscriber_id: "CUST-1234567"
What helped:
explicit key normalization
mapping rules per entity type
logging failures by category instead of treating every miss as “query failed”
separating routing, join resolution, and execution into different steps
This changed our design: we stopped treating the problem as “better SQL” and started treating it as data contract mismatch across systems.
Question:
For people working with mixed stores, do you solve this in ETL, in the query layer, or in application logic? Where have you found the least painful boundary?
5
u/Eleventhousand Apr 16 '26
Yeah, I probably haven't done any cross database joins in about twenty years.
I've pretty much always stuck to integrating the data together into one database, or pulling separately in the application layer and combining there.
2
u/Imaginary__Bar Apr 16 '26
Huh? This has nothing to do with cross-database joins. Rather this is just understanding the business rules (and fixing them if possible).
Even in the same database this join will fail (obviously).
(I have a database which has the same client represented in at least four different ways just because so much crud has built up over the years as people have come in and silently decided to 'fix' things without getting input from the people who matter until it's too late. My users just have to remember/look-up the correct format for their particular use-case. It's a pain, but it has nothing to do with cross-database functionality.)
1
u/B1zmark Apr 16 '26
I really interested in your use case for MongoDB here. It looks like you're expecting a fixed schema based on the joins?
1
u/Meseret_Bolled Apr 18 '26
We hit this exact issue building a multi-database agent on PostgreSQL and MongoDB.The key shift for us was stopping treating it as a query problem and starting treating it as a data contract problem. Once we framed it that way, the solution became clearer: build a dedicated join key resolver that runs before query generation, not inside it. It holds the normalisation rules per entity type deterministically, so the model never has to guess whether to strip a prefix or cast a type.
On your ETL vs query layer question — we found the query layer less painful, but only when the resolution logic is explicit and outside the model. The moment the model is responsible for figuring out that 1234567 and CUST-1234567 are the same entity, you get inconsistency across runs.
One other thing worth doing: separate "zero rows returned" from "query failed" in your logging. Silent empty results are a different failure mode and need a different correction path.