-
Notifications
You must be signed in to change notification settings - Fork 848
/
job.c
1530 lines (1316 loc) · 47.6 KB
/
job.c
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file and its contents are licensed under the Apache License 2.0.
* Please see the included NOTICE for copyright information and
* LICENSE-APACHE for a copy of the license.
*/
#include <postgres.h>
#include <miscadmin.h>
#include <pgstat.h>
#include <access/xact.h>
#include <catalog/pg_authid.h>
#include <nodes/makefuncs.h>
#include <parser/parse_func.h>
#include <parser/parser.h>
#include <postmaster/bgworker.h>
#include <storage/ipc.h>
#include <tcop/tcopprot.h>
#include <utils/builtins.h>
#include <utils/memutils.h>
#include <utils/syscache.h>
#include <utils/timestamp.h>
#include <storage/lock.h>
#include <storage/proc.h>
#include <storage/procarray.h>
#include <storage/sinvaladt.h>
#include <utils/acl.h>
#include <utils/elog.h>
#include <executor/execdebug.h>
#include <executor/instrument.h>
#include <utils/jsonb.h>
#include <utils/snapmgr.h>
#include <unistd.h>
#include "job.h"
#include "config.h"
#include "scanner.h"
#include "extension.h"
#include "compat/compat.h"
#include "job_stat.h"
#include "license_guc.h"
#include "utils.h"
#ifdef USE_TELEMETRY
#include "telemetry/telemetry.h"
#endif
#include "bgw_policy/chunk_stats.h"
#include "bgw_policy/policy.h"
#include "scan_iterator.h"
#include "bgw/scheduler.h"
#include <cross_module_fn.h>
#include "jsonb_utils.h"
#include "debug_assert.h"
static scheduler_test_hook_type scheduler_test_hook = NULL;
static char *job_entrypoint_function_name = "ts_bgw_job_entrypoint";
typedef enum JobLockLifetime
{
SESSION_LOCK = 0,
TXN_LOCK,
} JobLockLifetime;
BackgroundWorkerHandle *
ts_bgw_job_start(BgwJob *job, Oid user_oid)
{
BgwParams bgw_params = {
.job_id = Int32GetDatum(job->fd.id),
.user_oid = user_oid,
};
strlcpy(bgw_params.bgw_main, job_entrypoint_function_name, sizeof(bgw_params.bgw_main));
return ts_bgw_start_worker(NameStr(job->fd.application_name), &bgw_params);
}
static void
job_execute_function(FuncExpr *funcexpr)
{
bool isnull;
EState *estate = CreateExecutorState();
ExprContext *econtext = CreateExprContext(estate);
ExprState *es = ExecPrepareExpr((Expr *) funcexpr, estate);
ExecEvalExpr(es, econtext, &isnull);
FreeExprContext(econtext, true);
FreeExecutorState(estate);
}
/**
* Run configuration check validation function.
*
* This will run the configuration check validation function registered for
* the job. If a new job is added, the job_id is going to be zero.
*/
void
ts_bgw_job_run_config_check(Oid check, int32 job_id, Jsonb *config)
{
/* Nothing to check if there is no check function provided */
if (!OidIsValid(check))
return;
/* NULL config may be valid */
Const *arg;
if (config == NULL)
arg = makeNullConst(JSONBOID, -1, InvalidOid);
else
arg = makeConst(JSONBOID, -1, InvalidOid, -1, JsonbPGetDatum(config), false, false);
List *args = list_make1(arg);
FuncExpr *funcexpr =
makeFuncExpr(check, VOIDOID, args, InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL);
if (get_func_prokind(check) == PROKIND_FUNCTION)
job_execute_function(funcexpr);
else
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("unsupported function type"),
errdetail("Only functions are allowed as custom configuration checks"),
errhint("Use a FUNCTION instead")));
}
/* Run the check function on a configuration. It will generate errors if there
* is anything wrong with the configuration, otherwise just return. If the
* check function does not exist, no checking will be done.*/
static void
job_config_check(BgwJob *job, Jsonb *config)
{
Oid proc;
List *funcname;
/* Both should either be empty or contain a schema and name */
Assert((strlen(NameStr(job->fd.check_schema)) == 0) ==
(strlen(NameStr(job->fd.check_schema)) == 0));
/* If there is no function, just return */
if (strlen(NameStr(job->fd.check_name)) == 0)
return;
funcname = list_make2(makeString(NameStr(job->fd.check_schema)),
makeString(NameStr(job->fd.check_name)));
Oid argtypes[] = { JSONBOID };
/* Only functions allowed as custom checks, as procedures can cause errors with COMMIT
* statements */
proc = LookupFuncName(funcname, 1, argtypes, true);
/* a check function has been registered but it can't be found anymore
because it was dropped or renamed. Allow alter_job to run if that's the case
without validating the config but also print a warning */
if (OidIsValid(proc))
ts_bgw_job_run_config_check(proc, job->fd.id, config);
else
elog(WARNING,
"function %s.%s(config jsonb) not found, skipping config validation for "
"job %d",
NameStr(job->fd.check_schema),
NameStr(job->fd.check_name),
job->fd.id);
}
/* this function fills in a jsonb with the non-null fields of
the error data and also includes the proc name and schema in the jsonb
we include these here to avoid adding these fields to the table */
static Jsonb *
ts_errdata_to_jsonb(ErrorData *edata, Name proc_schema, Name proc_name)
{
JsonbParseState *parse_state = NULL;
pushJsonbValue(&parse_state, WJB_BEGIN_OBJECT, NULL);
if (edata->sqlerrcode)
ts_jsonb_add_str(parse_state, "sqlerrcode", unpack_sql_state(edata->sqlerrcode));
if (edata->message)
ts_jsonb_add_str(parse_state, "message", edata->message);
if (edata->detail)
ts_jsonb_add_str(parse_state, "detail", edata->detail);
if (edata->hint)
ts_jsonb_add_str(parse_state, "hint", edata->hint);
if (edata->filename)
ts_jsonb_add_str(parse_state, "filename", edata->filename);
if (edata->lineno)
ts_jsonb_add_int32(parse_state, "lineno", edata->lineno);
if (edata->funcname)
ts_jsonb_add_str(parse_state, "funcname", edata->funcname);
if (edata->domain)
ts_jsonb_add_str(parse_state, "domain", edata->domain);
if (edata->context_domain)
ts_jsonb_add_str(parse_state, "context_domain", edata->context_domain);
if (edata->context)
ts_jsonb_add_str(parse_state, "context", edata->context);
if (edata->schema_name)
ts_jsonb_add_str(parse_state, "schema_name", edata->schema_name);
if (edata->table_name)
ts_jsonb_add_str(parse_state, "table_name", edata->table_name);
if (edata->column_name)
ts_jsonb_add_str(parse_state, "column_name", edata->column_name);
if (edata->datatype_name)
ts_jsonb_add_str(parse_state, "datatype_name", edata->datatype_name);
if (edata->constraint_name)
ts_jsonb_add_str(parse_state, "constraint_name", edata->constraint_name);
if (edata->internalquery)
ts_jsonb_add_str(parse_state, "internalquery", edata->internalquery);
if (edata->detail_log)
ts_jsonb_add_str(parse_state, "detail_log", edata->detail_log);
if (strlen(NameStr(*proc_schema)) > 0)
ts_jsonb_add_str(parse_state, "proc_schema", NameStr(*proc_schema));
if (strlen(NameStr(*proc_name)) > 0)
ts_jsonb_add_str(parse_state, "proc_name", NameStr(*proc_name));
/* we add the schema qualified name here as well*/
JsonbValue *result = pushJsonbValue(&parse_state, WJB_END_OBJECT, NULL);
return JsonbValueToJsonb(result);
}
static BgwJob *
bgw_job_from_tupleinfo(TupleInfo *ti, size_t alloc_size)
{
BgwJob *job;
bool should_free;
HeapTuple tuple;
MemoryContext old_ctx;
Datum values[Natts_bgw_job] = { 0 };
bool nulls[Natts_bgw_job] = { false };
/*
* allow for embedding with arbitrary alloc_size, which means we can't use
* the STRUCT_FROM_TUPLE macro
*/
Assert(alloc_size >= sizeof(BgwJob));
job = MemoryContextAllocZero(ti->mctx, alloc_size);
tuple = ts_scanner_fetch_heap_tuple(ti, false, &should_free);
old_ctx = MemoryContextSwitchTo(ti->mctx);
/*
* Using heap_deform_tuple instead of GETSTRUCT since the tuple can
* contain NULL values. Some of these cannot really be null, but we check
* anyway since it is cheap and will avoid problems in the future. Note
* that the job structure is zeroed, so we only need to update the field
* if it is non-NULL.
*/
heap_deform_tuple(tuple, ts_scanner_get_tupledesc(ti), values, nulls);
if (!nulls[AttrNumberGetAttrOffset(Anum_bgw_job_id)])
job->fd.id = DatumGetInt32(values[AttrNumberGetAttrOffset(Anum_bgw_job_id)]);
if (!nulls[AttrNumberGetAttrOffset(Anum_bgw_job_application_name)])
namestrcpy(&job->fd.application_name,
NameStr(*DatumGetName(
values[AttrNumberGetAttrOffset(Anum_bgw_job_application_name)])));
if (!nulls[AttrNumberGetAttrOffset(Anum_bgw_job_schedule_interval)])
job->fd.schedule_interval =
*DatumGetIntervalP(values[AttrNumberGetAttrOffset(Anum_bgw_job_schedule_interval)]);
if (!nulls[AttrNumberGetAttrOffset(Anum_bgw_job_max_runtime)])
job->fd.max_runtime =
*DatumGetIntervalP(values[AttrNumberGetAttrOffset(Anum_bgw_job_max_runtime)]);
if (!nulls[AttrNumberGetAttrOffset(Anum_bgw_job_max_retries)])
job->fd.max_retries =
DatumGetInt32(values[AttrNumberGetAttrOffset(Anum_bgw_job_max_retries)]);
if (!nulls[AttrNumberGetAttrOffset(Anum_bgw_job_fixed_schedule)])
job->fd.fixed_schedule =
DatumGetBool(values[AttrNumberGetAttrOffset(Anum_bgw_job_fixed_schedule)]);
if (!nulls[AttrNumberGetAttrOffset(Anum_bgw_job_initial_start)])
{
job->fd.initial_start =
DatumGetTimestampTz(values[AttrNumberGetAttrOffset(Anum_bgw_job_initial_start)]);
}
else
job->fd.initial_start = DT_NOBEGIN;
if (!nulls[AttrNumberGetAttrOffset(Anum_bgw_job_timezone)])
job->fd.timezone =
DatumGetTextPCopy(values[AttrNumberGetAttrOffset(Anum_bgw_job_timezone)]);
if (!nulls[AttrNumberGetAttrOffset(Anum_bgw_job_retry_period)])
job->fd.retry_period =
*DatumGetIntervalP(values[AttrNumberGetAttrOffset(Anum_bgw_job_retry_period)]);
if (!nulls[AttrNumberGetAttrOffset(Anum_bgw_job_proc_schema)])
namestrcpy(&job->fd.proc_schema,
NameStr(
*DatumGetName(values[AttrNumberGetAttrOffset(Anum_bgw_job_proc_schema)])));
if (!nulls[AttrNumberGetAttrOffset(Anum_bgw_job_proc_name)])
namestrcpy(&job->fd.proc_name,
NameStr(*DatumGetName(values[AttrNumberGetAttrOffset(Anum_bgw_job_proc_name)])));
if (!nulls[AttrNumberGetAttrOffset(Anum_bgw_job_check_schema)])
namestrcpy(&job->fd.check_schema,
NameStr(
*DatumGetName(values[AttrNumberGetAttrOffset(Anum_bgw_job_check_schema)])));
if (!nulls[AttrNumberGetAttrOffset(Anum_bgw_job_check_name)])
namestrcpy(&job->fd.check_name,
NameStr(
*DatumGetName(values[AttrNumberGetAttrOffset(Anum_bgw_job_check_name)])));
if (!nulls[AttrNumberGetAttrOffset(Anum_bgw_job_owner)])
job->fd.owner = DatumGetObjectId(values[AttrNumberGetAttrOffset(Anum_bgw_job_owner)]);
if (!nulls[AttrNumberGetAttrOffset(Anum_bgw_job_scheduled)])
job->fd.scheduled = DatumGetBool(values[AttrNumberGetAttrOffset(Anum_bgw_job_scheduled)]);
if (!nulls[AttrNumberGetAttrOffset(Anum_bgw_job_hypertable_id)])
job->fd.hypertable_id =
DatumGetInt32(values[AttrNumberGetAttrOffset(Anum_bgw_job_hypertable_id)]);
if (!nulls[AttrNumberGetAttrOffset(Anum_bgw_job_config)])
job->fd.config = DatumGetJsonbPCopy(values[AttrNumberGetAttrOffset(Anum_bgw_job_config)]);
MemoryContextSwitchTo(old_ctx);
if (should_free)
heap_freetuple(tuple);
return job;
}
typedef struct AccumData
{
List *list;
size_t alloc_size;
} AccumData;
static ScanTupleResult
bgw_job_accum_tuple_found(TupleInfo *ti, void *data)
{
AccumData *list_data = data;
BgwJob *job = bgw_job_from_tupleinfo(ti, list_data->alloc_size);
MemoryContext orig = MemoryContextSwitchTo(ti->mctx);
list_data->list = lappend(list_data->list, job);
MemoryContextSwitchTo(orig);
return SCAN_CONTINUE;
}
static ScanFilterResult
bgw_job_filter_scheduled(const TupleInfo *ti, void *data)
{
bool isnull;
Datum scheduled = slot_getattr(ti->slot, Anum_bgw_job_scheduled, &isnull);
Ensure(!isnull, "scheduled column was null");
return DatumGetBool(scheduled);
}
/* This function is meant to be used by the scheduler only
* it does not include the config field which saves us from
* detoasting and makes memory management in the scheduler
* simpler as otherwise the config field would have to be
* freed separately when freeing jobs which would prevent
* the use of list_free_deep.
* The scheduler does not need the config field only the
* individual jobs do.
* The scheduler requires jobs to be sorted by id
* which is guaranteed by the index scan on the primary key
*/
List *
ts_bgw_job_get_scheduled(size_t alloc_size, MemoryContext mctx)
{
MemoryContext old_ctx;
List *jobs = NIL;
ScanIterator iterator = ts_scan_iterator_create(BGW_JOB, AccessShareLock, mctx);
iterator.ctx.index = catalog_get_index(ts_catalog_get(), BGW_JOB, BGW_JOB_PKEY_IDX);
iterator.ctx.filter = bgw_job_filter_scheduled;
ts_scanner_foreach(&iterator)
{
TupleInfo *ti = ts_scan_iterator_tuple_info(&iterator);
bool should_free, isnull, initial_start_isnull, timezone_isnull;
Datum value, initial_start, timezone;
BgwJob *job = MemoryContextAllocZero(mctx, alloc_size);
HeapTuple tuple = ts_scanner_fetch_heap_tuple(ti, false, &should_free);
/*
* Note that the nullable columns might have variable width, so we
* handle them below. We can only use memcpy for the non-nullable fixed
* width starting part of the BgwJob struct.
*/
memcpy(job, GETSTRUCT(tuple), offsetof(FormData_bgw_job, fixed_schedule));
if (should_free)
heap_freetuple(tuple);
#ifdef USE_TELEMETRY
/* ignore telemetry jobs if telemetry is disabled */
if (!ts_telemetry_on() && ts_is_telemetry_job(job))
{
pfree(job);
continue;
}
#endif
/* handle NULL columns */
value = slot_getattr(ti->slot, Anum_bgw_job_hypertable_id, &isnull);
job->fd.hypertable_id = isnull ? 0 : DatumGetInt32(value);
initial_start = slot_getattr(ti->slot, Anum_bgw_job_initial_start, &initial_start_isnull);
if (!initial_start_isnull)
job->fd.initial_start = DatumGetTimestampTz(initial_start);
else
job->fd.initial_start = DT_NOBEGIN;
timezone = slot_getattr(ti->slot, Anum_bgw_job_timezone, &timezone_isnull);
if (!timezone_isnull)
job->fd.timezone = DatumGetTextPP(timezone);
else
job->fd.timezone = NULL;
/* We skip config, check_name, and check_schema since the scheduler
* doesn't need these, it saves us from detoasting, and simplifies
* freeing job lists in the scheduler as otherwise the config field
* would have to be freed separately when freeing a job. */
job->fd.config = NULL;
old_ctx = MemoryContextSwitchTo(mctx);
jobs = lappend(jobs, job);
MemoryContextSwitchTo(old_ctx);
}
return jobs;
}
List *
ts_bgw_job_get_all(size_t alloc_size, MemoryContext mctx)
{
Catalog *catalog = ts_catalog_get();
AccumData list_data = {
.list = NIL,
.alloc_size = sizeof(BgwJob),
};
ScannerCtx scanctx = {
.table = catalog_get_table_id(catalog, BGW_JOB),
.data = &list_data,
.tuple_found = bgw_job_accum_tuple_found,
.lockmode = AccessShareLock,
.result_mctx = mctx,
.scandirection = ForwardScanDirection,
};
ts_scanner_scan(&scanctx);
return list_data.list;
}
static void
init_scan_by_proc_name(ScanKeyData *scankey, const char *proc_name)
{
ScanKeyInit(scankey,
Anum_bgw_job_proc_hypertable_id_idx_proc_name,
BTEqualStrategyNumber,
F_NAMEEQ,
CStringGetDatum(proc_name));
}
static void
init_scan_by_proc_schema(ScanKeyData *scankey, const char *proc_schema)
{
ScanKeyInit(scankey,
Anum_bgw_job_proc_hypertable_id_idx_proc_schema,
BTEqualStrategyNumber,
F_NAMEEQ,
CStringGetDatum(proc_schema));
}
static void
init_scan_by_hypertable_id(ScanKeyData *scankey, int32 hypertable_id)
{
ScanKeyInit(scankey,
Anum_bgw_job_proc_hypertable_id_idx_hypertable_id,
BTEqualStrategyNumber,
F_INT4EQ,
Int32GetDatum(hypertable_id));
}
List *
ts_bgw_job_find_by_proc_and_hypertable_id(const char *proc_name, const char *proc_schema,
int32 hypertable_id)
{
Catalog *catalog = ts_catalog_get();
ScanKeyData scankey[3];
AccumData list_data = {
.list = NIL,
.alloc_size = sizeof(BgwJob),
};
ScannerCtx scanctx = {
.table = catalog_get_table_id(catalog, BGW_JOB),
.index = catalog_get_index(ts_catalog_get(), BGW_JOB, BGW_JOB_PROC_HYPERTABLE_ID_IDX),
.data = &list_data,
.scankey = scankey,
.nkeys = sizeof(scankey) / sizeof(*scankey),
.tuple_found = bgw_job_accum_tuple_found,
.lockmode = AccessShareLock,
.scandirection = ForwardScanDirection,
};
init_scan_by_proc_schema(&scankey[0], proc_schema);
init_scan_by_proc_name(&scankey[1], proc_name);
init_scan_by_hypertable_id(&scankey[2], hypertable_id);
ts_scanner_scan(&scanctx);
return list_data.list;
}
List *
ts_bgw_job_find_by_proc(const char *proc_name, const char *proc_schema)
{
Catalog *catalog = ts_catalog_get();
ScanKeyData scankey[2];
AccumData list_data = {
.list = NIL,
.alloc_size = sizeof(BgwJob),
};
ScannerCtx scanctx = {
.table = catalog_get_table_id(catalog, BGW_JOB),
.index = catalog_get_index(ts_catalog_get(), BGW_JOB, BGW_JOB_PROC_HYPERTABLE_ID_IDX),
.data = &list_data,
.scankey = scankey,
.nkeys = sizeof(scankey) / sizeof(*scankey),
.tuple_found = bgw_job_accum_tuple_found,
.lockmode = AccessShareLock,
.scandirection = ForwardScanDirection,
};
init_scan_by_proc_schema(&scankey[0], proc_schema);
init_scan_by_proc_name(&scankey[1], proc_name);
ts_scanner_scan(&scanctx);
return list_data.list;
}
List *
ts_bgw_job_find_by_hypertable_id(int32 hypertable_id)
{
Catalog *catalog = ts_catalog_get();
ScanKeyData scankey[1];
AccumData list_data = {
.list = NIL,
.alloc_size = sizeof(BgwJob),
};
ScannerCtx scanctx = {
.table = catalog_get_table_id(catalog, BGW_JOB),
.index = catalog_get_index(ts_catalog_get(), BGW_JOB, BGW_JOB_PROC_HYPERTABLE_ID_IDX),
.data = &list_data,
.scankey = scankey,
.nkeys = sizeof(scankey) / sizeof(*scankey),
.tuple_found = bgw_job_accum_tuple_found,
.lockmode = AccessShareLock,
.scandirection = ForwardScanDirection,
};
init_scan_by_hypertable_id(&scankey[0], hypertable_id);
ts_scanner_scan(&scanctx);
return list_data.list;
}
static void
init_scan_by_job_id(ScanIterator *iterator, int32 job_id)
{
iterator->ctx.index = catalog_get_index(ts_catalog_get(), BGW_JOB, BGW_JOB_PKEY_IDX);
ts_scan_iterator_scan_key_init(iterator,
Anum_bgw_job_pkey_idx_id,
BTEqualStrategyNumber,
F_INT4EQ,
Int32GetDatum(job_id));
}
/* Lock a job tuple using an advisory lock. Advisory job locks are used to
* lock the job row while a job is running to prevent a job from being
* modified while in the middle of a run. This lock should be taken before
* bgw_job table lock to avoid deadlocks.
*
* We use an advisory lock instead of a tuple lock because we want the lock on
* the job id and not on the tid of the row (in case it is vacuumed or updated
* in some way). We don't want the job modified while it is running for safety
* reasons. Finally, we use this lock to be able to send a signal to the PID
* of the running job. This is used by delete because, a job deletion sends a
* SIGINT to the running job to cancel it.
*
* We acquire a SHARE lock on the job during scheduling and when the job is
* running so that it cannot be deleted during those times and an EXCLUSIVE
* lock when deleting.
*
* returns whether or not the lock was obtained (false return only possible if
* block==false)
*/
bool
ts_lock_job_id(int32 job_id, LOCKMODE mode, bool session_lock, LOCKTAG *tag, bool block)
{
/* Use a special pseudo-random field 4 value to avoid conflicting with user-advisory-locks */
TS_SET_LOCKTAG_ADVISORY(*tag, MyDatabaseId, job_id, 0);
return LockAcquire(tag, mode, session_lock, !block) != LOCKACQUIRE_NOT_AVAIL;
}
static BgwJob *
ts_bgw_job_find_with_lock(int32 bgw_job_id, MemoryContext mctx, LOCKMODE tuple_lock_mode,
JobLockLifetime lock_type, bool block, bool *got_lock)
{
/* Take a share lock on the table to prevent concurrent data changes during scan. This lock will
* be released after the scan */
ScanIterator iterator = ts_scan_iterator_create(BGW_JOB, ShareLock, mctx);
List *jobs = NIL;
BgwJob *job = NULL;
LOCKTAG tag;
/* take advisory lock before relation lock */
if (!(*got_lock =
ts_lock_job_id(bgw_job_id, tuple_lock_mode, lock_type == SESSION_LOCK, &tag, block)))
{
/* return NULL if lock could not be acquired */
Assert(!block);
return NULL;
}
init_scan_by_job_id(&iterator, bgw_job_id);
ts_scanner_foreach(&iterator)
{
TupleInfo *ti = ts_scan_iterator_tuple_info(&iterator);
job = bgw_job_from_tupleinfo(ti, sizeof(BgwJob));
jobs = lappend(jobs, job);
}
if (list_length(jobs) > 1)
{
ListCell *cell;
foreach (cell, jobs)
{
BgwJob *job = (BgwJob *) lfirst(cell);
ereport(LOG,
(errmsg("more than one job with same job_id %d", bgw_job_id),
errdetail("job_id: %d, application_name: %s, procedure: %s.%s, scheduled: %s",
job->fd.id,
NameStr(job->fd.application_name),
quote_identifier(NameStr(job->fd.proc_schema)),
quote_identifier(NameStr(job->fd.proc_name)),
job->fd.scheduled ? "true" : "false")));
}
}
/* We don't care about duplicate jobs in release builds and will take the
* last job */
Assert(list_length(jobs) <= 1);
return job;
}
/* Take a lock on the job for the duration of the txn. This prevents
* the job from being deleted.
*
* Returns true if the job is found ( we block till we can acquire a lock
* so we will always lock here)
* false if the job is missing.
*/
bool
ts_bgw_job_get_share_lock(int32 bgw_job_id, MemoryContext mctx)
{
bool got_lock;
/* note the mode here is equivalent to FOR SHARE row locks */
BgwJob *job = ts_bgw_job_find_with_lock(bgw_job_id,
mctx,
RowShareLock,
TXN_LOCK,
true, /* block */
&got_lock);
if (job != NULL)
{
if (!got_lock)
{
/* since we blocked for a lock , this is an unexpected condition */
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("could not acquire share lock for job=%d", bgw_job_id)));
}
pfree(job);
return true;
}
return false;
}
BgwJob *
ts_bgw_job_find(int32 bgw_job_id, MemoryContext mctx, bool fail_if_not_found)
{
ScanIterator iterator = ts_scan_iterator_create(BGW_JOB, AccessShareLock, mctx);
int num_found = 0;
BgwJob *job = NULL;
init_scan_by_job_id(&iterator, bgw_job_id);
ts_scanner_foreach(&iterator)
{
Assert(num_found == 0);
job = bgw_job_from_tupleinfo(ts_scan_iterator_tuple_info(&iterator), sizeof(BgwJob));
num_found++;
}
if (num_found == 0 && fail_if_not_found)
elog(ERROR, "job %d not found", bgw_job_id);
return job;
}
static void
get_job_lock_for_delete(int32 job_id)
{
LOCKTAG tag;
bool got_lock;
/* Try getting an exclusive lock on the tuple in a non-blocking manner. Note this is the
* equivalent of a row-based FOR UPDATE lock */
got_lock = ts_lock_job_id(job_id,
AccessExclusiveLock,
/* session_lock */ false,
&tag,
/* block */ false);
if (!got_lock)
{
/* If I couldn't get a lock, try killing the background worker that's running the job.
* This is probably not bulletproof but best-effort is good enough here. */
VirtualTransactionId *vxid = GetLockConflicts(&tag, AccessExclusiveLock, NULL);
PGPROC *proc;
if (VirtualTransactionIdIsValid(*vxid))
{
proc = BackendIdGetProc(vxid->backendId);
if (proc != NULL && proc->isBackgroundWorker)
{
/* Simply assuming that this pid corresponds to the background worker
* running the job is not sufficient. The scheduler could also be the
* one holding the lock, when transitioning the state of the job back
* to scheduled state. So we must check we don't kill the scheduler.
* See https://github.com/timescale/timescaledb/issues/5224
*/
const char *worker_name = GetBackgroundWorkerTypeByPid(proc->pid);
if (strcmp(worker_name, SCHEDULER_APPNAME) != 0)
{
elog(NOTICE,
"cancelling the background worker for job %d (pid %d)",
job_id,
proc->pid);
DirectFunctionCall1(pg_cancel_backend, Int32GetDatum(proc->pid));
}
}
}
/* We have to grab this lock before proceeding so grab it in a blocking manner now */
got_lock = ts_lock_job_id(job_id,
AccessExclusiveLock,
/* session_lock */ false,
&tag,
/* block */ true);
}
Ensure(got_lock, "unable to lock job id %d", job_id);
}
static ScanTupleResult
bgw_job_tuple_delete(TupleInfo *ti, void *data)
{
CatalogSecurityContext sec_ctx;
bool isnull_job_id;
Datum datum = slot_getattr(ti->slot, Anum_bgw_job_id, &isnull_job_id);
int32 job_id = DatumGetInt32(datum);
Ensure(!isnull_job_id, "job id was null");
/* Also delete the bgw_stat entry */
ts_bgw_job_stat_delete(job_id);
/* Delete any stats in bgw_policy_chunk_stats related to this job */
ts_bgw_policy_chunk_stats_delete_row_only_by_job_id(job_id);
ts_catalog_database_info_become_owner(ts_catalog_database_info_get(), &sec_ctx);
ts_catalog_delete_tid(ti->scanrel, ts_scanner_get_tuple_tid(ti));
ts_catalog_restore_user(&sec_ctx);
return SCAN_CONTINUE;
}
static bool
bgw_job_delete_scan(ScanKeyData *scankey, int32 job_id)
{
Catalog *catalog = ts_catalog_get();
ScannerCtx scanctx;
/* get job lock before relation lock */
get_job_lock_for_delete(job_id);
scanctx = (ScannerCtx){
.table = catalog_get_table_id(catalog, BGW_JOB),
.index = catalog_get_index(catalog, BGW_JOB, BGW_JOB_PKEY_IDX),
.nkeys = 1,
.scankey = scankey,
.data = NULL,
.limit = 1,
.tuple_found = bgw_job_tuple_delete,
.lockmode = RowExclusiveLock,
.scandirection = ForwardScanDirection,
.result_mctx = CurrentMemoryContext,
};
return ts_scanner_scan(&scanctx);
}
/*
* This function will try to delete the job identified by `job_id`. If the job is currently running,
* this function will send a `SIGINT` to the job, and wait for the job to terminate before deleting
* the job. In the event that it cannot send the signal (for instance, if the job is not in a
* transaction, we have no way to send the signal), it will still wait for the job to terminate and
* release the job lock, or will ERROR due to a lock or deadlock timeout. In this case, the user
* has to manually determine the `pid` of the BGW and send an `SIGINT` or a `SIGKILL`.
*/
bool
ts_bgw_job_delete_by_id(int32 job_id)
{
ScanKeyData scankey[1];
ScanKeyInit(&scankey[0],
Anum_bgw_job_pkey_idx_id,
BTEqualStrategyNumber,
F_INT4EQ,
Int32GetDatum(job_id));
return bgw_job_delete_scan(scankey, job_id);
}
/* This function only updates the fields modifiable with alter_job. */
static ScanTupleResult
bgw_job_tuple_update_by_id(TupleInfo *ti, void *const data)
{
BgwJob *updated_job = (BgwJob *) data;
bool should_free;
HeapTuple tuple = ts_scanner_fetch_heap_tuple(ti, false, &should_free);
HeapTuple new_tuple;
Datum values[Natts_bgw_job] = { 0 };
bool isnull[Natts_bgw_job] = { 0 };
bool doReplace[Natts_bgw_job] = { 0 };
Datum old_schedule_interval =
slot_getattr(ti->slot, Anum_bgw_job_schedule_interval, &isnull[0]);
Assert(!isnull[0]);
/* when we update the schedule interval, modify the next start time as well*/
if (!DatumGetBool(DirectFunctionCall2(interval_eq,
old_schedule_interval,
IntervalPGetDatum(&updated_job->fd.schedule_interval))))
{
BgwJobStat *stat = ts_bgw_job_stat_find(updated_job->fd.id);
if (stat != NULL)
{
TimestampTz next_start = DatumGetTimestampTz(
DirectFunctionCall2(timestamptz_pl_interval,
TimestampTzGetDatum(stat->fd.last_finish),
IntervalPGetDatum(&updated_job->fd.schedule_interval)));
/* allow DT_NOBEGIN for next_start here through allow_unset=true in the case that
* last_finish is DT_NOBEGIN,
* This means the value is counted as unset which is what we want */
ts_bgw_job_stat_update_next_start(updated_job->fd.id, next_start, true);
}
values[AttrNumberGetAttrOffset(Anum_bgw_job_schedule_interval)] =
IntervalPGetDatum(&updated_job->fd.schedule_interval);
doReplace[AttrNumberGetAttrOffset(Anum_bgw_job_schedule_interval)] = true;
}
values[AttrNumberGetAttrOffset(Anum_bgw_job_max_runtime)] =
IntervalPGetDatum(&updated_job->fd.max_runtime);
doReplace[AttrNumberGetAttrOffset(Anum_bgw_job_max_runtime)] = true;
values[AttrNumberGetAttrOffset(Anum_bgw_job_max_retries)] =
Int32GetDatum(updated_job->fd.max_retries);
doReplace[AttrNumberGetAttrOffset(Anum_bgw_job_max_retries)] = true;
values[AttrNumberGetAttrOffset(Anum_bgw_job_retry_period)] =
IntervalPGetDatum(&updated_job->fd.retry_period);
doReplace[AttrNumberGetAttrOffset(Anum_bgw_job_retry_period)] = true;
values[AttrNumberGetAttrOffset(Anum_bgw_job_scheduled)] =
BoolGetDatum(updated_job->fd.scheduled);
doReplace[AttrNumberGetAttrOffset(Anum_bgw_job_scheduled)] = true;
values[AttrNumberGetAttrOffset(Anum_bgw_job_fixed_schedule)] =
BoolGetDatum(updated_job->fd.fixed_schedule);
doReplace[AttrNumberGetAttrOffset(Anum_bgw_job_fixed_schedule)] = true;
doReplace[AttrNumberGetAttrOffset(Anum_bgw_job_config)] = true;
values[AttrNumberGetAttrOffset(Anum_bgw_job_check_schema)] =
NameGetDatum(&updated_job->fd.check_schema);
doReplace[AttrNumberGetAttrOffset(Anum_bgw_job_check_schema)] = true;
values[AttrNumberGetAttrOffset(Anum_bgw_job_check_name)] =
NameGetDatum(&updated_job->fd.check_name);
doReplace[AttrNumberGetAttrOffset(Anum_bgw_job_check_name)] = true;
if (strlen(NameStr(updated_job->fd.check_name)) == 0)
{
isnull[AttrNumberGetAttrOffset(Anum_bgw_job_check_name)] = true;
isnull[AttrNumberGetAttrOffset(Anum_bgw_job_check_schema)] = true;
}
if (updated_job->fd.config)
{
job_config_check(updated_job, updated_job->fd.config);
values[AttrNumberGetAttrOffset(Anum_bgw_job_config)] =
JsonbPGetDatum(updated_job->fd.config);
}
else
isnull[AttrNumberGetAttrOffset(Anum_bgw_job_config)] = true;
if (updated_job->fd.hypertable_id != 0)
{
values[AttrNumberGetAttrOffset(Anum_bgw_job_hypertable_id)] =
Int32GetDatum(updated_job->fd.hypertable_id);
doReplace[AttrNumberGetAttrOffset(Anum_bgw_job_hypertable_id)] = true;
}
else
isnull[AttrNumberGetAttrOffset(Anum_bgw_job_hypertable_id)] = true;
if (TIMESTAMP_NOT_FINITE(updated_job->fd.initial_start))
isnull[AttrNumberGetAttrOffset(Anum_bgw_job_initial_start)] = true;
else
values[AttrNumberGetAttrOffset(Anum_bgw_job_initial_start)] =
TimestampTzGetDatum(updated_job->fd.initial_start);
doReplace[AttrNumberGetAttrOffset(Anum_bgw_job_initial_start)] = true;
if (updated_job->fd.timezone)
{
values[AttrNumberGetAttrOffset(Anum_bgw_job_timezone)] =
PointerGetDatum(updated_job->fd.timezone);
}
else
isnull[AttrNumberGetAttrOffset(Anum_bgw_job_timezone)] = true;
doReplace[AttrNumberGetAttrOffset(Anum_bgw_job_timezone)] = true;
new_tuple = heap_modify_tuple(tuple, ts_scanner_get_tupledesc(ti), values, isnull, doReplace);
ts_catalog_update(ti->scanrel, new_tuple);
heap_freetuple(new_tuple);
if (should_free)
heap_freetuple(tuple);
return SCAN_DONE;
}
/*
* Overwrite job with specified job_id with the given fields
*
* This function only updates the fields modifiable with alter_job.
*/
bool
ts_bgw_job_update_by_id(int32 job_id, BgwJob *job)
{
ScanKeyData scankey[1];
Catalog *catalog = ts_catalog_get();
ScanTupLock scantuplock = {
.waitpolicy = LockWaitBlock,
.lockmode = LockTupleExclusive,
};
ScannerCtx scanctx = { .table = catalog_get_table_id(catalog, BGW_JOB),
.index = catalog_get_index(catalog, BGW_JOB, BGW_JOB_PKEY_IDX),
.nkeys = 1,
.scankey = scankey,
.data = job,
.limit = 1,
.tuple_found = bgw_job_tuple_update_by_id,
.lockmode = RowExclusiveLock,
.scandirection = ForwardScanDirection,
.result_mctx = CurrentMemoryContext,
.tuplock = &scantuplock };
ScanKeyInit(&scankey[0],
Anum_bgw_job_pkey_idx_id,
BTEqualStrategyNumber,
F_INT4EQ,
Int32GetDatum(job_id));
return ts_scanner_scan(&scanctx);
}
static void
ts_bgw_job_check_max_retries(BgwJob *job)
{
BgwJobStat *job_stat;
job_stat = ts_bgw_job_stat_find(job->fd.id);
/* stop to execute failing jobs after reached the "max_retries" option */
if (job->fd.max_retries > 0 && job_stat->fd.consecutive_failures >= job->fd.max_retries)
{
ereport(WARNING,
(errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("job %d reached max_retries after %d consecutive failures",
job->fd.id,
job_stat->fd.consecutive_failures),
errdetail("Job %d unscheduled as max_retries reached %d, consecutive failures %d.",
job->fd.id,