11diff --git a/src/backend/replication/basebackup.c b/src/backend/replication/basebackup.c
2- index 3e53b3df6fb..f76bfc2a646 100644
2+ index ba4b47443f..62baf8ab4f 100644
33--- a/src/backend/replication/basebackup.c
44+++ b/src/backend/replication/basebackup.c
55@@ -209,6 +209,13 @@ static const struct exclude_list_item excludeFiles[] =
@@ -28,7 +28,7 @@ index 3e53b3df6fb..f76bfc2a646 100644
2828 {"config_exec_params", true},
2929 #endif
3030diff --git a/src/backend/storage/file/copydir.c b/src/backend/storage/file/copydir.c
31- index 4a0d23b11e3..d59009a4c8c 100644
31+ index 4a0d23b11e..d59009a4c8 100644
3232--- a/src/backend/storage/file/copydir.c
3333+++ b/src/backend/storage/file/copydir.c
3434@@ -27,6 +27,8 @@
@@ -51,7 +51,7 @@ index 4a0d23b11e3..d59009a4c8c 100644
5151 * Be paranoid here and fsync all files to ensure the copy is really done.
5252 * But if fsync is disabled, we're done.
5353diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c
54- index 200cc7f657a..d0dcb5c0287 100644
54+ index 5418452261..3332a3c717 100644
5555--- a/src/backend/storage/smgr/md.c
5656+++ b/src/backend/storage/smgr/md.c
5757@@ -39,6 +39,7 @@
@@ -71,7 +71,7 @@ index 200cc7f657a..d0dcb5c0287 100644
7171
7272 /*
7373 * In some contexts (currently, standalone backends and the checkpointer)
74- @@ -558 ,6 +561 ,9 @@ mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
74+ @@ -603 ,6 +606 ,9 @@ mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
7575 register_dirty_segment(reln, forknum, v);
7676
7777 Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
@@ -81,7 +81,7 @@ index 200cc7f657a..d0dcb5c0287 100644
8181 }
8282
8383 /*
84- @@ -851 ,6 +857 ,9 @@ mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
84+ @@ -896 ,6 +902 ,9 @@ mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
8585
8686 if (!skipFsync && !SmgrIsTemp(reln))
8787 register_dirty_segment(reln, forknum, v);
@@ -91,7 +91,7 @@ index 200cc7f657a..d0dcb5c0287 100644
9191 }
9292
9393 /*
94- @@ -1329 ,6 +1338 ,9 @@ mdsync(void)
94+ @@ -1374 ,6 +1383 ,9 @@ mdsync(void)
9595 CheckpointStats.ckpt_longest_sync = longest;
9696 CheckpointStats.ckpt_agg_sync_time = total_elapsed;
9797
@@ -101,27 +101,124 @@ index 200cc7f657a..d0dcb5c0287 100644
101101 /* Flag successful completion of mdsync */
102102 mdsync_in_progress = false;
103103 }
104+ diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
105+ index c7f3ee733d..837ee73217 100644
106+ --- a/src/backend/utils/init/miscinit.c
107+ +++ b/src/backend/utils/init/miscinit.c
108+ @@ -74,6 +74,11 @@ static Latch LocalLatchData;
109+
110+ bool IgnoreSystemIndexes = false;
111+
112+ + /* ----------------------------------------------------------------
113+ + * Ptrack functions support
114+ + * ----------------------------------------------------------------
115+ + */
116+ + static void check_use_ptrack(const char *libraries);
117+
118+ /* ----------------------------------------------------------------
119+ * database path / name support stuff
120+ @@ -1601,6 +1606,8 @@ process_shared_preload_libraries(void)
121+ "shared_preload_libraries",
122+ false);
123+ process_shared_preload_libraries_in_progress = false;
124+ +
125+ + check_use_ptrack(shared_preload_libraries_string);
126+ }
127+
128+ /*
129+ @@ -1631,3 +1638,71 @@ pg_bindtextdomain(const char *domain)
130+ }
131+ #endif
132+ }
133+ +
134+ + /*-------------------------------------------------------------------------
135+ + * Ptrack functions support
136+ + *-------------------------------------------------------------------------
137+ + */
138+ +
139+ + /* Persistent copy of ptrack.map to restore after crash */
140+ + #define PTRACK_PATH "global/ptrack.map"
141+ + /* Used for atomical crash-safe update of ptrack.map */
142+ + #define PTRACK_PATH_TMP "global/ptrack.map.tmp"
143+ +
144+ + /*
145+ + * Check that path is accessible by us and return true if it is
146+ + * not a directory.
147+ + */
148+ + static bool
149+ + ptrack_file_exists(const char *path)
150+ + {
151+ + struct stat st;
152+ +
153+ + Assert(path != NULL);
154+ +
155+ + if (stat(path, &st) == 0)
156+ + return S_ISDIR(st.st_mode) ? false : true;
157+ + else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES))
158+ + ereport(ERROR,
159+ + (errcode_for_file_access(),
160+ + errmsg("could not access file \"%s\": %m", path)));
161+ +
162+ + return false;
163+ + }
164+ +
165+ + /*
166+ + * Delete ptrack files when ptrack is disabled.
167+ + *
168+ + * This is performed by postmaster at start,
169+ + * so that there are no concurrent delete issues.
170+ + */
171+ + static void
172+ + ptrackCleanFiles(void)
173+ + {
174+ + char ptrack_path[MAXPGPATH];
175+ + char ptrack_path_tmp[MAXPGPATH];
176+ +
177+ + sprintf(ptrack_path, "%s/%s", DataDir, PTRACK_PATH);
178+ + sprintf(ptrack_path_tmp, "%s/%s", DataDir, PTRACK_PATH_TMP);
179+ +
180+ + elog(DEBUG1, "ptrack: clean map files");
181+ +
182+ + if (ptrack_file_exists(ptrack_path_tmp))
183+ + durable_unlink(ptrack_path_tmp, LOG);
184+ +
185+ + if (ptrack_file_exists(ptrack_path))
186+ + durable_unlink(ptrack_path, LOG);
187+ + }
188+ +
189+ + static void
190+ + check_use_ptrack(const char *libraries)
191+ + {
192+ + /* We need to delete the ptrack.map file when ptrack was turned off */
193+ + if (strstr(shared_preload_libraries_string, "ptrack") == NULL)
194+ + {
195+ + ptrackCleanFiles();
196+ + }
197+ + }
198+ +
199+ + #undef PTRACK_PATH
200+ + #undef PTRACK_PATH_TMP
104201diff --git a/src/bin/pg_resetwal/pg_resetwal.c b/src/bin/pg_resetwal/pg_resetwal.c
105- index 6fb403a5a8a..6e31ccb3e0f 100644
202+ index 611edf6ade..ec1c1212bd 100644
106203--- a/src/bin/pg_resetwal/pg_resetwal.c
107204+++ b/src/bin/pg_resetwal/pg_resetwal.c
108- @@ -84 ,6 +84 ,7 @@ static void RewriteControlFile(void);
205+ @@ -85 ,6 +85 ,7 @@ static void RewriteControlFile(void);
109206 static void FindEndOfXLOG(void);
110207 static void KillExistingXLOG(void);
111208 static void KillExistingArchiveStatus(void);
112209+ static void KillExistingPtrack(void);
113210 static void WriteEmptyXLOG(void);
114211 static void usage(void);
115212
116- @@ -516 ,6 +517 ,7 @@ main(int argc, char *argv[])
213+ @@ -525 ,6 +526 ,7 @@ main(int argc, char *argv[])
117214 RewriteControlFile();
118215 KillExistingXLOG();
119216 KillExistingArchiveStatus();
120217+ KillExistingPtrack();
121218 WriteEmptyXLOG();
122219
123220 printf(_("Write-ahead log reset\n"));
124- @@ -1201 ,6 +1203 ,57 @@ KillExistingArchiveStatus(void)
221+ @@ -1210 ,6 +1212 ,57 @@ KillExistingArchiveStatus(void)
125222 }
126223 }
127224
@@ -180,7 +277,7 @@ index 6fb403a5a8a..6e31ccb3e0f 100644
180277 /*
181278 * Write an empty XLOG file, containing only the checkpoint record
182279diff --git a/src/bin/pg_rewind/filemap.c b/src/bin/pg_rewind/filemap.c
183- index 197163d5544..fc846e78175 100644
280+ index 8ea7fafa27..997168fcac 100644
184281--- a/src/bin/pg_rewind/filemap.c
185282+++ b/src/bin/pg_rewind/filemap.c
186283@@ -118,6 +118,10 @@ static const struct exclude_list_item excludeFiles[] =
@@ -195,10 +292,10 @@ index 197163d5544..fc846e78175 100644
195292 {NULL, false}
196293 };
197294diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h
198- index 80241455357..50dca7bf6f4 100644
295+ index 7c9f319b67..1e29827030 100644
199296--- a/src/include/miscadmin.h
200297+++ b/src/include/miscadmin.h
201- @@ -367 ,7 +367 ,7 @@ typedef enum ProcessingMode
298+ @@ -379 ,7 +379 ,7 @@ typedef enum ProcessingMode
202299 NormalProcessing /* normal processing */
203300 } ProcessingMode;
204301
@@ -208,7 +305,7 @@ index 80241455357..50dca7bf6f4 100644
208305 #define IsBootstrapProcessingMode() (Mode == BootstrapProcessing)
209306 #define IsInitProcessingMode() (Mode == InitProcessing)
210307diff --git a/src/include/storage/copydir.h b/src/include/storage/copydir.h
211- index 4fef3e21072..e55430879c3 100644
308+ index 4fef3e2107..e55430879c 100644
212309--- a/src/include/storage/copydir.h
213310+++ b/src/include/storage/copydir.h
214311@@ -13,6 +13,9 @@
@@ -222,7 +319,7 @@ index 4fef3e21072..e55430879c3 100644
222319 extern void copy_file(char *fromfile, char *tofile);
223320
224321diff --git a/src/include/storage/smgr.h b/src/include/storage/smgr.h
225- index 0298ed1a2bc..24c684771d0 100644
322+ index 0298ed1a2b..24c684771d 100644
226323--- a/src/include/storage/smgr.h
227324+++ b/src/include/storage/smgr.h
228325@@ -116,6 +116,17 @@ extern void AtEOXact_SMgr(void);
@@ -244,7 +341,7 @@ index 0298ed1a2bc..24c684771d0 100644
244341 extern void mdclose(SMgrRelation reln, ForkNumber forknum);
245342 extern void mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo);
246343diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
247- index b52baa90988..74870c048db 100644
344+ index b52baa9098..74870c048d 100644
248345--- a/src/tools/msvc/Mkvcbuild.pm
249346+++ b/src/tools/msvc/Mkvcbuild.pm
250347@@ -33,7 +33,7 @@ my @unlink_on_exit;
0 commit comments