ApiaryActive
Try: pause · settings · learn · wipe
← Community / Reading Room
QF
databases · 12 min read

Query Federation Across Heterogeneous Sources

In 2023 the Global Biodiversity Information Facility (GBIF) catalogued 2.4 billion occurrence records from more than 1.6 million species. For honeybees alone,…

The ability to ask a single question of many databases is no longer a nice‑to‑have; it’s a prerequisite for any organization that wants to turn data into action at the speed nature demands. In the world of bee conservation, where climate sensors, hive telemetry, land‑use maps, and citizen‑science observations live in different silos, a federated query engine can turn a fragmented data landscape into a single, actionable view.

In this pillar article we walk through the three most battle‑tested federation technologies—PostgreSQL Foreign Data Wrappers (FDW), Trino (formerly PrestoSQL), and Microsoft PolyBase—and show how they let you write one SQL statement that runs across relational, NoSQL, object‑store, and even streaming sources. We’ll compare their architectures, performance numbers, and operational trade‑offs, then stitch everything together in a practical, end‑to‑end pipeline that a conservation team could deploy today.


1. Why Heterogeneous Data Is the New Normal

1.1 A data ecosystem as diverse as a meadow

In 2023 the Global Biodiversity Information Facility (GBIF) catalogued 2.4 billion occurrence records from more than 1.6 million species. For honeybees alone, data streams include:

SourceData TypeTypical StoreVolume (2022)
Hive‑sensor APIs (e.g., Arnia, Bee‑Smart)Time‑series telemetry (temperature, weight, sound)InfluxDB / TimescaleDB~1 TB
Land‑cover rasters (Sentinel‑2)Multispectral imageryAmazon S3 (Parquet)8 TB
Citizen‑science sightings (iNaturalist)JSON eventsMongoDB350 GB
Government pesticide registersStructured tablesOracle12 GB
Weather forecasts (NOAA)NetCDF streamsHDFS / Azure Data Lake2 TB

Each store is optimized for its own workload: columnar files for analytics, key‑value for fast look‑ups, time‑series for high‑frequency telemetry. The moment a researcher wants to answer a question like “How did pesticide exposure correlate with colony weight loss during the 2022 heatwave?” they must join across at least four of those systems.

1.2 The cost of “copy‑and‑paste” pipelines

Traditional ETL (Extract‑Transform‑Load) approaches copy data into a central warehouse. A 2021 Gartner survey found 68 % of data teams spend more than 50 % of their time moving data rather than analyzing it. For conservation projects, that latency can be fatal: a sudden drop in hive weight may signal disease, but if the data sits in a warehouse that refreshes only nightly, the response window shrinks dramatically.

1.3 Federation as a strategic lever

Query federation removes the “move‑first, ask‑later” requirement. By pushing the query down to the source, you get:

  • Sub‑second latency for joins that would otherwise require batch loads (e.g., Trino can return a 10 TB join in < 30 s when sources are properly indexed).
  • Cost savings – you read only the columns and rows needed, avoiding full‑table scans and storage duplication.
  • Governance compliance – data never leaves its original governance domain, simplifying GDPR, CCPA, and the emerging “Bee‑Data‑Trust” regulations.

The next sections unpack how three mature federation engines achieve these gains.


2. The Core Mechanics of Query Federation

2.1 Logical vs. physical planning

All federation engines share a two‑stage planning model:

  1. Logical Plan – The parser builds an abstract syntax tree (AST) that represents the intent (e.g., SELECT hive_id, AVG(weight) FROM hive_data JOIN pesticide ON hive_id = pesticide.hive_id GROUP BY hive_id).
  2. Physical Plan – The optimizer decides which parts of the AST can be executed where. It pushes down filters, projections, and aggregates to the source that can handle them most efficiently.

A push‑down rule of thumb: filter → projection → aggregation. If a source can perform a filter (WHERE temperature > 30) and a projection (SELECT hive_id, weight) natively, the engine will translate those into the source’s native query language (SQL, REST, or even a Spark job).

2.2 Connector abstraction

Each engine implements a connector (or wrapper) that translates the engine’s internal representation to the source’s protocol.

EngineConnector NameSupported Sources (selected)
FDW (PostgreSQL)postgres_fdw, mongo_fdw, file_fdwPostgreSQL, MySQL, MongoDB, CSV/Parquet files
Trinotrino-mysql, trino-hive, trino-kafkaMySQL, Hive, S3, Kafka, Cassandra, Elasticsearch
PolyBaseSQL Server PolyBase, Azure Synapse PolyBaseSQL Server, Oracle, Hadoop, Azure Blob, Cosmos DB

Connectors expose metadata (column types, statistics) to the optimizer, enabling cost‑based decisions. For example, Trino’s connectorMetadata layer can fetch row‑count estimates from Hive’s metastore, allowing it to choose the smallest join side as the broadcast source.

2.3 Data format negotiation

When a query spans a columnar file (Parquet) and a row‑store (PostgreSQL), the engine must reconcile data types. Most connectors use Apache Arrow as an in‑memory interchange format, which standardizes numeric precision, timestamps (UTC), and null handling. Arrow reduces serialization overhead dramatically—benchmarking from the Trino community shows a 3× speedup for cross‑source joins compared to JSON‑based exchange.


3. PostgreSQL Foreign Data Wrappers (FDW) – The Open‑Source Workhorse

3.1 What FDW is, and why it matters

FDW is a PostgreSQL extension that lets you treat an external data source as a foreign table inside a PostgreSQL database. The foreign table appears in information_schema.tables just like any local table, so existing tools (pgAdmin, dbt) work without modification.

Key statistics (as of PostgreSQL 16, released 2023):

MetricValue
Supported FDWs (official)30+ (including postgres_fdw, mongo_fdw, file_fdw, odbc_fdw)
Avg. query latency overhead (filter push‑down)5 % vs native source
Max concurrent foreign scans per server500 (configurable)

3.2 Setting up a basic federation

-- 1. Install the FDW extension (requires superuser)
CREATE EXTENSION IF NOT EXISTS postgres_fdw;

-- 2. Create a server object that points to the remote PostgreSQL instance
CREATE SERVER remote_hive
  FOREIGN DATA WRAPPER postgres_fdw
  OPTIONS (host 'hive-db.example.com', port '5432', dbname 'hive');

-- 3. Map a user for authentication
CREATE USER MAPPING FOR current_user
  SERVER remote_hive
  OPTIONS (user 'hive_reader', password '••••••');

-- 4. Import a remote table as a foreign table
IMPORT FOREIGN SCHEMA public
  LIMIT TO (pesticide_application)
  FROM SERVER remote_hive
  INTO public;

Now pesticide_application can be queried alongside local tables:

SELECT h.hive_id, AVG(h.weight) AS avg_weight, p.chemical
FROM local_hive_telemetry h
JOIN pesticide_application p USING (hive_id)
WHERE h.timestamp BETWEEN '2022-07-01' AND '2022-07-31'
GROUP BY h.hive_id, p.chemical;

PostgreSQL will push the WHERE clause to the remote server, fetch only the relevant rows, and perform the aggregation locally.

3.3 Performance tuning

  • fdw_startup_cost and fdw_tuple_cost – Adjust these planner parameters to reflect network latency. For a 10 ms round‑trip to a cloud instance, set fdw_startup_cost = 10.
  • Batch size – postgres_fdw fetches rows in batches (fetch_size). Setting fetch_size = 5000 often yields the best trade‑off between memory pressure and network chatter.
  • Parallel foreign scans – PostgreSQL 14 introduced parallel execution for FDWs that expose supports_parallel = true. mongo_fdw now supports this, allowing a single query to spawn up to 8 workers across a sharded MongoDB cluster.

3.4 Limitations you need to know

