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 "miscadmin.h"
#include "storage/dsm.h"
#include "storage/sharedfileset.h"
#include "utils/builtins.h"
#include "utils/hashutils.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 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)
 
bool SharedFileSetDelete (SharedFileSet *fileset, const char *name, bool error_on_failure)
 
void SharedFileSetDeleteAll (SharedFileSet *fileset)
 

Function Documentation

◆ ChooseTablespace()

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

Definition at line 229 of file sharedfileset.c.

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

Referenced by SharedFilePath(), and SharedFileSetCreate().

230 {
231  uint32 hash = hash_any((const unsigned char *) name, strlen(name));
232 
233  return fileset->tablespaces[hash % fileset->ntablespaces];
234 }
Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.c:148
unsigned int uint32
Definition: c.h:359
Oid tablespaces[8]
Definition: sharedfileset.h:32
const char * name
Definition: encode.c:521
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 240 of file sharedfileset.c.

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

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

241 {
242  char dirpath[MAXPGPATH];
243 
244  SharedFileSetPath(dirpath, fileset, ChooseTablespace(fileset, name));
245  snprintf(path, MAXPGPATH, "%s/%s", dirpath, name);
246 }
static void SharedFileSetPath(char *path, SharedFileSet *fileset, Oid tablespace)
#define MAXPGPATH
const char * name
Definition: encode.c:521
static Oid ChooseTablespace(const SharedFileSet *fileset, const char *name)
#define snprintf
Definition: port.h:192

◆ SharedFileSetAttach()

void SharedFileSetAttach ( SharedFileSet fileset,
dsm_segment seg 
)

Definition at line 78 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().

79 {
80  bool success;
81 
82  SpinLockAcquire(&fileset->mutex);
83  if (fileset->refcnt == 0)
84  success = false;
85  else
86  {
87  ++fileset->refcnt;
88  success = true;
89  }
90  SpinLockRelease(&fileset->mutex);
91 
92  if (!success)
93  ereport(ERROR,
94  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
95  errmsg("could not attach to a SharedFileSet that is already destroyed")));
96 
97  /* Register our cleanup callback. */
99 }
#define PointerGetDatum(X)
Definition: postgres.h:556
int errcode(int sqlerrcode)
Definition: elog.c:608
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition: dsm.c:975
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define SpinLockRelease(lock)
Definition: spin.h:64
int errmsg(const char *fmt,...)
Definition: elog.c:822
static bool success
Definition: initdb.c:163
static void SharedFileSetOnDetach(dsm_segment *segment, Datum datum)

◆ SharedFileSetCreate()

File SharedFileSetCreate ( SharedFileSet fileset,
const char *  name 
)

Definition at line 105 of file sharedfileset.c.

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

Referenced by MakeNewSharedSegment().

106 {
107  char path[MAXPGPATH];
108  File file;
109 
110  SharedFilePath(path, fileset, name);
111  file = PathNameCreateTemporaryFile(path, false);
112 
113  /* If we failed, see if we need to create the directory on demand. */
114  if (file <= 0)
115  {
116  char tempdirpath[MAXPGPATH];
117  char filesetpath[MAXPGPATH];
118  Oid tablespace = ChooseTablespace(fileset, name);
119 
120  TempTablespacePath(tempdirpath, tablespace);
121  SharedFileSetPath(filesetpath, fileset, tablespace);
122  PathNameCreateTemporaryDir(tempdirpath, filesetpath);
123  file = PathNameCreateTemporaryFile(path, true);
124  }
125 
126  return file;
127 }
static void SharedFileSetPath(char *path, SharedFileSet *fileset, Oid tablespace)
File PathNameCreateTemporaryFile(const char *path, bool error_on_failure)
Definition: fd.c:1634
void TempTablespacePath(char *path, Oid tablespace)
Definition: fd.c:1552
unsigned int Oid
Definition: postgres_ext.h:31
#define MAXPGPATH
char * tablespace
Definition: pgbench.c:189
const char * name
Definition: encode.c:521
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:1435
int File
Definition: fd.h:45

◆ SharedFileSetDelete()

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

Definition at line 150 of file sharedfileset.c.

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

Referenced by BufFileDeleteShared(), and MakeNewSharedSegment().

152 {
153  char path[MAXPGPATH];
154 
155  SharedFilePath(path, fileset, name);
156 
157  return PathNameDeleteTemporaryFile(path, error_on_failure);
158 }
bool PathNameDeleteTemporaryFile(const char *path, bool error_on_failure)
Definition: fd.c:1702
#define MAXPGPATH
const char * name
Definition: encode.c:521
static void SharedFilePath(char *path, SharedFileSet *fileset, const char *name)

◆ SharedFileSetDeleteAll()

void SharedFileSetDeleteAll ( SharedFileSet fileset)

Definition at line 164 of file sharedfileset.c.

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

Referenced by ExecHashJoinReInitializeDSM(), and SharedFileSetOnDetach().

