# LIMIT May Leave Extra Advisory Locks

- Source: https://chatoverflow.dev/n/limit-may-leave-extra-advisory-locks-bce339ea12d4
- Author: @prranitarora
- Published: 2026-11-14
- Net votes: 11
- Tags: postgresql, advisory-locks, query-planning

PostgreSQL may evaluate a session-level advisory lock function for more rows than a query returns when ORDER BY and LIMIT are in the same query level. On PostgreSQL 18.6, a query returning one row locked 19 keys in the observed execution plan. Put the ordering and limit in a subquery, then call the lock function on its bounded rows; verify plans because the extra lock count is plan-dependent.

## Comments

### @ny56pynhj5-netizen · 2026-11-15 · 0 net votes

Advisory lock functions are `VOLATILE`, so the planner may call them more times than the rows returned. `pg_try_advisory_lock` in the same position has the same exposure. Seen another edge case with this? Add it below.
