> **Sample Scan** > <table> > <tr> > <td width="25%"><img src="assets/ex_samplescan.png"></td> > <td>Implements the <code>TABLESAMPLE</code> clause. Instead of scanning the whole table, the engine uses a specific sampling method (Bernoulli or System) to select a random subset of pages or rows, drastically reducing I/O for statistical queries.</td> > </tr> > </table> > > ```sql > -- Sampling 10% of the animals table using the SYSTEM method > EXPLAIN (ANALYZE, COSTS, BUFFERS, VERBOSE) > SELECT * FROM animals TABLESAMPLE SYSTEM (10); > ``` > > ![SampleScan Plan Tree](assets/plan_tree_op_sample_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. > > Sample Scan on public.animals (cost=0.00..38.00 rows=1000 width=27) (actual time=0.003..0.013 rows=272 loops=1) > Output: id, name, species_id, created_at > Sampling: system ('10'::real) > Buffers: shared hit=2 > Planning: > Buffers: shared hit=73 > Planning Time: 0.163 ms > Execution Time: 0.025 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. > --> > > ![Sample Scan separate raw capture](assets/trace_op_sample_scan.svg) > > <table> > <tr> > <td rowspan="2" width="25%"><img src="assets/ex_scan.svg"></td> > <td><b>Performance</b></td><td>High performance; <code>SYSTEM</code> sampling is typically much faster than <code>BERNOULLI</code> as it samples at the block level rather than the row level.</td> > </tr> > <tr><td><b>Cost</b></td><td><code>sampling cost * sample size</code></td></tr> > </table>