LimitationImpactMitigation
No native push‑down for window functions (e.g., ROW_NUMBER())Must be evaluated locally, causing larger data transfersPre‑aggregate in a materialized view on the source
Transaction semantics are limited to read‑only by defaultUpdates across multiple FDWs are not atomicUse two‑phase commit via postgres_fdw’s foreign_table option use_remote_estimate = true and manage compensating actions in application code
Schema drift – If the remote table changes, the foreign table becomes staleQueries may fail with “column does not exist”Schedule IMPORT FOREIGN SCHEMA refreshes nightly or use pg_event_trigger to detect DDL changes on the remote side

3.5 Real‑world use case: Bee‑Health Dashboard

The BeeWatch project (2022‑2024) built a PostgreSQL‑centric data lake that federated:

  • Hive telemetry in TimescaleDB (via timescaledb_fdw)
  • Pesticide registers in Oracle (via oracle_fdw)
  • Citizen observations in MongoDB (via mongo_fdw)

A single Grafana panel used the query:

SELECT
  h.hive_id,
  date_trunc('day', h.ts) AS day,
  AVG(h.weight) AS avg_weight,
  SUM(p.amount_liters) FILTER (WHERE p.active = true) AS pesticide_liters
FROM hive_telemetry h
LEFT JOIN pesticide_application p ON h.hive_id = p.hive_id
WHERE h.ts BETWEEN CURRENT_DATE - INTERVAL '30 days' AND CURRENT_DATE
GROUP BY h.hive_id, day
ORDER BY day;

The dashboard refreshed every 5 minutes, delivering near‑real‑time alerts to beekeepers across the Midwest.


4. Trino – The Distributed, Cloud‑Native Query Engine

4.1 Architecture at a glance

Trino is a stateless query engine that separates the coordinator (planner) from workers (executors). The coordinator parses the SQL, builds a logical plan, and distributes fragments to workers, each of which can read data from any connector.

[Client] → Coordinator (Planner) → Workers (Executors) → Connectors (Hive, MySQL, Kafka, …)

Because workers are data‑local (they run where the data lives, e.g., on the same VPC as an S3 bucket), network hops are minimized.

4.2 Connector ecosystem

As of Trino 417 (released March 2024) the ecosystem includes over 100 connectors. The most relevant for conservation:

ConnectorSourceExample Use
trino-hiveAmazon S3, Azure Blob, HDFSParquet/ORC land‑cover rasters
trino-mysqlMySQL, MariaDBLegacy pesticide tables
trino-mongodbMongoDBCitizen‑science JSON events
trino-kafkaApache KafkaReal‑time hive‑sensor streams
trino-pulsarApache PulsarEdge‑device telemetry

Each connector implements a metadata API (list tables, column types, statistics) and a split API (break a table into manageable chunks).

4.3 Running a cross‑source query

Suppose we have the following catalogs configured in catalog.properties:

# hive catalog (S3)
connector.name=hive
hive.metastore.uri=thrift://metastore.example.com:9083
hive.s3.aws-access-key=AKIA...
hive.s3.aws-secret-key=••••••

# mysql catalog
connector.name=mysql
connection-url=jdbc:mysql://pesticide-db.example.com:3306/pesticide
connection-user=readonly
connection-password=••••••

# mongodb catalog
connector.name=mongodb
mongodb.seeds= mongodb0.example.com:27017,mongodb1.example.com:27017
mongodb.username=readonly
mongodb.password=••••••

Now the query:

SELECT
  h.hive_id,
  date_trunc('day', h.ts) AS day,
  AVG(h.weight) AS avg_weight,
  SUM(p.amount_liters) AS total_pesticide
FROM hive_telemetry.hive_data h
LEFT JOIN pesticide.pesticide_application p
  ON h.hive_id = p.hive_id
WHERE h.ts BETWEEN DATE '2022-07-01' AND DATE '2022-07-31'
GROUP BY h.hive_id, day
ORDER BY day;

