<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Seqora Blog: PostgreSQL</title>
  <subtitle>Posts about PostgreSQL from the Seqora blog. Not affiliated with the PostgreSQL Global Development Group.</subtitle>
  <id>https://seqora.dev/blog/postgresql/feed.xml</id>
  <link href="https://seqora.dev/blog/" />
  <link rel="self" type="application/atom+xml" href="https://seqora.dev/blog/postgresql/feed.xml" />
  <updated>2026-11-18T00:00:00Z</updated>
  <author><name>Ayushman</name></author>
  <entry>
    <title>Which lock does ALTER TABLE take in PostgreSQL</title>
    <link href="https://seqora.dev/blog/alter-table-locks/" />
    <id>https://seqora.dev/blog/alter-table-locks/</id>
    <published>2026-11-18T00:00:00Z</published>
    <updated>2026-11-18T00:00:00Z</updated>
    <author><name>Ayushman</name></author>
    <summary>The lock each PostgreSQL schema change takes, what it blocks, and the safer form, from ADD COLUMN and SET NOT NULL to foreign keys, indexes, partitions and lock_timeout.</summary>
    <content type="html">&lt;div class=&quot;guide-gate&quot;&gt;
&lt;p&gt;Most &lt;code&gt;ALTER TABLE&lt;/code&gt; forms take an &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; lock, which blocks reads and writes on the table until the transaction commits. A few take something lighter, and most of the slow ones have a safer form. The table below gives the lock, what it blocks and the safer form for each common change. It holds for PostgreSQL 18, and the version notes say where older servers differ. If you only need one thing from this page, it is &lt;code&gt;SET lock_timeout&lt;/code&gt; before every migration.&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;the-table&quot;&gt;The table&lt;a class=&quot;guide-anchor&quot; href=&quot;#the-table&quot; aria-label=&quot;Link to this section&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#hash&quot; /&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Reads means a plain &lt;code&gt;SELECT&lt;/code&gt;, which takes &lt;code&gt;ACCESS SHARE&lt;/code&gt;. Writes means &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;, &lt;code&gt;DELETE&lt;/code&gt; and &lt;code&gt;MERGE&lt;/code&gt;, which take &lt;code&gt;ROW EXCLUSIVE&lt;/code&gt;. Work is what happens while the lock is held: instant is a catalog change, scan reads every row, rewrite copies the table and its indexes.&lt;/p&gt;
&lt;div class=&quot;guide-table&quot;&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Statement&lt;/th&gt;
&lt;th&gt;Lock on the table&lt;/th&gt;
&lt;th&gt;Reads&lt;/th&gt;
&lt;th&gt;Writes&lt;/th&gt;
&lt;th&gt;Work&lt;/th&gt;
&lt;th&gt;Safer form&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ADD COLUMN&lt;/code&gt; with no default, or a non-volatile one such as &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;now()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Instant (11+)&lt;/td&gt;
&lt;td&gt;Already the safe form. Bound the wait with &lt;code&gt;lock_timeout&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ADD COLUMN&lt;/code&gt; with a volatile default (&lt;code&gt;clock_timestamp()&lt;/code&gt;, &lt;code&gt;gen_random_uuid()&lt;/code&gt;), identity, serial, stored generated&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Rewrite&lt;/td&gt;
&lt;td&gt;Add the column bare, &lt;code&gt;SET DEFAULT&lt;/code&gt;, backfill in batches&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ADD COLUMN … GENERATED ALWAYS AS (…) VIRTUAL&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Instant (18+)&lt;/td&gt;
&lt;td&gt;Already the safe form&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;DROP COLUMN&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Instant&lt;/td&gt;
&lt;td&gt;Already the safe form&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ALTER COLUMN … TYPE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Rewrite, unless binary coercible&lt;/td&gt;
&lt;td&gt;None in PostgreSQL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;SET DEFAULT&lt;/code&gt;, &lt;code&gt;DROP DEFAULT&lt;/code&gt;, &lt;code&gt;DROP NOT NULL&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Instant&lt;/td&gt;
&lt;td&gt;Already the safe form&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ALTER COLUMN … SET NOT NULL&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Scan&lt;/td&gt;
&lt;td&gt;&lt;code&gt;CHECK (col IS NOT NULL) NOT VALID&lt;/code&gt;, &lt;code&gt;VALIDATE&lt;/code&gt;, &lt;code&gt;SET NOT NULL&lt;/code&gt;, drop the check (12+)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ADD CONSTRAINT … CHECK&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Scan&lt;/td&gt;
&lt;td&gt;&lt;code&gt;NOT VALID&lt;/code&gt;, then &lt;code&gt;VALIDATE CONSTRAINT&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ADD CONSTRAINT … FOREIGN KEY&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;SHARE ROW EXCLUSIVE&lt;/code&gt; on both tables&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Scan&lt;/td&gt;
&lt;td&gt;&lt;code&gt;NOT VALID&lt;/code&gt;, then &lt;code&gt;VALIDATE CONSTRAINT&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;VALIDATE CONSTRAINT&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt;, and &lt;code&gt;ROW SHARE&lt;/code&gt; on a foreign key&amp;#39;s referenced table&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;td&gt;Scan&lt;/td&gt;
&lt;td&gt;This is the safer form&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ADD PRIMARY KEY&lt;/code&gt;, &lt;code&gt;ADD UNIQUE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Index build&lt;/td&gt;
&lt;td&gt;&lt;code&gt;CREATE UNIQUE INDEX CONCURRENTLY&lt;/code&gt;, then &lt;code&gt;ADD CONSTRAINT … USING INDEX&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ADD CONSTRAINT … USING INDEX&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Instant, or a scan for a primary key&lt;/td&gt;
&lt;td&gt;Make the key columns &lt;code&gt;NOT NULL&lt;/code&gt; first&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CREATE INDEX&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;SHARE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Index build&lt;/td&gt;
&lt;td&gt;&lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;td&gt;Two scans, waits for transactions&lt;/td&gt;
&lt;td&gt;This is the safer form&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;DROP INDEX&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Instant&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DROP INDEX CONCURRENTLY&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;RENAME&lt;/code&gt; a table, column or constraint&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Instant&lt;/td&gt;
&lt;td&gt;Already the safe form&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ALTER INDEX … RENAME&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt; on the index (12+)&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;td&gt;Instant&lt;/td&gt;
&lt;td&gt;Already the safe form&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;REINDEX&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;SHARE&lt;/code&gt;, and &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; on the index&lt;/td&gt;
&lt;td&gt;Blocked, in practice&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Index build&lt;/td&gt;
&lt;td&gt;&lt;code&gt;REINDEX … CONCURRENTLY&lt;/code&gt; (12+)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;VACUUM FULL&lt;/code&gt;, &lt;code&gt;CLUSTER&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Rewrite&lt;/td&gt;
&lt;td&gt;Plain &lt;code&gt;VACUUM&lt;/code&gt;, which keeps the space for reuse instead of returning it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;TRUNCATE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Instant&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DELETE&lt;/code&gt; in batches, under &lt;code&gt;ROW EXCLUSIVE&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ATTACH PARTITION&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt; on the parent (12+), &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; on the partition&lt;/td&gt;
&lt;td&gt;Parent allowed&lt;/td&gt;
&lt;td&gt;Parent allowed&lt;/td&gt;
&lt;td&gt;Scan of the partition&lt;/td&gt;
&lt;td&gt;Add a valid &lt;code&gt;CHECK&lt;/code&gt; that matches the bounds first&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;DETACH PARTITION&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; on the parent and the partition&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Blocked&lt;/td&gt;
&lt;td&gt;Instant&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DETACH PARTITION … CONCURRENTLY&lt;/code&gt; (14+)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;Every row is &amp;quot;until the transaction commits&amp;quot;. A lock is never released at the end of the statement that took it. The &lt;a href=&quot;https://www.postgresql.org/docs/current/explicit-locking.html&quot; rel=&quot;noopener&quot;&gt;explicit locking chapter&lt;svg class=&quot;guide-icon guide-external&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#arrow-up-right&quot; /&gt;&lt;/svg&gt;&lt;/a&gt; has the full list of modes and Table 13.2, which says which modes conflict. The &lt;a href=&quot;https://www.postgresql.org/docs/current/sql-altertable.html&quot; rel=&quot;noopener&quot;&gt;ALTER TABLE page&lt;svg class=&quot;guide-icon guide-external&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#arrow-up-right&quot; /&gt;&lt;/svg&gt;&lt;/a&gt; gives the lock for each subcommand. Its rule is &amp;quot;An ACCESS EXCLUSIVE lock is acquired unless explicitly noted&amp;quot;, and for several statements above, nothing is noted.&lt;/p&gt;
&lt;h2 id=&quot;instant-still-waits-for-its-lock&quot;&gt;Instant still waits for its lock&lt;a class=&quot;guide-anchor&quot; href=&quot;#instant-still-waits-for-its-lock&quot; aria-label=&quot;Link to this section&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#hash&quot; /&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;ADD COLUMN&lt;/code&gt;, &lt;code&gt;DROP COLUMN&lt;/code&gt;, &lt;code&gt;RENAME&lt;/code&gt; and &lt;code&gt;SET DEFAULT&lt;/code&gt; change the catalog and finish in milliseconds. They still need &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;, and to get it they wait for every open transaction that has touched the table to finish. A report that has been reading &lt;code&gt;orders&lt;/code&gt; for twenty minutes holds &lt;code&gt;ACCESS SHARE&lt;/code&gt; for those twenty minutes, and your instant &lt;code&gt;ALTER&lt;/code&gt; waits behind it. While it waits, it blocks everyone else.&lt;/p&gt;
&lt;h2 id=&quot;the-lock-queue&quot;&gt;The lock queue&lt;a class=&quot;guide-anchor&quot; href=&quot;#the-lock-queue&quot; aria-label=&quot;Link to this section&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#hash&quot; /&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A lock request waits in a queue. A new request has to wait for the ones already granted that it conflicts with, and also for the ones already waiting that it conflicts with. &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; conflicts with everything, so once an &lt;code&gt;ALTER&lt;/code&gt; is waiting, every &lt;code&gt;SELECT&lt;/code&gt; that arrives after it waits too. The &lt;a href=&quot;https://www.postgresql.org/docs/current/functions-info.html&quot; rel=&quot;noopener&quot;&gt;pg_blocking_pids documentation&lt;svg class=&quot;guide-icon guide-external&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#arrow-up-right&quot; /&gt;&lt;/svg&gt;&lt;/a&gt; calls the second kind a soft block: a session that &amp;quot;is waiting for a lock that would conflict with the blocked process&amp;#39;s lock request and is ahead of it in the wait queue&amp;quot;.&lt;/p&gt;
&lt;p&gt;Three sessions on &lt;code&gt;orders&lt;/code&gt; show it.&lt;/p&gt;
&lt;div class=&quot;guide-code-block&quot;&gt;
&lt;div class=&quot;guide-code-tools&quot;&gt;&lt;span class=&quot;guide-code-lang&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#file-sql&quot; /&gt;&lt;/svg&gt;SQL&lt;/span&gt;&lt;button class=&quot;guide-code-copy&quot; type=&quot;button&quot; data-code-copy hidden&gt;&lt;svg class=&quot;guide-icon guide-code-copy-idle&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#copy&quot; /&gt;&lt;/svg&gt;&lt;svg class=&quot;guide-icon guide-code-copy-done&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#check&quot; /&gt;&lt;/svg&gt;&lt;span data-code-copy-label aria-live=&quot;polite&quot;&gt;Copy&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;
&lt;pre class=&quot;guide-code&quot; data-lang=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;tok-comment&quot;&gt;-- Session 1: a transaction that read orders and is still open.&lt;/span&gt;
&lt;span class=&quot;tok-key&quot;&gt;BEGIN&lt;/span&gt;;
&lt;span class=&quot;tok-key&quot;&gt;SELECT&lt;/span&gt; count(*) &lt;span class=&quot;tok-key&quot;&gt;FROM&lt;/span&gt; orders;
&lt;span class=&quot;tok-comment&quot;&gt;-- (no COMMIT yet)&lt;/span&gt;

&lt;span class=&quot;tok-comment&quot;&gt;-- Session 2: an instant change, which now waits for session 1.&lt;/span&gt;
&lt;span class=&quot;tok-key&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;TABLE&lt;/span&gt; orders &lt;span class=&quot;tok-key&quot;&gt;ADD&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;COLUMN&lt;/span&gt; gift_note text;

&lt;span class=&quot;tok-comment&quot;&gt;-- Session 3: an ordinary read, which now waits for session 2.&lt;/span&gt;
&lt;span class=&quot;tok-key&quot;&gt;SELECT&lt;/span&gt; * &lt;span class=&quot;tok-key&quot;&gt;FROM&lt;/span&gt; orders &lt;span class=&quot;tok-key&quot;&gt;WHERE&lt;/span&gt; id = &lt;span class=&quot;tok-number&quot;&gt;1001&lt;/span&gt;;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Session 3 needs only &lt;code&gt;ACCESS SHARE&lt;/code&gt;, which session 1 is happy to share. It waits anyway, because session 2 is ahead of it in the queue. Every query on &lt;code&gt;orders&lt;/code&gt; now queues until session 1 commits, and then session 2 runs for a millisecond and lets them all go. If session 1 is a connection someone left idle in a transaction, that can be hours.&lt;/p&gt;
&lt;p&gt;PostgreSQL waits for a lock indefinitely by default. &lt;code&gt;lock_timeout&lt;/code&gt; puts a limit on it. Set it for the transaction that runs the migration:&lt;/p&gt;
&lt;div class=&quot;guide-code-block&quot;&gt;
&lt;div class=&quot;guide-code-tools&quot;&gt;&lt;span class=&quot;guide-code-lang&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#file-sql&quot; /&gt;&lt;/svg&gt;SQL&lt;/span&gt;&lt;button class=&quot;guide-code-copy&quot; type=&quot;button&quot; data-code-copy hidden&gt;&lt;svg class=&quot;guide-icon guide-code-copy-idle&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#copy&quot; /&gt;&lt;/svg&gt;&lt;svg class=&quot;guide-icon guide-code-copy-done&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#check&quot; /&gt;&lt;/svg&gt;&lt;span data-code-copy-label aria-live=&quot;polite&quot;&gt;Copy&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;
&lt;pre class=&quot;guide-code&quot; data-lang=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;tok-key&quot;&gt;BEGIN&lt;/span&gt;;
&lt;span class=&quot;tok-key&quot;&gt;SET&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;LOCAL&lt;/span&gt; lock_timeout = &lt;span class=&quot;tok-string&quot;&gt;&amp;#39;3s&amp;#39;&lt;/span&gt;;
&lt;span class=&quot;tok-key&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;TABLE&lt;/span&gt; orders &lt;span class=&quot;tok-key&quot;&gt;ADD&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;COLUMN&lt;/span&gt; gift_note text;
&lt;span class=&quot;tok-key&quot;&gt;COMMIT&lt;/span&gt;;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;If the lock isn&amp;#39;t granted in three seconds, the statement fails with &lt;code&gt;canceling statement due to lock timeout&lt;/code&gt;, SQLSTATE &lt;code&gt;55P03&lt;/code&gt;, and the queue behind it drains. Nothing was changed. Retry it a little later, or find what was holding the table first. The &lt;a href=&quot;https://www.postgresql.org/docs/current/runtime-config-client.html#GUC-LOCK-TIMEOUT&quot; rel=&quot;noopener&quot;&gt;lock_timeout setting&lt;svg class=&quot;guide-icon guide-external&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#arrow-up-right&quot; /&gt;&lt;/svg&gt;&lt;/a&gt; applies to each lock acquisition separately. A &lt;code&gt;statement_timeout&lt;/code&gt; equal or shorter makes it pointless, so keep that one longer.&lt;/p&gt;
&lt;p&gt;Two more rules follow from the locks being held to commit:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Keep a migration&amp;#39;s transaction short. Ten statements in one transaction hold the strongest lock any of them took until the tenth is done.&lt;/li&gt;
&lt;li&gt;Don&amp;#39;t put a slow statement after a fast &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; one in the same transaction. The fast one&amp;#39;s lock lasts as long as the slow one runs.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;who-holds-the-lock-and-who-waits&quot;&gt;Who holds the lock, and who waits&lt;a class=&quot;guide-anchor&quot; href=&quot;#who-holds-the-lock-and-who-waits&quot; aria-label=&quot;Link to this section&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#hash&quot; /&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This lists every session with a lock on &lt;code&gt;orders&lt;/code&gt;, granted first, with what each one is blocked by:&lt;/p&gt;
&lt;div class=&quot;guide-code-block&quot;&gt;
&lt;div class=&quot;guide-code-tools&quot;&gt;&lt;span class=&quot;guide-code-lang&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#file-sql&quot; /&gt;&lt;/svg&gt;SQL&lt;/span&gt;&lt;button class=&quot;guide-code-copy&quot; type=&quot;button&quot; data-code-copy hidden&gt;&lt;svg class=&quot;guide-icon guide-code-copy-idle&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#copy&quot; /&gt;&lt;/svg&gt;&lt;svg class=&quot;guide-icon guide-code-copy-done&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#check&quot; /&gt;&lt;/svg&gt;&lt;span data-code-copy-label aria-live=&quot;polite&quot;&gt;Copy&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;
&lt;pre class=&quot;guide-code&quot; data-lang=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;tok-key&quot;&gt;SELECT&lt;/span&gt; a.pid,
       pg_blocking_pids(a.pid) &lt;span class=&quot;tok-key&quot;&gt;AS&lt;/span&gt; blocked_by,
       l.mode,
       l.granted,
       a.state,
       now() - a.xact_start &lt;span class=&quot;tok-key&quot;&gt;AS&lt;/span&gt; transaction_age,
       &lt;span class=&quot;tok-key&quot;&gt;left&lt;/span&gt;(a.query, &lt;span class=&quot;tok-number&quot;&gt;60&lt;/span&gt;) &lt;span class=&quot;tok-key&quot;&gt;AS&lt;/span&gt; query
&lt;span class=&quot;tok-key&quot;&gt;FROM&lt;/span&gt; pg_locks &lt;span class=&quot;tok-key&quot;&gt;AS&lt;/span&gt; l
&lt;span class=&quot;tok-key&quot;&gt;JOIN&lt;/span&gt; pg_stat_activity &lt;span class=&quot;tok-key&quot;&gt;AS&lt;/span&gt; a &lt;span class=&quot;tok-key&quot;&gt;ON&lt;/span&gt; a.pid = l.pid
&lt;span class=&quot;tok-key&quot;&gt;WHERE&lt;/span&gt; l.relation = &lt;span class=&quot;tok-string&quot;&gt;&amp;#39;orders&amp;#39;&lt;/span&gt;&lt;span class=&quot;tok-literal&quot;&gt;::regclass&lt;/span&gt;
&lt;span class=&quot;tok-key&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;BY&lt;/span&gt; l.granted &lt;span class=&quot;tok-key&quot;&gt;DESC&lt;/span&gt;, a.xact_start;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;A row with &lt;code&gt;granted&lt;/code&gt; false is waiting. Its &lt;code&gt;blocked_by&lt;/code&gt; names the sessions in its way, both the ones holding a lock and the ones queued ahead of it. A long &lt;code&gt;transaction_age&lt;/code&gt; with the state &lt;code&gt;idle in transaction&lt;/code&gt; is the usual culprit. The documentation warns that frequent calls to &lt;code&gt;pg_blocking_pids&lt;/code&gt; can affect performance, because it briefly needs exclusive access to the lock manager&amp;#39;s state, so run it when you need it, not in a loop.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;SELECT pg_cancel_backend(pid)&lt;/code&gt; cancels a session&amp;#39;s current query. &lt;code&gt;pg_terminate_backend(pid)&lt;/code&gt; ends the session, which rolls back its transaction and releases its locks. Both need the right to signal that session.&lt;/p&gt;
&lt;h2 id=&quot;adding-a-column&quot;&gt;Adding a column&lt;a class=&quot;guide-anchor&quot; href=&quot;#adding-a-column&quot; aria-label=&quot;Link to this section&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#hash&quot; /&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;From 11, a column added with no default, or with a non-volatile default, is a catalog change. The &lt;a href=&quot;https://www.postgresql.org/docs/current/sql-altertable.html#SQL-ALTERTABLE-NOTES&quot; rel=&quot;noopener&quot;&gt;ALTER TABLE notes&lt;svg class=&quot;guide-icon guide-external&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#arrow-up-right&quot; /&gt;&lt;/svg&gt;&lt;/a&gt; say the default &amp;quot;is evaluated at the time of the statement and the result stored in the table&amp;#39;s metadata&amp;quot;. &lt;code&gt;DEFAULT 0&lt;/code&gt;, &lt;code&gt;DEFAULT &amp;#39;pending&amp;#39;&lt;/code&gt; and &lt;code&gt;DEFAULT now()&lt;/code&gt; are all instant, because &lt;code&gt;now()&lt;/code&gt; is stable within a transaction. On 10 and older, any default rewrote the table.&lt;/p&gt;
&lt;p&gt;These still rewrite the table and every index on it, on every version:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a volatile default, such as &lt;code&gt;clock_timestamp()&lt;/code&gt;, &lt;code&gt;random()&lt;/code&gt; or &lt;code&gt;gen_random_uuid()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;an identity column, or &lt;code&gt;serial&lt;/code&gt;, whose default is &lt;code&gt;nextval()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;a stored generated column&lt;/li&gt;
&lt;li&gt;a column whose type is a domain with constraints&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;From 18, a virtual generated column never needs a rewrite, and generated columns are virtual unless you write &lt;code&gt;STORED&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;A rewrite holds &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; for as long as copying the table takes. The safer form splits the work so the lock is short each time:&lt;/p&gt;
&lt;div class=&quot;guide-code-block&quot;&gt;
&lt;div class=&quot;guide-code-tools&quot;&gt;&lt;span class=&quot;guide-code-lang&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#file-sql&quot; /&gt;&lt;/svg&gt;SQL&lt;/span&gt;&lt;button class=&quot;guide-code-copy&quot; type=&quot;button&quot; data-code-copy hidden&gt;&lt;svg class=&quot;guide-icon guide-code-copy-idle&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#copy&quot; /&gt;&lt;/svg&gt;&lt;svg class=&quot;guide-icon guide-code-copy-done&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#check&quot; /&gt;&lt;/svg&gt;&lt;span data-code-copy-label aria-live=&quot;polite&quot;&gt;Copy&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;
&lt;pre class=&quot;guide-code&quot; data-lang=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;tok-key&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;TABLE&lt;/span&gt; orders &lt;span class=&quot;tok-key&quot;&gt;ADD&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;COLUMN&lt;/span&gt; public_id uuid;
&lt;span class=&quot;tok-key&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;TABLE&lt;/span&gt; orders &lt;span class=&quot;tok-key&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;COLUMN&lt;/span&gt; public_id &lt;span class=&quot;tok-key&quot;&gt;SET&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;DEFAULT&lt;/span&gt; gen_random_uuid();

