Commit 5d16332e authored by Bruce Momjian's avatar Bruce Momjian

pg_upgrade: use CTE query rather than temp table

Now that 8.3 is not supported, we can use a CTE and not temp tables.
This allows for auto-oid assignment protection in a future patch.
parent e8c81b1b
...@@ -320,71 +320,75 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo) ...@@ -320,71 +320,75 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
*/ */
snprintf(query, sizeof(query), snprintf(query, sizeof(query),
"CREATE TEMPORARY TABLE info_rels (reloid) AS SELECT c.oid " /* get regular heap */
"FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n " "WITH regular_heap (reloid) AS ( "
" ON c.relnamespace = n.oid " " SELECT c.oid "
"LEFT OUTER JOIN pg_catalog.pg_index i " " FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
" ON c.oid = i.indexrelid " " ON c.relnamespace = n.oid "
"WHERE relkind IN ('r', 'm', 'i', 'S') AND " " LEFT OUTER JOIN pg_catalog.pg_index i "
" ON c.oid = i.indexrelid "
/* " WHERE relkind IN ('r', 'm', 'i', 'S') AND "
* pg_dump only dumps valid indexes; testing indisready is necessary in /*
* 9.2, and harmless in earlier/later versions. * pg_dump only dumps valid indexes; testing indisready is necessary in
*/ * 9.2, and harmless in earlier/later versions.
" i.indisvalid IS DISTINCT FROM false AND " */
" i.indisready IS DISTINCT FROM false AND " " i.indisvalid IS DISTINCT FROM false AND "
/* exclude possible orphaned temp tables */ " i.indisready IS DISTINCT FROM false AND "
" ((n.nspname !~ '^pg_temp_' AND " /* exclude possible orphaned temp tables */
" n.nspname !~ '^pg_toast_temp_' AND " " ((n.nspname !~ '^pg_temp_' AND "
/* skip pg_toast because toast index have relkind == 'i', not 't' */ " n.nspname !~ '^pg_toast_temp_' AND "
" n.nspname NOT IN ('pg_catalog', 'information_schema', " /* skip pg_toast because toast index have relkind == 'i', not 't' */
" 'binary_upgrade', 'pg_toast') AND " " n.nspname NOT IN ('pg_catalog', 'information_schema', "
" c.oid >= %u) " " 'binary_upgrade', 'pg_toast') AND "
" OR (n.nspname = 'pg_catalog' AND " " c.oid >= %u) OR "
" relname IN ('pg_largeobject', 'pg_largeobject_loid_pn_index'%s) ));", " (n.nspname = 'pg_catalog' AND "
FirstNormalObjectId, " relname IN ('pg_largeobject', 'pg_largeobject_loid_pn_index'%s) ))), "
/* does pg_largeobject_metadata need to be migrated? */ /*
(GET_MAJOR_VERSION(old_cluster.major_version) <= 804) ? * We have to gather the TOAST tables in later steps because we
"" : ", 'pg_largeobject_metadata', 'pg_largeobject_metadata_oid_index'"); * can't schema-qualify TOAST tables.
*/
PQclear(executeQueryOrDie(conn, "%s", query)); /* get TOAST heap */
" toast_heap (reloid) AS ( "
/* " SELECT reltoastrelid "
* Get TOAST tables and indexes; we have to gather the TOAST tables in " FROM regular_heap JOIN pg_catalog.pg_class c "
* later steps because we can't schema-qualify TOAST tables. " ON regular_heap.reloid = c.oid "
*/ " AND c.reltoastrelid != %u), "
PQclear(executeQueryOrDie(conn, /* get indexes on regular and TOAST heap */
"INSERT INTO info_rels " " all_index (reloid) AS ( "
"SELECT reltoastrelid " " SELECT indexrelid "
"FROM info_rels i JOIN pg_catalog.pg_class c " " FROM pg_index "
" ON i.reloid = c.oid " " WHERE indisvalid "
" AND c.reltoastrelid != %u", InvalidOid)); " AND indrelid IN (SELECT reltoastrelid "
PQclear(executeQueryOrDie(conn, " FROM (SELECT reloid FROM regular_heap "
"INSERT INTO info_rels " " UNION ALL "
"SELECT indexrelid " " SELECT reloid FROM toast_heap) all_heap "
"FROM pg_index " " JOIN pg_catalog.pg_class c "
"WHERE indisvalid " " ON all_heap.reloid = c.oid "
" AND indrelid IN (SELECT reltoastrelid " " AND c.reltoastrelid != %u)) "
" FROM info_rels i " /* get all rels */
" JOIN pg_catalog.pg_class c " "SELECT c.oid, n.nspname, c.relname, "
" ON i.reloid = c.oid " " c.relfilenode, c.reltablespace, %s "
" AND c.reltoastrelid != %u)", "FROM (SELECT reloid FROM regular_heap "
InvalidOid)); " UNION ALL "
" SELECT reloid FROM toast_heap "
snprintf(query, sizeof(query), " UNION ALL "
"SELECT c.oid, n.nspname, c.relname, " " SELECT reloid FROM all_index) all_rels "
" c.relfilenode, c.reltablespace, %s " " JOIN pg_catalog.pg_class c "
"FROM info_rels i JOIN pg_catalog.pg_class c " " ON all_rels.reloid = c.oid "
" ON i.reloid = c.oid " " JOIN pg_catalog.pg_namespace n "
" JOIN pg_catalog.pg_namespace n " " ON c.relnamespace = n.oid "
" ON c.relnamespace = n.oid " " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
" LEFT OUTER JOIN pg_catalog.pg_tablespace t " " ON c.reltablespace = t.oid "
" ON c.reltablespace = t.oid "
/* we preserve pg_class.oid so we sort by it to match old/new */ /* we preserve pg_class.oid so we sort by it to match old/new */
"ORDER BY 1;", "ORDER BY 1;",
FirstNormalObjectId,
/* does pg_largeobject_metadata need to be migrated? */
(GET_MAJOR_VERSION(old_cluster.major_version) <= 804) ?
"" : ", 'pg_largeobject_metadata', 'pg_largeobject_metadata_oid_index'",
InvalidOid, InvalidOid,
/* 9.2 removed the spclocation column */ /* 9.2 removed the spclocation column */
(GET_MAJOR_VERSION(cluster->major_version) <= 901) ? (GET_MAJOR_VERSION(cluster->major_version) <= 901) ?
"t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid) AS spclocation"); "t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid) AS spclocation");
res = executeQueryOrDie(conn, "%s", query); res = executeQueryOrDie(conn, "%s", query);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment