|
7 | 7 | * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
|
8 | 8 | * Portions Copyright (c) 1994, Regents of the University of California
|
9 | 9 | *
|
10 |
| - * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.351 2009/09/01 02:54:51 alvherre Exp $ |
| 10 | + * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.352 2009/09/10 09:42:10 heikki Exp $ |
11 | 11 | *
|
12 | 12 | *-------------------------------------------------------------------------
|
13 | 13 | */
|
@@ -3040,6 +3040,9 @@ RemoveOldXlogFiles(uint32 log, uint32 seg, XLogRecPtr endptr)
|
3040 | 3040 | struct dirent *xlde;
|
3041 | 3041 | char lastoff[MAXFNAMELEN];
|
3042 | 3042 | char path[MAXPGPATH];
|
| 3043 | +#ifdef WIN32 |
| 3044 | + char newpath[MAXPGPATH]; |
| 3045 | +#endif |
3043 | 3046 | struct stat statbuf;
|
3044 | 3047 |
|
3045 | 3048 | /*
|
@@ -3103,10 +3106,41 @@ RemoveOldXlogFiles(uint32 log, uint32 seg, XLogRecPtr endptr)
|
3103 | 3106 | else
|
3104 | 3107 | {
|
3105 | 3108 | /* No need for any more future segments... */
|
| 3109 | + int rc; |
| 3110 | + |
3106 | 3111 | ereport(DEBUG2,
|
3107 | 3112 | (errmsg("removing transaction log file \"%s\"",
|
3108 | 3113 | xlde->d_name)));
|
3109 |
| - unlink(path); |
| 3114 | + |
| 3115 | +#ifdef WIN32 |
| 3116 | + /* |
| 3117 | + * On Windows, if another process (e.g another backend) |
| 3118 | + * holds the file open in FILE_SHARE_DELETE mode, unlink |
| 3119 | + * will succeed, but the file will still show up in |
| 3120 | + * directory listing until the last handle is closed. |
| 3121 | + * To avoid confusing the lingering deleted file for a |
| 3122 | + * live WAL file that needs to be archived, rename it |
| 3123 | + * before deleting it. |
| 3124 | + * |
| 3125 | + * If another process holds the file open without |
| 3126 | + * FILE_SHARE_DELETE flag, rename will fail. We'll try |
| 3127 | + * again at the next checkpoint. |
| 3128 | + */ |
| 3129 | + snprintf(newpath, MAXPGPATH, "%s.deleted", path); |
| 3130 | + if (rename(path, newpath) != 0) |
| 3131 | + ereport(ERROR, |
| 3132 | + (errcode_for_file_access(), |
| 3133 | + errmsg("could not rename old transaction log file \"%s\"", |
| 3134 | + path))); |
| 3135 | + rc = unlink(newpath); |
| 3136 | +#else |
| 3137 | + rc = unlink(path); |
| 3138 | +#endif |
| 3139 | + if (rc != 0) |
| 3140 | + ereport(ERROR, |
| 3141 | + (errcode_for_file_access(), |
| 3142 | + errmsg("could not remove old transaction log file \"%s\": %m", |
| 3143 | + path))); |
3110 | 3144 | CheckpointStats.ckpt_segs_removed++;
|
3111 | 3145 | }
|
3112 | 3146 |
|
|
0 commit comments