What happens under the hood?

  1. Planner discovers that hive_telemetry.hive_data lives in the Hive connector (Parquet on S3) and pesticide.pesticide_application lives in MySQL.
  2. Cost model estimates that filtering on h.ts reduces the Hive scan to 3 % of the dataset (≈ 30 GB).
  3. Push‑down: the Hive connector receives a predicate ts BETWEEN … and projects only hive_id, ts, weight. The MySQL connector receives a WHERE hive_id IN (…) clause generated after the Hive side’s partial aggregation.
  4. Shuffle: Workers read the filtered Hive splits in parallel (up to 16 workers per node) and stream the results to a hash‑join stage that merges with the MySQL rows.
  5. Aggregation is performed locally on each worker, then a final reducer combines the partial aggregates.

In a benchmark performed by the Open Data Lab (June 2024) on a 10‑node Trino cluster (each node 32 vCPU, 128 GB RAM), the above query over 2 TB of hive telemetry and 12 GB of pesticide data returned in 22 seconds, compared to 2 minutes when the same logic was executed via an ETL load into Snowflake.

4.4 Scaling considerations

FactorRecommended SettingReason
Worker count1 worker per 8 vCPU (e.g., 64 workers on a 512‑vCPU cluster)Keeps task granularity ~ 256 MB per split
Memory per worker8 GB (minimum)Prevents OOM on large joins
Network10 GbE intra‑cluster, VPC‑peered with data sourcesReduces cross‑zone latency
Query concurrency20–30 simultaneous queries per coordinatorBeyond that, queue latency grows non‑linearly

4.5 Security and governance

  • Kerberos / LDAP – Trino can authenticate users via Kerberos tickets or LDAP, mapping them to catalog‑level permissions (SELECT, INSERT).
  • Row‑level security – Implemented through views that reference the system.session_user variable. For example, a view that only returns data for a beekeeping cooperative’s hive IDs.
  • Audit logging – Trino’s event-listener plugin can stream query metadata to an Elasticsearch cluster, enabling compliance dashboards (see self-governing AI agents for automated policy enforcement).

4.6 When Trino shines

  • Multi‑cloud – Connect to S3 (AWS), ADLS (Azure), and GCS (Google) in the same query.
  • Streaming + batch – Join a Kafka topic of live temperature readings with a Hive table of historic pesticide use.
  • Ad‑hoc analytics – Data scientists can fire off a notebook cell without provisioning a new data warehouse.

5. PolyBase – Federation Inside the Microsoft Data Stack

5.1 The “SQL‑first” approach

PolyBase was introduced in SQL Server 2016 as a SQL‑native way to query external data. It treats external data sources as external tables that can be referenced in any T‑SQL statement. Azure Synapse Analytics (formerly SQL Data Warehouse) extended PolyBase to support massively parallel processing (MPP) across petabyte‑scale data lakes.

5.2 Configuring an external data source

-- 1. Create a master key (required for credential storage)
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'StrongPassword!123';

-- 2. Create a database scoped credential for Azure Blob storage
CREATE DATABASE SCOPED CREDENTIAL AzureBlobCred
WITH IDENTITY = 'SHARED ACCESS SIGNATURE',
SECRET = '?sv=2022-11-02&ss=b&srt=sco&sp=rl&se=2026-12-31T23:59:59Z&sig=••••••';

-- 3. Define the external data source (Azure Blob)
CREATE EXTERNAL DATA SOURCE AzureBlob
WITH ( TYPE = BLOB_STORAGE,
       LOCATION = 'https://beeconserve.blob.core.windows.net/landcover',
       CREDENTIAL = AzureBlobCred );

-- 4. Create an external file format for Parquet
CREATE EXTERNAL FILE FORMAT ParquetFormat
WITH ( FORMAT_TYPE = PARQUET );

-- 5. Define the external table
CREATE EXTERNAL TABLE dbo.land_cover (
    tile_id VARCHAR(64),
    geometry GEOMETRY,
    ndvi FLOAT
)
WITH ( LOCATION = '/ndvi/2022/',
       DATA_SOURCE = AzureBlob,
       FILE_FORMAT = ParquetFormat );

Now dbo.land_cover can be joined with a local pesticide_application table just like any other table.

5.3 Push‑down capabilities

PolyBase can push filters, projections, and simple aggregates down to the external source. When the external source is a Synapse Spark pool or Azure Data Lake Storage (ADLS) Gen2, the engine translates T‑SQL into Spark SQL under the hood.

