Skip to content

Commit aa8bd08

Browse files
committed
Revert "Avoid creating archive status ".ready" files too early"
This reverts commit 515e3d8 and equivalent commits in back branches. This solution to the problem has a number of problems, so we'll try again with a different approach. Per note from Andres Freund Discussion: https://postgr.es/m/20210831042949.52eqp5xwbxgrfank@alap3.anarazel.de
1 parent 69d670e commit aa8bd08

File tree

4 files changed

+10
-215
lines changed

4 files changed

+10
-215
lines changed

src/backend/access/transam/xlog.c

Lines changed: 10 additions & 206 deletions
Original file line numberDiff line numberDiff line change
@@ -733,18 +733,6 @@ typedef struct XLogCtlData
733733
XLogRecPtr lastFpwDisableRecPtr;
734734

735735
slock_t info_lck; /* locks shared variables shown above */
736-
737-
/*
738-
* Variables used to track segment-boundary-crossing WAL records. See
739-
* RegisterSegmentBoundary. Protected by segtrack_lck.
740-
*/
741-
XLogSegNo lastNotifiedSeg;
742-
XLogSegNo earliestSegBoundary;
743-
XLogRecPtr earliestSegBoundaryEndPtr;
744-
XLogSegNo latestSegBoundary;
745-
XLogRecPtr latestSegBoundaryEndPtr;
746-
747-
slock_t segtrack_lck; /* locks shared variables shown above */
748736
} XLogCtlData;
749737

