Skip to content

Commit 25995b8

Browse files
committed
Use continuous aggregate catalog table for tuple lock instead
1 parent 382b0f7 commit 25995b8

3 files changed

Lines changed: 57 additions & 14 deletions

File tree

src/ts_catalog/continuous_agg.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,56 @@ init_materialization_invalidation_log_scan_by_materialization_id(ScanIterator *i
138138
Int32GetDatum(materialization_id));
139139
}
140140

141+
TSDLLEXPORT bool
142+
ts_lock_continuous_agg_tuple(int32 mat_hypertable_id)
143+
{
144+
bool success = false;
145+
ScanTupLock scantuplock = {
146+
.waitpolicy = LockWaitBlock,
147+
.lockmode = LockTupleExclusive,
148+
};
149+
ScanIterator iterator =
150+
ts_scan_iterator_create(CONTINUOUS_AGG, RowShareLock, CurrentMemoryContext);
151+
init_scan_by_mat_hypertable_id(&iterator, mat_hypertable_id);
152+
iterator.ctx.tuplock = &scantuplock;
153+
iterator.ctx.flags = SCANNER_F_KEEPLOCK;
154+
155+
/* see table_tuple_lock for details about flags that are set in TupleExclusive mode */
156+
scantuplock.lockflags = TUPLE_LOCK_FLAG_LOCK_UPDATE_IN_PROGRESS;
157+
if (!IsolationUsesXactSnapshot())
158+
{
159+
/* in read committed mode, we follow all updates to this tuple */
160+
scantuplock.lockflags |= TUPLE_LOCK_FLAG_FIND_LAST_VERSION;
161+
}
162+
163+
ts_scanner_foreach(&iterator)
164+
{
165+
TupleInfo *ti = ts_scan_iterator_tuple_info(&iterator);
166+
if (ti->lockresult != TM_Ok)
167+
{
168+
if (IsolationUsesXactSnapshot())
169+
{
170+
ereport(ERROR,
171+
(errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
172+
errmsg("could not serialize access due to concurrent update")));
173+
}
174+
else
175+
{
176+
ereport(ERROR,
177+
(errcode(ERRCODE_INTERNAL_ERROR),
178+
errmsg("unable to lock continuous aggregate catalog tuple, lock result "
179+
"is %d for mat_hypertable_id (%d)",
180+
ti->lockresult,
181+
mat_hypertable_id)));
182+
}
183+
}
184+
success = true;
185+
break;
186+
}
187+
ts_scan_iterator_close(&iterator);
188+
return success;
189+
}
190+
141191
static int32
142192
number_of_continuous_aggs_attached(int32 raw_hypertable_id)
143193
{

src/ts_catalog/continuous_agg.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ typedef struct ContinuousAggPolicyOffset
145145
const char *name;
146146
} ContinuousAggPolicyOffset;
147147

148+
extern TSDLLEXPORT bool ts_lock_continuous_agg_tuple(int32 mat_hypertable_id);
149+
148150
extern TSDLLEXPORT Oid ts_cagg_permissions_check(Oid cagg_oid, Oid userid);
149151

150152
extern TSDLLEXPORT ContinuousAggInfo ts_continuous_agg_get_all_caggs_info(int32 raw_hypertable_id);

tsl/src/continuous_aggs/refresh.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -643,21 +643,12 @@ process_cagg_invalidations_and_refresh(const ContinuousAgg *cagg,
643643
const ContinuousAggRefreshContext context,
644644
bool bucketing_refresh_window, bool force)
645645
{
646-
/* Lock the materialized hypertable's catalog table entry to protect against concurrent refreshes on the same cagg from invalidation log processing.
647-
*
648-
* This is supposed to be a short transaction and in the future we can consider
649-
* relaxing this lock.
646+
/* Lock the continuous aggregate's catalog table entry to protect against concurrent refreshes
647+
* on the same cagg processing the cagg invalidation logs for that CAgg.
650648
*/
651-
Cache *hcache = ts_hypertable_cache_pin();
652-
Hypertable *mat_ht =
653-
ts_hypertable_cache_get_entry_by_id(hcache, cagg->data.mat_hypertable_id);
654-
ts_cache_release(&hcache);
655-
656-
FormData_hypertable form;
657-
ItemPointerData tid;
658-
/* lock the tuple entry in the catalog table */
659-
bool found = ts_lock_hypertable_tuple(mat_ht->fd.id, &tid, &form);
660-
Ensure(found, "hypertable id %d not found", mat_ht->fd.id);
649+
bool found = ts_lock_continuous_agg_tuple(cagg->data.mat_hypertable_id);
650+
Ensure(found, "continuous aggregate with mat_hypertable_id %d not found",
651+
cagg->data.mat_hypertable_id);
661652
invalidation_process_cagg_log(cagg, refresh_window);
662653

663654
DEBUG_ERROR_INJECTION("cagg_refresh_fail_in_txn2");

0 commit comments

Comments
 (0)