&lt;span class=&quot;tok-comment&quot;&gt;-- Repeat until it updates 0 rows. Each run is its own short transaction.&lt;/span&gt;
&lt;span class=&quot;tok-key&quot;&gt;UPDATE&lt;/span&gt; orders &lt;span class=&quot;tok-key&quot;&gt;SET&lt;/span&gt; public_id = &lt;span class=&quot;tok-key&quot;&gt;DEFAULT&lt;/span&gt;
&lt;span class=&quot;tok-key&quot;&gt;WHERE&lt;/span&gt; ctid = &lt;span class=&quot;tok-key&quot;&gt;ANY&lt;/span&gt; (&lt;span class=&quot;tok-key&quot;&gt;ARRAY&lt;/span&gt;(
  &lt;span class=&quot;tok-key&quot;&gt;SELECT&lt;/span&gt; ctid &lt;span class=&quot;tok-key&quot;&gt;FROM&lt;/span&gt; orders &lt;span class=&quot;tok-key&quot;&gt;WHERE&lt;/span&gt; public_id &lt;span class=&quot;tok-key&quot;&gt;IS&lt;/span&gt; &lt;span class=&quot;tok-literal&quot;&gt;NULL&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;tok-number&quot;&gt;10000&lt;/span&gt;
));&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The first two statements are instant. New rows get a value from the default. The &lt;code&gt;UPDATE&lt;/code&gt; fills the old rows a batch at a time, under &lt;code&gt;ROW EXCLUSIVE&lt;/code&gt;, which doesn&amp;#39;t block reads or other writes to other rows. If the column must end up &lt;code&gt;NOT NULL&lt;/code&gt;, add that last, the safe way below.&lt;/p&gt;
&lt;h2 id=&quot;setting-not-null&quot;&gt;Setting NOT NULL&lt;a class=&quot;guide-anchor&quot; href=&quot;#setting-not-null&quot; aria-label=&quot;Link to this section&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#hash&quot; /&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;ALTER COLUMN … SET NOT NULL&lt;/code&gt; scans the whole table under &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; to prove there are no nulls. From 12, the scan is skipped when a valid &lt;code&gt;CHECK&lt;/code&gt; constraint already proves it. So the safer form builds that proof under a lighter lock first:&lt;/p&gt;
&lt;div class=&quot;guide-code-block&quot;&gt;
&lt;div class=&quot;guide-code-tools&quot;&gt;&lt;span class=&quot;guide-code-lang&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#file-sql&quot; /&gt;&lt;/svg&gt;SQL&lt;/span&gt;&lt;button class=&quot;guide-code-copy&quot; type=&quot;button&quot; data-code-copy hidden&gt;&lt;svg class=&quot;guide-icon guide-code-copy-idle&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#copy&quot; /&gt;&lt;/svg&gt;&lt;svg class=&quot;guide-icon guide-code-copy-done&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#check&quot; /&gt;&lt;/svg&gt;&lt;span data-code-copy-label aria-live=&quot;polite&quot;&gt;Copy&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;
&lt;pre class=&quot;guide-code&quot; data-lang=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;tok-key&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;TABLE&lt;/span&gt; orders &lt;span class=&quot;tok-key&quot;&gt;ADD&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;CONSTRAINT&lt;/span&gt; orders_channel_nn_check
  &lt;span class=&quot;tok-key&quot;&gt;CHECK&lt;/span&gt; (channel &lt;span class=&quot;tok-key&quot;&gt;IS&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;tok-literal&quot;&gt;NULL&lt;/span&gt;) &lt;span class=&quot;tok-key&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;VALID&lt;/span&gt;;            &lt;span class=&quot;tok-comment&quot;&gt;-- instant&lt;/span&gt;
