Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XcE9L-000232-Gl for pgsql-bugs@arkaria.postgresql.org; Thu, 09 Oct 2014 13:52:59 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XcE9K-0000KG-Cp for pgsql-bugs@arkaria.postgresql.org; Thu, 09 Oct 2014 13:52:58 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XcE9J-0000K7-FT for pgsql-bugs@postgresql.org; Thu, 09 Oct 2014 13:52:57 +0000 Received: from mout.kundenserver.de ([212.227.17.10]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XcE9F-0005yp-78 for pgsql-bugs@postgresql.org; Thu, 09 Oct 2014 13:52:55 +0000 Received: from anarazel.de (p54AF4407.dip0.t-ipconnect.de [84.175.68.7]) by mrelayeu.kundenserver.de (node=mreue104) with ESMTP (Nemesis) id 0Lx7w9-1YIFwH3fA1-016jx4; Thu, 09 Oct 2014 15:52:49 +0200 Received: by anarazel.de (Postfix, from userid 1000) id 0552F101CBB; Thu, 9 Oct 2014 15:52:47 +0200 (CEST) Date: Thu, 9 Oct 2014 15:52:47 +0200 From: Andres Freund To: Pavan Deolasee Cc: Tom Lane , "maxim.boguk" , Pg Bugs Subject: Re: BUG #10675: alter database set tablespace and unlogged table Message-ID: <20141009135247.GB29124@awork2.int> References: <20140617032438.2585.42918@wrigleys.postgresql.org> <20140618101045.GG3115@awork2.anarazel.de> <20140618110240.GJ3115@awork2.anarazel.de> <11621.1403099146@sss.pgh.pa.us> <20140618141949.GC3968@awork2.anarazel.de> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="fUYQa+Pmc3FrFX/N" Content-Disposition: inline In-Reply-To: X-Provags-ID: V02:K0:OANGxbq7MmPEiOO9BuUegK+43Q+GPq09VKcQCQTn9b3 rHPdmOthuci4eaz6RiBeWIdmt5UiYrefMgh16j6TGEGjw38rUO yMWQ3qrJ9KCXMvT0d4Q1n0QbwrLSNKzU/NLJoHMdj+RKonXEQU Jv0RkAUE2K01gBl2F7u0wPcHo9AThTR5I6ovoX3HYxuMcEyQZF JM5tl6qpcOx9S+KWMOZHI049xG93Ts93tHAh710coYpW+v2kfv xbotslp+EbLjhvjIgj5uRSHT7gVicFR8WJ7uuwc5xxaWXwSRBz JChohBMo6p4avSyW5HzX1Zm7+J53ku7MvG6Yd3rYOp+JscJ5hz db21n5vUy2USgAodJ8fs= X-UI-Out-Filterresults: notjunk:1; X-Pg-Spam-Score: -1.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 --fUYQa+Pmc3FrFX/N Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, On 2014-07-02 17:28:25 +0530, Pavan Deolasee wrote: > flag to tell checkpointer to flush all pages to the disk. Tom (and even I) > have reservations about the approach, but I would nevertheless leave it to > the committer to decide. IMV we must fix this bug one way or the other. > Otherwise users face risk of failing to do clean shutdown. I'm looking into this again now. We haven't really come to a agreement about which approach to take. So unless there's some progress on that front I'll push a variant of this patch. That seems better than leaving the issue open for another round of releases. Issues I've found with the patch so far: * The RequestCheckpoint() in createdb() also needs CHECKPOINT_FLUSH_ALL. * I don't think it's wise to renumber the CHECKPOINT_* flags in a commit that's supposed to be backpatched. That'll cause interesting issues if there's a extension out there containing a RequestCheckpoint() call. * I've also done some minor code and comment changes. Attached is my new version. I've confirmed that I could reproduce various broken behaviour before (wrong query results, ERRORs in further queries trying to write out buffers, shutdown failures), but not after. I'll note that I think there's arguably another bug that participated in the shutdown/checkpoint error you reported in http://archives.postgresql.org/message-id/CABOikdMxX0VdJEKSBd62sX_XAwa_%3DMAFqdAXXbU5V%2BBHZOxrng%40mail.gmail.com When moving a database into a different tablespace we never invalidate it's buffers even though they don't correspond to any existing files. The only reason that currently works is because they're not dirty and thus never written back or anything. I think movedb() should also grow a DropDatabaseBuffers() call to fix that. Greetings, Andres Freund -- Andres Freund http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --fUYQa+Pmc3FrFX/N Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="0001-Flush-unlogged-table-s-buffers-when-copying-or-movin.patch" From 48b62f4f57493051e31f06719adce46b83599a5f Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Thu, 9 Oct 2014 14:58:42 +0200 Subject: [PATCH] Flush unlogged table's buffers when copying or moving databases. CREATE DATABASE and ALTER DATABASE .. SET TABLESPACE copy the source database directory on the filesystem level. To ensure the on disk state is consistent they block out users of the affected database and force a checkpoint to flush out all data to disk. Unfortunately, up to now, that checkpoint didn't flush out dirty buffers from unlogged relations. That bug means there could be leftover dirty buffers in either the template database, or the database in its old location. Leading to problems when accessing relations in an inconsistent state; and to possible problems during shutdown in the SET TABLESPACE case because buffers belonging files that don't exist anymore are flushed. This was reported in bug #10675 by Maxim Boguk. Fix by Pavan Deolasee, modified somewhat by me. Reviewed by MauMau. Backpatch to 9.1 where unlogged tables were added. --- src/backend/access/transam/xlog.c | 3 ++- src/backend/commands/dbcommands.c | 26 +++++++++++++++----------- src/backend/storage/buffer/bufmgr.c | 16 +++++++++------- src/include/access/xlog.h | 2 ++ 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 5a4dbb9..5153d62 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -7782,7 +7782,8 @@ LogCheckpointStart(int flags, bool restartpoint) (flags & CHECKPOINT_FORCE) ? " force" : "", (flags & CHECKPOINT_WAIT) ? " wait" : "", (flags & CHECKPOINT_CAUSE_XLOG) ? " xlog" : "", - (flags & CHECKPOINT_CAUSE_TIME) ? " time" : ""); + (flags & CHECKPOINT_CAUSE_TIME) ? " time" : "", + (flags & CHECKPOINT_FLUSH_ALL) ? " flush-all" :""); } /* diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index 7831e90..343eb3a 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -549,15 +549,17 @@ createdb(const CreatedbStmt *stmt) InvokeObjectPostCreateHook(DatabaseRelationId, dboid, 0); /* - * Force a checkpoint before starting the copy. This will force dirty - * buffers out to disk, to ensure source database is up-to-date on disk - * for the copy. FlushDatabaseBuffers() would suffice for that, but we - * also want to process any pending unlink requests. Otherwise, if a - * checkpoint happened while we're copying files, a file might be deleted - * just when we're about to copy it, causing the lstat() call in copydir() - * to fail with ENOENT. + * Force a checkpoint before starting the copy. This will force all dirty + * buffers, including those of unlogged tables, out to disk, to ensure + * source database is up-to-date on disk for the copy. + * FlushDatabaseBuffers() would suffice for that, but we also want + * to process any pending unlink requests. Otherwise, if a checkpoint + * happened while we're copying files, a file might be deleted just when + * we're about to copy it, causing the lstat() call in copydir() to fail + * with ENOENT. */ - RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT); + RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT + | CHECKPOINT_FLUSH_ALL); /* * Once we start copying subdirectories, we need to be able to clean 'em @@ -1138,8 +1140,9 @@ movedb(const char *dbname, const char *tblspcname) dst_dbpath = GetDatabasePath(db_id, dst_tblspcoid); /* - * Force a checkpoint before proceeding. This will force dirty buffers out - * to disk, to ensure source database is up-to-date on disk for the copy. + * Force a checkpoint before proceeding. This will force all dirty + * buffers, including those of unlogged tables, out to disk, to ensure + * source database is up-to-date on disk for the copy. * FlushDatabaseBuffers() would suffice for that, but we also want to * process any pending unlink requests. Otherwise, the check for existing * files in the target directory might fail unnecessarily, not to mention @@ -1147,7 +1150,8 @@ movedb(const char *dbname, const char *tblspcname) * On Windows, this also ensures that background procs don't hold any open * files, which would cause rmdir() to fail. */ - RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT); + RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT + | CHECKPOINT_FLUSH_ALL); /* * Check for existence of files in the target directory, i.e., objects of diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 45d1d61..57067ef 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1493,9 +1493,10 @@ UnpinBuffer(volatile BufferDesc *buf, bool fixOwner) * * This is called at checkpoint time to write out all dirty shared buffers. * The checkpoint request flags should be passed in. If CHECKPOINT_IMMEDIATE - * is set, we disable delays between writes; if CHECKPOINT_IS_SHUTDOWN is - * set, we write even unlogged buffers, which are otherwise skipped. The - * remaining flags currently have no effect here. + * is set, we disable delays between writes; if CHECKPOINT_IS_SHUTDOWN, + * CHECKPOINT_END_OF_RECOVERY or CHECKPOINT_FLUSH_ALL is set, we write even + * unlogged buffers, which are otherwise skipped. The remaining flags + * currently have no effect here. */ static void BufferSync(int flags) @@ -1510,11 +1511,12 @@ BufferSync(int flags) ResourceOwnerEnlargeBuffers(CurrentResourceOwner); /* - * Unless this is a shutdown checkpoint, we write only permanent, dirty - * buffers. But at shutdown or end of recovery, we write all dirty - * buffers. + * Unless this is a shutdown checkpoint or we have been explicitly told, + * we write only permanent, dirty buffers. But at shutdown or end of + * recovery, we write all dirty buffers. */ - if (!((flags & CHECKPOINT_IS_SHUTDOWN) || (flags & CHECKPOINT_END_OF_RECOVERY))) + if (!((flags & (CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_END_OF_RECOVERY | + CHECKPOINT_FLUSH_ALL)))) mask |= BM_PERMANENT; /* diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h index 0f068d9..ca35f3e 100644 --- a/src/include/access/xlog.h +++ b/src/include/access/xlog.h @@ -253,6 +253,8 @@ extern bool XLOG_DEBUG; /* These indicate the cause of a checkpoint request */ #define CHECKPOINT_CAUSE_XLOG 0x0020 /* XLOG consumption */ #define CHECKPOINT_CAUSE_TIME 0x0040 /* Elapsed time */ +#define CHECKPOINT_FLUSH_ALL 0x0080 /* Flush all pages, including those + * belonging to unlogged tables */ /* Checkpoint statistics */ typedef struct CheckpointStatsData -- 2.0.0.rc2.4.g1dc51c6.dirty --fUYQa+Pmc3FrFX/N 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 --fUYQa+Pmc3FrFX/N--