From ce3ebb3a1ec85f1b99d2e34f72b91fbd2c0c46b8 Mon Sep 17 00:00:00 2001 From: David Rowley Date: Fri, 27 Nov 2026 16:33:04 +1300 Subject: [PATCH v2] Speed up pg_upgrade get_rel_infos_query() processing We've had some reports that this query could take an excessively long time to process when upgrading instances with a large number of tables. What seems to be happening is that the row estimate for the pg_namespace scan comes back as 1 row which results in compounding Nested Loop joins. Autovacuum doesn't run during pg_upgrade, and it would take many dozens of CREATE SCHEMA commands to create enough namespaces to push pg_namespace into growing an extra page for table_block_relation_estimate_size() to work out a more realistic reltuples estimate based on the current relation size. Here we rewrite the query to completely remove the joins to pg_namespace to avoid this issue by casting relnamespace to regnamespace and stripping off any quotes. Additionally, perform some more tuning on the query and have the initial CTE avoid joining to pg_class again to fetch information that it can get from selecting some additional columns. Move the OR condition for pg_largeobject and pg_largeobject_metadata and switch to using a UNION ALL to allow pg_class_oid_index to be used by looking them up via ::regclass. The join to pg_tablespace is also removed. This wasn't strictly needed and only served to produce a NULL for rows with reltablespace = 0. We can get the NULL with a CASE statement instead. Author: David Rowley Discussion: https://postgr.es/m/CAApHDvoxFXxZC7L3J8y5k2FNrXP4sBYo7pE-q-5JnEOEu9-BEg@mail.gmail.com --- src/bin/pg_upgrade/info.c | 115 +++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 59 deletions(-) diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c index 6d8f70c8c7d..5dfa66ca543 100644 --- a/src/bin/pg_upgrade/info.c +++ b/src/bin/pg_upgrade/info.c @@ -491,11 +491,17 @@ get_rel_infos_query(void) initPQExpBuffer(&query); /* - * Create a CTE that collects OIDs of regular user tables and matviews, + * Create a CTE that collects details of regular user tables and matviews, * but excluding toast tables and indexes. We assume that relations with * OIDs >= FirstNormalObjectId belong to the user. (That's probably * redundant with the namespace-name exclusions, but let's be safe.) * + * We take care here to avoid joining to pg_namespace. Outdated stats on + * that table have been known to cause issues, and autovacuum doesn't + * update these during pg_upgrade. Instead, we cast relnamespace to + * regnamespace then text, taking care to remove leading/trailing quotes + * and to unescape any quotes from the namespace name. + * * pg_largeobject contains user data that does not appear in pg_dump * output, so we have to copy that system table. It's easiest to do that * by treating it as a user table. We can do the same for @@ -504,73 +510,64 @@ get_rel_infos_query(void) * by pg_largeobject_metadata.lomacl) changed its storage format in v16. */ appendPQExpBuffer(&query, - "WITH regular_heap (reloid, indtable, toastheap) AS ( " - " SELECT c.oid, 0::oid, 0::oid " - " FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n " - " ON c.relnamespace = n.oid " - " WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", " - CppAsString2(RELKIND_MATVIEW) "%s) AND " + "WITH rt (reloid, toastheap, relname, reltablespace, relfilenode, nspname) AS (" + " SELECT * FROM (" + " SELECT" + " oid, reltoastrelid, relname, reltablespace, relfilenode," + " replace(regexp_replace(relnamespace::regnamespace::text, '^\"(.*)\"$', '\\1'), '\"\"', '\"') AS nspname" + " FROM pg_catalog.pg_class" + " WHERE oid >= %u::pg_catalog.oid" + " AND relkind IN(" CppAsString2(RELKIND_RELATION) ", " CppAsString2(RELKIND_MATVIEW) "%s)" + " ) c" /* exclude possible orphaned temp tables */ - " ((n.nspname !~ '^pg_temp_' AND " - " n.nspname !~ '^pg_toast_temp_' AND " - " n.nspname NOT IN ('pg_catalog', 'information_schema', " - " 'binary_upgrade', 'pg_toast') AND " - " c.oid >= %u::pg_catalog.oid) OR " - " (n.nspname = 'pg_catalog' AND " - " relname IN ('pg_largeobject'%s) ))), ", + " WHERE nspname !~'^pg_temp_' AND nspname !~'^pg_toast_temp_'" + " AND nspname NOT IN('pg_catalog', 'information_schema', 'binary_upgrade', 'pg_toast')" + " UNION ALL " + " SELECT oid, reltoastrelid, relname, reltablespace, relfilenode, 'pg_catalog'" + " FROM pg_catalog.pg_class" + " WHERE oid IN('pg_catalog.pg_largeobject'::regclass%s)" + ") ", FirstNormalObjectId, (user_opts.transfer_mode == TRANSFER_MODE_SWAP) ? ", " CppAsString2(RELKIND_SEQUENCE) : "", - FirstNormalObjectId, (GET_MAJOR_VERSION(old_cluster.major_version) >= 1600) ? - ", 'pg_largeobject_metadata'" : ""); - - /* - * Add a CTE that collects OIDs of toast tables belonging to the tables - * selected by the regular_heap CTE. (We have to do this separately - * because the namespace-name rules above don't work for toast tables.) - */ - appendPQExpBufferStr(&query, - " toast_heap (reloid, indtable, toastheap) AS ( " - " SELECT c.reltoastrelid, 0::oid, c.oid " - " FROM regular_heap JOIN pg_catalog.pg_class c " - " ON regular_heap.reloid = c.oid " - " WHERE c.reltoastrelid != 0), "); - - /* - * Add a CTE that collects OIDs of all valid indexes on the previously - * selected tables. We can ignore invalid indexes since pg_dump does. - * Testing indisready is necessary in 9.2, and harmless in earlier/later - * versions. - */ - appendPQExpBufferStr(&query, - " all_index (reloid, indtable, toastheap) AS ( " - " SELECT indexrelid, indrelid, 0::oid " - " FROM pg_catalog.pg_index " - " WHERE indisvalid AND indisready " - " AND indrelid IN " - " (SELECT reloid FROM regular_heap " - " UNION ALL " - " SELECT reloid FROM toast_heap)) "); + ", 'pg_catalog.pg_largeobject_metadata'::regclass" : ""); /* * And now we can write the query that retrieves the data we want for each - * heap and index relation. Make sure result is sorted by OID. + * heap table and its toast table (if any) and all indexes for each of + * those. Make sure result is sorted by OID. We can ignore invalid + * indexes since pg_dump does. Testing indisready is necessary in 9.2, + * and harmless in earlier/later versions. As above, we're careful not to + * join to pg_namespace. */ appendPQExpBufferStr(&query, - "SELECT all_rels.*, n.nspname, c.relname, " - " c.relfilenode, c.reltablespace, " - " pg_catalog.pg_tablespace_location(t.oid) AS spclocation " - "FROM (SELECT * FROM regular_heap " - " UNION ALL " - " SELECT * FROM toast_heap " - " UNION ALL " - " SELECT * FROM all_index) all_rels " - " JOIN pg_catalog.pg_class c " - " ON all_rels.reloid = c.oid " - " JOIN pg_catalog.pg_namespace n " - " ON c.relnamespace = n.oid " - " LEFT OUTER JOIN pg_catalog.pg_tablespace t " - " ON c.reltablespace = t.oid " + "SELECT" + " rt.reloid, 0::oid AS indtable, 0::oid AS toastheap, rt.nspname, rt.relname, rt.relfilenode, rt.reltablespace, " + " CASE WHEN rt.reltablespace <> 0 THEN pg_catalog.pg_tablespace_location(rt.reltablespace) END AS spclocation " + "FROM rt " + "UNION ALL " + /* get details for all the toast tables for each of the above tables */ + "SELECT" + " rt.toastheap, 0::oid, rt.reloid," + " replace(regexp_replace(c.relnamespace::regnamespace::text, '^\"(.*)\"$', '\\1'), '\"\"', '\"')," + " c.relname, c.relfilenode, c.reltablespace, " + " CASE WHEN c.reltablespace <> 0 THEN pg_catalog.pg_tablespace_location(c.reltablespace) END " + "FROM rt " + "INNER JOIN pg_catalog.pg_class c ON rt.toastheap = c.oid " + "WHERE rt.toastheap <> 0 " + "UNION ALL " + /* and index details for the above regular and toast tables */ + "SELECT" + " i.indexrelid, i.indrelid, 0::oid," + " replace(regexp_replace(c.relnamespace::regnamespace::text, '^\"(.*)\"$', '\\1'), '\"\"', '\"')," + " c.relname, c.relfilenode, c.reltablespace, " + " CASE WHEN c.reltablespace <> 0 THEN pg_catalog.pg_tablespace_location(c.reltablespace) END " + "FROM pg_catalog.pg_index i " + "INNER JOIN (" + " SELECT reloid FROM rt UNION ALL SELECT toastheap FROM rt WHERE toastheap <> 0" + ") rtt ON i.indrelid = rtt.reloid " + "INNER JOIN pg_catalog.pg_class c ON i.indexrelid = c.oid " + "WHERE i.indisvalid AND i.indisready " "ORDER BY 1"); return query.data; -- 2.53.0