From: Andres Freund <andres@2ndquadrant.com>
To: Pavan Deolasee <pavan.deolasee@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: maxim.boguk <maxim.boguk@gmail.com>
Cc: Pg Bugs <pgsql-bugs@postgresql.org>
Subject: Re: BUG #10675: alter database set tablespace and unlogged table
Date: Thu, 9 Oct 2014 15:52:47 +0200
Message-ID: <20141009135247.GB29124@awork2.int> (raw)
In-Reply-To: <CABOikdN=3B7zXfOnPTjDzRu6SsuDJsYGE14cu7t2NRv3CffdVw@mail.gmail.com>
References: <20140617032438.2585.42918@wrigleys.postgresql.org>
<CABOikdMxX0VdJEKSBd62sX_XAwa_=MAFqdAXXbU5V+BHZOxrng@mail.gmail.com>
<CABOikdPiDnBsAvdxo-wE6hY=UPUQPKQTRK5Cg=h0caMDgdxM+A@mail.gmail.com>
<20140618101045.GG3115@awork2.anarazel.de>
<CABOikdPY9AKdrzsOhfeoOv1xSWNsrZxKWGRthYaFdsxHXjiKfQ@mail.gmail.com>
<20140618110240.GJ3115@awork2.anarazel.de>
<11621.1403099146@sss.pgh.pa.us>
<20140618141949.GC3968@awork2.anarazel.de>
<CABOikdNMWyfvcm+1Ks-VVRzRDyKsEZj5Ehb3C2+-MYBYftcs+w@mail.gmail.com>
<CABOikdN=3B7zXfOnPTjDzRu6SsuDJsYGE14cu7t2NRv3CffdVw@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
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%40...
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
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugsAttachments:0001-Flush-unlogged-table-s-buffers-when-copying-or-movin.patch (text/x-patch, 6.8K) download
From 48b62f4f57493051e31f06719adce46b83599a5f Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
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.cindex 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.cindex 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.cindex 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.hindex 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: andres@2ndquadrant.com, pavan.deolasee@gmail.com, tgl@sss.pgh.pa.us, maxim.boguk@gmail.com
Subject: Re: BUG #10675: alter database set tablespace and unlogged table
In-Reply-To: <20141009135247.GB29124@awork2.int>
References: <20140617032438.2585.42918@wrigleys.postgresql.org> <CABOikdMxX0VdJEKSBd62sX_XAwa_=MAFqdAXXbU5V+BHZOxrng@mail.gmail.com> <CABOikdPiDnBsAvdxo-wE6hY=UPUQPKQTRK5Cg=h0caMDgdxM+A@mail.gmail.com> <20140618101045.GG3115@awork2.anarazel.de> <CABOikdPY9AKdrzsOhfeoOv1xSWNsrZxKWGRthYaFdsxHXjiKfQ@mail.gmail.com> <20140618110240.GJ3115@awork2.anarazel.de> <11621.1403099146@sss.pgh.pa.us> <20140618141949.GC3968@awork2.anarazel.de> <CABOikdNMWyfvcm+1Ks-VVRzRDyKsEZj5Ehb3C2+-MYBYftcs+w@mail.gmail.com> <CABOikdN=3B7zXfOnPTjDzRu6SsuDJsYGE14cu7t2NRv3CffdVw@mail.gmail.com> <20141009135247.GB29124@awork2.int>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox