Skip to content

Commit 923e8de

Browse files
committed
Add defenses against pre-crash files to BufFileOpenShared().
Crash restarts currently don't clean up temporary files, as a debugging aid. If a left-over file happens to have the same name as a segment file we're trying to create, we'll just truncate and reuse it, but there is a problem: BufFileOpenShared() determines how many segment files exist by trying to open .0, .1, .2, ... until it finds no more files. It might be confused by a junk file that has the next segment number. To defend against that, make sure we always create a gap after the end file by unlinking the following name if it exists. Also make it an error to try to open a BufFile that doesn't exist (has no segment 0), so as not to encourage the development of client code that depends on an interface that we can't reliably provide. Author: Thomas Munro Reviewed-By: Andres Freund Discussion: https://postgr.es/m/CAEepm%3D2jhCbC_GFQJaaDhWxLB4EXtT3vVd5czuRNaqF5CWSTog%40mail.gmail.com
1 parent 884a608 commit 923e8de

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/backend/storage/file/buffile.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,16 @@ MakeNewSharedSegment(BufFile *buffile, int segment)
211211
char name[MAXPGPATH];
212212
File file;
213213

214+
/*
215+
* It is possible that there are files left over from before a crash
216+
* restart with the same name. In order for BufFileOpenShared()
217+
* not to get confused about how many segments there are, we'll unlink
218+
* the next segment number if it already exists.
219+
*/
220+
SharedSegmentName(name, buffile->name, segment + 1);
221+
SharedFileSetDelete(buffile->fileset, name, true);
222+
223+
/* Create the new segment. */
214224
SharedSegmentName(name, buffile->name, segment);
215225
file = SharedFileSetCreate(buffile->fileset, name);
216226

@@ -303,7 +313,9 @@ BufFileOpenShared(SharedFileSet *fileset, const char *name)
303313
* name.
304314
*/
305315
if (nfiles == 0)
306-
return NULL;
316+
ereport(ERROR,
317+
(errcode_for_file_access(),
318+
errmsg("could not open BufFile \"%s\"", name)));
307319

308320
file->numFiles = nfiles;
309321
file->files = files;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy