TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Postgres query lock explainer

143 pointsby admtalabout 2 years ago
This is like 4 years old, but I’m braver now and ready to share stuff with this community. I’ve been a lurker for a while.<p>I made this tool - it’s kind of like “explain” but it tells you about what locks would be required by the query.<p>I was making it as part of a larger tool that would try to prevent deadlocks during migrations at my last company, I never finished it.

5 comments

fovcabout 2 years ago
It would be nice if you added a how it works section to the readme. Knowing that it does `begin; ${query}; rollback;` under the hood is important. The example with truncate in fact made me think the opposite was true, since truncate violates MVCC and cannot be rolled back.<p>I’m often worrying about locks in migrations that could be long running, so executing the query to figure out the locks defeats the purpose. Or at least I need to know to use a test DB.
评论 #35984344 未加载
评论 #35983678 未加载
评论 #35985390 未加载
efxhoyabout 2 years ago
Neat tool!<p>I prefer using environment variables for my connection info to avoid passwords in my shell history or plaintext config files. The standard[1] ones from libpq work in psycopg2 if you pass an empty connection string. Then you can keep them in the environment variables or do<p><pre><code> PGHOST=db.company.com PGUSER=postgres PGPORT=5432 PGDATABASE=postgres pg_explain_locks --query &quot;SELECT id FROM table&quot; </code></pre> [1] <a href="https:&#x2F;&#x2F;www.postgresql.org&#x2F;docs&#x2F;current&#x2F;libpq-envars.html" rel="nofollow">https:&#x2F;&#x2F;www.postgresql.org&#x2F;docs&#x2F;current&#x2F;libpq-envars.html</a>
评论 #35983839 未加载
hn92726819about 2 years ago
Not a postgres expert, but it looks like this is checked by <i>executing</i> the query: <a href="https:&#x2F;&#x2F;github.com&#x2F;AdmTal&#x2F;PostgreSQL-Query-Lock-Explainer&#x2F;blob&#x2F;master&#x2F;pg_explain_locks.py#L60">https:&#x2F;&#x2F;github.com&#x2F;AdmTal&#x2F;PostgreSQL-Query-Lock-Explainer&#x2F;bl...</a><p>Doesn&#x27;t this mean that checking the locks in a query like:<p><pre><code> Delete from t;commit </code></pre> Would kill everything? Or any query that can&#x27;t be transactional.
评论 #35983197 未加载
irrationalabout 2 years ago
If the query causes a trigger to fire, will any additional locks that are created by the trigger(s) (that is, any functions called by the triggers) be noted?
miohtamaabout 2 years ago
Related to this: would anyone know how to figure out which 1) applications and 2) queries where holidng locks after a dead lock happened, post mortem? As the default PSQL deadlock log output is very sparse to the point being useless.
评论 #35982785 未加载