public inbox for [email protected]
help / color / mirror / Atom feedFrom: Heikki Linnakangas <[email protected]>
To: Anthonin Bonnefoy <[email protected]>
To: Amul Sul <[email protected]>
Cc: PostgreSQL Hackers <[email protected]>
Subject: Re: Fix possible 'unexpected data beyond EOF' on replica restart
Date: Thu, 18 Dec 2025 16:18:46 +0200
Message-ID: <[email protected]> (raw)
In-Reply-To: <CAO6_XqojxYb=j_rqSi_MH-DB=tZ-OtMQHqEx6xRh9DQrr2uDuQ@mail.gmail.com>
References: <CAO6_Xqrv-snNJNhbj1KjQmWiWHX3nYGDgAc=vxaZP3qc4g1Siw@mail.gmail.com>
<CAAJ_b94FEArRqXd4q+drOh=yS4_2sPrdHTGXTVH+wHRP0EPuCw@mail.gmail.com>
<CAO6_XqojxYb=j_rqSi_MH-DB=tZ-OtMQHqEx6xRh9DQrr2uDuQ@mail.gmail.com>
On 17/12/2025 10:40, Anthonin Bonnefoy wrote:
> On Wed, Dec 17, 2025 at 8:26 AM Amul Sul <[email protected]
> <mailto:[email protected]>> wrote:
>
>> The deleted code you moved to mdtruncate() should be kept where it
>> was. We cannot ensure that every extension using this interface will
>> adhere to that requirement what the comment describes.
>
> Yeah, I've overlooked the case of extensions. I've moved back the
> InvalidBlockNumber assignment.
The smgr interface isn't really an extension point, so we don't need to
worry about that. (I wish it was, but that's a different story [1])
I don't think mdtruncate() should modify smgr_cached_nblocks, we should
keep that in smgrtruncate(). smgr.c is responsible for all other updates
of smgr_cached_nblocks, so doing it in mdtruncate() would be a layering
violation.
I'm thinking that we should do the attached. Untested, and we should
also add a comment to smgrtruncate() and mdtruncate() to explain how
they behave if nblocks > curnblk.
I wonder if we should move the whole "if (nblocks > curnblk)" check and
ereport() from mdtruncate() to smgrtruncate(). That logic doesn't really
depend on anything specific to md.c. If you'd imagine a different smgr
implementation, it'd need to just copy-paste that check. It's the
caller's mistake if it passes nblocks > curnblk, when not in recovery.
Then again, we do have other places in md.c too that behave differently
when InRecovery.
What do you think?
> I wonder how critical it is to have an up to date value of
> smgr_cached_nblocks after smgr_truncate. Leaving InvalidBlockNumber was
> also an option as the next user will ask the kernel for the real size.
> There are some functions like DropRelationBuffers which rely only on the
> cached value, so it's probably safer to keep the same behaviour.
Leaving it invalid should work. But as the comment says, we might as
well update the cached value since we have the value at hand. It's just
that we were doing it wrong.
[1]
https://www.postgresql.org/message-id/CAEze2WgMySu2suO_TLvFyGY3URa4mAx22WeoEicnK%3DPCNWEMrA%40mail.g...
- Heikki
Attachments:
[text/x-patch] v3-Fix-unexpected-data-beyond-EOF-on-replica-restart.patch (562B, ../[email protected]/2-v3-Fix-unexpected-data-beyond-EOF-on-replica-restart.patch)
download | inline diff:
diff --git a/src/backend/storage/smgr/smgr.c b/src/backend/storage/smgr/smgr.c
index f9066ab8c49..abb51f0a0bb 100644
--- a/src/backend/storage/smgr/smgr.c
+++ b/src/backend/storage/smgr/smgr.c
@@ -911,7 +911,8 @@ smgrtruncate(SMgrRelation reln, ForkNumber *forknum, int nforks,
* these ones too at the next command boundary. But ensure they aren't
* outright wrong until then.
*/
- reln->smgr_cached_nblocks[forknum[i]] = nblocks[i];
+ reln->smgr_cached_nblocks[forknum[i]] =
+ nblocks[i] > old_nblocks[i] ? old_nblocks[i] : nblocks[i];
}
}
view thread (5+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected], [email protected]
Subject: Re: Fix possible 'unexpected data beyond EOF' on replica restart
In-Reply-To: <[email protected]>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox