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

Commit4c6780f

Browse files
committed
pg_upgrade: prevent oid conflicts with new-cluster TOAST tables
Previously, TOAST tables only required in the new cluster could causeoid conflicts if they were auto-numbered and a later conflicting oid hadto be assigned.Backpatch through 9.3
1 parentec903d2 commit4c6780f

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+
intdbnum;
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+
intntups;
103+
introwno;
104+
inti_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+
"FROMpg_catalog.pg_class c, "
112+
"pg_catalog.pg_namespace n "
113+
"WHEREc.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 @@ voiddisable_old_cluster(void);
336336
/* dump.c */
337337

338338
voidgenerate_old_dump(void);
339+
voidoptionally_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
#ifndefBINARY_UPGRADE_H
1515
#defineBINARY_UPGRADE_H
1616

17+
#include"catalog/pg_authid.h"
18+
19+
/* pick a OID that will never be used for TOAST tables */
20+
#defineOPTIONALLY_CREATE_TOAST_OIDBOOTSTRAP_SUPERUSERID
21+
1722
externPGDLLIMPORTOidbinary_upgrade_next_pg_type_oid;
1823
externPGDLLIMPORTOidbinary_upgrade_next_array_pg_type_oid;
1924
externPGDLLIMPORTOidbinary_upgrade_next_toast_pg_type_oid;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp