/
queries_types.go
547 lines (486 loc) · 15.5 KB
/
queries_types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package backup
/*
* This file contains structs and functions related to executing specific
* queries to gather metadata for the objects handled in predata_types.go.
*/
import (
"fmt"
"github.com/greenplum-db/gp-common-go-libs/dbconn"
"github.com/greenplum-db/gp-common-go-libs/gplog"
"github.com/greenplum-db/gpbackup/toc"
"github.com/greenplum-db/gpbackup/utils"
)
func GetTypeMetadataEntry(schema string, name string) (string, toc.MetadataEntry) {
return "predata",
toc.MetadataEntry{
Schema: schema,
Name: name,
ObjectType: toc.OBJ_TYPE,
ReferenceObject: "",
StartByte: 0,
EndByte: 0,
}
}
type BaseType struct {
Oid uint32
Schema string
Name string
Input string
Output string
Receive string
Send string
ModIn string
ModOut string
InternalLength int
IsPassedByValue bool
Alignment string
Storage string
DefaultVal string
Element string
Category string
Preferred bool
Delimiter string
StorageOptions string
Collatable bool
Collation string
}
func (t BaseType) GetMetadataEntry() (string, toc.MetadataEntry) {
return GetTypeMetadataEntry(t.Schema, t.Name)
}
func (t BaseType) GetUniqueID() UniqueID {
return UniqueID{ClassID: PG_TYPE_OID, Oid: t.Oid}
}
func (t BaseType) FQN() string {
return utils.MakeFQN(t.Schema, t.Name)
}
func GetBaseTypes(connectionPool *dbconn.DBConn) []BaseType {
gplog.Verbose("Getting base types")
version5query := fmt.Sprintf(`
SELECT t.oid,
quote_ident(n.nspname) AS schema,
quote_ident(t.typname) AS name,
t.typinput AS input,
t.typoutput AS output,
CASE WHEN t.typreceive = '-'::regproc THEN '' ELSE t.typreceive::regproc::text END AS receive,
CASE WHEN t.typsend = '-'::regproc THEN '' ELSE t.typsend::regproc::text END AS send,
CASE WHEN t.typmodin = '-'::regproc THEN '' ELSE t.typmodin::regproc::text END AS modin,
CASE WHEN t.typmodout = '-'::regproc THEN '' ELSE t.typmodout::regproc::text END AS modout,
t.typlen AS internallength,
t.typbyval AS ispassedbyvalue,
CASE WHEN t.typalign = '-' THEN '' ELSE t.typalign END AS alignment,
t.typstorage AS storage,
coalesce(t.typdefault, '') AS defaultval,
CASE WHEN t.typelem != 0::regproc THEN pg_catalog.format_type(t.typelem, NULL) ELSE '' END AS element,
'U' AS category,
t.typdelim AS delimiter,
coalesce(array_to_string(e.typoptions, ', '), '') AS storageoptions
FROM pg_type t
JOIN pg_namespace n ON t.typnamespace = n.oid
LEFT JOIN pg_type_encoding e ON t.oid = e.typid
/*
* Identify if this is an automatically generated array type and exclude it if so.
* In GPDB 5 and 6 we use the typearray field to identify these array types.
*/
LEFT JOIN pg_type ut ON t.oid = ut.typarray
WHERE %s
AND t.typtype = 'b'
AND ut.oid IS NULL
AND %s`, SchemaFilterClause("n"), ExtensionFilterClause("t"))
atLeast6Query := fmt.Sprintf(`
SELECT t.oid,
quote_ident(n.nspname) AS schema,
quote_ident(t.typname) AS name,
t.typinput AS input,
t.typoutput AS output,
CASE WHEN t.typreceive = '-'::regproc THEN '' ELSE t.typreceive::regproc::text END AS receive,
CASE WHEN t.typsend = '-'::regproc THEN '' ELSE t.typsend::regproc::text END AS send,
CASE WHEN t.typmodin = '-'::regproc THEN '' ELSE t.typmodin::regproc::text END AS modin,
CASE WHEN t.typmodout = '-'::regproc THEN '' ELSE t.typmodout::regproc::text END AS modout,
t.typlen AS internallength,
t.typbyval AS ispassedbyvalue,
CASE WHEN t.typalign = '-' THEN '' ELSE t.typalign END AS alignment,
t.typstorage AS storage,
coalesce(t.typdefault, '') AS defaultval,
CASE WHEN t.typelem != 0::regproc THEN pg_catalog.format_type(t.typelem, NULL) ELSE '' END AS element,
t.typcategory AS category,
t.typispreferred AS preferred,
t.typdelim AS delimiter,
(t.typcollation <> 0) AS collatable,
coalesce(array_to_string(typoptions, ', '), '') AS storageoptions
FROM pg_type t
JOIN pg_namespace n ON t.typnamespace = n.oid
LEFT JOIN pg_type_encoding e ON t.oid = e.typid
/*
* Identify if this is an automatically generated array type and exclude it if so.
* In GPDB 5 and 6 we use the typearray field to identify these array types.
*/
LEFT JOIN pg_type ut ON t.oid = ut.typarray
WHERE %s
AND t.typtype = 'b'
AND ut.oid IS NULL
AND %s`, SchemaFilterClause("n"), ExtensionFilterClause("t"))
results := make([]BaseType, 0)
var err error
if connectionPool.Version.Is("5") {
err = connectionPool.Select(&results, version5query)
} else {
err = connectionPool.Select(&results, atLeast6Query)
}
gplog.FatalOnError(err)
return results
}
type CompositeType struct {
Oid uint32
Schema string
Name string
Attributes []Attribute
}
func (t CompositeType) GetMetadataEntry() (string, toc.MetadataEntry) {
return GetTypeMetadataEntry(t.Schema, t.Name)
}
func (t CompositeType) GetUniqueID() UniqueID {
return UniqueID{ClassID: PG_TYPE_OID, Oid: t.Oid}
}
func (t CompositeType) FQN() string {
return utils.MakeFQN(t.Schema, t.Name)
}
func GetCompositeTypes(connectionPool *dbconn.DBConn) []CompositeType {
gplog.Verbose("Getting composite types")
query := fmt.Sprintf(`
SELECT t.oid,
quote_ident(n.nspname) AS schema,
quote_ident(t.typname) AS name
FROM pg_type t
JOIN pg_namespace n ON t.typnamespace = n.oid
/*
* We join with pg_class to check if a type is truly a composite type
* (relkind='c') or implicitly generated from a relation
*/
JOIN pg_class c ON t.typrelid = c.oid
WHERE %s
AND t.typtype = 'c'
AND c.relkind = 'c'
AND %s`, SchemaFilterClause("n"), ExtensionFilterClause("t"))
compTypes := make([]CompositeType, 0)
err := connectionPool.Select(&compTypes, query)
gplog.FatalOnError(err)
attributeMap := getCompositeTypeAttributes(connectionPool)
for i, compType := range compTypes {
compTypes[i].Attributes = attributeMap[compType.Oid]
}
return compTypes
}
type Attribute struct {
CompositeTypeOid uint32
Name string
Type string
Comment string
Collation string
}
func getCompositeTypeAttributes(connectionPool *dbconn.DBConn) map[uint32][]Attribute {
gplog.Verbose("Getting composite type attributes")
before6Query := `
SELECT t.oid AS compositetypeoid,
quote_ident(a.attname) AS name,
pg_catalog.format_type(a.atttypid, a.atttypmod) AS type,
coalesce(quote_literal(d.description),'') AS comment
FROM pg_type t
JOIN pg_class c ON t.typrelid = c.oid
JOIN pg_attribute a ON t.typrelid = a.attrelid
LEFT JOIN pg_description d ON (d.objoid = a.attrelid AND d.classoid = 'pg_class'::regclass AND d.objsubid = a.attnum)
WHERE t.typtype = 'c'
AND c.relkind = 'c'
ORDER BY t.oid, a.attnum`
atLeast6Query := `
SELECT t.oid AS compositetypeoid,
quote_ident(a.attname) AS name,
pg_catalog.format_type(a.atttypid, a.atttypmod) AS type,
coalesce(quote_literal(d.description),'') AS comment,
CASE
WHEN at.typcollation <> a.attcollation
THEN quote_ident(cn.nspname) || '.' || quote_ident(coll.collname) ELSE ''
END AS collation
FROM pg_type t
JOIN pg_class c ON t.typrelid = c.oid
JOIN pg_attribute a ON t.typrelid = a.attrelid
LEFT JOIN pg_description d ON (d.objoid = a.attrelid AND d.classoid = 'pg_class'::regclass AND d.objsubid = a.attnum)
LEFT JOIN pg_type at ON at.oid = a.atttypid
LEFT JOIN pg_collation coll ON a.attcollation = coll.oid
LEFT JOIN pg_namespace cn ON coll.collnamespace = cn.oid
WHERE t.typtype = 'c'
AND c.relkind = 'c'
AND a.attisdropped = false
ORDER BY t.oid, a.attnum`
query := ""
if connectionPool.Version.Before("6") {
query = before6Query
} else {
query = atLeast6Query
}
results := make([]Attribute, 0)
err := connectionPool.Select(&results, query)
gplog.FatalOnError(err)
attributeMap := make(map[uint32][]Attribute)
for _, att := range results {
attributeMap[att.CompositeTypeOid] = append(attributeMap[att.CompositeTypeOid], att)
}
return attributeMap
}
type Domain struct {
Oid uint32
Schema string
Name string
DefaultVal string
Collation string
BaseType string
NotNull bool
}
func (t Domain) GetMetadataEntry() (string, toc.MetadataEntry) {
return "predata",
toc.MetadataEntry{
Schema: t.Schema,
Name: t.Name,
ObjectType: toc.OBJ_DOMAIN,
ReferenceObject: "",
StartByte: 0,
EndByte: 0,
}
}
func (t Domain) GetUniqueID() UniqueID {
return UniqueID{ClassID: PG_TYPE_OID, Oid: t.Oid}
}
func (t Domain) FQN() string {
return utils.MakeFQN(t.Schema, t.Name)
}
func GetDomainTypes(connectionPool *dbconn.DBConn) []Domain {
gplog.Verbose("Getting domain types")
results := make([]Domain, 0)
before6query := fmt.Sprintf(`
SELECT t.oid,
quote_ident(n.nspname) AS schema,
quote_ident(t.typname) AS name,
coalesce(t.typdefault, '') AS defaultval,
format_type(t.typbasetype, t.typtypmod) AS basetype,
t.typnotnull AS notnull
FROM pg_type t
JOIN pg_namespace n ON t.typnamespace = n.oid
WHERE %s
AND t.typtype = 'd'
AND %s
ORDER BY n.nspname, t.typname`, SchemaFilterClause("n"), ExtensionFilterClause("t"))
atLeast6Query := fmt.Sprintf(`
SELECT t.oid,
quote_ident(n.nspname) AS schema,
quote_ident(t.typname) AS name,
coalesce(t.typdefault, '') AS defaultval,
CASE
WHEN t.typcollation <> u.typcollation
THEN quote_ident(cn.nspname) || '.' || quote_ident(c.collname)
ELSE ''
END AS collation,
format_type(t.typbasetype, t.typtypmod) AS basetype,
t.typnotnull AS notnull
FROM pg_type t
JOIN pg_namespace n ON t.typnamespace = n.oid
LEFT JOIN pg_type u ON t.typbasetype = u.oid
LEFT JOIN pg_collation c ON t.typcollation = c.oid
LEFT JOIN pg_namespace cn ON c.collnamespace = cn.oid
WHERE %s
AND t.typtype = 'd'
AND %s
ORDER BY n.nspname, t.typname`, SchemaFilterClause("n"), ExtensionFilterClause("t"))
query := ""
if connectionPool.Version.Before("6") {
query = before6query
} else {
query = atLeast6Query
}
err := connectionPool.Select(&results, query)
gplog.FatalOnError(err)
return results
}
type EnumType struct {
Oid uint32
Schema string
Name string
EnumLabels string
}
func (t EnumType) GetMetadataEntry() (string, toc.MetadataEntry) {
return GetTypeMetadataEntry(t.Schema, t.Name)
}
func (t EnumType) GetUniqueID() UniqueID {
return UniqueID{ClassID: PG_TYPE_OID, Oid: t.Oid}
}
func (t EnumType) FQN() string {
return utils.MakeFQN(t.Schema, t.Name)
}
func GetEnumTypes(connectionPool *dbconn.DBConn) []EnumType {
enumSortClause := "ORDER BY e.enumsortorder"
if connectionPool.Version.Is("5") {
enumSortClause = "ORDER BY e.oid"
}
query := fmt.Sprintf(`
SELECT t.oid,
quote_ident(n.nspname) AS schema,
quote_ident(t.typname) AS name,
enumlabels
FROM pg_type t
LEFT JOIN pg_namespace n ON t.typnamespace = n.oid
LEFT JOIN (SELECT e.enumtypid,string_agg(quote_literal(e.enumlabel), E',\n\t' %s) AS enumlabels
FROM pg_enum e GROUP BY enumtypid) e ON t.oid = e.enumtypid
WHERE %s
AND t.typtype = 'e'
AND %s
ORDER BY n.nspname, t.typname`, enumSortClause, SchemaFilterClause("n"), ExtensionFilterClause("t"))
results := make([]EnumType, 0)
err := connectionPool.Select(&results, query)
gplog.FatalOnError(err)
return results
}
type RangeType struct {
Oid uint32
Schema string
Name string
SubType string
Collation string
SubTypeOpClass string
Canonical string
SubTypeDiff string
}
func (t RangeType) GetMetadataEntry() (string, toc.MetadataEntry) {
return GetTypeMetadataEntry(t.Schema, t.Name)
}
func (t RangeType) GetUniqueID() UniqueID {
return UniqueID{ClassID: PG_TYPE_OID, Oid: t.Oid}
}
func (t RangeType) FQN() string {
return utils.MakeFQN(t.Schema, t.Name)
}
func GetRangeTypes(connectionPool *dbconn.DBConn) []RangeType {
gplog.Verbose("Retrieving range types")
query := fmt.Sprintf(`
SELECT t.oid,
quote_ident(n.nspname) AS schema,
quote_ident(t.typname) AS name,
format_type(st.oid, st.typtypmod) AS subtype,
CASE
WHEN c.collname IS NULL THEN ''
ELSE quote_ident(nc.nspname) || '.' || quote_ident(c.collname)
END AS collation,
CASE
WHEN opc.opcname IS NULL THEN ''
ELSE quote_ident(nopc.nspname) || '.' || quote_ident(opc.opcname)
END AS subtypeopclass,
CASE
WHEN r.rngcanonical = '-'::regproc THEN ''
ELSE r.rngcanonical::regproc::text
END AS canonical,
CASE
WHEN r.rngsubdiff = '-'::regproc THEN ''
ELSE r.rngsubdiff::regproc::text
END AS subtypediff
FROM pg_range r
JOIN pg_type t ON t.oid = r.rngtypid
JOIN pg_namespace n ON t.typnamespace = n.oid
JOIN pg_type st ON st.oid = r.rngsubtype
LEFT JOIN pg_collation c ON c.oid = r.rngcollation
LEFT JOIN pg_namespace nc ON nc.oid = c.collnamespace
LEFT JOIN pg_opclass opc ON opc.oid = r.rngsubopc
LEFT JOIN pg_namespace nopc ON nopc.oid = opc.opcnamespace
WHERE %s
AND t.typtype = 'r'
AND %s`, SchemaFilterClause("n"), ExtensionFilterClause("t"))
results := make([]RangeType, 0)
err := connectionPool.Select(&results, query)
gplog.FatalOnError(err)
return results
}
type ShellType struct {
Oid uint32
Schema string
Name string
}
func (t ShellType) GetMetadataEntry() (string, toc.MetadataEntry) {
return GetTypeMetadataEntry(t.Schema, t.Name)
}
func (t ShellType) GetUniqueID() UniqueID {
return UniqueID{ClassID: PG_TYPE_OID, Oid: t.Oid}
}
func (t ShellType) FQN() string {
return utils.MakeFQN(t.Schema, t.Name)
}
func GetShellTypes(connectionPool *dbconn.DBConn) []ShellType {
gplog.Verbose("Getting shell types")
query := fmt.Sprintf(`
SELECT t.oid,
quote_ident(n.nspname) AS schema,
quote_ident(t.typname) AS name
FROM pg_type t
JOIN pg_namespace n ON t.typnamespace = n.oid
WHERE %s
AND t.typtype = 'p'
AND %s
ORDER BY n.nspname, t.typname`, SchemaFilterClause("n"), ExtensionFilterClause("t"))
results := make([]ShellType, 0)
err := connectionPool.Select(&results, query)
gplog.FatalOnError(err)
return results
}
type Collation struct {
Oid uint32
Schema string
Name string
Collate string
Ctype string
Provider string
IsDeterministic string
}
func (c Collation) GetMetadataEntry() (string, toc.MetadataEntry) {
return "predata",
toc.MetadataEntry{
Schema: c.Schema,
Name: c.Name,
ObjectType: toc.OBJ_COLLATION,
ReferenceObject: "",
StartByte: 0,
EndByte: 0,
}
}
func (c Collation) GetUniqueID() UniqueID {
return UniqueID{ClassID: PG_COLLATION_OID, Oid: c.Oid}
}
func (c Collation) FQN() string {
return utils.MakeFQN(c.Schema, c.Name)
}
func GetCollations(connectionPool *dbconn.DBConn) []Collation {
before7Query := fmt.Sprintf(`
SELECT c.oid,
quote_ident(n.nspname) AS schema,
quote_ident(c.collname) AS name,
c.collcollate AS collate,
c.collctype AS ctype
FROM pg_collation c
JOIN pg_namespace n ON c.collnamespace = n.oid
WHERE %s`, SchemaFilterClause("n"))
atLeast7Query := fmt.Sprintf(`
SELECT c.oid,
quote_ident(n.nspname) AS schema,
quote_ident(c.collname) AS name,
c.collcollate AS collate,
c.collctype AS ctype,
c.collprovider as provider,
c.collisdeterministic as IsDeterministic
FROM pg_collation c
JOIN pg_namespace n ON c.collnamespace = n.oid
WHERE %s`, SchemaFilterClause("n"))
query := ""
if connectionPool.Version.Before("7") {
query = before7Query
} else {
query = atLeast7Query
}
results := make([]Collation, 0)
err := connectionPool.Select(&results, query)
gplog.FatalOnError(err)
return results
}