Skip to content

Commit fd29810

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 3609c0d commit fd29810

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
@@ -7865,9 +7865,9 @@ LogCheckpointStart(int flags, bool restartpoint)
78657865
* the main message, but what about all the flags?
78667866
*/
78677867
if (restartpoint)
7868-
msg = "restartpoint starting:%s%s%s%s%s%s%s";
7868+
msg = "restartpoint starting:%s%s%s%s%s%s%s%s";
78697869
else
7870-
msg = "checkpoint starting:%s%s%s%s%s%s%s";
7870+
msg = "checkpoint starting:%s%s%s%s%s%s%s%s";
78717871

78727872
elog(LOG, msg,
78737873
(flags & CHECKPOINT_IS_SHUTDOWN) ? " shutdown" : "",
@@ -7876,7 +7876,8 @@ LogCheckpointStart(int flags, bool restartpoint)
78767876
(flags & CHECKPOINT_FORCE) ? " force" : "",
78777877
(flags & CHECKPOINT_WAIT) ? " wait" : "",
78787878
(flags & CHECKPOINT_CAUSE_XLOG) ? " xlog" : "",
7879-
(flags & CHECKPOINT_CAUSE_TIME) ? " time" : "");
7879+
(flags & CHECKPOINT_CAUSE_TIME) ? " time" : "",
7880+
(flags & CHECKPOINT_FLUSH_ALL) ? " flush-all" :"");
78807881
}
78817882

78827883
/*

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
DatabaseRelationId, dboid, 0, NULL);
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
@@ -1122,16 +1124,18 @@ movedb(const char *dbname, const char *tblspcname)
11221124
dst_dbpath = GetDatabasePath(db_id, dst_tblspcoid);
11231125

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

11361140
/*
11371141
* 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
@@ -1191,9 +1191,10 @@ UnpinBuffer(volatile BufferDesc *buf, bool fixOwner)
11911191
*
11921192
* This is called at checkpoint time to write out all dirty shared buffers.
11931193
* The checkpoint request flags should be passed in. If CHECKPOINT_IMMEDIATE
1194-
* is set, we disable delays between writes; if CHECKPOINT_IS_SHUTDOWN is
1195-
* set, we write even unlogged buffers, which are otherwise skipped. The
1196-
* remaining flags currently have no effect here.
1194+
* is set, we disable delays between writes; if CHECKPOINT_IS_SHUTDOWN,
1195+
* CHECKPOINT_END_OF_RECOVERY or CHECKPOINT_FLUSH_ALL is set, we write even
1196+
* unlogged buffers, which are otherwise skipped. The remaining flags
1197+
* currently have no effect here.
11971198
*/
11981199
static void
11991200
BufferSync(int flags)
@@ -1208,10 +1209,12 @@ BufferSync(int flags)
12081209
ResourceOwnerEnlargeBuffers(CurrentResourceOwner);
12091210

12101211
/*
1211-
* Unless this is a shutdown checkpoint, we write only permanent, dirty
1212-
* buffers. But at shutdown or end of recovery, we write all dirty buffers.
1212+
* Unless this is a shutdown checkpoint or we have been explicitly told,
1213+
* we write only permanent, dirty buffers. But at shutdown or end of
1214+
* recovery, we write all dirty buffers.
12131215
*/
1214-
if (!((flags & CHECKPOINT_IS_SHUTDOWN) || (flags & CHECKPOINT_END_OF_RECOVERY)))
1216+
if (!((flags & (CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_END_OF_RECOVERY |
1217+
CHECKPOINT_FLUSH_ALL))))
12151218
mask |= BM_PERMANENT;
12161219

12171220
/*

src/include/access/xlog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ extern bool XLOG_DEBUG;
240240
/* These indicate the cause of a checkpoint request */
241241
#define CHECKPOINT_CAUSE_XLOG 0x0020 /* XLOG consumption */
242242
#define CHECKPOINT_CAUSE_TIME 0x0040 /* Elapsed time */
243+
#define CHECKPOINT_FLUSH_ALL 0x0080 /* Flush all pages, including those
244+
* belonging to unlogged tables */
243245

244246
/* Checkpoint statistics */
245247
typedef struct CheckpointStatsData

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy