diff --git a/backup/queries_functions.go b/backup/queries_functions.go index e95d35a65..f3e86931d 100644 --- a/backup/queries_functions.go +++ b/backup/queries_functions.go @@ -856,7 +856,7 @@ func GetExtensions(connectionPool *dbconn.DBConn) []Extension { UNION DISTINCT SELECT e.oid, level + 1, e.extname, e.extnamespace FROM cte JOIN e ON cte.oid = e.objid - ) SELECT oid, quote_ident(extname) name, quote_ident(nspname) schema + ) SELECT cte.oid, quote_ident(extname) name, quote_ident(nspname) schema FROM cte JOIN pg_catalog.pg_namespace n ON extnamespace = n.oid GROUP BY 1, 2, 3 ORDER BY max(level) DESC` } diff --git a/backup/queries_relations.go b/backup/queries_relations.go index 45050b35c..519d14899 100644 --- a/backup/queries_relations.go +++ b/backup/queries_relations.go @@ -126,10 +126,27 @@ func getUserTableRelations(connectionPool *dbconn.DBConn) []Relation { WHERE e.reloid IS NULL)` } + prt := `LEFT JOIN ( + SELECT + p.parrelid, + sum(pc.relpages) AS pages + FROM pg_partition_rule AS pr + JOIN pg_partition AS p ON pr.paroid = p.oid + JOIN pg_class AS pc ON pr.parchildrelid = pc.oid + GROUP BY p.parrelid + ) AS prt ON prt.parrelid = c.oid` // In GPDB 7+, root partitions are marked as relkind 'p'. relkindFilter := `'r'` if connectionPool.Version.AtLeast("7") { relkindFilter = `'r', 'p'` + prt = `LEFT JOIN ( + SELECT + pg_partition_root(c.oid) oid, + sum(c.relpages) AS pages + FROM pg_class c + WHERE c.relispartition = true OR c.relkind = 'p' + GROUP BY 1 + ) AS prt ON prt.oid = c.oid` } query := fmt.Sprintf(` @@ -141,21 +158,13 @@ func getUserTableRelations(connectionPool *dbconn.DBConn) []Relation { coalesce(prt.pages, c.relpages) AS pages FROM pg_class c JOIN pg_namespace n ON c.relnamespace = n.oid - LEFT JOIN ( - SELECT - p.parrelid, - sum(pc.relpages) AS pages - FROM pg_partition_rule AS pr - JOIN pg_partition AS p ON pr.paroid = p.oid - JOIN pg_class AS pc ON pr.parchildrelid = pc.oid - GROUP BY p.parrelid - ) AS prt ON prt.parrelid = c.oid + %s WHERE %s %s AND relkind IN (%s) AND %s ) res - ORDER BY pages DESC, oid`, + ORDER BY pages DESC, oid`, prt, relationAndSchemaFilterClause(), childPartitionFilter, relkindFilter, ExtensionFilterClause("c")) results := make([]Relation, 0) @@ -166,10 +175,27 @@ func getUserTableRelations(connectionPool *dbconn.DBConn) []Relation { } func getUserTableRelationsWithIncludeFiltering(connectionPool *dbconn.DBConn, includedRelationsQuoted []string) []Relation { + prt := `LEFT JOIN ( + SELECT + p.parrelid, + sum(pc.relpages) AS pages + FROM pg_partition_rule AS pr + JOIN pg_partition AS p ON pr.paroid = p.oid + JOIN pg_class AS pc ON pr.parchildrelid = pc.oid + GROUP BY p.parrelid + ) AS prt ON prt.parrelid = c.oid` // In GPDB 7+, root partitions are marked as relkind 'p'. relkindFilter := `'r'` if connectionPool.Version.AtLeast("7") { relkindFilter = `'r', 'p'` + prt = `LEFT JOIN ( + SELECT + pg_partition_root(c.oid) oid, + sum(c.relpages) AS pages + FROM pg_class c + WHERE c.relispartition = true OR c.relkind = 'p' + GROUP BY 1 + ) AS prt ON prt.oid = c.oid` } includeOids := getOidsFromRelationList(connectionPool, includedRelationsQuoted) @@ -183,19 +209,11 @@ func getUserTableRelationsWithIncludeFiltering(connectionPool *dbconn.DBConn, in coalesce(prt.pages, c.relpages) AS pages FROM pg_class c JOIN pg_namespace n ON c.relnamespace = n.oid - LEFT JOIN ( - SELECT - p.parrelid, - sum(pc.relpages) AS pages - FROM pg_partition_rule AS pr - JOIN pg_partition AS p ON pr.paroid = p.oid - JOIN pg_class AS pc ON pr.parchildrelid = pc.oid - GROUP BY p.parrelid - ) AS prt ON prt.parrelid = c.oid + %s WHERE c.oid IN (%s) AND relkind IN (%s) ) res - ORDER BY pages DESC, oid`, oidStr, relkindFilter) + ORDER BY pages DESC, oid`, prt, oidStr, relkindFilter) results := make([]Relation, 0) err := connectionPool.Select(&results, query)