<story><title>Explaining the Postgres iceberg</title><url>https://avestura.dev/blog/explaining-the-postgres-meme</url></story><parent_chain></parent_chain><comment><author>phamilton</author><text>We got hit with a good bit of the postgres iceberg this week.&lt;p&gt;VACUUM by default runs with the TRUNCATE option. This will, if possible and valuable, truncate the heap and return those pages to the operating system. This operation takes an AccessExclusive lock, which blocks reads and writes. It is written to be very deferential and only runs if nothing else is trying to take a competing lock.&lt;p&gt;AccessExclusive is a unique lock in that it is replicated to standbys. When the writer holds the lock, reads on the standbys will be blocked. However, the writer doesn&amp;#x27;t know about them, so its conservative approach of only acquiring the lock if there&amp;#x27;s no contention is completely thwarted.&lt;p&gt;Finally, if vacuum truncate is interrupted by lock contention it must repeat a scan of the heap to ensure the blocks are still empty before proceeding with the truncation.&lt;p&gt;All of these details combined meant our writer got stuck in a &amp;quot;grab lock, scan, be interrupted, release lock, grab lock, restart scan, be interrupted, release lock... etc&amp;quot; loop. The replication of this lock to the reader seemed to have batched together, so the readers never got to run in between the loop iterations. The vacuum never actually succeeded because it kept re-scanning. We had to intervene manually to disable autovacuum and cancel the in-progress jobs.&lt;p&gt;We&amp;#x27;ve hit plenty of weird postgres issues, but this was a new one for us.&lt;p&gt;Edit: of note is the somewhat rare lack of clarity in the postgres docs. &amp;quot;VACUUM (without FULL)&amp;quot; is documented as only requiring ShareUpdateExclusive, which is why autovacuum is considered safe to run. Turns out that&amp;#x27;s not true.</text></comment>
<story><title>Explaining the Postgres iceberg</title><url>https://avestura.dev/blog/explaining-the-postgres-meme</url></story><parent_chain></parent_chain><comment><author>slotrans</author><text>Heh, this is pretty great. Is it good or bad that I know almost all of these?&lt;p&gt;Not sure &amp;quot;SERIAL is non-transactional&amp;quot; belongs at the bottom, I thought that was common knowledge... but that might be from years doing Oracle where sequences are more explicit.</text></comment>