@@ -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+
141191static int32
142192number_of_continuous_aggs_attached (int32 raw_hypertable_id )
143193{
0 commit comments