---
title: "Set time limits on every query · Agent Wranglers"
url: https://agentwranglers.se/playbook/data/operations/query-timeouts
updated: 2026-10-20
licence: "All rights reserved"
---

# Set time limits on every query

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Required
- Chapter: [Operations](https://agentwranglers.se/playbook/data/operations)
- Rule: Give every query and transaction a time limit, so one slow query cannot hold locks and connections forever.
- How: In PostgreSQL, set `statement_timeout` and `idle_in_transaction_session_timeout` for the app's role, and `transaction_timeout` from version 17. In MySQL, `max_execution_time` limits only `SELECT` statements, in milliseconds. In SQLite, set `busy_timeout` so a waiting write gives up. Set the driver's timeout too. Give reports and migrations their own longer limits on purpose. Keep the query limit shorter than the request's, as [timeouts](https://agentwranglers.se/playbook/api-design/reliability/timeouts) says.
- Check: Run `SELECT pg_sleep(60)` in PostgreSQL, or `SELECT SLEEP(60)` in MySQL, through the app's connection. It is cancelled at the set limit.

Example:

```sql
ALTER ROLE shop_app SET statement_timeout = '5s';
ALTER ROLE shop_app SET idle_in_transaction_session_timeout = '30s';
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers data playbook: https://agentwranglers.se/playbook/data/operations/query-timeouts`.

- Previous: [Pool database connections](https://agentwranglers.se/playbook/data/operations/connection-pooling)
- Next: [Watch slow queries](https://agentwranglers.se/playbook/data/operations/slow-query-log)
- [Operations](https://agentwranglers.se/playbook/data/operations)
