Skip to content

Commit 6b8ab83

Browse files
committed
cleanup in delete.c
1 parent 02840b8 commit 6b8ab83

File tree

1 file changed

+4
-118
lines changed

1 file changed

+4
-118
lines changed

src/delete.c

Lines changed: 4 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
#include <time.h>
1515
#include <unistd.h>
1616

17-
static void delete_walfiles(XLogRecPtr oldest_lsn, TimeLineID oldest_tli,
18-
uint32 xlog_seg_size);
19-
static void delete_walfiles_internal(XLogRecPtr keep_lsn, timelineInfo *tli,
17+
static void delete_walfiles(XLogRecPtr keep_lsn, timelineInfo *tli,
2018
uint32 xlog_seg_size, bool dry_run);
2119
static void do_retention_internal(parray *backup_list, parray *to_keep_list,
2220
parray *to_purge_list);
@@ -641,10 +639,10 @@ do_retention_wal(bool dry_run)
641639
* can be safely purged.
642640
*/
643641
if (tlinfo->oldest_backup)
644-
delete_walfiles_internal(tlinfo->oldest_backup->start_lsn,
642+
delete_walfiles(tlinfo->oldest_backup->start_lsn,
645643
tlinfo, instance_config.xlog_seg_size, dry_run);
646644
else
647-
delete_walfiles_internal(InvalidXLogRecPtr,
645+
delete_walfiles(InvalidXLogRecPtr,
648646
tlinfo, instance_config.xlog_seg_size, dry_run);
649647
}
650648
}
@@ -732,7 +730,7 @@ delete_backup_files(pgBackup *backup)
732730
* Q: Maybe we should stop treating partial WAL segments as second-class citizens?
733731
*/
734732
static void
735-
delete_walfiles_internal(XLogRecPtr keep_lsn, timelineInfo *tlinfo,
733+
delete_walfiles(XLogRecPtr keep_lsn, timelineInfo *tlinfo,
736734
uint32 xlog_seg_size, bool dry_run)
737735
{
738736
XLogSegNo StartSegNo; /* First segment to delete */
@@ -866,118 +864,6 @@ delete_walfiles_internal(XLogRecPtr keep_lsn, timelineInfo *tlinfo,
866864
}
867865
}
868866

869-
/*
870-
* Deletes WAL segments up to oldest_lsn or all WAL segments (if all backups
871-
* was deleted and so oldest_lsn is invalid).
872-
*
873-
* oldest_lsn - if valid, function deletes WAL segments, which contain lsn
874-
* older than oldest_lsn. If it is invalid function deletes all WAL segments.
875-
* oldest_tli - is used to construct oldest WAL segment in addition to
876-
* oldest_lsn.
877-
*/
878-
static void
879-
delete_walfiles(XLogRecPtr oldest_lsn, TimeLineID oldest_tli,
880-
uint32 xlog_seg_size)
881-
{
882-
XLogSegNo targetSegNo;
883-
char oldestSegmentNeeded[MAXFNAMELEN];
884-
DIR *arcdir;
885-
struct dirent *arcde;
886-
char wal_file[MAXPGPATH];
887-
char max_wal_file[MAXPGPATH];
888-
char min_wal_file[MAXPGPATH];
889-
int rc;
890-
891-
max_wal_file[0] = '\0';
892-
min_wal_file[0] = '\0';
893-
894-
if (!XLogRecPtrIsInvalid(oldest_lsn))
895-
{
896-
GetXLogSegNo(oldest_lsn, targetSegNo, xlog_seg_size);
897-
GetXLogFileName(oldestSegmentNeeded, oldest_tli, targetSegNo,
898-
xlog_seg_size);
899-
900-
elog(LOG, "removing WAL segments older than %s", oldestSegmentNeeded);
901-
}
902-
else
903-
elog(LOG, "removing all WAL segments");
904-
905-
/*
906-
* Now it is time to do the actual work and to remove all the segments
907-
* not needed anymore.
908-
*/
909-
if ((arcdir = opendir(arclog_path)) != NULL)
910-
{
911-
while (errno = 0, (arcde = readdir(arcdir)) != NULL)
912-
{
913-
/*
914-
* We ignore the timeline part of the WAL segment identifiers in
915-
* deciding whether a segment is still needed. This ensures that
916-
* we won't prematurely remove a segment from a parent timeline.
917-
* We could probably be a little more proactive about removing
918-
* segments of non-parent timelines, but that would be a whole lot
919-
* more complicated.
920-
*
921-
* We use the alphanumeric sorting property of the filenames to
922-
* decide which ones are earlier than the exclusiveCleanupFileName
923-
* file. Note that this means files are not removed in the order
924-
* they were originally written, in case this worries you.
925-
*
926-
* We also should not forget that WAL segment can be compressed.
927-
*/
928-
if (IsXLogFileName(arcde->d_name) ||
929-
IsPartialXLogFileName(arcde->d_name) ||
930-
IsBackupHistoryFileName(arcde->d_name) ||
931-
IsCompressedXLogFileName(arcde->d_name))
932-
{
933-
if (XLogRecPtrIsInvalid(oldest_lsn) ||
934-
strncmp(arcde->d_name + 8, oldestSegmentNeeded + 8, 16) < 0)
935-
{
936-
/*
937-
* Use the original file name again now, including any
938-
* extension that might have been chopped off before testing
939-
* the sequence.
940-
*/
941-
snprintf(wal_file, MAXPGPATH, "%s/%s",
942-
arclog_path, arcde->d_name);
943-
944-
rc = unlink(wal_file);
945-
if (rc != 0)
946-
{
947-
elog(WARNING, "could not remove file \"%s\": %s",
948-
wal_file, strerror(errno));
949-
break;
950-
}
951-
elog(LOG, "removed WAL segment \"%s\"", wal_file);
952-
953-
if (max_wal_file[0] == '\0' ||
954-
strcmp(max_wal_file + 8, arcde->d_name + 8) < 0)
955-
strcpy(max_wal_file, arcde->d_name);
956-
957-
if (min_wal_file[0] == '\0' ||
958-
strcmp(min_wal_file + 8, arcde->d_name + 8) > 0)
959-
strcpy(min_wal_file, arcde->d_name);
960-
}
961-
}
962-
}
963-
964-
if (min_wal_file[0] != '\0')
965-
elog(INFO, "removed min WAL segment \"%s\"", min_wal_file);
966-
if (max_wal_file[0] != '\0')
967-
elog(INFO, "removed max WAL segment \"%s\"", max_wal_file);
968-
969-
if (errno)
970-
elog(WARNING, "could not read archive location \"%s\": %s",
971-
arclog_path, strerror(errno));
972-
if (closedir(arcdir))
973-
elog(WARNING, "could not close archive location \"%s\": %s",
974-
arclog_path, strerror(errno));
975-
}
976-
else
977-
elog(WARNING, "could not open archive location \"%s\": %s",
978-
arclog_path, strerror(errno));
979-
}
980-
981867

982868
/* Delete all backup files and wal files of given instance. */
983869
int

0 commit comments

Comments
 (0)