PostgreSQL Source Code  git master
fileset.c File Reference
#include "postgres.h"
#include <limits.h>
#include "catalog/pg_tablespace.h"
#include "commands/tablespace.h"
#include "common/file_utils.h"
#include "common/hashfn.h"
#include "miscadmin.h"
#include "storage/ipc.h"
#include "storage/fileset.h"
#include "utils/builtins.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 189 of file fileset.c.

190 {
191  uint32 hash = hash_any((const unsigned char *) name, strlen(name));
192 
193  return fileset->tablespaces[hash % fileset->ntablespaces];
194 }
unsigned int uint32
Definition: c.h:495
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 200 of file fileset.c.

201 {
202  char dirpath[MAXPGPATH];
203 
204  FileSetPath(dirpath, fileset, ChooseTablespace(fileset, name));
205  snprintf(path, MAXPGPATH, "%s/%s", dirpath, name);
206 }
static void FileSetPath(char *path, FileSet *fileset, Oid tablespace)
Definition: fileset.c:175
static Oid ChooseTablespace(const FileSet *fileset, const char *name)
Definition: fileset.c:189
#define MAXPGPATH
#define snprintf
Definition: port.h:238

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

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

◆ FileSetCreate()

File FileSetCreate ( FileSet fileset,
const char *  name 
)

Definition at line 95 of file fileset.c.

96 {
97  char path[MAXPGPATH];
98  File file;
99 
100  FilePath(path, fileset, name);
101  file = PathNameCreateTemporaryFile(path, false);
102 
103  /* If we failed, see if we need to create the directory on demand. */
104  if (file <= 0)
105  {
106  char tempdirpath[MAXPGPATH];
107  char filesetpath[MAXPGPATH];
108  Oid tablespace = ChooseTablespace(fileset, name);
109 
110  TempTablespacePath(tempdirpath, tablespace);
111  FileSetPath(filesetpath, fileset, tablespace);
112  PathNameCreateTemporaryDir(tempdirpath, filesetpath);
113  file = PathNameCreateTemporaryFile(path, true);
114  }
115 
116  return file;
117 }
File PathNameCreateTemporaryFile(const char *path, bool error_on_failure)
Definition: fd.c:1838
void PathNameCreateTemporaryDir(const char *basedir, const char *directory)
Definition: fd.c:1637
void TempTablespacePath(char *path, Oid tablespace)
Definition: fd.c:1756
int File
Definition: fd.h:49
static void FilePath(char *path, FileSet *fileset, const char *name)
Definition: fileset.c:200
char * tablespace
Definition: pgbench.c:216
unsigned int Oid
Definition: postgres_ext.h:31

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 139 of file fileset.c.

141 {
142  char path[MAXPGPATH];
143 
144  FilePath(path, fileset, name);
145 
146  return PathNameDeleteTemporaryFile(path, error_on_failure);
147 }
bool PathNameDeleteTemporaryFile(const char *path, bool error_on_failure)
Definition: fd.c:1909

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

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

◆ FileSetDeleteAll()

void FileSetDeleteAll ( FileSet fileset)

Definition at line 153 of file fileset.c.

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

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 55 of file fileset.c.

56 {
57  static uint32 counter = 0;
58 
59  fileset->creator_pid = MyProcPid;
60  fileset->number = counter;
61  counter = (counter + 1) % INT_MAX;
62 
63  /* Capture the tablespace OIDs so that all backends agree on them. */
65  fileset->ntablespaces =
66  GetTempTablespaces(&fileset->tablespaces[0],
67  lengthof(fileset->tablespaces));
68  if (fileset->ntablespaces == 0)
69  {
70  /* If the GUC is empty, use current database's default tablespace */
71  fileset->tablespaces[0] = MyDatabaseTableSpace;
72  fileset->ntablespaces = 1;
73  }
74  else
75  {
76  int i;
77 
78  /*
79  * An entry of InvalidOid means use the default tablespace for the
80  * current database. Replace that now, to be sure that all users of
81  * the FileSet agree on what to do.
82  */
83  for (i = 0; i < fileset->ntablespaces; i++)
84  {
85  if (fileset->tablespaces[i] == InvalidOid)
87  }
88  }
89 }
void PrepareTempTablespaces(void)
Definition: tablespace.c:1337
#define lengthof(array)
Definition: c.h:777
int GetTempTablespaces(Oid *tableSpaces, int numSpaces)
Definition: fd.c:3063
int MyProcPid
Definition: globals.c:44
Oid MyDatabaseTableSpace
Definition: globals.c:91
#define InvalidOid
Definition: postgres_ext.h:36
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 122 of file fileset.c.

123 {
124  char path[MAXPGPATH];
125  File file;
126 
127  FilePath(path, fileset, name);
128  file = PathNameOpenTemporaryFile(path, mode);
129 
130  return file;
131 }
File PathNameOpenTemporaryFile(const char *path, int mode)
Definition: fd.c:1878
static PgChecksumMode mode
Definition: pg_checksums.c:56

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

Referenced by BufFileOpenFileSet().

◆ FileSetPath()

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

Definition at line 175 of file fileset.c.

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

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

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