@@ -134,15 +134,19 @@ do_retention_purge(void)
134
134
bool keep_next_backup = true; /* Do not delete first full backup */
135
135
bool backup_deleted = false; /* At least one backup was deleted */
136
136
137
- if (retention_redundancy > 0 )
138
- elog (LOG , "REDUNDANCY=%u" , retention_redundancy );
139
- if (retention_window > 0 )
140
- elog (LOG , "WINDOW=%u" , retention_window );
141
-
142
- if (retention_redundancy == 0 && retention_window == 0 && !delete_wal )
137
+ if (delete_expired )
143
138
{
144
- elog (WARNING , "Retention policy is not set" );
145
- return 0 ;
139
+ if (retention_redundancy > 0 )
140
+ elog (LOG , "REDUNDANCY=%u" , retention_redundancy );
141
+ if (retention_window > 0 )
142
+ elog (LOG , "WINDOW=%u" , retention_window );
143
+
144
+ if (retention_redundancy == 0 && retention_window == 0 )
145
+ {
146
+ elog (WARNING , "Retention policy is not set" );
147
+ if (!delete_wal )
148
+ return 0 ;
149
+ }
146
150
}
147
151
148
152
/* Get exclusive lock of backup catalog */
@@ -157,52 +161,67 @@ do_retention_purge(void)
157
161
}
158
162
159
163
/* Find target backups to be deleted */
160
- backup_num = 0 ;
161
- for (i = 0 ; i < parray_num (backup_list ); i ++ )
164
+ if (delete_expired && (retention_redundancy > 0 || retention_window > 0 ))
162
165
{
163
- pgBackup * backup = (pgBackup * ) parray_get (backup_list , i );
164
- uint32 backup_num_evaluate = backup_num ;
165
-
166
- /* Consider only validated and correct backups */
167
- if (backup -> status != BACKUP_STATUS_OK )
168
- continue ;
169
-
170
- /*
171
- * When a validate full backup was found, we can delete the
172
- * backup that is older than it using the number of generations.
173
- */
174
- if (backup -> backup_mode == BACKUP_MODE_FULL )
175
- backup_num ++ ;
176
-
177
- /* Evaluate if this backup is eligible for removal */
178
- if (keep_next_backup ||
179
- backup_num_evaluate + 1 <= retention_redundancy ||
180
- (retention_window > 0 && backup -> recovery_time >= days_threshold ))
166
+ backup_num = 0 ;
167
+ for (i = 0 ; i < parray_num (backup_list ); i ++ )
181
168
{
182
- /* Save LSN and Timeline to remove unnecessary WAL segments */
183
- oldest_lsn = backup -> start_lsn ;
184
- oldest_tli = backup -> tli ;
169
+ pgBackup * backup = (pgBackup * ) parray_get (backup_list , i );
170
+ uint32 backup_num_evaluate = backup_num ;
185
171
186
- /* Save parent backup of this incremental backup */
187
- if (backup -> backup_mode != BACKUP_MODE_FULL )
188
- keep_next_backup = true ;
172
+ /* Consider only validated and correct backups */
173
+ if (backup -> status != BACKUP_STATUS_OK )
174
+ continue ;
189
175
/*
190
- * Previous incremental backup was kept or this is first backup
191
- * so do not delete this backup .
176
+ * When a validate full backup was found, we can delete the
177
+ * backup that is older than it using the number of generations .
192
178
*/
193
- else
194
- keep_next_backup = false ;
179
+ if ( backup -> backup_mode == BACKUP_MODE_FULL )
180
+ backup_num ++ ;
195
181
196
- continue ;
182
+ /* Evaluateretention_redundancy if this backup is eligible for removal */
183
+ if (keep_next_backup ||
184
+ retention_redundancy >= backup_num_evaluate + 1 ||
185
+ (retention_window > 0 && backup -> recovery_time >= days_threshold ))
186
+ {
187
+ /* Save LSN and Timeline to remove unnecessary WAL segments */
188
+ oldest_lsn = backup -> start_lsn ;
189
+ oldest_tli = backup -> tli ;
190
+
191
+ /* Save parent backup of this incremental backup */
192
+ if (backup -> backup_mode != BACKUP_MODE_FULL )
193
+ keep_next_backup = true;
194
+ /*
195
+ * Previous incremental backup was kept or this is first backup
196
+ * so do not delete this backup.
197
+ */
198
+ else
199
+ keep_next_backup = false;
200
+
201
+ continue ;
202
+ }
203
+ /* Delete backup and update status to DELETED */
204
+ pgBackupDeleteFiles (backup );
205
+ backup_deleted = true;
197
206
}
207
+ }
198
208
199
- /* Delete backup and update status to DELETED */
200
- pgBackupDeleteFiles (backup );
201
- backup_deleted = true;
209
+ /*
210
+ * If oldest_lsn wasn`t set in prevous step
211
+ * get oldest backup LSN and TLI for WAL purging
212
+ */
213
+ if (delete_wal && (XLogRecPtrIsInvalid (oldest_lsn )))
214
+ {
215
+ pgBackup * backup = (pgBackup * ) parray_get (backup_list , parray_num (backup_list ) - 1 );
216
+ oldest_lsn = backup -> start_lsn ;
217
+ oldest_tli = backup -> tli ;
202
218
}
203
219
204
220
/* Purge WAL files */
205
- delete_walfiles (oldest_lsn , oldest_tli );
221
+ if (delete_wal )
222
+ {
223
+ delete_walfiles (oldest_lsn , oldest_tli );
224
+ }
206
225
207
226
/* Cleanup */
208
227
parray_walk (backup_list , pgBackupFree );
@@ -257,7 +276,7 @@ pgBackupDeleteFiles(pgBackup *backup)
257
276
pgFile * file = (pgFile * ) parray_get (files , i );
258
277
259
278
/* print progress */
260
- elog (LOG , "delete file(%zd/%lu) \"%s\"" , i + 1 ,
279
+ elog (VERBOSE , "delete file(%zd/%lu) \"%s\"" , i + 1 ,
261
280
(unsigned long ) parray_num (files ), file -> path );
262
281
263
282
if (remove (file -> path ))
0 commit comments