Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1osRSZ-0005ZC-Ul for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Nov 2022 16:28:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1osRSY-0004vt-Gq for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Nov 2022 16:28:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1osRSY-0004vj-7S for pgsql-hackers@lists.postgresql.org; Tue, 08 Nov 2022 16:28:22 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1osRSP-0002Y2-Uj for pgsql-hackers@lists.postgresql.org; Tue, 08 Nov 2022 16:28:19 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 2A8GS8Jt3797576; Tue, 8 Nov 2022 11:28:08 -0500 From: Tom Lane To: pgsql-hackers@lists.postgresql.org cc: Robert Haas , Andres Freund Subject: Non-emergency patch for bug #17679 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <3797416.1667924841.0@sss.pgh.pa.us> Date: Tue, 08 Nov 2022 11:28:08 -0500 Message-ID: <3797575.1667924888@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3797416.1667924841.1@sss.pgh.pa.us> In the release team's discussion leading up to commit 0e758ae89, Andres opined that what commit 4ab5dae94 had done to mdunlinkfork was a mess, and I concur. It invented an entirely new code path through that function, and required two different behaviors from the segment-deletion loop. I think a very straight line can be drawn between that extra complexity and the introduction of a nasty bug. It's all unnecessary too, because AFAICS all we really need is to apply the pre-existing behavior for temp tables and REDO mode to binary-upgrade mode as well. Hence, the attached reverts everything 4ab5dae94 did to this function, and most of 0e758ae89 too, and instead makes IsBinaryUpgrade an additional reason to take the immediate-unlink path. Barring objections, I'll push this after the release freeze lifts. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fix-mdunlinkfork-more-cleanly.patch"; charset="us-ascii" Content-ID: <3797416.1667924841.2@sss.pgh.pa.us> Content-Description: fix-mdunlinkfork-more-cleanly.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c index b1a2cb575d..0f642df3c1 100644 --- a/src/backend/storage/smgr/md.c +++ b/src/backend/storage/smgr/md.c @@ -263,6 +263,12 @@ mdcreate(SMgrRelation reln, ForkNumber forknum, bool = isRedo) * The fact that temp rels and regular rels have different file naming * patterns provides additional safety. * + * We also don't do it while performing a binary upgrade. There is no re= use + * hazard in that case, since after a crash or even a simple ERROR, the + * upgrade fails and the whole cluster must be recreated from scratch. + * Furthermore, it is important to remove the files from disk immediately= , + * because we may be about to reuse the same relfilenumber. + * * All the above applies only to the relation's main fork; other forks ca= n * just be removed immediately, since they are not needed to prevent the * relfilenumber from being recycled. Also, we do not carefully @@ -319,14 +325,15 @@ mdunlinkfork(RelFileLocatorBackend rlocator, ForkNum= ber forknum, bool isRedo) { char *path; int ret; - BlockNumber segno =3D 0; = path =3D relpath(rlocator, forknum); = /* - * Delete or truncate the first segment. + * Truncate and then unlink the first segment, or just register a reques= t + * to unlink it later, as described in the comments for mdunlink(). */ - if (isRedo || forknum !=3D MAIN_FORKNUM || RelFileLocatorBackendIsTemp(r= locator)) + if (isRedo || IsBinaryUpgrade || forknum !=3D MAIN_FORKNUM || + RelFileLocatorBackendIsTemp(rlocator)) { if (!RelFileLocatorBackendIsTemp(rlocator)) { @@ -351,7 +358,6 @@ mdunlinkfork(RelFileLocatorBackend rlocator, ForkNumbe= r forknum, bool isRedo) ereport(WARNING, (errcode_for_file_access(), errmsg("could not remove file \"%s\": %m", path))); - segno++; } } else @@ -359,42 +365,25 @@ mdunlinkfork(RelFileLocatorBackend rlocator, ForkNum= ber forknum, bool isRedo) /* Prevent other backends' fds from holding on to the disk space */ ret =3D do_truncate(path); = - /* - * Except during a binary upgrade, register request to unlink first - * segment later, rather than now. - * - * If we're performing a binary upgrade, the dangers described in the - * header comments for mdunlink() do not exist, since after a crash or - * even a simple ERROR, the upgrade fails and the whole new cluster - * must be recreated from scratch. And, on the other hand, it is - * important to remove the files from disk immediately, because we may - * be about to reuse the same relfilenumber. - */ - if (!IsBinaryUpgrade) - { - register_unlink_segment(rlocator, forknum, 0 /* first seg */ ); - segno++; - } + /* Register request to unlink first segment later */ + register_unlink_segment(rlocator, forknum, 0 /* first seg */ ); } = /* - * Delete any remaining segments (we might or might not have dealt with - * the first one above). + * Delete any additional segments. */ if (ret >=3D 0) { char *segpath =3D (char *) palloc(strlen(path) + 12); + BlockNumber segno; = /* * Note that because we loop until getting ENOENT, we will correctly * remove all inactive segments as well as active ones. */ - for (;; segno++) + for (segno =3D 1;; segno++) { - if (segno =3D=3D 0) - strcpy(segpath, path); - else - sprintf(segpath, "%s.%u", path, segno); + sprintf(segpath, "%s.%u", path, segno); = if (!RelFileLocatorBackendIsTemp(rlocator)) { ------- =_aaaaaaaaaa0--