From b3d9fc91f804c3f3cd96ce2af5bae7a46993b235 Mon Sep 17 00:00:00 2001 From: Onur Tirtir Date: Thu, 5 Aug 2021 19:27:10 +0300 Subject: [PATCH] Always use right mem cxt when creating ColumnarReadState All the callers except columnar_relation_copy_for_cluster were already switching to right memory context when creating ColumnarReadState. With this commit, we embed that logic into init_columnar_read_state to avoid further such bugs. That way, we start using the right memory context for columnar_relation_copy_for_cluster too. --- src/backend/columnar/columnar_tableam.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/backend/columnar/columnar_tableam.c b/src/backend/columnar/columnar_tableam.c index 8b6151b29..f167ec41b 100644 --- a/src/backend/columnar/columnar_tableam.c +++ b/src/backend/columnar/columnar_tableam.c @@ -253,12 +253,16 @@ CreateColumnarScanMemoryContext(void) */ static ColumnarReadState * init_columnar_read_state(Relation relation, TupleDesc tupdesc, Bitmapset *attr_needed, - List *scanQual) + List *scanQual, MemoryContext scanContext) { + MemoryContext oldContext = MemoryContextSwitchTo(scanContext); + List *neededColumnList = NeededColumnsList(tupdesc, attr_needed); ColumnarReadState *readState = ColumnarBeginRead(relation, tupdesc, neededColumnList, scanQual); + MemoryContextSwitchTo(oldContext); + return readState; } @@ -302,11 +306,10 @@ columnar_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlo */ if (scan->cs_readState == NULL) { - MemoryContext oldContext = MemoryContextSwitchTo(scan->scanContext); scan->cs_readState = init_columnar_read_state(scan->cs_base.rs_rd, slot->tts_tupleDescriptor, - scan->attr_needed, scan->scanQual); - MemoryContextSwitchTo(oldContext); + scan->attr_needed, scan->scanQual, + scan->scanContext); } ExecClearTuple(slot); @@ -490,8 +493,6 @@ columnar_index_fetch_tuple(struct IndexFetchTableData *sscan, /* initialize read state for the first row */ if (scan->cs_readState == NULL) { - MemoryContext oldContext = MemoryContextSwitchTo(scan->scanContext); - /* we need all columns */ int natts = columnarRelation->rd_att->natts; Bitmapset *attr_needed = bms_add_range(NULL, 0, natts - 1); @@ -501,8 +502,8 @@ columnar_index_fetch_tuple(struct IndexFetchTableData *sscan, scan->cs_readState = init_columnar_read_state(columnarRelation, slot->tts_tupleDescriptor, - attr_needed, scanQual); - MemoryContextSwitchTo(oldContext); + attr_needed, scanQual, + scan->scanContext); } uint64 rowNumber = tid_to_row_number(*tid); @@ -799,8 +800,10 @@ columnar_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, /* no quals for table rewrite */ List *scanQual = NIL; + MemoryContext scanContext = CreateColumnarScanMemoryContext(); ColumnarReadState *readState = init_columnar_read_state(OldHeap, sourceDesc, - attr_needed, scanQual); + attr_needed, scanQual, + scanContext); Datum *values = palloc0(sourceDesc->natts * sizeof(Datum)); bool *nulls = palloc0(sourceDesc->natts * sizeof(bool));