Skip to content

Commit 518621c

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 742b30c commit 518621c

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
@@ -725,18 +725,6 @@ typedef struct XLogCtlData
725725
XLogRecPtr lastFpwDisableRecPtr;
726726

727727
slock_t info_lck; /* locks shared variables shown above */
728-
729-
/*
730-
* Variables used to track segment-boundary-crossing WAL records. See
731-
* RegisterSegmentBoundary. Protected by segtrack_lck.
732-
*/
733-
XLogSegNo lastNotifiedSeg;
734-
XLogSegNo earliestSegBoundary;
735-
XLogRecPtr earliestSegBoundaryEndPtr;
736-
XLogSegNo latestSegBoundary;
737-
XLogRecPtr latestSegBoundaryEndPtr;
738-
739-
slock_t segtrack_lck; /* locks shared variables shown above */
740728
} XLogCtlData;
741729

742730
static XLogCtlData *XLogCtl = NULL;
@@ -933,7 +921,6 @@ static void RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr lastredoptr, XLogRecP
933921
static void RemoveXlogFile(const char *segname, XLogRecPtr lastredoptr, XLogRecPtr endptr);
934922
static void UpdateLastRemovedPtr(char *filename);
935923
static void ValidateXLOGDirectoryStructure(void);
936-
static void RegisterSegmentBoundary(XLogSegNo seg, XLogRecPtr pos);
937924
static void CleanupBackupHistory(void);
938925
static void UpdateMinRecoveryPoint(XLogRecPtr lsn, bool force);
939926
static XLogRecord *ReadRecord(XLogReaderState *xlogreader,
@@ -1167,56 +1154,23 @@ XLogInsertRecord(XLogRecData *rdata,
11671154
END_CRIT_SECTION();
11681155

11691156
/*
1170-
* If we crossed page boundary, update LogwrtRqst.Write; if we crossed
1171-
* segment boundary, register that and wake up walwriter.
1157+
* Update shared LogwrtRqst.Write, if we crossed page boundary.
11721158
*/
11731159
if (StartPos / XLOG_BLCKSZ != EndPos / XLOG_BLCKSZ)
11741160
{
1175-
XLogSegNo StartSeg;
1176-
XLogSegNo EndSeg;
1177-
1178-
XLByteToSeg(StartPos, StartSeg, wal_segment_size);
1179-
XLByteToSeg(EndPos, EndSeg, wal_segment_size);
1180-
1181-
/*
1182-
* Register our crossing the segment boundary if that occurred.
1183-
*
1184-
* Note that we did not use XLByteToPrevSeg() for determining the
1185-
* ending segment. This is so that a record that fits perfectly into
1186-
* the end of the segment causes the latter to get marked ready for
1187-
* archival immediately.
1188-
*/
1189-
if (StartSeg != EndSeg && XLogArchivingActive())
1190-
RegisterSegmentBoundary(EndSeg, EndPos);
1191-
1192-
/*
1193-
* Advance LogwrtRqst.Write so that it includes new block(s).
1194-
*
1195-
* We do this after registering the segment boundary so that the
1196-
* comparison with the flushed pointer below can use the latest value
1197-
* known globally.
1198-
*/
11991161
SpinLockAcquire(&XLogCtl->info_lck);
1162+
/* advance global request to include new block(s) */
12001163
if (XLogCtl->LogwrtRqst.Write < EndPos)
12011164
XLogCtl->LogwrtRqst.Write = EndPos;
12021165
/* update local result copy while I have the chance */
12031166
LogwrtResult = XLogCtl->LogwrtResult;
12041167
SpinLockRelease(&XLogCtl->info_lck);
1205-
1206-
/*
1207-
* There's a chance that the record was already flushed to disk and we
1208-
* missed marking segments as ready for archive. If this happens, we
1209-
* nudge the WALWriter, which will take care of notifying segments as
1210-
* needed.
1211-
*/
1212-
if (StartSeg != EndSeg && XLogArchivingActive() &&
1213-
LogwrtResult.Flush >= EndPos && ProcGlobal->walwriterLatch)
1214-
SetLatch(ProcGlobal->walwriterLatch);
12151168
}
12161169

12171170
/*
12181171
* If this was an XLOG_SWITCH record, flush the record and the empty
1219-
* padding space that fills the rest of the segment.
1172+
* padding space that fills the rest of the segment, and perform
1173+
* end-of-segment actions (eg, notifying archiver).
12201174
*/
12211175
if (isLogSwitch)
12221176
{
@@ -2468,7 +2422,6 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
24682422

24692423
/* We should always be inside a critical section here */
24702424
Assert(CritSectionCount > 0);
2471-
Assert(LWLockHeldByMe(WALWriteLock));
24722425

24732426
/*
24742427
* Update local LogwrtResult (caller probably did this already, but...)
@@ -2614,12 +2567,11 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
26142567
* later. Doing it here ensures that one and only one backend will
26152568
* perform this fsync.
26162569
*
2617-
* If WAL archiving is active, we attempt to notify the archiver
2618-
* of any segments that are now ready for archival.
2619-
*
2620-
* This is also the right place to update the timer for
2621-
* archive_timeout and to signal for a checkpoint if too many
2622-
* logfile segments have been used since the last checkpoint.
2570+
* This is also the right place to notify the Archiver that the
2571+
* segment is ready to copy to archival storage, and to update the
2572+
* timer for archive_timeout, and to signal for a checkpoint if
2573+
* too many logfile segments have been used since the last
2574+
* checkpoint.
26232575
*/
26242576
if (finishing_seg)
26252577
{
@@ -2631,7 +2583,7 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
26312583
LogwrtResult.Flush = LogwrtResult.Write; /* end of page */
26322584

26332585
if (XLogArchivingActive())
2634-
NotifySegmentsReadyForArchive(LogwrtResult.Flush);
2586+
XLogArchiveNotifySeg(openLogSegNo);
26352587

26362588
XLogCtl->lastSegSwitchTime = (pg_time_t) time(NULL);
26372589
XLogCtl->lastSegSwitchLSN = LogwrtResult.Flush;
@@ -2719,9 +2671,6 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
27192671
XLogCtl->LogwrtRqst.Flush = LogwrtResult.Flush;
27202672
SpinLockRelease(&XLogCtl->info_lck);
27212673
}
2722-
2723-
if (XLogArchivingActive())
2724-
NotifySegmentsReadyForArchive(LogwrtResult.Flush);
27252674
}
27262675

27272676
/*
@@ -4331,129 +4280,6 @@ ValidateXLOGDirectoryStructure(void)
43314280
}
43324281
}
43334282

4334-
/*
4335-
* RegisterSegmentBoundary
4336-
*
4337-
* WAL records that are split across a segment boundary require special
4338-
* treatment for archiving: the initial segment must not be archived until
4339-
* the end segment has been flushed, in case we crash before we have
4340-
* the chance to flush the end segment (because after recovery we would
4341-
* overwrite that WAL record with a different one, and so the file we
4342-
* archived no longer represents truth.) This also applies to streaming
4343-
* physical replication.
4344-
*
4345-
* To handle this, we keep track of the LSN of WAL records that cross
4346-
* segment boundaries. Two such are sufficient: the ones with the
4347-
* earliest and the latest end pointers we know about, since the flush
4348-
* position advances monotonically. WAL record writers register
4349-
* boundary-crossing records here, which is used by .ready file creation
4350-
* to delay until the end segment is known flushed.
4351-
*/
4352-
static void
4353-
RegisterSegmentBoundary(XLogSegNo seg, XLogRecPtr endpos)
4354-
{
4355-
XLogSegNo segno PG_USED_FOR_ASSERTS_ONLY;
4356-
4357-
/* verify caller computed segment number correctly */
4358-
AssertArg((XLByteToSeg(endpos, segno, wal_segment_size), segno == seg));
4359-
4360-
SpinLockAcquire(&XLogCtl->segtrack_lck);
4361-
4362-
/*
4363-
* If no segment boundaries are registered, store the new segment boundary
4364-
* in earliestSegBoundary. Otherwise, store the greater segment
4365-
* boundaries in latestSegBoundary.
4366-
*/
4367-
if (XLogCtl->earliestSegBoundary == MaxXLogSegNo)
4368-
{
4369-
XLogCtl->earliestSegBoundary = seg;
4370-
XLogCtl->earliestSegBoundaryEndPtr = endpos;
4371-
}
4372-
else if (seg > XLogCtl->earliestSegBoundary &&
4373-
(XLogCtl->latestSegBoundary == MaxXLogSegNo ||
4374-
seg > XLogCtl->latestSegBoundary))
4375-
{
4376-
XLogCtl->latestSegBoundary = seg;
4377-
XLogCtl->latestSegBoundaryEndPtr = endpos;
4378-
}
4379-
4380-
SpinLockRelease(&XLogCtl->segtrack_lck);
4381-
}
4382-
4383-
/*
4384-
* NotifySegmentsReadyForArchive
4385-
*
4386-
* Mark segments as ready for archival, given that it is safe to do so.
4387-
* This function is idempotent.
4388-
*/
4389-
void
4390-
NotifySegmentsReadyForArchive(XLogRecPtr flushRecPtr)
4391-
{
4392-
XLogSegNo latest_boundary_seg;
4393-
XLogSegNo last_notified;
4394-
XLogSegNo flushed_seg;
4395-
XLogSegNo seg;
4396-
bool keep_latest;
4397-
4398-
XLByteToSeg(flushRecPtr, flushed_seg, wal_segment_size);
4399-
4400-
SpinLockAcquire(&XLogCtl->segtrack_lck);
4401-
4402-
if (XLogCtl->latestSegBoundary <= flushed_seg &&
4403-
XLogCtl->latestSegBoundaryEndPtr <= flushRecPtr)
4404-
{
4405-
latest_boundary_seg = XLogCtl->latestSegBoundary;
4406-
keep_latest = false;
4407-
}
4408-
else if (XLogCtl->earliestSegBoundary <= flushed_seg &&
4409-
XLogCtl->earliestSegBoundaryEndPtr <= flushRecPtr)
4410-
{
4411-
latest_boundary_seg = XLogCtl->earliestSegBoundary;
4412-
keep_latest = true;
4413-
}
4414-
else
4415-
{
4416-
SpinLockRelease(&XLogCtl->segtrack_lck);
4417-
return;
4418-
}
4419-
4420-
last_notified = XLogCtl->lastNotifiedSeg;
4421-
4422-
/*
4423-
* Update shared memory and discard segment boundaries that are no longer
4424-
* needed.
4425-
*
4426-
* It is safe to update shared memory before we attempt to create the
4427-
* .ready files. If our calls to XLogArchiveNotifySeg() fail,
4428-
* RemoveOldXlogFiles() will retry it as needed.
4429-
*/
4430-
if (last_notified < latest_boundary_seg - 1)
4431-
XLogCtl->lastNotifiedSeg = latest_boundary_seg - 1;
4432-
4433-
if (keep_latest)
4434-
{
4435-
XLogCtl->earliestSegBoundary = XLogCtl->latestSegBoundary;
4436-
XLogCtl->earliestSegBoundaryEndPtr = XLogCtl->latestSegBoundaryEndPtr;
4437-
}
4438-
else
4439-
{
4440-
XLogCtl->earliestSegBoundary = MaxXLogSegNo;
4441-
XLogCtl->earliestSegBoundaryEndPtr = InvalidXLogRecPtr;
4442-
}
4443-
4444-
XLogCtl->latestSegBoundary = MaxXLogSegNo;
4445-
XLogCtl->latestSegBoundaryEndPtr = InvalidXLogRecPtr;
4446-
4447-
SpinLockRelease(&XLogCtl->segtrack_lck);
4448-
4449-
/*
4450-
* Notify archiver about segments that are ready for archival (by creating
4451-
* the corresponding .ready files).
4452-
*/
4453-
for (seg = last_notified + 1; seg < latest_boundary_seg; seg++)
4454-
XLogArchiveNotifySeg(seg);
4455-
}
4456-
44574283
/*
44584284
* Remove previous backup history files. This also retries creation of
44594285
* .ready files for any backup history files for which XLogArchiveNotify
@@ -5355,16 +5181,8 @@ XLOGShmemInit(void)
53555181

53565182
SpinLockInit(&XLogCtl->Insert.insertpos_lck);
53575183
SpinLockInit(&XLogCtl->info_lck);
5358-
SpinLockInit(&XLogCtl->segtrack_lck);
53595184
SpinLockInit(&XLogCtl->ulsn_lck);
53605185
InitSharedLatch(&XLogCtl->recoveryWakeupLatch);
5361-
5362-
/* Initialize stuff for marking segments as ready for archival. */
5363-
XLogCtl->lastNotifiedSeg = MaxXLogSegNo;
5364-
XLogCtl->earliestSegBoundary = MaxXLogSegNo;
5365-
XLogCtl->earliestSegBoundaryEndPtr = InvalidXLogRecPtr;
5366-
XLogCtl->latestSegBoundary = MaxXLogSegNo;
5367-
XLogCtl->latestSegBoundaryEndPtr = InvalidXLogRecPtr;
53685186
}
53695187

53705188
/*
@@ -7888,20 +7706,6 @@ StartupXLOG(void)
78887706
XLogCtl->LogwrtRqst.Write = EndOfLog;
78897707
XLogCtl->LogwrtRqst.Flush = EndOfLog;
78907708

7891-
/*
7892-
* Initialize XLogCtl->lastNotifiedSeg to the previous WAL file.
7893-
*/
7894-
if (XLogArchivingActive())
7895-
{
7896-
XLogSegNo EndOfLogSeg;
7897-
7898-
XLByteToSeg(EndOfLog, EndOfLogSeg, wal_segment_size);
7899-
7900-
SpinLockAcquire(&XLogCtl->segtrack_lck);
7901-
XLogCtl->lastNotifiedSeg = EndOfLogSeg - 1;
7902-
SpinLockRelease(&XLogCtl->segtrack_lck);
7903-
}
7904-
79057709
/*
79067710
* Update full_page_writes in shared memory and write an XLOG_FPW_CHANGE
79077711
* 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
@@ -234,13 +234,6 @@ WalWriterMain(void)
234234

235235
HandleMainLoopInterrupts();
236236

237-
/*
238-
* Notify the archiver of any WAL segments that are ready. We do this
239-
* here to handle a race condition where WAL is flushed to disk prior
240-
* to registering the segment boundary.
241-
*/
242-
NotifySegmentsReadyForArchive(GetFlushRecPtr());
243-
244237
/*
245238
* Do what we're here for; then, if XLogBackgroundFlush() found useful
246239
* 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
@@ -339,7 +339,6 @@ extern XLogRecPtr GetInsertRecPtr(void);
339339
extern XLogRecPtr GetFlushRecPtr(void);
340340
extern XLogRecPtr GetLastImportantRecPtr(void);
341341
extern void RemovePromoteSignalFiles(void);
342-
extern void NotifySegmentsReadyForArchive(XLogRecPtr flushRecPtr);
343342

344343
extern bool PromoteIsTriggered(void);
345344
extern bool CheckPromoteSignal(void);

src/include/access/xlogdefs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ typedef uint64 XLogRecPtr;
3939
* XLogSegNo - physical log file sequence number.
4040
*/
4141
typedef uint64 XLogSegNo;
42-
#define MaxXLogSegNo ((XLogSegNo) 0xFFFFFFFFFFFFFFFF)
4342

4443
/*
4544
* TimeLineID (TLI) - identifies different database histories to prevent

0 commit comments

Comments
 (0)