750738
static XLogCtlData *XLogCtl = NULL;
@@ -943,7 +931,6 @@ static void RemoveXlogFile(const char *segname, XLogSegNo recycleSegNo,
943931
XLogSegNo *endlogSegNo);
944932
static void UpdateLastRemovedPtr(char *filename);
945933
static void ValidateXLOGDirectoryStructure(void);
946-
static void RegisterSegmentBoundary(XLogSegNo seg, XLogRecPtr pos);
947934
static void CleanupBackupHistory(void);
948935
static void UpdateMinRecoveryPoint(XLogRecPtr lsn, bool force);
949936
static XLogRecord *ReadRecord(XLogReaderState *xlogreader,
@@ -1178,56 +1165,23 @@ XLogInsertRecord(XLogRecData *rdata,
11781165
END_CRIT_SECTION();
11791166

11801167
/*
1181-
* If we crossed page boundary, update LogwrtRqst.Write; if we crossed
1182-
* segment boundary, register that and wake up walwriter.
1168+
* Update shared LogwrtRqst.Write, if we crossed page boundary.
11831169
*/
11841170
if (StartPos / XLOG_BLCKSZ != EndPos / XLOG_BLCKSZ)
11851171
{
1186-
XLogSegNo StartSeg;
1187-
XLogSegNo EndSeg;
1188-
1189-
XLByteToSeg(StartPos, StartSeg, wal_segment_size);
1190-
XLByteToSeg(EndPos, EndSeg, wal_segment_size);
1191-
1192-
/*
1193-
* Register our crossing the segment boundary if that occurred.
1194-
*
1195-
* Note that we did not use XLByteToPrevSeg() for determining the
1196-
* ending segment. This is so that a record that fits perfectly into
1197-
* the end of the segment causes the latter to get marked ready for
1198-
* archival immediately.
1199-
*/
1200-
if (StartSeg != EndSeg && XLogArchivingActive())
1201-
RegisterSegmentBoundary(EndSeg, EndPos);
1202-
1203-
/*
1204-
* Advance LogwrtRqst.Write so that it includes new block(s).
1205-
*
1206-
* We do this after registering the segment boundary so that the
1207-
* comparison with the flushed pointer below can use the latest value
1208-
* known globally.
1209-
*/
12101172
SpinLockAcquire(&XLogCtl->info_lck);
1173+
/* advance global request to include new block(s) */
12111174
if (XLogCtl->LogwrtRqst.Write < EndPos)
12121175
XLogCtl->LogwrtRqst.Write = EndPos;
12131176
/* update local result copy while I have the chance */
12141177
LogwrtResult = XLogCtl->LogwrtResult;
12151178
SpinLockRelease(&XLogCtl->info_lck);
1216-
1217-
/*
1218-
* There's a chance that the record was already flushed to disk and we
1219-
* missed marking segments as ready for archive. If this happens, we
1220-
* nudge the WALWriter, which will take care of notifying segments as
1221-
* needed.
1222-
*/
1223-
if (StartSeg != EndSeg && XLogArchivingActive() &&
1224-
LogwrtResult.Flush >= EndPos && ProcGlobal->walwriterLatch)
1225-
SetLatch(ProcGlobal->walwriterLatch);
12261179
}
12271180

12281181
/*
12291182
* If this was an XLOG_SWITCH record, flush the record and the empty
1230-
* padding space that fills the rest of the segment.
1183+
* padding space that fills the rest of the segment, and perform
1184+
* end-of-segment actions (eg, notifying archiver).
12311185
*/
12321186
if (isLogSwitch)
12331187
{
@@ -2479,7 +2433,6 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
24792433

24802434
/* We should always be inside a critical section here */
24812435
Assert(CritSectionCount > 0);
2482-
Assert(LWLockHeldByMe(WALWriteLock));
24832436

24842437
/*
24852438
* Update local LogwrtResult (caller probably did this already, but...)
@@ -2646,12 +2599,11 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
26462599
* later. Doing it here ensures that one and only one backend will
26472600
* perform this fsync.
26482601
*
2649-
* If WAL archiving is active, we attempt to notify the archiver
2650-
* of any segments that are now ready for archival.
2651-
*
2652-
* This is also the right place to update the timer for
2653-
* archive_timeout and to signal for a checkpoint if too many
2654-
* logfile segments have been used since the last checkpoint.
2602+
* This is also the right place to notify the Archiver that the
2603+
* segment is ready to copy to archival storage, and to update the
2604+
* timer for archive_timeout, and to signal for a checkpoint if
2605+
* too many logfile segments have been used since the last
2606+
* checkpoint.
26552607
*/
26562608
if (finishing_seg)
26572609
{
@@ -2663,7 +2615,7 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
26632615
LogwrtResult.Flush = LogwrtResult.Write; /* end of page */
26642616

26652617
if (XLogArchivingActive())
2666-
NotifySegmentsReadyForArchive(LogwrtResult.Flush);
2618+
XLogArchiveNotifySeg(openLogSegNo);
26672619

26682620
XLogCtl->lastSegSwitchTime = (pg_time_t) time(NULL);
26692621
XLogCtl->lastSegSwitchLSN = LogwrtResult.Flush;
@@ -2751,9 +2703,6 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
27512703
XLogCtl->LogwrtRqst.Flush = LogwrtResult.Flush;
27522704
SpinLockRelease(&XLogCtl->info_lck);
27532705
}
2754-
2755-
if (XLogArchivingActive())
2756-
NotifySegmentsReadyForArchive(LogwrtResult.Flush);
27572706
}
27582707

27592708
/*
@@ -4375,129 +4324,6 @@ ValidateXLOGDirectoryStructure(void)
43754324
}
43764325
}
43774326

4378-
/*
4379-
* RegisterSegmentBoundary
4380-
*
4381-
* WAL records that are split across a segment boundary require special
4382-
* treatment for archiving: the initial segment must not be archived until
4383-
* the end segment has been flushed, in case we crash before we have
4384-
* the chance to flush the end segment (because after recovery we would
4385-
* overwrite that WAL record with a different one, and so the file we
4386-
* archived no longer represents truth.) This also applies to streaming
4387-
* physical replication.
4388-
*
4389-
* To handle this, we keep track of the LSN of WAL records that cross
4390-
* segment boundaries. Two such are sufficient: the ones with the
4391-
* earliest and the latest end pointers we know about, since the flush
4392-
* position advances monotonically. WAL record writers register
4393-
* boundary-crossing records here, which is used by .ready file creation
4394-
* to delay until the end segment is known flushed.
4395-
*/
4396-
static void
4397-
RegisterSegmentBoundary(XLogSegNo seg, XLogRecPtr endpos)
4398-
{
4399-
XLogSegNo segno PG_USED_FOR_ASSERTS_ONLY;
4400-
4401-
/* verify caller computed segment number correctly */
4402-
AssertArg((XLByteToSeg(endpos, segno, wal_segment_size), segno == seg));
4403-
4404-
SpinLockAcquire(&XLogCtl->segtrack_lck);
4405-
4406-
/*
4407-
* If no segment boundaries are registered, store the new segment boundary
4408-
* in earliestSegBoundary. Otherwise, store the greater segment
4409-
* boundaries in latestSegBoundary.
4410-
*/
4411-
if (XLogCtl->earliestSegBoundary == MaxXLogSegNo)
4412-
{
4413-
XLogCtl->earliestSegBoundary = seg;
4414-
XLogCtl->earliestSegBoundaryEndPtr = endpos;
4415-
}
4416-
else if (seg > XLogCtl->earliestSegBoundary &&
4417-
(XLogCtl->latestSegBoundary == MaxXLogSegNo ||
4418-
seg > XLogCtl->latestSegBoundary))
4419-
{
4420-
XLogCtl->latestSegBoundary = seg;
4421-
XLogCtl->latestSegBoundaryEndPtr = endpos;
4422-
}
4423-
4424-
SpinLockRelease(&XLogCtl->segtrack_lck);
4425-
}
4426-
4427-
/*
4428-
* NotifySegmentsReadyForArchive
4429-
*
4430-
* Mark segments as ready for archival, given that it is safe to do so.
4431-
* This function is idempotent.
4432-
*/
4433-
void
4434-
NotifySegmentsReadyForArchive(XLogRecPtr flushRecPtr)
4435-
{
4436-
XLogSegNo latest_boundary_seg;
4437-
XLogSegNo last_notified;
4438-
XLogSegNo flushed_seg;
4439-
XLogSegNo seg;
4440-
bool keep_latest;
4441-
4442-
XLByteToSeg(flushRecPtr, flushed_seg, wal_segment_size);
4443-
4444-
SpinLockAcquire(&XLogCtl->segtrack_lck);
4445-
4446-
if (XLogCtl->latestSegBoundary <= flushed_seg &&
4447-
XLogCtl->latestSegBoundaryEndPtr <= flushRecPtr)
4448-
{
4449-
latest_boundary_seg = XLogCtl->latestSegBoundary;
4450-
keep_latest = false;
4451-
}
4452-
else if (XLogCtl->earliestSegBoundary <= flushed_seg &&
4453-
XLogCtl->earliestSegBoundaryEndPtr <= flushRecPtr)
4454-
{
4455-
latest_boundary_seg = XLogCtl->earliestSegBoundary;
4456-
keep_latest = true;
4457-
}
4458-
else
4459-
{
4460-
SpinLockRelease(&XLogCtl->segtrack_lck);
4461-
return;
4462-
}
4463-
4464-
last_notified = XLogCtl->lastNotifiedSeg;
4465-
4466-
/*
4467-
* Update shared memory and discard segment boundaries that are no longer
4468-
* needed.
4469-
*
4470-
* It is safe to update shared memory before we attempt to create the
4471-
* .ready files. If our calls to XLogArchiveNotifySeg() fail,
4472-
* RemoveOldXlogFiles() will retry it as needed.
4473-
*/
4474-
if (last_notified < latest_boundary_seg - 1)
4475-
XLogCtl->lastNotifiedSeg = latest_boundary_seg - 1;
4476-
4477-
if (keep_latest)
4478-
{
4479-
XLogCtl->earliestSegBoundary = XLogCtl->latestSegBoundary;
4480-
XLogCtl->earliestSegBoundaryEndPtr = XLogCtl->latestSegBoundaryEndPtr;
4481-
}
4482-
else
4483-
{
4484-
XLogCtl->earliestSegBoundary = MaxXLogSegNo;
4485-
XLogCtl->earliestSegBoundaryEndPtr = InvalidXLogRecPtr;
4486-
}
4487-
4488-
XLogCtl->latestSegBoundary = MaxXLogSegNo;
4489-
XLogCtl->latestSegBoundaryEndPtr = InvalidXLogRecPtr;
4490-
4491-
SpinLockRelease(&XLogCtl->segtrack_lck);
4492-
4493-
/*
4494-
* Notify archiver about segments that are ready for archival (by creating
4495-
* the corresponding .ready files).
4496-
*/
4497-
for (seg = last_notified + 1; seg < latest_boundary_seg; seg++)
4498-
XLogArchiveNotifySeg(seg);
4499-
}
4500-
45014327
/*
45024328
* Remove previous backup history files. This also retries creation of
45034329
* .ready files for any backup history files for which XLogArchiveNotify
@@ -5399,17 +5225,9 @@ XLOGShmemInit(void)
53995225

54005226
SpinLockInit(&XLogCtl->Insert.insertpos_lck);
54015227
SpinLockInit(&XLogCtl->info_lck);
5402-
SpinLockInit(&XLogCtl->segtrack_lck);
54035228
SpinLockInit(&XLogCtl->ulsn_lck);
54045229
InitSharedLatch(&XLogCtl->recoveryWakeupLatch);
54055230
ConditionVariableInit(&XLogCtl->recoveryNotPausedCV);
5406-
5407-
/* Initialize stuff for marking segments as ready for archival. */
5408-
XLogCtl->lastNotifiedSeg = MaxXLogSegNo;
5409-
XLogCtl->earliestSegBoundary = MaxXLogSegNo;
5410-
XLogCtl->earliestSegBoundaryEndPtr = InvalidXLogRecPtr;
5411-
XLogCtl->latestSegBoundary = MaxXLogSegNo;
5412-
XLogCtl->latestSegBoundaryEndPtr = InvalidXLogRecPtr;
54135231
}
54145232

54155233
/*
@@ -8040,20 +7858,6 @@ StartupXLOG(void)
80407858
XLogCtl->LogwrtRqst.Write = EndOfLog;
80417859
XLogCtl->LogwrtRqst.Flush = EndOfLog;
80427860

8043-
/*
8044-
* Initialize XLogCtl->lastNotifiedSeg to the previous WAL file.
8045-
*/
8046-
if (XLogArchivingActive())
8047-
{
8048-
XLogSegNo EndOfLogSeg;
8049-
8050-
XLByteToSeg(EndOfLog, EndOfLogSeg, wal_segment_size);
8051-
8052-
SpinLockAcquire(&XLogCtl->segtrack_lck);
8053-
XLogCtl->lastNotifiedSeg = EndOfLogSeg - 1;
8054-
SpinLockRelease(&XLogCtl->segtrack_lck);
8055-
}
8056-
80577861
/*
80587862
* Update full_page_writes in shared memory and write an XLOG_FPW_CHANGE
80597863
* record before resource manager writes cleanup WAL records or checkpoint

src/backend/postmaster/walwriter.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,6 @@ WalWriterMain(void)
248248
/* Process any signals received recently */
249249
HandleWalWriterInterrupts();
250250

251-
/*
252-
* Notify the archiver of any WAL segments that are ready. We do this
253-
* here to handle a race condition where WAL is flushed to disk prior
254-
* to registering the segment boundary.
255-
*/
256-
NotifySegmentsReadyForArchive(GetFlushRecPtr());
257-
258251
/*
259252
* Do what we're here for; then, if XLogBackgroundFlush() found useful
260253
* work to do, reset hibernation counter.

src/include/access/xlog.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ extern XLogRecPtr GetInsertRecPtr(void);
349349
extern XLogRecPtr GetFlushRecPtr(void);
350350
extern XLogRecPtr GetLastImportantRecPtr(void);
351351
extern void RemovePromoteSignalFiles(void);
352-
extern void NotifySegmentsReadyForArchive(XLogRecPtr flushRecPtr);
353352

354353
extern bool PromoteIsTriggered(void);
355354
extern bool CheckPromoteSignal(void);

src/include/access/xlogdefs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ typedef uint64 XLogRecPtr;
4646
* XLogSegNo - physical log file sequence number.
4747
*/
4848
typedef uint64 XLogSegNo;
49-
#define MaxXLogSegNo ((XLogSegNo) 0xFFFFFFFFFFFFFFFF)
5049

5150
/*
5251
* TimeLineID (TLI) - identifies different database histories to prevent

0 commit comments

Comments
 (0)