diff --git a/src/backend/access/aocs/aocs_compaction.c b/src/backend/access/aocs/aocs_compaction.c index 0467fe5da5872cdf8c024bd4aa4595ae0a43b301..a9e1b9e51d994d8cabd28fadaec4e9562d0f9cd4 100644 --- a/src/backend/access/aocs/aocs_compaction.c +++ b/src/backend/access/aocs/aocs_compaction.c @@ -353,16 +353,14 @@ AOCSSegmentFileFullCompaction(Relation aorel, estate->es_num_result_relations = 1; estate->es_result_relation_info = resultRelInfo; - aocs_getnext(scanDesc, ForwardScanDirection, slot); - while (!TupIsNull(slot)) + while (aocs_getnext(scanDesc, ForwardScanDirection, slot)) { CHECK_FOR_INTERRUPTS(); aoTupleId = (AOTupleId *) slot_get_ctid(slot); if (AppendOnlyVisimap_IsVisible(&scanDesc->visibilityMap, aoTupleId)) { - AOCSMoveTuple( - slot, + AOCSMoveTuple(slot, insertDesc, resultRelInfo, estate); @@ -370,11 +368,8 @@ AOCSSegmentFileFullCompaction(Relation aorel, } else { - MemTuple tuple = TupGetMemTuple(slot); - /* Tuple is invisible and needs to be dropped */ AppendOnlyThrowAwayTuple(aorel, - tuple, slot, mt_bind); } @@ -387,9 +382,6 @@ AOCSSegmentFileFullCompaction(Relation aorel, { vacuum_delay_point(); } - - aocs_getnext(scanDesc, ForwardScanDirection, slot); - } SetAOCSFileSegInfoState(aorel, compact_segno, diff --git a/src/backend/access/aocs/aocsam.c b/src/backend/access/aocs/aocsam.c index f24085faeebbe9c1c92ce55818e9b97f9b607347..0235797ecda657a3a4620d1f8d1a6973a6e6a83e 100644 --- a/src/backend/access/aocs/aocsam.c +++ b/src/backend/access/aocs/aocsam.c @@ -612,7 +612,7 @@ static void upgrade_datum_fetch(AOCSFetchDesc fetch, int attno, Datum values[], values, isnull, formatversion); } -void +bool aocs_getnext(AOCSScanDesc scan, ScanDirection direction, TupleTableSlot *slot) { int ncol; @@ -643,7 +643,7 @@ ReadNext: /* No more seg, we are at the end */ ExecClearTuple(slot); scan->cur_seg = -1; - return; + return false; } scan->cur_seg_row = 0; } @@ -720,11 +720,11 @@ ReadNext: TupSetVirtualTupleNValid(slot, ncol); slot_set_ctid(slot, &(scan->cdb_fake_ctid)); - return; + return true; } Assert(!"Never here"); - return; + return false; } diff --git a/src/backend/access/appendonly/appendonly_compaction.c b/src/backend/access/appendonly/appendonly_compaction.c index d835297c566789919f2797f983ca058816d42a1a..e8be879b3ba0740642f874745d3803a97db254a7 100644 --- a/src/backend/access/appendonly/appendonly_compaction.c +++ b/src/backend/access/appendonly/appendonly_compaction.c @@ -267,13 +267,13 @@ AppendOnlySegmentFileTruncateToEOF(Relation aorel, } static void -AppendOnlyMoveTuple(MemTuple tuple, - TupleTableSlot *slot, +AppendOnlyMoveTuple(TupleTableSlot *slot, MemTupleBinding *mt_bind, AppendOnlyInsertDesc insertDesc, ResultRelInfo *resultRelInfo, EState *estate) { + MemTuple tuple; AOTupleId *oldAoTupleId; Oid tupleOid; AOTupleId newAoTupleId; @@ -287,6 +287,7 @@ AppendOnlyMoveTuple(MemTuple tuple, /* Extract all the values of the tuple */ slot_getallattrs(slot); + tuple = TupGetMemTuple(slot); tupleOid = MemTupleGetOid(tuple, mt_bind); appendonly_insert(insertDesc, tuple, @@ -308,10 +309,10 @@ AppendOnlyMoveTuple(MemTuple tuple, void AppendOnlyThrowAwayTuple(Relation rel, - MemTuple tuple, TupleTableSlot *slot, MemTupleBinding *mt_bind) { + MemTuple tuple; AOTupleId *oldAoTupleId; Assert(slot); @@ -321,6 +322,7 @@ AppendOnlyThrowAwayTuple(Relation rel, /* Extract all the values of the tuple */ slot_getallattrs(slot); + tuple = TupGetMemTuple(slot); if (MemTupleHasExternal(tuple, mt_bind)) { toast_delete(rel, (GenericTuple) tuple, mt_bind); @@ -346,7 +348,6 @@ AppendOnlySegmentFileFullCompaction(Relation aorel, AppendOnlyVisimap visiMap; AppendOnlyScanDesc scanDesc; TupleDesc tupDesc; - MemTuple tuple; TupleTableSlot *slot; MemTupleBinding *mt_bind; int compact_segno; @@ -409,7 +410,7 @@ AppendOnlySegmentFileFullCompaction(Relation aorel, /* * Go through all visible tuples and move them to a new segfile. */ - while ((tuple = appendonly_getnext(scanDesc, ForwardScanDirection, slot)) != NULL) + while (appendonly_getnext(scanDesc, ForwardScanDirection, slot)) { /* Check interrupts as this may take time. */ CHECK_FOR_INTERRUPTS(); @@ -417,8 +418,7 @@ AppendOnlySegmentFileFullCompaction(Relation aorel, aoTupleId = (AOTupleId *) slot_get_ctid(slot); if (AppendOnlyVisimap_IsVisible(&scanDesc->visibilityMap, aoTupleId)) { - AppendOnlyMoveTuple(tuple, - slot, + AppendOnlyMoveTuple(slot, mt_bind, insertDesc, resultRelInfo, @@ -429,7 +429,6 @@ AppendOnlySegmentFileFullCompaction(Relation aorel, { /* Tuple is invisible and needs to be dropped */ AppendOnlyThrowAwayTuple(aorel, - tuple, slot, mt_bind); } diff --git a/src/backend/access/appendonly/appendonlyam.c b/src/backend/access/appendonly/appendonlyam.c index 072522faa349e9128440276ce1b9db0b08d5a365..06f92563547a4d012ab00c9be12365e057eba833 100755 --- a/src/backend/access/appendonly/appendonlyam.c +++ b/src/backend/access/appendonly/appendonlyam.c @@ -1044,7 +1044,7 @@ AppendOnlyExecutorReadBlock_ProcessTuple(AppendOnlyExecutorReadBlock *executorRe return valid; } -static MemTuple +static bool AppendOnlyExecutorReadBlock_ScanNextTuple(AppendOnlyExecutorReadBlock *executorReadBlock, int nkeys, ScanKey key, @@ -1076,7 +1076,7 @@ AppendOnlyExecutorReadBlock_ScanNextTuple(AppendOnlyExecutorReadBlock *executorR /* no more items in the varblock, get new buffer */ AppendOnlyExecutionReadBlock_FinishedScanBlock( executorReadBlock); - return NULL; + return false; } executorReadBlock->currentItemCount++; @@ -1098,7 +1098,7 @@ AppendOnlyExecutorReadBlock_ScanNextTuple(AppendOnlyExecutorReadBlock *executorR nkeys, key, slot)) - return TupGetMemTuple(slot); + return true; } } @@ -1119,7 +1119,7 @@ AppendOnlyExecutorReadBlock_ScanNextTuple(AppendOnlyExecutorReadBlock *executorR { AppendOnlyExecutionReadBlock_FinishedScanBlock( executorReadBlock); - return NULL; + return false; /* Force fetching new block. */ } @@ -1144,7 +1144,7 @@ AppendOnlyExecutorReadBlock_ScanNextTuple(AppendOnlyExecutorReadBlock *executorR nkeys, key, slot)) - return TupGetMemTuple(slot); + return true; } break; @@ -1156,7 +1156,7 @@ AppendOnlyExecutorReadBlock_ScanNextTuple(AppendOnlyExecutorReadBlock *executorR AppendOnlyExecutionReadBlock_FinishedScanBlock( executorReadBlock); - return NULL; + return false; /* No match. */ } @@ -1298,15 +1298,13 @@ getNextBlock(AppendOnlyScanDesc scan) * the scankeys. * ---------------- */ -static MemTuple +static bool appendonlygettup(AppendOnlyScanDesc scan, ScanDirection dir __attribute__((unused)), int nkeys, ScanKey key, TupleTableSlot *slot) { - MemTuple tuple; - Assert(ScanDirectionIsForward(dir)); Assert(scan->usableBlockSize > 0); @@ -1314,6 +1312,8 @@ appendonlygettup(AppendOnlyScanDesc scan, for (;;) { + bool found; + if (scan->bufferDone) { /* @@ -1325,18 +1325,17 @@ appendonlygettup(AppendOnlyScanDesc scan, { /* have we read all this relation's data. done! */ if (scan->aos_done_all_segfiles) - return NULL; + return false; } scan->bufferDone = false; } - tuple = AppendOnlyExecutorReadBlock_ScanNextTuple( - &scan->executorReadBlock, + found = AppendOnlyExecutorReadBlock_ScanNextTuple(&scan->executorReadBlock, nkeys, key, slot); - if (tuple != NULL) + if (found) { /* @@ -1353,7 +1352,7 @@ appendonlygettup(AppendOnlyScanDesc scan, else { /* The tuple is visible */ - return tuple; + return true; } } else @@ -1361,9 +1360,7 @@ appendonlygettup(AppendOnlyScanDesc scan, /* no more items in the varblock, get new buffer */ scan->bufferDone = true; } - } - } static void @@ -1789,22 +1786,22 @@ appendonly_endscan(AppendOnlyScanDesc scan) * appendonly_getnext - retrieve next tuple in scan * ---------------- */ -MemTuple +bool appendonly_getnext(AppendOnlyScanDesc scan, ScanDirection direction, TupleTableSlot *slot) { - MemTuple tup = appendonlygettup(scan, direction, scan->aos_nkeys, scan->aos_key, slot); + if (appendonlygettup(scan, direction, scan->aos_nkeys, scan->aos_key, slot)) + { + pgstat_count_heap_getnext(scan->aos_rd); - if (tup == NULL) + return true; + } + else { if (slot) ExecClearTuple(slot); - return NULL; + return false; } - - pgstat_count_heap_getnext(scan->aos_rd); - - return tup; } static void diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 9d70c928ce3979418bc92fbdb9d32beb7885864a..d8ea3187b6f8867ef483aef7c9e7267a765f74cb 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -2743,7 +2743,7 @@ IndexBuildAppendOnlyRowScan(Relation parentRelation, blockDirectory = aoscan->blockDirectory; } - while (appendonly_getnext(aoscan, ForwardScanDirection, slot) != NULL) + while (appendonly_getnext(aoscan, ForwardScanDirection, slot)) { CHECK_FOR_INTERRUPTS(); @@ -2878,18 +2878,14 @@ IndexBuildAppendOnlyColScan(Relation parentRelation, } - while (true) + while (aocs_getnext(aocsscan, ForwardScanDirection, slot)) { CHECK_FOR_INTERRUPTS(); - - aocs_getnext(aocsscan, ForwardScanDirection, slot); - if (TupIsNull(slot)) - break; reltuples++; - + MemoryContextReset(econtext->ecxt_per_tuple_memory); - + if (predicate != NIL) { if (!ExecQual(predicate, econtext, false)) @@ -2911,11 +2907,10 @@ IndexBuildAppendOnlyColScan(Relation parentRelation, callback(indexRelation, slot_get_ctid(slot), values, isnull, true, callback_state); - } pfree(proj); - + aocs_endscan(aocsscan); if (blockDirectory != NULL) diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index 1eafdbe0969bb411dbd9ee80af61c907ed75b87f..80126afbc3e1821097008022c0d97f5ea45303b0 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -1689,7 +1689,7 @@ acquire_sample_rows_ao(Relation onerel, int elevel, for (;;) { if (aoScanDesc) - (void) appendonly_getnext(aoScanDesc, ForwardScanDirection, slot); + appendonly_getnext(aoScanDesc, ForwardScanDirection, slot); else aocs_getnext(aocsScanDesc, ForwardScanDirection, slot); diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index 20e9996cde7a414b4fb6a03c1cb11ba0d95b630a..31cefef8b302c9de0437665b09fd3dbc11293fa8 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -2969,24 +2969,32 @@ CopyTo(CopyState cstate) } else if (RelationIsAoRows(rel)) { - MemTuple tuple; TupleTableSlot *slot = MakeSingleTupleTableSlot(tupDesc); MemTupleBinding *mt_bind = create_memtuple_binding(tupDesc); aoscandesc = appendonly_beginscan(rel, GetActiveSnapshot(), GetActiveSnapshot(), 0, NULL); - while ((tuple = appendonly_getnext(aoscandesc, ForwardScanDirection, slot)) != NULL) + while (appendonly_getnext(aoscandesc, ForwardScanDirection, slot)) { + MemTuple tuple; + Oid tupleOid = InvalidOid; + CHECK_FOR_INTERRUPTS(); - /* Extract all the values of the tuple */ + /* Extract all the values of the tuple */ slot_getallattrs(slot); values = slot_get_values(slot); nulls = slot_get_isnull(slot); + if (mtbind_has_oid(mt_bind)) + { + tuple = TupGetMemTuple(slot); + tupleOid = MemTupleGetOid(tuple, mt_bind); + } + /* Format and send the data */ - CopyOneRowTo(cstate, MemTupleGetOid(tuple, mt_bind), values, nulls); + CopyOneRowTo(cstate, tupleOid, values, nulls); processed++; } @@ -3016,14 +3024,10 @@ CopyTo(CopyState cstate) GetActiveSnapshot(), NULL /* relationTupleDesc */, proj); - for(;;) + while (aocs_getnext(scan, ForwardScanDirection, slot)) { CHECK_FOR_INTERRUPTS(); - aocs_getnext(scan, ForwardScanDirection, slot); - if (TupIsNull(slot)) - break; - slot_getallattrs(slot); values = slot_get_values(slot); nulls = slot_get_isnull(slot); diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 41fe8ef1a20b7db08544d00a94fe679f5092148e..7afaaa8b6442b96759fbc7ed58d7cf66638c33cb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -6587,8 +6587,10 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode) */ oldCxt = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate)); - while ((mtuple = appendonly_getnext(aoscan, ForwardScanDirection, oldslot)) != NULL) + while (appendonly_getnext(aoscan, ForwardScanDirection, oldslot)) { + mtuple = TupGetMemTuple(oldslot); + if (newrel) { Oid tupOid = InvalidOid; @@ -6717,9 +6719,7 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode) sdesc = aocs_beginscan(oldrel, snapshot, snapshot, oldTupDesc, proj); - aocs_getnext(sdesc, ForwardScanDirection, oldslot); - - while(!TupIsNull(oldslot)) + while (aocs_getnext(sdesc, ForwardScanDirection, oldslot)) { oldCxt = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate)); econtext->ecxt_scantuple = oldslot; @@ -6808,7 +6808,6 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode) CHECK_FOR_INTERRUPTS(); MemoryContextSwitchTo(oldCxt); - aocs_getnext(sdesc, ForwardScanDirection, oldslot); } aocs_endscan(sdesc); @@ -17185,10 +17184,8 @@ split_rows(Relation intoa, Relation intob, Relation temprel) } else if (RelationIsAoRows(temprel)) { - MemTuple mtuple; - - mtuple = appendonly_getnext(aoscan, ForwardScanDirection, slotT); - if (!PointerIsValid(mtuple)) + appendonly_getnext(aoscan, ForwardScanDirection, slotT); + if (TupIsNull(slotT)) break; TupClearShouldFree(slotT); diff --git a/src/include/access/appendonly_compaction.h b/src/include/access/appendonly_compaction.h index 694b2ce292526465f78d1cfe517f9fa0018c65e2..2b3a4ee12d73cce203c84462fb286d872859f8d3 100644 --- a/src/include/access/appendonly_compaction.h +++ b/src/include/access/appendonly_compaction.h @@ -33,7 +33,7 @@ extern bool AppendOnlyCompaction_ShouldCompact( int64 segmentTotalTupcount, bool isFull, Snapshot appendOnlyMetaDataSnapshot); -extern void AppendOnlyThrowAwayTuple(Relation rel, MemTuple tuple, +extern void AppendOnlyThrowAwayTuple(Relation rel, TupleTableSlot *slot, MemTupleBinding *mt_bind); extern void AppendOnlyTruncateToEOF(Relation aorel); extern bool HasLockForSegmentFileDrop(Relation aorel); diff --git a/src/include/cdb/cdbaocsam.h b/src/include/cdb/cdbaocsam.h index da2eba2d72ce9c6e48037d285578af8cc49b345e..41f6cd3d645c0727861d3475d0b636b9d2236fc8 100644 --- a/src/include/cdb/cdbaocsam.h +++ b/src/include/cdb/cdbaocsam.h @@ -211,7 +211,7 @@ extern AOCSScanDesc aocs_beginrangescan(Relation relation, Snapshot snapshot, extern void aocs_rescan(AOCSScanDesc scan); extern void aocs_endscan(AOCSScanDesc scan); -extern void aocs_getnext(AOCSScanDesc scan, ScanDirection direction, TupleTableSlot *slot); +extern bool aocs_getnext(AOCSScanDesc scan, ScanDirection direction, TupleTableSlot *slot); extern AOCSInsertDesc aocs_insert_init(Relation rel, int segno, bool update_mode); extern Oid aocs_insert_values(AOCSInsertDesc idesc, Datum *d, bool *null, AOTupleId *aoTupleId); static inline Oid aocs_insert(AOCSInsertDesc idesc, TupleTableSlot *slot) diff --git a/src/include/cdb/cdbappendonlyam.h b/src/include/cdb/cdbappendonlyam.h index aa6de8c429782b531cf42504f755eb4d97c55214..a11416f743bee14aa0eb633273a7f4154c847337 100644 --- a/src/include/cdb/cdbappendonlyam.h +++ b/src/include/cdb/cdbappendonlyam.h @@ -338,9 +338,9 @@ extern AppendOnlyScanDesc appendonly_beginrangescan(Relation relation, int nkeys, ScanKey keys); extern void appendonly_rescan(AppendOnlyScanDesc scan, ScanKey key); extern void appendonly_endscan(AppendOnlyScanDesc scan); -extern MemTuple appendonly_getnext(AppendOnlyScanDesc scan, - ScanDirection direction, - TupleTableSlot *slot); +extern bool appendonly_getnext(AppendOnlyScanDesc scan, + ScanDirection direction, + TupleTableSlot *slot); extern AppendOnlyFetchDesc appendonly_fetch_init( Relation relation, Snapshot snapshot,