@@ -126,10 +126,27 @@ func getUserTableRelations(connectionPool *dbconn.DBConn) []Relation {
126
126
WHERE e.reloid IS NULL)`
127
127
}
128
128
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`
129
138
// In GPDB 7+, root partitions are marked as relkind 'p'.
130
139
relkindFilter := `'r'`
131
140
if connectionPool .Version .AtLeast ("7" ) {
132
141
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`
133
150
}
134
151
135
152
query := fmt .Sprintf (`
@@ -141,21 +158,13 @@ func getUserTableRelations(connectionPool *dbconn.DBConn) []Relation {
141
158
coalesce(prt.pages, c.relpages) AS pages
142
159
FROM pg_class c
143
160
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
153
162
WHERE %s
154
163
%s
155
164
AND relkind IN (%s)
156
165
AND %s
157
166
) res
158
- ORDER BY pages DESC, oid` ,
167
+ ORDER BY pages DESC, oid` , prt ,
159
168
relationAndSchemaFilterClause (), childPartitionFilter , relkindFilter , ExtensionFilterClause ("c" ))
160
169
161
170
results := make ([]Relation , 0 )
@@ -166,10 +175,27 @@ func getUserTableRelations(connectionPool *dbconn.DBConn) []Relation {
166
175
}
167
176
168
177
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`
169
187
// In GPDB 7+, root partitions are marked as relkind 'p'.
170
188
relkindFilter := `'r'`
171
189
if connectionPool .Version .AtLeast ("7" ) {
172
190
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`
173
199
}
174
200
175
201
includeOids := getOidsFromRelationList (connectionPool , includedRelationsQuoted )
@@ -183,19 +209,11 @@ func getUserTableRelationsWithIncludeFiltering(connectionPool *dbconn.DBConn, in
183
209
coalesce(prt.pages, c.relpages) AS pages
184
210
FROM pg_class c
185
211
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
195
213
WHERE c.oid IN (%s)
196
214
AND relkind IN (%s)
197
215
) res
198
- ORDER BY pages DESC, oid` , oidStr , relkindFilter )
216
+ ORDER BY pages DESC, oid` , prt , oidStr , relkindFilter )
199
217
200
218
results := make ([]Relation , 0 )
201
219
err := connectionPool .Select (& results , query )
0 commit comments