PostgreSQL Source Code  git master
sharedfileset.h File Reference
#include "storage/dsm.h"
#include "storage/fd.h"
#include "storage/spin.h"
Include dependency graph for sharedfileset.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SharedFileSet
 

Typedefs

typedef struct SharedFileSet SharedFileSet
 

Functions

void SharedFileSetInit (SharedFileSet *fileset, dsm_segment *seg)
 
void SharedFileSetAttach (SharedFileSet *fileset, dsm_segment *seg)
 
File SharedFileSetCreate (SharedFileSet *fileset, const char *name)
 
File SharedFileSetOpen (SharedFileSet *fileset, const char *name, int mode)
 
bool SharedFileSetDelete (SharedFileSet *fileset, const char *name, bool error_on_failure)
 
void SharedFileSetDeleteAll (SharedFileSet *fileset)
 
void SharedFileSetUnregister (SharedFileSet *input_fileset)
 

Typedef Documentation

◆ SharedFileSet

typedef struct SharedFileSet SharedFileSet

Function Documentation

◆ SharedFileSetAttach()

void SharedFileSetAttach ( SharedFileSet fileset,
dsm_segment seg 
)

Definition at line 127 of file sharedfileset.c.

References ereport, errcode(), errmsg(), ERROR, SharedFileSet::mutex, on_dsm_detach(), PointerGetDatum, SharedFileSet::refcnt, SharedFileSetOnDetach(), SpinLockAcquire, SpinLockRelease, and success.

Referenced by ExecHashJoinInitializeWorker(), and tuplesort_attach_shared().

128 {
129  bool success;
130 
131  SpinLockAcquire(&fileset->mutex);
132  if (fileset->refcnt == 0)
133  success = false;
134  else
135  {
136  ++fileset->refcnt;
137  success = true;
138  }
139  SpinLockRelease(&fileset->mutex);
140 
141  if (!success)
142  ereport(ERROR,
143  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
144  errmsg("could not attach to a SharedFileSet that is already destroyed")));
145 
146  /* Register our cleanup callback. */
148 }
#define PointerGetDatum(X)
Definition: postgres.h:556
int errcode(int sqlerrcode)
Definition: elog.c:610
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition: dsm.c:1091
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define ERROR
Definition: elog.h:43
#define SpinLockRelease(lock)
Definition: spin.h:64
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
static bool success
Definition: initdb.c:162
static void SharedFileSetOnDetach(dsm_segment *segment, Datum datum)

◆ SharedFileSetCreate()

File SharedFileSetCreate ( SharedFileSet fileset,
const char *  name 
)

Definition at line 154 of file sharedfileset.c.

References ChooseTablespace(), MAXPGPATH, PathNameCreateTemporaryDir(), PathNameCreateTemporaryFile(), SharedFilePath(), SharedFileSetPath(), tablespace, and TempTablespacePath().

Referenced by MakeNewSharedSegment().

155 {
156  char path[MAXPGPATH];
157  File file;
158 
159  SharedFilePath(path, fileset, name);
160  file = PathNameCreateTemporaryFile(path, false);
161 
162  /* If we failed, see if we need to create the directory on demand. */
163  if (file <= 0)
164  {
165  char tempdirpath[MAXPGPATH];
166  char filesetpath[MAXPGPATH];
167  Oid tablespace = ChooseTablespace(fileset, name);
168 
169  TempTablespacePath(tempdirpath, tablespace);
170  SharedFileSetPath(filesetpath, fileset, tablespace);
171  PathNameCreateTemporaryDir(tempdirpath, filesetpath);
172  file = PathNameCreateTemporaryFile(path, true);
173  }
174 
175  return file;
176 }
static void SharedFileSetPath(char *path, SharedFileSet *fileset, Oid tablespace)
File PathNameCreateTemporaryFile(const char *path, bool error_on_failure)
Definition: fd.c:1713
void TempTablespacePath(char *path, Oid tablespace)
Definition: fd.c:1631
unsigned int Oid
Definition: postgres_ext.h:31
#define MAXPGPATH
char * tablespace
Definition: pgbench.c:189
const char * name
Definition: encode.c:561
static void SharedFilePath(char *path, SharedFileSet *fileset, const char *name)
static Oid ChooseTablespace(const SharedFileSet *fileset, const char *name)
void PathNameCreateTemporaryDir(const char *basedir, const char *directory)
Definition: fd.c:1514
int File
Definition: fd.h:49

