Skip to content

Commit a19747a

Browse files
svenklemmtimescale-automation
authored andcommitted
Don't error on failure to update job stat
Failing to update job stats should not result in the job producing an error instead this should be debug logged. This can happen when jobs concurrently with job stat retention policy. (cherry picked from commit 3176651)
1 parent cb77c74 commit a19747a

File tree

2 files changed

+31
-36
lines changed

2 files changed

+31
-36
lines changed

.unreleased/pr_8873

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes: #8873 Don't error on failure to update job stats

src/bgw/job_stat_history.c

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,26 @@ bgw_job_stat_history_mark_start(BgwJobStatHistoryContext *context)
155155
bgw_job_stat_history_insert(context, false);
156156
}
157157

158-
static bool
159-
bgw_job_stat_history_scan_one(int indexid, ScanKeyData scankey[], int nkeys,
160-
tuple_found_func tuple_found, tuple_filter_func tuple_filter,
161-
void *data, LOCKMODE lockmode)
158+
static void
159+
bgw_job_stat_history_update_entry(int64 bgw_job_history_id, tuple_found_func tuple_found,
160+
tuple_filter_func tuple_filter, void *data, LOCKMODE lockmode)
162161
{
162+
if (bgw_job_history_id == INVALID_BGW_JOB_STAT_HISTORY_ID)
163+
return;
164+
165+
ScanKeyData scankey[1];
166+
167+
ScanKeyInit(&scankey[0],
168+
Anum_bgw_job_stat_history_pkey_idx_id,
169+
BTEqualStrategyNumber,
170+
F_INT8EQ,
171+
Int64GetDatum(bgw_job_history_id));
172+
163173
Catalog *catalog = ts_catalog_get();
164174
ScannerCtx scanctx = {
165175
.table = catalog_get_table_id(catalog, BGW_JOB_STAT_HISTORY),
166-
.index = catalog_get_index(catalog, BGW_JOB_STAT_HISTORY, indexid),
167-
.nkeys = nkeys,
176+
.index = catalog_get_index(catalog, BGW_JOB_STAT_HISTORY, BGW_JOB_STAT_HISTORY_PKEY_IDX),
177+
.nkeys = 1,
168178
.scankey = scankey,
169179
.flags = SCANNER_F_KEEPLOCK,
170180
.tuple_found = tuple_found,
@@ -174,31 +184,18 @@ bgw_job_stat_history_scan_one(int indexid, ScanKeyData scankey[], int nkeys,
174184
.scandirection = ForwardScanDirection,
175185
};
176186

177-
return ts_scanner_scan_one(&scanctx, false, "bgw job stat");
178-
}
179-
180-
static inline bool
181-
bgw_job_stat_history_scan_id(int64 bgw_job_history_id, tuple_found_func tuple_found,
182-
tuple_filter_func tuple_filter, void *data, LOCKMODE lockmode)
183-
{
184-
if (bgw_job_history_id == INVALID_BGW_JOB_STAT_HISTORY_ID)
185-
return true;
186-
187-
ScanKeyData scankey[1];
188-
189-
ScanKeyInit(&scankey[0],
190-
Anum_bgw_job_stat_history_pkey_idx_id,
191-
BTEqualStrategyNumber,
192-
F_INT8EQ,
193-
Int64GetDatum(bgw_job_history_id));
194-
195-
return bgw_job_stat_history_scan_one(BGW_JOB_STAT_HISTORY_PKEY_IDX,
196-
scankey,
197-
1,
198-
tuple_found,
199-
tuple_filter,
200-
data,
201-
lockmode);
187+
int num_found = ts_scanner_scan(&scanctx);
188+
189+
/* We do not want to raise an error in case there is something wrong with history entries */
190+
if (num_found == 0)
191+
/* This might happen due to job history retention deleting entries */
192+
ereport(DEBUG1,
193+
(errmsg("could not find job stat history entry with id " INT64_FORMAT,
194+
bgw_job_history_id)));
195+
else if (num_found > 1)
196+
ereport(DEBUG1,
197+
(errmsg("found multiple job stat history entries with id " INT64_FORMAT,
198+
bgw_job_history_id)));
202199
}
203200

204201
static ScanTupleResult
@@ -291,14 +288,11 @@ bgw_job_stat_history_update(BgwJobStatHistoryContext *context)
291288
else
292289
{
293290
/* Mark the end of the previous inserted start execution */
294-
if (!bgw_job_stat_history_scan_id(new_job->job_history.id,
291+
bgw_job_stat_history_update_entry(new_job->job_history.id,
295292
bgw_job_stat_history_tuple_update,
296293
NULL,
297294
context,
298-
RowExclusiveLock))
299-
ereport(ERROR,
300-
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
301-
errmsg("unable to find job history " INT64_FORMAT, new_job->job_history.id)));
295+
RowExclusiveLock);
302296
}
303297
}
304298

0 commit comments

Comments
 (0)