mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-03 09:13:20 +03:00 
			
		
		
		
	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.
This commit is contained in:
		@@ -320,13 +320,14 @@ 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 ( "
 | 
				
			||||||
 | 
								"	SELECT c.oid "
 | 
				
			||||||
 | 
								"	FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
 | 
				
			||||||
			"		   ON c.relnamespace = n.oid "
 | 
								"		   ON c.relnamespace = n.oid "
 | 
				
			||||||
			 "LEFT OUTER JOIN pg_catalog.pg_index i "
 | 
								"	LEFT OUTER JOIN pg_catalog.pg_index i "
 | 
				
			||||||
			"		   ON c.oid = i.indexrelid "
 | 
								"		   ON c.oid = i.indexrelid "
 | 
				
			||||||
			 "WHERE relkind IN ('r', 'm', 'i', 'S') AND "
 | 
								"	WHERE relkind IN ('r', 'm', 'i', 'S') AND "
 | 
				
			||||||
 | 
					 | 
				
			||||||
		/*
 | 
							/*
 | 
				
			||||||
		 * pg_dump only dumps valid indexes;  testing indisready is necessary in
 | 
							 * pg_dump only dumps valid indexes;  testing indisready is necessary in
 | 
				
			||||||
		 * 9.2, and harmless in earlier/later versions.
 | 
							 * 9.2, and harmless in earlier/later versions.
 | 
				
			||||||
@@ -339,49 +340,52 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
 | 
				
			|||||||
		/* skip pg_toast because toast index have relkind == 'i', not 't' */
 | 
							/* skip pg_toast because toast index have relkind == 'i', not 't' */
 | 
				
			||||||
			"	    n.nspname NOT IN ('pg_catalog', 'information_schema', "
 | 
								"	    n.nspname NOT IN ('pg_catalog', 'information_schema', "
 | 
				
			||||||
			"							'binary_upgrade', 'pg_toast') AND "
 | 
								"							'binary_upgrade', 'pg_toast') AND "
 | 
				
			||||||
			 "	  c.oid >= %u) "
 | 
								"		  c.oid >= %u) OR "
 | 
				
			||||||
			 "  OR (n.nspname = 'pg_catalog' AND "
 | 
								"	  (n.nspname = 'pg_catalog' AND "
 | 
				
			||||||
	"    relname IN ('pg_largeobject', 'pg_largeobject_loid_pn_index'%s) ));",
 | 
						"    relname IN ('pg_largeobject', 'pg_largeobject_loid_pn_index'%s) ))), "
 | 
				
			||||||
			 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'");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	PQclear(executeQueryOrDie(conn, "%s", query));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		/*
 | 
							/*
 | 
				
			||||||
	 * Get TOAST tables and indexes;  we have to gather the TOAST tables in
 | 
							 * We have to gather the TOAST tables in later steps because we
 | 
				
			||||||
	 * later steps because we can't schema-qualify TOAST tables.
 | 
							 * can't schema-qualify TOAST tables.
 | 
				
			||||||
		 */
 | 
							 */
 | 
				
			||||||
	PQclear(executeQueryOrDie(conn,
 | 
					 		 /* get TOAST heap */
 | 
				
			||||||
							  "INSERT INTO info_rels "
 | 
								"	toast_heap (reloid) AS ( "
 | 
				
			||||||
							  "SELECT reltoastrelid "
 | 
								"	SELECT reltoastrelid "
 | 
				
			||||||
							  "FROM info_rels i JOIN pg_catalog.pg_class c "
 | 
								"	FROM regular_heap JOIN pg_catalog.pg_class c "
 | 
				
			||||||
							  "		ON i.reloid = c.oid "
 | 
								"		ON regular_heap.reloid = c.oid "
 | 
				
			||||||
						  "		AND c.reltoastrelid != %u", InvalidOid));
 | 
								"		AND c.reltoastrelid != %u), "
 | 
				
			||||||
	PQclear(executeQueryOrDie(conn,
 | 
							 /* get indexes on regular and TOAST heap */
 | 
				
			||||||
							  "INSERT INTO info_rels "
 | 
								"	all_index (reloid) AS ( "
 | 
				
			||||||
							  "SELECT indexrelid "
 | 
								"	SELECT indexrelid "
 | 
				
			||||||
							  "FROM pg_index "
 | 
								"	FROM pg_index "
 | 
				
			||||||
							  "WHERE indisvalid "
 | 
								"	WHERE indisvalid "
 | 
				
			||||||
			"    AND indrelid IN (SELECT reltoastrelid "
 | 
								"    AND indrelid IN (SELECT reltoastrelid "
 | 
				
			||||||
							  "        FROM info_rels i "
 | 
								"        FROM (SELECT reloid FROM regular_heap "
 | 
				
			||||||
 | 
								"			   UNION ALL "
 | 
				
			||||||
 | 
								"			   SELECT reloid FROM toast_heap) all_heap "
 | 
				
			||||||
			"            JOIN pg_catalog.pg_class c "
 | 
								"            JOIN pg_catalog.pg_class c "
 | 
				
			||||||
							  "            ON i.reloid = c.oid "
 | 
								"            ON all_heap.reloid = c.oid "
 | 
				
			||||||
							  "            AND c.reltoastrelid != %u)",
 | 
								"            AND c.reltoastrelid != %u)) "
 | 
				
			||||||
							  InvalidOid));
 | 
							/* get all rels */
 | 
				
			||||||
 | 
					 | 
				
			||||||
	snprintf(query, sizeof(query),
 | 
					 | 
				
			||||||
			"SELECT c.oid, n.nspname, c.relname, "
 | 
								"SELECT c.oid, n.nspname, c.relname, "
 | 
				
			||||||
			"	c.relfilenode, c.reltablespace, %s "
 | 
								"	c.relfilenode, c.reltablespace, %s "
 | 
				
			||||||
			 "FROM info_rels i JOIN pg_catalog.pg_class c "
 | 
								"FROM (SELECT reloid FROM regular_heap "
 | 
				
			||||||
			 "		ON i.reloid = c.oid "
 | 
								"	   UNION ALL "
 | 
				
			||||||
 | 
								"	   SELECT reloid FROM toast_heap  "
 | 
				
			||||||
 | 
								"	   UNION ALL "
 | 
				
			||||||
 | 
								"	   SELECT reloid FROM all_index) all_rels "
 | 
				
			||||||
 | 
								"  JOIN pg_catalog.pg_class c "
 | 
				
			||||||
 | 
								"		ON all_rels.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");
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user