Two issues leading to discrepancies in FSM data on the standby server
Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.
This thread has been committed, so CI has stopped here. Anything below is the last result it produced.
You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:
docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t53547psql -h localhost -U postgresBuilt from patchset v19 (message #19), September 15, 2026 at 01:08 PM.
Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:
git clone --branch t53547_19 https://github.com/hackorum-dev/postgres.gitIn a checkout you already have, add the fork once:
git remote add hackorum https://github.com/hackorum-dev/postgres.gitthen, for this patchset and every later one:
git fetch hackorum t53547_19 && git checkout t53547_19Patchset v19 (message #19) is on t53547_19
We’ve recently observed a situation with significant increase in
response time for insert operations after switching to a replica server.
The collected information pointed to the discrepancy in the FSM data on
the replica side, which became visible to the insert sessions once
autovacuum process pulled incorrect data from from leaf blocks into FSM
root. The entire situation was looking like the case discussed in
/messages/by-id/20180802172857.5skoexsilnjvgruk@alvherre.pgsql and
which was supposed to be fixed by ‘ab7dbd681’ (which introduced FSM
update during 'heap_xlog_visible' invocation). However in our case and
synthetic tests we were able to see data blocks marked as ‘all visible’,
but still having incorrect FSM records.
After analyzing the code I’ve noticed that during recovery FSM data is
updated in XLogRecordPageWithFreeSpace, which uses MarkBufferDirtyHint
to mark FSM block as modified. However, if data checksums are enabled,
then this call does nothing during recovery and is actually a no-op –
basically it just exits immediately without marking block as dirty. The
logic here is that as no new WAL data could not be generated during the
recovery, so changes to hints in block should not mark block as dirty to
avoid risk of torn pages being written. This seems logical, but it seems
not aligned well with the FSM case, as its blocks could be just zeroed
if checksum mismatch is detected. Currently changes to a FSM block could
be lost if each change to the particular FSM block occur rarely enough
to allow its eviction from the cache. To persist the change the
modification need to be performed while FSM block is still kept in
buffers and marked as dirty after receiving its FPI. If block was
already cleaned, then the change won’t be persisted and stored FSM
blocks may remain in an obsolete state. In our case the table had its
'fillfactor' parameter set below 80, so during insert bursts each FSM
block on replica side was modified only during first access of FSM block
since checkpoint (with FPI) and then by processing XLOG_HEAP2_VISIBLE
record for data once it was marked as ‘all visible’. This gives plenty
of time to cleanup buffer between these moments, so the second change
was just never written to the disk. So, large number of blocks were left
with incorrect data in FSM leaf blocks, which caused problem after
switchover.
Given that FSM is ready to handle torn page writes and
XLogRecordPageWithFreeSpace is called only during the recovery there
seems to be no reason to use MarkBufferDirtyHint here instead of a
regular MarkBufferDirty call. The code is already trying to limit
updates to the FSM (i.e. by updating it only after reaching 80% of used
space for regular DML), so we probably want to ensure that these updates
are actually persisted.
The second noticed issue (not related to our observed problem) is
related to the ‘heap_xlog_visible’ – this function uses
‘PageGetFreeSpace’ call instead of ‘PageGetHeapFreeSpace’ to get size of
free space for regular heap blocks. This seems like a bug, as method
'PageGetHeapFreeSpace' is used for any other case where we need to get
free space for a heap page. Usage of incorrect function could also cause
incorrect data being written to the FSM on replica: if block still have
free space, but already reached MaxHeapTuplesPerPage limit, then it
should be marked as unavailable for new rows in FSM, otherwise inserter
will need to check and update its FSM data as well.
Attached are separate patches, which tries to fixes both these problems
– calling ‘MarkBufferDirty’ instead of ‘MarkBufferDirtyHint’ in the
first case and replacing ‘PageGetFreeSpace’ with ‘PageGetHeapFreeSpace’
in the second case.
Two synthetic test cases are also attached which simulates both these
situations – ‘test_case1.zip’ to simulate the problem with lost FSM
update on replica side and ‘test_case2.zip’ to simulate incorrect FSM
data on standby server for blocks with large number of redirect slots.
In both cases the ‘test_prepare.sh’ script could be edited to specify
path to PG installation and port numbers. Then invoke ‘test_preapre.sh’
script to prepare two databases. For first case the second script
‘test_run.sh’ need to be invoked after that to show large number of
blocks being visited for simple insert and for second test case state of
the FSM (for single block) is just displayed at the end of
‘test_prepare.sh’.
Thanks,
Alexey
Attachments:
0001-Mark-modified-FSM-buffer-as-dirty-during-recovery.patchtext/x-patch; charset=UTF-8; name=0001-Mark-modified-FSM-buffer-as-dirty-during-recovery.patchDownload+1-2
0002-Use-PageGetHeapFreeSpace-in-heap_xlog_visible.patchtext/x-patch; charset=UTF-8; name=0002-Use-PageGetHeapFreeSpace-in-heap_xlog_visible.patchDownload+1-2
Hi!
Very interesting cases!
On 20 Mar 2026, at 06:32, Alexey Makhmutov <a.makhmutov@postgrespro.ru> wrote:
Attached are separate patches, which tries to fixes both these problems – calling ‘MarkBufferDirty’ instead of ‘MarkBufferDirtyHint’ in the first case and replacing ‘PageGetFreeSpace’ with ‘PageGetHeapFreeSpace’ in the second case.
Patch 0001 - MarkBufferDirty() instead of MarkBufferDirtyHint() in XLogRecordPageWithFreeSpace().
Yes, MarkBufferDirtyHint() is no-op in recovery and it's the only case I found of using MarkBufferDirtyHint()
in redo.
Originally in e981653 was used MarkBufferDirty() but 96ef3b8 flipped to MarkBufferDirtyHint().
Neither of these commits provided a comment on why this version was chosen. I think if we fix it we must comment things.
Patch 0002 - PageGetHeapFreeSpace instead of PageGetFreeSpace in heap_xlog_visible.
This seems to be just an oversight in ab7dbd6. Every other call to XLogRecordPageWithFreeSpace() uses PageGetHeapFreeSpace().
And this seems correct to me, but a bit odd. Why indexes do not update FSM via this routine?
It seems indexes do not log free pages at all, relying on index vacuum to rebuild fsm on Standby.
Nice catch!
Best regards, Andrey Borodin.
Hi Andrey!
Thank you for the attention to this patch!
Originally in e981653 was used MarkBufferDirty() but 96ef3b8 flipped to MarkBufferDirtyHint().
Neither of these commits provided a comment on why this version was chosen. I think if we fix it we must comment things.
I think that reason of change in 96ef3b8 (changing of 'MarkBufferDirty'
to 'MarkBufferDirtyHint') may be described in the next commit (9df56f6),
during the README update:
New WAL records cannot be written during recovery, so hint bits set
during recovery must not dirty the page if the buffer is not already
dirty, when checksums are enabled. Systems in Hot-Standby mode may
benefit from hint bits being set, but with checksums enabled, a page
cannot be dirtied after setting a hint bit (due to the torn page risk).
So, it must wait for full-page images containing the hint bit updates to
arrive from the master.
So, it seems logical, that any changes to the data not protected by the
WAL (which includes VM and FSM as well) should use MarkBufferDirtyHint,
which does not set dirty flag during recovery. However, as FSM blocks
could be just zeroed in case of checksums mismatch, so I think it's
perfectly fine to use regular MarkBufferDirty here.
I've updated the first patch by adding the comment with explanation of
the reason for using MarkBufferDirty instead of MarkBufferDirtyHint here.
As for the second issue and the patch - it seems to be resolved in the
current master by a881cc9, which removed the entire 'heap_xlog_visible'
method, as all-visibility information is now sent with the
XLOG_HEAP2_PRUNE_VACUUM_CLEANUP message and its handler already uses
PageGetHeapFreeSpace. The problem is still relevant for the pre-19
versions, so I will probably move it to the separate thread in bugs.
Thanks,
Alexey
Attachments:
0001-Mark-modified-FSM-buffer-as-dirty-during-recovery.patchtext/x-patch; charset=UTF-8; name=0001-Mark-modified-FSM-buffer-as-dirty-during-recovery.patchDownload+11-2
On Mon, Apr 6, 2026 at 10:26 AM Alexey Makhmutov
<a.makhmutov@postgrespro.ru> wrote:
Originally in e981653 was used MarkBufferDirty() but 96ef3b8 flipped to MarkBufferDirtyHint().
Neither of these commits provided a comment on why this version was chosen. I think if we fix it we must comment things.I think that reason of change in 96ef3b8 (changing of 'MarkBufferDirty'
to 'MarkBufferDirtyHint') may be described in the next commit (9df56f6),
during the README update:New WAL records cannot be written during recovery, so hint bits set
during recovery must not dirty the page if the buffer is not already
dirty, when checksums are enabled. Systems in Hot-Standby mode may
benefit from hint bits being set, but with checksums enabled, a page
cannot be dirtied after setting a hint bit (due to the torn page risk).
So, it must wait for full-page images containing the hint bit updates to
arrive from the master.So, it seems logical, that any changes to the data not protected by the
WAL (which includes VM and FSM as well) should use MarkBufferDirtyHint,
which does not set dirty flag during recovery. However, as FSM blocks
could be just zeroed in case of checksums mismatch, so I think it's
perfectly fine to use regular MarkBufferDirty here.
Yea, I agree that this seems like simply an oversight in 96ef3b8. And
it seems safe to use MarkBufferDirty() here instead.
- Melanie
On Tue, Apr 14, 2026 at 7:22 PM Melanie Plageman
<melanieplageman@gmail.com> wrote:
On Mon, Apr 6, 2026 at 10:26 AM Alexey Makhmutov
<a.makhmutov@postgrespro.ru> wrote:Originally in e981653 was used MarkBufferDirty() but 96ef3b8 flipped to MarkBufferDirtyHint().
Neither of these commits provided a comment on why this version was chosen. I think if we fix it we must comment things.I think that reason of change in 96ef3b8 (changing of 'MarkBufferDirty'
to 'MarkBufferDirtyHint') may be described in the next commit (9df56f6),
during the README update:New WAL records cannot be written during recovery, so hint bits set
during recovery must not dirty the page if the buffer is not already
dirty, when checksums are enabled. Systems in Hot-Standby mode may
benefit from hint bits being set, but with checksums enabled, a page
cannot be dirtied after setting a hint bit (due to the torn page risk).
So, it must wait for full-page images containing the hint bit updates to
arrive from the master.So, it seems logical, that any changes to the data not protected by the
WAL (which includes VM and FSM as well) should use MarkBufferDirtyHint,
which does not set dirty flag during recovery. However, as FSM blocks
could be just zeroed in case of checksums mismatch, so I think it's
perfectly fine to use regular MarkBufferDirty here.Yea, I agree that this seems like simply an oversight in 96ef3b8. And
it seems safe to use MarkBufferDirty() here instead.
I also think that usage of MarkBufferDirty() here is safe. If I
understood correctly.
1) When wal_log_hints = on, should be completely safe. Even if we
have torn page after the crash, during recovery FPI from the primary
should come first.
2) When wal_log_hints = off, we can end up with torn pages not covered
by FPI. Without checksums, FSM can tolerate torn pages. With
checksums, that would result in zeroed pages. FSM can tolerate that
as well. And the last shouldn't happen too frequently. So, we should
finally get way better FSM state than it is now.
Should we push it to all supported branches?
------
Regards,
Alexander Korotkov
Supabase
On Tue, Apr 21, 2026 at 9:49 AM Alexander Korotkov <aekorotkov@gmail.com> wrote:
On Tue, Apr 14, 2026 at 7:22 PM Melanie Plageman
Yea, I agree that this seems like simply an oversight in 96ef3b8. And
it seems safe to use MarkBufferDirty() here instead.I also think that usage of MarkBufferDirty() here is safe. If I
understood correctly.
1) When wal_log_hints = on, should be completely safe. Even if we
have torn page after the crash, during recovery FPI from the primary
should come first.
I think FPIs from primary don't really matter here, since we are only
talking about MarkBufferDirty() in XLogRecordPageWithFreespace(). If
we change it to MarkBufferDirty() on the standby and the machine
crashes mid-write leading to a checksum error, we'll just zero it out
-- which is really your point 2. While FPIs from the primary will
overwrite the standby's FSM page, they don't provide torn-page
protection for modifications made by the standby as you could read the
page between the torn write and replaying any FPI from the primary.
2) When wal_log_hints = off, we can end up with torn pages not covered
by FPI. Without checksums, FSM can tolerate torn pages. With
checksums, that would result in zeroed pages. FSM can tolerate that
as well. And the last shouldn't happen too frequently. So, we should
finally get way better FSM state than it is now.
Yes, I think the bottom line is that we can't get checksum errors
reading FSM pages because of ZERO_ON_ERROR, so there is no reason to
do MarkBufferDirtyHint() in recovery for FSM. It only leads to losing
changes to the page.
Should we push it to all supported branches?
I haven't looked at the code paths in previous versions, but as long
as they are reading FSM pages with RBM_ZERO_ON_ERROR, I think it is
safe to do so. It is a bug that is causing overly optimistic FSM
numbers, but it's not a correctness issue like wrong results/data
corruption etc. So, I think you could make an argument either way
about fixing it.
I don't know how much of Alexey's reported issue was this vs
PageGetFreeSpace() in heap_xlog_visible(). The MarkBufferDirty()
change is easy to fix, so it probably makes sense to do so. I haven't
investigated more about the PageGetFreeSpace() issue.
- Melanie
On Tue, Apr 21, 2026 at 5:42 PM Melanie Plageman
<melanieplageman@gmail.com> wrote:
On Tue, Apr 21, 2026 at 9:49 AM Alexander Korotkov <aekorotkov@gmail.com> wrote:
On Tue, Apr 14, 2026 at 7:22 PM Melanie Plageman
Yea, I agree that this seems like simply an oversight in 96ef3b8. And
it seems safe to use MarkBufferDirty() here instead.I also think that usage of MarkBufferDirty() here is safe. If I
understood correctly.
1) When wal_log_hints = on, should be completely safe. Even if we
have torn page after the crash, during recovery FPI from the primary
should come first.I think FPIs from primary don't really matter here, since we are only
talking about MarkBufferDirty() in XLogRecordPageWithFreespace(). If
we change it to MarkBufferDirty() on the standby and the machine
crashes mid-write leading to a checksum error, we'll just zero it out
-- which is really your point 2. While FPIs from the primary will
overwrite the standby's FSM page, they don't provide torn-page
protection for modifications made by the standby as you could read the
page between the torn write and replaying any FPI from the primary.
It's probably not so important in this context, but I'd like to verify
my thoughts further. My idea is that standby's changes of FSM are
mirroring primary's changes of FSM, even that FSM changes don't have
own WAL-records and being decoded from other WAL records. Thus, if
some FSM page on primary gets changed then primary emits FPI for the
first change after checkpoint. The standby restartpoints are
synchronized with primary's checkpoints, and the FSM changes mirrors
FSM changes on primary. Standby should also have its first change of
FSM page after the restartpoint covered by FPI received from primary.
So, the consistency of FSM pages should be guaranteed in the similar
way to every other WAL-logged pages, except FSM pages are not directly
WAL-logged, but got their changes decoded from main fork WAL-records.
The weak point I see in the reasoning above is the assumption that FSM
changes on standby fully mirrors FSM changes on primary. I didn't
really check this invariant. But other than that, could you please,
re-check this thoughts and let me know what do you think?
2) When wal_log_hints = off, we can end up with torn pages not covered
by FPI. Without checksums, FSM can tolerate torn pages. With
checksums, that would result in zeroed pages. FSM can tolerate that
as well. And the last shouldn't happen too frequently. So, we should
finally get way better FSM state than it is now.Yes, I think the bottom line is that we can't get checksum errors
reading FSM pages because of ZERO_ON_ERROR, so there is no reason to
do MarkBufferDirtyHint() in recovery for FSM. It only leads to losing
changes to the page.Should we push it to all supported branches?
I haven't looked at the code paths in previous versions, but as long
as they are reading FSM pages with RBM_ZERO_ON_ERROR, I think it is
safe to do so.
I've checked that since 96ef3b8 we only read FSM pages with RBM_ZERO_ON_ERROR.
It is a bug that is causing overly optimistic FSM
numbers, but it's not a correctness issue like wrong results/data
corruption etc. So, I think you could make an argument either way
about fixing it.
It has user-visible effect of increased insertion time after replica
promotion. I think this is quite a reason to backpatch.
I don't know how much of Alexey's reported issue was this vs
PageGetFreeSpace() in heap_xlog_visible(). The MarkBufferDirty()
change is easy to fix, so it probably makes sense to do so. I haven't
investigated more about the PageGetFreeSpace() issue.
Makes sense. I suggest Alexey could clarify this.
------
Regards,
Alexander Korotkov
Supabase
On Wed, Apr 22, 2026 at 8:32 AM Alexander Korotkov <aekorotkov@gmail.com> wrote:
On Tue, Apr 21, 2026 at 5:42 PM Melanie Plageman
<melanieplageman@gmail.com> wrote:I also think that usage of MarkBufferDirty() here is safe. If I
understood correctly.
1) When wal_log_hints = on, should be completely safe. Even if we
have torn page after the crash, during recovery FPI from the primary
should come first.I think FPIs from primary don't really matter here, since we are only
talking about MarkBufferDirty() in XLogRecordPageWithFreespace(). If
we change it to MarkBufferDirty() on the standby and the machine
crashes mid-write leading to a checksum error, we'll just zero it out
-- which is really your point 2. While FPIs from the primary will
overwrite the standby's FSM page, they don't provide torn-page
protection for modifications made by the standby as you could read the
page between the torn write and replaying any FPI from the primary.It's probably not so important in this context, but I'd like to verify
my thoughts further. My idea is that standby's changes of FSM are
mirroring primary's changes of FSM, even that FSM changes don't have
own WAL-records and being decoded from other WAL records. Thus, if
some FSM page on primary gets changed then primary emits FPI for the
first change after checkpoint. The standby restartpoints are
synchronized with primary's checkpoints, and the FSM changes mirrors
FSM changes on primary. Standby should also have its first change of
FSM page after the restartpoint covered by FPI received from primary.
So, the consistency of FSM pages should be guaranteed in the similar
way to every other WAL-logged pages, except FSM pages are not directly
WAL-logged, but got their changes decoded from main fork WAL-records.The weak point I see in the reasoning above is the assumption that FSM
changes on standby fully mirrors FSM changes on primary. I didn't
really check this invariant. But other than that, could you please,
re-check this thoughts and let me know what do you think?
Yea, I don't think they are really being mirrored. The conditions for
updating the FSM in normal operation vs in recovery are different.
When doing an INSERT, we can look for freespace on heap page 5, find
it doesn't have enough and then update the freespace map to reflect
that. Then, let's say we eventually find space for the INSERTed tuple
on heap page 10000. In normal operation, we update the FSM on
rejection, not when we find freespace. So we do not update the FSM for
heap page 10000 to reflect how much freespace it now has remaining.
Later, when replaying the INSERT, if the heuristic conditions are met
(action == BLK_NEEDS_REDO && freespace < BLCKSZ / 5) for freespace on
heap block 10000, we may then update the FSM for page 10000. The local
changes to the FSM page don't mark the buffer dirty and thus aren't
safe from loss if the page is evicted and definitely not safe from
being wiped out by a future FPI since they don't update the FSM page
LSN.
It is a bug that is causing overly optimistic FSM
numbers, but it's not a correctness issue like wrong results/data
corruption etc. So, I think you could make an argument either way
about fixing it.It has user-visible effect of increased insertion time after replica
promotion. I think this is quite a reason to backpatch.
Right, since it's not invasive, I'm fine with backpatching.
I don't know how much of Alexey's reported issue was this vs
PageGetFreeSpace() in heap_xlog_visible(). The MarkBufferDirty()
change is easy to fix, so it probably makes sense to do so. I haven't
investigated more about the PageGetFreeSpace() issue.Makes sense. I suggest Alexey could clarify this.
Would be good also if he could add a 19 open item for the
PageGetFreeSpace() thing (in the issues affecting back branches
section) or register a patch for next commitfest (as a bug), so we
don't forget about it.
- Melanie
Hi Alexander and Melanie,
Thank you very much for the attention to this thread!
On 4/22/26 17:15, Melanie Plageman wrote:
I don't know how much of Alexey's reported issue was this vs
PageGetFreeSpace() in heap_xlog_visible(). The MarkBufferDirty()
change is easy to fix, so it probably makes sense to do so. I haven't
investigated more about the PageGetFreeSpace() issue.
Makes sense. I suggest Alexey could clarify this.
The issue with 'PageGetFreeSpace' call is completely unrelated to the
other issue discussed in this thread (i.e. marking FSM buffer as dirty).
In my view it's just a small overlook in the ab7dbd6, which used
'PageGetFreeSpace' instead of 'PageGetHeapFreeSpace'. This latter
function calls ‘PageGetFreeSpace’ and additionally checks that there is
a space in line pointers section (i.e. checks that MaxHeapTuplesPerPage
is not exceeded). If MaxHeapTuplesPerfPage items are already created,
then free space for a heap block need to be reported as 0, as no new
tuple could be added to the page. Such case could be observed if page
has large number of redirect slots created as result of HOT cleanups.
Basically, every code working with heap pages should use
‘PageGetHeapFreeSpace’ rather than ‘PageGetFreeSpace’ and function
‘heap_xlog_visible’ is the only place in the code where
‘PageGetFreeSpace’ is used for heap pages. I think that situation with
difference between results of these two functions is rarely observed,
but it seems to be logical to just call the correct function in all cases.
This issue is present in all supported version (PG 14-18), but is not
present in current master for PG19 (as ‘heap_xlog_visible’ was removed
in a881cc9).
Would be good also if he could add a 19 open item for the
PageGetFreeSpace() thing (in the issues affecting back branches
section) or register a patch for next commitfest (as a bug), so we
don't forget about it.
Sure, I could just move this second patch (with test to reproduce the
problem) to a different thread and register a new commitfest entry for
it. Target version will be 14 (first supported version with the code).
As it cannot by applied on top of current master (as the problem was
already fixed for PG19 as result of a881cc9), so probably it will be
displayed in 'need rebase' status.
Thanks,
Alexey
On Wed, Apr 22, 2026 at 10:15 AM Melanie Plageman
<melanieplageman@gmail.com> wrote:
On Wed, Apr 22, 2026 at 8:32 AM Alexander Korotkov <aekorotkov@gmail.com> wrote:
It is a bug that is causing overly optimistic FSM
numbers, but it's not a correctness issue like wrong results/data
corruption etc. So, I think you could make an argument either way
about fixing it.It has user-visible effect of increased insertion time after replica
promotion. I think this is quite a reason to backpatch.Right, since it's not invasive, I'm fine with backpatching.
Alexander, are you planning on committing this?
- Melanie
On Fri, May 1, 2026 at 12:57 AM Melanie Plageman
<melanieplageman@gmail.com> wrote:
On Wed, Apr 22, 2026 at 10:15 AM Melanie Plageman
<melanieplageman@gmail.com> wrote:On Wed, Apr 22, 2026 at 8:32 AM Alexander Korotkov <aekorotkov@gmail.com> wrote:
It is a bug that is causing overly optimistic FSM
numbers, but it's not a correctness issue like wrong results/data
corruption etc. So, I think you could make an argument either way
about fixing it.It has user-visible effect of increased insertion time after replica
promotion. I think this is quite a reason to backpatch.Right, since it's not invasive, I'm fine with backpatching.
Alexander, are you planning on committing this?
Melanie, thank you for reminder. Yes, I'm planning to commit (with
backpatching) this in a couple of days.
------
Regards,
Alexander Korotkov
Supabase
On Tue, Apr 21, 2026 at 04:49:12PM +0300, Alexander Korotkov wrote:
I also think that usage of MarkBufferDirty() here is safe. If I
understood correctly.
1) When wal_log_hints = on, should be completely safe. Even if we
have torn page after the crash, during recovery FPI from the primary
should come first.
I think this change (commit c06d1a4) is incorrect. Assume checksums and
full-page writes are enabled, both defaults. Before this change, redo would
transition FSM pages clean->dirty only via XLogReadBufferForRedo() of an FPI.
At end of recovery, the FSM passed checksum validation. After $SUBJECT,
nothing stops the following sequence of events: finish restartpoint; dirty FSM
page via heap_xlog_*; crash tears write of that FSM page; resume recovery; end
recovery without curing the torn page. The FSM code's own use of
RBM_ZERO_ON_ERROR makes FSM code accept torn pages. Other callers assume
normal reads will succeed on FSM_FORKNUM, e.g., the
heapam_relation_copy_data() call to RelationCopyStorage(). Claude wrote a
test of that, attached.
To be fair, freespace/README itself incorrectly predicts the commit is okay,
by saying things like, "we rely on a bunch of self-correcting measures to
repair possible corruption." Long-term, we do have at least these
alternatives:
- Keep the pre-2026-05 invariant that FSM is free from torn pages at end of
recovery. (We'll likely still have the property that heap_xlog_* will read
torn FSM pages during recovery. That arises, I think, because the FPI that
fixes the torn page may be later in the WAL stream. FSM is unlike other
forks this way; other forks write before they read.)
- Require all readers of FSM_FORKNUM to use RBM_ZERO_ON_ERROR or the
equivalent.
Side excursion that I'm dumping here in case it saves someone time: I was
suspicious of the older MarkBufferDirty(), in
FreeSpaceMapPrepareTruncateRel(). Subtle interactions make it harmless. It
involves a WAL write via log_newpage_buffer(), so it almost follows the
transam/README protocol. The odd part is that the protocol-required actions
happen across two WAL records. The redo of XLOG_FPI comes first and dirties
the buffer properly. Then redo of XLOG_SMGR_TRUNCATE calls
FreeSpaceMapPrepareTruncateRel() and dirties it again. That would normally
not be okay. However, the primary side used DELAY_CHKPT_COMPLETE to prevent a
checkpoint or restartpoint between the two records. No problem behavior
arises.
Should we push it to all supported branches?
(The commit did get back-patched, even though its commit log doesn't say that.)
Attachments:
fsm-tear-test-v0.patchtext/plain; charset=us-asciiDownload+273-0
On Wed, Sep 2, 2026 at 12:18 AM Noah Misch <noah@leadboat.com> wrote:
On Tue, Apr 21, 2026 at 04:49:12PM +0300, Alexander Korotkov wrote:
I also think that usage of MarkBufferDirty() here is safe. If I
understood correctly.
1) When wal_log_hints = on, should be completely safe. Even if we
have torn page after the crash, during recovery FPI from the primary
should come first.I think this change (commit c06d1a4) is incorrect. Assume checksums and
full-page writes are enabled, both defaults. Before this change, redo would
transition FSM pages clean->dirty only via XLogReadBufferForRedo() of an FPI.
At end of recovery, the FSM passed checksum validation. After $SUBJECT,
nothing stops the following sequence of events: finish restartpoint; dirty FSM
page via heap_xlog_*; crash tears write of that FSM page; resume recovery; end
recovery without curing the torn page. The FSM code's own use of
RBM_ZERO_ON_ERROR makes FSM code accept torn pages. Other callers assume
normal reads will succeed on FSM_FORKNUM, e.g., the
heapam_relation_copy_data() call to RelationCopyStorage(). Claude wrote a
test of that, attached.To be fair, freespace/README itself incorrectly predicts the commit is okay,
by saying things like, "we rely on a bunch of self-correcting measures to
repair possible corruption." Long-term, we do have at least these
alternatives:- Keep the pre-2026-05 invariant that FSM is free from torn pages at end of
recovery. (We'll likely still have the property that heap_xlog_* will read
torn FSM pages during recovery. That arises, I think, because the FPI that
fixes the torn page may be later in the WAL stream. FSM is unlike other
forks this way; other forks write before they read.)- Require all readers of FSM_FORKNUM to use RBM_ZERO_ON_ERROR or the
equivalent.Side excursion that I'm dumping here in case it saves someone time: I was
suspicious of the older MarkBufferDirty(), in
FreeSpaceMapPrepareTruncateRel(). Subtle interactions make it harmless. It
involves a WAL write via log_newpage_buffer(), so it almost follows the
transam/README protocol. The odd part is that the protocol-required actions
happen across two WAL records. The redo of XLOG_FPI comes first and dirties
the buffer properly. Then redo of XLOG_SMGR_TRUNCATE calls
FreeSpaceMapPrepareTruncateRel() and dirties it again. That would normally
not be okay. However, the primary side used DELAY_CHKPT_COMPLETE to prevent a
checkpoint or restartpoint between the two records. No problem behavior
arises.Should we push it to all supported branches?
(The commit did get back-patched, even though its commit log doesn't say that.)
Apologize for not setting all the commit tags correctly. I'm going to
get into details of this issue during this week.
------
Regards,
Alexander Korotkov
Supabase
On Tue, Sep 1, 2026 at 5:18 PM Noah Misch <noah@leadboat.com> wrote:
On Tue, Apr 21, 2026 at 04:49:12PM +0300, Alexander Korotkov wrote:
I also think that usage of MarkBufferDirty() here is safe. If I
understood correctly.
1) When wal_log_hints = on, should be completely safe. Even if we
have torn page after the crash, during recovery FPI from the primary
should come first.I think this change (commit c06d1a4) is incorrect. Assume checksums and
full-page writes are enabled, both defaults. Before this change, redo would
transition FSM pages clean->dirty only via XLogReadBufferForRedo() of an FPI.
At end of recovery, the FSM passed checksum validation. After $SUBJECT,
nothing stops the following sequence of events: finish restartpoint; dirty FSM
page via heap_xlog_*; crash tears write of that FSM page; resume recovery; end
recovery without curing the torn page. The FSM code's own use of
RBM_ZERO_ON_ERROR makes FSM code accept torn pages. Other callers assume
normal reads will succeed on FSM_FORKNUM, e.g., the
heapam_relation_copy_data() call to RelationCopyStorage(). Claude wrote a
test of that, attached.
Ah, I guess this all hinged on having correctly surveyed all readers
of the FSM and ensuring they also use RBM_ZERO_ON_ERROR, which I did
not do and it turns out they don't. Oops.
Long-term, we do have at least these
alternatives:- Keep the pre-2026-05 invariant that FSM is free from torn pages at end of
recovery. (We'll likely still have the property that heap_xlog_* will read
torn FSM pages during recovery. That arises, I think, because the FPI that
fixes the torn page may be later in the WAL stream. FSM is unlike other
forks this way; other forks write before they read.)- Require all readers of FSM_FORKNUM to use RBM_ZERO_ON_ERROR or the
equivalent.
I need to think more about it, but my initial thought was maybe
RelationCopyStorage() shouldn't be reading all forks the same with a
simple smgrread(). Though I don't know how reasonable it is to try to
prevent anyone in the future from ever reading the FSM fork without
RBM_ZERO_ON_ERROR. And I also didn't yet check if other callers are
reading the FSM without RBM_ZERO_ON_ERROR already. Hmm...I'll need to
think and dig a bit more.
- Melanie
Hi!
On Wed, Sep 2, 2026 at 2:39 AM Melanie Plageman
<melanieplageman@gmail.com> wrote:
On Tue, Sep 1, 2026 at 5:18 PM Noah Misch <noah@leadboat.com> wrote:
On Tue, Apr 21, 2026 at 04:49:12PM +0300, Alexander Korotkov wrote:
I also think that usage of MarkBufferDirty() here is safe. If I
understood correctly.
1) When wal_log_hints = on, should be completely safe. Even if we
have torn page after the crash, during recovery FPI from the primary
should come first.I think this change (commit c06d1a4) is incorrect. Assume checksums and
full-page writes are enabled, both defaults. Before this change, redo would
transition FSM pages clean->dirty only via XLogReadBufferForRedo() of an FPI.
At end of recovery, the FSM passed checksum validation. After $SUBJECT,
nothing stops the following sequence of events: finish restartpoint; dirty FSM
page via heap_xlog_*; crash tears write of that FSM page; resume recovery; end
recovery without curing the torn page. The FSM code's own use of
RBM_ZERO_ON_ERROR makes FSM code accept torn pages. Other callers assume
normal reads will succeed on FSM_FORKNUM, e.g., the
heapam_relation_copy_data() call to RelationCopyStorage(). Claude wrote a
test of that, attached.Ah, I guess this all hinged on having correctly surveyed all readers
of the FSM and ensuring they also use RBM_ZERO_ON_ERROR, which I did
not do and it turns out they don't. Oops.Long-term, we do have at least these
alternatives:- Keep the pre-2026-05 invariant that FSM is free from torn pages at end of
recovery. (We'll likely still have the property that heap_xlog_* will read
torn FSM pages during recovery. That arises, I think, because the FPI that
fixes the torn page may be later in the WAL stream. FSM is unlike other
forks this way; other forks write before they read.)- Require all readers of FSM_FORKNUM to use RBM_ZERO_ON_ERROR or the
equivalent.I need to think more about it, but my initial thought was maybe
RelationCopyStorage() shouldn't be reading all forks the same with a
simple smgrread(). Though I don't know how reasonable it is to try to
prevent anyone in the future from ever reading the FSM fork without
RBM_ZERO_ON_ERROR. And I also didn't yet check if other callers are
reading the FSM without RBM_ZERO_ON_ERROR already. Hmm...I'll need to
think and dig a bit more.
I've been thinking about it for a while. While FSM code can tolerate
torn pages, other code can't (and that's not only backend code). I
think we have nothing to do with this (at least with back branches).
0001 is revert
0002 is patch for FSM readme to highlight this aspect
I'm going to push (and backpatch) 0001. 0002 needs review.
------
Regards,
Alexander Korotkov
Supabase
Attachments:
t53547_15v1-0002-Say-in-the-FSM-README-why-its-hint-writes-still-n.patchapplication/x-patch; name=v1-0002-Say-in-the-FSM-README-why-its-hint-writes-still-n.patchDownload+20-4
v1-0001-Revert-Mark-modified-the-FSM-buffer-as-dirty-duri.patchapplication/x-patch; name=v1-0001-Revert-Mark-modified-the-FSM-buffer-as-dirty-duri.patchDownload+1-12
Hi Alexander,
On Sun, Sep 7, 2026 at 3:22 PM Alexander Korotkov wrote:
0001 is revert
0002 is patch for FSM readme to highlight this aspectI'm going to push (and backpatch) 0001. 0002 needs review.
The revert looks correct to me. The heap WAL record drives the FSM
update during redo, but it contains neither a block reference nor an FPI
for the FSM page. MarkBufferDirty() therefore lets the page reach disk
without torn-page protection.
I agree with the README change. "Repairable by a later FPI" could
sound like an unrelated future FPI is expected to repair the page.
Perhaps the paragraph could instead say that, when hint changes require
WAL protection and full_page_writes is enabled, MarkBufferDirtyHint()
either provides that protection before a newly dirtied page can reach
disk or, during recovery where WAL cannot be generated, leaves a clean
page clean.
Should the README correction be backpatched with the revert? The
misleading text is present in the back branches.
The revert leaves Alexey's original performance problem open. For
comparison, the recent VM clear fix c0d9864f5ce registered VM blocks in
heap WAL records. That does not transfer cheaply to FSM, since primary
and redo do not make matching FSM updates.
For master, I think Noah's second alternative is worth developing. It
would need to be a fork-wide contract rather than scattered
ZERO_ON_ERROR exceptions: every consumer must know how to discard or
reinitialize an invalid FSM page, and verification tools need an
explicit policy. I couldn't find a concrete proposal for that in the
thread, so this seems like separate work.
There are some other cases when we try to avoid FPIs...
Or perhaps this will all become simple once we have a double-write
buffer. :)
One small commit message fix:
s/everything else do so/everything else does so/
Thank you!
Best regards, Andrey Borodin.
On Tue, Sep 8, 2026 at 7:43 AM Andrey Borodin <x4mmm@yandex-team.ru> wrote:
On Sun, Sep 7, 2026 at 3:22 PM Alexander Korotkov wrote:
0001 is revert
0002 is patch for FSM readme to highlight this aspectI'm going to push (and backpatch) 0001. 0002 needs review.
The revert looks correct to me. The heap WAL record drives the FSM
update during redo, but it contains neither a block reference nor an FPI
for the FSM page. MarkBufferDirty() therefore lets the page reach disk
without torn-page protection.
I've pushed the revert.
I agree with the README change. "Repairable by a later FPI" could
sound like an unrelated future FPI is expected to repair the page.
Perhaps the paragraph could instead say that, when hint changes require
WAL protection and full_page_writes is enabled, MarkBufferDirtyHint()
either provides that protection before a newly dirtied page can reach
disk or, during recovery where WAL cannot be generated, leaves a clean
page clean.
Didn't yet pushed the README. I wish it have more review.
Should the README correction be backpatched with the revert? The
misleading text is present in the back branches.
Not sure we necessarily need this. Whoever would try to fix this
should start from exploring master.
The revert leaves Alexey's original performance problem open. For
comparison, the recent VM clear fix c0d9864f5ce registered VM blocks in
heap WAL records. That does not transfer cheaply to FSM, since primary
and redo do not make matching FSM updates.For master, I think Noah's second alternative is worth developing. It
would need to be a fork-wide contract rather than scattered
ZERO_ON_ERROR exceptions: every consumer must know how to discard or
reinitialize an invalid FSM page, and verification tools need an
explicit policy. I couldn't find a concrete proposal for that in the
thread, so this seems like separate work.
Yes, that should be separate and rather bigger work on master.
There are some other cases when we try to avoid FPIs...
Or perhaps this will all become simple once we have a double-write
buffer. :)One small commit message fix:
s/everything else do so/everything else does so/
That was applied before commit.
------
Regards,
Alexander Korotkov
Supabase
On Mon, Sep 07, 2026 at 06:22:22PM +0300, Alexander Korotkov wrote:
0002 is patch for FSM readme to highlight this aspect
I'm going to push (and backpatch) 0001. 0002 needs review.
Subject: [PATCH v1 2/2] Say in the FSM README why its hint writes still need
full page images
+That RBM_ZERO_ON_ERROR is a property of this directory, not of the FSM fork +itself. The files themselves are ordinary relation files, and plenty of code +outside freespace.c reads them and does verify checksums: +RelationCopyStorage(), which ALTER TABLE ... SET TABLESPACE runs over every +fork; the read stream in RelationCopyStorageUsingBuffer(), used by +CREATE DATABASE ... STRATEGY = wal_log; and the checksum verification in base +backups and in pg_checksums. The last two are the reason this cannot simply be +relaxed: an external verification tool has no way to tell a tear that we +consider harmless from one caused by failing storage, so making it accept +the former blinds it to the latter.
One could relax the "last two" by having those tools skip checksum
verification for *_fsm files, so let's not claim it's infeasible. It's true
that this could miss failing-storage evidence that coincidentally landed in
FSM, but accepting that could be the right thing someday. Other than that, I
agree with this README change. Thanks.
On Sat, Sep 12, 2026 at 11:05 PM Noah Misch <noah@leadboat.com> wrote:
On Mon, Sep 07, 2026 at 06:22:22PM +0300, Alexander Korotkov wrote:
0002 is patch for FSM readme to highlight this aspect
I'm going to push (and backpatch) 0001. 0002 needs review.
Subject: [PATCH v1 2/2] Say in the FSM README why its hint writes still need
full page images+That RBM_ZERO_ON_ERROR is a property of this directory, not of the FSM fork +itself. The files themselves are ordinary relation files, and plenty of code +outside freespace.c reads them and does verify checksums: +RelationCopyStorage(), which ALTER TABLE ... SET TABLESPACE runs over every +fork; the read stream in RelationCopyStorageUsingBuffer(), used by +CREATE DATABASE ... STRATEGY = wal_log; and the checksum verification in base +backups and in pg_checksums. The last two are the reason this cannot simply be +relaxed: an external verification tool has no way to tell a tear that we +consider harmless from one caused by failing storage, so making it accept +the former blinds it to the latter.One could relax the "last two" by having those tools skip checksum
verification for *_fsm files, so let's not claim it's infeasible. It's true
that this could miss failing-storage evidence that coincidentally landed in
FSM, but accepting that could be the right thing someday. Other than that, I
agree with this README change. Thanks.
I've corrected that paragraph. I'm going to push this to master if no
objections.
------
Regards,
Alexander Korotkov
Supabase
On Sun, Sep 13, 2026 at 03:10:58PM +0300, Alexander Korotkov wrote:
On Sat, Sep 12, 2026 at 11:05 PM Noah Misch <noah@leadboat.com> wrote:
On Mon, Sep 07, 2026 at 06:22:22PM +0300, Alexander Korotkov wrote:
0002 is patch for FSM readme to highlight this aspect
I'm going to push (and backpatch) 0001. 0002 needs review.
Subject: [PATCH v1 2/2] Say in the FSM README why its hint writes still need
full page images+That RBM_ZERO_ON_ERROR is a property of this directory, not of the FSM fork +itself. The files themselves are ordinary relation files, and plenty of code +outside freespace.c reads them and does verify checksums: +RelationCopyStorage(), which ALTER TABLE ... SET TABLESPACE runs over every +fork; the read stream in RelationCopyStorageUsingBuffer(), used by +CREATE DATABASE ... STRATEGY = wal_log; and the checksum verification in base +backups and in pg_checksums. The last two are the reason this cannot simply be +relaxed: an external verification tool has no way to tell a tear that we +consider harmless from one caused by failing storage, so making it accept +the former blinds it to the latter.One could relax the "last two" by having those tools skip checksum
verification for *_fsm files, so let's not claim it's infeasible. It's true
that this could miss failing-storage evidence that coincidentally landed in
FSM, but accepting that could be the right thing someday. Other than that, I
agree with this README change. Thanks.I've corrected that paragraph. I'm going to push this to master if no
objections.
+That RBM_ZERO_ON_ERROR is a property of this directory, not of the FSM fork +itself. The files themselves are ordinary relation files, and plenty of code +outside freespace.c reads them and does verify checksums: +RelationCopyStorage(), which ALTER TABLE ... SET TABLESPACE runs over every +fork; the read stream in RelationCopyStorageUsingBuffer(), used by +CREATE DATABASE ... STRATEGY = wal_log; and the checksum verification in base +backups and in pg_checksums. The last two could instead be taught to skip +*_fsm files, and that may be the right call someday. The cost would be that +a tool told to ignore a tear in the FSM can no longer report one caused by +failing storage. Today they do verify, so what we write here has to be +valid.
I'd handle the last sentences differently, yielding a paragraph like this:
That RBM_ZERO_ON_ERROR is a property of this directory, not of the FSM fork
itself. The files themselves are ordinary relation files, and plenty of code
outside freespace.c reads them and does verify checksums:
RelationCopyStorage(), which ALTER TABLE ... SET TABLESPACE runs over every
fork; the read stream in RelationCopyStorageUsingBuffer(), used by CREATE
DATABASE ... STRATEGY = wal_log; and the checksum verification in base backups
and in pg_checksums. The alternative would be all such code skipping checksum
validation of *_fsm files, and that may be the right call someday. Today they
do verify, so what we write here has to be valid.