@@ -15,8 +15,6 @@ import (
15
15
"github.com/cockroachdb/cockroach/pkg/roachpb"
16
16
"github.com/cockroachdb/cockroach/pkg/settings"
17
17
"github.com/cockroachdb/cockroach/pkg/sql/isql"
18
- "github.com/cockroachdb/cockroach/pkg/sql/parser"
19
- "github.com/cockroachdb/cockroach/pkg/sql/parser/statements"
20
18
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
21
19
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
22
20
"github.com/cockroachdb/cockroach/pkg/sql/sqlliveness"
35
33
)
36
34
)
37
35
38
- // Parse all job storage SQL statements at init time to avoid parsing them on
39
- // every execution.
40
- func mustParseOne (s string ) statements.Statement [tree.Statement ] {
41
- stmt , err := parser .ParseOne (s )
42
- if err != nil {
43
- panic (err )
44
- }
45
- return stmt
46
- }
47
-
48
36
// ProgressStorage reads and writes progress rows.
49
37
type ProgressStorage jobspb.JobID
50
38
@@ -53,10 +41,6 @@ func (j *Job) ProgressStorage() ProgressStorage {
53
41
return ProgressStorage (j .id )
54
42
}
55
43
56
- var getJobProgressQuery = mustParseOne (
57
- "SELECT written, fraction, resolved FROM system.job_progress WHERE job_id = $1" ,
58
- )
59
-
60
44
// Get returns the latest progress report for the job along with when it was
61
45
// written. If the fraction is null it is returned as NaN, and if the resolved
62
46
// ts is null, it is empty.
@@ -66,10 +50,10 @@ func (i ProgressStorage) Get(
66
50
ctx , sp := tracing .ChildSpan (ctx , "get-job-progress" )
67
51
defer sp .Finish ()
68
52
69
- row , err := txn .QueryRowExParsed (
53
+ row , err := txn .QueryRowEx (
70
54
ctx , "job-progress-get" , txn .KV (),
71
55
sessiondata .NodeUserSessionDataOverride ,
72
- getJobProgressQuery , i ,
56
+ "SELECT written, fraction, resolved FROM system.job_progress WHERE job_id = $1" , i ,
73
57
)
74
58
75
59
if err != nil || row == nil {
@@ -107,22 +91,6 @@ func (i ProgressStorage) Get(
107
91
return fraction , ts , written .Time , nil
108
92
}
109
93
110
- var (
111
- deleteJobProgressStmt = mustParseOne (
112
- `DELETE FROM system.job_progress WHERE job_id = $1` ,
113
- )
114
- insertJobProgressStmt = mustParseOne (
115
- `INSERT INTO system.job_progress (job_id, written, fraction, resolved) VALUES ($1, now(), $2, $3)` ,
116
- )
117
- insertJobProgressHistoryStmt = mustParseOne (
118
- `UPSERT INTO system.job_progress_history (job_id, written, fraction, resolved) VALUES ($1, now(), $2, $3)` ,
119
- )
120
- pruneJobProgressHistoryStmt = mustParseOne (
121
- `DELETE FROM system.job_progress_history WHERE job_id = $1 AND written IN
122
- (SELECT written FROM system.job_progress_history WHERE job_id = $1 ORDER BY written DESC OFFSET $2)` ,
123
- )
124
- )
125
-
126
94
// Set records a progress update. If fraction is NaN or resolved is empty, that
127
95
// field is left null. The time at which the progress was reported is recorded.
128
96
func (i ProgressStorage ) Set (
@@ -131,9 +99,9 @@ func (i ProgressStorage) Set(
131
99
ctx , sp := tracing .ChildSpan (ctx , "write-job-progress" )
132
100
defer sp .Finish ()
133
101
134
- if _ , err := txn .ExecParsed (
102
+ if _ , err := txn .ExecEx (
135
103
ctx , "write-job-progress-delete" , txn .KV (), sessiondata .NodeUserSessionDataOverride ,
136
- deleteJobProgressStmt , i ,
104
+ `DELETE FROM system.job_progress WHERE job_id = $1` , i ,
137
105
); err != nil {
138
106
return err
139
107
}
@@ -146,25 +114,26 @@ func (i ProgressStorage) Set(
146
114
ts = resolved .AsOfSystemTime ()
147
115
}
148
116
149
- if _ , err := txn .ExecParsed (
117
+ if _ , err := txn .ExecEx (
150
118
ctx , "write-job-progress-insert" , txn .KV (), sessiondata .NodeUserSessionDataOverride ,
151
- insertJobProgressStmt ,
119
+ `INSERT INTO system.job_progress (job_id, written, fraction, resolved) VALUES ($1, now(), $2, $3)` ,
152
120
i , frac , ts ,
153
121
); err != nil {
154
122
return err
155
123
}
156
124
157
- if _ , err := txn .ExecParsed (
125
+ if _ , err := txn .ExecEx (
158
126
ctx , "write-job-progress-history-insert" , txn .KV (), sessiondata .NodeUserSessionDataOverride ,
159
- insertJobProgressHistoryStmt ,
127
+ `UPSERT INTO system.job_progress_history (job_id, written, fraction, resolved) VALUES ($1, now(), $2, $3)` ,
160
128
i , frac , ts ,
161
129
); err != nil {
162
130
return err
163
131
}
164
132
165
- if _ , err := txn .ExecParsed (
133
+ if _ , err := txn .ExecEx (
166
134
ctx , "write-job-progress-history-prune" , txn .KV (), sessiondata .NodeUserSessionDataOverride ,
167
- pruneJobProgressHistoryStmt ,
135
+ `DELETE FROM system.job_progress_history WHERE job_id = $1 AND written IN
136
+ (SELECT written FROM system.job_progress_history WHERE job_id = $1 ORDER BY written DESC OFFSET $2)` ,
168
137
i , retainedProgressHistory .Get (txn .KV ().DB ().SettingsValues ()),
169
138
); err != nil {
170
139
return err
@@ -196,23 +165,15 @@ func (j *Job) StatusStorage() StatusStorage {
196
165
return StatusStorage (j .id )
197
166
}
198
167
199
- var deleteJobStatusStmt = mustParseOne (
200
- `DELETE FROM system.job_status WHERE job_id = $1` ,
201
- )
202
-
203
168
// Clear clears the status message row for the job, if it exists.
204
169
func (i StatusStorage ) Clear (ctx context.Context , txn isql.Txn ) error {
205
- _ , err := txn .ExecParsed (
170
+ _ , err := txn .ExecEx (
206
171
ctx , "clear-job-status-delete" , txn .KV (), sessiondata .NodeUserSessionDataOverride ,
207
- deleteJobStatusStmt , i ,
172
+ `DELETE FROM system.job_status WHERE job_id = $1` , i ,
208
173
)
209
174
return err
210
175
}
211
176
212
- var insertJobStatusStmt = mustParseOne (
213
- `INSERT INTO system.job_status (job_id, written, status) VALUES ($1, now(), $2)` ,
214
- )
215
-
216
177
// Sets writes the current status, replacing the current one if it exists.
217
178
// Setting an empty status is the same as calling Clear().
218
179
func (i StatusStorage ) Set (ctx context.Context , txn isql.Txn , status string ) error {
@@ -225,9 +186,9 @@ func (i StatusStorage) Set(ctx context.Context, txn isql.Txn, status string) err
225
186
return err
226
187
}
227
188
228
- if _ , err := txn .ExecParsed (
189
+ if _ , err := txn .ExecEx (
229
190
ctx , "write-job-status-insert" , txn .KV (), sessiondata .NodeUserSessionDataOverride ,
230
- insertJobStatusStmt ,
191
+ `INSERT INTO system.job_status (job_id, written, status) VALUES ($1, now(), $2)` ,
231
192
i , status ,
232
193
); err != nil {
233
194
return err
@@ -240,18 +201,14 @@ func (i StatusStorage) Set(ctx context.Context, txn isql.Txn, status string) err
240
201
return nil
241
202
}
242
203
243
- var getJobStatusQuery = mustParseOne (
244
- "SELECT written, status FROM system.job_status WHERE job_id = $1" ,
245
- )
246
-
247
204
// Get gets the current status mesasge for a job, if any.
248
205
func (i StatusStorage ) Get (ctx context.Context , txn isql.Txn ) (string , time.Time , error ) {
249
206
ctx , sp := tracing .ChildSpan (ctx , "get-job-status" )
250
207
defer sp .Finish ()
251
208
252
- row , err := txn .QueryRowExParsed (
209
+ row , err := txn .QueryRowEx (
253
210
ctx , "job-status-get" , txn .KV (), sessiondata .NodeUserSessionDataOverride ,
254
- getJobStatusQuery ,
211
+ "SELECT written, status FROM system.job_status WHERE job_id = $1" ,
255
212
i ,
256
213
)
257
214
@@ -292,17 +249,6 @@ func (j *Job) Messages() MessageStorage {
292
249
return MessageStorage (j .id )
293
250
}
294
251
295
- var (
296
- upsertJobMessageStmt = mustParseOne (
297
- `UPSERT INTO system.job_message (job_id, written, kind, message) VALUES ($1, now(),$2, $3)` ,
298
- )
299
- pruneJobMessageStmt = mustParseOne (
300
- `DELETE FROM system.job_message WHERE job_id = $1 AND kind = $2 AND written IN (
301
- SELECT written FROM system.job_message WHERE job_id = $1 AND kind = $2 ORDER BY written DESC OFFSET $3
302
- )` ,
303
- )
304
- )
305
-
306
252
// Record writes a human readable message of the specified kind to the message
307
253
// log for this job, and prunes retained messages of the same kind based on the
308
254
// configured limit to keep the total number of retained messages bounded.
@@ -311,18 +257,20 @@ func (i MessageStorage) Record(ctx context.Context, txn isql.Txn, kind, message
311
257
defer sp .Finish ()
312
258
313
259
// Insert the new message.
314
- if _ , err := txn .ExecParsed (
260
+ if _ , err := txn .ExecEx (
315
261
ctx , "write-job-message-insert" , txn .KV (), sessiondata .NodeUserSessionDataOverride ,
316
- upsertJobMessageStmt ,
262
+ `UPSERT INTO system.job_message (job_id, written, kind, message) VALUES ($1, now(),$2, $3)` ,
317
263
i , kind , message ,
318
264
); err != nil {
319
265
return err
320
266
}
321
267
322
268
// Prune old messages of the same kind to bound historical data.
323
- if _ , err := txn .ExecParsed (
269
+ if _ , err := txn .ExecEx (
324
270
ctx , "write-job-message-prune" , txn .KV (), sessiondata .NodeUserSessionDataOverride ,
325
- pruneJobMessageStmt ,
271
+ `DELETE FROM system.job_message WHERE job_id = $1 AND kind = $2 AND written IN (
272
+ SELECT written FROM system.job_message WHERE job_id = $1 AND kind = $2 ORDER BY written DESC OFFSET $3
273
+ )` ,
326
274
i , kind , retainedMessageHistory .Get (txn .KV ().DB ().SettingsValues ()),
327
275
); err != nil {
328
276
return err
@@ -404,18 +352,14 @@ func InfoStorageForJob(txn isql.Txn, jobID jobspb.JobID) InfoStorage {
404
352
return InfoStorage {j : & Job {id : jobID }, txn : txn }
405
353
}
406
354
407
- var checkClaimSessionQuery = mustParseOne (
408
- `SELECT claim_session_id FROM system.jobs WHERE id = $1` ,
409
- )
410
-
411
355
func (i * InfoStorage ) checkClaimSession (ctx context.Context ) error {
412
356
if i .claimChecked {
413
357
return nil
414
358
}
415
359
416
- row , err := i .txn .QueryRowExParsed (ctx , "check-claim-session" , i .txn .KV (),
360
+ row , err := i .txn .QueryRowEx (ctx , "check-claim-session" , i .txn .KV (),
417
361
sessiondata .NodeUserSessionDataOverride ,
418
- checkClaimSessionQuery , i .j .ID ())
362
+ `SELECT claim_session_id FROM system.jobs WHERE id = $1` , i .j .ID ())
419
363
if err != nil {
420
364
return err
421
365
}
@@ -435,10 +379,6 @@ func (i *InfoStorage) checkClaimSession(ctx context.Context) error {
435
379
return nil
436
380
}
437
381
438
- var getJobInfoQuery = mustParseOne (
439
- "SELECT value FROM system.job_info WHERE job_id = $1 AND info_key::string = $2 ORDER BY written DESC LIMIT 1" ,
440
- )
441
-
442
382
func (i InfoStorage ) get (ctx context.Context , opName , infoKey string ) ([]byte , bool , error ) {
443
383
if i .txn == nil {
444
384
return nil , false , errors .New ("cannot access the job info table without an associated txn" )
@@ -452,10 +392,10 @@ func (i InfoStorage) get(ctx context.Context, opName, infoKey string) ([]byte, b
452
392
// We expect there to be only a single row for a given <job_id, info_key>.
453
393
// This is because all older revisions are deleted before a new one is
454
394
// inserted in `InfoStorage.Write`.
455
- row , err := i .txn .QueryRowExParsed (
395
+ row , err := i .txn .QueryRowEx (
456
396
ctx , "job-info-get" , i .txn .KV (),
457
397
sessiondata .NodeUserSessionDataOverride ,
458
- getJobInfoQuery ,
398
+ "SELECT value FROM system.job_info WHERE job_id = $1 AND info_key::string = $2 ORDER BY written DESC LIMIT 1" ,
459
399
j .ID (), infoKey ,
460
400
)
461
401
@@ -475,22 +415,13 @@ func (i InfoStorage) get(ctx context.Context, opName, infoKey string) ([]byte, b
475
415
return []byte (* value ), true , nil
476
416
}
477
417
478
- var (
479
- deleteJobInfoStmt = mustParseOne (
480
- `DELETE FROM system.job_info WHERE job_id = $1 AND info_key::string = $2` ,
481
- )
482
- insertJobInfoStmt = mustParseOne (
483
- `INSERT INTO system.job_info (job_id, info_key, written, value) VALUES ($1, $2, now(), $3)` ,
484
- )
485
- )
486
-
487
418
func (i InfoStorage ) write (ctx context.Context , infoKey string , value []byte ) error {
488
419
return i .doWrite (ctx , func (ctx context.Context , j * Job , txn isql.Txn ) error {
489
420
// First clear out any older revisions of this info.
490
- _ , err := txn .ExecParsed (
421
+ _ , err := txn .ExecEx (
491
422
ctx , "write-job-info-delete" , txn .KV (),
492
423
sessiondata .NodeUserSessionDataOverride ,
493
- deleteJobInfoStmt ,
424
+ "DELETE FROM system.job_info WHERE job_id = $1 AND info_key::string = $2" ,
494
425
j .ID (), infoKey ,
495
426
)
496
427
if err != nil {
@@ -502,10 +433,10 @@ func (i InfoStorage) write(ctx context.Context, infoKey string, value []byte) er
502
433
return nil
503
434
}
504
435
// Write the new info, using the same transaction.
505
- _ , err = txn .ExecParsed (
436
+ _ , err = txn .ExecEx (
506
437
ctx , "write-job-info-insert" , txn .KV (),
507
438
sessiondata .NodeUserSessionDataOverride ,
508
- insertJobInfoStmt ,
439
+ `INSERT INTO system.job_info (job_id, info_key, written, value) VALUES ($1, $2, now(), $3)` ,
509
440
j .ID (), infoKey , value ,
510
441
)
511
442
return err
@@ -625,46 +556,33 @@ func (i InfoStorage) Delete(ctx context.Context, infoKey string) error {
625
556
return i .write (ctx , infoKey , nil /* value */ )
626
557
}
627
558
628
- var (
629
- deleteJobInfoRangeLimitStmt = mustParseOne (
630
- `DELETE FROM system.job_info WHERE job_id = $1 AND info_key >= $2 AND info_key < $3
631
- ORDER BY info_key ASC LIMIT $4` ,
632
- )
633
- deleteJobInfoRangeStmt = mustParseOne (
634
- "DELETE FROM system.job_info WHERE job_id = $1 AND info_key >= $2 AND info_key < $3" ,
635
- )
636
- )
637
-
638
559
// DeleteRange removes the info records between the provided
639
560
// start key (inclusive) and end key (exclusive).
640
561
func (i InfoStorage ) DeleteRange (
641
562
ctx context.Context , startInfoKey , endInfoKey string , limit int ,
642
563
) error {
643
564
return i .doWrite (ctx , func (ctx context.Context , j * Job , txn isql.Txn ) error {
644
565
if limit > 0 {
645
- _ , err := txn .ExecParsed (
646
- ctx , "write-job-info-delete-limit " , txn .KV (),
566
+ _ , err := txn .ExecEx (
567
+ ctx , "write-job-info-delete" , txn .KV (),
647
568
sessiondata .NodeUserSessionDataOverride ,
648
- deleteJobInfoRangeLimitStmt ,
569
+ "DELETE FROM system.job_info WHERE job_id = $1 AND info_key >= $2 AND info_key < $3 " +
570
+ "ORDER BY info_key ASC LIMIT $4" ,
649
571
j .ID (), startInfoKey , endInfoKey , limit ,
650
572
)
651
573
return err
652
574
} else {
653
- _ , err := txn .ExecParsed (
575
+ _ , err := txn .ExecEx (
654
576
ctx , "write-job-info-delete" , txn .KV (),
655
577
sessiondata .NodeUserSessionDataOverride ,
656
- deleteJobInfoRangeStmt ,
578
+ "DELETE FROM system.job_info WHERE job_id = $1 AND info_key >= $2 AND info_key < $3" ,
657
579
j .ID (), startInfoKey , endInfoKey ,
658
580
)
659
581
return err
660
582
}
661
583
})
662
584
}
663
585
664
- var getJobInfoCountQuery = mustParseOne (
665
- "SELECT count(*) FROM system.job_info WHERE job_id = $1 AND info_key >= $2 AND info_key < $3" ,
666
- )
667
-
668
586
// Count counts the info records in the range [start, end).
669
587
func (i InfoStorage ) Count (ctx context.Context , startInfoKey , endInfoKey string ) (int , error ) {
670
588
if i .txn == nil {
@@ -674,10 +592,10 @@ func (i InfoStorage) Count(ctx context.Context, startInfoKey, endInfoKey string)
674
592
ctx , sp := tracing .ChildSpan (ctx , "count-job-info" )
675
593
defer sp .Finish ()
676
594
677
- row , err := i .txn .QueryRowExParsed (
595
+ row , err := i .txn .QueryRowEx (
678
596
ctx , "job-info-count" , i .txn .KV (),
679
597
sessiondata .NodeUserSessionDataOverride ,
680
- getJobInfoCountQuery ,
598
+ "SELECT count(*) FROM system.job_info WHERE job_id = $1 AND info_key >= $2 AND info_key < $3" ,
681
599
i .j .ID (), startInfoKey , endInfoKey ,
682
600
)
683
601
0 commit comments