Featured image of post Cross-Database Joins: The Coupling You Can't See Until You Split

Cross-Database Joins: The Coupling You Can't See Until You Split

Two schemas on one database server can be joined as if the boundary were not there, and after a few years a codebase holds more of those joins than anyone has counted. Then one schema has to move to its own server. On Postgres the usual bridge is a foreign data wrapper, and Svix documented a join through one that moved millions of rows where thousands were expected. How do you find these joins, and what does each one cost to remove?

TL;DR
A join across two schemas on the same MySQL server costs nothing to write, which is exactly why nobody counts it. Every one of those joins is a hard coupling that pins both schemas to one instance, so the day you need to split for scale, the joins are the wall. And the referential integrity you assumed the boundary carried was never enforced to begin with.

A codebase that keeps two schemas on one MySQL server usually has a query like this somewhere in it:

1
2
3
4
SELECT li.order_id, li.sku, li.qty, inv.status, inv.amount_due
FROM orders.line_items li
JOIN billing.invoices inv ON inv.order_id = li.order_id
WHERE li.created_at >= '2026-07-01';

Two schemas, orders and billing, one MySQL server. Queries of this shape accumulate in application code, dbt models, cron reports and the admin dashboard, and nobody writes a design doc for any of them, because MySQL lets you qualify a table with schema.table and join across the boundary as if it weren’t there. They stay invisible until the primary runs short of capacity and the obvious relief is to lift billing onto its own instance. Every one of those joins stops compiling the moment the schemas live on different servers. GitHub and Datadog have both published how they got through that split, and both write-ups come up below.

Free to write, fake underneath

The two engines draw this boundary in different places. On MySQL, a “database” and a “schema” are the same object: a namespace on one server. There is no cross-database join inside one server, only a fully-qualified one, and the qualifier costs a few keystrokes. On Postgres, schemas in one database join freely, but separate databases can’t be joined at all without a foreign data wrapper (FDW, a table that stands in for one on another server) or dblink, so a team that split into Postgres databases hits the wall on day one, where at least it’s visible.

The worse fact, back on MySQL: foreign keys can cross schemas. REFERENCES billing.invoices(order_id) is legal DDL, and almost nobody writes it, because the schemas belong to different teams and nobody wants their writes gated on another team’s table. So the boundary those joins cross has no enforced referential integrity and never did. The failure modes are the ones you’d predict from dropping a foreign key, except no FK was ever declared: orphaned line items with no cascade to catch them, ID-type drift that turns the join into a silent cast (BIGINT on one side, CHAR(36) UUID after a migration on the other), backfills that assume the far side exists.

This is a same-server, cross-namespace problem
Cross-schema joins inside one Postgres database, or on a MySQL server you never intend to split, are not the anti-pattern. The coupling bites when scale forces the namespaces onto different instances. If that day never comes, the free join is genuinely free; the trap is assuming that’s your situation when the growth curve says otherwise.

Recovery has the same hole. Each database backs up independently, so rolling billing back while orders keeps its afternoon of writes leaves every cross-schema join comparing two timelines, with no constraint to fail. The honest unit of backup and restore is the set of databases your queries treat as one, and the joins say that set has two members.

The FDW that looks like a fix

The escape hatch people reach for is split anyway and put a foreign data wrapper between the instances, so the joins keep working across the wire. This section is about Postgres: postgres_fdw is a Postgres extension, and the evidence below comes from Postgres. The syntax survives the split. The performance does not. Cross-server joins don’t push down; postgres_fdw only sends a join to the far side when both tables live on the same foreign server (the docs are explicit), so across two instances the join executes locally, pulling rows over the network first. Svix hit exactly this in October 2025: a join that failed to push down pulled both tables in their entirety, moved millions of rows where thousands were expected, and timed out.

So the FDW gives you back the SELECT and takes away the reason you split. Every cross-boundary query drags its working set back over the network to be joined next to the primary anyway.

An FDW join across instances is a distributed join, not a local one
If EXPLAIN (VERBOSE) shows the join in the local plan rather than in the Remote SQL, the far table is shipping over in full. Treat every cross-instance FDW join as a network transfer sized by the larger table, not by your WHERE clause.

Find the joins before you need the split

The coupling lives in query text, not the catalog: no pg_constraint row, no KEY_COLUMN_USAGE entry. But it always leaves a fingerprint, because at least one table in the query has to be namespace-qualified. On a running MySQL server, the statement digest table (performance_schema’s summary of every normalized query shape the server has run) preserves the qualifiers:

1
2
3
4
5
SELECT SCHEMA_NAME, DIGEST_TEXT, COUNT_STAR, SUM_ROWS_EXAMINED
FROM performance_schema.events_statements_summary_by_digest
WHERE SCHEMA_NAME = 'orders'
  AND DIGEST_TEXT LIKE '%`billing` . %'
ORDER BY COUNT_STAR DESC;

The pattern has backticks and spaces in it because that is how the digest writes a qualified name: on MySQL 8.4 a query against billing.invoices is stored as `billing` . `invoices`, and a plain '%billing.%' matches nothing. Two caveats: DIGEST_TEXT truncates at performance_schema_max_digest_length (1024 bytes by default), and the digest table resets on restart, so it shows recent traffic, not history. Pair it with a grep of the repos (application code, ORM models, dbt sources) for the billing. qualifier. Datadog ran this inventory across 30-plus teams when they unwound a shared database in 2025; mapping the cross-boundary queries came before any carving. In Postgres the hunt is easier: the cross-instance dependency is already wearing a postgres_fdw label, listed in pg_foreign_table, with query text in pg_stat_statements.

What the fixes cost

The cheapest move is writing the coupling down where a machine can read it: COMMENT 'references billing.invoices.order_id, service-owned, no FK across instances' on the column. Readable by whoever opens the table, enforced by nothing. If the boundary has to actually hold, a scheduled reconciliation job anti-joins the two sides and reports the orphans the missing FK would have rejected at write time. Eventually-consistent by construction, and someone has to own the alert. For the analytics joins, a consolidated replica (logical replication or CDC into one reporting instance) makes the join legal again, at the price of a pipeline to run and a lag window to reason about. For the transactional joins, the durable answer is the one Datadog and the AWS decomposition guidance both land on: replace the query with a service call. That’s the real unwind and the expensive one, a rewrite of every call site the join touched.

When the split itself arrives, replication makes it survivable, and the order of operations matters more than the tooling. Write-path joins and cross-schema transactions get rewritten first, while everything still shares a server; after cutover there is no atomicity across the boundary. Then the future cluster comes up as a replica of the whole instance, billing reads migrate at leisure, and writes cut over in a short freeze once the new cluster has applied every transaction the old one committed (the GTID sets, MySQL’s global transaction identifiers, match). GitHub ran this playbook in 2021: SQL linters to stop new cross-boundary queries, then a write-cutover that moved 130 tables with sub-second downtime. Read joins stay gradual. A second replication channel (multi-source replication, in MySQL’s terms) keeps feeding orders into the new cluster read-only, so stragglers join against a lagged copy while the digest counts drain to zero; give that channel a deadline, it’s the weld on life support. One trap: a read join that feeds a write decision (check invoice status, then update the order) is a cross-schema transaction wearing a SELECT, and the lagged copy turns it into a race. The digest table won’t flag it; only the call sites do.

SELECT insights FROM experience WHERE downtime = 0; -- Ruslan Tolkachev