Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd5fef87

Browse files
committed
Flush unlogged table's buffers when copying or moving databases.
CREATE DATABASE and ALTER DATABASE .. SET TABLESPACE copy the sourcedatabase directory on the filesystem level. To ensure the on diskstate is consistent they block out users of the affected database andforce a checkpoint to flush out all data to disk. Unfortunately, up tonow, that checkpoint didn't flush out dirty buffers from unloggedrelations.That bug means there could be leftover dirty buffers in either thetemplate database, or the database in its old location. Leading toproblems when accessing relations in an inconsistent state; and topossible problems during shutdown in the SET TABLESPACE case becausebuffers 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 andFujii Masao.Backpatch to 9.1 where unlogged tables were introduced.
1 parent28e9ebb commitd5fef87

File tree

4 files changed

+30
-20
lines changed

4 files changed

+30
-20
lines changed

‎src/backend/access/transam/xlog.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7537,9 +7537,9 @@ LogCheckpointStart(int flags, bool restartpoint)
75377537
* the main message, but what about all the flags?
75387538
*/
75397539
if (restartpoint)
7540-
msg="restartpoint starting:%s%s%s%s%s%s%s";
7540+
msg="restartpoint starting:%s%s%s%s%s%s%s%s";
75417541
else
7542-
msg="checkpoint starting:%s%s%s%s%s%s%s";
7542+
msg="checkpoint starting:%s%s%s%s%s%s%s%s";
75437543

75447544
elog(LOG,msg,
75457545
(flags&CHECKPOINT_IS_SHUTDOWN) ?" shutdown" :"",
@@ -7548,7 +7548,8 @@ LogCheckpointStart(int flags, bool restartpoint)
75487548
(flags&CHECKPOINT_FORCE) ?" force" :"",
75497549
(flags&CHECKPOINT_WAIT) ?" wait" :"",
75507550
(flags&CHECKPOINT_CAUSE_XLOG) ?" xlog" :"",
7551-
(flags&CHECKPOINT_CAUSE_TIME) ?" time" :"");
7551+
(flags&CHECKPOINT_CAUSE_TIME) ?" time" :"",
7552+
(flags&CHECKPOINT_FLUSH_ALL) ?" flush-all" :"");
75527553
}
75537554

75547555
/*

‎src/backend/commands/dbcommands.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -523,15 +523,17 @@ createdb(const CreatedbStmt *stmt)
523523
InvokeObjectAccessHook(OAT_POST_CREATE,DatabaseRelationId,dboid,0);
524524

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

536538
/*
537539
* Take an MVCC snapshot to use while scanning through pg_tablespace. For
@@ -1111,16 +1113,18 @@ movedb(const char *dbname, const char *tblspcname)
11111113
dst_dbpath=GetDatabasePath(db_id,dst_tblspcoid);
11121114

11131115
/*
1114-
* Force a checkpoint before proceeding. This will force dirty buffers out
1115-
* to disk, to ensure source database is up-to-date on disk for the copy.
1116+
* Force a checkpoint before proceeding. This will force all dirty
1117+
* buffers, including those of unlogged tables, out to disk, to ensure
1118+
* source database is up-to-date on disk for the copy.
11161119
* FlushDatabaseBuffers() would suffice for that, but we also want to
11171120
* process any pending unlink requests. Otherwise, the check for existing
11181121
* files in the target directory might fail unnecessarily, not to mention
11191122
* that the copy might fail due to source files getting deleted under it.
11201123
* On Windows, this also ensures that the bgwriter doesn't hold any open
11211124
* files, which would cause rmdir() to fail.
11221125
*/
1123-
RequestCheckpoint(CHECKPOINT_IMMEDIATE |CHECKPOINT_FORCE |CHECKPOINT_WAIT);
1126+
RequestCheckpoint(CHECKPOINT_IMMEDIATE |CHECKPOINT_FORCE |CHECKPOINT_WAIT
1127+
|CHECKPOINT_FLUSH_ALL);
11241128

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

‎src/backend/storage/buffer/bufmgr.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,9 +1168,10 @@ UnpinBuffer(volatile BufferDesc *buf, bool fixOwner)
11681168
*
11691169
* This is called at checkpoint time to write out all dirty shared buffers.
11701170
* The checkpoint request flags should be passed in. If CHECKPOINT_IMMEDIATE
1171-
* is set, we disable delays between writes; if CHECKPOINT_IS_SHUTDOWN is
1172-
* set, we write even unlogged buffers, which are otherwise skipped. The
1173-
* remaining flags currently have no effect here.
1171+
* is set, we disable delays between writes; if CHECKPOINT_IS_SHUTDOWN,
1172+
* CHECKPOINT_END_OF_RECOVERY or CHECKPOINT_FLUSH_ALL is set, we write even
1173+
* unlogged buffers, which are otherwise skipped. The remaining flags
1174+
* currently have no effect here.
11741175
*/
11751176
staticvoid
11761177
BufferSync(intflags)
@@ -1185,10 +1186,12 @@ BufferSync(int flags)
11851186
ResourceOwnerEnlargeBuffers(CurrentResourceOwner);
11861187

11871188
/*
1188-
* Unless this is a shutdown checkpoint, we write only permanent, dirty
1189-
* buffers. But at shutdown or end of recovery, we write all dirty buffers.
1189+
* Unless this is a shutdown checkpoint or we have been explicitly told,
1190+
* we write only permanent, dirty buffers. But at shutdown or end of
1191+
* recovery, we write all dirty buffers.
11901192
*/
1191-
if (!((flags&CHECKPOINT_IS_SHUTDOWN)|| (flags&CHECKPOINT_END_OF_RECOVERY)))
1193+
if (!((flags& (CHECKPOINT_IS_SHUTDOWN |CHECKPOINT_END_OF_RECOVERY |
1194+
CHECKPOINT_FLUSH_ALL))))
11921195
mask |=BM_PERMANENT;
11931196

11941197
/*

‎src/include/access/xlog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ extern bool XLOG_DEBUG;
246246
/* These indicate the cause of a checkpoint request */
247247
#defineCHECKPOINT_CAUSE_XLOG0x0020/* XLOG consumption */
248248
#defineCHECKPOINT_CAUSE_TIME0x0040/* Elapsed time */
249+
#defineCHECKPOINT_FLUSH_ALL0x0080/* Flush all pages, including those
250+
* belonging to unlogged tables */
249251

250252
/* Checkpoint statistics */
251253
typedefstructCheckpointStatsData

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp