mirror of
https://github.com/postgres/postgres.git
synced 2025-08-25 20:23:07 +03:00
Refactor sharedfileset.c to separate out fileset implementation.
Move fileset related implementation out of sharedfileset.c to allow its usage by backends that don't want to share filesets among different processes. After this split, fileset infrastructure is used by both sharedfileset.c and worker.c for the named temporary files that survive across transactions. Author: Dilip Kumar, based on suggestion by Andres Freund Reviewed-by: Hou Zhijie, Masahiko Sawada, Amit Kapila Discussion: https://postgr.es/m/E1mCC6U-0004Ik-Fs@gemulon.postgresql.org
This commit is contained in:
@@ -16,6 +16,7 @@ OBJS = \
|
||||
buffile.o \
|
||||
copydir.o \
|
||||
fd.o \
|
||||
fileset.o \
|
||||
reinit.o \
|
||||
sharedfileset.o
|
||||
|
||||
|
@@ -39,7 +39,7 @@
|
||||
* BufFile also supports temporary files that can be used by the single backend
|
||||
* when the corresponding files need to be survived across the transaction and
|
||||
* need to be opened and closed multiple times. Such files need to be created
|
||||
* as a member of a SharedFileSet.
|
||||
* as a member of a FileSet.
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
@@ -77,8 +77,8 @@ struct BufFile
|
||||
bool dirty; /* does buffer need to be written? */
|
||||
bool readOnly; /* has the file been set to read only? */
|
||||
|
||||
SharedFileSet *fileset; /* space for segment files if shared */
|
||||
const char *name; /* name of this BufFile if shared */
|
||||
FileSet *fileset; /* space for fileset based segment files */
|
||||
const char *name; /* name of fileset based BufFile */
|
||||
|
||||
/*
|
||||
* resowner is the ResourceOwner to use for underlying temp files. (We
|
||||
@@ -104,7 +104,7 @@ static void extendBufFile(BufFile *file);
|
||||
static void BufFileLoadBuffer(BufFile *file);
|
||||
static void BufFileDumpBuffer(BufFile *file);
|
||||
static void BufFileFlush(BufFile *file);
|
||||
static File MakeNewSharedSegment(BufFile *file, int segment);
|
||||
static File MakeNewFileSetSegment(BufFile *file, int segment);
|
||||
|
||||
/*
|
||||
* Create BufFile and perform the common initialization.
|
||||
@@ -160,7 +160,7 @@ extendBufFile(BufFile *file)
|
||||
if (file->fileset == NULL)
|
||||
pfile = OpenTemporaryFile(file->isInterXact);
|
||||
else
|
||||
pfile = MakeNewSharedSegment(file, file->numFiles);
|
||||
pfile = MakeNewFileSetSegment(file, file->numFiles);
|
||||
|
||||
Assert(pfile >= 0);
|
||||
|
||||
@@ -214,34 +214,34 @@ BufFileCreateTemp(bool interXact)
|
||||
* Build the name for a given segment of a given BufFile.
|
||||
*/
|
||||
static void
|
||||
SharedSegmentName(char *name, const char *buffile_name, int segment)
|
||||
FileSetSegmentName(char *name, const char *buffile_name, int segment)
|
||||
{
|
||||
snprintf(name, MAXPGPATH, "%s.%d", buffile_name, segment);
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a new segment file backing a shared BufFile.
|
||||
* Create a new segment file backing a fileset based BufFile.
|
||||
*/
|
||||
static File
|
||||
MakeNewSharedSegment(BufFile *buffile, int segment)
|
||||
MakeNewFileSetSegment(BufFile *buffile, int segment)
|
||||
{
|
||||
char name[MAXPGPATH];
|
||||
File file;
|
||||
|
||||
/*
|
||||
* It is possible that there are files left over from before a crash
|
||||
* restart with the same name. In order for BufFileOpenShared() not to
|
||||
* restart with the same name. In order for BufFileOpenFileSet() not to
|
||||
* get confused about how many segments there are, we'll unlink the next
|
||||
* segment number if it already exists.
|
||||
*/
|
||||
SharedSegmentName(name, buffile->name, segment + 1);
|
||||
SharedFileSetDelete(buffile->fileset, name, true);
|
||||
FileSetSegmentName(name, buffile->name, segment + 1);
|
||||
FileSetDelete(buffile->fileset, name, true);
|
||||
|
||||
/* Create the new segment. */
|
||||
SharedSegmentName(name, buffile->name, segment);
|
||||
file = SharedFileSetCreate(buffile->fileset, name);
|
||||
FileSetSegmentName(name, buffile->name, segment);
|
||||
file = FileSetCreate(buffile->fileset, name);
|
||||
|
||||
/* SharedFileSetCreate would've errored out */
|
||||
/* FileSetCreate would've errored out */
|
||||
Assert(file > 0);
|
||||
|
||||
return file;
|
||||
@@ -251,15 +251,15 @@ MakeNewSharedSegment(BufFile *buffile, int segment)
|
||||
* Create a BufFile that can be discovered and opened read-only by other
|
||||
* backends that are attached to the same SharedFileSet using the same name.
|
||||
*
|
||||
* The naming scheme for shared BufFiles is left up to the calling code. The
|
||||
* name will appear as part of one or more filenames on disk, and might
|
||||
* The naming scheme for fileset based BufFiles is left up to the calling code.
|
||||
* The name will appear as part of one or more filenames on disk, and might
|
||||
* provide clues to administrators about which subsystem is generating
|
||||
* temporary file data. Since each SharedFileSet object is backed by one or
|
||||
* more uniquely named temporary directory, names don't conflict with
|
||||
* unrelated SharedFileSet objects.
|
||||
*/
|
||||
BufFile *
|
||||
BufFileCreateShared(SharedFileSet *fileset, const char *name)
|
||||
BufFileCreateFileSet(FileSet *fileset, const char *name)
|
||||
{
|
||||
BufFile *file;
|
||||
|
||||
@@ -267,7 +267,7 @@ BufFileCreateShared(SharedFileSet *fileset, const char *name)
|
||||
file->fileset = fileset;
|
||||
file->name = pstrdup(name);
|
||||
file->files = (File *) palloc(sizeof(File));
|
||||
file->files[0] = MakeNewSharedSegment(file, 0);
|
||||
file->files[0] = MakeNewFileSetSegment(file, 0);
|
||||
file->readOnly = false;
|
||||
|
||||
return file;
|
||||
@@ -275,13 +275,13 @@ BufFileCreateShared(SharedFileSet *fileset, const char *name)
|
||||
|
||||
/*
|
||||
* Open a file that was previously created in another backend (or this one)
|
||||
* with BufFileCreateShared in the same SharedFileSet using the same name.
|
||||
* with BufFileCreateFileSet in the same FileSet using the same name.
|
||||
* The backend that created the file must have called BufFileClose() or
|
||||
* BufFileExportShared() to make sure that it is ready to be opened by other
|
||||
* BufFileExportFileSet() to make sure that it is ready to be opened by other
|
||||
* backends and render it read-only.
|
||||
*/
|
||||
BufFile *
|
||||
BufFileOpenShared(SharedFileSet *fileset, const char *name, int mode)
|
||||
BufFileOpenFileSet(FileSet *fileset, const char *name, int mode)
|
||||
{
|
||||
BufFile *file;
|
||||
char segment_name[MAXPGPATH];
|
||||
@@ -304,8 +304,8 @@ BufFileOpenShared(SharedFileSet *fileset, const char *name, int mode)
|
||||
files = repalloc(files, sizeof(File) * capacity);
|
||||
}
|
||||
/* Try to load a segment. */
|
||||
SharedSegmentName(segment_name, name, nfiles);
|
||||
files[nfiles] = SharedFileSetOpen(fileset, segment_name, mode);
|
||||
FileSetSegmentName(segment_name, name, nfiles);
|
||||
files[nfiles] = FileSetOpen(fileset, segment_name, mode);
|
||||
if (files[nfiles] <= 0)
|
||||
break;
|
||||
++nfiles;
|
||||
@@ -333,18 +333,18 @@ BufFileOpenShared(SharedFileSet *fileset, const char *name, int mode)
|
||||
}
|
||||
|
||||
/*
|
||||
* Delete a BufFile that was created by BufFileCreateShared in the given
|
||||
* SharedFileSet using the given name.
|
||||
* Delete a BufFile that was created by BufFileCreateFileSet in the given
|
||||
* FileSet using the given name.
|
||||
*
|
||||
* It is not necessary to delete files explicitly with this function. It is
|
||||
* provided only as a way to delete files proactively, rather than waiting for
|
||||
* the SharedFileSet to be cleaned up.
|
||||
* the FileSet to be cleaned up.
|
||||
*
|
||||
* Only one backend should attempt to delete a given name, and should know
|
||||
* that it exists and has been exported or closed.
|
||||
*/
|
||||
void
|
||||
BufFileDeleteShared(SharedFileSet *fileset, const char *name)
|
||||
BufFileDeleteFileSet(FileSet *fileset, const char *name)
|
||||
{
|
||||
char segment_name[MAXPGPATH];
|
||||
int segment = 0;
|
||||
@@ -357,8 +357,8 @@ BufFileDeleteShared(SharedFileSet *fileset, const char *name)
|
||||
*/
|
||||
for (;;)
|
||||
{
|
||||
SharedSegmentName(segment_name, name, segment);
|
||||
if (!SharedFileSetDelete(fileset, segment_name, true))
|
||||
FileSetSegmentName(segment_name, name, segment);
|
||||
if (!FileSetDelete(fileset, segment_name, true))
|
||||
break;
|
||||
found = true;
|
||||
++segment;
|
||||
@@ -367,16 +367,16 @@ BufFileDeleteShared(SharedFileSet *fileset, const char *name)
|
||||
}
|
||||
|
||||
if (!found)
|
||||
elog(ERROR, "could not delete unknown shared BufFile \"%s\"", name);
|
||||
elog(ERROR, "could not delete unknown BufFile \"%s\"", name);
|
||||
}
|
||||
|
||||
/*
|
||||
* BufFileExportShared --- flush and make read-only, in preparation for sharing.
|
||||
* BufFileExportFileSet --- flush and make read-only, in preparation for sharing.
|
||||
*/
|
||||
void
|
||||
BufFileExportShared(BufFile *file)
|
||||
BufFileExportFileSet(BufFile *file)
|
||||
{
|
||||
/* Must be a file belonging to a SharedFileSet. */
|
||||
/* Must be a file belonging to a FileSet. */
|
||||
Assert(file->fileset != NULL);
|
||||
|
||||
/* It's probably a bug if someone calls this twice. */
|
||||
@@ -785,7 +785,7 @@ BufFileTellBlock(BufFile *file)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Return the current shared BufFile size.
|
||||
* Return the current fileset based BufFile size.
|
||||
*
|
||||
* Counts any holes left behind by BufFileAppend as part of the size.
|
||||
* ereport()s on failure.
|
||||
@@ -811,8 +811,8 @@ BufFileSize(BufFile *file)
|
||||
}
|
||||
|
||||
/*
|
||||
* Append the contents of source file (managed within shared fileset) to
|
||||
* end of target file (managed within same shared fileset).
|
||||
* Append the contents of source file (managed within fileset) to
|
||||
* end of target file (managed within same fileset).
|
||||
*
|
||||
* Note that operation subsumes ownership of underlying resources from
|
||||
* "source". Caller should never call BufFileClose against source having
|
||||
@@ -854,11 +854,11 @@ BufFileAppend(BufFile *target, BufFile *source)
|
||||
}
|
||||
|
||||
/*
|
||||
* Truncate a BufFile created by BufFileCreateShared up to the given fileno and
|
||||
* the offset.
|
||||
* Truncate a BufFile created by BufFileCreateFileSet up to the given fileno
|
||||
* and the offset.
|
||||
*/
|
||||
void
|
||||
BufFileTruncateShared(BufFile *file, int fileno, off_t offset)
|
||||
BufFileTruncateFileSet(BufFile *file, int fileno, off_t offset)
|
||||
{
|
||||
int numFiles = file->numFiles;
|
||||
int newFile = fileno;
|
||||
@@ -876,12 +876,12 @@ BufFileTruncateShared(BufFile *file, int fileno, off_t offset)
|
||||
{
|
||||
if ((i != fileno || offset == 0) && i != 0)
|
||||
{
|
||||
SharedSegmentName(segment_name, file->name, i);
|
||||
FileSetSegmentName(segment_name, file->name, i);
|
||||
FileClose(file->files[i]);
|
||||
if (!SharedFileSetDelete(file->fileset, segment_name, true))
|
||||
if (!FileSetDelete(file->fileset, segment_name, true))
|
||||
ereport(ERROR,
|
||||
(errcode_for_file_access(),
|
||||
errmsg("could not delete shared fileset \"%s\": %m",
|
||||
errmsg("could not delete fileset \"%s\": %m",
|
||||
segment_name)));
|
||||
numFiles--;
|
||||
newOffset = MAX_PHYSICAL_FILESIZE;
|
||||
|
@@ -1921,7 +1921,7 @@ PathNameDeleteTemporaryFile(const char *path, bool error_on_failure)
|
||||
|
||||
/*
|
||||
* Unlike FileClose's automatic file deletion code, we tolerate
|
||||
* non-existence to support BufFileDeleteShared which doesn't know how
|
||||
* non-existence to support BufFileDeleteFileSet which doesn't know how
|
||||
* many segments it has to delete until it runs out.
|
||||
*/
|
||||
if (stat_errno == ENOENT)
|
||||
|
205
src/backend/storage/file/fileset.c
Normal file
205
src/backend/storage/file/fileset.c
Normal file
@@ -0,0 +1,205 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* fileset.c
|
||||
* Management of named temporary files.
|
||||
*
|
||||
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* src/backend/storage/file/fileset.c
|
||||
*
|
||||
* FileSets provide a temporary namespace (think directory) so that files can
|
||||
* be discovered by name.
|
||||
*
|
||||
* FileSets can be used by backends when the temporary files need to be
|
||||
* opened/closed multiple times and the underlying files need to survive across
|
||||
* transactions.
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include "postgres.h"
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "catalog/pg_tablespace.h"
|
||||
#include "commands/tablespace.h"
|
||||
#include "common/hashfn.h"
|
||||
#include "miscadmin.h"
|
||||
#include "storage/ipc.h"
|
||||
#include "storage/fileset.h"
|
||||
#include "utils/builtins.h"
|
||||
|
||||
static void FileSetPath(char *path, FileSet *fileset, Oid tablespace);
|
||||
static void FilePath(char *path, FileSet *fileset, const char *name);
|
||||
static Oid ChooseTablespace(const FileSet *fileset, const char *name);
|
||||
|
||||
/*
|
||||
* Initialize a space for temporary files. This API can be used by shared
|
||||
* fileset as well as if the temporary files are used only by single backend
|
||||
* but the files need to be opened and closed multiple times and also the
|
||||
* underlying files need to survive across transactions.
|
||||
*
|
||||
* The callers are expected to explicitly remove such files by using
|
||||
* FileSetDelete/FileSetDeleteAll.
|
||||
*
|
||||
* Files will be distributed over the tablespaces configured in
|
||||
* temp_tablespaces.
|
||||
*
|
||||
* Under the covers the set is one or more directories which will eventually
|
||||
* be deleted.
|
||||
*/
|
||||
void
|
||||
FileSetInit(FileSet *fileset)
|
||||
{
|
||||
static uint32 counter = 0;
|
||||
|
||||
fileset->creator_pid = MyProcPid;
|
||||
fileset->number = counter;
|
||||
counter = (counter + 1) % INT_MAX;
|
||||
|
||||
/* Capture the tablespace OIDs so that all backends agree on them. */
|
||||
PrepareTempTablespaces();
|
||||
fileset->ntablespaces =
|
||||
GetTempTablespaces(&fileset->tablespaces[0],
|
||||
lengthof(fileset->tablespaces));
|
||||
if (fileset->ntablespaces == 0)
|
||||
{
|
||||
/* If the GUC is empty, use current database's default tablespace */
|
||||
fileset->tablespaces[0] = MyDatabaseTableSpace;
|
||||
fileset->ntablespaces = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int i;
|
||||
|
||||
/*
|
||||
* An entry of InvalidOid means use the default tablespace for the
|
||||
* current database. Replace that now, to be sure that all users of
|
||||
* the FileSet agree on what to do.
|
||||
*/
|
||||
for (i = 0; i < fileset->ntablespaces; i++)
|
||||
{
|
||||
if (fileset->tablespaces[i] == InvalidOid)
|
||||
fileset->tablespaces[i] = MyDatabaseTableSpace;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a new file in the given set.
|
||||
*/
|
||||
File
|
||||
FileSetCreate(FileSet *fileset, const char *name)
|
||||
{
|
||||
char path[MAXPGPATH];
|
||||
File file;
|
||||
|
||||
FilePath(path, fileset, name);
|
||||
file = PathNameCreateTemporaryFile(path, false);
|
||||
|
||||
/* If we failed, see if we need to create the directory on demand. */
|
||||
if (file <= 0)
|
||||
{
|
||||
char tempdirpath[MAXPGPATH];
|
||||
char filesetpath[MAXPGPATH];
|
||||
Oid tablespace = ChooseTablespace(fileset, name);
|
||||
|
||||
TempTablespacePath(tempdirpath, tablespace);
|
||||
FileSetPath(filesetpath, fileset, tablespace);
|
||||
PathNameCreateTemporaryDir(tempdirpath, filesetpath);
|
||||
file = PathNameCreateTemporaryFile(path, true);
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
/*
|
||||
* Open a file that was created with FileSetCreate() */
|
||||
File
|
||||
FileSetOpen(FileSet *fileset, const char *name, int mode)
|
||||
{
|
||||
char path[MAXPGPATH];
|
||||
File file;
|
||||
|
||||
FilePath(path, fileset, name);
|
||||
file = PathNameOpenTemporaryFile(path, mode);
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
/*
|
||||
* Delete a file that was created with FileSetCreate().
|
||||
*
|
||||
* Return true if the file existed, false if didn't.
|
||||
*/
|
||||
bool
|
||||
FileSetDelete(FileSet *fileset, const char *name,
|
||||
bool error_on_failure)
|
||||
{
|
||||
char path[MAXPGPATH];
|
||||
|
||||
FilePath(path, fileset, name);
|
||||
|
||||
return PathNameDeleteTemporaryFile(path, error_on_failure);
|
||||
}
|
||||
|
||||
/*
|
||||
* Delete all files in the set.
|
||||
*/
|
||||
void
|
||||
FileSetDeleteAll(FileSet *fileset)
|
||||
{
|
||||
char dirpath[MAXPGPATH];
|
||||
int i;
|
||||
|
||||
/*
|
||||
* Delete the directory we created in each tablespace. Doesn't fail
|
||||
* because we use this in error cleanup paths, but can generate LOG
|
||||
* message on IO error.
|
||||
*/
|
||||
for (i = 0; i < fileset->ntablespaces; ++i)
|
||||
{
|
||||
FileSetPath(dirpath, fileset, fileset->tablespaces[i]);
|
||||
PathNameDeleteTemporaryDir(dirpath);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Build the path for the directory holding the files backing a FileSet in a
|
||||
* given tablespace.
|
||||
*/
|
||||
static void
|
||||
FileSetPath(char *path, FileSet *fileset, Oid tablespace)
|
||||
{
|
||||
char tempdirpath[MAXPGPATH];
|
||||
|
||||
TempTablespacePath(tempdirpath, tablespace);
|
||||
snprintf(path, MAXPGPATH, "%s/%s%lu.%u.fileset",
|
||||
tempdirpath, PG_TEMP_FILE_PREFIX,
|
||||
(unsigned long) fileset->creator_pid, fileset->number);
|
||||
}
|
||||
|
||||
/*
|
||||
* Sorting has to determine which tablespace a given temporary file belongs in.
|
||||
*/
|
||||
static Oid
|
||||
ChooseTablespace(const FileSet *fileset, const char *name)
|
||||
{
|
||||
uint32 hash = hash_any((const unsigned char *) name, strlen(name));
|
||||
|
||||
return fileset->tablespaces[hash % fileset->ntablespaces];
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute the full path of a file in a FileSet.
|
||||
*/
|
||||
static void
|
||||
FilePath(char *path, FileSet *fileset, const char *name)
|
||||
{
|
||||
char dirpath[MAXPGPATH];
|
||||
|
||||
FileSetPath(dirpath, fileset, ChooseTablespace(fileset, name));
|
||||
snprintf(path, MAXPGPATH, "%s/%s", dirpath, name);
|
||||
}
|
@@ -13,10 +13,6 @@
|
||||
* files can be discovered by name, and a shared ownership semantics so that
|
||||
* shared files survive until the last user detaches.
|
||||
*
|
||||
* SharedFileSets can be used by backends when the temporary files need to be
|
||||
* opened/closed multiple times and the underlying files need to survive across
|
||||
* transactions.
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
@@ -33,13 +29,7 @@
|
||||
#include "storage/sharedfileset.h"
|
||||
#include "utils/builtins.h"
|
||||
|
||||
static List *filesetlist = NIL;
|
||||
|
||||
static void SharedFileSetOnDetach(dsm_segment *segment, Datum datum);
|
||||
static void SharedFileSetDeleteOnProcExit(int status, Datum arg);
|
||||
static void SharedFileSetPath(char *path, SharedFileSet *fileset, Oid tablespace);
|
||||
static void SharedFilePath(char *path, SharedFileSet *fileset, const char *name);
|
||||
static Oid ChooseTablespace(const SharedFileSet *fileset, const char *name);
|
||||
|
||||
/*
|
||||
* Initialize a space for temporary files that can be opened by other backends.
|
||||
@@ -47,77 +37,22 @@ static Oid ChooseTablespace(const SharedFileSet *fileset, const char *name);
|
||||
* SharedFileSet with 'seg'. Any contained files will be deleted when the
|
||||
* last backend detaches.
|
||||
*
|
||||
* We can also use this interface if the temporary files are used only by
|
||||
* single backend but the files need to be opened and closed multiple times
|
||||
* and also the underlying files need to survive across transactions. For
|
||||
* such cases, dsm segment 'seg' should be passed as NULL. Callers are
|
||||
* expected to explicitly remove such files by using SharedFileSetDelete/
|
||||
* SharedFileSetDeleteAll or we remove such files on proc exit.
|
||||
*
|
||||
* Files will be distributed over the tablespaces configured in
|
||||
* temp_tablespaces.
|
||||
*
|
||||
* Under the covers the set is one or more directories which will eventually
|
||||
* be deleted.
|
||||
*/
|
||||
void
|
||||
SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg)
|
||||
{
|
||||
static uint32 counter = 0;
|
||||
|
||||
/* Initialize the shared fileset specific members. */
|
||||
SpinLockInit(&fileset->mutex);
|
||||
fileset->refcnt = 1;
|
||||
fileset->creator_pid = MyProcPid;
|
||||
fileset->number = counter;
|
||||
counter = (counter + 1) % INT_MAX;
|
||||
|
||||
/* Capture the tablespace OIDs so that all backends agree on them. */
|
||||
PrepareTempTablespaces();
|
||||
fileset->ntablespaces =
|
||||
GetTempTablespaces(&fileset->tablespaces[0],
|
||||
lengthof(fileset->tablespaces));
|
||||
if (fileset->ntablespaces == 0)
|
||||
{
|
||||
/* If the GUC is empty, use current database's default tablespace */
|
||||
fileset->tablespaces[0] = MyDatabaseTableSpace;
|
||||
fileset->ntablespaces = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int i;
|
||||
|
||||
/*
|
||||
* An entry of InvalidOid means use the default tablespace for the
|
||||
* current database. Replace that now, to be sure that all users of
|
||||
* the SharedFileSet agree on what to do.
|
||||
*/
|
||||
for (i = 0; i < fileset->ntablespaces; i++)
|
||||
{
|
||||
if (fileset->tablespaces[i] == InvalidOid)
|
||||
fileset->tablespaces[i] = MyDatabaseTableSpace;
|
||||
}
|
||||
}
|
||||
/* Initialize the fileset. */
|
||||
FileSetInit(&fileset->fs);
|
||||
|
||||
/* Register our cleanup callback. */
|
||||
if (seg)
|
||||
on_dsm_detach(seg, SharedFileSetOnDetach, PointerGetDatum(fileset));
|
||||
else
|
||||
{
|
||||
static bool registered_cleanup = false;
|
||||
|
||||
if (!registered_cleanup)
|
||||
{
|
||||
/*
|
||||
* We must not have registered any fileset before registering the
|
||||
* fileset clean up.
|
||||
*/
|
||||
Assert(filesetlist == NIL);
|
||||
on_proc_exit(SharedFileSetDeleteOnProcExit, 0);
|
||||
registered_cleanup = true;
|
||||
}
|
||||
|
||||
filesetlist = lcons((void *) fileset, filesetlist);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -147,87 +82,13 @@ SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg)
|
||||
on_dsm_detach(seg, SharedFileSetOnDetach, PointerGetDatum(fileset));
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a new file in the given set.
|
||||
*/
|
||||
File
|
||||
SharedFileSetCreate(SharedFileSet *fileset, const char *name)
|
||||
{
|
||||
char path[MAXPGPATH];
|
||||
File file;
|
||||
|
||||
SharedFilePath(path, fileset, name);
|
||||
file = PathNameCreateTemporaryFile(path, false);
|
||||
|
||||
/* If we failed, see if we need to create the directory on demand. */
|
||||
if (file <= 0)
|
||||
{
|
||||
char tempdirpath[MAXPGPATH];
|
||||
char filesetpath[MAXPGPATH];
|
||||
Oid tablespace = ChooseTablespace(fileset, name);
|
||||
|
||||
TempTablespacePath(tempdirpath, tablespace);
|
||||
SharedFileSetPath(filesetpath, fileset, tablespace);
|
||||
PathNameCreateTemporaryDir(tempdirpath, filesetpath);
|
||||
file = PathNameCreateTemporaryFile(path, true);
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
/*
|
||||
* Open a file that was created with SharedFileSetCreate(), possibly in
|
||||
* another backend.
|
||||
*/
|
||||
File
|
||||
SharedFileSetOpen(SharedFileSet *fileset, const char *name, int mode)
|
||||
{
|
||||
char path[MAXPGPATH];
|
||||
File file;
|
||||
|
||||
SharedFilePath(path, fileset, name);
|
||||
file = PathNameOpenTemporaryFile(path, mode);
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
/*
|
||||
* Delete a file that was created with SharedFileSetCreate().
|
||||
* Return true if the file existed, false if didn't.
|
||||
*/
|
||||
bool
|
||||
SharedFileSetDelete(SharedFileSet *fileset, const char *name,
|
||||
bool error_on_failure)
|
||||
{
|
||||
char path[MAXPGPATH];
|
||||
|
||||
SharedFilePath(path, fileset, name);
|
||||
|
||||
return PathNameDeleteTemporaryFile(path, error_on_failure);
|
||||
}
|
||||
|
||||
/*
|
||||
* Delete all files in the set.
|
||||
*/
|
||||
void
|
||||
SharedFileSetDeleteAll(SharedFileSet *fileset)
|
||||
{
|
||||
char dirpath[MAXPGPATH];
|
||||
int i;
|
||||
|
||||
/*
|
||||
* Delete the directory we created in each tablespace. Doesn't fail
|
||||
* because we use this in error cleanup paths, but can generate LOG
|
||||
* message on IO error.
|
||||
*/
|
||||
for (i = 0; i < fileset->ntablespaces; ++i)
|
||||
{
|
||||
SharedFileSetPath(dirpath, fileset, fileset->tablespaces[i]);
|
||||
PathNameDeleteTemporaryDir(dirpath);
|
||||
}
|
||||
|
||||
/* Unregister the shared fileset */
|
||||
SharedFileSetUnregister(fileset);
|
||||
FileSetDeleteAll(&fileset->fs);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -255,100 +116,5 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum)
|
||||
* this function so we can safely access its data.
|
||||
*/
|
||||
if (unlink_all)
|
||||
SharedFileSetDeleteAll(fileset);
|
||||
}
|
||||
|
||||
/*
|
||||
* Callback function that will be invoked on the process exit. This will
|
||||
* process the list of all the registered sharedfilesets and delete the
|
||||
* underlying files.
|
||||
*/
|
||||
static void
|
||||
SharedFileSetDeleteOnProcExit(int status, Datum arg)
|
||||
{
|
||||
/*
|
||||
* Remove all the pending shared fileset entries. We don't use foreach()
|
||||
* here because SharedFileSetDeleteAll will remove the current element in
|
||||
* filesetlist. Though we have used foreach_delete_current() to remove the
|
||||
* element from filesetlist it could only fix up the state of one of the
|
||||
* loops, see SharedFileSetUnregister.
|
||||
*/
|
||||
while (list_length(filesetlist) > 0)
|
||||
{
|
||||
SharedFileSet *fileset = (SharedFileSet *) linitial(filesetlist);
|
||||
|
||||
SharedFileSetDeleteAll(fileset);
|
||||
}
|
||||
|
||||
filesetlist = NIL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Unregister the shared fileset entry registered for cleanup on proc exit.
|
||||
*/
|
||||
void
|
||||
SharedFileSetUnregister(SharedFileSet *input_fileset)
|
||||
{
|
||||
ListCell *l;
|
||||
|
||||
/*
|
||||
* If the caller is following the dsm based cleanup then we don't maintain
|
||||
* the filesetlist so return.
|
||||
*/
|
||||
if (filesetlist == NIL)
|
||||
return;
|
||||
|
||||
foreach(l, filesetlist)
|
||||
{
|
||||
SharedFileSet *fileset = (SharedFileSet *) lfirst(l);
|
||||
|
||||
/* Remove the entry from the list */
|
||||
if (input_fileset == fileset)
|
||||
{
|
||||
filesetlist = foreach_delete_current(filesetlist, l);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* Should have found a match */
|
||||
Assert(false);
|
||||
}
|
||||
|
||||
/*
|
||||
* Build the path for the directory holding the files backing a SharedFileSet
|
||||
* in a given tablespace.
|
||||
*/
|
||||
static void
|
||||
SharedFileSetPath(char *path, SharedFileSet *fileset, Oid tablespace)
|
||||
{
|
||||
char tempdirpath[MAXPGPATH];
|
||||
|
||||
TempTablespacePath(tempdirpath, tablespace);
|
||||
snprintf(path, MAXPGPATH, "%s/%s%lu.%u.sharedfileset",
|
||||
tempdirpath, PG_TEMP_FILE_PREFIX,
|
||||
(unsigned long) fileset->creator_pid, fileset->number);
|
||||
}
|
||||
|
||||
/*
|
||||
* Sorting hat to determine which tablespace a given shared temporary file
|
||||
* belongs in.
|
||||
*/
|
||||
static Oid
|
||||
ChooseTablespace(const SharedFileSet *fileset, const char *name)
|
||||
{
|
||||
uint32 hash = hash_any((const unsigned char *) name, strlen(name));
|
||||
|
||||
return fileset->tablespaces[hash % fileset->ntablespaces];
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute the full path of a file in a SharedFileSet.
|
||||
*/
|
||||
static void
|
||||
SharedFilePath(char *path, SharedFileSet *fileset, const char *name)
|
||||
{
|
||||
char dirpath[MAXPGPATH];
|
||||
|
||||
SharedFileSetPath(dirpath, fileset, ChooseTablespace(fileset, name));
|
||||
snprintf(path, MAXPGPATH, "%s/%s", dirpath, name);
|
||||
FileSetDeleteAll(&fileset->fs);
|
||||
}
|
||||
|
Reference in New Issue
Block a user