Skip to content

Commit

Permalink
Fix memory leak when executing vectorized quals (#242)
Browse files Browse the repository at this point in the history
* Fix memory leak when executing vectorized quals

Currently, a VectorColumn structure is created when executing vectorized
quals for each tuple on the ExecutorState memory context.  However, it
will be freed only until execution finishes.  This commit changes the
memory context to a tuple memory context.

* Fix memory leak in ReadStripeNextVector()

In ReadStripeNextVector(), the memory of columnValueOffset is allocated
in ExecutorState memory content, it will be freed until execution is
finished, so call pfree() to explicitly release the memory to avoid
memory growing up.
  • Loading branch information
japinli authored Mar 20, 2024
1 parent 67e37bd commit 3194451
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion columnar/src/backend/columnar/columnar_customscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -2380,7 +2380,7 @@ CustomExecScan(ColumnarScanState *columnarScanState,
bool *resultQual =
ExecuteVectorizedQual(slot,
columnarScanState->vectorization.constructedVectorizedQualList,
AND_EXPR);
AND_EXPR, econtext);

memcpy(vectorSlot->keep, resultQual, COLUMNAR_VECTOR_COLUMN_SIZE);
}
Expand Down
2 changes: 2 additions & 0 deletions columnar/src/backend/columnar/columnar_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2171,9 +2171,11 @@ ReadStripeNextVector(StripeReadState *stripeReadState, Datum *columnValues,
else
stripeReadState->currentRow += stripeReadState->chunkGroupReadState->rowCount;

pfree(columnValueOffset);
return true;
}

pfree(columnValueOffset);
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,16 +390,21 @@ vectorizedOr(bool *left, bool *right, int dimension)
}

static bool *
executeVectorizedExpr(VectorQual *vectorQual)
executeVectorizedExpr(VectorQual *vectorQual, ExprContext *econtext)
{
MemoryContext oldContext;

oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
VectorColumn *res =
(VectorColumn *) vectorQual->u.expr.fmgrInfo->fn_addr(vectorQual->u.expr.fcInfo);
MemoryContextSwitchTo(oldContext);

return (bool *) res->value;
}

bool *
ExecuteVectorizedQual(TupleTableSlot *slot, List *vectorizedQualList, BoolExprType boolType)
ExecuteVectorizedQual(TupleTableSlot *slot, List *vectorizedQualList,
BoolExprType boolType, ExprContext *econtext)
{
VectorTupleTableSlot *vectorSlot = (VectorTupleTableSlot *) slot;
ListCell *lc;
Expand All @@ -416,18 +421,22 @@ ExecuteVectorizedQual(TupleTableSlot *slot, List *vectorizedQualList, BoolExprTy
{
case VECTOR_QUAL_EXPR:
{
qualResult = executeVectorizedExpr(vectorQual);
qualResult = executeVectorizedExpr(vectorQual, econtext);
break;
}
case VECTOR_QUAL_BOOL_EXPR:
{
if (vectorQual->u.boolExpr.boolExprType == AND_EXPR)
{
qualResult = ExecuteVectorizedQual(slot, vectorQual->u.boolExpr.vectorQualExprList, AND_EXPR);
qualResult = ExecuteVectorizedQual(slot,
vectorQual->u.boolExpr.vectorQualExprList,
AND_EXPR, econtext);
}
else if (vectorQual->u.boolExpr.boolExprType == OR_EXPR)
{
qualResult = ExecuteVectorizedQual(slot, vectorQual->u.boolExpr.vectorQualExprList, OR_EXPR);
qualResult = ExecuteVectorizedQual(slot,
vectorQual->u.boolExpr.vectorQualExprList,
OR_EXPR, econtext);
}
break;
}
Expand All @@ -448,4 +457,4 @@ ExecuteVectorizedQual(TupleTableSlot *slot, List *vectorizedQualList, BoolExprTy
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern List * CreateVectorizedExprList(List *exprList);
extern List * ConstructVectorizedQualList(TupleTableSlot *slot, List *vectorizedQual);
extern bool * ExecuteVectorizedQual(TupleTableSlot *slot,
List *vectorizedQualList,
BoolExprType boolType);
BoolExprType boolType,
ExprContext *econtext);

#endif
#endif

0 comments on commit 3194451

Please sign in to comment.