Index: src/port/open.c =================================================================== RCS file: /projects/cvsroot/pgsql/src/port/open.c,v retrieving revision 1.22 diff -c -r1.22 open.c *** src/port/open.c 30 Nov 2007 11:16:43 -0000 1.22 --- src/port/open.c 19 Dec 2007 15:50:06 -0000 *************** *** 13,19 **** #ifdef WIN32 ! #include "c.h" #include #include --- 13,23 ---- #ifdef WIN32 ! #ifndef FRONTEND ! #include "postgres.h" ! #else ! #include "postgres_fe.h" ! #endif #include #include *************** *** 58,65 **** pgwin32_open(const char *fileName, int fileFlags,...) { int fd; ! HANDLE h; SECURITY_ATTRIBUTES sa; /* Check that we can handle the request */ assert((fileFlags & ((O_RDONLY | O_WRONLY | O_RDWR) | O_APPEND | --- 62,70 ---- pgwin32_open(const char *fileName, int fileFlags,...) { int fd; ! HANDLE h = INVALID_HANDLE_VALUE; SECURITY_ATTRIBUTES sa; + int loops = 0; /* Check that we can handle the request */ assert((fileFlags & ((O_RDONLY | O_WRONLY | O_RDWR) | O_APPEND | *************** *** 71,77 **** sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = NULL; ! if ((h = CreateFile(fileName, /* cannot use O_RDONLY, as it == 0 */ (fileFlags & O_RDWR) ? (GENERIC_WRITE | GENERIC_READ) : ((fileFlags & O_WRONLY) ? GENERIC_WRITE : GENERIC_READ), --- 76,82 ---- sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = NULL; ! while ((h = CreateFile(fileName, /* cannot use O_RDONLY, as it == 0 */ (fileFlags & O_RDWR) ? (GENERIC_WRITE | GENERIC_READ) : ((fileFlags & O_WRONLY) ? GENERIC_WRITE : GENERIC_READ), *************** *** 88,93 **** --- 93,121 ---- ((fileFlags & O_DSYNC) ? FILE_FLAG_WRITE_THROUGH : 0), NULL)) == INVALID_HANDLE_VALUE) { + /* + * Sharing violation or locking error can indicate antivirus, backup + * or similar software that's locking the file. Try again for 30 seconds + * before giving up. + */ + if (GetLastError() == ERROR_SHARING_VIOLATION || + GetLastError() == ERROR_LOCK_VIOLATION) + { + pg_usleep(100000); + loops++; + + #ifndef FRONTEND + if (loops == 50) + ereport(WARNING, + (errmsg("could not open file \"%s\" due to sharing violation. Will retry for 30 seconds.", + fileName, + errhint("You may have antivirus, backup or similar software interfering with the database.")))); + #endif + + if (loops < 300) + continue; + } + _dosmaperr(GetLastError()); return -1; }