> **Subquery Scan** > <table> > <tr> > <td width="25%"><img src="assets/ex_subqueryscan.png"></td> > <td>A wrapper node used when the planner needs to treat a subquery as a standalone relation. It passes the results of the subquery up the execution tree, often used to bridge different scopes of visibility or filtering.</td> > </tr> > </table> > > ```sql > -- OFFSET prevents pull-up; the outer filter requires a Subquery Scan > EXPLAIN (ANALYZE, COSTS, BUFFERS, VERBOSE) > SELECT * > FROM ( > SELECT id FROM animals > OFFSET 0 > ) s > WHERE id <= 5; > ``` > > ![SubqueryScan Plan Tree](assets/plan_tree_op_subquery_scan.svg) > > <!-- literal-explain-plan > Captured EXPLAIN provenance for the adjacent reader-facing visual plan. > Canonical capture metadata lives in scratch/actual_operation_plans.json. > > Subquery Scan on s (cost=0.00..299.00 rows=5 width=4) (actual time=0.008..0.567 rows=5 loops=1) > Output: s.id > Filter: (s.id <= 5) > Rows Removed by Filter: 9995 > Buffers: shared hit=74 > -> Seq Scan on public.animals (cost=0.00..174.00 rows=10000 width=4) (actual time=0.007..0.352 rows=10000 loops=1) > Output: animals.id > Buffers: shared hit=74 > Planning: > Buffers: shared hit=76 > Planning Time: 0.419 ms > Execution Time: 0.579 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. > --> > > ![Subquery Scan separate raw capture](assets/trace_op_subquery_scan.svg) > > <table> > <tr> > <td rowspan="2" width="25%"><img src="assets/ex_subplan.svg"></td> > <td><b>Performance</b></td><td>High performance; essentially a pass-through node for subqueries that the optimizer cannot flatten into the outer query.</td> > </tr> > <tr><td><b>Cost</b></td><td>Equivalent to the cost of the child subquery plan.</td></tr> > </table>