> [!NOTE] 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) > > ```text > 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 > ``` > > ![LockRows measured plan performance signature](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>