Skip to content

Commit 0e007d9

Browse files
committed
feat: queryPreprocessing in logicalRestore + support inline SQL in any queryPreprocessing (#407):
* support queryPreprocessing in logicalRestore * add `inline` SQL processing to snapshot jobs (`logicalSnapshot`, `physicalSnapshot`) as well
1 parent 757125e commit 0e007d9

File tree

10 files changed

+273
-164
lines changed

10 files changed

+273
-164
lines changed

engine/configs/config.example.logical_generic.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,18 @@ retrieval:
288288
# database2:
289289
# databaseN:
290290

291+
# It is possible to define pre-processing SQL queries. For example, "/tmp/scripts/sql".
292+
# Default: empty string (no pre-processing defined).
293+
queryPreprocessing:
294+
# Path to SQL pre-processing queries. Default: empty string (no pre-processing defined).
295+
queryPath: ""
296+
297+
# Worker limit for parallel queries. Parallelization doesn't work for inline SQL queries.
298+
maxParallelWorkers: 2
299+
300+
# Inline SQL. Queries run after scripts placed in 'queryPath'.
301+
inline: ""
302+
291303
logicalSnapshot:
292304
options:
293305
# Adjust PostgreSQL configuration
@@ -308,6 +320,9 @@ retrieval:
308320
# Worker limit for parallel queries.
309321
maxParallelWorkers: 2
310322

323+
# Inline SQL. Queries run after scripts placed in 'queryPath'.
324+
inline: ""
325+
311326
cloning:
312327
# Host that will be specified in database connection info for all clones
313328
# Use public IP address if database connections are allowed from outside

engine/configs/config.example.logical_rds_iam.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,18 @@ retrieval:
289289
# database2:
290290
# databaseN:
291291

292+
# It is possible to define pre-processing SQL queries. For example, "/tmp/scripts/sql".
293+
# Default: empty string (no pre-processing defined).
294+
queryPreprocessing:
295+
# Path to SQL pre-processing queries. Default: empty string (no pre-processing defined).
296+
queryPath: ""
297+
298+
# Worker limit for parallel queries. Parallelization doesn't work for inline SQL queries.
299+
maxParallelWorkers: 2
300+
301+
# Inline SQL. Queries run after scripts placed in 'queryPath'.
302+
inline: ""
303+
292304
logicalSnapshot:
293305
options:
294306
# Adjust PostgreSQL configuration
@@ -309,6 +321,9 @@ retrieval:
309321
# Worker limit for parallel queries.
310322
maxParallelWorkers: 2
311323

324+
# Inline SQL. Queries run after scripts placed in 'queryPath'.
325+
inline: ""
326+
312327
cloning:
313328
# Host that will be specified in database connection info for all clones
314329
# Use public IP address if database connections are allowed from outside

engine/configs/config.example.physical_generic.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ retrieval:
261261
# Worker limit for parallel queries.
262262
maxParallelWorkers: 2
263263

264+
# Inline SQL. Queries run after scripts placed in 'queryPath'.
265+
inline: ""
266+
264267
# Add PostgreSQL configuration parameters to the promotion container.
265268
configs:
266269
shared_buffers: 2GB

engine/configs/config.example.physical_pgbackrest.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ retrieval:
265265
# Worker limit for parallel queries.
266266
maxParallelWorkers: 2
267267

268+
# Inline SQL. Queries run after scripts placed in 'queryPath'.
269+
inline: ""
270+
268271
# Add PostgreSQL configuration parameters to the promotion container.
269272
configs:
270273
shared_buffers: 2GB

engine/configs/config.example.physical_walg.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ retrieval:
251251
# Worker limit for parallel queries.
252252
maxParallelWorkers: 2
253253

254+
# Inline SQL. Queries run after scripts placed in 'queryPath'.
255+
inline: ""
256+
254257
# Add PostgreSQL configuration parameters to the promotion container.
255258
configs:
256259
shared_buffers: 2GB

engine/internal/retrieval/engine/postgres/logical/restore.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/cont"
3232
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/defaults"
3333
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/health"
34+
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/query"
3435
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/options"
3536

3637
"gitlab.com/postgres-ai/database-lab/v3/pkg/config/global"
@@ -87,19 +88,21 @@ type RestoreJob struct {
8788
engineProps global.EngineProps
8889
dbMarker *dbmarker.Marker
8990
dbMark *dbmarker.Config
91+
queryProcessor *query.Processor
9092
isDumpLocationDir bool
9193
RestoreOptions
9294
}
9395

9496
// RestoreOptions defines a logical restore options.
9597
type RestoreOptions struct {
96-
DumpLocation string `yaml:"dumpLocation"`
97-
DockerImage string `yaml:"dockerImage"`
98-
ContainerConfig map[string]interface{} `yaml:"containerConfig"`
99-
Databases map[string]DumpDefinition `yaml:"databases"`
100-
ForceInit bool `yaml:"forceInit"`
101-
ParallelJobs int `yaml:"parallelJobs"`
102-
Configs map[string]string `yaml:"configs"`
98+
DumpLocation string `yaml:"dumpLocation"`
99+
DockerImage string `yaml:"dockerImage"`
100+
ContainerConfig map[string]interface{} `yaml:"containerConfig"`
101+
Databases map[string]DumpDefinition `yaml:"databases"`
102+
ForceInit bool `yaml:"forceInit"`
103+
ParallelJobs int `yaml:"parallelJobs"`
104+
Configs map[string]string `yaml:"configs"`
105+
QueryPreprocessing query.PreprocessorCfg `yaml:"queryPreprocessing"`
103106
}
104107

105108
// Partial defines tables and rules for a partial logical restore.
@@ -125,6 +128,10 @@ func NewJob(cfg config.JobConfig, global *global.Config, engineProps global.Engi
125128

126129
restoreJob.setDefaults()
127130

131+
if qp := restoreJob.RestoreOptions.QueryPreprocessing; qp.QueryPath != "" || qp.Inline != "" {
132+
restoreJob.queryProcessor = query.NewQueryProcessor(cfg.Docker, qp, global.Database.Name(), global.Database.User())
133+
}
134+
128135
return restoreJob, nil
129136
}
130137

@@ -233,6 +240,10 @@ func (r *RestoreJob) Run(ctx context.Context) (err error) {
233240
}
234241
}
235242

