Refactor UpdateStripeMetadataRow to handle locking modes based on PostgreSQL version

m3hm3t/pg18_columnar_write_1
Mehmet Yilmaz 2025-06-23 16:35:37 +00:00
parent 433e22e995
commit e9e976d331
1 changed files with 39 additions and 46 deletions

View File

@ -1391,7 +1391,15 @@ UpdateStripeMetadataRow(uint64 storageId, uint64 stripeId, bool *update,
Oid columnarStripesOid = ColumnarStripeRelationId();
Relation columnarStripes = table_open(columnarStripesOid, AccessShareLock);
#if PG_VERSION_NUM >= 180000
/* CatalogTupleUpdate performs a normal heap UPDATE → RowExclusiveLock */
const LOCKMODE openLockMode = RowExclusiveLock;
#else
/* Inplace update never changed tuple length → AccessShareLock was enough */
const LOCKMODE openLockMode = AccessShareLock;
#endif
Relation columnarStripes = table_open(columnarStripesOid, openLockMode);
Oid indexId = ColumnarStripePKeyIndexRelationId();
bool indexOk = OidIsValid(indexId);
@ -1407,24 +1415,13 @@ UpdateStripeMetadataRow(uint64 storageId, uint64 stripeId, bool *update,
HeapTuple oldTuple = systable_getnext(scanDescriptor);
if (!HeapTupleIsValid(oldTuple))
{
ereport(ERROR, (errmsg("attempted to modify an unexpected stripe, "
ereport(ERROR,
(errmsg("attempted to modify an unexpected stripe, "
"columnar storage with id=" UINT64_FORMAT
" does not have stripe with id=" UINT64_FORMAT,
storageId, stripeId)));
}
/*
* heap_modify_tuple + heap_inplace_update only exist on PG < 18;
* on PG18 the in-place helper was removed upstream, so we skip the whole block.
*/
#if PG_VERSION_NUM < PG_VERSION_18
/*
* heap_inplace_update already doesn't allow changing size of the original
* tuple, so we don't allow setting any Datum's to NULL values.
*/
/* ---------------- construct the new tuple ---------------- */
bool newNulls[Natts_columnar_stripe] = {false};
TupleDesc tupleDescriptor = RelationGetDescr(columnarStripes);
HeapTuple modifiedTuple = heap_modify_tuple(oldTuple,
@ -1433,29 +1430,25 @@ UpdateStripeMetadataRow(uint64 storageId, uint64 stripeId, bool *update,
newNulls,
update);
#if PG_VERSION_NUM < 180000
/* Fast path: true inplace update (same physical tuple) */
heap_inplace_update(columnarStripes, modifiedTuple);
HeapTuple newTuple = oldTuple; /* contents overwritten in place */
#else
/* Regular catalog UPDATE keeps indexes in sync */
CatalogTupleUpdate(columnarStripes, &oldTuple->t_self, modifiedTuple);
HeapTuple newTuple = modifiedTuple; /* freshly written tuple */
#endif
/*
* Existing tuple now contains modifications, because we used
* heap_inplace_update().
*/
HeapTuple newTuple = oldTuple;
/*
* Must not pass modifiedTuple, because BuildStripeMetadata expects a real
* heap tuple with MVCC fields.
*/
StripeMetadata *modifiedStripeMetadata = BuildStripeMetadata(columnarStripes,
newTuple);
CommandCounterIncrement();
systable_endscan(scanDescriptor);
table_close(columnarStripes, AccessShareLock);
/* Build StripeMetadata from the uptodate tuple */
StripeMetadata *modifiedStripeMetadata =
BuildStripeMetadata(columnarStripes, newTuple);
systable_endscan(scanDescriptor);
table_close(columnarStripes, openLockMode);
/* return StripeMetadata object built from modified tuple */
return modifiedStripeMetadata;
}