Skip to content

Commit 34fce22

Browse files
author
Dennis Kovalenko
authored
Implement gpdb 7 support for gpbackup ordering patch (#56)
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
1 parent 038dc2d commit 34fce22

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

backup/queries_functions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ func GetExtensions(connectionPool *dbconn.DBConn) []Extension {
856856
UNION DISTINCT
857857
SELECT e.oid, level + 1, e.extname, e.extnamespace
858858
FROM cte JOIN e ON cte.oid = e.objid
859-
) SELECT oid, quote_ident(extname) name, quote_ident(nspname) schema
859+
) SELECT cte.oid, quote_ident(extname) name, quote_ident(nspname) schema
860860
FROM cte JOIN pg_catalog.pg_namespace n ON extnamespace = n.oid
861861
GROUP BY 1, 2, 3 ORDER BY max(level) DESC`
862862
}

backup/queries_relations.go

+38-20
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,27 @@ func getUserTableRelations(connectionPool *dbconn.DBConn) []Relation {
126126
WHERE e.reloid IS NULL)`
127127
}
128128

129+
prt := `LEFT JOIN (
130+
SELECT
131+
p.parrelid,
132+
sum(pc.relpages) AS pages
133+
FROM pg_partition_rule AS pr
134+
JOIN pg_partition AS p ON pr.paroid = p.oid
135+
JOIN pg_class AS pc ON pr.parchildrelid = pc.oid
136+
GROUP BY p.parrelid
137+
) AS prt ON prt.parrelid = c.oid`
129138
// In GPDB 7+, root partitions are marked as relkind 'p'.
130139
relkindFilter := `'r'`
131140
if connectionPool.Version.AtLeast("7") {
132141
relkindFilter = `'r', 'p'`
142+
prt = `LEFT JOIN (
143+
SELECT
144+
pg_partition_root(c.oid) oid,
145+
sum(c.relpages) AS pages
146+
FROM pg_class c
147+
WHERE c.relispartition = true OR c.relkind = 'p'
148+
GROUP BY 1
149+
) AS prt ON prt.oid = c.oid`
133150
}
134151

135152
query := fmt.Sprintf(`
@@ -141,21 +158,13 @@ func getUserTableRelations(connectionPool *dbconn.DBConn) []Relation {
141158
coalesce(prt.pages, c.relpages) AS pages
142159
FROM pg_class c
143160
JOIN pg_namespace n ON c.relnamespace = n.oid
144-
LEFT JOIN (
145-
SELECT
146-
p.parrelid,
147-
sum(pc.relpages) AS pages
148-
FROM pg_partition_rule AS pr
149-
JOIN pg_partition AS p ON pr.paroid = p.oid
150-
JOIN pg_class AS pc ON pr.parchildrelid = pc.oid
151-
GROUP BY p.parrelid
152-
) AS prt ON prt.parrelid = c.oid
161+
%s
153162
WHERE %s
154163
%s
155164
AND relkind IN (%s)
156165
AND %s
157166
) res
158-
ORDER BY pages DESC, oid`,
167+
ORDER BY pages DESC, oid`, prt,
159168
relationAndSchemaFilterClause(), childPartitionFilter, relkindFilter, ExtensionFilterClause("c"))
160169

161170
results := make([]Relation, 0)
@@ -166,10 +175,27 @@ func getUserTableRelations(connectionPool *dbconn.DBConn) []Relation {
166175
}
167176

168177
func getUserTableRelationsWithIncludeFiltering(connectionPool *dbconn.DBConn, includedRelationsQuoted []string) []Relation {
178+
prt := `LEFT JOIN (
179+
SELECT
180+
p.parrelid,
181+
sum(pc.relpages) AS pages
182+
FROM pg_partition_rule AS pr
183+
JOIN pg_partition AS p ON pr.paroid = p.oid
184+
JOIN pg_class AS pc ON pr.parchildrelid = pc.oid
185+
GROUP BY p.parrelid
186+
) AS prt ON prt.parrelid = c.oid`
169187
// In GPDB 7+, root partitions are marked as relkind 'p'.
170188
relkindFilter := `'r'`
171189
if connectionPool.Version.AtLeast("7") {
172190
relkindFilter = `'r', 'p'`
191+
prt = `LEFT JOIN (
192+
SELECT
193+
pg_partition_root(c.oid) oid,
194+
sum(c.relpages) AS pages
195+
FROM pg_class c
196+
WHERE c.relispartition = true OR c.relkind = 'p'
197+
GROUP BY 1
198+
) AS prt ON prt.oid = c.oid`
173199
}
174200

175201
includeOids := getOidsFromRelationList(connectionPool, includedRelationsQuoted)
@@ -183,19 +209,11 @@ func getUserTableRelationsWithIncludeFiltering(connectionPool *dbconn.DBConn, in
183209
coalesce(prt.pages, c.relpages) AS pages
184210
FROM pg_class c
185211
JOIN pg_namespace n ON c.relnamespace = n.oid
186-
LEFT JOIN (
187-
SELECT
188-
p.parrelid,
189-
sum(pc.relpages) AS pages
190-
FROM pg_partition_rule AS pr
191-
JOIN pg_partition AS p ON pr.paroid = p.oid
192-
JOIN pg_class AS pc ON pr.parchildrelid = pc.oid
193-
GROUP BY p.parrelid
194-
) AS prt ON prt.parrelid = c.oid
212+
%s
195213
WHERE c.oid IN (%s)
196214
AND relkind IN (%s)
197215
) res
198-
ORDER BY pages DESC, oid`, oidStr, relkindFilter)
216+
ORDER BY pages DESC, oid`, prt, oidStr, relkindFilter)
199217

200218
results := make([]Relation, 0)
201219
err := connectionPool.Select(&results, query)

0 commit comments

Comments
 (0)