Skip to content

Commit

Permalink
Implement gpdb 7 support for gpbackup ordering patch (#56)
Browse files Browse the repository at this point in the history
gpbackup used pg_partition and pg_partition_rule to select partitions for gpdb6
and prior, but gpdb7 ditched them. This patch adds a new query for gpdb7 which
uses pg_partition_root. Also, gpdb7 complains on the query from GetExtension
saying that it's ambiguous. This patch also fixes it

Ticket: ADBDEV-4534
  • Loading branch information
Dennis Kovalenko authored Feb 2, 2024
1 parent 038dc2d commit 34fce22
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion backup/queries_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
}
Expand Down
58 changes: 38 additions & 20 deletions backup/queries_relations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 34fce22

Please sign in to comment.