###

Berkeley DB Reference Guide:
  Transaction Protected Applications

# Log file removal

The fourth component of the infrastructure, log file removal, concerns the ongoing disk consumption of the database log files. Depending on the rate at which the application writes to the databases and the available disk space, the number of log files may increase quickly enough that disk space will be a resource problem. For this reason, you will periodically want to remove log files in order to conserve disk space. This procedure is distinct from database and log file archival for catastrophic recovery, and you cannot remove the current log files simply because you have created a database snapshot or copied log files to archival media.

Log files may be removed at any time, as long as:

- the log file is not involved in an active transaction
- at least two checkpoints have been written subsequent to the log file's creation, and
- the log file is not the only log file in the environment.

Obviously, if you are preparing for catastrophic failure, you will want to copy the log files to archival media before you remove them.

To remove log files, take the following steps:

1. If you are concerned with catastrophic failure, first copy the log files to backup media as described in Archival for catastrophic recovery.

2. Run db_archive without options to identify all of the log files that are no longer in use (e.g., no longer involved in an active transaction).

3. Remove those log files from the system.

The functionality provided by the db_archive utility is also available directly from the Berkeley DB library. The following code fragment removes log files that are no longer needed by the database environment.

>     int
>     main(int argc, char *argv)
>     {
>     	...

>     	/* Start a logfile removal thread. */
>     	if ((errno = pthread_create(
>     	    &ptid, NULL, logfile_thread, (void *)dbenv)) != 0) {
>     		fprintf(stderr,
>     		    "txnapp: failed spawning log file removal thread: %s\n",
>     		    strerror(errno));
>     		exit (1);
>     	}

>     	...
>     }

>     void *
>     logfile_thread(void *arg)
>     {
>     	DB_ENV *dbenv;
>     	int ret;
>     	char **begin, **list;

>     	dbenv = arg;
>     	dbenv->errx(dbenv,
>     	    "Log file removal thread: %lu", (u_long)pthread_self());

>     	/* Check once every 5 minutes. */
>     	for (;; sleep(300)) {
>     		/* Get the list of log files. */
>     		if ((ret = log_archive(dbenv, &list, DB_ARCH_ABS, NULL)) != 0) {
>     			dbenv->err(dbenv, ret, "log_archive");
>     			exit (1);
>     		}

>     		/* Remove the log files. */
>     		if (list != NULL) {
>     			for (begin = list; *list != NULL; ++list)
>     				if ((ret = remove(*list)) != 0) {
>     					dbenv->err(dbenv,
>     					    ret, "remove %s", *list);
>     					exit (1);
>     				}
>     			free (begin);
>     		}
>     	}
>     	/* NOTREACHED */
>     }

Copyright Sleepycat Software