@@ -930,7 +930,8 @@ static void XLogFileClose(void);
930
930
static void PreallocXlogFiles (XLogRecPtr endptr );
931
931
static void RemoveTempXlogFiles (void );
932
932
static void RemoveOldXlogFiles (XLogSegNo segno , XLogRecPtr lastredoptr , XLogRecPtr endptr );
933
- static void RemoveXlogFile (const char * segname , XLogRecPtr lastredoptr , XLogRecPtr endptr );
933
+ static void RemoveXlogFile (const char * segname , XLogSegNo recycleSegNo ,
934
+ XLogSegNo * endlogSegNo );
934
935
static void UpdateLastRemovedPtr (char * filename );
935
936
static void ValidateXLOGDirectoryStructure (void );
936
937
static void CleanupBackupHistory (void );
@@ -4055,6 +4056,12 @@ RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr lastredoptr, XLogRecPtr endptr)
4055
4056
DIR * xldir ;
4056
4057
struct dirent * xlde ;
4057
4058
char lastoff [MAXFNAMELEN ];
4059
+ XLogSegNo endlogSegNo ;
4060
+ XLogSegNo recycleSegNo ;
4061
+
4062
+ /* Initialize info about where to try to recycle to */
4063
+ XLByteToSeg (endptr , endlogSegNo , wal_segment_size );
4064
+ recycleSegNo = XLOGfileslop (lastredoptr );
4058
4065
4059
4066
/*
4060
4067
* Construct a filename of the last segment to be kept. The timeline ID
@@ -4093,7 +4100,7 @@ RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr lastredoptr, XLogRecPtr endptr)
4093
4100
/* Update the last removed location in shared memory first */
4094
4101
UpdateLastRemovedPtr (xlde -> d_name );
4095
4102
4096
- RemoveXlogFile (xlde -> d_name , lastredoptr , endptr );
4103
+ RemoveXlogFile (xlde -> d_name , recycleSegNo , & endlogSegNo );
4097
4104
}
4098
4105
}
4099
4106
}
@@ -4123,13 +4130,21 @@ RemoveNonParentXlogFiles(XLogRecPtr switchpoint, TimeLineID newTLI)
4123
4130
struct dirent * xlde ;
4124
4131
char switchseg [MAXFNAMELEN ];
4125
4132
XLogSegNo endLogSegNo ;
4133
+ XLogSegNo switchLogSegNo ;
4134
+ XLogSegNo recycleSegNo ;
4126
4135
4127
- XLByteToPrevSeg (switchpoint , endLogSegNo , wal_segment_size );
4136
+ /*
4137
+ * Initialize info about where to begin the work. This will recycle,
4138
+ * somewhat arbitrarily, 10 future segments.
4139
+ */
4140
+ XLByteToPrevSeg (switchpoint , switchLogSegNo , wal_segment_size );
4141
+ XLByteToSeg (switchpoint , endLogSegNo , wal_segment_size );
4142
+ recycleSegNo = endLogSegNo + 10 ;
4128
4143
4129
4144
/*
4130
4145
* Construct a filename of the last segment to be kept.
4131
4146
*/
4132
- XLogFileName (switchseg , newTLI , endLogSegNo , wal_segment_size );
4147
+ XLogFileName (switchseg , newTLI , switchLogSegNo , wal_segment_size );
4133
4148
4134
4149
elog (DEBUG2 , "attempting to remove WAL segments newer than log file %s" ,
4135
4150
switchseg );
@@ -4157,7 +4172,7 @@ RemoveNonParentXlogFiles(XLogRecPtr switchpoint, TimeLineID newTLI)
4157
4172
* - but seems safer to let them be archived and removed later.
4158
4173
*/
4159
4174
if (!XLogArchiveIsReady (xlde -> d_name ))
4160
- RemoveXlogFile (xlde -> d_name , InvalidXLogRecPtr , switchpoint );
4175
+ RemoveXlogFile (xlde -> d_name , recycleSegNo , & endLogSegNo );
4161
4176
}
4162
4177
}
4163
4178
@@ -4167,36 +4182,22 @@ RemoveNonParentXlogFiles(XLogRecPtr switchpoint, TimeLineID newTLI)
4167
4182
/*
4168
4183
* Recycle or remove a log file that's no longer needed.
4169
4184
*
4170
- * endptr is current (or recent) end of xlog, and lastredoptr is the
4171
- * redo pointer of the last checkpoint. These are used to determine
4172
- * whether we want to recycle rather than delete no-longer-wanted log files.
4173
- * If lastredoptr is not known, pass invalid, and the function will recycle,
4174
- * somewhat arbitrarily, 10 future segments.
4185
+ * segname is the name of the segment to recycle or remove. recycleSegNo
4186
+ * is the segment number to recycle up to. endlogSegNo is the segment
4187
+ * number of the current (or recent) end of WAL.
4188
+ *
4189
+ * endlogSegNo gets incremented if the segment is recycled so as it is not
4190
+ * checked again with future callers of this function.
4175
4191
*/
4176
4192
static void
4177
- RemoveXlogFile (const char * segname , XLogRecPtr lastredoptr , XLogRecPtr endptr )
4193
+ RemoveXlogFile (const char * segname , XLogSegNo recycleSegNo ,
4194
+ XLogSegNo * endlogSegNo )
4178
4195
{
4179
4196
char path [MAXPGPATH ];
4180
4197
#ifdef WIN32
4181
4198
char newpath [MAXPGPATH ];
4182
4199
#endif
4183
4200
struct stat statbuf ;
4184
- XLogSegNo endlogSegNo ;
4185
- XLogSegNo recycleSegNo ;
4186
-
4187
- if (wal_recycle )
4188
- {
4189
- /*
4190
- * Initialize info about where to try to recycle to.
4191
- */
4192
- XLByteToSeg (endptr , endlogSegNo , wal_segment_size );
4193
- if (lastredoptr == InvalidXLogRecPtr )
4194
- recycleSegNo = endlogSegNo + 10 ;
4195
- else
4196
- recycleSegNo = XLOGfileslop (lastredoptr );
4197
- }
4198
- else
4199
- recycleSegNo = 0 ; /* keep compiler quiet */
4200
4201
4201
4202
snprintf (path , MAXPGPATH , XLOGDIR "/%s" , segname );
4202
4203
@@ -4206,17 +4207,17 @@ RemoveXlogFile(const char *segname, XLogRecPtr lastredoptr, XLogRecPtr endptr)
4206
4207
* symbolic links pointing to a separate archive directory.
4207
4208
*/
4208
4209
if (wal_recycle &&
4209
- endlogSegNo <= recycleSegNo &&
4210
+ * endlogSegNo <= recycleSegNo &&
4210
4211
lstat (path , & statbuf ) == 0 && S_ISREG (statbuf .st_mode ) &&
4211
- InstallXLogFileSegment (& endlogSegNo , path ,
4212
+ InstallXLogFileSegment (endlogSegNo , path ,
4212
4213
true, recycleSegNo , true))
4213
4214
{
4214
4215
ereport (DEBUG2 ,
4215
4216
(errmsg ("recycled write-ahead log file \"%s\"" ,
4216
4217
segname )));
4217
4218
CheckpointStats .ckpt_segs_recycled ++ ;
4218
4219
/* Needn't recheck that slot on future iterations */
4219
- endlogSegNo ++ ;
4220
+ ( * endlogSegNo ) ++ ;
4220
4221
}
4221
4222
else
4222
4223
{
0 commit comments