Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Tm2e2-0003IL-Bm for pgsql-bugs@arkaria.postgresql.org; Fri, 21 Dec 2012 13:28:10 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1Tm2e1-00010p-6A for pgsql-bugs@arkaria.postgresql.org; Fri, 21 Dec 2012 13:28:09 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Tm2dz-00010j-W7 for pgsql-bugs@postgresql.org; Fri, 21 Dec 2012 13:28:08 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Tm2dx-0007N9-Ar for pgsql-bugs@postgresql.org; Fri, 21 Dec 2012 13:28:06 +0000 Received: from sc9-mailhost2.vmware.com (sc9-mailhost2.vmware.com [10.113.161.72]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id E69AE2841E; Fri, 21 Dec 2012 05:28:03 -0800 (PST) Received: from zcs-prod-ext-mta-2.vmware.com (zcs-prod-ext-mta-2.vmware.com [10.113.62.224]) by sc9-mailhost2.vmware.com (Postfix) with ESMTP id E152FB0142; Fri, 21 Dec 2012 05:28:03 -0800 (PST) Received: from localhost (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTP id 83FDDC1531; Fri, 21 Dec 2012 05:28:03 -0800 (PST) X-Virus-Scanned: amavisd-new at zcs-prod-ext-mta-2.vmware.com Received: from zcs-prod-ext-mta-2.vmware.com ([127.0.0.1]) by localhost (zcs-prod-ext-mta-2.vmware.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ECc7ZXul3xzY; Fri, 21 Dec 2012 05:28:03 -0800 (PST) Received: from localhost (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTP id 596FCC1534; Fri, 21 Dec 2012 05:28:03 -0800 (PST) X-Virus-Scanned: amavisd-new at zcs-prod-ext-mta-2.vmware.com Received: from zcs-prod-ext-mta-2.vmware.com ([127.0.0.1]) by localhost (zcs-prod-ext-mta-2.vmware.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id FvgWvuDY7pxz; Fri, 21 Dec 2012 05:28:03 -0800 (PST) Received: from [192.168.1.90] (unknown [10.113.62.14]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTPSA id 8137AC1531; Fri, 21 Dec 2012 05:28:02 -0800 (PST) Message-ID: <50D463E8.9000000@vmware.com> Date: Fri, 21 Dec 2012 15:28:08 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.10) Gecko/20121027 Icedove/10.0.10 MIME-Version: 1.0 To: Pg Bugs CC: Magnus Hagander Subject: pg_basebackup fails if a data file is removed Content-Type: multipart/mixed; boundary="------------020806010201030005000209" X-Pg-Spam-Score: -6.9 (------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is a multi-part message in MIME format. --------------020806010201030005000209 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit When pg_basebackup copies data files, it does basically this: > if (lstat(pathbuf, &statbuf) != 0) > { > if (errno != ENOENT) > ereport(ERROR, > (errcode_for_file_access(), > errmsg("could not stat file or directory \"%s\": %m", > pathbuf))); > > /* If the file went away while scanning, it's no error. */ > continue; > } > ... > sendFile(pathbuf, pathbuf + basepathlen + 1, &statbuf); There's a race condition there. If the file is removed after the lstat call, and before sendFile opens the file, the backup fails with an error. It's a fairly tight window, so it's difficult to run into by accident, but by putting a breakpoint with a debugger there it's quite easy to reproduce, by e.g doing a VACUUM FULL on the table about to be copied. A straightforward fix is to allow sendFile() to ignore ENOENT. Patch attached. - Heikki --------------020806010201030005000209 Content-Type: text/x-diff; name="fix-enoent-in-basebackup.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="fix-enoent-in-basebackup.patch" diff --git a/src/backend/replication/basebackup.c b/src/backend/replication/basebackup.c index 1b234c6..bc95215 100644 --- a/src/backend/replication/basebackup.c +++ b/src/backend/replication/basebackup.c @@ -44,8 +44,8 @@ typedef struct static int64 sendDir(char *path, int basepathlen, bool sizeonly); -static void sendFile(char *readfilename, char *tarfilename, - struct stat * statbuf); +static bool sendFile(char *readfilename, char *tarfilename, + struct stat * statbuf, bool missing_ok); static void sendFileWithContent(const char *filename, const char *content); static void _tarWriteHeader(const char *filename, const char *linktarget, struct stat * statbuf); @@ -194,7 +194,7 @@ perform_base_backup(basebackup_options *opt, DIR *tblspcdir) XLOG_CONTROL_FILE))); } - sendFile(XLOG_CONTROL_FILE, XLOG_CONTROL_FILE, &statbuf); + sendFile(XLOG_CONTROL_FILE, XLOG_CONTROL_FILE, &statbuf, false); } /* @@ -715,11 +715,18 @@ sendDir(char *path, int basepathlen, bool sizeonly) } else if (S_ISREG(statbuf.st_mode)) { - /* Add size, rounded up to 512byte block */ - size += ((statbuf.st_size + 511) & ~511); + bool sent = false; + if (!sizeonly) - sendFile(pathbuf, pathbuf + basepathlen + 1, &statbuf); - size += 512; /* Size of the header of the file */ + sent = sendFile(pathbuf, pathbuf + basepathlen + 1, &statbuf, + true); + + if (sent || sizeonly) + { + /* Add size, rounded up to 512byte block */ + size += ((statbuf.st_size + 511) & ~511); + size += 512; /* Size of the header of the file */ + } } else ereport(WARNING, @@ -779,9 +786,17 @@ _tarChecksum(char *header) return sum; } -/* Given the member, write the TAR header & send the file */ -static void -sendFile(char *readfilename, char *tarfilename, struct stat * statbuf) +/* + * Given the member, write the TAR header & send the file. + * + * If 'missing_ok' is true, will not throw an error if the file is not found. + * + * Returns true if the file was successfully sent, false if 'missing_ok', + * and the file did not exist. + */ +static bool +sendFile(char *readfilename, char *tarfilename, struct stat *statbuf, + bool missing_ok) { FILE *fp; char buf[TAR_SEND_SIZE]; @@ -791,9 +806,13 @@ sendFile(char *readfilename, char *tarfilename, struct stat * statbuf) fp = AllocateFile(readfilename, "rb"); if (fp == NULL) + { + if (errno == ENOENT && missing_ok) + return false; ereport(ERROR, (errcode_for_file_access(), errmsg("could not open file \"%s\": %m", readfilename))); + } /* * Some compilers will throw a warning knowing this test can never be true @@ -847,6 +866,8 @@ sendFile(char *readfilename, char *tarfilename, struct stat * statbuf) } FreeFile(fp); + + return true; } --------------020806010201030005000209 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------020806010201030005000209--