243+
if err := r.queryProcessor.ApplyPreprocessingQueries(ctx, containerID); err != nil {
244+
return errors.Wrap(err, "failed to run preprocessing queries")
245+
}
246+
236247
dbList, err := r.getDBList(ctx, containerID)
237248
if err != nil {
238249
return err

engine/internal/retrieval/engine/postgres/snapshot/logical.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools"
3030
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/cont"
3131
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/health"
32+
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/query"
3233
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/options"
3334
"gitlab.com/postgres-ai/database-lab/v3/internal/telemetry"
3435
"gitlab.com/postgres-ai/database-lab/v3/pkg/config/global"
@@ -54,7 +55,7 @@ type LogicalInitial struct {
5455
globalCfg *global.Config
5556
engineProps global.EngineProps
5657
dbMarker *dbmarker.Marker
57-
queryProcessor *queryProcessor
58+
queryProcessor *query.Processor
5859
}
5960

6061
// LogicalOptions describes options for a logical initialization job.
@@ -68,7 +69,7 @@ type LogicalOptions struct {
6869
// DataPatching allows executing queries to transform data before snapshot taking.
6970
type DataPatching struct {
7071
DockerImage string `yaml:"dockerImage"`
71-
QueryPreprocessing QueryPreprocessing `yaml:"queryPreprocessing"`
72+
QueryPreprocessing query.PreprocessorCfg `yaml:"queryPreprocessing"`
7273
ContainerConfig map[string]interface{} `yaml:"containerConfig"`
7374
}
7475

@@ -90,10 +91,8 @@ func NewLogicalInitialJob(cfg config.JobConfig, global *global.Config, enginePro
9091
return nil, errors.Wrap(err, "failed to unmarshal configuration options")
9192
}
9293

93-
if li.options.DataPatching.QueryPreprocessing.QueryPath != "" {
94-
li.queryProcessor = newQueryProcessor(cfg.Docker, global.Database.Name(), global.Database.User(),
95-
li.options.DataPatching.QueryPreprocessing.QueryPath,
96-
li.options.DataPatching.QueryPreprocessing.MaxParallelWorkers)
94+
if qp := li.options.DataPatching.QueryPreprocessing; qp.QueryPath != "" || qp.Inline != "" {
95+
li.queryProcessor = query.NewQueryProcessor(cfg.Docker, qp, global.Database.Name(), global.Database.User())
9796
}
9897

9998
return li, nil
@@ -254,7 +253,7 @@ func (s *LogicalInitial) runPreprocessingQueries(ctx context.Context, dataDir st
254253
return errors.Wrap(err, "failed to readiness check")
255254
}
256255

257-
if err := s.queryProcessor.applyPreprocessingQueries(ctx, containerID); err != nil {
256+
if err := s.queryProcessor.ApplyPreprocessingQueries(ctx, containerID); err != nil {
258257
return errors.Wrap(err, "failed to run preprocessing queries")
259258
}
260259

engine/internal/retrieval/engine/postgres/snapshot/physical.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/defaults"
3939
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/health"
4040
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/pgtool"
41+
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/query"
4142
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/options"
4243
"gitlab.com/postgres-ai/database-lab/v3/internal/telemetry"
4344
"gitlab.com/postgres-ai/database-lab/v3/pkg/config/global"
@@ -96,7 +97,7 @@ type PhysicalInitial struct {
9697
scheduler *cron.Cron
9798
schedulerCtx context.Context
9899
promotionMutex sync.Mutex
99-
queryProcessor *queryProcessor
100+
queryProcessor *query.Processor
100101
tm *telemetry.Agent
101102
}
102103

@@ -117,7 +118,7 @@ type Promotion struct {
117118
DockerImage string `yaml:"dockerImage"`
118119
ContainerConfig map[string]interface{} `yaml:"containerConfig"`
119120
HealthCheck HealthCheck `yaml:"healthCheck"`
120-
QueryPreprocessing QueryPreprocessing `yaml:"queryPreprocessing"`
121+
QueryPreprocessing query.PreprocessorCfg `yaml:"queryPreprocessing"`
121122
Configs map[string]string `yaml:"configs"`
122123
Recovery map[string]string `yaml:"recovery"`
123124
}
@@ -177,10 +178,8 @@ func NewPhysicalInitialJob(
177178
return nil, errors.Wrap(err, "invalid physicalSnapshot configuration")
178179
}
179180

180-
if p.options.Promotion.QueryPreprocessing.QueryPath != "" {
181-
p.queryProcessor = newQueryProcessor(cfg.Docker, global.Database.Name(), global.Database.User(),
182-
p.options.Promotion.QueryPreprocessing.QueryPath,
183-
p.options.Promotion.QueryPreprocessing.MaxParallelWorkers)
181+
if qp := p.options.Promotion.QueryPreprocessing; qp.QueryPath != "" || qp.Inline != "" {
182+
p.queryProcessor = query.NewQueryProcessor(cfg.Docker, qp, global.Database.Name(), global.Database.User())
184183
}
185184

186185
p.setupScheduler()
@@ -631,7 +630,7 @@ func (p *PhysicalInitial) promoteInstance(ctx context.Context, clonePath string,
631630
}
632631

633632
if p.queryProcessor != nil {
634-
if err := p.queryProcessor.applyPreprocessingQueries(ctx, containerID); err != nil {
633+
if err := p.queryProcessor.ApplyPreprocessingQueries(ctx, containerID); err != nil {
635634
return errors.Wrap(err, "failed to run preprocessing queries")
636635
}
637636
}

engine/internal/retrieval/engine/postgres/snapshot/queryPreprocessor.go

Lines changed: 0 additions & 143 deletions
This file was deleted.

0 commit comments

Comments
 (0)