Skip to content

Commit df10f7c

Browse files
Oleg Gurevfunny-falcon
Oleg Gurev
authored andcommitted
[PGPRO-10166] Set ptrack init_lsn before first transaction to switched xlog segment
- Because of vanilla commit: - "During online checkpoints, insert XLOG_CHECKPOINT_REDO at redo point." - Ptrack in some cases got init_lsn right after this xlog record - New hook will set init_lsn before insernig checkpoint record
1 parent 82b1f80 commit df10f7c

File tree

4 files changed

+88
-21
lines changed

4 files changed

+88
-21
lines changed

engine.c

+17-10
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,21 @@ ptrack_mark_block(RelFileNodeBackend smgr_rnode,
643643
slots[0] = (size_t)(hash % PtrackContentNblocks);
644644
slots[1] = (size_t)(((hash << 32) | (hash >> 32)) % PtrackContentNblocks);
645645

646+
new_lsn = ptrack_set_init_lsn();
647+
648+
/* Atomically assign new LSN value to the slots */
649+
for (i = 0; i < lengthof(slots); i++)
650+
{
651+
#if USE_ASSERT_CHECKING
652+
elog(DEBUG3, "ptrack_mark_block: map[%zu]", slots[i]);
653+
#endif
654+
ptrack_atomic_increase(new_lsn, &ptrack_map->entries[slots[i]]);
655+
}
656+
}
657+
658+
extern XLogRecPtr ptrack_set_init_lsn(void)
659+
{
660+
XLogRecPtr new_lsn;
646661
if (RecoveryInProgress())
647662
new_lsn = GetXLogReplayRecPtr(NULL);
648663
else
@@ -652,17 +667,9 @@ ptrack_mark_block(RelFileNodeBackend smgr_rnode,
652667
if (pg_atomic_read_u64(&ptrack_map->init_lsn) == InvalidXLogRecPtr)
653668
{
654669
#if USE_ASSERT_CHECKING
655-
elog(DEBUG3, "ptrack_mark_block: init_lsn");
670+
elog(DEBUG3, "ptrack_set_init_lsn: init_lsn");
656671
#endif
657672
ptrack_atomic_increase(new_lsn, &ptrack_map->init_lsn);
658673
}
659-
660-
/* Atomically assign new LSN value to the slots */
661-
for (i = 0; i < lengthof(slots); i++)
662-
{
663-
#if USE_ASSERT_CHECKING
664-
elog(DEBUG3, "ptrack_mark_block: map[%zu]", slots[i]);
665-
#endif
666-
ptrack_atomic_increase(new_lsn, &ptrack_map->entries[slots[i]]);
667-
}
674+
return new_lsn;
668675
}

engine.h

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ extern int ptrack_map_size_tmp;
104104
extern void ptrackCheckpoint(void);
105105
extern void ptrackMapInit(void);
106106
extern void ptrackCleanFiles(void);
107+
extern XLogRecPtr ptrack_set_init_lsn(void);
107108

108109
extern void assign_ptrack_map_size(int newval, void *extra);
109110

patches/master-ptrack-core.diff

+50-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
1+
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
2+
index 34a2c71812..2d73d8023e 100644
3+
--- a/src/backend/access/transam/xlog.c
4+
+++ b/src/backend/access/transam/xlog.c
5+
@@ -135,6 +135,7 @@ int wal_retrieve_retry_interval = 5000;
6+
int max_slot_wal_keep_size_mb = -1;
7+
int wal_decode_buffer_size = 512 * 1024;
8+
bool track_wal_io_timing = false;
9+
+backup_checkpoint_request_hook_type backup_checkpoint_request_hook = NULL;
10+
11+
#ifdef WAL_DEBUG
12+
bool XLOG_DEBUG = false;
13+
@@ -8801,6 +8802,12 @@ do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces,
14+
{
15+
bool checkpointfpw;
16+
17+
+ /*
18+
+ * Before we call RequestCheckpoint() we need to set
19+
+ * init_lsn for ptrack map
20+
+ */
21+
+ if (backup_checkpoint_request_hook)
22+
+ backup_checkpoint_request_hook();
23+
/*
24+
* Force a CHECKPOINT. Aside from being necessary to prevent torn
25+
* page problems, this guarantees that two successive backup runs
126
diff --git a/src/backend/backup/basebackup.c b/src/backend/backup/basebackup.c
2-
index 5fbbe5ffd20..3c9b99f851d 100644
27+
index 9a2bf59e84..ade9115651 100644
328
--- a/src/backend/backup/basebackup.c
429
+++ b/src/backend/backup/basebackup.c
530
@@ -220,6 +220,13 @@ static const struct exclude_list_item excludeFiles[] =
@@ -17,7 +42,7 @@ index 5fbbe5ffd20..3c9b99f851d 100644
1742
{NULL, false}
1843
};
1944
diff --git a/src/backend/storage/file/copydir.c b/src/backend/storage/file/copydir.c
20-
index d4fbe542077..b108416c708 100644
45+
index d4fbe54207..b108416c70 100644
2146
--- a/src/backend/storage/file/copydir.c
2247
+++ b/src/backend/storage/file/copydir.c
2348
@@ -27,6 +27,8 @@
@@ -40,7 +65,7 @@ index d4fbe542077..b108416c708 100644
4065
* Be paranoid here and fsync all files to ensure the copy is really done.
4166
* But if fsync is disabled, we're done.
4267
diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c
43-
index bf0f3ca76d1..7d9833a3604 100644
68+
index bf0f3ca76d..7d9833a360 100644
4469
--- a/src/backend/storage/smgr/md.c
4570
+++ b/src/backend/storage/smgr/md.c
4671
@@ -85,6 +85,8 @@ typedef struct _MdfdVec
@@ -92,7 +117,7 @@ index bf0f3ca76d1..7d9833a3604 100644
92117
}
93118

94119
diff --git a/src/backend/storage/sync/sync.c b/src/backend/storage/sync/sync.c
95-
index ab7137d0fff..bc40a763c05 100644
120+
index ab7137d0ff..bc40a763c0 100644
96121
--- a/src/backend/storage/sync/sync.c
97122
+++ b/src/backend/storage/sync/sync.c
98123
@@ -74,6 +74,8 @@ static MemoryContext pendingOpsCxt; /* context for the above */
@@ -115,7 +140,7 @@ index ab7137d0fff..bc40a763c05 100644
115140
sync_in_progress = false;
116141
}
117142
diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c
118-
index 9e6fd435f60..f2180b9f6de 100644
143+
index 9e6fd435f6..f2180b9f6d 100644
119144
--- a/src/bin/pg_checksums/pg_checksums.c
120145
+++ b/src/bin/pg_checksums/pg_checksums.c
121146
@@ -110,6 +110,11 @@ static const struct exclude_list_item skip[] = {
@@ -131,7 +156,7 @@ index 9e6fd435f60..f2180b9f6de 100644
131156
{"config_exec_params", true},
132157
#endif
133158
diff --git a/src/bin/pg_resetwal/pg_resetwal.c b/src/bin/pg_resetwal/pg_resetwal.c
134-
index e9dcb5a6d89..844b04d5e12 100644
159+
index e9dcb5a6d8..844b04d5e1 100644
135160
--- a/src/bin/pg_resetwal/pg_resetwal.c
136161
+++ b/src/bin/pg_resetwal/pg_resetwal.c
137162
@@ -86,6 +86,7 @@ static void FindEndOfXLOG(void);
@@ -193,10 +218,10 @@ index e9dcb5a6d89..844b04d5e12 100644
193218
/*
194219
* Remove existing archive status files
195220
diff --git a/src/bin/pg_rewind/filemap.c b/src/bin/pg_rewind/filemap.c
196-
index 255ddf2ffaf..1142c244926 100644
221+
index 4458324c9d..7d857467f7 100644
197222
--- a/src/bin/pg_rewind/filemap.c
198223
+++ b/src/bin/pg_rewind/filemap.c
199-
@@ -157,6 +157,10 @@ static const struct exclude_list_item excludeFiles[] =
224+
@@ -156,6 +156,10 @@ static const struct exclude_list_item excludeFiles[] =
200225
{"postmaster.pid", false},
201226
{"postmaster.opts", false},
202227

@@ -207,8 +232,22 @@ index 255ddf2ffaf..1142c244926 100644
207232
/* end of list */
208233
{NULL, false}
209234
};
235+
diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h
236+
index 76787a8267..2c662f4022 100644
237+
--- a/src/include/access/xlog.h
238+
+++ b/src/include/access/xlog.h
239+
@@ -57,6 +57,9 @@ extern PGDLLIMPORT int wal_decode_buffer_size;
240+
241+
extern PGDLLIMPORT int CheckPointSegments;
242+
243+
+typedef void (*backup_checkpoint_request_hook_type) (void);
244+
+extern PGDLLIMPORT backup_checkpoint_request_hook_type backup_checkpoint_request_hook;
245+
+
246+
/* Archive modes */
247+
typedef enum ArchiveMode
248+
{
210249
diff --git a/src/include/storage/copydir.h b/src/include/storage/copydir.h
211-
index a25e258f479..b20b9c76e8d 100644
250+
index a25e258f47..b20b9c76e8 100644
212251
--- a/src/include/storage/copydir.h
213252
+++ b/src/include/storage/copydir.h
214253
@@ -13,6 +13,9 @@
@@ -222,7 +261,7 @@ index a25e258f479..b20b9c76e8d 100644
222261
extern void copy_file(const char *fromfile, const char *tofile);
223262

224263
diff --git a/src/include/storage/md.h b/src/include/storage/md.h
225-
index 620f10abdeb..b36936871bd 100644
264+
index 620f10abde..b36936871b 100644
226265
--- a/src/include/storage/md.h
227266
+++ b/src/include/storage/md.h
228267
@@ -19,6 +19,13 @@
@@ -240,7 +279,7 @@ index 620f10abdeb..b36936871bd 100644
240279
extern void mdinit(void);
241280
extern void mdopen(SMgrRelation reln);
242281
diff --git a/src/include/storage/sync.h b/src/include/storage/sync.h
243-
index 9dee8fa6e5b..348ed53e4e2 100644
282+
index 9dee8fa6e5..348ed53e4e 100644
244283
--- a/src/include/storage/sync.h
245284
+++ b/src/include/storage/sync.h
246285
@@ -55,6 +55,9 @@ typedef struct FileTag

ptrack.c

+20
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ static copydir_hook_type prev_copydir_hook = NULL;
6464
static mdwrite_hook_type prev_mdwrite_hook = NULL;
6565
static mdextend_hook_type prev_mdextend_hook = NULL;
6666
static ProcessSyncRequests_hook_type prev_ProcessSyncRequests_hook = NULL;
67+
#if PG_VERSION_NUM >= 170000
68+
static backup_checkpoint_request_hook_type prev_backup_checkpoint_request_hook = NULL;
69+
#endif
6770

6871
void _PG_init(void);
6972

@@ -74,6 +77,9 @@ static void ptrack_mdwrite_hook(RelFileNodeBackend smgr_rnode,
7477
static void ptrack_mdextend_hook(RelFileNodeBackend smgr_rnode,
7578
ForkNumber forkno, BlockNumber blkno);
7679
static void ptrack_ProcessSyncRequests_hook(void);
80+
#if PG_VERSION_NUM >= 170000
81+
static void ptrack_backup_checkpoint_request_hook(void);
82+
#endif
7783

7884
static void ptrack_gather_filelist(List **filelist, char *path, Oid spcOid, Oid dbOid);
7985
static int ptrack_filelist_getnext(PtScanCtx * ctx);
@@ -141,6 +147,10 @@ _PG_init(void)
141147
mdextend_hook = ptrack_mdextend_hook;
142148
prev_ProcessSyncRequests_hook = ProcessSyncRequests_hook;
143149
ProcessSyncRequests_hook = ptrack_ProcessSyncRequests_hook;
150+
#if PG_VERSION_NUM >= 170000
151+
prev_backup_checkpoint_request_hook = backup_checkpoint_request_hook;
152+
backup_checkpoint_request_hook = ptrack_backup_checkpoint_request_hook;
153+
#endif
144154
}
145155

146156
#if PG_VERSION_NUM >= 150000
@@ -267,6 +277,16 @@ ptrack_ProcessSyncRequests_hook()
267277
prev_ProcessSyncRequests_hook();
268278
}
269279

280+
#if PG_VERSION_NUM >= 170000
281+
static void
282+
ptrack_backup_checkpoint_request_hook(void)
283+
{
284+
ptrack_set_init_lsn();
285+
286+
if (prev_backup_checkpoint_request_hook)
287+
prev_backup_checkpoint_request_hook();
288+
}
289+
#endif
270290
/*
271291
* Recursively walk through the path and add all data files to filelist.
272292
*/

0 commit comments

Comments
 (0)