Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vWEqK-00FkY9-2Y for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Dec 2025 14:19:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vWEqI-002T9l-2t for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Dec 2025 14:18:59 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vWEqI-002T9a-1d for pgsql-hackers@lists.postgresql.org; Thu, 18 Dec 2025 14:18:59 +0000 Received: from lahtoruutu.iki.fi ([2a0b:5c81:1c1::37]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vWEqG-001LE2-1g for pgsql-hackers@postgresql.org; Thu, 18 Dec 2025 14:18:58 +0000 Received: from [10.0.2.15] (unknown [130.41.208.2]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by lahtoruutu.iki.fi (Postfix) with ESMTPSA id 4dXCT30Wsgz49PxD; Thu, 18 Dec 2025 16:18:46 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1766067527; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=xqjghnLyZ99KiucAwyvrDULUFLn7NLsWL4UynVR6wVg=; b=H/KOn9Jdl0DIWjO3xMBH7R1DavPAlmkNPfl1GWSl2l5J8bs2KgobGuZ1NmMDsO90UEYp7X buhJjph7saWbOa9SeIbcDDBdVxziAE0drbKeVouMxfrjrl0ksN9/OVpav6E9TPVg4YnGFR p393gExj6nikG/3RIYqwMNZ5RWVG6VcUvfOSXf60g92vElpUEyWePvzqxrOJ6y0r4RLSAM 72/wZaVSVpIGRl54/kpwDw6KqI8jSl2ULETxlD4ShfqHk0WJf6kTOF+vn9HGDzWbqxUBKf p1oAjwL8J23Ur0ATDeqc2fuh7b1EdJZ0YqUkF6n0ltEEN/n8Uw5IcfByclxjrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1766067527; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=xqjghnLyZ99KiucAwyvrDULUFLn7NLsWL4UynVR6wVg=; b=XVPBTCC7jLS9iBg8H5EO66705bfhad0YlDuqaul2cvK5HVlLNUvTTsdO7Bw8hIAZ3o1uVU swvl33lCIMDzJ+//zX5Zm+Q9p6KKNF8zOjRqjZIoKN2YVoomi/qaI2TTjwT46xrMN+EG5w uCn93Ecyh2qcb5nKjLUP4TR8H9ANFhTkiwDXeMeZOcsdYJZa7Mb0eofeaEHPqB3MVWrs0w BxJ1465vdrpyspDHsYOFo4TlvDNuPGgAe16+PnWfFMOgArRju3XFfQ9LWSKsRGDmYMvDFk 4Avr2vqRTi8ayikqem3GtdwOJfzrWQ3k16vvzt5eguuJLR2RSFOG3ZgT6DgpQQ== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi ARC-Seal: i=1; a=rsa-sha256; d=iki.fi; s=lahtoruutu; cv=none; t=1766067527; b=jDdVKlSCJ7M5r3L+IYvlHO2gL9cKFKqxbM7b2b1aAnHMD0r5GP8IoMwonXYsv6hdpLscUg bZe0RKPIsNxrpKSyBsY+juszQ7IMChRbFLazN2X2Yx+kFBox+FpJ3fWLnbMXPa5DRMnzpV aRCftN/UIKXF66b448xM7TtebFAsMfI/4jQnAii/mzNOQmZ4Tn4LS5d9CZzWOs4z5cagFm Cf/Ha73r549fCir+I47Zj0ILAzWQWLGNgtL13bFBoAMVrFMKOsLixtU0IA1yRzOdPN2KQR HKlqBt54EEeGwGpUayOdi3ez3yJ+vbyZFHU6MKY+25EdXvENz0a8rC1Cl7tWyA== Content-Type: multipart/mixed; boundary="------------R5UIhwD7yzT9kBUybJ0Giya3" Message-ID: <02e720a8-761b-4309-b26f-f41c288f7990@iki.fi> Date: Thu, 18 Dec 2025 16:18:46 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Fix possible 'unexpected data beyond EOF' on replica restart To: Anthonin Bonnefoy , Amul Sul Cc: PostgreSQL Hackers References: Content-Language: en-US From: Heikki Linnakangas In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------R5UIhwD7yzT9kBUybJ0Giya3 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 17/12/2025 10:40, Anthonin Bonnefoy wrote: > On Wed, Dec 17, 2025 at 8:26 AM Amul Sul > 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.gmail.com - Heikki --------------R5UIhwD7yzT9kBUybJ0Giya3 Content-Type: text/x-patch; charset=UTF-8; name="v3-Fix-unexpected-data-beyond-EOF-on-replica-restart.patch" Content-Disposition: attachment; filename*0="v3-Fix-unexpected-data-beyond-EOF-on-replica-restart.patch" Content-Transfer-Encoding: base64 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL3N0b3JhZ2Uvc21nci9zbWdyLmMgYi9zcmMvYmFj a2VuZC9zdG9yYWdlL3NtZ3Ivc21nci5jCmluZGV4IGY5MDY2YWI4YzQ5Li5hYmI1MWYwYTBi YiAxMDA2NDQKLS0tIGEvc3JjL2JhY2tlbmQvc3RvcmFnZS9zbWdyL3NtZ3IuYworKysgYi9z cmMvYmFja2VuZC9zdG9yYWdlL3NtZ3Ivc21nci5jCkBAIC05MTEsNyArOTExLDggQEAgc21n cnRydW5jYXRlKFNNZ3JSZWxhdGlvbiByZWxuLCBGb3JrTnVtYmVyICpmb3JrbnVtLCBpbnQg bmZvcmtzLAogCQkgKiB0aGVzZSBvbmVzIHRvbyBhdCB0aGUgbmV4dCBjb21tYW5kIGJvdW5k YXJ5LiBCdXQgZW5zdXJlIHRoZXkgYXJlbid0CiAJCSAqIG91dHJpZ2h0IHdyb25nIHVudGls IHRoZW4uCiAJCSAqLwotCQlyZWxuLT5zbWdyX2NhY2hlZF9uYmxvY2tzW2ZvcmtudW1baV1d ID0gbmJsb2Nrc1tpXTsKKwkJcmVsbi0+c21ncl9jYWNoZWRfbmJsb2Nrc1tmb3JrbnVtW2ld XSA9CisJCQluYmxvY2tzW2ldID4gb2xkX25ibG9ja3NbaV0gPyBvbGRfbmJsb2Nrc1tpXSA6 IG5ibG9ja3NbaV07CiAJfQogfQogCg== --------------R5UIhwD7yzT9kBUybJ0Giya3--