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

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

Referenced by FilePath(), and FileSetCreate().

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

◆ FilePath()

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

Definition at line 199 of file fileset.c.

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

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

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

◆ FileSetCreate()

File FileSetCreate ( FileSet fileset,
const char *  name 
)

Definition at line 94 of file fileset.c.

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

Referenced by MakeNewFileSetSegment().

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

◆ FileSetDelete()

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

Definition at line 138 of file fileset.c.

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

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

140 {
141  char path[MAXPGPATH];
142 
143  FilePath(path, fileset, name);
144 
145  return PathNameDeleteTemporaryFile(path, error_on_failure);
146 }
bool PathNameDeleteTemporaryFile(const char *path, bool error_on_failure)
Definition: fd.c:1913
#define MAXPGPATH
const char * name
Definition: encode.c:561
static void FilePath(char *path, FileSet *fileset, const char *name)
Definition: fileset.c:199

◆ FileSetDeleteAll()

void FileSetDeleteAll ( FileSet fileset)

Definition at line 152 of file fileset.c.

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

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

153 {
154  char dirpath[MAXPGPATH];
155  int i;
156 
157  /*
158  * Delete the directory we created in each tablespace. Doesn't fail
159  * because we use this in error cleanup paths, but can generate LOG
160  * message on IO error.
161  */
162  for (i = 0; i < fileset->ntablespaces; ++i)
163  {
164  FileSetPath(dirpath, fileset, fileset->tablespaces[i]);
166  }
167 }
void PathNameDeleteTemporaryDir(const char *dirname)
Definition: fd.c:1672
static void FileSetPath(char *path, FileSet *fileset, Oid tablespace)
Definition: fileset.c:174
#define MAXPGPATH
Oid tablespaces[8]
Definition: fileset.h:27
int ntablespaces
Definition: fileset.h:26
int i

◆ FileSetInit()

void FileSetInit ( FileSet fileset)

Definition at line 54 of file fileset.c.

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

Referenced by apply_handle_stream_start(), and SharedFileSetInit().

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

◆ FileSetOpen()

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

Definition at line 121 of file fileset.c.

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

Referenced by BufFileOpenFileSet().

122 {
123  char path[MAXPGPATH];
124  File file;
125 
126  FilePath(path, fileset, name);
127  file = PathNameOpenTemporaryFile(path, mode);
128 
129  return file;
130 }
static PgChecksumMode mode
Definition: pg_checksums.c:65
#define MAXPGPATH
File PathNameOpenTemporaryFile(const char *path, int mode)
Definition: fd.c:1882
const char * name
Definition: encode.c:561
static void FilePath(char *path, FileSet *fileset, const char *name)
Definition: fileset.c:199
int File
Definition: fd.h:54

◆ FileSetPath()

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

Definition at line 174 of file fileset.c.

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

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

175 {
176  char tempdirpath[MAXPGPATH];
177 
178  TempTablespacePath(tempdirpath, tablespace);
179  snprintf(path, MAXPGPATH, "%s/%s%lu.%u.fileset",
180  tempdirpath, PG_TEMP_FILE_PREFIX,
181  (unsigned long) fileset->creator_pid, fileset->number);
182 }
pid_t creator_pid
Definition: fileset.h:24
uint32 number
Definition: fileset.h:25
void TempTablespacePath(char *path, Oid tablespace)
Definition: fd.c:1760
#define PG_TEMP_FILE_PREFIX
Definition: pg_checksums.c:63
#define MAXPGPATH
char * tablespace
Definition: pgbench.c:226
#define snprintf
Definition: port.h:217