diff options
Diffstat (limited to 'contrib/pg_upgrade/dump.c')
-rw-r--r-- | contrib/pg_upgrade/dump.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/contrib/pg_upgrade/dump.c b/contrib/pg_upgrade/dump.c index b112f3a9527..e623a226327 100644 --- a/contrib/pg_upgrade/dump.c +++ b/contrib/pg_upgrade/dump.c @@ -12,6 +12,8 @@ #include "pg_upgrade.h" #include <sys/types.h> +#include "catalog/binary_upgrade.h" + void generate_old_dump(void) @@ -67,3 +69,71 @@ generate_old_dump(void) end_progress_output(); check_ok(); } + + +/* + * It is possible for there to be a mismatch in the need for TOAST tables + * between the old and new servers, e.g. some pre-9.1 tables didn't need + * TOAST tables but will need them in 9.1+. (There are also opposite cases, + * but these are handled by setting binary_upgrade_next_toast_pg_class_oid.) + * + * We can't allow the TOAST table to be created by pg_dump with a + * pg_dump-assigned oid because it might conflict with a later table that + * uses that oid, causing a "file exists" error for pg_class conflicts, and + * a "duplicate oid" error for pg_type conflicts. (TOAST tables need pg_type + * entries.) + * + * Therefore, a backend in binary-upgrade mode will not create a TOAST + * table unless an OID as passed in via pg_upgrade_support functions. + * This function is called after the restore and uses ALTER TABLE to + * auto-create any needed TOAST tables which will not conflict with + * restored oids. + */ +void +optionally_create_toast_tables(void) +{ + int dbnum; + + prep_status("Creating newly-required TOAST tables"); + + for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++) + { + PGresult *res; + int ntups; + int rowno; + int i_nspname, + i_relname; + DbInfo *active_db = &new_cluster.dbarr.dbs[dbnum]; + PGconn *conn = connectToServer(&new_cluster, active_db->db_name); + + res = executeQueryOrDie(conn, + "SELECT n.nspname, c.relname " + "FROM pg_catalog.pg_class c, " + " pg_catalog.pg_namespace n " + "WHERE c.relnamespace = n.oid AND " + " n.nspname NOT IN ('pg_catalog', 'information_schema') AND " + "c.relkind IN ('r', 'm') AND " + "c.reltoastrelid = 0"); + + ntups = PQntuples(res); + i_nspname = PQfnumber(res, "nspname"); + i_relname = PQfnumber(res, "relname"); + for (rowno = 0; rowno < ntups; rowno++) + { + /* enable auto-oid-numbered TOAST creation if needed */ + PQclear(executeQueryOrDie(conn, "SELECT binary_upgrade.set_next_toast_pg_class_oid('%d'::pg_catalog.oid);", + OPTIONALLY_CREATE_TOAST_OID)); + + /* dummy command that also triggers check for required TOAST table */ + PQclear(executeQueryOrDie(conn, "ALTER TABLE %s.%s RESET (binary_upgrade_dummy_option);", + quote_identifier(PQgetvalue(res, rowno, i_nspname)), + quote_identifier(PQgetvalue(res, rowno, i_relname)))); + } + + PQclear(res); + + PQfinish(conn); + } + + check_ok(); +} |