/
discovery.go
246 lines (215 loc) · 7.5 KB
/
discovery.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
package postgres
import (
"context"
_ "embed"
"errors"
"fmt"
"github.com/jackc/pgtype"
"go.uber.org/zap"
)
const ( // shortcuts
binT = pgtype.BinaryFormatCode
textT = pgtype.TextFormatCode
)
var (
// ErrUnknownType type discovery failed
ErrUnknownType = errors.New("unknown type")
)
// TODO: check `wal_status` from `pg_replication_slots`. See: https://www.postgresql.org/docs/13/view-pg-replication-slots.html
// And if status is `lost`, start reindexing from scratch.
// discoverQuery selects all table and column comments for tables mentioned in publication.
// comments are used as golang structtags for configuration.
// TODO (vitalii): check if FK -> tablename can help with inlines
//
//go:embed queries/discover_query.sql
var discoverQuery string
// Discover uses Postgres publication and table or column comments to generate replication config
// Only tables explosed via Publication will be considered for exporting to ES.
func (db *Database) Discover(ctx context.Context) error {
db.queryConnMu.Lock()
res := db.queryConn.ExecParams(
ctx, discoverQuery,
[][]byte{[]byte(db.Publication)}, nil, nil,
[]int16{binT, binT, binT, binT, binT, binT, binT, binT},
).Read()
db.queryConnMu.Unlock()
if res.Err != nil {
return fmt.Errorf("discover db tags config: %w", res.Err)
}
cd := struct {
PK pgtype.Bool
Schema pgtype.Text
Table pgtype.Text
TableComment pgtype.Text
Column pgtype.Text
ColumnComment pgtype.Text
Typ pgtype.OID
OldInWAL pgtype.Bool
}{}
for _, row := range res.Rows {
cd.Schema.DecodeBinary(nil, row[0])
cd.Table.DecodeBinary(nil, row[1])
cd.Column.DecodeBinary(nil, row[2])
cd.TableComment.DecodeBinary(nil, row[3])
cd.ColumnComment.DecodeBinary(nil, row[4])
cd.PK.DecodeBinary(nil, row[5])
cd.Typ.DecodeBinary(nil, row[6])
cd.OldInWAL.DecodeBinary(nil, row[7])
t := db.schema(cd.Schema.String).table(cd.Table.String)
t.parseStructTag(cd.TableComment.String) // table config needs to be parsed before column config, since some values are inherited from it
col := t.Column(cd.Column.String)
col.parseStructTag(cd.ColumnComment.String)
col.sqlPK = cd.PK.Bool
col.oldInWAL = cd.OldInWAL.Bool
dataType, err := db.dataTypeDecoder(uint32(cd.Typ))
if err != nil {
t.logger.Warn("can not find data type for column", zap.String("column", cd.Column.String))
}
col.setTyp(dataType)
// col.logger.Debug("discovered column", zap.String("name", col.name), zap.String("field", col.fieldName))
}
return nil
}
// discoverENUMQuery can be used to retrieve enum members, which is not required. But gives slightly increased performance.
//
//go:embed queries/discover_enum_query.sql
var discoverENUMQuery string
// discoverUnknownType fetches type description from postgres and saves into connInfo.
// works with Enum, EnumArray and Composite fields.
func (db *Database) discoverUnknownType(ctx context.Context, oid pgtype.OID) error {
if _, ok := db.connInfo.DataTypeForOID(uint32(oid)); ok {
return nil
}
typInfo, err := db.getTypInfo(ctx, oid)
if err != nil {
return fmt.Errorf("get type info", err)
}
switch typInfo.Typ.Int {
case 'c':
// Here are some examples of, how composite type casting is done in python psycopg
// https://github.com/psycopg/psycopg2/blob/1d3a89a0bba621dc1cc9b32db6d241bd2da85ad1/lib/extras.py#L1089-L1097
attrelid, err := typInfo.TypRelOID.EncodeBinary(db.connInfo, nil)
if err != nil {
return fmt.Errorf("can not encode OID parameter: %w", err)
}
db.queryConnMu.Lock()
res := db.queryConn.ExecParams(ctx,
"select attname, atttypid from pg_attribute where attrelid=$1 order by attnum",
[][]byte{attrelid}, []uint32{uint32(pgtype.OIDOID)}, []int16{binT},
[]int16{binT, binT},
).Read()
db.queryConnMu.Unlock()
if res.Err != nil {
return res.Err
}
fields := make([]pgtype.CompositeTypeField, len(res.Rows))
for i, row := range res.Rows {
db.connInfo.Scan(pgtype.TextOID, binT, row[0], &(fields[i]).Name)
db.connInfo.Scan(pgtype.OIDOID, binT, row[1], &(fields[i]).OID)
// double check, for nested enums or composite types
db.discoverUnknownType(ctx, pgtype.OID(fields[i].OID))
}
typ, err := pgtype.NewCompositeType(typInfo.Name.String, fields, db.connInfo)
if err != nil {
return err
}
db.connInfo.RegisterDataType(pgtype.DataType{
Value: typ,
Name: typInfo.Name.String,
OID: uint32(typInfo.OID),
})
db.logger.Info("registered composite type", zap.Uint32("OID", uint32(typInfo.OID)), zap.String("name", typInfo.Name.String))
if typInfo.ArrOID > 0 {
db.logger.Warn("array composites are not yet supported", zap.Uint32("OID", uint32(typInfo.OID)), zap.String("name", typInfo.Name.String))
// db.connInfo.RegisterDataType(pgtype.DataType{
// Value: &pgtype.CompositeArray{}, // todo implement
// Name: typInfo.Name.String + "_array",
// OID: uint32(typInfo.ArrOID),
// })
}
case 'e':
db.connInfo.RegisterDataType(pgtype.DataType{
Value: pgtype.NewEnumType(typInfo.Name.String, nil),
Name: typInfo.Name.String,
OID: uint32(typInfo.OID),
})
db.logger.Info("registered enum type", zap.Uint32("OID", uint32(typInfo.OID)), zap.String("name", typInfo.Name.String))
if typInfo.ArrOID > 0 {
db.connInfo.RegisterDataType(pgtype.DataType{
Value: &pgtype.EnumArray{},
Name: typInfo.Name.String + "_array",
OID: uint32(typInfo.ArrOID),
})
db.logger.Info("registered enum array", zap.Uint32("OID", uint32(typInfo.OID)), zap.String("name", typInfo.Name.String))
}
return nil
// resolve enum type
case 'b':
break
default:
return ErrUnknownType
}
switch typInfo.Cat.Int {
case 'A':
if typInfo.ElemOID > 0 {
return db.discoverUnknownType(ctx, typInfo.ElemOID)
}
// if typInfo.TypRelOID > 0 { // Not supported yet
// return db.discoverUnknownType(ctx, typInfo.TypRelOID)
// }
//
// db.connInfo.RegisterDataType(pgtype.DataType{
// Value: &pgtype.EnumArray{},
// Name: typInfo.Name.String + "_array",
// OID: uint32(typInfo.OID),
// })
// case 'U':
}
return nil
}
type typInfoRow struct {
OID pgtype.OID
ArrOID pgtype.OID
ElemOID pgtype.OID
TypRelOID pgtype.OID // kinda relation, but for composite field types
Name pgtype.Text
Typ pgtype.QChar
Cat pgtype.QChar
}
//go:embed queries/get_type_by_oid.sql
var queryTypeByOID string
func (db *Database) getTypInfo(ctx context.Context, oid pgtype.OID) (*typInfoRow, error) {
typOIDArg, err := oid.EncodeBinary(db.connInfo, nil)
if err != nil {
return nil, fmt.Errorf("can not encode OID parameter: %w", err)
}
db.queryConnMu.Lock()
res := db.queryConn.ExecParams(
ctx, queryTypeByOID,
[][]byte{typOIDArg}, []uint32{uint32(pgtype.OIDOID)}, []int16{binT},
[]int16{binT, binT, binT, binT, binT, binT, binT},
).Read()
db.queryConnMu.Unlock()
typInfo := typInfoRow{}
if res.Err != nil {
return nil, fmt.Errorf("type not found %v: %w", oid, res.Err)
}
if len(res.Rows) != 1 {
return nil, fmt.Errorf("type request returned %v rows, 1 expected", len(res.Rows))
}
row := res.Rows[0]
errs := make([]error, 7)
errs[0] = typInfo.OID.DecodeBinary(nil, row[0])
errs[1] = typInfo.Name.DecodeBinary(nil, row[1])
errs[2] = typInfo.Typ.DecodeBinary(nil, row[2])
errs[3] = typInfo.Cat.DecodeBinary(nil, row[3])
errs[4] = typInfo.ArrOID.DecodeBinary(nil, row[4])
errs[5] = typInfo.ElemOID.DecodeBinary(nil, row[5])
errs[6] = typInfo.TypRelOID.DecodeBinary(nil, row[6])
for _, err := range errs {
if err != nil {
return nil, fmt.Errorf("decode type response error: %w", err)
}
}
return &typInfo, nil
}