Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 049469e

Browse files
committed
Teach mdnblocks() not to create zero-length files.
It's entirely surprising that mdnblocks() has the side effect of creating new files on disk, so let's make it not do that. One consequence of the old behavior is that, if running on a damaged cluster that is missing a file, mdnblocks() can recreate the file and allow a subsequent _mdfd_getseg() for a higher segment to succeed. This happens because, while mdnblocks() stops when it finds a segment that is shorter than 1GB, _mdfd_getseg() has no such check, and thus the empty file created by mdnblocks() can allow it to continue its traversal and find higher-numbered segments which remain. It might be a good idea for _mdfd_getseg() to actually verify that each segment it finds is exactly 1GB before proceeding to the next one, but that would involve some additional system calls, so for now I'm just doing this much. Patch by me, per off-list analysis by Kevin Grittner and Rahila Syed. Review by Andres Freund.
1 parent 6150a1b commit 049469e

File tree

1 file changed

+7
-9
lines changed
  • src/backend/storage/smgr

1 file changed

+7
-9
lines changed

src/backend/storage/smgr/md.c

+7-9
Original file line numberDiff line numberDiff line change
@@ -848,17 +848,15 @@ mdnblocks(SMgrRelation reln, ForkNumber forknum)
848848
if (v->mdfd_chain == NULL)
849849
{
850850
/*
851-
* Because we pass O_CREAT, we will create the next segment (with
852-
* zero length) immediately, if the last segment is of length
853-
* RELSEG_SIZE. While perhaps not strictly necessary, this keeps
854-
* the logic simple.
851+
* We used to pass O_CREAT here, but that's has the disadvantage
852+
* that it might create a segment which has vanished through some
853+
* operating system misadventure. In such a case, creating the
854+
* segment here undermine _mdfd_getseg's attempts to notice and
855+
* report an error upon access to a missing segment.
855856
*/
856-
v->mdfd_chain = _mdfd_openseg(reln, forknum, segno, O_CREAT);
857+
v->mdfd_chain = _mdfd_openseg(reln, forknum, segno, 0);
857858
if (v->mdfd_chain == NULL)
858-
ereport(ERROR,
859-
(errcode_for_file_access(),
860-
errmsg("could not open file \"%s\": %m",
861-
_mdfd_segpath(reln, forknum, segno))));
859+
return segno * ((BlockNumber) RELSEG_SIZE);
862860
}
863861

864862
v = v->mdfd_chain;

0 commit comments

Comments
 (0)