Skip to content

Commit 1a2c17f

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 0f97c72 commit 1a2c17f

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
@@ -166,50 +166,38 @@ create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
166166
if (rel->rd_rel->reltoastrelid != InvalidOid)
167167
return false;
168168

169+
/*
170+
* Check to see whether the table actually needs a TOAST table.
171+
*/
169172
if (!IsBinaryUpgrade)
170173
{
174+
/* Normal mode, normal check */
171175
if (!needs_toast_table(rel))
172176
return false;
173177
}
174178
else
175179
{
176180
/*
177-
* Check to see whether the table needs a TOAST table.
181+
* In binary-upgrade mode, create a TOAST table if and only if
182+
* pg_upgrade told us to (ie, a TOAST table OID has been provided).
178183
*
179-
* If an update-in-place TOAST relfilenode is specified, force TOAST
180-
* file creation even if it seems not to need one. This handles the
181-
* case where the old cluster needed a TOAST table but the new cluster
182-
* would not normally create one.
183-
*/
184-
185-
/*
186-
* If a TOAST oid is not specified, skip TOAST creation as we will do
187-
* it later so we don't create a TOAST table whose OID later conflicts
188-
* with a user-supplied OID. This handles cases where the old cluster
189-
* didn't need a TOAST table, but the new cluster does.
184+
* This indicates that the old cluster had a TOAST table for the
185+
* current table. We must create a TOAST table to receive the old
186+
* TOAST file, even if the table seems not to need one.
187+
*
188+
* Contrariwise, if the old cluster did not have a TOAST table, we
189+
* should be able to get along without one even if the new version's
190+
* needs_toast_table rules suggest we should have one. There is a lot
191+
* of daylight between where we will create a TOAST table and where
192+
* one is really necessary to avoid failures, so small cross-version
193+
* differences in the when-to-create heuristic shouldn't be a problem.
194+
* If we tried to create a TOAST table anyway, we would have the
195+
* problem that it might take up an OID that will conflict with some
196+
* old-cluster table we haven't seen yet.
190197
*/
191-
if (!OidIsValid(binary_upgrade_next_toast_pg_class_oid))
198+
if (!OidIsValid(binary_upgrade_next_toast_pg_class_oid) ||
199+
!OidIsValid(binary_upgrade_next_toast_pg_type_oid))
192200
return false;
193-
194-
/*
195-
* If a special TOAST value has been passed in, it means we are in
196-
* cleanup mode --- we are creating needed TOAST tables after all user
197-
* tables with specified OIDs have been created. We let the system
198-
* assign a TOAST oid for us. The tables are empty so the missing
199-
* TOAST tables were not a problem.
200-
*/
201-
if (binary_upgrade_next_toast_pg_class_oid == OPTIONALLY_CREATE_TOAST_OID)
202-
{
203-
/* clear as it is not to be used; it is just a flag */
204-
binary_upgrade_next_toast_pg_class_oid = InvalidOid;
205-
206-
if (!needs_toast_table(rel))
207-
return false;
208-
}
209-
210-
/* both should be set, or not set */
211-
Assert(OidIsValid(binary_upgrade_next_toast_pg_class_oid) ==
212-
OidIsValid(binary_upgrade_next_toast_pg_type_oid));
213201
}
214202

215203
/*

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
@@ -332,13 +332,11 @@ create_new_objects(void)
332332

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

340-
optionally_create_toast_tables();
341-
342340
/* regenerate now that we have objects in the databases */
343341
get_db_and_rel_infos(&new_cluster);
344342
}

src/bin/pg_upgrade/pg_upgrade.h

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

355355
void generate_old_dump(void);
356-
void optionally_create_toast_tables(void);
357356

358357

359358
/* 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)