Skip to content

Commit

Permalink
Do not lock OSM chunk's tuple on replica instances
Browse files Browse the repository at this point in the history
Planner attempts acquiring a `LockTupleKeyShare` tuple lock on an OSM
chunk's dimension slice which results in an error on replica. This
patch removes tuple lock when in recovery mode.
  • Loading branch information
zilder committed Jun 18, 2024
1 parent c30cdf8 commit f68006b
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/hypertable.c
Original file line number Diff line number Diff line change
Expand Up @@ -2461,14 +2461,25 @@ ts_chunk_get_osm_slice_and_lock(int32 osm_chunk_id, int32 time_dim_id, LockTuple
.lockmode = tuplockmode,
.waitpolicy = LockWaitBlock,
};
/*
* We cannot acquire a tuple lock when running in recovery mode
* since that prevents scans on tiered hypertables from running
* on a read-only secondary. Acquiring a tuple lock requires
* assigning a transaction id for the current transaction state
* which is not possible in recovery mode. So we only acquire the
* lock if we are not in recovery mode.
*/
ScanTupLock *const tuplock_ptr = RecoveryInProgress() ? NULL : &tuplock;

if (!IsolationUsesXactSnapshot())
{
/* in read committed mode, we follow all updates to this tuple */
tuplock.lockflags |= TUPLE_LOCK_FLAG_FIND_LAST_VERSION;
}

DimensionSlice *dimslice =
ts_dimension_slice_scan_by_id_and_lock(cc->fd.dimension_slice_id,
&tuplock,
tuplock_ptr,
CurrentMemoryContext,
tablelockmode);
if (dimslice->fd.dimension_id == time_dim_id)
Expand Down Expand Up @@ -2499,6 +2510,18 @@ ts_hypertable_osm_range_update(PG_FUNCTION_ARGS)
Oid time_type; /* required for resolving the argument types, should match the hypertable
partitioning column type */

/*
* This function is not meant to be run on a read-only secondary. It is
* only used by OSM to update chunk's range in timescaledb catalog when
* tiering configuration changes (a new chunk is created, a chunk drop
* etc); OSM would already be holding a lock on a dimension slice tuple
* by this moment (which is not possible on read-only instance).
* Technically this function can be executed from SQL (e.g. from psql) when
* in recovery mode; in that instance an ERROR would be thrown when trying
* to update the dimension slice tuple, no harm will be done.
*/
Assert(!RecoveryInProgress());

hcache = ts_hypertable_cache_pin();
ht = ts_resolve_hypertable_from_table_or_cagg(hcache, relid, true);
Assert(ht != NULL);
Expand Down

0 comments on commit f68006b

Please sign in to comment.