PostgreSQL Source Code
git master
sharedfileset.c
Go to the documentation of this file.
1
/*-------------------------------------------------------------------------
2
*
3
* sharedfileset.c
4
* Shared temporary file management.
5
*
6
* Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
7
* Portions Copyright (c) 1994, Regents of the University of California
8
*
9
* IDENTIFICATION
10
* src/backend/storage/file/sharedfileset.c
11
*
12
* SharedFileSets provide a temporary namespace (think directory) so that
13
* files can be discovered by name, and a shared ownership semantics so that
14
* shared files survive until the last user detaches.
15
*
16
*-------------------------------------------------------------------------
17
*/
18
19
#include "
postgres.h
"
20
21
#include <limits.h>
22
23
#include "
catalog/pg_tablespace.h
"
24
#include "
commands/tablespace.h
"
25
#include "
common/hashfn.h
"
26
#include "
miscadmin.h
"
27
#include "
storage/dsm.h
"
28
#include "
storage/ipc.h
"
29
#include "
storage/sharedfileset.h
"
30
#include "
utils/builtins.h
"
31
32
static
void
SharedFileSetOnDetach
(
dsm_segment
*segment,
Datum
datum);
33
34
/*
35
* Initialize a space for temporary files that can be opened by other backends.
36
* Other backends must attach to it before accessing it. Associate this
37
* SharedFileSet with 'seg'. Any contained files will be deleted when the
38
* last backend detaches.
39
*
40
* Under the covers the set is one or more directories which will eventually
41
* be deleted.
42
*/
43
void
44
SharedFileSetInit
(
SharedFileSet
*fileset,
dsm_segment
*seg)
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)
55
on_dsm_detach
(seg,
SharedFileSetOnDetach
,
PointerGetDatum
(fileset));
56
}
57
58
/*
59
* Attach to a set of directories that was created with SharedFileSetInit.
60
*/
61
void
62
SharedFileSetAttach
(
SharedFileSet
*fileset,
dsm_segment
*seg)
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. */
82
on_dsm_detach
(seg,
SharedFileSetOnDetach
,
PointerGetDatum
(fileset));
83
}
84
85
/*
86
* Delete all files in the set.
87
*/
88
void
89
SharedFileSetDeleteAll
(
SharedFileSet
*fileset)
90
{
91
FileSetDeleteAll
(&fileset->
fs
);
92
}
93
94
/*
95
* Callback function that will be invoked when this backend detaches from a
96
* DSM segment holding a SharedFileSet that it has created or attached to. If
97
* we are the last to detach, then try to remove the directories and
98
* everything in them. We can't raise an error on failures, because this runs
99
* in error cleanup paths.
100
*/
101
static
void
102
SharedFileSetOnDetach
(
dsm_segment
*segment,
Datum
datum)
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
}
builtins.h
on_dsm_detach
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition:
dsm.c:1103
dsm.h
errcode
int errcode(int sqlerrcode)
Definition:
elog.c:858
errmsg
int errmsg(const char *fmt,...)
Definition:
elog.c:1069
ERROR
#define ERROR
Definition:
elog.h:39
ereport
#define ereport(elevel,...)
Definition:
elog.h:149
FileSetInit
void FileSetInit(FileSet *fileset)
Definition:
fileset.c:55
FileSetDeleteAll
void FileSetDeleteAll(FileSet *fileset)
Definition:
fileset.c:153
hashfn.h
success
static bool success
Definition:
initdb.c:184
ipc.h
Assert
Assert(fmt[strlen(fmt) - 1] !='\n')
miscadmin.h
pg_tablespace.h
postgres.h
PointerGetDatum
static Datum PointerGetDatum(const void *X)
Definition:
postgres.h:322
Datum
uintptr_t Datum
Definition:
postgres.h:64
DatumGetPointer
static Pointer DatumGetPointer(Datum X)
Definition:
postgres.h:312
SharedFileSetOnDetach
static void SharedFileSetOnDetach(dsm_segment *segment, Datum datum)
Definition:
sharedfileset.c:102
SharedFileSetAttach
void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg)
Definition:
sharedfileset.c:62
SharedFileSetDeleteAll
void SharedFileSetDeleteAll(SharedFileSet *fileset)
Definition:
sharedfileset.c:89
SharedFileSetInit
void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg)
Definition:
sharedfileset.c:44
sharedfileset.h
SpinLockInit
#define SpinLockInit(lock)
Definition:
spin.h:60
SpinLockRelease
#define SpinLockRelease(lock)
Definition:
spin.h:64
SpinLockAcquire
#define SpinLockAcquire(lock)
Definition:
spin.h:62
SharedFileSet
Definition:
sharedfileset.h:27
SharedFileSet::mutex
slock_t mutex
Definition:
sharedfileset.h:29
SharedFileSet::refcnt
int refcnt
Definition:
sharedfileset.h:30
SharedFileSet::fs
FileSet fs
Definition:
sharedfileset.h:28
dsm_segment
Definition:
dsm.c:66
tablespace.h
src
backend
storage
file
sharedfileset.c
Generated on Wed Sep 27 2023 18:13:19 for PostgreSQL Source Code by
1.9.1