◆ SharedFileSetDelete()

bool SharedFileSetDelete ( SharedFileSet fileset,
const char *  name,
bool  error_on_failure 
)

Definition at line 199 of file sharedfileset.c.

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

Referenced by BufFileDeleteShared(), BufFileTruncateShared(), and MakeNewSharedSegment().

201 {
202  char path[MAXPGPATH];
203 
204  SharedFilePath(path, fileset, name);
205 
206  return PathNameDeleteTemporaryFile(path, error_on_failure);
207 }
bool PathNameDeleteTemporaryFile(const char *path, bool error_on_failure)
Definition: fd.c:1780
#define MAXPGPATH
const char * name
Definition: encode.c:561
static void SharedFilePath(char *path, SharedFileSet *fileset, const char *name)

◆ SharedFileSetDeleteAll()

void SharedFileSetDeleteAll ( SharedFileSet fileset)

Definition at line 213 of file sharedfileset.c.

References i, MAXPGPATH, SharedFileSet::ntablespaces, PathNameDeleteTemporaryDir(), SharedFileSetPath(), SharedFileSetUnregister(), and SharedFileSet::tablespaces.

Referenced by ExecHashJoinReInitializeDSM(), SharedFileSetDeleteOnProcExit(), SharedFileSetOnDetach(), stream_cleanup_files(), and subxact_info_write().

214 {
215  char dirpath[MAXPGPATH];
216  int i;
217 
218  /*
219  * Delete the directory we created in each tablespace. Doesn't fail
220  * because we use this in error cleanup paths, but can generate LOG
221  * message on IO error.
222  */
223  for (i = 0; i < fileset->ntablespaces; ++i)
224  {
225  SharedFileSetPath(dirpath, fileset, fileset->tablespaces[i]);
227  }
228 
229  /* Unregister the shared fileset */
230  SharedFileSetUnregister(fileset);
231 }
static void SharedFileSetPath(char *path, SharedFileSet *fileset, Oid tablespace)
void SharedFileSetUnregister(SharedFileSet *input_fileset)
void PathNameDeleteTemporaryDir(const char *dirname)
Definition: fd.c:1545
#define MAXPGPATH
Oid tablespaces[8]
Definition: sharedfileset.h:32
int i

◆ SharedFileSetInit()

void SharedFileSetInit ( SharedFileSet fileset,
dsm_segment seg 
)

Definition at line 64 of file sharedfileset.c.

References Assert, SharedFileSet::creator_pid, GetTempTablespaces(), i, InvalidOid, lcons(), lengthof, SharedFileSet::mutex, MyDatabaseTableSpace, MyProcPid, NIL, SharedFileSet::ntablespaces, SharedFileSet::number, on_dsm_detach(), on_proc_exit(), PointerGetDatum, PrepareTempTablespaces(), SharedFileSet::refcnt, SharedFileSetDeleteOnProcExit(), SharedFileSetOnDetach(), SpinLockInit, and SharedFileSet::tablespaces.

Referenced by ExecHashJoinInitializeDSM(), stream_open_file(), subxact_info_write(), and tuplesort_initialize_shared().

