> **Custom & Foreign Scans**
> <table>
> <tr>
> <td width="25%"><img src="assets/ex_customscanforeignscan.png"></td>
> <td>Scans external data sources (Foreign Data Wrappers) or executes custom extension logic.</td>
> </tr>
> </table>
>
> ```sql
> -- A self-contained file_fdw example using a tiny generated CSV stream
> CREATE EXTENSION IF NOT EXISTS file_fdw;
> CREATE SERVER lyap_local_files FOREIGN DATA WRAPPER file_fdw;
> CREATE FOREIGN TABLE external_logs (id integer, message text)
> SERVER lyap_local_files
> OPTIONS (
> program 'printf ''1,started\n2,ready\n''',
> format 'csv'
> );
>
> EXPLAIN (ANALYZE, COSTS, BUFFERS, VERBOSE)
> SELECT * FROM external_logs;
> ```
>
> 
>
> <!-- literal-explain-plan
> Captured EXPLAIN provenance for the adjacent reader-facing visual plan.
> Canonical capture metadata lives in scratch/actual_operation_plans.json.
>
> Foreign Scan on public.external_logs (cost=0.00..138.00 rows=1280 width=36) (actual time=4.577..4.578 rows=2 loops=1)
> Output: id, message
> Foreign Program: printf '1,started\n2,ready\n'
> Planning:
> Buffers: shared hit=29
> Planning Time: 0.054 ms
> Execution Time: 4.873 ms
> -->
>
>
> <!--
> Raw-capture provenance — separate run.
> SQL, setup, dataset, settings, and scope: artifacts/chapter4_capture_matrix.json.
> Target: PostgreSQL 18.x companion fixture. Cache state: uncontrolled.
> Boundary: pg_wait_tracer backend execution root; client states are included when the chart shows them.
> Fidelity: exact pg_wait_tracer export. Not the adjacent EXPLAIN run; compare state shape, not durations.
> -->
>
> 
>
> <table>
> <tr>
> <td rowspan="2" width="25%"><img src="assets/ex_foreign_scan.svg"></td>
> <td><b>Performance</b></td><td>Entirely dependent on the external source's latency and the efficiency of the Foreign Data Wrapper (FDW) or custom extension.</td>
> </tr>
> <tr><td><b>Cost</b></td><td>Defined by the FDW's custom cost model.</td></tr>
> </table>