Loading…
Postgres FDW: Pushdown is a negotiation
ClickhouseKaushik Iska, David Wheeler, Philip Dubé
Summary
Postgres Foreign Data Wrapper extensions allow PostgreSQL to query external datastores like ClickHouse by delegating execution to remote engines. Engineering pg_clickhouse centers on pushdown decisions that determine whether SQL expressions run remotely or stream raw data back across the wire. Because pushdown depends on planner callbacks, deparser translations, and ClickHouse semantic compatibility, a single untranslated clause can block upper-level query pushdown entirely. Resolving pushdown barriers enables complex analytic queries to return hundreds of rows in milliseconds rather than pulling tens of millions of rows for local processing. Ultimately, pushdown development requires an iterative negotiation across differing SQL grammars, occasionally requiring the revocation of translations that fail to maintain strict equivalence.
Context
PostgreSQL Foreign Data Wrappers (FDWs) like pg_clickhouse query remote systems like ClickHouse, but transferring raw data across the network for local aggregation is slow. Engineering efficient FDW queries depends on determining which SQL clauses can be safely pushed down to the remote database without altering query semantics.
Approach / What changed
pg_clickhouse registers planning callbacks such as GetForeignPaths, GetForeignJoinPaths, and GetForeignUpperPaths with the Postgres planner. Its SQL deparser incrementally maps Postgres expressions—such as JSON access, window functions, and filtered aggregates—into ClickHouse SQL, prioritizing semantic correctness and revoking pushdowns when exact behavioral parity cannot be guaranteed.
Takeaways
- Pushdown is granular and all-or-nothing at the upper-relation level; a single unsupported sub-expression or aggregate can block an entire grouped query from running remotely.
- Foreign Data Wrappers register specific planning callbacks in Postgres, including GetForeignPaths and GetForeignUpperPaths, to offer foreign execution paths before deparsing SQL.
- Pushdowns must be revoked if SQL dialect semantics diverge, as seen when pg_clickhouse removed array function pushdowns to prevent returning incorrect results.