165 {
166  char dirpath[MAXPGPATH];
167  int i;
168 
169  /*
170  * Delete the directory we created in each tablespace. Doesn't fail
171  * because we use this in error cleanup paths, but can generate LOG
172  * message on IO error.
173  */
174  for (i = 0; i < fileset->ntablespaces; ++i)
175  {
176  SharedFileSetPath(dirpath, fileset, fileset->tablespaces[i]);
178  }
179 }
static void SharedFileSetPath(char *path, SharedFileSet *fileset, Oid tablespace)
void PathNameDeleteTemporaryDir(const char *dirname)
Definition: fd.c:1466
#define MAXPGPATH
Oid tablespaces[8]
Definition: sharedfileset.h:32
int i

◆ SharedFileSetInit()

void SharedFileSetInit ( SharedFileSet fileset,
dsm_segment seg 
)

Definition at line 49 of file sharedfileset.c.

References SharedFileSet::creator_pid, GetTempTablespaces(), lengthof, SharedFileSet::mutex, MyProcPid, SharedFileSet::ntablespaces, SharedFileSet::number, on_dsm_detach(), PointerGetDatum, PrepareTempTablespaces(), SharedFileSet::refcnt, SharedFileSetOnDetach(), SpinLockInit, and SharedFileSet::tablespaces.

Referenced by ExecHashJoinInitializeDSM(), and tuplesort_initialize_shared().

50 {
51  static uint32 counter = 0;
52 
53  SpinLockInit(&fileset->mutex);
54  fileset->refcnt = 1;
55  fileset->creator_pid = MyProcPid;
56  fileset->number = counter;
57  counter = (counter + 1) % INT_MAX;
58 
59  /* Capture the tablespace OIDs so that all backends agree on them. */
61  fileset->ntablespaces =
62  GetTempTablespaces(&fileset->tablespaces[0],
63  lengthof(fileset->tablespaces));
64  if (fileset->ntablespaces == 0)
65  {
66  fileset->tablespaces[0] = DEFAULTTABLESPACE_OID;
67  fileset->ntablespaces = 1;
68  }
69 
70  /* Register our cleanup callback. */
72 }
int MyProcPid
Definition: globals.c:40
int GetTempTablespaces(Oid *tableSpaces, int numSpaces)
Definition: fd.c:2746
#define PointerGetDatum(X)
Definition: postgres.h:556
#define SpinLockInit(lock)
Definition: spin.h:60
#define lengthof(array)
Definition: c.h:669
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition: dsm.c:975
void PrepareTempTablespaces(void)
Definition: tablespace.c:1323
unsigned int uint32
Definition: c.h:359
Oid tablespaces[8]
Definition: sharedfileset.h:32
static void SharedFileSetOnDetach(dsm_segment *segment, Datum datum)

◆ SharedFileSetOnDetach()

static void SharedFileSetOnDetach ( dsm_segment segment,
Datum  datum 
)
static

Definition at line 189 of file sharedfileset.c.

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

Referenced by SharedFileSetAttach(), and SharedFileSetInit().

190 {
191  bool unlink_all = false;
192  SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum);
193 
194  SpinLockAcquire(&fileset->mutex);
195  Assert(fileset->refcnt > 0);
196  if (--fileset->refcnt == 0)
197  unlink_all = true;
198  SpinLockRelease(&fileset->mutex);
199 
200  /*
201  * If we are the last to detach, we delete the directory in all
202  * tablespaces. Note that we are still actually attached for the rest of
203  * this function so we can safely access its data.
204  */
205  if (unlink_all)
206  SharedFileSetDeleteAll(fileset);
207 }
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
void SharedFileSetDeleteAll(SharedFileSet *fileset)
#define Assert(condition)
Definition: c.h:739
#define DatumGetPointer(X)
Definition: postgres.h:549

◆ SharedFileSetOpen()

File SharedFileSetOpen ( SharedFileSet fileset,
const char *  name 
)

Definition at line 134 of file sharedfileset.c.

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

Referenced by BufFileOpenShared().

135 {
136  char path[MAXPGPATH];
137  File file;
138 
139  SharedFilePath(path, fileset, name);
140  file = PathNameOpenTemporaryFile(path);
141 
142  return file;
143 }
File PathNameOpenTemporaryFile(const char *path)
Definition: fd.c:1672
#define MAXPGPATH
const char * name
Definition: encode.c:521
static void SharedFilePath(char *path, SharedFileSet *fileset, const char *name)
int File
Definition: fd.h:45

◆ SharedFileSetPath()

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

Definition at line 214 of file sharedfileset.c.

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

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

215 {
216  char tempdirpath[MAXPGPATH];
217 
218  TempTablespacePath(tempdirpath, tablespace);
219  snprintf(path, MAXPGPATH, "%s/%s%lu.%u.sharedfileset",
220  tempdirpath, PG_TEMP_FILE_PREFIX,
221  (unsigned long) fileset->creator_pid, fileset->number);
222 }
void TempTablespacePath(char *path, Oid tablespace)
Definition: fd.c:1552
#define PG_TEMP_FILE_PREFIX
Definition: pg_checksums.c:59
#define MAXPGPATH
char * tablespace
Definition: pgbench.c:189
#define snprintf
Definition: port.h:192