Skip to content

Commit 4c6780f

Browse files
committed
pg_upgrade: prevent oid conflicts with new-cluster TOAST tables
Previously, TOAST tables only required in the new cluster could cause oid conflicts if they were auto-numbered and a later conflicting oid had to be assigned. Backpatch through 9.3
1 parent ec903d2 commit 4c6780f

File tree

5 files changed

+123
-10
lines changed

5 files changed

+123
-10
lines changed

contrib/pg_upgrade/dump.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "pg_upgrade.h"
1313

1414
#include <sys/types.h>
15+
#include "catalog/binary_upgrade.h"
16+
1517

1618
void
1719
generate_old_dump(void)
@@ -67,3 +69,71 @@ generate_old_dump(void)
6769
end_progress_output();
6870
check_ok();
6971
}
72+
73+
74+
/*
75+
* It is possible for there to be a mismatch in the need for TOAST tables
76+
* between the old and new servers, e.g. some pre-9.1 tables didn't need
77+
* TOAST tables but will need them in 9.1+. (There are also opposite cases,
78+
* but these are handled by setting binary_upgrade_next_toast_pg_class_oid.)
79+
*
80+
* We can't allow the TOAST table to be created by pg_dump with a
81+
* pg_dump-assigned oid because it might conflict with a later table that
82+
* uses that oid, causing a "file exists" error for pg_class conflicts, and
83+
* a "duplicate oid" error for pg_type conflicts. (TOAST tables need pg_type
84+
* entries.)
85+
*
86+
* Therefore, a backend in binary-upgrade mode will not create a TOAST
87+
* table unless an OID as passed in via pg_upgrade_support functions.
88+
* This function is called after the restore and uses ALTER TABLE to
89+
* auto-create any needed TOAST tables which will not conflict with
90+
* restored oids.
91+
*/
92+
void
93+
optionally_create_toast_tables(void)
94+
{
95+
int dbnum;
96+
97+
prep_status("Creating newly-required TOAST tables");
98+
99+
for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
100+
{
101+
PGresult *res;
102+
int ntups;
103+
int rowno;
104+
int i_nspname,
105+
i_relname;
106+
DbInfo *active_db = &new_cluster.dbarr.dbs[dbnum];
107+
PGconn *conn = connectToServer(&new_cluster, active_db->db_name);
108+
109+
res = executeQueryOrDie(conn,
110+
"SELECT n.nspname, c.relname "
111+
"FROM pg_catalog.pg_class c, "
112+
" pg_catalog.pg_namespace n "
113+
"WHERE c.relnamespace = n.oid AND "
114+
" n.nspname NOT IN ('pg_catalog', 'information_schema') AND "
115+
"c.relkind IN ('r', 'm') AND "
116+
"c.reltoastrelid = 0");
117+
118+
ntups = PQntuples(res);
119+
i_nspname = PQfnumber(res, "nspname");
120+
i_relname = PQfnumber(res, "relname");
121+
for (rowno = 0; rowno < ntups; rowno++)
122+
{
123+
/* enable auto-oid-numbered TOAST creation if needed */
124+
PQclear(executeQueryOrDie(conn, "SELECT binary_upgrade.set_next_toast_pg_class_oid('%d'::pg_catalog.oid);",
125+
OPTIONALLY_CREATE_TOAST_OID));
126+
127+
/* dummy command that also triggers check for required TOAST table */
128+
PQclear(executeQueryOrDie(conn, "ALTER TABLE %s.%s RESET (binary_upgrade_dummy_option);",
129+
quote_identifier(PQgetvalue(res, rowno, i_nspname)),
130+
quote_identifier(PQgetvalue(res, rowno, i_relname))));
131+
}
132+
133+
PQclear(res);
134+
135+
PQfinish(conn);
136+
}
137+
138+
check_ok();
139+
}

contrib/pg_upgrade/pg_upgrade.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ create_new_objects(void)
363363
if (GET_MAJOR_VERSION(old_cluster.major_version) < 903)
364364
set_frozenxids(true);
365365

366+
optionally_create_toast_tables();
367+
366368
/* regenerate now that we have objects in the databases */
367369
get_db_and_rel_infos(&new_cluster);
368370

contrib/pg_upgrade/pg_upgrade.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ void disable_old_cluster(void);
336336
/* dump.c */
337337

338338
void generate_old_dump(void);
339+
void optionally_create_toast_tables(void);
339340

340341

341342
/* exec.c */

src/backend/catalog/toasting.c

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,51 @@ create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
165165
if (rel->rd_rel->reltoastrelid != InvalidOid)
166166
return false;
167167

168-
/*
169-
* Check to see whether the table actually needs a TOAST table.
170-
*
171-
* If an update-in-place toast relfilenode is specified, force toast file
172-
* creation even if it seems not to need one.
173-
*/
174-
if (!needs_toast_table(rel) &&
175-
(!IsBinaryUpgrade ||
176-
!OidIsValid(binary_upgrade_next_toast_pg_class_oid)))
177-
return false;
168+
if (!IsBinaryUpgrade)
169+
{
170+
if (!needs_toast_table(rel))
171+
return false;
172+
}
173+
else
174+
{
175+
/*
176+
* Check to see whether the table needs a TOAST table.
177+
*
178+
* If an update-in-place TOAST relfilenode is specified, force TOAST file
179+
* creation even if it seems not to need one. This handles the case
180+
* where the old cluster needed a TOAST table but the new cluster
181+
* would not normally create one.
182+
*/
183+
184+
/*
185+
* If a TOAST oid is not specified, skip TOAST creation as we will do
186+
* it later so we don't create a TOAST table whose OID later conflicts
187+
* with a user-supplied OID. This handles cases where the old cluster
188+
* didn't need a TOAST table, but the new cluster does.
189+
*/
190+
if (!OidIsValid(binary_upgrade_next_toast_pg_class_oid))
191+
return false;
192+
193+
/*
194+
* If a special TOAST value has been passed in, it means we are in
195+
* cleanup mode --- we are creating needed TOAST tables after all user
196+
* tables with specified OIDs have been created. We let the system
197+
* assign a TOAST oid for us. The tables are empty so the missing
198+
* TOAST tables were not a problem.
199+
*/
200+
if (binary_upgrade_next_toast_pg_class_oid == OPTIONALLY_CREATE_TOAST_OID)
201+
{
202+
/* clear as it is not to be used; it is just a flag */
203+
binary_upgrade_next_toast_pg_class_oid = InvalidOid;
204+
205+
if (!needs_toast_table(rel))
206+
return false;
207+
}
208+
209+
/* both should be set, or not set */
210+
Assert(OidIsValid(binary_upgrade_next_toast_pg_class_oid) ==
211+
OidIsValid(binary_upgrade_next_toast_pg_type_oid));
212+
}
178213

179214
/*
180215
* If requested check lockmode is sufficient. This is a cross check in

src/include/catalog/binary_upgrade.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
#ifndef BINARY_UPGRADE_H
1515
#define BINARY_UPGRADE_H
1616

17+
#include "catalog/pg_authid.h"
18+
19+
/* pick a OID that will never be used for TOAST tables */
20+
#define OPTIONALLY_CREATE_TOAST_OID BOOTSTRAP_SUPERUSERID
21+
1722
extern PGDLLIMPORT Oid binary_upgrade_next_pg_type_oid;
1823
extern PGDLLIMPORT Oid binary_upgrade_next_array_pg_type_oid;
1924
extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_type_oid;

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