65 {
66  static uint32 counter = 0;
67 
68  SpinLockInit(&fileset->mutex);
69  fileset->refcnt = 1;
70  fileset->creator_pid = MyProcPid;
71  fileset->number = counter;
72  counter = (counter + 1) % INT_MAX;
73 
74  /* Capture the tablespace OIDs so that all backends agree on them. */
76  fileset->ntablespaces =
77  GetTempTablespaces(&fileset->tablespaces[0],
78  lengthof(fileset->tablespaces));
79  if (fileset->ntablespaces == 0)
80  {
81  /* If the GUC is empty, use current database's default tablespace */
82  fileset->tablespaces[0] = MyDatabaseTableSpace;
83  fileset->ntablespaces = 1;
84  }
85  else
86  {
87  int i;
88 
89  /*
90  * An entry of InvalidOid means use the default tablespace for the
91  * current database. Replace that now, to be sure that all users of
92  * the SharedFileSet agree on what to do.
93  */
94  for (i = 0; i < fileset->ntablespaces; i++)
95  {
96  if (fileset->tablespaces[i] == InvalidOid)
98  }
99  }
100 
101  /* Register our cleanup callback. */
102  if (seg)
104  else
105  {
106  static bool registered_cleanup = false;
107 
108  if (!registered_cleanup)
109  {
110  /*
111  * We must not have registered any fileset before registering the
112  * fileset clean up.
113  */
114  Assert(filesetlist == NIL);
116  registered_cleanup = true;
117  }
118 
119  filesetlist = lcons((void *) fileset, filesetlist);
120  }
121 }
#define NIL
Definition: pg_list.h:65
int MyProcPid
Definition: globals.c:40
void on_proc_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:305
int GetTempTablespaces(Oid *tableSpaces, int numSpaces)
Definition: fd.c:2832
#define PointerGetDatum(X)
Definition: postgres.h:556
static void SharedFileSetDeleteOnProcExit(int status, Datum arg)
#define SpinLockInit(lock)
Definition: spin.h:60
#define lengthof(array)
Definition: c.h:675
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition: dsm.c:1091
Oid MyDatabaseTableSpace
Definition: globals.c:87
void PrepareTempTablespaces(void)
Definition: tablespace.c:1326
static List * filesetlist
Definition: sharedfileset.c:36
unsigned int uint32
Definition: c.h:374
#define InvalidOid
Definition: postgres_ext.h:36
List * lcons(void *datum, List *list)
Definition: list.c:453
#define Assert(condition)
Definition: c.h:745
Oid tablespaces[8]
Definition: sharedfileset.h:32
int i
static void SharedFileSetOnDetach(dsm_segment *segment, Datum datum)

◆ SharedFileSetOpen()

File SharedFileSetOpen ( SharedFileSet fileset,
const char *  name,
int  mode 
)

Definition at line 183 of file sharedfileset.c.

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

Referenced by BufFileOpenShared().

184 {
185  char path[MAXPGPATH];
186  File file;
187 
188  SharedFilePath(path, fileset, name);
189  file = PathNameOpenTemporaryFile(path, mode);
190 
191  return file;
192 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
#define MAXPGPATH
File PathNameOpenTemporaryFile(const char *path, int mode)
Definition: fd.c:1751
const char * name
Definition: encode.c:561
static void SharedFilePath(char *path, SharedFileSet *fileset, const char *name)
int File
Definition: fd.h:49

◆ SharedFileSetUnregister()

void SharedFileSetUnregister ( SharedFileSet input_fileset)

Definition at line 290 of file sharedfileset.c.

References Assert, foreach_delete_current, lfirst, and NIL.

Referenced by SharedFileSetDeleteAll().

291 {
292  ListCell *l;
293 
294  /*
295  * If the caller is following the dsm based cleanup then we don't maintain
296  * the filesetlist so return.
297  */
298  if (filesetlist == NIL)
299  return;
300 
301  foreach(l, filesetlist)
302  {
303  SharedFileSet *fileset = (SharedFileSet *) lfirst(l);
304 
305  /* Remove the entry from the list */
306  if (input_fileset == fileset)
307  {
309  return;
310  }
311  }
312 
313  /* Should have found a match */
314  Assert(false);
315 }
#define NIL
Definition: pg_list.h:65
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:368
static List * filesetlist
Definition: sharedfileset.c:36
#define Assert(condition)
Definition: c.h:745
#define lfirst(lc)
Definition: pg_list.h:190