Debugging maxlocksper_transaction: A Journey into Pytest Parallelism
So I was fixing some slow tests, and whenever I ran them through the pytest
command, I was greeted with the dreaded max_locks_per_transaction
error.
My first instinct? Just crank up the max_locks_per_transaction
from 64 to 1024.
But... that didn’t feel right. I recreate my DB frequently, which means I’d have to set that value again and again. It felt like a hacky workaround rather than a proper solution.
Then, like any developer, I started digging around — first checking the Confluence page for dev docs to see if anyone else had faced this issue. No luck. Then I moved to Slack, and that’s where I found this command someone had shared:
pytest -n=0
This was new to me. So, like any sane dev in 2025, I asked ChatGPT what this was about. That’s how I came across pytest-xdist
.
What is pytest-xdist
?
The
pytest-xdist
plugin extends pytest with new test execution modes — the most common one is distributing tests across multiple CPUs to speed up test execution.
What does pytest-xdist
do?
Runs tests in parallel using
<numprocesses>
workers (Python processes), which is a game changer when: – You have a large test suite
– Each test takes a significant amount of time
– Your tests are independent (i.e., no shared global state)
That’s pretty much it — I plugged in pytest -n=0
and boom, no more transaction locks errors.
Cheers!
References – https://pytest-xdist.readthedocs.io/en/stable/ – https://docs.pytest.org/en/stable/reference/reference.html