-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmaster-ptrack-core.diff
More file actions
389 lines (355 loc) · 11.9 KB
/
master-ptrack-core.diff
File metadata and controls
389 lines (355 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 22d0a2e8c3a..3f597e54c59 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -137,6 +137,7 @@ int wal_retrieve_retry_interval = 5000;
int max_slot_wal_keep_size_mb = -1;
int wal_decode_buffer_size = 512 * 1024;
bool track_wal_io_timing = false;
+backup_checkpoint_request_hook_type backup_checkpoint_request_hook = NULL;
#ifdef WAL_DEBUG
bool XLOG_DEBUG = false;
@@ -8945,6 +8946,12 @@ do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces,
{
bool checkpointfpw;
+ /*
+ * Before we call RequestCheckpoint() we need to set
+ * init_lsn for ptrack map
+ */
+ if (backup_checkpoint_request_hook)
+ backup_checkpoint_request_hook();
/*
* Force a CHECKPOINT. Aside from being necessary to prevent torn
* page problems, this guarantees that two successive backup runs
diff --git a/src/backend/backup/basebackup.c b/src/backend/backup/basebackup.c
index 2be4e069816..af00164b01d 100644
--- a/src/backend/backup/basebackup.c
+++ b/src/backend/backup/basebackup.c
@@ -219,6 +219,12 @@ static const struct exclude_list_item excludeFiles[] =
{"postmaster.pid", false},
{"postmaster.opts", false},
+ /*
+ * Skip all transient ptrack files, but do copy ptrack.map, since it may
+ * be successfully used immediately after backup. TODO: check, test?
+ */
+ {"ptrack.map.mmap", false},
+ {"ptrack.map.tmp", false},
/* end of list */
{NULL, false}
diff --git a/src/backend/storage/file/copydir.c b/src/backend/storage/file/copydir.c
index aa8c64a2c9e..56ed1b4df0a 100644
--- a/src/backend/storage/file/copydir.c
+++ b/src/backend/storage/file/copydir.c
@@ -30,6 +30,8 @@
#include "storage/copydir.h"
#include "storage/fd.h"
+copydir_hook_type copydir_hook = NULL;
+
/* GUCs */
int file_copy_method = FILE_COPY_METHOD_COPY;
@@ -91,6 +93,9 @@ copydir(const char *fromdir, const char *todir, bool recurse)
}
FreeDir(xldir);
+ if (copydir_hook)
+ copydir_hook(todir);
+
/*
* Be paranoid here and fsync all files to ensure the copy is really done.
* But if fsync is disabled, we're done.
diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c
index e3f335a8340..aa36716d9fb 100644
--- a/src/backend/storage/smgr/md.c
+++ b/src/backend/storage/smgr/md.c
@@ -96,6 +96,8 @@ typedef struct _MdfdVec
static MemoryContext MdCxt; /* context for all MdfdVec objects */
+mdextend_hook_type mdextend_hook = NULL;
+mdwrite_hook_type mdwrite_hook = NULL;
/* Populate a file tag describing an md.c segment file. */
#define INIT_MD_FILETAG(a,xx_rlocator,xx_forknum,xx_segno) \
@@ -540,6 +542,9 @@ mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
register_dirty_segment(reln, forknum, v);
Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
+
+ if (mdextend_hook)
+ mdextend_hook(reln->smgr_rlocator, forknum, blocknum);
}
/*
@@ -647,6 +652,12 @@ mdzeroextend(SMgrRelation reln, ForkNumber forknum,
remblocks -= numblocks;
curblocknum += numblocks;
+
+ if (mdextend_hook)
+ {
+ for (; blocknum < curblocknum; blocknum++)
+ mdextend_hook(reln->smgr_rlocator, forknum, blocknum);
+ }
}
}
@@ -1149,7 +1160,14 @@ mdwritev(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
nblocks -= nblocks_this_segment;
buffers += nblocks_this_segment;
- blocknum += nblocks_this_segment;
+
+ if (mdwrite_hook)
+ {
+ for (; nblocks_this_segment--; blocknum++)
+ mdwrite_hook(reln->smgr_rlocator, forknum, blocknum);
+ }
+ else
+ blocknum += nblocks_this_segment;
}
}
diff --git a/src/backend/storage/sync/sync.c b/src/backend/storage/sync/sync.c
index fc16db90133..08cd553ad55 100644
--- a/src/backend/storage/sync/sync.c
+++ b/src/backend/storage/sync/sync.c
@@ -74,6 +74,8 @@ static MemoryContext pendingOpsCxt; /* context for the above */
static CycleCtr sync_cycle_ctr = 0;
static CycleCtr checkpoint_cycle_ctr = 0;
+ProcessSyncRequests_hook_type ProcessSyncRequests_hook = NULL;
+
/* Intervals for calling AbsorbSyncRequests */
#define FSYNCS_PER_ABSORB 10
#define UNLINKS_PER_ABSORB 10
@@ -470,6 +472,9 @@ ProcessSyncRequests(void)
CheckpointStats.ckpt_longest_sync = longest;
CheckpointStats.ckpt_agg_sync_time = total_elapsed;
+ if (ProcessSyncRequests_hook)
+ ProcessSyncRequests_hook();
+
/* Flag successful completion of ProcessSyncRequests */
sync_in_progress = false;
}
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index fec79992c8d..3191c7998dd 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -80,6 +80,11 @@ static Latch LocalLatchData;
bool IgnoreSystemIndexes = false;
+/* ----------------------------------------------------------------
+ * Ptrack functions support
+ * ----------------------------------------------------------------
+ */
+static void check_use_ptrack(const char *libraries);
/* ----------------------------------------------------------------
* common process startup code
@@ -1856,6 +1861,8 @@ process_shared_preload_libraries(void)
false);
process_shared_preload_libraries_in_progress = false;
process_shared_preload_libraries_done = true;
+
+ check_use_ptrack(shared_preload_libraries_string);
}
/*
@@ -1898,3 +1905,71 @@ pg_bindtextdomain(const char *domain)
}
#endif
}
+
+/*-------------------------------------------------------------------------
+ * Ptrack functions support
+ *-------------------------------------------------------------------------
+ */
+
+/* Persistent copy of ptrack.map to restore after crash */
+#define PTRACK_PATH "global/ptrack.map"
+/* Used for atomical crash-safe update of ptrack.map */
+#define PTRACK_PATH_TMP "global/ptrack.map.tmp"
+
+/*
+ * Check that path is accessible by us and return true if it is
+ * not a directory.
+ */
+static bool
+ptrack_file_exists(const char *path)
+{
+ struct stat st;
+
+ Assert(path != NULL);
+
+ if (stat(path, &st) == 0)
+ return S_ISDIR(st.st_mode) ? false : true;
+ else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES))
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not access file \"%s\": %m", path)));
+
+ return false;
+}
+
+/*
+ * Delete ptrack files when ptrack is disabled.
+ *
+ * This is performed by postmaster at start,
+ * so that there are no concurrent delete issues.
+ */
+static void
+ptrackCleanFiles(void)
+{
+ char ptrack_path[MAXPGPATH];
+ char ptrack_path_tmp[MAXPGPATH];
+
+ sprintf(ptrack_path, "%s/%s", DataDir, PTRACK_PATH);
+ sprintf(ptrack_path_tmp, "%s/%s", DataDir, PTRACK_PATH_TMP);
+
+ elog(DEBUG1, "ptrack: clean map files");
+
+ if (ptrack_file_exists(ptrack_path_tmp))
+ durable_unlink(ptrack_path_tmp, LOG);
+
+ if (ptrack_file_exists(ptrack_path))
+ durable_unlink(ptrack_path, LOG);
+}
+
+static void
+check_use_ptrack(const char *libraries)
+{
+ /* We need to delete the ptrack.map file when ptrack was turned off */
+ if (strstr(shared_preload_libraries_string, "ptrack") == NULL)
+ {
+ ptrackCleanFiles();
+ }
+}
+
+#undef PTRACK_PATH
+#undef PTRACK_PATH_TMP
diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c
index 46cb2f36efa..428ce4c5bfe 100644
--- a/src/bin/pg_checksums/pg_checksums.c
+++ b/src/bin/pg_checksums/pg_checksums.c
@@ -110,6 +110,11 @@ static const struct exclude_list_item skip[] = {
{"pg_filenode.map", false},
{"pg_internal.init", true},
{"PG_VERSION", false},
+
+ {"ptrack.map.mmap", false},
+ {"ptrack.map", false},
+ {"ptrack.map.tmp", false},
+
#ifdef EXEC_BACKEND
{"config_exec_params", true},
#endif
diff --git a/src/bin/pg_resetwal/pg_resetwal.c b/src/bin/pg_resetwal/pg_resetwal.c
index 8d5d9805279..4a95fabb076 100644
--- a/src/bin/pg_resetwal/pg_resetwal.c
+++ b/src/bin/pg_resetwal/pg_resetwal.c
@@ -90,6 +90,7 @@ static void FindEndOfXLOG(void);
static void KillExistingXLOG(void);
static void KillExistingArchiveStatus(void);
static void KillExistingWALSummaries(void);
+static void KillExistingPtrack(void);
static void WriteEmptyXLOG(void);
static void usage(void);
@@ -523,6 +524,7 @@ main(int argc, char *argv[])
KillExistingXLOG();
KillExistingArchiveStatus();
KillExistingWALSummaries();
+ KillExistingPtrack();
WriteEmptyXLOG();
printf(_("Write-ahead log reset\n"));
@@ -1018,6 +1020,40 @@ KillExistingXLOG(void)
pg_fatal("could not close directory \"%s\": %m", XLOGDIR);
}
+/*
+ * Remove existing ptrack files
+ */
+static void
+KillExistingPtrack(void)
+{
+#define PTRACKDIR "global"
+
+ DIR *xldir;
+ struct dirent *xlde;
+ char path[MAXPGPATH + sizeof(PTRACKDIR)];
+
+ xldir = opendir(PTRACKDIR);
+ if (xldir == NULL)
+ pg_fatal("could not open directory \"%s\": %m", PTRACKDIR);
+
+ while (errno = 0, (xlde = readdir(xldir)) != NULL)
+ {
+ if (strcmp(xlde->d_name, "ptrack.map.mmap") == 0 ||
+ strcmp(xlde->d_name, "ptrack.map") == 0 ||
+ strcmp(xlde->d_name, "ptrack.map.tmp") == 0)
+ {
+ snprintf(path, sizeof(path), "%s/%s", PTRACKDIR, xlde->d_name);
+ if (unlink(path) < 0)
+ pg_fatal("could not delete file \"%s\": %m", path);
+ }
+ }
+
+ if (errno)
+ pg_fatal("could not read directory \"%s\": %m", PTRACKDIR);
+
+ if (closedir(xldir))
+ pg_fatal("could not close directory \"%s\": %m", PTRACKDIR);
+}
/*
* Remove existing archive status files
diff --git a/src/bin/pg_rewind/filemap.c b/src/bin/pg_rewind/filemap.c
index 467fd97ebcf..af0a646b251 100644
--- a/src/bin/pg_rewind/filemap.c
+++ b/src/bin/pg_rewind/filemap.c
@@ -186,6 +186,10 @@ static const struct exclude_list_item excludeFiles[] =
{"postmaster.pid", false},
{"postmaster.opts", false},
+ {"ptrack.map.mmap", false},
+ {"ptrack.map", false},
+ {"ptrack.map.tmp", false},
+
/* end of list */
{NULL, false}
};
diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h
index 605280ed8fb..f0f30cb6168 100644
--- a/src/include/access/xlog.h
+++ b/src/include/access/xlog.h
@@ -59,6 +59,9 @@ extern PGDLLIMPORT int wal_decode_buffer_size;
extern PGDLLIMPORT int CheckPointSegments;
+typedef void (*backup_checkpoint_request_hook_type) (void);
+extern PGDLLIMPORT backup_checkpoint_request_hook_type backup_checkpoint_request_hook;
+
/* Archive modes */
typedef enum ArchiveMode
{
diff --git a/src/include/storage/copydir.h b/src/include/storage/copydir.h
index f1d7beeed1a..f162c4405dc 100644
--- a/src/include/storage/copydir.h
+++ b/src/include/storage/copydir.h
@@ -22,6 +22,9 @@ typedef enum FileCopyMethod
/* GUC parameters */
extern PGDLLIMPORT int file_copy_method;
+typedef void (*copydir_hook_type) (const char *path);
+extern PGDLLIMPORT copydir_hook_type copydir_hook;
+
extern void copydir(const char *fromdir, const char *todir, bool recurse);
extern void copy_file(const char *fromfile, const char *tofile);
diff --git a/src/include/storage/md.h b/src/include/storage/md.h
index b563c27abf0..29fbc1c80c3 100644
--- a/src/include/storage/md.h
+++ b/src/include/storage/md.h
@@ -22,6 +22,13 @@
extern PGDLLIMPORT const PgAioHandleCallbacks aio_md_readv_cb;
+typedef void (*mdextend_hook_type) (RelFileLocatorBackend smgr_rlocator,
+ ForkNumber forknum, BlockNumber blocknum);
+extern PGDLLIMPORT mdextend_hook_type mdextend_hook;
+typedef void (*mdwrite_hook_type) (RelFileLocatorBackend smgr_rlocator,
+ ForkNumber forknum, BlockNumber blocknum);
+extern PGDLLIMPORT mdwrite_hook_type mdwrite_hook;
+
/* md storage manager functionality */
extern void mdinit(void);
extern void mdopen(SMgrRelation reln);
diff --git a/src/include/storage/sync.h b/src/include/storage/sync.h
index c2272d14175..4132cfd92c6 100644
--- a/src/include/storage/sync.h
+++ b/src/include/storage/sync.h
@@ -55,6 +55,9 @@ typedef struct FileTag
uint64 segno;
} FileTag;
+typedef void (*ProcessSyncRequests_hook_type) (void);
+extern PGDLLIMPORT ProcessSyncRequests_hook_type ProcessSyncRequests_hook;
+
extern void InitSync(void);
extern void SyncPreCheckpoint(void);
extern void SyncPostCheckpoint(void);