<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Row Locking on Postgres Scripts</title><link>https://www.postgresscripts.com/tags/row-locking/</link><description>Recent content in Row Locking on Postgres Scripts</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>PostgresScripts.com</copyright><lastBuildDate>Sat, 24 Oct 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://www.postgresscripts.com/tags/row-locking/index.xml" rel="self" type="application/rss+xml"/><item><title>Claim Queue Rows with FOR UPDATE SKIP LOCKED in PostgreSQL</title><link>https://www.postgresscripts.com/post/claim-queue-rows-with-for-update-skip-locked-in-postgresql/</link><pubDate>Sat, 24 Oct 2026 00:00:00 +0000</pubDate><guid>https://www.postgresscripts.com/post/claim-queue-rows-with-for-update-skip-locked-in-postgresql/</guid><description>
&lt;h2 id="claim-queue-rows-with-for-update-skip-locked-in-postgresql"&gt;Claim Queue Rows with FOR UPDATE SKIP LOCKED in PostgreSQL&lt;/h2&gt;
&lt;p&gt;Several workers poll the same job table with &lt;code&gt;SELECT ... LIMIT 10&lt;/code&gt;, and a plain LIMIT does nothing to stop two of them from &lt;a href="https://www.crunchydata.com/blog/message-queuing-using-native-postgresql"&gt;getting the same list of items to process&lt;/a&gt;. Add &lt;code&gt;FOR UPDATE&lt;/code&gt; and &lt;a href="https://www.crunchydata.com/blog/message-queuing-using-native-postgresql"&gt;the second worker hangs&lt;/a&gt; until the first one commits. Put &lt;code&gt;FOR UPDATE SKIP LOCKED&lt;/code&gt; in the CTE of a batch &lt;code&gt;DELETE ... RETURNING&lt;/code&gt; and &lt;a href="https://www.postgresql.org/docs/current/sql-select.html"&gt;rows that cannot be locked immediately are skipped&lt;/a&gt;, so each worker deletes and receives its own batch in one statement.&lt;/p&gt;</description></item></channel></rss>