Skip to content

Commit 196870f

Browse files
committed
Fix pg_upgrade to not fail when new-cluster TOAST rules differ from old.
This patch essentially reverts commit 4c6780f, in favor of a much simpler solution for the case where the new cluster would choose to create a TOAST table but the old cluster doesn't have one: just don't create a TOAST table. The existing code failed in at least two different ways if the situation arose: (1) ALTER TABLE RESET didn't grab an exclusive lock, so that the lock sanity check in create_toast_table failed; (2) pg_upgrade did not provide a pg_type OID for the new toast table, so that the crosscheck in TypeCreate failed. While both these problems were introduced by later patches, they show that the hack being used to cause TOAST table creation is overwhelmingly fragile (and untested). I also note that before the TypeCreate crosscheck was added, the code would have resulted in assigning an indeterminate pg_type OID to the toast table, possibly causing a later OID conflict in that catalog; so that it didn't really work even when committed. If we simply don't create a TOAST table, there will only be a problem if the code tries to store a tuple that's wider than a page, and field compression isn't sufficient to get it under a page. Given that the TOAST creation threshold is intended to be about a quarter of a page, it's very hard to believe that cross-version differences in the do-we-need-a-toast- table heuristic could result in an observable problem. So let's just follow the old version's conclusion about whether a TOAST table is needed. (If we ever do change needs_toast_table() so much that this conclusion doesn't apply, we can devise a solution at that time, and hopefully do it in a less klugy way than 4c6780f did.) Back-patch to 9.3, like the previous patch. Discussion: <8110.1462291671@sss.pgh.pa.us>
1 parent 504af1f commit 196870f

File tree

5 files changed

+22
-111
lines changed

5 files changed

+22
-111
lines changed

src/backend/catalog/toasting.c

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -165,50 +165,38 @@ 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+
*/
168171
if (!IsBinaryUpgrade)
169172
{
173+
/* Normal mode, normal check */
170174
if (!needs_toast_table(rel))
171175
return false;
172176
}
173177
else
174178
{
175179
/*
176-
* Check to see whether the table needs a TOAST table.
180+
* In binary-upgrade mode, create a TOAST table if and only if
181+
* pg_upgrade told us to (ie, a TOAST table OID has been provided).
177182
*
178-
* If an update-in-place TOAST relfilenode is specified, force TOAST
179-
* file creation even if it seems not to need one. This handles the
180-
* case 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.
183+
* This indicates that the old cluster had a TOAST table for the
184+
* current table. We must create a TOAST table to receive the old
185+
* TOAST file, even if the table seems not to need one.
186+
*
187+
* Contrariwise, if the old cluster did not have a TOAST table, we
188+
* should be able to get along without one even if the new version's
189+
* needs_toast_table rules suggest we should have one. There is a lot
190+
* of daylight between where we will create a TOAST table and where
191+
* one is really necessary to avoid failures, so small cross-version
192+
* differences in the when-to-create heuristic shouldn't be a problem.
193+
* If we tried to create a TOAST table anyway, we would have the
194+
* problem that it might take up an OID that will conflict with some
195+
* old-cluster table we haven't seen yet.
189196
*/
190-
if (!OidIsValid(binary_upgrade_next_toast_pg_class_oid))
197+
if (!OidIsValid(binary_upgrade_next_toast_pg_class_oid) ||
198+
!OidIsValid(binary_upgrade_next_toast_pg_type_oid))
191199
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));
212200
}
213201

214202
/*

src/bin/pg_upgrade/dump.c

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

1414
#include <sys/types.h>
15-
#include "catalog/binary_upgrade.h"
1615

1716

1817
void
@@ -69,71 +68,3 @@ generate_old_dump(void)
6968
end_progress_output();
7069
check_ok();
7170
}
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 pg_catalog.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-
}

src/bin/pg_upgrade/pg_upgrade.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,11 @@ create_new_objects(void)
334334

335335
/*
336336
* We don't have minmxids for databases or relations in pre-9.3 clusters,
337-
* so set those after we have restores the schemas.
337+
* so set those after we have restored the schema.
338338
*/
339339
if (GET_MAJOR_VERSION(old_cluster.major_version) < 903)
340340
set_frozenxids(true);
341341

342-
optionally_create_toast_tables();
343-
344342
/* regenerate now that we have objects in the databases */
345343
get_db_and_rel_infos(&new_cluster);
346344
}

src/bin/pg_upgrade/pg_upgrade.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ void disable_old_cluster(void);
349349
/* dump.c */
350350

351351
void generate_old_dump(void);
352-
void optionally_create_toast_tables(void);
353352

354353

355354
/* exec.c */

src/include/catalog/binary_upgrade.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
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-
2217
extern PGDLLIMPORT Oid binary_upgrade_next_pg_type_oid;
2318
extern PGDLLIMPORT Oid binary_upgrade_next_array_pg_type_oid;
2419
extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_type_oid;

0 commit comments

Comments
 (0)