Skip to content

Commit

Permalink
[columnar] [bug] panic query on select count(*) from ..
Browse files Browse the repository at this point in the history
* When stripe assignment for next parallel worker is executing we
  iterate sequentially to find next stripe that will be used. That
  could be expensive - especially if number of stripes for table is big.
  During this loop we are holding shared mutex that will abort if held
  to long.

* Fixed by using second column from index to faster assign
  stripe for worker.
  • Loading branch information
mkaruza authored and wuputah committed Oct 16, 2023
1 parent 9462f75 commit 882d490
Showing 1 changed file with 12 additions and 28 deletions.
40 changes: 12 additions & 28 deletions columnar/src/backend/columnar/columnar_metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -1091,44 +1091,28 @@ FindNextStripeForParallelWorker(Relation relation,
StripeMetadata *foundStripeMetadata = NULL;

uint64 storageId = ColumnarStorageGetStorageId(relation, false);
ScanKeyData scanKey;
ScanKeyData scanKey[2];

ScanKeyInit(&scanKey, Anum_columnar_stripe_storageid,
ScanKeyInit(&scanKey[0], Anum_columnar_stripe_storageid,
BTEqualStrategyNumber, F_OIDEQ, UInt64GetDatum(storageId));

ScanKeyInit(&scanKey[1], Anum_columnar_stripe_stripe,
BTGreaterEqualStrategyNumber, F_INT8GE, UInt64GetDatum(nextStripeId));

Relation columnarStripes = table_open(ColumnarStripeRelationId(), AccessShareLock);

Relation index = index_open(ColumnarStripeFirstRowNumberIndexRelationId(),
Relation index = index_open(ColumnarStripePKeyIndexRelationId(),
AccessShareLock);

SysScanDesc scanDescriptor = systable_beginscan_ordered(columnarStripes, index,
snapshot, 1,
&scanKey);
snapshot, 2,
scanKey);

while(true)
HeapTuple heapTuple = systable_getnext_ordered(scanDescriptor, ForwardScanDirection);
if (HeapTupleIsValid(heapTuple))
{
HeapTuple heapTuple = systable_getnext_ordered(scanDescriptor, ForwardScanDirection);

if (HeapTupleIsValid(heapTuple))
{
foundStripeMetadata = BuildStripeMetadata(columnarStripes, heapTuple);

if (foundStripeMetadata->id == nextStripeId)
break;

if (foundStripeMetadata->id > nextStripeId)
{
*nextHigherStripeId = foundStripeMetadata->id;
break;
}

pfree(foundStripeMetadata);
}
else
{
foundStripeMetadata = NULL;
break;
}
foundStripeMetadata = BuildStripeMetadata(columnarStripes, heapTuple);
*nextHigherStripeId = foundStripeMetadata->id;
}

systable_endscan_ordered(scanDescriptor);
Expand Down

0 comments on commit 882d490

Please sign in to comment.