> **LockRows** > <table> > <tr> > <td width="25%"><img src="assets/ex_lockrows.png"></td> > <td>Implements <code>FOR UPDATE</code> or <code>FOR SHARE</code> locking. This node ensures that the rows returned by the query are locked against concurrent modification until the current transaction commits or rolls back.</td> > </tr> > </table> > > ```sql > -- Locking a specific row for modification > EXPLAIN (ANALYZE, COSTS, BUFFERS, VERBOSE) > SELECT * FROM animals WHERE id = 1 FOR UPDATE; > ``` > > ![LockRows Plan Tree](assets/plan_tree_op_lock_rows.svg) > > <!-- literal-explain-plan > Captured EXPLAIN provenance for the adjacent reader-facing visual plan. > Canonical capture metadata lives in scratch/actual_operation_plans.json. > > LockRows (cost=0.29..8.31 rows=1 width=33) (actual time=0.017..0.020 rows=1 loops=1) > Output: id, name, species_id, created_at, ctid > Buffers: shared hit=6 > -> Index Scan using animals_pkey on public.animals (cost=0.29..8.30 rows=1 width=33) (actual time=0.009..0.012 rows=1 loops=1) > Output: id, name, species_id, created_at, ctid > Index Cond: (animals.id = 1) > Buffers: shared hit=5 > Planning: > Buffers: shared hit=86 > Planning Time: 0.180 ms > Execution Time: 0.037 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. > --> > > ![LockRows separate raw capture](assets/trace_op_lock_rows.svg) > > <table> > <tr> > <td rowspan="2" width="25%"><img src="assets/ex_lock_rows.svg"></td> > <td><b>Performance</b></td><td>Introduces overhead for lock acquisition and can lead to transaction blocking if other sessions held conflicting locks.</td> > </tr> > <tr><td><b>Cost</b></td><td><code>lock acquisition cost * number of rows</code></td></tr> > </table>