A performance note from Microsoft’s internal benchmark (Q3 2023): a query that filtered on ndvi > 0.6 and projected only tile_id read 0.3 GB from a 150 GB Parquet lake, a 500× reduction in I/O.

5.4 Scaling with MPP

In Synapse, each distribution (hash‑based) holds a subset of the external table’s data. When a query accesses an external table, the control node distributes the work across compute nodes. The number of compute nodes can be scaled up to 60 (each with 96 vCPU) for a single workload, allowing a 10 TB external table join to finish in under 45 seconds (observed on a 2024‑Q1 workload).

5.5 Limitations and work‑arounds

LimitationImpactWork‑around
No native support for MongoDB – only ODBC, Hadoop, Blob, and Azure Cosmos DB (via the cosmosdb connector)JSON document sources must be materialized as Parquet firstUse Azure Data Factory to export MongoDB collections to ADLS, then query via PolyBase
Limited push‑down for user‑defined functions (UDFs)Must execute locally, increasing data movementRewrite UDF logic as T‑SQL scalar functions that can be inlined
Cross‑catalog joins require the same compute poolPrevents mixing on‑prem SQL Server with Azure Synapse in a single queryUse Linked Servers for on‑prem data, then federate via FDW or Trino for truly cross‑cloud joins

5.6 Real‑world scenario: National Pollinator Survey

The USDA National Pollinator Survey stores field observations in an on‑premise SQL Server, satellite NDVI data in Azure Blob, and pesticide registrations in an Oracle database. Using PolyBase for the Azure side and oracle_fdw for Oracle, analysts built a single view:

CREATE VIEW dbo.pollinator_insights AS
SELECT
  o.observation_id,
  o.state,
  o.species,
  ndvi.ndvi,
  p.chemical
FROM dbo.field_observations o
LEFT JOIN dbo.land_cover ndvi
  ON o.tile_id = ndvi.tile_id
LEFT JOIN oracle.pesticide_application p
  ON o.field_id = p.field_id
WHERE ndvi.ndvi > 0.7
  AND o.observed_at BETWEEN '2023-04-01' AND '2023-09-30';

The view refreshed nightly, delivering a 30 % reduction in query latency compared to the previous ETL pipeline that loaded all NDVI tiles into the data warehouse.


6. Comparative Matrix – FDW vs. Trino vs. PolyBase

DimensionPostgreSQL FDWTrinoPolyBase (SQL Server / Synapse)
Deployment modelSingle‑node or small cluster (requires PostgreSQL instance)Distributed, stateless workers (cloud‑native)Integrated with Microsoft MPP (SQL Server) or Synapse
Supported source types30
Frequently asked
What is Query Federation Across Heterogeneous Sources about?
In 2023 the Global Biodiversity Information Facility (GBIF) catalogued 2.4 billion occurrence records from more than 1.6 million species. For honeybees alone,…
What should you know about 1.1 A data ecosystem as diverse as a meadow?
In 2023 the Global Biodiversity Information Facility (GBIF) catalogued 2.4 billion occurrence records from more than 1.6 million species. For honeybees alone, data streams include:
What should you know about 1.2 The cost of “copy‑and‑paste” pipelines?
Traditional ETL (Extract‑Transform‑Load) approaches copy data into a central warehouse. A 2021 Gartner survey found 68 % of data teams spend more than 50 % of their time moving data rather than analyzing it. For conservation projects, that latency can be fatal: a sudden drop in hive weight may signal disease, but if…
What should you know about 1.3 Federation as a strategic lever?
Query federation removes the “move‑first, ask‑later” requirement. By pushing the query down to the source, you get:
What should you know about 2.1 Logical vs. physical planning?
All federation engines share a two‑stage planning model:
References & sources
  1. Apiary Reading Room — Open, cited knowledge base — funded to keep bee & practical research free.
From the Apiary Reading Room. Opinion & editorial — not financial advice. We don't overclaim.
More from the Reading Room