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)
 
void SharedFileSetInit (SharedFileSet *fileset, dsm_segment *seg)
 
void SharedFileSetAttach (SharedFileSet *fileset, dsm_segment *seg)
 
void SharedFileSetDeleteAll (SharedFileSet *fileset)
 

Function Documentation

◆ SharedFileSetAttach()

void SharedFileSetAttach ( SharedFileSet fileset,
dsm_segment seg 
)

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

63 {
64  bool success;
65 
66  SpinLockAcquire(&fileset->mutex);
67  if (fileset->refcnt == 0)
68  success = false;
69  else
70  {
71  ++fileset->refcnt;
72  success = true;
73  }
74  SpinLockRelease(&fileset->mutex);
75 
76  if (!success)
77  ereport(ERROR,
78  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
79  errmsg("could not attach to a SharedFileSet that is already destroyed")));
80 
81  /* Register our cleanup callback. */
83 }
#define PointerGetDatum(X)
Definition: postgres.h:600
int errcode(int sqlerrcode)
Definition: elog.c:698
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition: dsm.c:1096
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define ERROR
Definition: elog.h:46
#define SpinLockRelease(lock)
Definition: spin.h:64
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
static bool success
Definition: initdb.c:165
static void SharedFileSetOnDetach(dsm_segment *segment, Datum datum)

◆ SharedFileSetDeleteAll()

void SharedFileSetDeleteAll ( SharedFileSet fileset)

Definition at line 89 of file sharedfileset.c.

References FileSetDeleteAll(), and SharedFileSet::fs.

Referenced by ExecHashJoinReInitializeDSM().

90 {
91  FileSetDeleteAll(&fileset->fs);
92 }
void FileSetDeleteAll(FileSet *fileset)
Definition: fileset.c:152

◆ SharedFileSetInit()

void SharedFileSetInit ( SharedFileSet fileset,
dsm_segment seg 
)

Definition at line 44 of file sharedfileset.c.

References FileSetInit(), SharedFileSet::fs, SharedFileSet::mutex, on_dsm_detach(), PointerGetDatum, SharedFileSet::refcnt, SharedFileSetOnDetach(), and SpinLockInit.

Referenced by ExecHashJoinInitializeDSM(), and tuplesort_initialize_shared().

45 {
46  /* Initialize the shared fileset specific members. */
47  SpinLockInit(&fileset->mutex);
48  fileset->refcnt = 1;
49 
50  /* Initialize the fileset. */
51  FileSetInit(&fileset->fs);
52 
53  /* Register our cleanup callback. */
54  if (seg)
56 }
#define PointerGetDatum(X)
Definition: postgres.h:600
#define SpinLockInit(lock)
Definition: spin.h:60
void FileSetInit(FileSet *fileset)
Definition: fileset.c:54
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition: dsm.c:1096
static void SharedFileSetOnDetach(dsm_segment *segment, Datum datum)

◆ SharedFileSetOnDetach()

static void SharedFileSetOnDetach ( dsm_segment segment,
Datum  datum 
)
static

Definition at line 102 of file sharedfileset.c.

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

Referenced by SharedFileSetAttach(), and SharedFileSetInit().

103 {
104  bool unlink_all = false;
105  SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum);
106 
107  SpinLockAcquire(&fileset->mutex);
108  Assert(fileset->refcnt > 0);
109  if (--fileset->refcnt == 0)
110  unlink_all = true;
111  SpinLockRelease(&fileset->mutex);
112 
113  /*
114  * If we are the last to detach, we delete the directory in all
115  * tablespaces. Note that we are still actually attached for the rest of
116  * this function so we can safely access its data.
117  */
118  if (unlink_all)
119  FileSetDeleteAll(&fileset->fs);
120 }
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
#define Assert(condition)
Definition: c.h:804
#define DatumGetPointer(X)
Definition: postgres.h:593
void FileSetDeleteAll(FileSet *fileset)
Definition: fileset.c:152