PostgreSQL Source Code  git master
sharedfileset.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/dsm.h"
#include "storage/ipc.h"
#include "storage/sharedfileset.h"
#include "utils/builtins.h"
Include dependency graph for sharedfileset.c:

Go to the source code of this file.

Functions

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)
 
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)
 

Variables

static Listfilesetlist = NIL
 

Function Documentation

◆ ChooseTablespace()

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

Definition at line 337 of file sharedfileset.c.

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

Referenced by SharedFilePath(), and SharedFileSetCreate().

338 {
339  uint32 hash = hash_any((const unsigned char *) name, strlen(name));
340 
341  return fileset->tablespaces[hash % fileset->ntablespaces];
342 }
unsigned int uint32
Definition: c.h:429
static Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.h:31
Oid tablespaces[8]
Definition: sharedfileset.h:32
const char * name
Definition: encode.c:561
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:541

◆ SharedFilePath()

static void SharedFilePath ( char *  path,
SharedFileSet fileset,
const char *  name 
)
static

Definition at line 348 of file sharedfileset.c.

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

Referenced by SharedFileSetCreate(), SharedFileSetDelete(), and SharedFileSetOpen().

349 {
350  char dirpath[MAXPGPATH];
351 
352  SharedFileSetPath(dirpath, fileset, ChooseTablespace(fileset, name));
353  snprintf(path, MAXPGPATH, "%s/%s", dirpath, name);
354 }
static void SharedFileSetPath(char *path, SharedFileSet *fileset, Oid tablespace)
#define MAXPGPATH
const char * name
Definition: encode.c:561
static Oid ChooseTablespace(const SharedFileSet *fileset, const char *name)
#define snprintf
Definition: port.h:215

◆ 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:691
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:155
int errmsg(const char *fmt,...)
Definition: elog.c:902
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

◆ SharedFileSetDeleteOnProcExit()

static void SharedFileSetDeleteOnProcExit ( int  status,
Datum  arg 
)
static

Definition at line 267 of file sharedfileset.c.

References linitial, list_length(), NIL, and SharedFileSetDeleteAll().

Referenced by SharedFileSetInit().

268 {
269  /*
270  * Remove all the pending shared fileset entries. We don't use foreach() here
271  * because SharedFileSetDeleteAll will remove the current element in
272  * filesetlist. Though we have used foreach_delete_current() to remove the
273  * element from filesetlist it could only fix up the state of one of the
274  * loops, see SharedFileSetUnregister.
275  */
276  while (list_length(filesetlist) > 0)
277  {
279 
280  SharedFileSetDeleteAll(fileset);
281  }
282 
283  filesetlist = NIL;
284 }
#define NIL
Definition: pg_list.h:65
#define linitial(l)
Definition: pg_list.h:174
static List * filesetlist
Definition: sharedfileset.c:36
void SharedFileSetDeleteAll(SharedFileSet *fileset)
static int list_length(const List *l)
Definition: pg_list.h:149

◆ 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:730
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:429
#define InvalidOid
Definition: postgres_ext.h:36
List * lcons(void *datum, List *list)
Definition: list.c:453
#define Assert(condition)
Definition: c.h:800
Oid tablespaces[8]
Definition: sharedfileset.h:32
int i
static void SharedFileSetOnDetach(dsm_segment *segment, Datum datum)

◆ SharedFileSetOnDetach()

static void SharedFileSetOnDetach ( dsm_segment segment,
Datum  datum 
)
static

Definition at line 241 of file sharedfileset.c.

References Assert, DatumGetPointer, SharedFileSet::mutex, SharedFileSet::refcnt, SharedFileSetDeleteAll(), SpinLockAcquire, and SpinLockRelease.

Referenced by SharedFileSetAttach(), and SharedFileSetInit().

242 {
243  bool unlink_all = false;
244  SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum);
245 
246  SpinLockAcquire(&fileset->mutex);
247  Assert(fileset->refcnt > 0);
248  if (--fileset->refcnt == 0)
249  unlink_all = true;
250  SpinLockRelease(&fileset->mutex);
251 
252  /*
253  * If we are the last to detach, we delete the directory in all
254  * tablespaces. Note that we are still actually attached for the rest of
255  * this function so we can safely access its data.
256  */
257  if (unlink_all)
258  SharedFileSetDeleteAll(fileset);
259 }
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
void SharedFileSetDeleteAll(SharedFileSet *fileset)
#define Assert(condition)
Definition: c.h:800
#define DatumGetPointer(X)
Definition: postgres.h:549

◆ 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

◆ SharedFileSetPath()

static void SharedFileSetPath ( char *  path,
SharedFileSet fileset,
Oid  tablespace 
)
static

Definition at line 322 of file sharedfileset.c.

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

Referenced by SharedFilePath(), SharedFileSetCreate(), and SharedFileSetDeleteAll().

323 {
324  char tempdirpath[MAXPGPATH];
325 
326  TempTablespacePath(tempdirpath, tablespace);
327  snprintf(path, MAXPGPATH, "%s/%s%lu.%u.sharedfileset",
328  tempdirpath, PG_TEMP_FILE_PREFIX,
329  (unsigned long) fileset->creator_pid, fileset->number);
330 }
void TempTablespacePath(char *path, Oid tablespace)
Definition: fd.c:1631
#define PG_TEMP_FILE_PREFIX
Definition: pg_checksums.c:59
#define MAXPGPATH
char * tablespace
Definition: pgbench.c:189
#define snprintf
Definition: port.h:215

◆ 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:357
static List * filesetlist
Definition: sharedfileset.c:36
#define Assert(condition)
Definition: c.h:800
#define lfirst(lc)
Definition: pg_list.h:169

Variable Documentation

◆ filesetlist

List* filesetlist = NIL
static

Definition at line 36 of file sharedfileset.c.