From 882d4901c288e63f2fabcce25827e2e0143e174b Mon Sep 17 00:00:00 2001 From: mkaruza Date: Wed, 11 Oct 2023 09:36:48 +0200 Subject: [PATCH] [columnar] [bug] panic query on select count(*) from .. * 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. --- .../src/backend/columnar/columnar_metadata.c | 40 ++++++------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/columnar/src/backend/columnar/columnar_metadata.c b/columnar/src/backend/columnar/columnar_metadata.c index 90b2e8d1..ba80a8f0 100644 --- a/columnar/src/backend/columnar/columnar_metadata.c +++ b/columnar/src/backend/columnar/columnar_metadata.c @@ -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);