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

Commit52b0777

Browse files
committed
pg_dump, pg_upgrade: allow postgres/template1 tablespace moves
Modify pg_dump to restore postgres/template1 databases to non-defaulttablespaces by switching out of the database to be moved, then switchingback.Also, to fix potentially cases where the old/new tablespaces might notmatch, fix pg_upgrade to process new/old tablespaces separately in allcases.Report by Marti RaudseppPatch by Marti Raudsepp, meBackpatch through 9.0
1 parent6d36d7b commit52b0777

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

‎contrib/pg_upgrade/info.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static void map_rel(migratorContext *ctx, const RelInfo *oldrel,
2727
staticvoidmap_rel_by_id(migratorContext*ctx,Oidoldid,Oidnewid,
2828
constchar*old_nspname,constchar*old_relname,
2929
constchar*new_nspname,constchar*new_relname,
30-
constchar*old_tablespace,constDbInfo*old_db,
30+
constchar*old_tablespace,constchar*new_tablespace,constDbInfo*old_db,
3131
constDbInfo*new_db,constchar*olddata,
3232
constchar*newdata,FileNameMap*map);
3333
staticRelInfo*relarr_lookup_reloid(migratorContext*ctx,
@@ -140,7 +140,7 @@ map_rel(migratorContext *ctx, const RelInfo *oldrel, const RelInfo *newrel,
140140
constchar*newdata,FileNameMap*map)
141141
{
142142
map_rel_by_id(ctx,oldrel->relfilenode,newrel->relfilenode,oldrel->nspname,
143-
oldrel->relname,newrel->nspname,newrel->relname,oldrel->tablespace,old_db,
143+
oldrel->relname,newrel->nspname,newrel->relname,oldrel->tablespace,newrel->tablespace,old_db,
144144
new_db,olddata,newdata,map);
145145
}
146146

@@ -154,7 +154,7 @@ static void
154154
map_rel_by_id(migratorContext*ctx,Oidoldid,Oidnewid,
155155
constchar*old_nspname,constchar*old_relname,
156156
constchar*new_nspname,constchar*new_relname,
157-
constchar*old_tablespace,constDbInfo*old_db,
157+
constchar*old_tablespace,constchar*new_tablespace,constDbInfo*old_db,
158158
constDbInfo*new_db,constchar*olddata,
159159
constchar*newdata,FileNameMap*map)
160160
{
@@ -166,14 +166,14 @@ map_rel_by_id(migratorContext *ctx, Oid oldid, Oid newid,
166166
snprintf(map->new_nspname,sizeof(map->new_nspname),"%s",new_nspname);
167167
snprintf(map->new_relname,sizeof(map->new_relname),"%s",new_relname);
168168

169+
/* In case old/new tablespaces don't match, do them separately. */
169170
if (strlen(old_tablespace)==0)
170171
{
171172
/*
172173
* relation belongs to the default tablespace, hence relfiles would
173174
* exist in the data directories.
174175
*/
175176
snprintf(map->old_file,sizeof(map->old_file),"%s/base/%u",olddata,old_db->db_oid);
176-
snprintf(map->new_file,sizeof(map->new_file),"%s/base/%u",newdata,new_db->db_oid);
177177
}
178178
else
179179
{
@@ -183,7 +183,24 @@ map_rel_by_id(migratorContext *ctx, Oid oldid, Oid newid,
183183
*/
184184
snprintf(map->old_file,sizeof(map->old_file),"%s%s/%u",old_tablespace,
185185
ctx->old.tablespace_suffix,old_db->db_oid);
186-
snprintf(map->new_file,sizeof(map->new_file),"%s%s/%u",old_tablespace,
186+
}
187+
188+
/* Do the same for new tablespaces */
189+
if (strlen(new_tablespace)==0)
190+
{
191+
/*
192+
* relation belongs to the default tablespace, hence relfiles would
193+
* exist in the data directories.
194+
*/
195+
snprintf(map->new_file,sizeof(map->new_file),"%s/base/%u",newdata,new_db->db_oid);
196+
}
197+
else
198+
{
199+
/*
200+
* relation belongs to some tablespace, hence copy its physical
201+
* location
202+
*/
203+
snprintf(map->new_file,sizeof(map->new_file),"%s%s/%u",new_tablespace,
187204
ctx->new.tablespace_suffix,new_db->db_oid);
188205
}
189206
}

‎src/bin/pg_dump/pg_dumpall.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,28 @@ dumpCreateDB(PGconn *conn)
13031303
appendPQExpBuffer(buf,";\n");
13041304
}
13051305
}
1306+
elseif (strcmp(dbtablespace,"pg_default")!=0&& !no_tablespaces)
1307+
{
1308+
/*
1309+
* Cannot change tablespace of the database we're connected to,
1310+
* so to move "postgres" to another tablespace, we connect to
1311+
* "template1", and vice versa.
1312+
*/
1313+
if (strcmp(dbname,"postgres")==0)
1314+
appendPQExpBuffer(buf,"%s\\connect template1\n",
1315+
/* Add a space before \\connect so pg_upgrade can split */
1316+
binary_upgrade ?" " :"");
1317+
else
1318+
appendPQExpBuffer(buf,"%s\\connect postgres\n",
1319+
binary_upgrade ?" " :"");
1320+
1321+
appendPQExpBuffer(buf,"ALTER DATABASE %s SET TABLESPACE %s;\n",
1322+
fdbname,fmtId(dbtablespace));
1323+
1324+
/* connect to original database */
1325+
appendPQExpBuffer(buf,"%s\\connect %s\n",
1326+
binary_upgrade ?" " :"",fdbname);
1327+
}
13061328

13071329
if (binary_upgrade)
13081330
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp