1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-17 06:41:09 +03:00

Back-patch fix that allows AllocateFile() to return errno=ENFILE/EMFILE

after we are no longer able to close any more VFDs.  This is needed to
avoid postmaster crash under out-of-file-descriptors conditions.
This commit is contained in:
Tom Lane
2000-09-23 23:31:24 +00:00
parent c954088b83
commit de3e0183b8

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/file/fd.c,v 1.56 2000/04/12 17:15:35 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/storage/file/fd.c,v 1.56.2.1 2000/09/23 23:31:24 tgl Exp $
* *
* NOTES: * NOTES:
* *
@ -181,7 +181,7 @@ static void Delete(File file);
static void LruDelete(File file); static void LruDelete(File file);
static void Insert(File file); static void Insert(File file);
static int LruInsert(File file); static int LruInsert(File file);
static void ReleaseLruFile(void); static bool ReleaseLruFile(void);
static File AllocateVfd(void); static File AllocateVfd(void);
static void FreeVfd(File file); static void FreeVfd(File file);
@ -347,7 +347,10 @@ LruInsert(File file)
{ {
while (nfile + numAllocatedFiles >= pg_nofile()) while (nfile + numAllocatedFiles >= pg_nofile())
ReleaseLruFile(); {
if (! ReleaseLruFile())
break;
}
/* /*
* The open could still fail for lack of file descriptors, eg due * The open could still fail for lack of file descriptors, eg due
@ -358,9 +361,12 @@ tryAgain:
vfdP->fd = open(vfdP->fileName, vfdP->fileFlags, vfdP->fileMode); vfdP->fd = open(vfdP->fileName, vfdP->fileFlags, vfdP->fileMode);
if (vfdP->fd < 0 && (errno == EMFILE || errno == ENFILE)) if (vfdP->fd < 0 && (errno == EMFILE || errno == ENFILE))
{ {
int save_errno = errno;
errno = 0; errno = 0;
ReleaseLruFile(); if (ReleaseLruFile())
goto tryAgain; goto tryAgain;
errno = save_errno;
} }
if (vfdP->fd < 0) if (vfdP->fd < 0)
@ -392,20 +398,22 @@ tryAgain:
return 0; return 0;
} }
static void static bool
ReleaseLruFile() ReleaseLruFile()
{ {
DO_DB(elog(DEBUG, "ReleaseLruFile. Opened %d", nfile)); DO_DB(elog(DEBUG, "ReleaseLruFile. Opened %d", nfile));
if (nfile <= 0) if (nfile > 0)
elog(ERROR, "ReleaseLruFile: No open files available to be closed"); {
/*
/* * There are opened files and so there should be at least one used
* There are opened files and so there should be at least one used vfd * vfd in the ring.
* in the ring. */
*/ Assert(VfdCache[0].lruMoreRecently != 0);
Assert(VfdCache[0].lruMoreRecently != 0); LruDelete(VfdCache[0].lruMoreRecently);
LruDelete(VfdCache[0].lruMoreRecently); return true; /* freed a file */
}
return false; /* no files available to free */
} }
/* /*
@ -612,17 +620,23 @@ fileNameOpenFile(FileName fileName,
vfdP = &VfdCache[file]; vfdP = &VfdCache[file];
while (nfile + numAllocatedFiles >= pg_nofile()) while (nfile + numAllocatedFiles >= pg_nofile())
ReleaseLruFile(); {
if (! ReleaseLruFile())
break;
}
tryAgain: tryAgain:
vfdP->fd = open(fileName, fileFlags, fileMode); vfdP->fd = open(fileName, fileFlags, fileMode);
if (vfdP->fd < 0 && (errno == EMFILE || errno == ENFILE)) if (vfdP->fd < 0 && (errno == EMFILE || errno == ENFILE))
{ {
int save_errno = errno;
DO_DB(elog(DEBUG, "fileNameOpenFile: not enough descs, retry, er= %d", DO_DB(elog(DEBUG, "fileNameOpenFile: not enough descs, retry, er= %d",
errno)); errno));
errno = 0; errno = 0;
ReleaseLruFile(); if (ReleaseLruFile())
goto tryAgain; goto tryAgain;
errno = save_errno;
} }
if (vfdP->fd < 0) if (vfdP->fd < 0)
@ -1004,11 +1018,14 @@ TryAgain:
{ {
if (errno == EMFILE || errno == ENFILE) if (errno == EMFILE || errno == ENFILE)
{ {
int save_errno = errno;
DO_DB(elog(DEBUG, "AllocateFile: not enough descs, retry, er= %d", DO_DB(elog(DEBUG, "AllocateFile: not enough descs, retry, er= %d",
errno)); errno));
errno = 0; errno = 0;
ReleaseLruFile(); if (ReleaseLruFile())
goto TryAgain; goto TryAgain;
errno = save_errno;
} }
} }
else else