&lt;span class=&quot;tok-key&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;TABLE&lt;/span&gt; orders &lt;span class=&quot;tok-key&quot;&gt;VALIDATE&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;CONSTRAINT&lt;/span&gt; orders_channel_nn_check;  &lt;span class=&quot;tok-comment&quot;&gt;-- scan, SHARE UPDATE EXCLUSIVE&lt;/span&gt;
&lt;span class=&quot;tok-key&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;TABLE&lt;/span&gt; orders &lt;span class=&quot;tok-key&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;COLUMN&lt;/span&gt; channel &lt;span class=&quot;tok-key&quot;&gt;SET&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;tok-literal&quot;&gt;NULL&lt;/span&gt;;           &lt;span class=&quot;tok-comment&quot;&gt;-- instant, the check proves it&lt;/span&gt;
&lt;span class=&quot;tok-key&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;TABLE&lt;/span&gt; orders &lt;span class=&quot;tok-key&quot;&gt;DROP&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;CONSTRAINT&lt;/span&gt; orders_channel_nn_check;     &lt;span class=&quot;tok-comment&quot;&gt;-- instant&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;code&gt;VALIDATE CONSTRAINT&lt;/code&gt; takes &lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt;, which blocks neither reads nor writes. Run each statement in its own transaction, or the &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; from the first is held through the scan.&lt;/p&gt;
&lt;p&gt;From 18, a not-null constraint can itself be added &lt;code&gt;NOT VALID&lt;/code&gt; and validated later: &lt;code&gt;ALTER TABLE orders ADD CONSTRAINT orders_channel_nn NOT NULL channel NOT VALID&lt;/code&gt;, then &lt;code&gt;VALIDATE CONSTRAINT orders_channel_nn&lt;/code&gt;. The &lt;code&gt;CHECK&lt;/code&gt; route still works on 18 and is the one that works back to 12.&lt;/p&gt;
&lt;h2 id=&quot;constraints-and-foreign-keys&quot;&gt;Constraints and foreign keys&lt;a class=&quot;guide-anchor&quot; href=&quot;#constraints-and-foreign-keys&quot; aria-label=&quot;Link to this section&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#hash&quot; /&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Adding a &lt;code&gt;CHECK&lt;/code&gt; takes &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; and scans. Adding a foreign key takes &lt;code&gt;SHARE ROW EXCLUSIVE&lt;/code&gt; on both tables, the one you alter and the one it references, and scans the referencing table. &lt;code&gt;SHARE ROW EXCLUSIVE&lt;/code&gt; lets reads through but blocks writes, so a foreign key from &lt;code&gt;orders&lt;/code&gt; to &lt;code&gt;users&lt;/code&gt; stops writes to both while it checks every order.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;NOT VALID&lt;/code&gt; skips the scan. The constraint is enforced for new and changed rows from that moment, and the old rows are checked later by &lt;code&gt;VALIDATE CONSTRAINT&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;guide-code-block&quot;&gt;
&lt;div class=&quot;guide-code-tools&quot;&gt;&lt;span class=&quot;guide-code-lang&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#file-sql&quot; /&gt;&lt;/svg&gt;SQL&lt;/span&gt;&lt;button class=&quot;guide-code-copy&quot; type=&quot;button&quot; data-code-copy hidden&gt;&lt;svg class=&quot;guide-icon guide-code-copy-idle&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#copy&quot; /&gt;&lt;/svg&gt;&lt;svg class=&quot;guide-icon guide-code-copy-done&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#check&quot; /&gt;&lt;/svg&gt;&lt;span data-code-copy-label aria-live=&quot;polite&quot;&gt;Copy&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;
&lt;pre class=&quot;guide-code&quot; data-lang=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;tok-key&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;TABLE&lt;/span&gt; orders &lt;span class=&quot;tok-key&quot;&gt;ADD&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;CONSTRAINT&lt;/span&gt; orders_user_id_fkey
  &lt;span class=&quot;tok-key&quot;&gt;FOREIGN&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;KEY&lt;/span&gt; (user_id) &lt;span class=&quot;tok-key&quot;&gt;REFERENCES&lt;/span&gt; users (id) &lt;span class=&quot;tok-key&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;VALID&lt;/span&gt;;
&lt;span class=&quot;tok-key&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;TABLE&lt;/span&gt; orders &lt;span class=&quot;tok-key&quot;&gt;VALIDATE&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;CONSTRAINT&lt;/span&gt; orders_user_id_fkey;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The first statement still takes &lt;code&gt;SHARE ROW EXCLUSIVE&lt;/code&gt; on both tables, briefly. The second takes &lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt; on &lt;code&gt;orders&lt;/code&gt; and &lt;code&gt;ROW SHARE&lt;/code&gt; on &lt;code&gt;users&lt;/code&gt;, and neither blocks reads or writes. &lt;code&gt;NOT VALID&lt;/code&gt; works for foreign keys, &lt;code&gt;CHECK&lt;/code&gt; and, from 18, not-null constraints. A &lt;code&gt;NOT VALID&lt;/code&gt; foreign key on a partitioned table needs 18.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;VALIDATE CONSTRAINT&lt;/code&gt; fails if an old row breaks the constraint. Find the rows first if you are not sure: for the foreign key above, the orders whose &lt;code&gt;user_id&lt;/code&gt; has no match in &lt;code&gt;users&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;indexes&quot;&gt;Indexes&lt;a class=&quot;guide-anchor&quot; href=&quot;#indexes&quot; aria-label=&quot;Link to this section&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#hash&quot; /&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;CREATE INDEX&lt;/code&gt; takes &lt;code&gt;SHARE&lt;/code&gt;. Reads continue, and every write to the table waits until the build ends. &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt; takes &lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt;, so writes continue. In return it scans the table twice and waits for transactions that could use the index to finish, so it takes longer.&lt;/p&gt;
&lt;div class=&quot;guide-code-block&quot;&gt;
&lt;div class=&quot;guide-code-tools&quot;&gt;&lt;span class=&quot;guide-code-lang&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#file-sql&quot; /&gt;&lt;/svg&gt;SQL&lt;/span&gt;&lt;button class=&quot;guide-code-copy&quot; type=&quot;button&quot; data-code-copy hidden&gt;&lt;svg class=&quot;guide-icon guide-code-copy-idle&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#copy&quot; /&gt;&lt;/svg&gt;&lt;svg class=&quot;guide-icon guide-code-copy-done&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#check&quot; /&gt;&lt;/svg&gt;&lt;span data-code-copy-label aria-live=&quot;polite&quot;&gt;Copy&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;
&lt;pre class=&quot;guide-code&quot; data-lang=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;tok-key&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;INDEX&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;CONCURRENTLY&lt;/span&gt; orders_channel_idx &lt;span class=&quot;tok-key&quot;&gt;ON&lt;/span&gt; orders (channel);&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;What to know about the concurrent form, from the &lt;a href=&quot;https://www.postgresql.org/docs/current/sql-createindex.html&quot; rel=&quot;noopener&quot;&gt;CREATE INDEX page&lt;svg class=&quot;guide-icon guide-external&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#arrow-up-right&quot; /&gt;&lt;/svg&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It can&amp;#39;t run inside a transaction block. A migration tool that wraps each file in a transaction needs to be told not to for this one.&lt;/li&gt;
&lt;li&gt;If it fails, for example on a deadlock or a duplicate in a unique index, it leaves an invalid index behind. That index is ignored by queries and still costs every write. Drop it and build again. &lt;code&gt;SELECT indexrelid::regclass FROM pg_index WHERE NOT indisvalid&lt;/code&gt; finds them.&lt;/li&gt;
&lt;li&gt;It isn&amp;#39;t supported on a partitioned table. Build the index concurrently on each partition, then create the parent&amp;#39;s index without &lt;code&gt;CONCURRENTLY&lt;/code&gt;, which is then a catalog change.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;DROP INDEX&lt;/code&gt; takes &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; on the table. &lt;code&gt;DROP INDEX CONCURRENTLY&lt;/code&gt; waits for conflicting transactions instead, takes one index at a time, has no &lt;code&gt;CASCADE&lt;/code&gt;, and can&amp;#39;t drop an index behind a &lt;code&gt;UNIQUE&lt;/code&gt; or &lt;code&gt;PRIMARY KEY&lt;/code&gt; constraint. It doesn&amp;#39;t work on a partitioned table&amp;#39;s index either.&lt;/p&gt;
&lt;h3 id=&quot;primary-keys-and-unique-constraints&quot;&gt;Primary keys and unique constraints&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;ADD PRIMARY KEY&lt;/code&gt; and &lt;code&gt;ADD UNIQUE&lt;/code&gt; build their index under &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;. Build the index concurrently first, then turn it into the constraint:&lt;/p&gt;
&lt;div class=&quot;guide-code-block&quot;&gt;
&lt;div class=&quot;guide-code-tools&quot;&gt;&lt;span class=&quot;guide-code-lang&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#file-sql&quot; /&gt;&lt;/svg&gt;SQL&lt;/span&gt;&lt;button class=&quot;guide-code-copy&quot; type=&quot;button&quot; data-code-copy hidden&gt;&lt;svg class=&quot;guide-icon guide-code-copy-idle&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#copy&quot; /&gt;&lt;/svg&gt;&lt;svg class=&quot;guide-icon guide-code-copy-done&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#check&quot; /&gt;&lt;/svg&gt;&lt;span data-code-copy-label aria-live=&quot;polite&quot;&gt;Copy&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;
&lt;pre class=&quot;guide-code&quot; data-lang=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;tok-key&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;UNIQUE&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;INDEX&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;CONCURRENTLY&lt;/span&gt; orders_public_id_key &lt;span class=&quot;tok-key&quot;&gt;ON&lt;/span&gt; orders (public_id);
&lt;span class=&quot;tok-key&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;TABLE&lt;/span&gt; orders &lt;span class=&quot;tok-key&quot;&gt;ADD&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;CONSTRAINT&lt;/span&gt; orders_public_id_key &lt;span class=&quot;tok-key&quot;&gt;UNIQUE&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;USING&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;INDEX&lt;/span&gt; orders_public_id_key;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The second statement is instant for &lt;code&gt;UNIQUE&lt;/code&gt;. For &lt;code&gt;PRIMARY KEY&lt;/code&gt;, it also runs &lt;code&gt;SET NOT NULL&lt;/code&gt; on any key column that isn&amp;#39;t already &lt;code&gt;NOT NULL&lt;/code&gt;, and that scans the table under &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;. Make the columns &lt;code&gt;NOT NULL&lt;/code&gt; first with the &lt;code&gt;CHECK&lt;/code&gt; route above.&lt;/p&gt;
&lt;h3 id=&quot;reindex&quot;&gt;REINDEX&lt;/h3&gt;
&lt;p&gt;The &lt;a href=&quot;https://www.postgresql.org/docs/current/sql-reindex.html&quot; rel=&quot;noopener&quot;&gt;REINDEX page&lt;svg class=&quot;guide-icon guide-external&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#arrow-up-right&quot; /&gt;&lt;/svg&gt;&lt;/a&gt; says plain &lt;code&gt;REINDEX&lt;/code&gt; &amp;quot;locks out writes but not reads&amp;quot; of the table, and takes &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; on the index it rebuilds. Then it adds that the planner takes &lt;code&gt;ACCESS SHARE&lt;/code&gt; on every index of a table for any query, &amp;quot;and so REINDEX blocks virtually any queries&amp;quot;. Treat it as blocking reads. &lt;code&gt;REINDEX … CONCURRENTLY&lt;/code&gt;, from 12, takes &lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt; on the table and the indexes. It can&amp;#39;t run in a transaction block either, and a failed run leaves an invalid index named with &lt;code&gt;_ccnew&lt;/code&gt;, which you drop.&lt;/p&gt;
&lt;h2 id=&quot;rewrites-with-no-online-form&quot;&gt;Rewrites with no online form&lt;a class=&quot;guide-anchor&quot; href=&quot;#rewrites-with-no-online-form&quot; aria-label=&quot;Link to this section&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#hash&quot; /&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;ALTER COLUMN … TYPE&lt;/code&gt; rewrites the table and its indexes unless the old type is binary coercible to the new one and &lt;code&gt;USING&lt;/code&gt; doesn&amp;#39;t change the values. &lt;code&gt;varchar&lt;/code&gt; to &lt;code&gt;text&lt;/code&gt; is the common case that skips the rewrite. Indexes are rebuilt unless PostgreSQL can tell they would come out the same. There is no concurrent form. A new column, a batched backfill and a switch in the application move the work out of the lock, at the cost of more steps.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;VACUUM FULL&lt;/code&gt; and &lt;code&gt;CLUSTER&lt;/code&gt; rewrite the table under &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; and need room for a second copy of it. Plain &lt;code&gt;VACUUM&lt;/code&gt; takes &lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt; and runs beside reads and writes, but it keeps the freed space inside the table for reuse rather than returning it to the operating system. Its last step, cutting empty pages off the end of the table, needs &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;, and &lt;code&gt;VACUUM (TRUNCATE false)&lt;/code&gt; skips it.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;TRUNCATE&lt;/code&gt; is instant and takes &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;. A &lt;code&gt;DELETE&lt;/code&gt; in batches takes &lt;code&gt;ROW EXCLUSIVE&lt;/code&gt; instead, and leaves dead rows for &lt;code&gt;VACUUM&lt;/code&gt; to clean.&lt;/p&gt;
&lt;h2 id=&quot;partitions&quot;&gt;Partitions&lt;a class=&quot;guide-anchor&quot; href=&quot;#partitions&quot; aria-label=&quot;Link to this section&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#hash&quot; /&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;ATTACH PARTITION&lt;/code&gt; takes &lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt; on the parent from 12, so queries on the partitioned table keep running. It takes &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; on the table being attached, and on the default partition if there is one. It scans the new partition to check its rows fit the bounds, unless a valid &lt;code&gt;CHECK&lt;/code&gt; constraint already proves it:&lt;/p&gt;
&lt;div class=&quot;guide-code-block&quot;&gt;
&lt;div class=&quot;guide-code-tools&quot;&gt;&lt;span class=&quot;guide-code-lang&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#file-sql&quot; /&gt;&lt;/svg&gt;SQL&lt;/span&gt;&lt;button class=&quot;guide-code-copy&quot; type=&quot;button&quot; data-code-copy hidden&gt;&lt;svg class=&quot;guide-icon guide-code-copy-idle&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#copy&quot; /&gt;&lt;/svg&gt;&lt;svg class=&quot;guide-icon guide-code-copy-done&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#check&quot; /&gt;&lt;/svg&gt;&lt;span data-code-copy-label aria-live=&quot;polite&quot;&gt;Copy&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;
&lt;pre class=&quot;guide-code&quot; data-lang=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;tok-key&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;TABLE&lt;/span&gt; orders_2026_12 &lt;span class=&quot;tok-key&quot;&gt;ADD&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;CONSTRAINT&lt;/span&gt; orders_2026_12_bounds
  &lt;span class=&quot;tok-key&quot;&gt;CHECK&lt;/span&gt; (created_at &amp;gt;= &lt;span class=&quot;tok-string&quot;&gt;&amp;#39;2026-12-01&amp;#39;&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;AND&lt;/span&gt; created_at &amp;lt; &lt;span class=&quot;tok-string&quot;&gt;&amp;#39;2027-01-01&amp;#39;&lt;/span&gt;);
&lt;span class=&quot;tok-key&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;TABLE&lt;/span&gt; orders &lt;span class=&quot;tok-key&quot;&gt;ATTACH&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;PARTITION&lt;/span&gt; orders_2026_12
  &lt;span class=&quot;tok-key&quot;&gt;FOR&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;VALUES&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;FROM&lt;/span&gt; (&lt;span class=&quot;tok-string&quot;&gt;&amp;#39;2026-12-01&amp;#39;&lt;/span&gt;) &lt;span class=&quot;tok-key&quot;&gt;TO&lt;/span&gt; (&lt;span class=&quot;tok-string&quot;&gt;&amp;#39;2027-01-01&amp;#39;&lt;/span&gt;);
&lt;span class=&quot;tok-key&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;TABLE&lt;/span&gt; orders_2026_12 &lt;span class=&quot;tok-key&quot;&gt;DROP&lt;/span&gt; &lt;span class=&quot;tok-key&quot;&gt;CONSTRAINT&lt;/span&gt; orders_2026_12_bounds;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Plain &lt;code&gt;DETACH PARTITION&lt;/code&gt; takes &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; on the parent, so every query on the partitioned table waits. It also takes &lt;code&gt;SHARE&lt;/code&gt; on tables with foreign keys to the partitioned table. &lt;code&gt;DETACH PARTITION … CONCURRENTLY&lt;/code&gt;, from 14, uses two transactions: &lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt; on both tables first, then a wait for every transaction using the partitioned table, then &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; on the partition alone. It can&amp;#39;t run in a transaction block, and it isn&amp;#39;t allowed when the partitioned table has a default partition. If it is interrupted, &lt;code&gt;DETACH PARTITION … FINALIZE&lt;/code&gt; completes it.&lt;/p&gt;
&lt;h2 id=&quot;what-changed-by-version&quot;&gt;What changed, by version&lt;a class=&quot;guide-anchor&quot; href=&quot;#what-changed-by-version&quot; aria-label=&quot;Link to this section&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#hash&quot; /&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;guide-table&quot;&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Version&lt;/th&gt;
&lt;th&gt;Change&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;&lt;tr&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ADD COLUMN&lt;/code&gt; with a non-volatile default stops rewriting the table&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;&lt;code&gt;REINDEX CONCURRENTLY&lt;/code&gt;. &lt;code&gt;SET NOT NULL&lt;/code&gt; skips its scan when a valid &lt;code&gt;CHECK&lt;/code&gt; proves it. &lt;code&gt;ATTACH PARTITION&lt;/code&gt; takes a lighter lock on the parent. &lt;code&gt;ALTER INDEX … RENAME&lt;/code&gt; takes &lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DETACH PARTITION … CONCURRENTLY&lt;/code&gt; and &lt;code&gt;FINALIZE&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;18&lt;/td&gt;
&lt;td&gt;Not-null constraints can be &lt;code&gt;NOT VALID&lt;/code&gt;. &lt;code&gt;NOT VALID&lt;/code&gt; foreign keys on partitioned tables. Generated columns are virtual by default and never rewrite. &lt;code&gt;CHECK&lt;/code&gt; and foreign keys can be &lt;code&gt;NOT ENFORCED&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;The lock modes and the conflict table have not changed in that time.&lt;/p&gt;
&lt;section class=&quot;guide-caveats&quot;&gt;&lt;h2 id=&quot;caveats&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#warning&quot; /&gt;&lt;/svg&gt;Caveats&lt;a class=&quot;guide-anchor&quot; href=&quot;#caveats&quot; aria-label=&quot;Link to this section&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#hash&quot; /&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Everything here is from the PostgreSQL 18 documentation, with the version notes from the release notes for 11, 12, 14 and 18. Check the page for your own major version when it matters.&lt;/li&gt;
&lt;li&gt;Reads and writes in the table mean ordinary statements. &lt;code&gt;SELECT … FOR UPDATE&lt;/code&gt; and its relatives take &lt;code&gt;ROW SHARE&lt;/code&gt;, which &lt;code&gt;EXCLUSIVE&lt;/code&gt; and &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; block.&lt;/li&gt;
&lt;li&gt;Row locks are a separate system. A long &lt;code&gt;UPDATE&lt;/code&gt; holds row locks that block other writes to those rows under any table lock.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt; waits for older transactions through lock waits, so a &lt;code&gt;lock_timeout&lt;/code&gt; can cancel it and leave an invalid index. Leave it unset for the concurrent forms, or be ready to drop and rebuild.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ALTER TABLE&lt;/code&gt; recurses into partitions and inheritance children unless you write &lt;code&gt;ONLY&lt;/code&gt;, and it locks each of them.&lt;/li&gt;
&lt;/ul&gt;
&lt;/section&gt;
&lt;section class=&quot;blog-faq&quot; aria-labelledby=&quot;faq&quot;&gt;&lt;h2 id=&quot;faq&quot;&gt;Questions&lt;a class=&quot;guide-anchor&quot; href=&quot;#faq&quot; aria-label=&quot;Link to this section&quot;&gt;&lt;svg class=&quot;guide-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/guide-icons.svg#hash&quot; /&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;blog-faq-item&quot;&gt;
&lt;h3 id=&quot;does-alter-table-add-column-lock-the-table&quot;&gt;Does ALTER TABLE ADD COLUMN lock the table?&lt;/h3&gt;
&lt;p&gt;Yes. It takes &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt;, which blocks reads and writes. From PostgreSQL 11, a column with no default or a non-volatile default is a catalog change that holds the lock for milliseconds. A volatile default, an identity column or a stored generated column rewrites the table under the lock. The danger in both cases is the wait for the lock, so set &lt;code&gt;lock_timeout&lt;/code&gt; first.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;blog-faq-item&quot;&gt;
&lt;h3 id=&quot;does-create-index-block-reads-in-postgresql&quot;&gt;Does CREATE INDEX block reads in PostgreSQL?&lt;/h3&gt;
&lt;p&gt;No. &lt;code&gt;CREATE INDEX&lt;/code&gt; takes &lt;code&gt;SHARE&lt;/code&gt;, which allows reads and blocks inserts, updates and deletes until the build ends. &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt; takes &lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt; and blocks neither, but it can&amp;#39;t run in a transaction block and takes longer.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;blog-faq-item&quot;&gt;
&lt;h3 id=&quot;does-adding-a-foreign-key-lock-both-tables&quot;&gt;Does adding a foreign key lock both tables?&lt;/h3&gt;
&lt;p&gt;Yes. &lt;code&gt;ADD FOREIGN KEY&lt;/code&gt; takes &lt;code&gt;SHARE ROW EXCLUSIVE&lt;/code&gt; on the table you alter and on the table it references, which blocks writes to both while the existing rows are checked. Add it &lt;code&gt;NOT VALID&lt;/code&gt;, then run &lt;code&gt;VALIDATE CONSTRAINT&lt;/code&gt;, which takes &lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt; and &lt;code&gt;ROW SHARE&lt;/code&gt; and blocks neither reads nor writes.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;blog-faq-item&quot;&gt;
&lt;h3 id=&quot;why-did-a-fast-alter-table-block-every-query&quot;&gt;Why did a fast ALTER TABLE block every query?&lt;/h3&gt;
&lt;p&gt;It waited for its lock behind a long transaction, and every query after it queued behind the &lt;code&gt;ALTER&lt;/code&gt;. &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; conflicts with every other lock, so even a plain &lt;code&gt;SELECT&lt;/code&gt; waits behind a waiting &lt;code&gt;ALTER&lt;/code&gt;. Set &lt;code&gt;lock_timeout&lt;/code&gt; so the statement gives up instead, and look for sessions &lt;code&gt;idle in transaction&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;blog-faq-item&quot;&gt;
&lt;h3 id=&quot;how-do-i-see-which-query-is-blocking-my-alter-table&quot;&gt;How do I see which query is blocking my ALTER TABLE?&lt;/h3&gt;
&lt;p&gt;Join &lt;code&gt;pg_locks&lt;/code&gt; to &lt;code&gt;pg_stat_activity&lt;/code&gt; on &lt;code&gt;pid&lt;/code&gt; for the table, and call &lt;code&gt;pg_blocking_pids(pid)&lt;/code&gt; for the waiting session. It returns the sessions holding a conflicting lock and the ones queued ahead of it. The query is under &amp;quot;Who holds the lock, and who waits&amp;quot; above.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;blog-faq-item&quot;&gt;
&lt;h3 id=&quot;does-vacuum-full-lock-the-table&quot;&gt;Does VACUUM FULL lock the table?&lt;/h3&gt;
&lt;p&gt;Yes. &lt;code&gt;VACUUM FULL&lt;/code&gt; rewrites the table under &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; and blocks reads and writes until it finishes. Plain &lt;code&gt;VACUUM&lt;/code&gt; takes &lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt; and runs alongside normal traffic, but it does not return space to the operating system in most cases.&lt;/p&gt;
&lt;/div&gt;
&lt;/section&gt;
</content>
  </entry>
</feed>
