Skip to content

Commit ee46c21

Browse files
authored
Make it possible to disable schemas (#234)
1 parent 9bea3c0 commit ee46c21

File tree

7 files changed

+57
-32
lines changed

7 files changed

+57
-32
lines changed

source.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ func (s *Source) Open(ctx context.Context, pos opencdc.Position) error {
113113
Tables: s.config.Tables,
114114
TableKeys: s.tableKeys,
115115
WithSnapshot: s.config.SnapshotMode == source.SnapshotModeInitial,
116+
WithAvroSchema: s.config.WithAvroSchema,
116117
SnapshotFetchSize: s.config.SnapshotFetchSize,
117118
})
118119
if err != nil {

source/logrepl/cdc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func NewCDCIterator(ctx context.Context, pool *pgxpool.Pool, c CDCConfig) (*CDCI
6565
}
6666

6767
records := make(chan opencdc.Record)
68-
handler := NewCDCHandler(internal.NewRelationSet(), c.TableKeys, records)
68+
handler := NewCDCHandler(internal.NewRelationSet(), c.TableKeys, records, c.WithAvroSchema)
6969

7070
sub, err := internal.CreateSubscription(
7171
ctx,

source/logrepl/cdc_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ func testCDCIterator(ctx context.Context, t *testing.T, pool *pgxpool.Pool, tabl
475475
TableKeys: map[string]string{table: "id"},
476476
PublicationName: table, // table is random, reuse for publication name
477477
SlotName: table, // table is random, reuse for slot name
478+
WithAvroSchema: true,
478479
}
479480

480481
i, err := NewCDCIterator(ctx, pool, config)

source/logrepl/combined.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Config struct {
4848
Tables []string
4949
TableKeys map[string]string
5050
WithSnapshot bool
51+
WithAvroSchema bool
5152
SnapshotFetchSize int
5253
}
5354

@@ -178,6 +179,7 @@ func (c *CombinedIterator) initCDCIterator(ctx context.Context, pos position.Pos
178179
PublicationName: c.conf.PublicationName,
179180
Tables: c.conf.Tables,
180181
TableKeys: c.conf.TableKeys,
182+
WithAvroSchema: c.conf.WithAvroSchema,
181183
})
182184
if err != nil {
183185
return fmt.Errorf("failed to create CDC iterator: %w", err)
@@ -199,11 +201,12 @@ func (c *CombinedIterator) initSnapshotIterator(ctx context.Context, pos positio
199201
}
200202

201203
snapshotIterator, err := snapshot.NewIterator(ctx, c.pool, snapshot.Config{
202-
Position: c.conf.Position,
203-
Tables: c.conf.Tables,
204-
TableKeys: c.conf.TableKeys,
205-
TXSnapshotID: c.cdcIterator.TXSnapshotID(),
206-
FetchSize: c.conf.SnapshotFetchSize,
204+
Position: c.conf.Position,
205+
Tables: c.conf.Tables,
206+
TableKeys: c.conf.TableKeys,
207+
TXSnapshotID: c.cdcIterator.TXSnapshotID(),
208+
FetchSize: c.conf.SnapshotFetchSize,
209+
WithAvroSchema: c.conf.WithAvroSchema,
207210
})
208211
if err != nil {
209212
return fmt.Errorf("failed to create snapshot iterator: %w", err)

source/logrepl/handler.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@ type CDCHandler struct {
3636
out chan<- opencdc.Record
3737
lastTXLSN pglogrepl.LSN
3838

39+
withAvroSchema bool
3940
keySchemas map[string]cschema.Schema
4041
payloadSchemas map[string]cschema.Schema
4142
}
4243

43-
func NewCDCHandler(rs *internal.RelationSet, tableKeys map[string]string, out chan<- opencdc.Record) *CDCHandler {
44+
func NewCDCHandler(rs *internal.RelationSet, tableKeys map[string]string, out chan<- opencdc.Record, withAvroSchema bool) *CDCHandler {
4445
return &CDCHandler{
4546
tableKeys: tableKeys,
4647
relationSet: rs,
4748
out: out,
49+
withAvroSchema: withAvroSchema,
4850
keySchemas: make(map[string]cschema.Schema),
4951
payloadSchemas: make(map[string]cschema.Schema),
5052
}
@@ -245,6 +247,9 @@ func (*CDCHandler) buildPosition(lsn pglogrepl.LSN) opencdc.Position {
245247
// updateAvroSchema generates and stores avro schema based on the relation's row,
246248
// when usage of avro schema is requested.
247249
func (h *CDCHandler) updateAvroSchema(ctx context.Context, rel *pglogrepl.RelationMessage) error {
250+
if !h.withAvroSchema {
251+
return nil
252+
}
248253
// Payload schema
249254
avroPayloadSch, err := schema.Avro.ExtractLogrepl(rel.RelationName+"_payload", rel)
250255
if err != nil {
@@ -281,6 +286,9 @@ func (h *CDCHandler) updateAvroSchema(ctx context.Context, rel *pglogrepl.Relati
281286
}
282287

283288
func (h *CDCHandler) attachSchemas(rec opencdc.Record, relationName string) {
289+
if !h.withAvroSchema {
290+
return
291+
}
284292
cschema.AttachPayloadSchemaToRecord(rec, h.payloadSchemas[relationName])
285293
cschema.AttachKeySchemaToRecord(rec, h.keySchemas[relationName])
286294
}

source/snapshot/fetch_worker.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ var supportedKeyTypes = []string{
4444
}
4545

4646
type FetchConfig struct {
47-
Table string
48-
Key string
49-
TXSnapshotID string
50-
FetchSize int
51-
Position position.Position
47+
Table string
48+
Key string
49+
TXSnapshotID string
50+
FetchSize int
51+
Position position.Position
52+
WithAvroSchema bool
5253
}
5354

5455
var (
@@ -337,14 +338,17 @@ func (f *FetchWorker) buildFetchData(fields []pgconn.FieldDescription, values []
337338
return FetchData{}, fmt.Errorf("failed to encode record data: %w", err)
338339
}
339340

340-
return FetchData{
341-
Key: key,
342-
Payload: payload,
343-
Position: pos,
344-
Table: f.conf.Table,
345-
PayloadSchema: *f.payloadSchema,
346-
KeySchema: *f.keySchema,
347-
}, nil
341+
fd := FetchData{
342+
Key: key,
343+
Payload: payload,
344+
Position: pos,
345+
Table: f.conf.Table,
346+
}
347+
if f.conf.WithAvroSchema {
348+
fd.PayloadSchema = *f.payloadSchema
349+
fd.KeySchema = *f.keySchema
350+
}
351+
return fd, nil
348352
}
349353

350354
func (f *FetchWorker) buildSnapshotPosition(fields []pgconn.FieldDescription, values []any) (position.SnapshotPosition, error) {
@@ -467,6 +471,10 @@ func (*FetchWorker) validateTable(ctx context.Context, table string, tx pgx.Tx)
467471
}
468472

469473
func (f *FetchWorker) extractSchemas(ctx context.Context, fields []pgconn.FieldDescription) error {
474+
if !f.conf.WithAvroSchema {
475+
return nil
476+
}
477+
470478
if f.payloadSchema == nil {
471479
sdk.Logger(ctx).Debug().
472480
Msgf("extracting payload schema for %v fields in %v", len(fields), f.conf.Table)

source/snapshot/iterator.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ import (
3131
var ErrIteratorDone = errors.New("snapshot complete")
3232

3333
type Config struct {
34-
Position opencdc.Position
35-
Tables []string
36-
TableKeys map[string]string
37-
TXSnapshotID string
38-
FetchSize int
34+
Position opencdc.Position
35+
Tables []string
36+
TableKeys map[string]string
37+
TXSnapshotID string
38+
FetchSize int
39+
WithAvroSchema bool
3940
}
4041

4142
type Iterator struct {
@@ -122,8 +123,10 @@ func (i *Iterator) buildRecord(d FetchData) opencdc.Record {
122123
metadata["postgres.table"] = d.Table
123124

124125
rec := sdk.Util.Source.NewRecordSnapshot(pos, metadata, d.Key, d.Payload)
125-
cschema.AttachKeySchemaToRecord(rec, d.KeySchema)
126-
cschema.AttachPayloadSchemaToRecord(rec, d.PayloadSchema)
126+
if i.conf.WithAvroSchema {
127+
cschema.AttachKeySchemaToRecord(rec, d.KeySchema)
128+
cschema.AttachPayloadSchemaToRecord(rec, d.PayloadSchema)
129+
}
127130

128131
return rec
129132
}
@@ -135,11 +138,12 @@ func (i *Iterator) initFetchers(ctx context.Context) error {
135138

136139
for j, t := range i.conf.Tables {
137140
w := NewFetchWorker(i.db, i.data, FetchConfig{
138-
Table: t,
139-
Key: i.conf.TableKeys[t],
140-
TXSnapshotID: i.conf.TXSnapshotID,
141-
Position: i.lastPosition,
142-
FetchSize: i.conf.FetchSize,
141+
Table: t,
142+
Key: i.conf.TableKeys[t],
143+
TXSnapshotID: i.conf.TXSnapshotID,
144+
Position: i.lastPosition,
145+
FetchSize: i.conf.FetchSize,
146+
WithAvroSchema: i.conf.WithAvroSchema,
143147
})
144148

145149
if err := w.Validate(ctx); err != nil {

0 commit comments

Comments
 (0)