Skip to content

Commit d7624e5

Browse files
committed
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 and Fujii Masao. Backpatch to 9.1 where unlogged tables were introduced.
1 parent 4e54685 commit d7624e5

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

src/backend/access/transam/xlog.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -6686,9 +6686,9 @@ LogCheckpointStart(int flags, bool restartpoint)
66866686
* the main message, but what about all the flags?
66876687
*/
66886688
if (restartpoint)
6689-
msg = "restartpoint starting:%s%s%s%s%s%s%s";
6689+
msg = "restartpoint starting:%s%s%s%s%s%s%s%s";
66906690
else
6691-
msg = "checkpoint starting:%s%s%s%s%s%s%s";
6691+
msg = "checkpoint starting:%s%s%s%s%s%s%s%s";
66926692

66936693
elog(LOG, msg,
66946694
(flags & CHECKPOINT_IS_SHUTDOWN) ? " shutdown" : "",
@@ -6697,7 +6697,8 @@ LogCheckpointStart(int flags, bool restartpoint)
66976697
(flags & CHECKPOINT_FORCE) ? " force" : "",
66986698
(flags & CHECKPOINT_WAIT) ? " wait" : "",
66996699
(flags & CHECKPOINT_CAUSE_XLOG) ? " xlog" : "",
6700-
(flags & CHECKPOINT_CAUSE_TIME) ? " time" : "");
6700+
(flags & CHECKPOINT_CAUSE_TIME) ? " time" : "",
6701+
(flags & CHECKPOINT_FLUSH_ALL) ? " flush-all" :"");
67016702
}
67026703

67036704
/*

src/backend/commands/dbcommands.c

+15-11
Original file line numberDiff line numberDiff line change
@@ -527,15 +527,17 @@ createdb(const CreatedbStmt *stmt)
527527
InvokeObjectPostCreateHook(DatabaseRelationId, dboid, 0);
528528

529529
/*
530-
* Force a checkpoint before starting the copy. This will force dirty
531-
* buffers out to disk, to ensure source database is up-to-date on disk
532-
* for the copy. FlushDatabaseBuffers() would suffice for that, but we
533-
* also want to process any pending unlink requests. Otherwise, if a
534-
* checkpoint happened while we're copying files, a file might be deleted
535-
* just when we're about to copy it, causing the lstat() call in copydir()
536-
* to fail with ENOENT.
530+
* Force a checkpoint before starting the copy. This will force all dirty
531+
* buffers, including those of unlogged tables, out to disk, to ensure
532+
* source database is up-to-date on disk for the copy.
533+
* FlushDatabaseBuffers() would suffice for that, but we also want
534+
* to process any pending unlink requests. Otherwise, if a checkpoint
535+
* happened while we're copying files, a file might be deleted just when
536+
* we're about to copy it, causing the lstat() call in copydir() to fail
537+
* with ENOENT.
537538
*/
538-
RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT);
539+
RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT
540+
| CHECKPOINT_FLUSH_ALL);
539541

540542
/*
541543
* Take an MVCC snapshot to use while scanning through pg_tablespace. For
@@ -1125,16 +1127,18 @@ movedb(const char *dbname, const char *tblspcname)
11251127
dst_dbpath = GetDatabasePath(db_id, dst_tblspcoid);
11261128

11271129
/*
1128-
* Force a checkpoint before proceeding. This will force dirty buffers out
1129-
* to disk, to ensure source database is up-to-date on disk for the copy.
1130+
* Force a checkpoint before proceeding. This will force all dirty
1131+
* buffers, including those of unlogged tables, out to disk, to ensure
1132+
* source database is up-to-date on disk for the copy.
11301133
* FlushDatabaseBuffers() would suffice for that, but we also want to
11311134
* process any pending unlink requests. Otherwise, the check for existing
11321135
* files in the target directory might fail unnecessarily, not to mention
11331136
* that the copy might fail due to source files getting deleted under it.
11341137
* On Windows, this also ensures that background procs don't hold any open
11351138
* files, which would cause rmdir() to fail.
11361139
*/
1137-
RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT);
1140+
RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT
1141+
| CHECKPOINT_FLUSH_ALL);
11381142

11391143
/*
11401144
* Check for existence of files in the target directory, i.e., objects of

src/backend/storage/buffer/bufmgr.c

+9-7
Original file line numberDiff line numberDiff line change
@@ -1205,9 +1205,10 @@ UnpinBuffer(volatile BufferDesc *buf, bool fixOwner)
12051205
*
12061206
* This is called at checkpoint time to write out all dirty shared buffers.
12071207
* The checkpoint request flags should be passed in. If CHECKPOINT_IMMEDIATE
1208-
* is set, we disable delays between writes; if CHECKPOINT_IS_SHUTDOWN is
1209-
* set, we write even unlogged buffers, which are otherwise skipped. The
1210-
* remaining flags currently have no effect here.
1208+
* is set, we disable delays between writes; if CHECKPOINT_IS_SHUTDOWN,
1209+
* CHECKPOINT_END_OF_RECOVERY or CHECKPOINT_FLUSH_ALL is set, we write even
1210+
* unlogged buffers, which are otherwise skipped. The remaining flags
1211+
* currently have no effect here.
12111212
*/
12121213
static void
12131214
BufferSync(int flags)
@@ -1222,11 +1223,12 @@ BufferSync(int flags)
12221223
ResourceOwnerEnlargeBuffers(CurrentResourceOwner);
12231224

12241225
/*
1225-
* Unless this is a shutdown checkpoint, we write only permanent, dirty
1226-
* buffers. But at shutdown or end of recovery, we write all dirty
1227-
* buffers.
1226+
* Unless this is a shutdown checkpoint or we have been explicitly told,
1227+
* we write only permanent, dirty buffers. But at shutdown or end of
1228+
* recovery, we write all dirty buffers.
12281229
*/
1229-
if (!((flags & CHECKPOINT_IS_SHUTDOWN) || (flags & CHECKPOINT_END_OF_RECOVERY)))
1230+
if (!((flags & (CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_END_OF_RECOVERY |
1231+
CHECKPOINT_FLUSH_ALL))))
12301232
mask |= BM_PERMANENT;
12311233

12321234
/*

src/include/access/xlog.h

+2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ extern bool XLOG_DEBUG;
234234
/* These indicate the cause of a checkpoint request */
235235
#define CHECKPOINT_CAUSE_XLOG 0x0020 /* XLOG consumption */
236236
#define CHECKPOINT_CAUSE_TIME 0x0040 /* Elapsed time */
237+
#define CHECKPOINT_FLUSH_ALL 0x0080 /* Flush all pages, including those
238+
* belonging to unlogged tables */
237239

238240
/* Checkpoint statistics */
239241
typedef struct CheckpointStatsData

0 commit comments

Comments
 (0)