Glossary

What is a federated query?

A federated query is a single SQL statement that reads from two or more separate systems, such as a PostgreSQL database and Parquet files in object storage, and returns one result. The engine that runs it fetches data from each source at query time, and nothing is loaded in advance.

How a federated query runs

To the person writing it, a federated query is ordinary SQL whose table names point at different systems. The engine splits it into steps:

StepWhat happensWhere
BindTable names are resolved to sources, and their columns and types are readThe engine, from each source's metadata
Push downFilters, column lists and sometimes whole aggregates are rewritten into the source's own SQLThe source
FetchThe rows that are left travel backThe network
CombineJoins across sources, remaining filters, aggregates and sortingThe engine

Most of the cost is decided at the second step. Whatever the source does not filter out has to be fetched; see predicate pushdown. Running queries this way across an organization's systems is called data federation.

An example: PostgreSQL and Parquet in one statement

This uses DuckDB as the engine. One million orders live in a PostgreSQL database, with an index on order_date, and 1,000 customers are a Parquet file.

DuckDB
ATTACH 'host=localhost dbname=postgres user=postgres' AS shop (TYPE postgres);

CREATE TABLE shop.public.orders AS
SELECT range                                         AS order_id,
       (range // 330) % 1000                         AS customer_id,
       ((range * 37) % 20000 / 100.0)::DECIMAL(10,2) AS amount,
       DATE '2026-01-01' + (range % 330)::INT        AS order_date
FROM range(1_000_000);
CALL postgres_execute('shop', 'CREATE INDEX ON public.orders (order_date)');

COPY (
  SELECT range AS id,
         'Customer ' || range AS name,
         CASE WHEN range % 10 = 0 THEN 'enterprise'
              WHEN range % 10 < 4 THEN 'mid-market'
              ELSE 'smb' END AS segment
  FROM range(1000)
) TO 'customers.parquet' (FORMAT parquet);

-- One statement, two systems
SELECT c.segment, count(*) AS orders, sum(o.amount) AS revenue
FROM shop.public.orders  o
JOIN 'customers.parquet' c ON c.id = o.customer_id
WHERE o.order_date = DATE '2026-11-26'
GROUP BY c.segment
ORDER BY revenue DESC;

It returns 1,818 orders and 181,838.84 in revenue for SMB, 909 and 91,069.37 for mid-market, and 303 and 30,097.19 for enterprise, in about 27 ms. With SET pg_debug_show_queries = true, DuckDB prints what it sends to PostgreSQL: a SELECT of customer_id, order_date and amount from public.orders with the condition "order_date" = '2026-11-26'. The filter and the column list went to the source, and 3,030 of the 1 million rows came back. The join with the Parquet file and the GROUP BY ran in DuckDB.

Why some federated queries are slow

Two other ways to write a similar question look harmless and do far more work.

DuckDB
-- The date comes from a subquery, so it is not a constant when the scan is planned
SELECT c.segment, count(*) AS orders, sum(o.amount) AS revenue
FROM shop.public.orders  o
JOIN 'customers.parquet' c ON c.id = o.customer_id
WHERE o.order_date = (SELECT max(order_date) FROM shop.public.orders)
GROUP BY c.segment
ORDER BY revenue DESC;

-- The filter is on the other source
SELECT count(*) AS orders, sum(o.amount) AS revenue
FROM shop.public.orders  o
JOIN 'customers.parquet' c ON c.id = o.customer_id
WHERE c.name = 'Customer 7';
QueryRows fetched from PostgreSQLTime
Date as a literal3,03027 ms
Date from a subquery1,000,000, plus the order_date column a second time900 ms
Two statements: max(order_date), then the literal1, then 3,03012 ms + 27 ms
Filter on the Parquet side (c.name)1,000,000500 ms
The same customer as a literal (o.customer_id = 7)1,32053 ms
DuckDB 2.0.1 against PostgreSQL 16 in a local container, median of five runs. Over a real network the gap is wider.

In the subquery version PostgreSQL received no date condition. It sent every row, and DuckDB discarded all but 3,030. Run on its own, SELECT max(order_date) was sent to PostgreSQL as an aggregate and came back as one row in 12 ms, so reading the date first and writing it as a literal in the next statement takes about 39 ms in total instead of 900.

In the last pair, one customer matches, but that fact lives in the Parquet file. PostgreSQL was asked for the whole table to return 1,320 orders. Filtering the orders table on the key itself sends the condition to PostgreSQL and takes about a tenth of the time.

Writing federated queries that stay fast

  • Filter the remote table with constants. A condition that compares a column with a literal is the one every engine can push down.
  • Select only the columns you need. The column list is pushed down too.
  • Compute dynamic values, such as the latest date or a set of keys, in their own statement, then write them as literals.
  • Read the plan. EXPLAIN shows which filters reached each scan, and anything missing there runs after the fetch.
  • Copy what you query heavily. A large remote table that a dashboard reads every few minutes is cheaper as a local columnar table; see ELT vs ETL.

What vendors mean by federated query

ProductWhat the term covers
BigQueryA statement sent to Cloud SQL, AlloyDB or Spanner through EXTERNAL_QUERY, with the result returned as a table
Amazon AthenaAthena Federated Query: connectors that run as AWS Lambda functions and read sources other than S3
Amazon RedshiftLive reads from RDS and Aurora PostgreSQL and MySQL databases
Trino and PrestoEvery table comes through a connector, so a join across two catalogs is the normal case
PostgreSQLForeign data wrappers such as postgres_fdw, which expose remote tables as foreign tables
DuckDBATTACH for PostgreSQL, MySQL and SQLite databases, and readers for Parquet, CSV and JSON files

In Thunderduck

In Thunderduck every catalog can be named in the same statement, so a federated query needs no special syntax: catalog.schema.table for PostgreSQL, MySQL, SQL Server and Oracle, catalog.dataset.table for BigQuery, catalog.default.table for Iceberg, and read_parquet('catalog/path') for files in object storage.

The pushdown rules match the example above. On PostgreSQL, MySQL and BigQuery, filters that compare a column with constants run in the source, as do GROUP BY, ORDER BY and LIMIT when the remote table is alone in its SELECT. A filter that depends on another table or a subquery stays local and the remote table is read in full, so read a dynamic value such as a high-water mark in one statement and write it as a literal in the next.

Each pushed-down statement carries a comment with the Thunderduck run and user id, so it can be found in the source database's own log. Every query is priced from its plan before it runs, and a scan of a large external table with no selective filter is refused.

Docs: https://docs.thunderduck.ai/catalogs/postgresql-mysql/

Related terms

Query the data where it lives.

Databases, warehouses and buckets in one SQL statement, from SQL clients, BI tools and AI agents.