PostgreSQL Source Code git master
fileset.c File Reference
#include "postgres.h"
#include <limits.h>
#include "commands/tablespace.h"
#include "common/file_utils.h"
#include "common/hashfn.h"
#include "miscadmin.h"
#include "storage/fileset.h"
Include dependency graph for fileset.c:

Go to the source code of this file.

Functions

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)
 
void FileSetInit (FileSet *fileset)
 
File FileSetCreate (FileSet *fileset, const char *name)
 
File FileSetOpen (FileSet *fileset, const char *name, int mode)
 
bool FileSetDelete (FileSet *fileset, const char *name, bool error_on_failure)
 
void FileSetDeleteAll (FileSet *fileset)
 

Function Documentation

◆ ChooseTablespace()

static Oid ChooseTablespace ( const FileSet fileset,
const char *  name 
)
static

Definition at line 186 of file fileset.c.

187{
188 uint32 hash = hash_any((const unsigned char *) name, strlen(name));
189
190 return fileset->tablespaces[hash % fileset->ntablespaces];
191}
uint32_t uint32
Definition: c.h:502
static Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.h:31
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:715
Oid tablespaces[8]
Definition: fileset.h:27
int ntablespaces
Definition: fileset.h:26
const char * name

References hash(), hash_any(), name, FileSet::ntablespaces, and FileSet::tablespaces.

Referenced by FilePath(), and FileSetCreate().

◆ FilePath()

static void FilePath ( char *  path,
FileSet fileset,
const char *  name 
)
static

Definition at line 197 of file fileset.c.

198{
199 char dirpath[MAXPGPATH];
200
201 FileSetPath(dirpath, fileset, ChooseTablespace(fileset, name));
202 snprintf(path, MAXPGPATH, "%s/%s", dirpath, name);
203}
static void FileSetPath(char *path, FileSet *fileset, Oid tablespace)
Definition: fileset.c:172
static Oid ChooseTablespace(const FileSet *fileset, const char *name)
Definition: fileset.c:186
#define MAXPGPATH
#define snprintf
Definition: port.h:239

References ChooseTablespace(), FileSetPath(), MAXPGPATH, name, and snprintf.

Referenced by FileSetCreate(), FileSetDelete(), and FileSetOpen().

◆ FileSetCreate()

File FileSetCreate ( FileSet fileset,
const char *  name 
)

Definition at line 92 of file fileset.c.

93{
94 char path[MAXPGPATH];
95 File file;
96
97 FilePath(path, fileset, name);
98 file = PathNameCreateTemporaryFile(path, false);
99
100 /* If we failed, see if we need to create the directory on demand. */
101 if (file <= 0)
102 {
103 char tempdirpath[MAXPGPATH];
104 char filesetpath[MAXPGPATH];
106
107 TempTablespacePath(tempdirpath, tablespace);
108 FileSetPath(filesetpath, fileset, tablespace);
109 PathNameCreateTemporaryDir(tempdirpath, filesetpath);
110 file = PathNameCreateTemporaryFile(path, true);
111 }
112
113 return file;
114}
File PathNameCreateTemporaryFile(const char *path, bool error_on_failure)
Definition: fd.c:1860
void PathNameCreateTemporaryDir(const char *basedir, const char *directory)
Definition: fd.c:1659
void TempTablespacePath(char *path, Oid tablespace)
Definition: fd.c:1778
int File
Definition: fd.h:51
static void FilePath(char *path, FileSet *fileset, const char *name)
Definition: fileset.c:197
static char * tablespace
Definition: pgbench.c:217
unsigned int Oid
Definition: postgres_ext.h:32

References ChooseTablespace(), FilePath(), FileSetPath(), MAXPGPATH, name, PathNameCreateTemporaryDir(), PathNameCreateTemporaryFile(), tablespace, and TempTablespacePath().

Referenced by MakeNewFileSetSegment().

◆ FileSetDelete()

bool FileSetDelete ( FileSet fileset,
const char *  name,
bool  error_on_failure 
)

Definition at line 136 of file fileset.c.

138{
139 char path[MAXPGPATH];
140
141 FilePath(path, fileset, name);
142
143 return PathNameDeleteTemporaryFile(path, error_on_failure);
144}
bool PathNameDeleteTemporaryFile(const char *path, bool error_on_failure)
Definition: fd.c:1931

References FilePath(), MAXPGPATH, name, and PathNameDeleteTemporaryFile().

Referenced by BufFileDeleteFileSet(), BufFileTruncateFileSet(), and MakeNewFileSetSegment().

◆ FileSetDeleteAll()

void FileSetDeleteAll ( FileSet fileset)

Definition at line 150 of file fileset.c.

151{
152 char dirpath[MAXPGPATH];
153 int i;
154
155 /*
156 * Delete the directory we created in each tablespace. Doesn't fail
157 * because we use this in error cleanup paths, but can generate LOG
158 * message on IO error.
159 */
160 for (i = 0; i < fileset->ntablespaces; ++i)
161 {
162 FileSetPath(dirpath, fileset, fileset->tablespaces[i]);
164 }
165}
void PathNameDeleteTemporaryDir(const char *dirname)
Definition: fd.c:1690
int i
Definition: isn.c:74

References FileSetPath(), i, MAXPGPATH, FileSet::ntablespaces, PathNameDeleteTemporaryDir(), and FileSet::tablespaces.

Referenced by logicalrep_worker_onexit(), SharedFileSetDeleteAll(), and SharedFileSetOnDetach().

◆ FileSetInit()

void FileSetInit ( FileSet fileset)

Definition at line 52 of file fileset.c.

53{
54 static uint32 counter = 0;
55
56 fileset->creator_pid = MyProcPid;
57 fileset->number = counter;
58 counter = (counter + 1) % INT_MAX;
59
60 /* Capture the tablespace OIDs so that all backends agree on them. */
62 fileset->ntablespaces =
64 lengthof(fileset->tablespaces));
65 if (fileset->ntablespaces == 0)
66 {
67 /* If the GUC is empty, use current database's default tablespace */
69 fileset->ntablespaces = 1;
70 }
71 else
72 {
73 int i;
74
75 /*
76 * An entry of InvalidOid means use the default tablespace for the
77 * current database. Replace that now, to be sure that all users of
78 * the FileSet agree on what to do.
79 */
80 for (i = 0; i < fileset->ntablespaces; i++)
81 {
82 if (fileset->tablespaces[i] == InvalidOid)
84 }
85 }
86}
void PrepareTempTablespaces(void)
Definition: tablespace.c:1331
#define lengthof(array)
Definition: c.h:759
int GetTempTablespaces(Oid *tableSpaces, int numSpaces)
Definition: fd.c:3115
int MyProcPid
Definition: globals.c:46
Oid MyDatabaseTableSpace
Definition: globals.c:95
#define InvalidOid
Definition: postgres_ext.h:37
pid_t creator_pid
Definition: fileset.h:24
uint32 number
Definition: fileset.h:25

References FileSet::creator_pid, GetTempTablespaces(), i, InvalidOid, lengthof, MyDatabaseTableSpace, MyProcPid, FileSet::ntablespaces, FileSet::number, PrepareTempTablespaces(), and FileSet::tablespaces.

Referenced by SharedFileSetInit(), and stream_start_internal().

◆ FileSetOpen()

File FileSetOpen ( FileSet fileset,
const char *  name,
int  mode 
)

Definition at line 119 of file fileset.c.

120{
121 char path[MAXPGPATH];
122 File file;
123
124 FilePath(path, fileset, name);
125 file = PathNameOpenTemporaryFile(path, mode);
126
127 return file;
128}
File PathNameOpenTemporaryFile(const char *path, int mode)
Definition: fd.c:1900
static PgChecksumMode mode
Definition: pg_checksums.c:55

References FilePath(), MAXPGPATH, mode, name, and PathNameOpenTemporaryFile().

Referenced by BufFileOpenFileSet().

◆ FileSetPath()

static void FileSetPath ( char *  path,
FileSet fileset,
Oid  tablespace 
)
static

Definition at line 172 of file fileset.c.

173{
174 char tempdirpath[MAXPGPATH];
175
176 TempTablespacePath(tempdirpath, tablespace);
177 snprintf(path, MAXPGPATH, "%s/%s%lu.%u.fileset",
178 tempdirpath, PG_TEMP_FILE_PREFIX,
179 (unsigned long) fileset->creator_pid, fileset->number);
180}
#define PG_TEMP_FILE_PREFIX
Definition: file_utils.h:63

References FileSet::creator_pid, MAXPGPATH, FileSet::number, PG_TEMP_FILE_PREFIX, snprintf, tablespace, and TempTablespacePath().

Referenced by FilePath(), FileSetCreate(), and FileSetDeleteAll().