Skip to content

Commit 5cff5b5

Browse files
committed
Clarify pg_upgrade's creation of the map file structure. Also clean
up pg_dump's calling of pg_upgrade_support functions.
1 parent 66a8a04 commit 5cff5b5

File tree

4 files changed

+69
-80
lines changed

4 files changed

+69
-80
lines changed

contrib/pg_upgrade/info.c

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ static RelInfo *relarr_lookup_rel_oid(ClusterInfo *cluster, RelInfoArr *rel_arr,
3333
* generates database mappings for "old_db" and "new_db". Returns a malloc'ed
3434
* array of mappings. nmaps is a return parameter which refers to the number
3535
* mappings.
36-
*
37-
* NOTE: Its the Caller's responsibility to free the returned array.
3836
*/
3937
FileNameMap *
4038
gen_db_file_maps(DbInfo *old_db, DbInfo *new_db,
@@ -45,72 +43,56 @@ gen_db_file_maps(DbInfo *old_db, DbInfo *new_db,
4543
int num_maps = 0;
4644

4745
maps = (FileNameMap *) pg_malloc(sizeof(FileNameMap) *
48-
new_db->rel_arr.nrels);
46+
old_db->rel_arr.nrels);
4947

50-
for (relnum = 0; relnum < new_db->rel_arr.nrels; relnum++)
48+
for (relnum = 0; relnum < old_db->rel_arr.nrels; relnum++)
5149
{
52-
RelInfo *newrel = &new_db->rel_arr.rels[relnum];
53-
RelInfo *oldrel;
50+
RelInfo *oldrel = &old_db->rel_arr.rels[relnum];
51+
RelInfo *newrel;
5452

55-
/* toast tables are handled by their parent */
56-
if (strcmp(newrel->nspname, "pg_toast") == 0)
53+
/* toast tables are handled by their parents */
54+
if (strcmp(oldrel->nspname, "pg_toast") == 0)
5755
continue;
5856

59-
oldrel = relarr_lookup_rel_name(&old_cluster, &old_db->rel_arr,
60-
newrel->nspname, newrel->relname);
57+
newrel = relarr_lookup_rel_name(&old_cluster, &old_db->rel_arr,
58+
oldrel->nspname, oldrel->relname);
6159

6260
create_rel_filename_map(old_pgdata, new_pgdata, old_db, new_db,
6361
oldrel, newrel, maps + num_maps);
6462
num_maps++;
6563

6664
/*
6765
* So much for mapping this relation; now we need a mapping
68-
* for its corresponding toast relation, if any.
66+
* for its corresponding toast relation and toast index, if any.
6967
*/
7068
if (oldrel->toastrelid > 0)
7169
{
72-
RelInfo *new_toast;
73-
RelInfo *old_toast;
74-
char new_name[MAXPGPATH];
75-
char old_name[MAXPGPATH];
76-
77-
/* construct the new and old relnames for the toast relation */
78-
snprintf(old_name, sizeof(old_name), "pg_toast_%u", oldrel->reloid);
79-
snprintf(new_name, sizeof(new_name), "pg_toast_%u", newrel->reloid);
70+
char old_name[MAXPGPATH], new_name[MAXPGPATH];
71+
RelInfo *old_toast, *new_toast;
8072

81-
/* look them up in their respective arrays */
8273
old_toast = relarr_lookup_rel_oid(&old_cluster, &old_db->rel_arr,
83-
oldrel->toastrelid);
84-
new_toast = relarr_lookup_rel_name(&new_cluster, &new_db->rel_arr,
85-
"pg_toast", new_name);
74+
oldrel->toastrelid);
75+
new_toast = relarr_lookup_rel_oid(&new_cluster, &new_db->rel_arr,
76+
newrel->toastrelid);
8677

87-
/* finally create a mapping for them */
8878
create_rel_filename_map(old_pgdata, new_pgdata, old_db, new_db,
8979
old_toast, new_toast, maps + num_maps);
9080
num_maps++;
9181

9282
/*
93-
* also need to provide a mapping for the index of this toast
83+
* We also need to provide a mapping for the index of this toast
9484
* relation. The procedure is similar to what we did above for
9585
* toast relation itself, the only difference being that the
9686
* relnames need to be appended with _index.
9787
*/
98-
99-
/*
100-
* construct the new and old relnames for the toast index
101-
* relations
102-
*/
10388
snprintf(old_name, sizeof(old_name), "%s_index", old_toast->relname);
104-
snprintf(new_name, sizeof(new_name), "pg_toast_%u_index",
105-
newrel->reloid);
89+
snprintf(new_name, sizeof(new_name), "%s_index", new_toast->relname);
10690

107-
/* look them up in their respective arrays */
10891
old_toast = relarr_lookup_rel_name(&old_cluster, &old_db->rel_arr,
10992
"pg_toast", old_name);
11093
new_toast = relarr_lookup_rel_name(&new_cluster, &new_db->rel_arr,
11194
"pg_toast", new_name);
11295

113-
/* finally create a mapping for them */
11496
create_rel_filename_map(old_pgdata, new_pgdata, old_db,
11597
new_db, old_toast, new_toast, maps + num_maps);
11698
num_maps++;
@@ -133,15 +115,6 @@ create_rel_filename_map(const char *old_data, const char *new_data,
133115
const RelInfo *old_rel, const RelInfo *new_rel,
134116
FileNameMap *map)
135117
{
136-
map->old_relfilenode = old_rel->relfilenode;
137-
map->new_relfilenode = new_rel->relfilenode;
138-
139-
snprintf(map->old_nspname, sizeof(map->old_nspname), "%s", old_rel->nspname);
140-
snprintf(map->new_nspname, sizeof(map->new_nspname), "%s", new_rel->nspname);
141-
142-
snprintf(map->old_relname, sizeof(map->old_relname), "%s", old_rel->relname);
143-
snprintf(map->new_relname, sizeof(map->new_relname), "%s", new_rel->relname);
144-
145118
if (strlen(old_rel->tablespace) == 0)
146119
{
147120
/*
@@ -155,14 +128,21 @@ create_rel_filename_map(const char *old_data, const char *new_data,
155128
}
156129
else
157130
{
158-
/*
159-
* relation belongs to some tablespace, so use the tablespace location
160-
*/
131+
/* relation belongs to a tablespace, so use the tablespace location */
161132
snprintf(map->old_dir, sizeof(map->old_dir), "%s%s/%u", old_rel->tablespace,
162133
old_cluster.tablespace_suffix, old_db->db_oid);
163134
snprintf(map->new_dir, sizeof(map->new_dir), "%s%s/%u", new_rel->tablespace,
164135
new_cluster.tablespace_suffix, new_db->db_oid);
165136
}
137+
138+
map->old_relfilenode = old_rel->relfilenode;
139+
map->new_relfilenode = new_rel->relfilenode;
140+
141+
/* used only for logging and error reporing */
142+
snprintf(map->old_nspname, sizeof(map->old_nspname), "%s", old_rel->nspname);
143+
snprintf(map->new_nspname, sizeof(map->new_nspname), "%s", new_rel->nspname);
144+
snprintf(map->old_relname, sizeof(map->old_relname), "%s", old_rel->relname);
145+
snprintf(map->new_relname, sizeof(map->new_relname), "%s", new_rel->relname);
166146
}
167147

168148

contrib/pg_upgrade/pg_upgrade.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,18 @@ typedef struct
8787
{
8888
char old_dir[MAXPGPATH];
8989
char new_dir[MAXPGPATH];
90-
Oid old_relfilenode; /* Relfilenode of the old relation */
91-
Oid new_relfilenode; /* Relfilenode of the new relation */
92-
char old_nspname[NAMEDATALEN]; /* old name of the namespace */
93-
char old_relname[NAMEDATALEN]; /* old name of the relation */
94-
char new_nspname[NAMEDATALEN]; /* new name of the namespace */
95-
char new_relname[NAMEDATALEN]; /* new name of the relation */
90+
/*
91+
* old/new relfilenodes might differ for pg_largeobject(_metadata) indexes
92+
* due to VACUUM FULL or REINDEX. Other relfilenodes are preserved.
93+
*/
94+
Oid old_relfilenode;
95+
Oid new_relfilenode;
96+
/* the rest are used only for logging and error reporting */
97+
char old_nspname[NAMEDATALEN]; /* namespaces */
98+
char new_nspname[NAMEDATALEN];
99+
/* old/new relnames differ for toast tables and toast indexes */
100+
char old_relname[NAMEDATALEN];
101+
char new_relname[NAMEDATALEN];
96102
} FileNameMap;
97103

98104
/*

contrib/pg_upgrade/version_old_8_3.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ old_8_3_rebuild_tsvector_tables(ClusterInfo *cluster, bool check_mode)
222222
{
223223
PGresult *res;
224224
bool db_used = false;
225-
char old_nspname[NAMEDATALEN] = "",
226-
old_relname[NAMEDATALEN] = "";
225+
char nspname[NAMEDATALEN] = "",
226+
relname[NAMEDATALEN] = "";
227227
int ntups;
228228
int rowno;
229229
int i_nspname,
@@ -283,19 +283,19 @@ old_8_3_rebuild_tsvector_tables(ClusterInfo *cluster, bool check_mode)
283283
}
284284

285285
/* Rebuild all tsvector collumns with one ALTER TABLE command */
286-
if (strcmp(PQgetvalue(res, rowno, i_nspname), old_nspname) != 0 ||
287-
strcmp(PQgetvalue(res, rowno, i_relname), old_relname) != 0)
286+
if (strcmp(PQgetvalue(res, rowno, i_nspname), nspname) != 0 ||
287+
strcmp(PQgetvalue(res, rowno, i_relname), relname) != 0)
288288
{
289-
if (strlen(old_nspname) != 0 || strlen(old_relname) != 0)
289+
if (strlen(nspname) != 0 || strlen(relname) != 0)
290290
fprintf(script, ";\n\n");
291291
fprintf(script, "ALTER TABLE %s.%s\n",
292292
quote_identifier(PQgetvalue(res, rowno, i_nspname)),
293293
quote_identifier(PQgetvalue(res, rowno, i_relname)));
294294
}
295295
else
296296
fprintf(script, ",\n");
297-
strlcpy(old_nspname, PQgetvalue(res, rowno, i_nspname), sizeof(old_nspname));
298-
strlcpy(old_relname, PQgetvalue(res, rowno, i_relname), sizeof(old_relname));
297+
strlcpy(nspname, PQgetvalue(res, rowno, i_nspname), sizeof(nspname));
298+
strlcpy(relname, PQgetvalue(res, rowno, i_relname), sizeof(relname));
299299

300300
fprintf(script, "ALTER COLUMN %s "
301301
/* This could have been a custom conversion function call. */
@@ -304,7 +304,7 @@ old_8_3_rebuild_tsvector_tables(ClusterInfo *cluster, bool check_mode)
304304
quote_identifier(PQgetvalue(res, rowno, i_attname)));
305305
}
306306
}
307-
if (strlen(old_nspname) != 0 || strlen(old_relname) != 0)
307+
if (strlen(nspname) != 0 || strlen(relname) != 0)
308308
fprintf(script, ";\n\n");
309309

310310
PQclear(res);

src/bin/pg_dump/pg_dump.c

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,34 +2354,37 @@ binary_upgrade_set_relfilenodes(PQExpBuffer upgrade_buffer, Oid pg_class_oid,
23542354
"\n-- For binary upgrade, must preserve relfilenodes\n");
23552355

23562356
if (!is_index)
2357+
{
23572358
appendPQExpBuffer(upgrade_buffer,
23582359
"SELECT binary_upgrade.set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
23592360
pg_class_relfilenode);
2361+
/* only tables have toast tables, not indexes */
2362+
if (OidIsValid(pg_class_reltoastrelid))
2363+
{
2364+
/*
2365+
* One complexity is that the table definition might not require the
2366+
* creation of a TOAST table, and the TOAST table might have been
2367+
* created long after table creation, when the table was loaded with
2368+
* wide data. By setting the TOAST relfilenode we force creation of
2369+
* the TOAST heap and TOAST index by the backend so we can cleanly
2370+
* migrate the files during binary migration.
2371+
*/
2372+
2373+
appendPQExpBuffer(upgrade_buffer,
2374+
"SELECT binary_upgrade.set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
2375+
pg_class_reltoastrelid);
2376+
2377+
/* every toast table has an index */
2378+
appendPQExpBuffer(upgrade_buffer,
2379+
"SELECT binary_upgrade.set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
2380+
pg_class_reltoastidxid);
2381+
}
2382+
}
23602383
else
23612384
appendPQExpBuffer(upgrade_buffer,
23622385
"SELECT binary_upgrade.set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
23632386
pg_class_relfilenode);
23642387

2365-
if (OidIsValid(pg_class_reltoastrelid))
2366-
{
2367-
/*
2368-
* One complexity is that the table definition might not require the
2369-
* creation of a TOAST table, and the TOAST table might have been
2370-
* created long after table creation, when the table was loaded with
2371-
* wide data. By setting the TOAST relfilenode we force creation of
2372-
* the TOAST heap and TOAST index by the backend so we can cleanly
2373-
* migrate the files during binary migration.
2374-
*/
2375-
2376-
appendPQExpBuffer(upgrade_buffer,
2377-
"SELECT binary_upgrade.set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
2378-
pg_class_reltoastrelid);
2379-
2380-
/* every toast table has an index */
2381-
appendPQExpBuffer(upgrade_buffer,
2382-
"SELECT binary_upgrade.set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
2383-
pg_class_reltoastidxid);
2384-
}
23852388
appendPQExpBuffer(upgrade_buffer, "\n");
23862389

23872390
PQclear(upgrade_res);

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