Data Virtualization and Federated Queries
#data-virtualization#federated-query#data-integration#trino#postgresql
Contents
Assume orders live in PostgreSQL, customers in MySQL, and click logs in Parquet files on object storage.
| Data | Store | Table |
|---|---|---|
| Orders | PostgreSQL | shop.orders |
| Customers | MySQL | crm.customers |
| Click logs | Object storage (Parquet) | logs.daily_click |
Answering “orders and clicks per VIP customer last month” means joining data from all three stores.
Splitting the query across the original sources instead of copying the data into one place is data virtualization, and its core capability is the federated query.
1. What the Two Terms Mean
1.1 Data Virtualization
Users query through a single access layer without knowing where or in what format the data is stored.
It is called virtualization because the data looks gathered in one place without any copy being made.
1.2 Federated Query
A federated query splits one query that spans several sources into per-source queries, runs them, and combines the results.
Data virtualization is mostly implemented with federated queries.
| Term | In this post |
|---|---|
| Data virtualization | querying data through one access layer without moving it |
| Federated query | splitting a query across sources, running the parts, and combining the results |
2. Copying Data Together vs Querying It in Place
ETL (Extract, Transform, Load) extracts data from the sources, transforms it into a common format, and loads it into an analytical store such as a data warehouse. It usually runs on a schedule, such as daily or hourly.
| ① ETL + warehouse | ② Data virtualization | |
|---|---|---|
| Where data lives | copied into one place | where it already is |
| Point in time of data | last load | query time (cache time if a cache is used) |
| Query speed | good for repeated analysis | depends on sources, network and execution plan |
| Adding a new source | build a load pipeline | add a connector, handle permissions and schema differences |
| Load on sources | at load time | on every query |
| Storage cost | size of the copy | no copy of the sources; only cache and intermediate results |
- When ETL fits: repeated large-scale analysis, reports that need data from a single point in time
- When virtualization fits: exploratory queries, checking the latest state of the source, data that is hard to copy
3. How a Federation Engine Splits SQL
A federation engine does not store source data permanently; it connects to each source through a connector.
Users see one catalog per source, and intermediate data during execution stays in the engine’s memory or temporary storage.
-- Trino example: pg, mysql and lake are separate catalogs
-- Orders and clicks are aggregated per customer first, so the join does not double count
WITH monthly_orders AS (
SELECT customer_id, count(*) AS order_count
FROM pg.shop.orders
WHERE created_at >= DATE '2026-09-01'
AND created_at < DATE '2026-10-01'
GROUP BY customer_id
), monthly_clicks AS (
SELECT customer_id, sum(clicks) AS click_count
FROM lake.logs.daily_click
WHERE day >= DATE '2026-09-01'
AND day < DATE '2026-10-01'
GROUP BY customer_id
)
SELECT c.id, c.name,
coalesce(o.order_count, 0) AS orders,
coalesce(k.click_count, 0) AS clicks
FROM mysql.crm.customers c
LEFT JOIN monthly_orders o ON o.customer_id = c.id
LEFT JOIN monthly_clicks k ON k.customer_id = c.id
WHERE c.tier = 'VIP';
The engine works in this order.
- Analyze: use the catalogs to find which source each table belongs to.
- Plan: if the connectors support it, send
tier = 'VIP'to MySQL and the date filter and per-customer count to PostgreSQL. For the Parquet files, the date filter narrows the partitions and blocks to read. - Collect: receive the filtered results from each source.
- Combine: the engine performs the cross-source joins. Trino spreads this work across multiple nodes.
- Return the result
4. Pushdown Decides Performance
Handing work the engine would do over to the source is called pushdown.
Pushdown is the key optimization for reducing both transfer and processing.
| Pushdown type | What goes to the source | Effect |
|---|---|---|
| Predicate | WHERE tier = 'VIP' | the source filters rows first |
| Projection | SELECT only the needed columns | less data transferred |
| Aggregation | compute count and sum at the source | a few result rows instead of raw rows |
| Join | joins between tables in the same source | handled by the source |
| LIMIT, sort | top N only | less data transferred |
If tier = 'VIP' cannot be pushed down, non-VIP customer rows also travel from MySQL to the engine.
The same SQL can run at very different speeds depending on which pushdowns the connector supports.
A function or type cast the source does not understand keeps a filter from being pushed down, so check the execution plan with EXPLAIN.
5. Why Cross-Source Joins Are Costly
A join inside one database is handled by that database using its indexes and statistics.
A join between different databases cannot be handed to either source, so the federation engine receives data from both sides and processes it.
Take a query for last month’s orders from VIP customers.
SELECT c.name, o.id, o.amount
FROM mysql.crm.customers c
JOIN pg.shop.orders o ON o.customer_id = c.id
WHERE c.tier = 'VIP'
AND o.created_at >= DATE '2026-09-01'
AND o.created_at < DATE '2026-10-01';
Assume there are 300 VIP customers, 10 million orders last month, and 4,500 of those orders belong to VIP customers.
5.1 Hash Join: Build a Hash Table from the Smaller Side
For cross-source joins, a federation engine mostly uses a hash join.
- build: read the smaller side, the 300 VIP customers, and build an in-memory hash table keyed by
id. - probe: read the larger side, the 10 million orders, row by row and look up each
customer_idin the hash table. - output: emit only the 4,500 orders whose customer is in the hash table.
Looking up one key in a hash table costs about the same regardless of the table’s size.
The orders are read only once, and only the 300 customers sit in memory.
5.2 Statistics: The Engine Must Know Which Side Is Smaller
The engine decides which side to build the hash table from by looking at statistics (row counts, distinct value counts).
Trino chooses the join order from statistics that connectors fetch from the sources.
- With statistics: the hash table is built from the 300 customers. Only 300 rows go into memory.
- Without statistics: the join order is left as written, and the hash table is built from the table on the right of the SQL, the 10 million orders. Memory use grows sharply, and the query fails if it exceeds the memory limit.
In Trino, check whether statistics exist with SHOW STATS FOR pg.shop.orders, and check which side is build with EXPLAIN.
When running on multiple nodes, statistics also decide how the hash table is distributed.
| Distribution | What it does | When it fits |
|---|---|---|
| broadcast | copy the whole build side to every node | the build side is small |
| partitioned | split both sides by join key and send the same keys to the same node | both sides are large |
5.3 Dynamic Filtering: Filter Early at the Larger Source
With a hash join alone, PostgreSQL sends all 10 million orders from last month to the engine.
Only 4,500 of them end up in the result.
A dynamic filter turns the join keys collected during build into a filter on the probe side and sends it to the source.
- Collect the 300 VIP customer IDs during build.
- Turn the 300 IDs into a
customer_id IN (...)filter and attach it to the PostgreSQL query. If there are too many IDs, send a range from the minimum to the maximum instead. - PostgreSQL sends only the 4,500 orders from VIP customers.
tier = 'VIP' is written in the SQL, so it can be pushed down at planning time, but the list of VIP customer IDs is known only after reading MySQL.
That is why a dynamic filter is created while the query is running.
Trino’s PostgreSQL and MySQL connectors wait a configured amount of time for the build side to finish before sending the probe-side query.
Whether the dynamic filter reaches the remote database’s SQL depends on the connector.
6. Open Source and Commercial Products
| Name | Characteristics |
|---|---|
| Trino | distributed SQL query engine. Connects to RDBMSs, data lakes and search engines through connectors. Forked from Presto |
| Presto | distributed SQL engine that started at Meta |
| PostgreSQL FDW | connects tables in other databases as foreign tables inside PostgreSQL. A partial implementation of the SQL/MED (Management of External Data) standard |
| Apache Calcite | query parsing and optimization framework, used inside many engines |
| DuckDB | in-process analytical database. Extensions connect PostgreSQL, MySQL and Parquet directly for joins |
| Dremio, Denodo | dedicated data virtualization products with caching, acceleration layers and governance |
6.1 What PostgreSQL FDW Covers
PostgreSQL connects tables in other stores as if they were its own through FDWs (Foreign Data Wrappers).
The FDWs shipped with PostgreSQL are postgres_fdw (other PostgreSQL servers) and file_fdw (files on the server).
MySQL, Oracle and SQL Server use separately installed community FDWs.
Since the order database is PostgreSQL, the customer table in MySQL can be connected with the community extension mysql_fdw.
CREATE EXTENSION mysql_fdw;
CREATE SERVER crm FOREIGN DATA WRAPPER mysql_fdw
OPTIONS (host 'crm-db', port '3306');
CREATE USER MAPPING FOR CURRENT_USER SERVER crm
OPTIONS (username 'reader', password '********');
CREATE SCHEMA crm_remote;
IMPORT FOREIGN SCHEMA crm LIMIT TO (customers) FROM SERVER crm INTO crm_remote;
-- Join the remote table as if it were local
SELECT o.id, c.name
FROM shop.orders o
JOIN crm_remote.customers c ON c.id = o.customer_id
WHERE c.tier = 'VIP';
| What it can do | Limits |
|---|---|
| query and join tables from other stores as if they were local | joins across different servers are processed by the single local PostgreSQL after fetching the data |
postgres_fdw pushes filters, joins within the same server, aggregates and sorts to the remote server (check what is actually sent with EXPLAIN VERBOSE) | pushdown support differs by FDW; for community FDWs, check what each version supports |
| group foreign tables as partitions to scan several remote servers at once (asynchronous execution) | execution happens on one PostgreSQL server only; it cannot spread work across nodes like Trino |
remote writes (INSERT, UPDATE, DELETE) | no two-phase commit, so atomic commits across servers are not guaranteed |
- When FDW is enough: few sources, PostgreSQL-centric, and small remote transfers.
- When a distributed engine (such as Trino) fits: many kinds of sources, or large analytical queries that must run across multiple nodes.
7. What Data Virtualization Cannot Solve
- It naturally knows nothing about how data relates or what the data means. Whether
cust_idin source A andcustomer_noin source B hold the same values, or whatstatus = 3means, is for the user to know. - It cannot guarantee one point in time across sources. Each source is read at a different moment with different transaction boundaries. Reading every source as of the same point in time needs a separate architecture.
- It puts load directly on the source databases. Analytical queries run as-is on production databases. Connect to read-only replicas or limit query volume.
- It cannot make slow sources fast. The result waits for the slowest source to respond.
Some products speed up repeated queries with caches and acceleration layers. In exchange, cache refresh intervals and storage costs have to be managed.
8. Semantic Layers on Top of the Federation Engine
A layer that defines metrics or concepts can sit on top of the federation engine.
- Semantic layer: defines metrics and dimensions and generates SQL from those definitions.
- OBDA (Ontology-Based Data Access): queries with the concepts and relations of an ontology and generates SQL through mappings.
When all the data is in one database, the semantic layer queries that database directly, so a federation engine is not required.
Common Misconceptions
- ❌ With data virtualization, you no longer need ETL.
✅ Loading is better for repeated analysis or data from a single point in time. Using both is the better choice. - ❌ A federation engine handles cross-source joins as fast as joins inside one database.
✅ Cross-source joins need network transfer and engine processing. Pushdown and dynamic filters only reduce the transfer. - ❌ If you can query it with one SQL statement, the data is integrated.
✅ Only access is integrated. Field meanings and identifiers still have to be aligned.
Summary
- Data virtualization lets you query through a single access layer without copying the sources, and makes federated queries possible.
- A federation engine splits a query by source and performs cross-source joins and remaining operations itself.
- Federated query performance depends on pushdown, source performance, the network, and the execution plan the federation engine builds from statistics.
- PostgreSQL FDW fits small, PostgreSQL-centric setups; a distributed engine fits when sources and analytical workloads are large.