8000 Fix ALTER TABLE ... SET TABLESPACE for unlogged relations. · qadahtm/postgres@5f9a86b · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f9a86b

Browse files
committed
Fix ALTER TABLE ... SET TABLESPACE for unlogged relations.
Changing the tablespace of an unlogged relation did not WAL log the creation and content of the init fork. Thus, after a standby is promoted, unlogged relation cannot be accessed anymore, with errors like: ERROR: 58P01: could not open file "pg_tblspc/...": No such file or directory Additionally the init fork was not synced to disk, independent of the configured wal_level, a relatively small durability risk. Investigation of that problem also brought to light that, even for permanent relations, the creation of !main forks was not WAL logged, i.e. no XLOG_SMGR_CREATE record were emitted. That mostly turns out not to be a problem, because these files were created when the actual relation data is copied; nonexistent files are not treated as an error condition during replay. But that doesn't work for empty files, and generally feels a bit haphazard. Luckily, outside init and main forks, empty forks don't occur often or are not a problem. Add the required WAL logging and syncing to disk. Reported-By: Michael Paquier Author: Michael Paquier and Andres Freund Discussion: 20151210163230.GA11331@alap3.anarazel.de Backpatch: 9.1, where unlogged relations were introduced
1 parent 386dcd5 commit 5f9a86b

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/backend/commands/tablecmds.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8049,6 +8049,15 @@ ATExecSetTableSpace(Oid tableOid, Oid newTableSpace, LOCKMODE lockmode)
80498049
if (smgrexists(rel->rd_smgr, forkNum))
80508050
{
80518051
smgrcreate(dstrel, forkNum, false);
8052+
8053+
/*
8054+
* WAL log creation if the relation is persistent, or this is the
8055+
* init fork of an unlogged relation.
8056+
*/
8057+
if (rel->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT ||
8058+
(rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED &&
8059+
forkNum == INIT_FORKNUM))
8060+
log_smgrcreate(&newrnode, forkNum);
80528061
copy_relation_data(rel->rd_smgr, dstrel, forkNum,
80538062
rel->rd_rel->relpersistence);
80548063
}
@@ -8090,6 +8099,7 @@ copy_relation_data(SMgrRelation src, SMgrRelation dst,
80908099
char *buf;
80918100
Page page;
80928101
bool use_wal;
8102+
bool copying_initfork;
80938103
BlockNumber nblocks;
80948104
BlockNumber blkno;
80958105

@@ -8102,11 +8112,20 @@ copy_relation_data(SMgrRelation src, SMgrRelation dst,
81028112
buf = (char *) palloc(BLCKSZ);
81038113
page = (Page) buf;
81048114

8115+
/*
8116+
* The init fork for an unlogged relation in many respects has to be
8117+
* treated the same as normal relation, changes need to be WAL logged and
8118+
* it needs to be synced to disk.
8119+
*/
8120+
copying_initfork = relpersistence == RELPERSISTENCE_UNLOGGED &&
8121+
forkNum == INIT_FORKNUM;
8122+
81058123
/*
81068124
* We need to log the copied data in WAL iff WAL archiving/streaming is
81078125
* enabled AND it's a permanent relation.
81088126
*/
8109-
use_wal = XLogIsNeeded() && relpersistence == RELPERSISTENCE_PERMANENT;
8127+
use_wal = XLogIsNeeded() &&
8128+
(relpersistence == RELPERSISTENCE_PERMANENT || copying_initfork);
81108129

81118130
nblocks = smgrnblocks(src, forkNum);
81128131

@@ -8145,7 +8164,7 @@ copy_relation_data(SMgrRelation src, SMgrRelation dst,
81458164
* wouldn't replay our earlier WAL entries. If we do not fsync those pages
81468165
* here, they might still not be on disk when the crash occurs.
81478166
*/
8148-
if (relpersistence == RELPERSISTENCE_PERMANENT)
8167+
if (relpersistence == RELPERSISTENCE_PERMANENT || copying_initfork)
81498168
smgrimmedsync(dst, forkNum);
81508169
}
81518170

0 commit comments

Comments
 (0)
0