Skip to content

Commit fca9f34

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 24ae449 commit fca9f34

File tree

4 files changed

+125
-10
lines changed

4 files changed

+125
-10
lines changed

contrib/pg_upgrade/dump.c

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

1414
#include <sys/types.h>
15+
#include "catalog/pg_authid.h"
16+
17+
/* pick a OID that will never be used for TOAST tables */
18+
#define OPTIONALLY_CREATE_TOAST_OID BOOTSTRAP_SUPERUSERID
19+
1520

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

contrib/pg_upgrade/pg_upgrade.c

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

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

contrib/pg_upgrade/pg_upgrade.h

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

337337
void generate_old_dump(void);
338+
void optionally_create_toast_tables(void);
338339

339340

340341
/* exec.c */

src/backend/catalog/toasting.c

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "catalog/heap.h"
2121
#include "catalog/index.h"
2222
#include "catalog/namespace.h"
23+
#include "catalog/pg_authid.h"
2324
#include "catalog/pg_namespace.h"
2425
#include "catalog/pg_opclass.h"
2526
#include "catalog/pg_type.h"
@@ -39,6 +40,9 @@ static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
3940
Datum reloptions);
4041
static bool needs_toast_table(Relation rel);
4142

43+
/* pick a OID that will never be used for TOAST tables */
44+
#define OPTIONALLY_CREATE_TOAST_OID BOOTSTRAP_SUPERUSERID
45+
4246

4347
/*
4448
* AlterTableCreateToastTable
@@ -150,16 +154,51 @@ create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid, Datum reloptio
150154
if (rel->rd_rel->reltoastrelid != InvalidOid)
151155
return false;
152156

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

164203
/*
165204
* Create the toast table and its index

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