Commit c2fe139c authored by Andres Freund's avatar Andres Freund

tableam: Add and use scan APIs.

Too allow table accesses to be not directly dependent on heap, several
new abstractions are needed. Specifically:

1) Heap scans need to be generalized into table scans. Do this by
   introducing TableScanDesc, which will be the "base class" for
   individual AMs. This contains the AM independent fields from
   HeapScanDesc.

   The previous heap_{beginscan,rescan,endscan} et al. have been
   replaced with a table_ version.

   There's no direct replacement for heap_getnext(), as that returned
   a HeapTuple, which is undesirable for a other AMs. Instead there's
   table_scan_getnextslot().  But note that heap_getnext() lives on,
   it's still used widely to access catalog tables.

   This is achieved by new scan_begin, scan_end, scan_rescan,
   scan_getnextslot callbacks.

2) The portion of parallel scans that's shared between backends need
   to be able to do so without the user doing per-AM work. To achieve
   that new parallelscan_{estimate, initialize, reinitialize}
   callbacks are introduced, which operate on a new
   ParallelTableScanDesc, which again can be subclassed by AMs.

   As it is likely that several AMs are going to be block oriented,
   block oriented callbacks that can be shared between such AMs are
   provided and used by heap. table_block_parallelscan_{estimate,
   intiialize, reinitialize} as callbacks, and
   table_block_parallelscan_{nextpage, init} for use in AMs. These
   operate on a ParallelBlockTableScanDesc.

3) Index scans need to be able to access tables to return a tuple, and
   there needs to be state across individual accesses to the heap to
   store state like buffers. That's now handled by introducing a
   sort-of-scan IndexFetchTable, which again is intended to be
   subclassed by individual AMs (for heap IndexFetchHeap).

   The relevant callbacks for an AM are index_fetch_{end, begin,
   reset} to create the necessary state, and index_fetch_tuple to
   retrieve an indexed tuple.  Note that index_fetch_tuple
   implementations need to be smarter than just blindly fetching the
   tuples for AMs that have optimizations similar to heap's HOT - the
   currently alive tuple in the update chain needs to be fetched if
   appropriate.

   Similar to table_scan_getnextslot(), it's undesirable to continue
   to return HeapTuples. Thus index_fetch_heap (might want to rename
   that later) now accepts a slot as an argument. Core code doesn't
   have a lot of call sites performing index scans without going
   through the systable_* API (in contrast to loads of heap_getnext
   calls and working directly with HeapTuples).

   Index scans now store the result of a search in
   IndexScanDesc->xs_heaptid, rather than xs_ctup->t_self. As the
   target is not generally a HeapTuple anymore that seems cleaner.

To be able to sensible adapt code to use the above, two further
callbacks have been introduced:

a) slot_callbacks returns a TupleTableSlotOps* suitable for creating
   slots capable of holding a tuple of the AMs
   type. table_slot_callbacks() and table_slot_create() are based
   upon that, but have additional logic to deal with views, foreign
   tables, etc.

   While this change could have been done separately, nearly all the
   call sites that needed to be adapted for the rest of this commit
   also would have been needed to be adapted for
   table_slot_callbacks(), making separation not worthwhile.

b) tuple_satisfies_snapshot checks whether the tuple in a slot is
   currently visible according to a snapshot. That's required as a few
   places now don't have a buffer + HeapTuple around, but a
   slot (which in heap's case internally has that information).

Additionally a few infrastructure changes were needed:

I) SysScanDesc, as used by systable_{beginscan, getnext} et al. now
   internally uses a slot to keep track of tuples. While
   systable_getnext() still returns HeapTuples, and will so for the
   foreseeable future, the index API (see 1) above) now only deals with
   slots.

The remainder, and largest part, of this commit is then adjusting all
scans in postgres to use the new APIs.

Author: Andres Freund, Haribabu Kommi, Alvaro Herrera
Discussion:
    https://postgr.es/m/20180703070645.wchpu5muyto5n647@alap3.anarazel.de
    https://postgr.es/m/20160812231527.GA690404@alvherre.pgsql
parent a4784152
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "access/heapam.h" #include "access/heapam.h"
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/nbtree.h" #include "access/nbtree.h"
#include "access/tableam.h"
#include "access/transam.h" #include "access/transam.h"
#include "access/xact.h" #include "access/xact.h"
#include "catalog/index.h" #include "catalog/index.h"
...@@ -481,7 +482,7 @@ bt_check_every_level(Relation rel, Relation heaprel, bool readonly, ...@@ -481,7 +482,7 @@ bt_check_every_level(Relation rel, Relation heaprel, bool readonly,
if (state->heapallindexed) if (state->heapallindexed)
{ {
IndexInfo *indexinfo = BuildIndexInfo(state->rel); IndexInfo *indexinfo = BuildIndexInfo(state->rel);
HeapScanDesc scan; TableScanDesc scan;
/* Report on extra downlink checks performed in readonly case */ /* Report on extra downlink checks performed in readonly case */
if (state->readonly) if (state->readonly)
...@@ -500,12 +501,12 @@ bt_check_every_level(Relation rel, Relation heaprel, bool readonly, ...@@ -500,12 +501,12 @@ bt_check_every_level(Relation rel, Relation heaprel, bool readonly,
* *
* Note that IndexBuildHeapScan() calls heap_endscan() for us. * Note that IndexBuildHeapScan() calls heap_endscan() for us.
*/ */
scan = heap_beginscan_strat(state->heaprel, /* relation */ scan = table_beginscan_strat(state->heaprel, /* relation */
snapshot, /* snapshot */ snapshot, /* snapshot */
0, /* number of keys */ 0, /* number of keys */
NULL, /* scan key */ NULL, /* scan key */
true, /* buffer access strategy OK */ true, /* buffer access strategy OK */
true); /* syncscan OK? */ true); /* syncscan OK? */
/* /*
* Scan will behave as the first scan of a CREATE INDEX CONCURRENTLY * Scan will behave as the first scan of a CREATE INDEX CONCURRENTLY
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include "access/heapam.h" #include "access/heapam.h"
#include "access/multixact.h" #include "access/multixact.h"
#include "access/relscan.h" #include "access/relscan.h"
#include "access/tableam.h"
#include "access/xact.h" #include "access/xact.h"
#include "catalog/namespace.h" #include "catalog/namespace.h"
#include "catalog/pg_authid.h" #include "catalog/pg_authid.h"
...@@ -55,7 +56,7 @@ PG_FUNCTION_INFO_V1(pgrowlocks); ...@@ -55,7 +56,7 @@ PG_FUNCTION_INFO_V1(pgrowlocks);
typedef struct typedef struct
{ {
Relation rel; Relation rel;
HeapScanDesc scan; TableScanDesc scan;
int ncolumns; int ncolumns;
} MyData; } MyData;
...@@ -70,7 +71,8 @@ Datum ...@@ -70,7 +71,8 @@ Datum
pgrowlocks(PG_FUNCTION_ARGS) pgrowlocks(PG_FUNCTION_ARGS)
{ {
FuncCallContext *funcctx; FuncCallContext *funcctx;
HeapScanDesc scan; TableScanDesc scan;
HeapScanDesc hscan;
HeapTuple tuple; HeapTuple tuple;
TupleDesc tupdesc; TupleDesc tupdesc;
AttInMetadata *attinmeta; AttInMetadata *attinmeta;
...@@ -124,7 +126,8 @@ pgrowlocks(PG_FUNCTION_ARGS) ...@@ -124,7 +126,8 @@ pgrowlocks(PG_FUNCTION_ARGS)
aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind), aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind),
RelationGetRelationName(rel)); RelationGetRelationName(rel));
scan = heap_beginscan(rel, GetActiveSnapshot(), 0, NULL); scan = table_beginscan(rel, GetActiveSnapshot(), 0, NULL);
hscan = (HeapScanDesc) scan;
mydata = palloc(sizeof(*mydata)); mydata = palloc(sizeof(*mydata));
mydata->rel = rel; mydata->rel = rel;
mydata->scan = scan; mydata->scan = scan;
...@@ -138,6 +141,7 @@ pgrowlocks(PG_FUNCTION_ARGS) ...@@ -138,6 +141,7 @@ pgrowlocks(PG_FUNCTION_ARGS)
attinmeta = funcctx->attinmeta; attinmeta = funcctx->attinmeta;
mydata = (MyData *) funcctx->user_fctx; mydata = (MyData *) funcctx->user_fctx;
scan = mydata->scan; scan = mydata->scan;
hscan = (HeapScanDesc) scan;
/* scan the relation */ /* scan the relation */
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
...@@ -147,11 +151,11 @@ pgrowlocks(PG_FUNCTION_ARGS) ...@@ -147,11 +151,11 @@ pgrowlocks(PG_FUNCTION_ARGS)
uint16 infomask; uint16 infomask;
/* must hold a buffer lock to call HeapTupleSatisfiesUpdate */ /* must hold a buffer lock to call HeapTupleSatisfiesUpdate */
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE); LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
htsu = HeapTupleSatisfiesUpdate(tuple, htsu = HeapTupleSatisfiesUpdate(tuple,
GetCurrentCommandId(false), GetCurrentCommandId(false),
scan->rs_cbuf); hscan->rs_cbuf);
xmax = HeapTupleHeaderGetRawXmax(tuple->t_data); xmax = HeapTupleHeaderGetRawXmax(tuple->t_data);
infomask = tuple->t_data->t_infomask; infomask = tuple->t_data->t_infomask;
...@@ -284,7 +288,7 @@ pgrowlocks(PG_FUNCTION_ARGS) ...@@ -284,7 +288,7 @@ pgrowlocks(PG_FUNCTION_ARGS)
BackendXidGetPid(xmax)); BackendXidGetPid(xmax));
} }
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK); LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
/* build a tuple */ /* build a tuple */
tuple = BuildTupleFromCStrings(attinmeta, values); tuple = BuildTupleFromCStrings(attinmeta, values);
...@@ -301,11 +305,11 @@ pgrowlocks(PG_FUNCTION_ARGS) ...@@ -301,11 +305,11 @@ pgrowlocks(PG_FUNCTION_ARGS)
} }
else else
{ {
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK); LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
} }
} }
heap_endscan(scan); table_endscan(scan);
table_close(mydata->rel, AccessShareLock); table_close(mydata->rel, AccessShareLock);
SRF_RETURN_DONE(funcctx); SRF_RETURN_DONE(funcctx);
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "access/heapam.h" #include "access/heapam.h"
#include "access/nbtree.h" #include "access/nbtree.h"
#include "access/relscan.h" #include "access/relscan.h"
#include "access/tableam.h"
#include "catalog/namespace.h" #include "catalog/namespace.h"
#include "catalog/pg_am.h" #include "catalog/pg_am.h"
#include "funcapi.h" #include "funcapi.h"
...@@ -317,7 +318,8 @@ pgstat_relation(Relation rel, FunctionCallInfo fcinfo) ...@@ -317,7 +318,8 @@ pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
static Datum static Datum
pgstat_heap(Relation rel, FunctionCallInfo fcinfo) pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
{ {
HeapScanDesc scan; TableScanDesc scan;
HeapScanDesc hscan;
HeapTuple tuple; HeapTuple tuple;
BlockNumber nblocks; BlockNumber nblocks;
BlockNumber block = 0; /* next block to count free space in */ BlockNumber block = 0; /* next block to count free space in */
...@@ -327,10 +329,12 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo) ...@@ -327,10 +329,12 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
SnapshotData SnapshotDirty; SnapshotData SnapshotDirty;
/* Disable syncscan because we assume we scan from block zero upwards */ /* Disable syncscan because we assume we scan from block zero upwards */
scan = heap_beginscan_strat(rel, SnapshotAny, 0, NULL, true, false); scan = table_beginscan_strat(rel, SnapshotAny, 0, NULL, true, false);
hscan = (HeapScanDesc) scan;
InitDirtySnapshot(SnapshotDirty); InitDirtySnapshot(SnapshotDirty);
nblocks = scan->rs_nblocks; /* # blocks to be scanned */ nblocks = hscan->rs_nblocks; /* # blocks to be scanned */
/* scan the relation */ /* scan the relation */
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
...@@ -338,9 +342,9 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo) ...@@ -338,9 +342,9 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
CHECK_FOR_INTERRUPTS(); CHECK_FOR_INTERRUPTS();
/* must hold a buffer lock to call HeapTupleSatisfiesVisibility */ /* must hold a buffer lock to call HeapTupleSatisfiesVisibility */
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE); LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
if (HeapTupleSatisfiesVisibility(tuple, &SnapshotDirty, scan->rs_cbuf)) if (HeapTupleSatisfiesVisibility(tuple, &SnapshotDirty, hscan->rs_cbuf))
{ {
stat.tuple_len += tuple->t_len; stat.tuple_len += tuple->t_len;
stat.tuple_count++; stat.tuple_count++;
...@@ -351,7 +355,7 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo) ...@@ -351,7 +355,7 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
stat.dead_tuple_count++; stat.dead_tuple_count++;
} }
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK); LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
/* /*
* To avoid physically reading the table twice, try to do the * To avoid physically reading the table twice, try to do the
...@@ -366,7 +370,7 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo) ...@@ -366,7 +370,7 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
CHECK_FOR_INTERRUPTS(); CHECK_FOR_INTERRUPTS();
buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block, buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block,
RBM_NORMAL, scan->rs_strategy); RBM_NORMAL, hscan->rs_strategy);
LockBuffer(buffer, BUFFER_LOCK_SHARE); LockBuffer(buffer, BUFFER_LOCK_SHARE);
stat.free_space += PageGetHeapFreeSpace((Page) BufferGetPage(buffer)); stat.free_space += PageGetHeapFreeSpace((Page) BufferGetPage(buffer));
UnlockReleaseBuffer(buffer); UnlockReleaseBuffer(buffer);
...@@ -379,14 +383,14 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo) ...@@ -379,14 +383,14 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
CHECK_FOR_INTERRUPTS(); CHECK_FOR_INTERRUPTS();
buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block, buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block,
RBM_NORMAL, scan->rs_strategy); RBM_NORMAL, hscan->rs_strategy);
LockBuffer(buffer, BUFFER_LOCK_SHARE); LockBuffer(buffer, BUFFER_LOCK_SHARE);
stat.free_space += PageGetHeapFreeSpace((Page) BufferGetPage(buffer)); stat.free_space += PageGetHeapFreeSpace((Page) BufferGetPage(buffer));
UnlockReleaseBuffer(buffer); UnlockReleaseBuffer(buffer);
block++; block++;
} }
heap_endscan(scan); table_endscan(scan);
relation_close(rel, AccessShareLock); relation_close(rel, AccessShareLock);
stat.table_len = (uint64) nblocks * BLCKSZ; stat.table_len = (uint64) nblocks * BLCKSZ;
......
...@@ -209,7 +209,8 @@ static BlockNumber ...@@ -209,7 +209,8 @@ static BlockNumber
system_rows_nextsampleblock(SampleScanState *node) system_rows_nextsampleblock(SampleScanState *node)
{ {
SystemRowsSamplerData *sampler = (SystemRowsSamplerData *) node->tsm_state; SystemRowsSamplerData *sampler = (SystemRowsSamplerData *) node->tsm_state;
HeapScanDesc scan = node->ss.ss_currentScanDesc; TableScanDesc scan = node->ss.ss_currentScanDesc;
HeapScanDesc hscan = (HeapScanDesc) scan;
/* First call within scan? */ /* First call within scan? */
if (sampler->doneblocks == 0) if (sampler->doneblocks == 0)
...@@ -221,14 +222,14 @@ system_rows_nextsampleblock(SampleScanState *node) ...@@ -221,14 +222,14 @@ system_rows_nextsampleblock(SampleScanState *node)
SamplerRandomState randstate; SamplerRandomState randstate;
/* If relation is empty, there's nothing to scan */ /* If relation is empty, there's nothing to scan */
if (scan->rs_nblocks == 0) if (hscan->rs_nblocks == 0)
return InvalidBlockNumber; return InvalidBlockNumber;
/* We only need an RNG during this setup step */ /* We only need an RNG during this setup step */
sampler_random_init_state(sampler->seed, randstate); sampler_random_init_state(sampler->seed, randstate);
/* Compute nblocks/firstblock/step only once per query */ /* Compute nblocks/firstblock/step only once per query */
sampler->nblocks = scan->rs_nblocks; sampler->nblocks = hscan->rs_nblocks;
/* Choose random starting block within the relation */ /* Choose random starting block within the relation */
/* (Actually this is the predecessor of the first block visited) */ /* (Actually this is the predecessor of the first block visited) */
...@@ -258,7 +259,7 @@ system_rows_nextsampleblock(SampleScanState *node) ...@@ -258,7 +259,7 @@ system_rows_nextsampleblock(SampleScanState *node)
{ {
/* Advance lb, using uint64 arithmetic to forestall overflow */ /* Advance lb, using uint64 arithmetic to forestall overflow */
sampler->lb = ((uint64) sampler->lb + sampler->step) % sampler->nblocks; sampler->lb = ((uint64) sampler->lb + sampler->step) % sampler->nblocks;
} while (sampler->lb >= scan->rs_nblocks); } while (sampler->lb >= hscan->rs_nblocks);
return sampler->lb; return sampler->lb;
} }
...@@ -278,7 +279,8 @@ system_rows_nextsampletuple(SampleScanState *node, ...@@ -278,7 +279,8 @@ system_rows_nextsampletuple(SampleScanState *node,
OffsetNumber maxoffset) OffsetNumber maxoffset)
{ {
SystemRowsSamplerData *sampler = (SystemRowsSamplerData *) node->tsm_state; SystemRowsSamplerData *sampler = (SystemRowsSamplerData *) node->tsm_state;
HeapScanDesc scan = node->ss.ss_currentScanDesc; TableScanDesc scan = node->ss.ss_currentScanDesc;
HeapScanDesc hscan = (HeapScanDesc) scan;
OffsetNumber tupoffset = sampler->lt; OffsetNumber tupoffset = sampler->lt;
/* Quit if we've returned all needed tuples */ /* Quit if we've returned all needed tuples */
...@@ -308,7 +310,7 @@ system_rows_nextsampletuple(SampleScanState *node, ...@@ -308,7 +310,7 @@ system_rows_nextsampletuple(SampleScanState *node,
} }
/* Found a candidate? */ /* Found a candidate? */
if (SampleOffsetVisible(tupoffset, scan)) if (SampleOffsetVisible(tupoffset, hscan))
{ {
sampler->donetuples++; sampler->donetuples++;
break; break;
......
...@@ -216,7 +216,8 @@ static BlockNumber ...@@ -216,7 +216,8 @@ static BlockNumber
system_time_nextsampleblock(SampleScanState *node) system_time_nextsampleblock(SampleScanState *node)
{ {
SystemTimeSamplerData *sampler = (SystemTimeSamplerData *) node->tsm_state; SystemTimeSamplerData *sampler = (SystemTimeSamplerData *) node->tsm_state;
HeapScanDesc scan = node->ss.ss_currentScanDesc; TableScanDesc scan = node->ss.ss_currentScanDesc;
HeapScanDesc hscan = (HeapScanDesc) scan;
instr_time cur_time; instr_time cur_time;
/* First call within scan? */ /* First call within scan? */
...@@ -229,14 +230,14 @@ system_time_nextsampleblock(SampleScanState *node) ...@@ -229,14 +230,14 @@ system_time_nextsampleblock(SampleScanState *node)
SamplerRandomState randstate; SamplerRandomState randstate;
/* If relation is empty, there's nothing to scan */ /* If relation is empty, there's nothing to scan */
if (scan->rs_nblocks == 0) if (hscan->rs_nblocks == 0)
return InvalidBlockNumber; return InvalidBlockNumber;
/* We only need an RNG during this setup step */ /* We only need an RNG during this setup step */
sampler_random_init_state(sampler->seed, randstate); sampler_random_init_state(sampler->seed, randstate);
/* Compute nblocks/firstblock/step only once per query */ /* Compute nblocks/firstblock/step only once per query */
sampler->nblocks = scan->rs_nblocks; sampler->nblocks = hscan->rs_nblocks;
/* Choose random starting block within the relation */ /* Choose random starting block within the relation */
/* (Actually this is the predecessor of the first block visited) */ /* (Actually this is the predecessor of the first block visited) */
...@@ -272,7 +273,7 @@ system_time_nextsampleblock(SampleScanState *node) ...@@ -272,7 +273,7 @@ system_time_nextsampleblock(SampleScanState *node)
{ {
/* Advance lb, using uint64 arithmetic to forestall overflow */ /* Advance lb, using uint64 arithmetic to forestall overflow */
sampler->lb = ((uint64) sampler->lb + sampler->step) % sampler->nblocks; sampler->lb = ((uint64) sampler->lb + sampler->step) % sampler->nblocks;
} while (sampler->lb >= scan->rs_nblocks); } while (sampler->lb >= hscan->rs_nblocks);
return sampler->lb; return sampler->lb;
} }
......
...@@ -561,7 +561,7 @@ getNextNearest(IndexScanDesc scan) ...@@ -561,7 +561,7 @@ getNextNearest(IndexScanDesc scan)
if (GISTSearchItemIsHeap(*item)) if (GISTSearchItemIsHeap(*item))
{ {
/* found a heap item at currently minimal distance */ /* found a heap item at currently minimal distance */
scan->xs_ctup.t_self = item->data.heap.heapPtr; scan->xs_heaptid = item->data.heap.heapPtr;
scan->xs_recheck = item->data.heap.recheck; scan->xs_recheck = item->data.heap.recheck;
index_store_float8_orderby_distances(scan, so->orderByTypes, index_store_float8_orderby_distances(scan, so->orderByTypes,
...@@ -650,7 +650,7 @@ gistgettuple(IndexScanDesc scan, ScanDirection dir) ...@@ -650,7 +650,7 @@ gistgettuple(IndexScanDesc scan, ScanDirection dir)
so->pageData[so->curPageData - 1].offnum; so->pageData[so->curPageData - 1].offnum;
} }
/* continuing to return tuples from a leaf page */ /* continuing to return tuples from a leaf page */
scan->xs_ctup.t_self = so->pageData[so->curPageData].heapPtr; scan->xs_heaptid = so->pageData[so->curPageData].heapPtr;
scan->xs_recheck = so->pageData[so->curPageData].recheck; scan->xs_recheck = so->pageData[so->curPageData].recheck;
/* in an index-only scan, also return the reconstructed tuple */ /* in an index-only scan, also return the reconstructed tuple */
......
...@@ -119,7 +119,7 @@ _hash_next(IndexScanDesc scan, ScanDirection dir) ...@@ -119,7 +119,7 @@ _hash_next(IndexScanDesc scan, ScanDirection dir)
/* OK, itemIndex says what to return */ /* OK, itemIndex says what to return */
currItem = &so->currPos.items[so->currPos.itemIndex]; currItem = &so->currPos.items[so->currPos.itemIndex];
scan->xs_ctup.t_self = currItem->heapTid; scan->xs_heaptid = currItem->heapTid;
return true; return true;
} }
...@@ -432,7 +432,7 @@ _hash_first(IndexScanDesc scan, ScanDirection dir) ...@@ -432,7 +432,7 @@ _hash_first(IndexScanDesc scan, ScanDirection dir)
/* OK, itemIndex says what to return */ /* OK, itemIndex says what to return */
currItem = &so->currPos.items[so->currPos.itemIndex]; currItem = &so->currPos.items[so->currPos.itemIndex];
scan->xs_ctup.t_self = currItem->heapTid; scan->xs_heaptid = currItem->heapTid;
/* if we're here, _hash_readpage found a valid tuples */ /* if we're here, _hash_readpage found a valid tuples */
return true; return true;
......
This diff is collapsed.
...@@ -19,15 +19,181 @@ ...@@ -19,15 +19,181 @@
*/ */
#include "postgres.h" #include "postgres.h"
#include "access/heapam.h"
#include "access/tableam.h" #include "access/tableam.h"
#include "storage/bufmgr.h"
#include "utils/builtins.h" #include "utils/builtins.h"
static const TableAmRoutine heapam_methods; static const TableAmRoutine heapam_methods;
/* ------------------------------------------------------------------------
* Slot related callbacks for heap AM
* ------------------------------------------------------------------------
*/
static const TupleTableSlotOps *
heapam_slot_callbacks(Relation relation)
{
return &TTSOpsBufferHeapTuple;
}
/* ------------------------------------------------------------------------
* Index Scan Callbacks for heap AM
* ------------------------------------------------------------------------
*/
static IndexFetchTableData *
heapam_index_fetch_begin(Relation rel)
{
IndexFetchHeapData *hscan = palloc0(sizeof(IndexFetchHeapData));
hscan->xs_base.rel = rel;
hscan->xs_cbuf = InvalidBuffer;
return &hscan->xs_base;
}
static void
heapam_index_fetch_reset(IndexFetchTableData *scan)
{
IndexFetchHeapData *hscan = (IndexFetchHeapData *) scan;
if (BufferIsValid(hscan->xs_cbuf))
{
ReleaseBuffer(hscan->xs_cbuf);
hscan->xs_cbuf = InvalidBuffer;
}
}
static void
heapam_index_fetch_end(IndexFetchTableData *scan)
{
IndexFetchHeapData *hscan = (IndexFetchHeapData *) scan;
heapam_index_fetch_reset(scan);
pfree(hscan);
}
static bool
heapam_index_fetch_tuple(struct IndexFetchTableData *scan,
ItemPointer tid,
Snapshot snapshot,
TupleTableSlot *slot,
bool *call_again, bool *all_dead)
{
IndexFetchHeapData *hscan = (IndexFetchHeapData *) scan;
BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
bool got_heap_tuple;
Assert(TTS_IS_BUFFERTUPLE(slot));
/* We can skip the buffer-switching logic if we're in mid-HOT chain. */
if (!*call_again)
{
/* Switch to correct buffer if we don't have it already */
Buffer prev_buf = hscan->xs_cbuf;
hscan->xs_cbuf = ReleaseAndReadBuffer(hscan->xs_cbuf,
hscan->xs_base.rel,
ItemPointerGetBlockNumber(tid));
/*
* Prune page, but only if we weren't already on this page
*/
if (prev_buf != hscan->xs_cbuf)
heap_page_prune_opt(hscan->xs_base.rel, hscan->xs_cbuf);
}
/* Obtain share-lock on the buffer so we can examine visibility */
LockBuffer(hscan->xs_cbuf, BUFFER_LOCK_SHARE);
got_heap_tuple = heap_hot_search_buffer(tid,
hscan->xs_base.rel,
hscan->xs_cbuf,
snapshot,
&bslot->base.tupdata,
all_dead,
!*call_again);
bslot->base.tupdata.t_self = *tid;
LockBuffer(hscan->xs_cbuf, BUFFER_LOCK_UNLOCK);
if (got_heap_tuple)
{
/*
* Only in a non-MVCC snapshot can more than one member of the HOT
* chain be visible.
*/
*call_again = !IsMVCCSnapshot(snapshot);
slot->tts_tableOid = RelationGetRelid(scan->rel);
ExecStoreBufferHeapTuple(&bslot->base.tupdata, slot, hscan->xs_cbuf);
}
else
{
/* We've reached the end of the HOT chain. */
*call_again = false;
}
return got_heap_tuple;
}
/* ------------------------------------------------------------------------
* Callbacks for non-modifying operations on individual tuples for heap AM
* ------------------------------------------------------------------------
*/
static bool
heapam_tuple_satisfies_snapshot(Relation rel, TupleTableSlot *slot,
Snapshot snapshot)
{
BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
bool res;
Assert(TTS_IS_BUFFERTUPLE(slot));
Assert(BufferIsValid(bslot->buffer));
/*
* We need buffer pin and lock to call HeapTupleSatisfiesVisibility.
* Caller should be holding pin, but not lock.
*/
LockBuffer(bslot->buffer, BUFFER_LOCK_SHARE);
res = HeapTupleSatisfiesVisibility(bslot->base.tuple, snapshot,
bslot->buffer);
LockBuffer(bslot->buffer, BUFFER_LOCK_UNLOCK);
return res;
}
/* ------------------------------------------------------------------------
* Definition of the heap table access method.
* ------------------------------------------------------------------------
*/
static const TableAmRoutine heapam_methods = { static const TableAmRoutine heapam_methods = {
.type = T_TableAmRoutine, .type = T_TableAmRoutine,
.slot_callbacks = heapam_slot_callbacks,
.scan_begin = heap_beginscan,
.scan_end = heap_endscan,
.scan_rescan = heap_rescan,
.scan_getnextslot = heap_getnextslot,
.parallelscan_estimate = table_block_parallelscan_estimate,
.parallelscan_initialize = table_block_parallelscan_initialize,
.parallelscan_reinitialize = table_block_parallelscan_reinitialize,
.index_fetch_begin = heapam_index_fetch_begin,
.index_fetch_reset = heapam_index_fetch_reset,
.index_fetch_end = heapam_index_fetch_end,
.index_fetch_tuple = heapam_index_fetch_tuple,
.tuple_satisfies_snapshot = heapam_tuple_satisfies_snapshot,
}; };
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "access/genam.h" #include "access/genam.h"
#include "access/heapam.h" #include "access/heapam.h"
#include "access/relscan.h" #include "access/relscan.h"
#include "access/tableam.h"
#include "access/transam.h" #include "access/transam.h"
#include "catalog/index.h" #include "catalog/index.h"
#include "lib/stringinfo.h" #include "lib/stringinfo.h"
...@@ -83,6 +84,7 @@ RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys) ...@@ -83,6 +84,7 @@ RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
scan = (IndexScanDesc) palloc(sizeof(IndexScanDescData)); scan = (IndexScanDesc) palloc(sizeof(IndexScanDescData));
scan->heapRelation = NULL; /* may be set later */ scan->heapRelation = NULL; /* may be set later */
scan->xs_heapfetch = NULL;
scan->indexRelation = indexRelation; scan->indexRelation = indexRelation;
scan->xs_snapshot = InvalidSnapshot; /* caller must initialize this */ scan->xs_snapshot = InvalidSnapshot; /* caller must initialize this */
scan->numberOfKeys = nkeys; scan->numberOfKeys = nkeys;
...@@ -123,11 +125,6 @@ RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys) ...@@ -123,11 +125,6 @@ RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
scan->xs_hitup = NULL; scan->xs_hitup = NULL;
scan->xs_hitupdesc = NULL; scan->xs_hitupdesc = NULL;
ItemPointerSetInvalid(&scan->xs_ctup.t_self);
scan->xs_ctup.t_data = NULL;
scan->xs_cbuf = InvalidBuffer;
scan->xs_continue_hot = false;
return scan; return scan;
} }
...@@ -335,6 +332,7 @@ systable_beginscan(Relation heapRelation, ...@@ -335,6 +332,7 @@ systable_beginscan(Relation heapRelation,
sysscan->heap_rel = heapRelation; sysscan->heap_rel = heapRelation;
sysscan->irel = irel; sysscan->irel = irel;
sysscan->slot = table_slot_create(heapRelation, NULL);
if (snapshot == NULL) if (snapshot == NULL)
{ {
...@@ -384,9 +382,9 @@ systable_beginscan(Relation heapRelation, ...@@ -384,9 +382,9 @@ systable_beginscan(Relation heapRelation,
* disadvantage; and there are no compensating advantages, because * disadvantage; and there are no compensating advantages, because
* it's unlikely that such scans will occur in parallel. * it's unlikely that such scans will occur in parallel.
*/ */
sysscan->scan = heap_beginscan_strat(heapRelation, snapshot, sysscan->scan = table_beginscan_strat(heapRelation, snapshot,
nkeys, key, nkeys, key,
true, false); true, false);
sysscan->iscan = NULL; sysscan->iscan = NULL;
} }
...@@ -401,28 +399,46 @@ systable_beginscan(Relation heapRelation, ...@@ -401,28 +399,46 @@ systable_beginscan(Relation heapRelation,
* Note that returned tuple is a reference to data in a disk buffer; * Note that returned tuple is a reference to data in a disk buffer;
* it must not be modified, and should be presumed inaccessible after * it must not be modified, and should be presumed inaccessible after
* next getnext() or endscan() call. * next getnext() or endscan() call.
*
* XXX: It'd probably make sense to offer a slot based interface, at least
* optionally.
*/ */
HeapTuple HeapTuple
systable_getnext(SysScanDesc sysscan) systable_getnext(SysScanDesc sysscan)
{ {
HeapTuple htup; HeapTuple htup = NULL;
if (sysscan->irel) if (sysscan->irel)
{ {
htup = index_getnext(sysscan->iscan, ForwardScanDirection); if (index_getnext_slot(sysscan->iscan, ForwardScanDirection, sysscan->slot))
{
bool shouldFree;
/* htup = ExecFetchSlotHeapTuple(sysscan->slot, false, &shouldFree);
* We currently don't need to support lossy index operators for any Assert(!shouldFree);
* system catalog scan. It could be done here, using the scan keys to
* drive the operator calls, if we arranged to save the heap attnums /*
* during systable_beginscan(); this is practical because we still * We currently don't need to support lossy index operators for
* wouldn't need to support indexes on expressions. * any system catalog scan. It could be done here, using the scan
*/ * keys to drive the operator calls, if we arranged to save the
if (htup && sysscan->iscan->xs_recheck) * heap attnums during systable_beginscan(); this is practical
elog(ERROR, "system catalog scans with lossy index conditions are not implemented"); * because we still wouldn't need to support indexes on
* expressions.
*/
if (sysscan->iscan->xs_recheck)
elog(ERROR, "system catalog scans with lossy index conditions are not implemented");
}
} }
else else
htup = heap_getnext(sysscan->scan, ForwardScanDirection); {
if (table_scan_getnextslot(sysscan->scan, ForwardScanDirection, sysscan->slot))
{
bool shouldFree;
htup = ExecFetchSlotHeapTuple(sysscan->slot, false, &shouldFree);
Assert(!shouldFree);
}
}
return htup; return htup;
} }
...@@ -446,37 +462,20 @@ systable_recheck_tuple(SysScanDesc sysscan, HeapTuple tup) ...@@ -446,37 +462,20 @@ systable_recheck_tuple(SysScanDesc sysscan, HeapTuple tup)
Snapshot freshsnap; Snapshot freshsnap;
bool result; bool result;
Assert(tup == ExecFetchSlotHeapTuple(sysscan->slot, false, NULL));
/* /*
* Trust that LockBuffer() and HeapTupleSatisfiesMVCC() do not themselves * Trust that table_tuple_satisfies_snapshot() and its subsidiaries
* (commonly LockBuffer() and HeapTupleSatisfiesMVCC()) do not themselves
* acquire snapshots, so we need not register the snapshot. Those * acquire snapshots, so we need not register the snapshot. Those
* facilities are too low-level to have any business scanning tables. * facilities are too low-level to have any business scanning tables.
*/ */
freshsnap = GetCatalogSnapshot(RelationGetRelid(sysscan->heap_rel)); freshsnap = GetCatalogSnapshot(RelationGetRelid(sysscan->heap_rel));
if (sysscan->irel) result = table_tuple_satisfies_snapshot(sysscan->heap_rel,
{ sysscan->slot,
IndexScanDesc scan = sysscan->iscan; freshsnap);
Assert(IsMVCCSnapshot(scan->xs_snapshot));
Assert(tup == &scan->xs_ctup);
Assert(BufferIsValid(scan->xs_cbuf));
/* must hold a buffer lock to call HeapTupleSatisfiesVisibility */
LockBuffer(scan->xs_cbuf, BUFFER_LOCK_SHARE);
result = HeapTupleSatisfiesVisibility(tup, freshsnap, scan->xs_cbuf);
LockBuffer(scan->xs_cbuf, BUFFER_LOCK_UNLOCK);
}
else
{
HeapScanDesc scan = sysscan->scan;
Assert(IsMVCCSnapshot(scan->rs_snapshot));
Assert(tup == &scan->rs_ctup);
Assert(BufferIsValid(scan->rs_cbuf));
/* must hold a buffer lock to call HeapTupleSatisfiesVisibility */
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE);
result = HeapTupleSatisfiesVisibility(tup, freshsnap, scan->rs_cbuf);
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK);
}
return result; return result;
} }
...@@ -488,13 +487,19 @@ systable_recheck_tuple(SysScanDesc sysscan, HeapTuple tup) ...@@ -488,13 +487,19 @@ systable_recheck_tuple(SysScanDesc sysscan, HeapTuple tup)
void void
systable_endscan(SysScanDesc sysscan) systable_endscan(SysScanDesc sysscan)
{ {
if (sysscan->slot)
{
ExecDropSingleTupleTableSlot(sysscan->slot);
sysscan->slot = NULL;
}
if (sysscan->irel) if (sysscan->irel)
{ {
index_endscan(sysscan->iscan); index_endscan(sysscan->iscan);
index_close(sysscan->irel, AccessShareLock); index_close(sysscan->irel, AccessShareLock);
} }
else else
heap_endscan(sysscan->scan); table_endscan(sysscan->scan);
if (sysscan->snapshot) if (sysscan->snapshot)
UnregisterSnapshot(sysscan->snapshot); UnregisterSnapshot(sysscan->snapshot);
...@@ -541,6 +546,7 @@ systable_beginscan_ordered(Relation heapRelation, ...@@ -541,6 +546,7 @@ systable_beginscan_ordered(Relation heapRelation,
sysscan->heap_rel = heapRelation; sysscan->heap_rel = heapRelation;
sysscan->irel = indexRelation; sysscan->irel = indexRelation;
sysscan->slot = table_slot_create(heapRelation, NULL);
if (snapshot == NULL) if (snapshot == NULL)
{ {
...@@ -586,10 +592,12 @@ systable_beginscan_ordered(Relation heapRelation, ...@@ -586,10 +592,12 @@ systable_beginscan_ordered(Relation heapRelation,
HeapTuple HeapTuple
systable_getnext_ordered(SysScanDesc sysscan, ScanDirection direction) systable_getnext_ordered(SysScanDesc sysscan, ScanDirection direction)
{ {
HeapTuple htup; HeapTuple htup = NULL;
Assert(sysscan->irel); Assert(sysscan->irel);
htup = index_getnext(sysscan->iscan, direction); if (index_getnext_slot(sysscan->iscan, direction, sysscan->slot))
htup = ExecFetchSlotHeapTuple(sysscan->slot, false, NULL);
/* See notes in systable_getnext */ /* See notes in systable_getnext */
if (htup && sysscan->iscan->xs_recheck) if (htup && sysscan->iscan->xs_recheck)
elog(ERROR, "system catalog scans with lossy index conditions are not implemented"); elog(ERROR, "system catalog scans with lossy index conditions are not implemented");
...@@ -603,6 +611,12 @@ systable_getnext_ordered(SysScanDesc sysscan, ScanDirection direction) ...@@ -603,6 +611,12 @@ systable_getnext_ordered(SysScanDesc sysscan, ScanDirection direction)
void void
systable_endscan_ordered(SysScanDesc sysscan) systable_endscan_ordered(SysScanDesc sysscan)
{ {
if (sysscan->slot)
{
ExecDropSingleTupleTableSlot(sysscan->slot);
sysscan->slot = NULL;
}
Assert(sysscan->irel); Assert(sysscan->irel);
index_endscan(sysscan->iscan); index_endscan(sysscan->iscan);
if (sysscan->snapshot) if (sysscan->snapshot)
......
...@@ -72,6 +72,7 @@ ...@@ -72,6 +72,7 @@
#include "access/amapi.h" #include "access/amapi.h"
#include "access/heapam.h" #include "access/heapam.h"
#include "access/relscan.h" #include "access/relscan.h"
#include "access/tableam.h"
#include "access/transam.h" #include "access/transam.h"
#include "access/xlog.h" #include "access/xlog.h"
#include "catalog/index.h" #include "catalog/index.h"
...@@ -235,6 +236,9 @@ index_beginscan(Relation heapRelation, ...@@ -235,6 +236,9 @@ index_beginscan(Relation heapRelation,
scan->heapRelation = heapRelation; scan->heapRelation = heapRelation;
scan->xs_snapshot = snapshot; scan->xs_snapshot = snapshot;
/* prepare to fetch index matches from table */
scan->xs_heapfetch = table_index_fetch_begin(heapRelation);
return scan; return scan;
} }
...@@ -318,16 +322,12 @@ index_rescan(IndexScanDesc scan, ...@@ -318,16 +322,12 @@ index_rescan(IndexScanDesc scan,
Assert(nkeys == scan->numberOfKeys); Assert(nkeys == scan->numberOfKeys);
Assert(norderbys == scan->numberOfOrderBys); Assert(norderbys == scan->numberOfOrderBys);
/* Release any held pin on a heap page */ /* Release resources (like buffer pins) from table accesses */
if (BufferIsValid(scan->xs_cbuf)) if (scan->xs_heapfetch)
{ table_index_fetch_reset(scan->xs_heapfetch);
ReleaseBuffer(scan->xs_cbuf);
scan->xs_cbuf = InvalidBuffer;
}
scan->xs_continue_hot = false;
scan->kill_prior_tuple = false; /* for safety */ scan->kill_prior_tuple = false; /* for safety */
scan->xs_heap_continue = false;
scan->indexRelation->rd_indam->amrescan(scan, keys, nkeys, scan->indexRelation->rd_indam->amrescan(scan, keys, nkeys,
orderbys, norderbys); orderbys, norderbys);
...@@ -343,11 +343,11 @@ index_endscan(IndexScanDesc scan) ...@@ -343,11 +343,11 @@ index_endscan(IndexScanDesc scan)
SCAN_CHECKS; SCAN_CHECKS;
CHECK_SCAN_PROCEDURE(amendscan); CHECK_SCAN_PROCEDURE(amendscan);
/* Release any held pin on a heap page */ /* Release resources (like buffer pins) from table accesses */
if (BufferIsValid(scan->xs_cbuf)) if (scan->xs_heapfetch)
{ {
ReleaseBuffer(scan->xs_cbuf); table_index_fetch_end(scan->xs_heapfetch);
scan->xs_cbuf = InvalidBuffer; scan->xs_heapfetch = NULL;
} }
/* End the AM's scan */ /* End the AM's scan */
...@@ -379,17 +379,16 @@ index_markpos(IndexScanDesc scan) ...@@ -379,17 +379,16 @@ index_markpos(IndexScanDesc scan)
/* ---------------- /* ----------------
* index_restrpos - restore a scan position * index_restrpos - restore a scan position
* *
* NOTE: this only restores the internal scan state of the index AM. * NOTE: this only restores the internal scan state of the index AM. See
* The current result tuple (scan->xs_ctup) doesn't change. See comments * comments for ExecRestrPos().
* for ExecRestrPos(). *
* * NOTE: For heap, in the presence of HOT chains, mark/restore only works
* NOTE: in the presence of HOT chains, mark/restore only works correctly * correctly if the scan's snapshot is MVCC-safe; that ensures that there's at
* if the scan's snapshot is MVCC-safe; that ensures that there's at most one * most one returnable tuple in each HOT chain, and so restoring the prior
* returnable tuple in each HOT chain, and so restoring the prior state at the * state at the granularity of the index AM is sufficient. Since the only
* granularity of the index AM is sufficient. Since the only current user * current user of mark/restore functionality is nodeMergejoin.c, this
* of mark/restore functionality is nodeMergejoin.c, this effectively means * effectively means that merge-join plans only work for MVCC snapshots. This
* that merge-join plans only work for MVCC snapshots. This could be fixed * could be fixed if necessary, but for now it seems unimportant.
* if necessary, but for now it seems unimportant.
* ---------------- * ----------------
*/ */
void void
...@@ -400,9 +399,12 @@ index_restrpos(IndexScanDesc scan) ...@@ -400,9 +399,12 @@ index_restrpos(IndexScanDesc scan)
SCAN_CHECKS; SCAN_CHECKS;
CHECK_SCAN_PROCEDURE(amrestrpos); CHECK_SCAN_PROCEDURE(amrestrpos);
scan->xs_continue_hot = false; /* release resources (like buffer pins) from table accesses */
if (scan->xs_heapfetch)
table_index_fetch_reset(scan->xs_heapfetch);
scan->kill_prior_tuple = false; /* for safety */ scan->kill_prior_tuple = false; /* for safety */
scan->xs_heap_continue = false;
scan->indexRelation->rd_indam->amrestrpos(scan); scan->indexRelation->rd_indam->amrestrpos(scan);
} }
...@@ -483,6 +485,9 @@ index_parallelrescan(IndexScanDesc scan) ...@@ -483,6 +485,9 @@ index_parallelrescan(IndexScanDesc scan)
{ {
SCAN_CHECKS; SCAN_CHECKS;
if (scan->xs_heapfetch)
table_index_fetch_reset(scan->xs_heapfetch);
/* amparallelrescan is optional; assume no-op if not provided by AM */ /* amparallelrescan is optional; assume no-op if not provided by AM */
if (scan->indexRelation->rd_indam->amparallelrescan != NULL) if (scan->indexRelation->rd_indam->amparallelrescan != NULL)
scan->indexRelation->rd_indam->amparallelrescan(scan); scan->indexRelation->rd_indam->amparallelrescan(scan);
...@@ -513,6 +518,9 @@ index_beginscan_parallel(Relation heaprel, Relation indexrel, int nkeys, ...@@ -513,6 +518,9 @@ index_beginscan_parallel(Relation heaprel, Relation indexrel, int nkeys,
scan->heapRelation = heaprel; scan->heapRelation = heaprel;
scan->xs_snapshot = snapshot; scan->xs_snapshot = snapshot;
/* prepare to fetch index matches from table */
scan->xs_heapfetch = table_index_fetch_begin(heaprel);
return scan; return scan;
} }
...@@ -535,7 +543,7 @@ index_getnext_tid(IndexScanDesc scan, ScanDirection direction) ...@@ -535,7 +543,7 @@ index_getnext_tid(IndexScanDesc scan, ScanDirection direction)
/* /*
* The AM's amgettuple proc finds the next index entry matching the scan * The AM's amgettuple proc finds the next index entry matching the scan
* keys, and puts the TID into scan->xs_ctup.t_self. It should also set * keys, and puts the TID into scan->xs_heaptid. It should also set
* scan->xs_recheck and possibly scan->xs_itup/scan->xs_hitup, though we * scan->xs_recheck and possibly scan->xs_itup/scan->xs_hitup, though we
* pay no attention to those fields here. * pay no attention to those fields here.
*/ */
...@@ -543,23 +551,23 @@ index_getnext_tid(IndexScanDesc scan, ScanDirection direction) ...@@ -543,23 +551,23 @@ index_getnext_tid(IndexScanDesc scan, ScanDirection direction)
/* Reset kill flag immediately for safety */ /* Reset kill flag immediately for safety */
scan->kill_prior_tuple = false; scan->kill_prior_tuple = false;
scan->xs_heap_continue = false;
/* If we're out of index entries, we're done */ /* If we're out of index entries, we're done */
if (!found) if (!found)
{ {
/* ... but first, release any held pin on a heap page */ /* release resources (like buffer pins) from table accesses */
if (BufferIsValid(scan->xs_cbuf)) if (scan->xs_heapfetch)
{ table_index_fetch_reset(scan->xs_heapfetch);
ReleaseBuffer(scan->xs_cbuf);
scan->xs_cbuf = InvalidBuffer;
}
return NULL; return NULL;
} }
Assert(ItemPointerIsValid(&scan->xs_heaptid));
pgstat_count_index_tuples(scan->indexRelation, 1); pgstat_count_index_tuples(scan->indexRelation, 1);
/* Return the TID of the tuple we found. */ /* Return the TID of the tuple we found. */
return &scan->xs_ctup.t_self; return &scan->xs_heaptid;
} }
/* ---------------- /* ----------------
...@@ -580,53 +588,18 @@ index_getnext_tid(IndexScanDesc scan, ScanDirection direction) ...@@ -580,53 +588,18 @@ index_getnext_tid(IndexScanDesc scan, ScanDirection direction)
* enough information to do it efficiently in the general case. * enough information to do it efficiently in the general case.
* ---------------- * ----------------
*/ */
HeapTuple bool
index_fetch_heap(IndexScanDesc scan) index_fetch_heap(IndexScanDesc scan, TupleTableSlot *slot)
{ {
ItemPointer tid = &scan->xs_ctup.t_self;
bool all_dead = false; bool all_dead = false;
bool got_heap_tuple; bool found;
/* We can skip the buffer-switching logic if we're in mid-HOT chain. */
if (!scan->xs_continue_hot)
{
/* Switch to correct buffer if we don't have it already */
Buffer prev_buf = scan->xs_cbuf;
scan->xs_cbuf = ReleaseAndReadBuffer(scan->xs_cbuf,
scan->heapRelation,
ItemPointerGetBlockNumber(tid));
/* found = table_index_fetch_tuple(scan->xs_heapfetch, &scan->xs_heaptid,
* Prune page, but only if we weren't already on this page scan->xs_snapshot, slot,
*/ &scan->xs_heap_continue, &all_dead);
if (prev_buf != scan->xs_cbuf)
heap_page_prune_opt(scan->heapRelation, scan->xs_cbuf);
}
/* Obtain share-lock on the buffer so we can examine visibility */ if (found)
LockBuffer(scan->xs_cbuf, BUFFER_LOCK_SHARE);
got_heap_tuple = heap_hot_search_buffer(tid, scan->heapRelation,
scan->xs_cbuf,
scan->xs_snapshot,
&scan->xs_ctup,
&all_dead,
!scan->xs_continue_hot);
LockBuffer(scan->xs_cbuf, BUFFER_LOCK_UNLOCK);
if (got_heap_tuple)
{
/*
* Only in a non-MVCC snapshot can more than one member of the HOT
* chain be visible.
*/
scan->xs_continue_hot = !IsMVCCSnapshot(scan->xs_snapshot);
pgstat_count_heap_fetch(scan->indexRelation); pgstat_count_heap_fetch(scan->indexRelation);
return &scan->xs_ctup;
}
/* We've reached the end of the HOT chain. */
scan->xs_continue_hot = false;
/* /*
* If we scanned a whole HOT chain and found only dead tuples, tell index * If we scanned a whole HOT chain and found only dead tuples, tell index
...@@ -638,17 +611,17 @@ index_fetch_heap(IndexScanDesc scan) ...@@ -638,17 +611,17 @@ index_fetch_heap(IndexScanDesc scan)
if (!scan->xactStartedInRecovery) if (!scan->xactStartedInRecovery)
scan->kill_prior_tuple = all_dead; scan->kill_prior_tuple = all_dead;
return NULL; return found;
} }
/* ---------------- /* ----------------
* index_getnext - get the next heap tuple from a scan * index_getnext_slot - get the next tuple from a scan
* *
* The result is the next heap tuple satisfying the scan keys and the * The result is true if a tuple satisfying the scan keys and the snapshot was
* snapshot, or NULL if no more matching tuples exist. * found, false otherwise. The tuple is stored in the specified slot.
* *
* On success, the buffer containing the heap tup is pinned (the pin will be * On success, resources (like buffer pins) are likely to be held, and will be
* dropped in a future index_getnext_tid, index_fetch_heap or index_endscan * dropped by a future index_getnext_tid, index_fetch_heap or index_endscan
* call). * call).
* *
* Note: caller must check scan->xs_recheck, and perform rechecking of the * Note: caller must check scan->xs_recheck, and perform rechecking of the
...@@ -656,32 +629,23 @@ index_fetch_heap(IndexScanDesc scan) ...@@ -656,32 +629,23 @@ index_fetch_heap(IndexScanDesc scan)
* enough information to do it efficiently in the general case. * enough information to do it efficiently in the general case.
* ---------------- * ----------------
*/ */
HeapTuple bool
index_getnext(IndexScanDesc scan, ScanDirection direction) index_getnext_slot(IndexScanDesc scan, ScanDirection direction, TupleTableSlot *slot)
{ {
HeapTuple heapTuple;
ItemPointer tid;
for (;;) for (;;)
{ {
if (scan->xs_continue_hot) if (!scan->xs_heap_continue)
{
/*
* We are resuming scan of a HOT chain after having returned an
* earlier member. Must still hold pin on current heap page.
*/
Assert(BufferIsValid(scan->xs_cbuf));
Assert(ItemPointerGetBlockNumber(&scan->xs_ctup.t_self) ==
BufferGetBlockNumber(scan->xs_cbuf));
}
else
{ {
ItemPointer tid;
/* Time to fetch the next TID from the index */ /* Time to fetch the next TID from the index */
tid = index_getnext_tid(scan, direction); tid = index_getnext_tid(scan, direction);
/* If we're out of index entries, we're done */ /* If we're out of index entries, we're done */
if (tid == NULL) if (tid == NULL)
break; break;
Assert(ItemPointerEquals(tid, &scan->xs_heaptid));
} }
/* /*
...@@ -689,12 +653,12 @@ index_getnext(IndexScanDesc scan, ScanDirection direction) ...@@ -689,12 +653,12 @@ index_getnext(IndexScanDesc scan, ScanDirection direction)
* If we don't find anything, loop around and grab the next TID from * If we don't find anything, loop around and grab the next TID from
* the index. * the index.
*/ */
heapTuple = index_fetch_heap(scan); Assert(ItemPointerIsValid(&scan->xs_heaptid));
if (heapTuple != NULL) if (index_fetch_heap(scan, slot))
return heapTuple; return true;
} }
return NULL; /* failure exit */ return false;
} }
/* ---------------- /* ----------------
......
...@@ -310,7 +310,7 @@ btgetbitmap(IndexScanDesc scan, TIDBitmap *tbm) ...@@ -310,7 +310,7 @@ btgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
if (_bt_first(scan, ForwardScanDirection)) if (_bt_first(scan, ForwardScanDirection))
{ {
/* Save tuple ID, and continue scanning */ /* Save tuple ID, and continue scanning */
heapTid = &scan->xs_ctup.t_self; heapTid = &scan->xs_heaptid;
tbm_add_tuples(tbm, heapTid, 1, false); tbm_add_tuples(tbm, heapTid, 1, false);
ntids++; ntids++;
......
...@@ -1135,7 +1135,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir) ...@@ -1135,7 +1135,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
readcomplete: readcomplete:
/* OK, itemIndex says what to return */ /* OK, itemIndex says what to return */
currItem = &so->currPos.items[so->currPos.itemIndex]; currItem = &so->currPos.items[so->currPos.itemIndex];
scan->xs_ctup.t_self = currItem->heapTid; scan->xs_heaptid = currItem->heapTid;
if (scan->xs_want_itup) if (scan->xs_want_itup)
scan->xs_itup = (IndexTuple) (so->currTuples + currItem->tupleOffset); scan->xs_itup = (IndexTuple) (so->currTuples + currItem->tupleOffset);
...@@ -1185,7 +1185,7 @@ _bt_next(IndexScanDesc scan, ScanDirection dir) ...@@ -1185,7 +1185,7 @@ _bt_next(IndexScanDesc scan, ScanDirection dir)
/* OK, itemIndex says what to return */ /* OK, itemIndex says what to return */
currItem = &so->currPos.items[so->currPos.itemIndex]; currItem = &so->currPos.items[so->currPos.itemIndex];
scan->xs_ctup.t_self = currItem->heapTid; scan->xs_heaptid = currItem->heapTid;
if (scan->xs_want_itup) if (scan->xs_want_itup)
scan->xs_itup = (IndexTuple) (so->currTuples + currItem->tupleOffset); scan->xs_itup = (IndexTuple) (so->currTuples + currItem->tupleOffset);
...@@ -1964,7 +1964,7 @@ _bt_endpoint(IndexScanDesc scan, ScanDirection dir) ...@@ -1964,7 +1964,7 @@ _bt_endpoint(IndexScanDesc scan, ScanDirection dir)
/* OK, itemIndex says what to return */ /* OK, itemIndex says what to return */
currItem = &so->currPos.items[so->currPos.itemIndex]; currItem = &so->currPos.items[so->currPos.itemIndex];
scan->xs_ctup.t_self = currItem->heapTid; scan->xs_heaptid = currItem->heapTid;
if (scan->xs_want_itup) if (scan->xs_want_itup)
scan->xs_itup = (IndexTuple) (so->currTuples + currItem->tupleOffset); scan->xs_itup = (IndexTuple) (so->currTuples + currItem->tupleOffset);
......
...@@ -61,6 +61,7 @@ ...@@ -61,6 +61,7 @@
#include "access/nbtree.h" #include "access/nbtree.h"
#include "access/parallel.h" #include "access/parallel.h"
#include "access/relscan.h" #include "access/relscan.h"
#include "access/tableam.h"
#include "access/xact.h" #include "access/xact.h"
#include "access/xlog.h" #include "access/xlog.h"
#include "access/xloginsert.h" #include "access/xloginsert.h"
...@@ -158,9 +159,9 @@ typedef struct BTShared ...@@ -158,9 +159,9 @@ typedef struct BTShared
/* /*
* This variable-sized field must come last. * This variable-sized field must come last.
* *
* See _bt_parallel_estimate_shared() and heap_parallelscan_estimate(). * See _bt_parallel_estimate_shared() and table_parallelscan_estimate().
*/ */
ParallelHeapScanDescData heapdesc; ParallelTableScanDescData heapdesc;
} BTShared; } BTShared;
/* /*
...@@ -282,7 +283,7 @@ static void _bt_load(BTWriteState *wstate, ...@@ -282,7 +283,7 @@ static void _bt_load(BTWriteState *wstate,
static void _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, static void _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent,
int request); int request);
static void _bt_end_parallel(BTLeader *btleader); static void _bt_end_parallel(BTLeader *btleader);
static Size _bt_parallel_estimate_shared(Snapshot snapshot); static Size _bt_parallel_estimate_shared(Relation heap, Snapshot snapshot);
static double _bt_parallel_heapscan(BTBuildState *buildstate, static double _bt_parallel_heapscan(BTBuildState *buildstate,
bool *brokenhotchain); bool *brokenhotchain);
static void _bt_leader_participate_as_worker(BTBuildState *buildstate); static void _bt_leader_participate_as_worker(BTBuildState *buildstate);
...@@ -1275,7 +1276,7 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request) ...@@ -1275,7 +1276,7 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request)
* Estimate size for our own PARALLEL_KEY_BTREE_SHARED workspace, and * Estimate size for our own PARALLEL_KEY_BTREE_SHARED workspace, and
* PARALLEL_KEY_TUPLESORT tuplesort workspace * PARALLEL_KEY_TUPLESORT tuplesort workspace
*/ */
estbtshared = _bt_parallel_estimate_shared(snapshot); estbtshared = _bt_parallel_estimate_shared(btspool->heap, snapshot);
shm_toc_estimate_chunk(&pcxt->estimator, estbtshared); shm_toc_estimate_chunk(&pcxt->estimator, estbtshared);
estsort = tuplesort_estimate_shared(scantuplesortstates); estsort = tuplesort_estimate_shared(scantuplesortstates);
shm_toc_estimate_chunk(&pcxt->estimator, estsort); shm_toc_estimate_chunk(&pcxt->estimator, estsort);
...@@ -1316,7 +1317,8 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request) ...@@ -1316,7 +1317,8 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request)
btshared->havedead = false; btshared->havedead = false;
btshared->indtuples = 0.0; btshared->indtuples = 0.0;
btshared->brokenhotchain = false; btshared->brokenhotchain = false;
heap_parallelscan_initialize(&btshared->heapdesc, btspool->heap, snapshot); table_parallelscan_initialize(btspool->heap, &btshared->heapdesc,
snapshot);
/* /*
* Store shared tuplesort-private state, for which we reserved space. * Store shared tuplesort-private state, for which we reserved space.
...@@ -1403,10 +1405,10 @@ _bt_end_parallel(BTLeader *btleader) ...@@ -1403,10 +1405,10 @@ _bt_end_parallel(BTLeader *btleader)
* btree index build based on the snapshot its parallel scan will use. * btree index build based on the snapshot its parallel scan will use.
*/ */
static Size static Size
_bt_parallel_estimate_shared(Snapshot snapshot) _bt_parallel_estimate_shared(Relation heap, Snapshot snapshot)
{ {
return add_size(offsetof(BTShared, heapdesc), return add_size(offsetof(BTShared, heapdesc),
heap_parallelscan_estimate(snapshot)); table_parallelscan_estimate(heap, snapshot));
} }
/* /*
...@@ -1617,7 +1619,7 @@ _bt_parallel_scan_and_sort(BTSpool *btspool, BTSpool *btspool2, ...@@ -1617,7 +1619,7 @@ _bt_parallel_scan_and_sort(BTSpool *btspool, BTSpool *btspool2,
{ {
SortCoordinate coordinate; SortCoordinate coordinate;
BTBuildState buildstate; BTBuildState buildstate;
HeapScanDesc scan; TableScanDesc scan;
double reltuples; double reltuples;
IndexInfo *indexInfo; IndexInfo *indexInfo;
...@@ -1670,7 +1672,7 @@ _bt_parallel_scan_and_sort(BTSpool *btspool, BTSpool *btspool2, ...@@ -1670,7 +1672,7 @@ _bt_parallel_scan_and_sort(BTSpool *btspool, BTSpool *btspool2,
/* Join parallel scan */ /* Join parallel scan */
indexInfo = BuildIndexInfo(btspool->index); indexInfo = BuildIndexInfo(btspool->index);
indexInfo->ii_Concurrent = btshared->isconcurrent; indexInfo->ii_Concurrent = btshared->isconcurrent;
scan = heap_beginscan_parallel(btspool->heap, &btshared->heapdesc); scan = table_beginscan_parallel(btspool->heap, &btshared->heapdesc);
reltuples = IndexBuildHeapScan(btspool->heap, btspool->index, indexInfo, reltuples = IndexBuildHeapScan(btspool->heap, btspool->index, indexInfo,
true, _bt_build_callback, true, _bt_build_callback,
(void *) &buildstate, scan); (void *) &buildstate, scan);
......
...@@ -927,7 +927,7 @@ spggettuple(IndexScanDesc scan, ScanDirection dir) ...@@ -927,7 +927,7 @@ spggettuple(IndexScanDesc scan, ScanDirection dir)
if (so->iPtr < so->nPtrs) if (so->iPtr < so->nPtrs)
{ {
/* continuing to return reported tuples */ /* continuing to return reported tuples */
scan->xs_ctup.t_self = so->heapPtrs[so->iPtr]; scan->xs_heaptid = so->heapPtrs[so->iPtr];
scan->xs_recheck = so->recheck[so->iPtr]; scan->xs_recheck = so->recheck[so->iPtr];
scan->xs_hitup = so->reconTups[so->iPtr]; scan->xs_hitup = so->reconTups[so->iPtr];
......
...@@ -6,13 +6,304 @@ ...@@ -6,13 +6,304 @@
* Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* src/backend/access/table/tableam.c *
* IDENTIFICATION
* src/backend/access/table/tableam.c
*
* NOTES
* Note that most function in here are documented in tableam.h, rather than
* here. That's because there's a lot of inline functions in tableam.h and
* it'd be harder to understand if one constantly had to switch between files.
*
*---------------------------------------------------------------------- *----------------------------------------------------------------------
*/ */
#include "postgres.h" #include "postgres.h"
#include "access/heapam.h" /* for ss_* */
#include "access/tableam.h" #include "access/tableam.h"
#include "access/xact.h"
#include "storage/bufmgr.h"
#include "storage/shmem.h"
/* GUC variables */ /* GUC variables */
char *default_table_access_method = DEFAULT_TABLE_ACCESS_METHOD; char *default_table_access_method = DEFAULT_TABLE_ACCESS_METHOD;
bool synchronize_seqscans = true;
/* ----------------------------------------------------------------------------
* Slot functions.
* ----------------------------------------------------------------------------
*/
const TupleTableSlotOps *
table_slot_callbacks(Relation relation)
{
const TupleTableSlotOps *tts_cb;
if (relation->rd_tableam)
tts_cb = relation->rd_tableam->slot_callbacks(relation);
else if (relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
{
/*
* Historically FDWs expect to store heap tuples in slots. Continue
* handing them one, to make it less painful to adapt FDWs to new
* versions. The cost of a heap slot over a virtual slot is pretty
* small.
*/
tts_cb = &TTSOpsHeapTuple;
}
else
{
/*
* These need to be supported, as some parts of the code (like COPY)
* need to create slots for such relations too. It seems better to
* centralize the knowledge that a heap slot is the right thing in
* that case here.
*/
Assert(relation->rd_rel->relkind == RELKIND_VIEW ||
relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
tts_cb = &TTSOpsVirtual;
}
return tts_cb;
}
TupleTableSlot *
table_slot_create(Relation relation, List **reglist)
{
const TupleTableSlotOps *tts_cb;
TupleTableSlot *slot;
tts_cb = table_slot_callbacks(relation);
slot = MakeSingleTupleTableSlot(RelationGetDescr(relation), tts_cb);
if (reglist)
*reglist = lappend(*reglist, slot);
return slot;
}
/* ----------------------------------------------------------------------------
* Table scan functions.
* ----------------------------------------------------------------------------
*/
TableScanDesc
table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
{
Oid relid = RelationGetRelid(relation);
Snapshot snapshot = RegisterSnapshot(GetCatalogSnapshot(relid));
return relation->rd_tableam->scan_begin(relation, snapshot, nkeys, key, NULL,
true, true, true, false, false, true);
}
void
table_scan_update_snapshot(TableScanDesc scan, Snapshot snapshot)
{
Assert(IsMVCCSnapshot(snapshot));
RegisterSnapshot(snapshot);
scan->rs_snapshot = snapshot;
scan->rs_temp_snap = true;
}
/* ----------------------------------------------------------------------------
* Parallel table scan related functions.
* ----------------------------------------------------------------------------
*/
Size
table_parallelscan_estimate(Relation rel, Snapshot snapshot)
{
Size sz = 0;
if (IsMVCCSnapshot(snapshot))
sz = add_size(sz, EstimateSnapshotSpace(snapshot));
else
Assert(snapshot == SnapshotAny);
sz = add_size(sz, rel->rd_tableam->parallelscan_estimate(rel));
return sz;
}
void
table_parallelscan_initialize(Relation rel, ParallelTableScanDesc pscan,
Snapshot snapshot)
{
Size snapshot_off = rel->rd_tableam->parallelscan_initialize(rel, pscan);
pscan->phs_snapshot_off = snapshot_off;
if (IsMVCCSnapshot(snapshot))
{
SerializeSnapshot(snapshot, (char *) pscan + pscan->phs_snapshot_off);
pscan->phs_snapshot_any = false;
}
else
{
Assert(snapshot == SnapshotAny);
pscan->phs_snapshot_any = true;
}
}
TableScanDesc
table_beginscan_parallel(Relation relation, ParallelTableScanDesc parallel_scan)
{
Snapshot snapshot;
Assert(RelationGetRelid(relation) == parallel_scan->phs_relid);
if (!parallel_scan->phs_snapshot_any)
{
/* Snapshot was serialized -- restore it */
snapshot = RestoreSnapshot((char *) parallel_scan +
parallel_scan->phs_snapshot_off);
RegisterSnapshot(snapshot);
}
else
{
/* SnapshotAny passed by caller (not serialized) */
snapshot = SnapshotAny;
}
return relation->rd_tableam->scan_begin(relation, snapshot, 0, NULL, parallel_scan,
true, true, true, false, false, !parallel_scan->phs_snapshot_any);
}
/* ----------------------------------------------------------------------------
* Helper functions to implement parallel scans for block oriented AMs.
* ----------------------------------------------------------------------------
*/
Size
table_block_parallelscan_estimate(Relation rel)
{
return sizeof(ParallelBlockTableScanDescData);
}
Size
table_block_parallelscan_initialize(Relation rel, ParallelTableScanDesc pscan)
{
ParallelBlockTableScanDesc bpscan = (ParallelBlockTableScanDesc) pscan;
bpscan->base.phs_relid = RelationGetRelid(rel);
bpscan->phs_nblocks = RelationGetNumberOfBlocks(rel);
/* compare phs_syncscan initialization to similar logic in initscan */
bpscan->base.phs_syncscan = synchronize_seqscans &&
!RelationUsesLocalBuffers(rel) &&
bpscan->phs_nblocks > NBuffers / 4;
SpinLockInit(&bpscan->phs_mutex);
bpscan->phs_startblock = InvalidBlockNumber;
pg_atomic_init_u64(&bpscan->phs_nallocated, 0);
return sizeof(ParallelBlockTableScanDescData);
}
void
table_block_parallelscan_reinitialize(Relation rel, ParallelTableScanDesc pscan)
{
ParallelBlockTableScanDesc bpscan = (ParallelBlockTableScanDesc) pscan;
pg_atomic_write_u64(&bpscan->phs_nallocated, 0);
}
/*
* find and set the scan's startblock
*
* Determine where the parallel seq scan should start. This function may be
* called many times, once by each parallel worker. We must be careful only
* to set the startblock once.
*/
void
table_block_parallelscan_startblock_init(Relation rel, ParallelBlockTableScanDesc pbscan)
{
BlockNumber sync_startpage = InvalidBlockNumber;
retry:
/* Grab the spinlock. */
SpinLockAcquire(&pbscan->phs_mutex);
/*
* If the scan's startblock has not yet been initialized, we must do so
* now. If this is not a synchronized scan, we just start at block 0, but
* if it is a synchronized scan, we must get the starting position from
* the synchronized scan machinery. We can't hold the spinlock while
* doing that, though, so release the spinlock, get the information we
* need, and retry. If nobody else has initialized the scan in the
* meantime, we'll fill in the value we fetched on the second time
* through.
*/
if (pbscan->phs_startblock == InvalidBlockNumber)
{
if (!pbscan->base.phs_syncscan)
pbscan->phs_startblock = 0;
else if (sync_startpage != InvalidBlockNumber)
pbscan->phs_startblock = sync_startpage;
else
{
SpinLockRelease(&pbscan->phs_mutex);
sync_startpage = ss_get_location(rel, pbscan->phs_nblocks);
goto retry;
}
}
SpinLockRelease(&pbscan->phs_mutex);
}
/*
* get the next page to scan
*
* Get the next page to scan. Even if there are no pages left to scan,
* another backend could have grabbed a page to scan and not yet finished
* looking at it, so it doesn't follow that the scan is done when the first
* backend gets an InvalidBlockNumber return.
*/
BlockNumber
table_block_parallelscan_nextpage(Relation rel, ParallelBlockTableScanDesc pbscan)
{
BlockNumber page;
uint64 nallocated;
/*
* phs_nallocated tracks how many pages have been allocated to workers
* already. When phs_nallocated >= rs_nblocks, all blocks have been
* allocated.
*
* Because we use an atomic fetch-and-add to fetch the current value, the
* phs_nallocated counter will exceed rs_nblocks, because workers will
* still increment the value, when they try to allocate the next block but
* all blocks have been allocated already. The counter must be 64 bits
* wide because of that, to avoid wrapping around when rs_nblocks is close
* to 2^32.
*
* The actual page to return is calculated by adding the counter to the
* starting block number, modulo nblocks.
*/
nallocated = pg_atomic_fetch_add_u64(&pbscan->phs_nallocated, 1);
if (nallocated >= pbscan->phs_nblocks)
page = InvalidBlockNumber; /* all blocks have been allocated */
else
page = (nallocated + pbscan->phs_startblock) % pbscan->phs_nblocks;
/*
* Report scan location. Normally, we report the current page number.
* When we reach the end of the scan, though, we report the starting page,
* not the ending page, just so the starting positions for later scans
* doesn't slew backwards. We only report the position at the end of the
* scan once, though: subsequent callers will report nothing.
*/
if (pbscan->base.phs_syncscan)
{
if (page != InvalidBlockNumber)
ss_report_location(rel, page);
else if (nallocated == pbscan->phs_nblocks)
ss_report_location(rel, pbscan->phs_startblock);
}
return page;
}
...@@ -44,6 +44,26 @@ GetTableAmRoutine(Oid amhandler) ...@@ -44,6 +44,26 @@ GetTableAmRoutine(Oid amhandler)
elog(ERROR, "Table access method handler %u did not return a TableAmRoutine struct", elog(ERROR, "Table access method handler %u did not return a TableAmRoutine struct",
amhandler); amhandler);
/*
* Assert that all required callbacks are present. That makes it a bit
* easier to keep AMs up to date, e.g. when forward porting them to a new
* major version.
*/
Assert(routine->scan_begin != NULL);
Assert(routine->scan_end != NULL);
Assert(routine->scan_rescan != NULL);
Assert(routine->parallelscan_estimate != NULL);
Assert(routine->parallelscan_initialize != NULL);
Assert(routine->parallelscan_reinitialize != NULL);
Assert(routine->index_fetch_begin != NULL);
Assert(routine->index_fetch_reset != NULL);
Assert(routine->index_fetch_end != NULL);
Assert(routine->index_fetch_tuple != NULL);
Assert(routine->tuple_satisfies_snapshot != NULL);
return routine; return routine;
} }
...@@ -98,7 +118,7 @@ get_table_am_oid(const char *tableamname, bool missing_ok) ...@@ -98,7 +118,7 @@ get_table_am_oid(const char *tableamname, bool missing_ok)
{ {
Oid result; Oid result;
Relation rel; Relation rel;
HeapScanDesc scandesc; TableScanDesc scandesc;
HeapTuple tuple; HeapTuple tuple;
ScanKeyData entry[1]; ScanKeyData entry[1];
...@@ -113,7 +133,7 @@ get_table_am_oid(const char *tableamname, bool missing_ok) ...@@ -113,7 +133,7 @@ get_table_am_oid(const char *tableamname, bool missing_ok)
Anum_pg_am_amname, Anum_pg_am_amname,
BTEqualStrategyNumber, F_NAMEEQ, BTEqualStrategyNumber, F_NAMEEQ,
CStringGetDatum(tableamname)); CStringGetDatum(tableamname));
scandesc = heap_beginscan_catalog(rel, 1, entry); scandesc = table_beginscan_catalog(rel, 1, entry);
tuple = heap_getnext(scandesc, ForwardScanDirection); tuple = heap_getnext(scandesc, ForwardScanDirection);
/* We assume that there can be at most one matching tuple */ /* We assume that there can be at most one matching tuple */
...@@ -123,7 +143,7 @@ get_table_am_oid(const char *tableamname, bool missing_ok) ...@@ -123,7 +143,7 @@ get_table_am_oid(const char *tableamname, bool missing_ok)
else else
result = InvalidOid; result = InvalidOid;
heap_endscan(scandesc); table_endscan(scandesc);
heap_close(rel, AccessShareLock); heap_close(rel, AccessShareLock);
if (!OidIsValid(result) && !missing_ok) if (!OidIsValid(result) && !missing_ok)
......
...@@ -180,7 +180,8 @@ static BlockNumber ...@@ -180,7 +180,8 @@ static BlockNumber
system_nextsampleblock(SampleScanState *node) system_nextsampleblock(SampleScanState *node)
{ {
SystemSamplerData *sampler = (SystemSamplerData *) node->tsm_state; SystemSamplerData *sampler = (SystemSamplerData *) node->tsm_state;
HeapScanDesc scan = node->ss.ss_currentScanDesc; TableScanDesc scan = node->ss.ss_currentScanDesc;
HeapScanDesc hscan = (HeapScanDesc) scan;
BlockNumber nextblock = sampler->nextblock; BlockNumber nextblock = sampler->nextblock;
uint32 hashinput[2]; uint32 hashinput[2];
...@@ -199,7 +200,7 @@ system_nextsampleblock(SampleScanState *node) ...@@ -199,7 +200,7 @@ system_nextsampleblock(SampleScanState *node)
* Loop over block numbers until finding suitable block or reaching end of * Loop over block numbers until finding suitable block or reaching end of
* relation. * relation.
*/ */
for (; nextblock < scan->rs_nblocks; nextblock++) for (; nextblock < hscan->rs_nblocks; nextblock++)
{ {
uint32 hash; uint32 hash;
...@@ -211,7 +212,7 @@ system_nextsampleblock(SampleScanState *node) ...@@ -211,7 +212,7 @@ system_nextsampleblock(SampleScanState *node)
break; break;
} }
if (nextblock < scan->rs_nblocks) if (nextblock < hscan->rs_nblocks)
{ {
/* Found a suitable block; remember where we should start next time */ /* Found a suitable block; remember where we should start next time */
sampler->nextblock = nextblock + 1; sampler->nextblock = nextblock + 1;
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "access/genam.h" #include "access/genam.h"
#include "access/heapam.h" #include "access/heapam.h"
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/tableam.h"
#include "access/xact.h" #include "access/xact.h"
#include "access/xlog_internal.h" #include "access/xlog_internal.h"
#include "bootstrap/bootstrap.h" #include "bootstrap/bootstrap.h"
...@@ -594,7 +595,7 @@ boot_openrel(char *relname) ...@@ -594,7 +595,7 @@ boot_openrel(char *relname)
int i; int i;
struct typmap **app; struct typmap **app;
Relation rel; Relation rel;
HeapScanDesc scan; TableScanDesc scan;
HeapTuple tup; HeapTuple tup;
if (strlen(relname) >= NAMEDATALEN) if (strlen(relname) >= NAMEDATALEN)
...@@ -604,16 +605,16 @@ boot_openrel(char *relname) ...@@ -604,16 +605,16 @@ boot_openrel(char *relname)
{ {
/* We can now load the pg_type data */ /* We can now load the pg_type data */
rel = table_open(TypeRelationId, NoLock); rel = table_open(TypeRelationId, NoLock);
scan = heap_beginscan_catalog(rel, 0, NULL); scan = table_beginscan_catalog(rel, 0, NULL);
i = 0; i = 0;
while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
++i; ++i;
heap_endscan(scan); table_endscan(scan);
app = Typ = ALLOC(struct typmap *, i + 1); app = Typ = ALLOC(struct typmap *, i + 1);
while (i-- > 0) while (i-- > 0)
*app++ = ALLOC(struct typmap, 1); *app++ = ALLOC(struct typmap, 1);
*app = NULL; *app = NULL;
scan = heap_beginscan_catalog(rel, 0, NULL); scan = table_beginscan_catalog(rel, 0, NULL);
app = Typ; app = Typ;
while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
{ {
...@@ -623,7 +624,7 @@ boot_openrel(char *relname) ...@@ -623,7 +624,7 @@ boot_openrel(char *relname)
sizeof((*app)->am_typ)); sizeof((*app)->am_typ));
app++; app++;
} }
heap_endscan(scan); table_endscan(scan);
table_close(rel, NoLock); table_close(rel, NoLock);
} }
...@@ -915,7 +916,7 @@ gettype(char *type) ...@@ -915,7 +916,7 @@ gettype(char *type)
{ {
int i; int i;
Relation rel; Relation rel;
HeapScanDesc scan; TableScanDesc scan;
HeapTuple tup; HeapTuple tup;
struct typmap **app; struct typmap **app;
...@@ -939,16 +940,16 @@ gettype(char *type) ...@@ -939,16 +940,16 @@ gettype(char *type)
} }
elog(DEBUG4, "external type: %s", type); elog(DEBUG4, "external type: %s", type);
rel = table_open(TypeRelationId, NoLock); rel = table_open(TypeRelationId, NoLock);
scan = heap_beginscan_catalog(rel, 0, NULL); scan = table_beginscan_catalog(rel, 0, NULL);
i = 0; i = 0;
while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
++i; ++i;
heap_endscan(scan); table_endscan(scan);
app = Typ = ALLOC(struct typmap *, i + 1); app = Typ = ALLOC(struct typmap *, i + 1);
while (i-- > 0) while (i-- > 0)
*app++ = ALLOC(struct typmap, 1); *app++ = ALLOC(struct typmap, 1);
*app = NULL; *app = NULL;
scan = heap_beginscan_catalog(rel, 0, NULL); scan = table_beginscan_catalog(rel, 0, NULL);
app = Typ; app = Typ;
while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
{ {
...@@ -957,7 +958,7 @@ gettype(char *type) ...@@ -957,7 +958,7 @@ gettype(char *type)
(char *) GETSTRUCT(tup), (char *) GETSTRUCT(tup),
sizeof((*app)->am_typ)); sizeof((*app)->am_typ));
} }
heap_endscan(scan); table_endscan(scan);
table_close(rel, NoLock); table_close(rel, NoLock);
return gettype(type); return gettype(type);
} }
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "access/heapam.h" #include "access/heapam.h"
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/sysattr.h" #include "access/sysattr.h"
#include "access/tableam.h"
#include "access/xact.h" #include "access/xact.h"
#include "catalog/binary_upgrade.h" #include "catalog/binary_upgrade.h"
#include "catalog/catalog.h" #include "catalog/catalog.h"
...@@ -821,7 +822,7 @@ objectsInSchemaToOids(ObjectType objtype, List *nspnames) ...@@ -821,7 +822,7 @@ objectsInSchemaToOids(ObjectType objtype, List *nspnames)
ScanKeyData key[2]; ScanKeyData key[2];
int keycount; int keycount;
Relation rel; Relation rel;
HeapScanDesc scan; TableScanDesc scan;
HeapTuple tuple; HeapTuple tuple;
keycount = 0; keycount = 0;
...@@ -843,7 +844,7 @@ objectsInSchemaToOids(ObjectType objtype, List *nspnames) ...@@ -843,7 +844,7 @@ objectsInSchemaToOids(ObjectType objtype, List *nspnames)
CharGetDatum(PROKIND_PROCEDURE)); CharGetDatum(PROKIND_PROCEDURE));
rel = table_open(ProcedureRelationId, AccessShareLock); rel = table_open(ProcedureRelationId, AccessShareLock);
scan = heap_beginscan_catalog(rel, keycount, key); scan = table_beginscan_catalog(rel, keycount, key);
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
{ {
...@@ -852,7 +853,7 @@ objectsInSchemaToOids(ObjectType objtype, List *nspnames) ...@@ -852,7 +853,7 @@ objectsInSchemaToOids(ObjectType objtype, List *nspnames)
objects = lappend_oid(objects, oid); objects = lappend_oid(objects, oid);
} }
heap_endscan(scan); table_endscan(scan);
table_close(rel, AccessShareLock); table_close(rel, AccessShareLock);
} }
break; break;
...@@ -877,7 +878,7 @@ getRelationsInNamespace(Oid namespaceId, char relkind) ...@@ -877,7 +878,7 @@ getRelationsInNamespace(Oid namespaceId, char relkind)
List *relations = NIL; List *relations = NIL;
ScanKeyData key[2]; ScanKeyData key[2];
Relation rel; Relation rel;
HeapScanDesc scan; TableScanDesc scan;
HeapTuple tuple; HeapTuple tuple;
ScanKeyInit(&key[0], ScanKeyInit(&key[0],
...@@ -890,7 +891,7 @@ getRelationsInNamespace(Oid namespaceId, char relkind) ...@@ -890,7 +891,7 @@ getRelationsInNamespace(Oid namespaceId, char relkind)
CharGetDatum(relkind)); CharGetDatum(relkind));
rel = table_open(RelationRelationId, AccessShareLock); rel = table_open(RelationRelationId, AccessShareLock);
scan = heap_beginscan_catalog(rel, 2, key); scan = table_beginscan_catalog(rel, 2, key);
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
{ {
...@@ -899,7 +900,7 @@ getRelationsInNamespace(Oid namespaceId, char relkind) ...@@ -899,7 +900,7 @@ getRelationsInNamespace(Oid namespaceId, char relkind)
relations = lappend_oid(relations, oid); relations = lappend_oid(relations, oid);
} }
heap_endscan(scan); table_endscan(scan);
table_close(rel, AccessShareLock); table_close(rel, AccessShareLock);
return relations; return relations;
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include "access/multixact.h" #include "access/multixact.h"
#include "access/relscan.h" #include "access/relscan.h"
#include "access/sysattr.h" #include "access/sysattr.h"
#include "access/tableam.h"
#include "access/transam.h" #include "access/transam.h"
#include "access/visibilitymap.h" #include "access/visibilitymap.h"
#include "access/xact.h" #include "access/xact.h"
...@@ -2138,7 +2139,7 @@ index_update_stats(Relation rel, ...@@ -2138,7 +2139,7 @@ index_update_stats(Relation rel,
ReindexIsProcessingHeap(RelationRelationId)) ReindexIsProcessingHeap(RelationRelationId))
{ {
/* don't assume syscache will work */ /* don't assume syscache will work */
HeapScanDesc pg_class_scan; TableScanDesc pg_class_scan;
ScanKeyData key[1]; ScanKeyData key[1];
ScanKeyInit(&key[0], ScanKeyInit(&key[0],
...@@ -2146,10 +2147,10 @@ index_update_stats(Relation rel, ...@@ -2146,10 +2147,10 @@ index_update_stats(Relation rel,
BTEqualStrategyNumber, F_OIDEQ, BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(relid)); ObjectIdGetDatum(relid));
pg_class_scan = heap_beginscan_catalog(pg_class, 1, key); pg_class_scan = table_beginscan_catalog(pg_class, 1, key);
tuple = heap_getnext(pg_class_scan, ForwardScanDirection); tuple = heap_getnext(pg_class_scan, ForwardScanDirection);
tuple = heap_copytuple(tuple); tuple = heap_copytuple(tuple);
heap_endscan(pg_class_scan); table_endscan(pg_class_scan);
} }
else else
{ {
...@@ -2431,7 +2432,7 @@ IndexBuildHeapScan(Relation heapRelation, ...@@ -2431,7 +2432,7 @@ IndexBuildHeapScan(Relation heapRelation,
bool allow_sync, bool allow_sync,
IndexBuildCallback callback, IndexBuildCallback callback,
void *callback_state, void *callback_state,
HeapScanDesc scan) TableScanDesc scan)
{ {
return IndexBuildHeapRangeScan(heapRelation, indexRelation, return IndexBuildHeapRangeScan(heapRelation, indexRelation,
indexInfo, allow_sync, indexInfo, allow_sync,
...@@ -2460,8 +2461,9 @@ IndexBuildHeapRangeScan(Relation heapRelation, ...@@ -2460,8 +2461,9 @@ IndexBuildHeapRangeScan(Relation heapRelation,
BlockNumber numblocks, BlockNumber numblocks,
IndexBuildCallback callback, IndexBuildCallback callback,
void *callback_state, void *callback_state,
HeapScanDesc scan) TableScanDesc scan)
{ {
HeapScanDesc hscan;
bool is_system_catalog; bool is_system_catalog;
bool checking_uniqueness; bool checking_uniqueness;
HeapTuple heapTuple; HeapTuple heapTuple;
...@@ -2502,8 +2504,7 @@ IndexBuildHeapRangeScan(Relation heapRelation, ...@@ -2502,8 +2504,7 @@ IndexBuildHeapRangeScan(Relation heapRelation,
*/ */
estate = CreateExecutorState(); estate = CreateExecutorState();
econtext = GetPerTupleExprContext(estate); econtext = GetPerTupleExprContext(estate);
slot = MakeSingleTupleTableSlot(RelationGetDescr(heapRelation), slot = table_slot_create(heapRelation, NULL);
&TTSOpsHeapTuple);
/* Arrange for econtext's scan tuple to be the tuple under test */ /* Arrange for econtext's scan tuple to be the tuple under test */
econtext->ecxt_scantuple = slot; econtext->ecxt_scantuple = slot;
...@@ -2540,12 +2541,12 @@ IndexBuildHeapRangeScan(Relation heapRelation, ...@@ -2540,12 +2541,12 @@ IndexBuildHeapRangeScan(Relation heapRelation,
else else
snapshot = SnapshotAny; snapshot = SnapshotAny;
scan = heap_beginscan_strat(heapRelation, /* relation */ scan = table_beginscan_strat(heapRelation, /* relation */
snapshot, /* snapshot */ snapshot, /* snapshot */
0, /* number of keys */ 0, /* number of keys */
NULL, /* scan key */ NULL, /* scan key */
true, /* buffer access strategy OK */ true, /* buffer access strategy OK */
allow_sync); /* syncscan OK? */ allow_sync); /* syncscan OK? */
} }
else else
{ {
...@@ -2561,6 +2562,8 @@ IndexBuildHeapRangeScan(Relation heapRelation, ...@@ -2561,6 +2562,8 @@ IndexBuildHeapRangeScan(Relation heapRelation,
snapshot = scan->rs_snapshot; snapshot = scan->rs_snapshot;
} }
hscan = (HeapScanDesc) scan;
/* /*
* Must call GetOldestXmin() with SnapshotAny. Should never call * Must call GetOldestXmin() with SnapshotAny. Should never call
* GetOldestXmin() with MVCC snapshot. (It's especially worth checking * GetOldestXmin() with MVCC snapshot. (It's especially worth checking
...@@ -2618,15 +2621,15 @@ IndexBuildHeapRangeScan(Relation heapRelation, ...@@ -2618,15 +2621,15 @@ IndexBuildHeapRangeScan(Relation heapRelation,
* tuple per HOT-chain --- else we could create more than one index * tuple per HOT-chain --- else we could create more than one index
* entry pointing to the same root tuple. * entry pointing to the same root tuple.
*/ */
if (scan->rs_cblock != root_blkno) if (hscan->rs_cblock != root_blkno)
{ {
Page page = BufferGetPage(scan->rs_cbuf); Page page = BufferGetPage(hscan->rs_cbuf);
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE); LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
heap_get_root_tuples(page, root_offsets); heap_get_root_tuples(page, root_offsets);
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK); LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
root_blkno = scan->rs_cblock; root_blkno = hscan->rs_cblock;
} }
if (snapshot == SnapshotAny) if (snapshot == SnapshotAny)
...@@ -2643,7 +2646,7 @@ IndexBuildHeapRangeScan(Relation heapRelation, ...@@ -2643,7 +2646,7 @@ IndexBuildHeapRangeScan(Relation heapRelation,
* be conservative about it. (This remark is still correct even * be conservative about it. (This remark is still correct even
* with HOT-pruning: our pin on the buffer prevents pruning.) * with HOT-pruning: our pin on the buffer prevents pruning.)
*/ */
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE); LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
/* /*
* The criteria for counting a tuple as live in this block need to * The criteria for counting a tuple as live in this block need to
...@@ -2652,7 +2655,7 @@ IndexBuildHeapRangeScan(Relation heapRelation, ...@@ -2652,7 +2655,7 @@ IndexBuildHeapRangeScan(Relation heapRelation,
* values, e.g. when there are many recently-dead tuples. * values, e.g. when there are many recently-dead tuples.
*/ */
switch (HeapTupleSatisfiesVacuum(heapTuple, OldestXmin, switch (HeapTupleSatisfiesVacuum(heapTuple, OldestXmin,
scan->rs_cbuf)) hscan->rs_cbuf))
{ {
case HEAPTUPLE_DEAD: case HEAPTUPLE_DEAD:
/* Definitely dead, we can ignore it */ /* Definitely dead, we can ignore it */
...@@ -2733,7 +2736,7 @@ IndexBuildHeapRangeScan(Relation heapRelation, ...@@ -2733,7 +2736,7 @@ IndexBuildHeapRangeScan(Relation heapRelation,
/* /*
* Must drop the lock on the buffer before we wait * Must drop the lock on the buffer before we wait
*/ */
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK); LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
XactLockTableWait(xwait, heapRelation, XactLockTableWait(xwait, heapRelation,
&heapTuple->t_self, &heapTuple->t_self,
XLTW_InsertIndexUnique); XLTW_InsertIndexUnique);
...@@ -2800,7 +2803,7 @@ IndexBuildHeapRangeScan(Relation heapRelation, ...@@ -2800,7 +2803,7 @@ IndexBuildHeapRangeScan(Relation heapRelation,
/* /*
* Must drop the lock on the buffer before we wait * Must drop the lock on the buffer before we wait
*/ */
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK); LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
XactLockTableWait(xwait, heapRelation, XactLockTableWait(xwait, heapRelation,
&heapTuple->t_self, &heapTuple->t_self,
XLTW_InsertIndexUnique); XLTW_InsertIndexUnique);
...@@ -2852,7 +2855,7 @@ IndexBuildHeapRangeScan(Relation heapRelation, ...@@ -2852,7 +2855,7 @@ IndexBuildHeapRangeScan(Relation heapRelation,
break; break;
} }
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK); LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
if (!indexIt) if (!indexIt)
continue; continue;
...@@ -2867,7 +2870,7 @@ IndexBuildHeapRangeScan(Relation heapRelation, ...@@ -2867,7 +2870,7 @@ IndexBuildHeapRangeScan(Relation heapRelation,
MemoryContextReset(econtext->ecxt_per_tuple_memory); MemoryContextReset(econtext->ecxt_per_tuple_memory);
/* Set up for predicate or expression evaluation */ /* Set up for predicate or expression evaluation */
ExecStoreHeapTuple(heapTuple, slot, false); ExecStoreBufferHeapTuple(heapTuple, slot, hscan->rs_cbuf);
/* /*
* In a partial index, discard tuples that don't satisfy the * In a partial index, discard tuples that don't satisfy the
...@@ -2931,7 +2934,7 @@ IndexBuildHeapRangeScan(Relation heapRelation, ...@@ -2931,7 +2934,7 @@ IndexBuildHeapRangeScan(Relation heapRelation,
} }
} }
heap_endscan(scan); table_endscan(scan);
/* we can now forget our snapshot, if set and registered by us */ /* we can now forget our snapshot, if set and registered by us */
if (need_unregister_snapshot) if (need_unregister_snapshot)
...@@ -2966,8 +2969,7 @@ IndexCheckExclusion(Relation heapRelation, ...@@ -2966,8 +2969,7 @@ IndexCheckExclusion(Relation heapRelation,
Relation indexRelation, Relation indexRelation,
IndexInfo *indexInfo) IndexInfo *indexInfo)
{ {
HeapScanDesc scan; TableScanDesc scan;
HeapTuple heapTuple;
Datum values[INDEX_MAX_KEYS]; Datum values[INDEX_MAX_KEYS];
bool isnull[INDEX_MAX_KEYS]; bool isnull[INDEX_MAX_KEYS];
ExprState *predicate; ExprState *predicate;
...@@ -2990,8 +2992,7 @@ IndexCheckExclusion(Relation heapRelation, ...@@ -2990,8 +2992,7 @@ IndexCheckExclusion(Relation heapRelation,
*/ */
estate = CreateExecutorState(); estate = CreateExecutorState();
econtext = GetPerTupleExprContext(estate); econtext = GetPerTupleExprContext(estate);
slot = MakeSingleTupleTableSlot(RelationGetDescr(heapRelation), slot = table_slot_create(heapRelation, NULL);
&TTSOpsHeapTuple);
/* Arrange for econtext's scan tuple to be the tuple under test */ /* Arrange for econtext's scan tuple to be the tuple under test */
econtext->ecxt_scantuple = slot; econtext->ecxt_scantuple = slot;
...@@ -3003,22 +3004,17 @@ IndexCheckExclusion(Relation heapRelation, ...@@ -3003,22 +3004,17 @@ IndexCheckExclusion(Relation heapRelation,
* Scan all live tuples in the base relation. * Scan all live tuples in the base relation.
*/ */
snapshot = RegisterSnapshot(GetLatestSnapshot()); snapshot = RegisterSnapshot(GetLatestSnapshot());
scan = heap_beginscan_strat(heapRelation, /* relation */ scan = table_beginscan_strat(heapRelation, /* relation */
snapshot, /* snapshot */ snapshot, /* snapshot */
0, /* number of keys */ 0, /* number of keys */
NULL, /* scan key */ NULL, /* scan key */
true, /* buffer access strategy OK */ true, /* buffer access strategy OK */
true); /* syncscan OK */ true); /* syncscan OK */
while ((heapTuple = heap_getnext(scan, ForwardScanDirection)) != NULL) while (table_scan_getnextslot(scan, ForwardScanDirection, slot))
{ {
CHECK_FOR_INTERRUPTS(); CHECK_FOR_INTERRUPTS();
MemoryContextReset(econtext->ecxt_per_tuple_memory);
/* Set up for predicate or expression evaluation */
ExecStoreHeapTuple(heapTuple, slot, false);
/* /*
* In a partial index, ignore tuples that don't satisfy the predicate. * In a partial index, ignore tuples that don't satisfy the predicate.
*/ */
...@@ -3042,11 +3038,13 @@ IndexCheckExclusion(Relation heapRelation, ...@@ -3042,11 +3038,13 @@ IndexCheckExclusion(Relation heapRelation,
*/ */
check_exclusion_constraint(heapRelation, check_exclusion_constraint(heapRelation,
indexRelation, indexInfo, indexRelation, indexInfo,
&(heapTuple->t_self), values, isnull, &(slot->tts_tid), values, isnull,
estate, true); estate, true);
MemoryContextReset(econtext->ecxt_per_tuple_memory);
} }
heap_endscan(scan); table_endscan(scan);
UnregisterSnapshot(snapshot); UnregisterSnapshot(snapshot);
ExecDropSingleTupleTableSlot(slot); ExecDropSingleTupleTableSlot(slot);
...@@ -3281,7 +3279,8 @@ validate_index_heapscan(Relation heapRelation, ...@@ -3281,7 +3279,8 @@ validate_index_heapscan(Relation heapRelation,
Snapshot snapshot, Snapshot snapshot,
v_i_state *state) v_i_state *state)
{ {
HeapScanDesc scan; TableScanDesc scan;
HeapScanDesc hscan;
HeapTuple heapTuple; HeapTuple heapTuple;
Datum values[INDEX_MAX_KEYS]; Datum values[INDEX_MAX_KEYS];
bool isnull[INDEX_MAX_KEYS]; bool isnull[INDEX_MAX_KEYS];
...@@ -3324,12 +3323,13 @@ validate_index_heapscan(Relation heapRelation, ...@@ -3324,12 +3323,13 @@ validate_index_heapscan(Relation heapRelation,
* here, because it's critical that we read from block zero forward to * here, because it's critical that we read from block zero forward to
* match the sorted TIDs. * match the sorted TIDs.
*/ */
scan = heap_beginscan_strat(heapRelation, /* relation */ scan = table_beginscan_strat(heapRelation, /* relation */
snapshot, /* snapshot */ snapshot, /* snapshot */
0, /* number of keys */ 0, /* number of keys */
NULL, /* scan key */ NULL, /* scan key */
true, /* buffer access strategy OK */ true, /* buffer access strategy OK */
false); /* syncscan not OK */ false); /* syncscan not OK */
hscan = (HeapScanDesc) scan;
/* /*
* Scan all tuples matching the snapshot. * Scan all tuples matching the snapshot.
...@@ -3358,17 +3358,17 @@ validate_index_heapscan(Relation heapRelation, ...@@ -3358,17 +3358,17 @@ validate_index_heapscan(Relation heapRelation,
* already-passed-over tuplesort output TIDs of the current page. We * already-passed-over tuplesort output TIDs of the current page. We
* clear that array here, when advancing onto a new heap page. * clear that array here, when advancing onto a new heap page.
*/ */
if (scan->rs_cblock != root_blkno) if (hscan->rs_cblock != root_blkno)
{ {
Page page = BufferGetPage(scan->rs_cbuf); Page page = BufferGetPage(hscan->rs_cbuf);
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE); LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
heap_get_root_tuples(page, root_offsets); heap_get_root_tuples(page, root_offsets);
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK); LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
memset(in_index, 0, sizeof(in_index)); memset(in_index, 0, sizeof(in_index));
root_blkno = scan->rs_cblock; root_blkno = hscan->rs_cblock;
} }
/* Convert actual tuple TID to root TID */ /* Convert actual tuple TID to root TID */
...@@ -3493,7 +3493,7 @@ validate_index_heapscan(Relation heapRelation, ...@@ -3493,7 +3493,7 @@ validate_index_heapscan(Relation heapRelation,
} }
} }
heap_endscan(scan); table_endscan(scan);
ExecDropSingleTupleTableSlot(slot); ExecDropSingleTupleTableSlot(slot);
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "access/heapam.h" #include "access/heapam.h"
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/sysattr.h" #include "access/sysattr.h"
#include "access/tableam.h"
#include "catalog/catalog.h" #include "catalog/catalog.h"
#include "catalog/dependency.h" #include "catalog/dependency.h"
#include "catalog/indexing.h" #include "catalog/indexing.h"
...@@ -152,7 +153,7 @@ RemoveConversionById(Oid conversionOid) ...@@ -152,7 +153,7 @@ RemoveConversionById(Oid conversionOid)
{ {
Relation rel; Relation rel;
HeapTuple tuple; HeapTuple tuple;
HeapScanDesc scan; TableScanDesc scan;
ScanKeyData scanKeyData; ScanKeyData scanKeyData;
ScanKeyInit(&scanKeyData, ScanKeyInit(&scanKeyData,
...@@ -163,14 +164,14 @@ RemoveConversionById(Oid conversionOid) ...@@ -163,14 +164,14 @@ RemoveConversionById(Oid conversionOid)
/* open pg_conversion */ /* open pg_conversion */
rel = table_open(ConversionRelationId, RowExclusiveLock); rel = table_open(ConversionRelationId, RowExclusiveLock);
scan = heap_beginscan_catalog(rel, 1, &scanKeyData); scan = table_beginscan_catalog(rel, 1, &scanKeyData);
/* search for the target tuple */ /* search for the target tuple */
if (HeapTupleIsValid(tuple = heap_getnext(scan, ForwardScanDirection))) if (HeapTupleIsValid(tuple = heap_getnext(scan, ForwardScanDirection)))
CatalogTupleDelete(rel, &tuple->t_self); CatalogTupleDelete(rel, &tuple->t_self);
else else
elog(ERROR, "could not find tuple for conversion %u", conversionOid); elog(ERROR, "could not find tuple for conversion %u", conversionOid);
heap_endscan(scan); table_endscan(scan);
table_close(rel, RowExclusiveLock); table_close(rel, RowExclusiveLock);
} }
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "access/genam.h" #include "access/genam.h"
#include "access/heapam.h" #include "access/heapam.h"
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/tableam.h"
#include "catalog/indexing.h" #include "catalog/indexing.h"
#include "catalog/objectaccess.h" #include "catalog/objectaccess.h"
#include "catalog/pg_db_role_setting.h" #include "catalog/pg_db_role_setting.h"
...@@ -169,7 +170,7 @@ void ...@@ -169,7 +170,7 @@ void
DropSetting(Oid databaseid, Oid roleid) DropSetting(Oid databaseid, Oid roleid)
{ {
Relation relsetting; Relation relsetting;
HeapScanDesc scan; TableScanDesc scan;
ScanKeyData keys[2]; ScanKeyData keys[2];
HeapTuple tup; HeapTuple tup;
int numkeys = 0; int numkeys = 0;
...@@ -195,12 +196,12 @@ DropSetting(Oid databaseid, Oid roleid) ...@@ -195,12 +196,12 @@ DropSetting(Oid databaseid, Oid roleid)
numkeys++; numkeys++;
} }
scan = heap_beginscan_catalog(relsetting, numkeys, keys); scan = table_beginscan_catalog(relsetting, numkeys, keys);
while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection))) while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
{ {
CatalogTupleDelete(relsetting, &tup->t_self); CatalogTupleDelete(relsetting, &tup->t_self);
} }
heap_endscan(scan); table_endscan(scan);
table_close(relsetting, RowExclusiveLock); table_close(relsetting, RowExclusiveLock);
} }
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "access/genam.h" #include "access/genam.h"
#include "access/heapam.h" #include "access/heapam.h"
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/tableam.h"
#include "access/xact.h" #include "access/xact.h"
#include "catalog/catalog.h" #include "catalog/catalog.h"
...@@ -328,7 +329,7 @@ GetAllTablesPublicationRelations(void) ...@@ -328,7 +329,7 @@ GetAllTablesPublicationRelations(void)
{ {
Relation classRel; Relation classRel;
ScanKeyData key[1]; ScanKeyData key[1];
HeapScanDesc scan; TableScanDesc scan;
HeapTuple tuple; HeapTuple tuple;
List *result = NIL; List *result = NIL;
...@@ -339,7 +340,7 @@ GetAllTablesPublicationRelations(void) ...@@ -339,7 +340,7 @@ GetAllTablesPublicationRelations(void)
BTEqualStrategyNumber, F_CHAREQ, BTEqualStrategyNumber, F_CHAREQ,
CharGetDatum(RELKIND_RELATION)); CharGetDatum(RELKIND_RELATION));
scan = heap_beginscan_catalog(classRel, 1, key); scan = table_beginscan_catalog(classRel, 1, key);
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
{ {
...@@ -350,7 +351,7 @@ GetAllTablesPublicationRelations(void) ...@@ -350,7 +351,7 @@ GetAllTablesPublicationRelations(void)
result = lappend_oid(result, relid); result = lappend_oid(result, relid);
} }
heap_endscan(scan); table_endscan(scan);
table_close(classRel, AccessShareLock); table_close(classRel, AccessShareLock);
return result; return result;
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "access/genam.h" #include "access/genam.h"
#include "access/heapam.h" #include "access/heapam.h"
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/tableam.h"
#include "access/xact.h" #include "access/xact.h"
#include "catalog/indexing.h" #include "catalog/indexing.h"
...@@ -390,7 +391,7 @@ void ...@@ -390,7 +391,7 @@ void
RemoveSubscriptionRel(Oid subid, Oid relid) RemoveSubscriptionRel(Oid subid, Oid relid)
{ {
Relation rel; Relation rel;
HeapScanDesc scan; TableScanDesc scan;
ScanKeyData skey[2]; ScanKeyData skey[2];
HeapTuple tup; HeapTuple tup;
int nkeys = 0; int nkeys = 0;
...@@ -416,12 +417,12 @@ RemoveSubscriptionRel(Oid subid, Oid relid) ...@@ -416,12 +417,12 @@ RemoveSubscriptionRel(Oid subid, Oid relid)
} }
/* Do the search and delete what we found. */ /* Do the search and delete what we found. */
scan = heap_beginscan_catalog(rel, nkeys, skey); scan = table_beginscan_catalog(rel, nkeys, skey);
while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection))) while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
{ {
CatalogTupleDelete(rel, &tup->t_self); CatalogTupleDelete(rel, &tup->t_self);
} }
heap_endscan(scan); table_endscan(scan);
table_close(rel, RowExclusiveLock); table_close(rel, RowExclusiveLock);
} }
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "access/multixact.h" #include "access/multixact.h"
#include "access/relscan.h" #include "access/relscan.h"
#include "access/rewriteheap.h" #include "access/rewriteheap.h"
#include "access/tableam.h"
#include "access/transam.h" #include "access/transam.h"
#include "access/tuptoaster.h" #include "access/tuptoaster.h"
#include "access/xact.h" #include "access/xact.h"
...@@ -764,6 +765,7 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose, ...@@ -764,6 +765,7 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose,
Datum *values; Datum *values;
bool *isnull; bool *isnull;
IndexScanDesc indexScan; IndexScanDesc indexScan;
TableScanDesc tableScan;
HeapScanDesc heapScan; HeapScanDesc heapScan;
bool use_wal; bool use_wal;
bool is_system_catalog; bool is_system_catalog;
...@@ -779,6 +781,8 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose, ...@@ -779,6 +781,8 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose,
BlockNumber num_pages; BlockNumber num_pages;
int elevel = verbose ? INFO : DEBUG2; int elevel = verbose ? INFO : DEBUG2;
PGRUsage ru0; PGRUsage ru0;
TupleTableSlot *slot;
BufferHeapTupleTableSlot *hslot;
pg_rusage_init(&ru0); pg_rusage_init(&ru0);
...@@ -924,16 +928,21 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose, ...@@ -924,16 +928,21 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose,
*/ */
if (OldIndex != NULL && !use_sort) if (OldIndex != NULL && !use_sort)
{ {
tableScan = NULL;
heapScan = NULL; heapScan = NULL;
indexScan = index_beginscan(OldHeap, OldIndex, SnapshotAny, 0, 0); indexScan = index_beginscan(OldHeap, OldIndex, SnapshotAny, 0, 0);
index_rescan(indexScan, NULL, 0, NULL, 0); index_rescan(indexScan, NULL, 0, NULL, 0);
} }
else else
{ {
heapScan = heap_beginscan(OldHeap, SnapshotAny, 0, (ScanKey) NULL); tableScan = table_beginscan(OldHeap, SnapshotAny, 0, (ScanKey) NULL);
heapScan = (HeapScanDesc) tableScan;
indexScan = NULL; indexScan = NULL;
} }
slot = table_slot_create(OldHeap, NULL);
hslot = (BufferHeapTupleTableSlot *) slot;
/* Log what we're doing */ /* Log what we're doing */
if (indexScan != NULL) if (indexScan != NULL)
ereport(elevel, ereport(elevel,
...@@ -968,19 +977,19 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose, ...@@ -968,19 +977,19 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose,
if (indexScan != NULL) if (indexScan != NULL)
{ {
tuple = index_getnext(indexScan, ForwardScanDirection); if (!index_getnext_slot(indexScan, ForwardScanDirection, slot))
if (tuple == NULL)
break; break;
/* Since we used no scan keys, should never need to recheck */ /* Since we used no scan keys, should never need to recheck */
if (indexScan->xs_recheck) if (indexScan->xs_recheck)
elog(ERROR, "CLUSTER does not support lossy index conditions"); elog(ERROR, "CLUSTER does not support lossy index conditions");
buf = indexScan->xs_cbuf; tuple = hslot->base.tuple;
buf = hslot->buffer;
} }
else else
{ {
tuple = heap_getnext(heapScan, ForwardScanDirection); tuple = heap_getnext(tableScan, ForwardScanDirection);
if (tuple == NULL) if (tuple == NULL)
break; break;
...@@ -1066,7 +1075,9 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose, ...@@ -1066,7 +1075,9 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose,
if (indexScan != NULL) if (indexScan != NULL)
index_endscan(indexScan); index_endscan(indexScan);
if (heapScan != NULL) if (heapScan != NULL)
heap_endscan(heapScan); table_endscan(tableScan);
if (slot)
ExecDropSingleTupleTableSlot(slot);
/* /*
* In scan-and-sort mode, complete the sort, then read out all live tuples * In scan-and-sort mode, complete the sort, then read out all live tuples
...@@ -1694,7 +1705,7 @@ static List * ...@@ -1694,7 +1705,7 @@ static List *
get_tables_to_cluster(MemoryContext cluster_context) get_tables_to_cluster(MemoryContext cluster_context)
{ {
Relation indRelation; Relation indRelation;
HeapScanDesc scan; TableScanDesc scan;
ScanKeyData entry; ScanKeyData entry;
HeapTuple indexTuple; HeapTuple indexTuple;
Form_pg_index index; Form_pg_index index;
...@@ -1713,7 +1724,7 @@ get_tables_to_cluster(MemoryContext cluster_context) ...@@ -1713,7 +1724,7 @@ get_tables_to_cluster(MemoryContext cluster_context)
Anum_pg_index_indisclustered, Anum_pg_index_indisclustered,
BTEqualStrategyNumber, F_BOOLEQ, BTEqualStrategyNumber, F_BOOLEQ,
BoolGetDatum(true)); BoolGetDatum(true));
scan = heap_beginscan_catalog(indRelation, 1, &entry); scan = table_beginscan_catalog(indRelation, 1, &entry);
while ((indexTuple = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((indexTuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
{ {
index = (Form_pg_index) GETSTRUCT(indexTuple); index = (Form_pg_index) GETSTRUCT(indexTuple);
...@@ -1734,7 +1745,7 @@ get_tables_to_cluster(MemoryContext cluster_context) ...@@ -1734,7 +1745,7 @@ get_tables_to_cluster(MemoryContext cluster_context)
MemoryContextSwitchTo(old_context); MemoryContextSwitchTo(old_context);
} }
heap_endscan(scan); table_endscan(scan);
relation_close(indRelation, AccessShareLock); relation_close(indRelation, AccessShareLock);
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "access/genam.h" #include "access/genam.h"
#include "access/heapam.h" #include "access/heapam.h"
#include "access/tableam.h"
#include "catalog/index.h" #include "catalog/index.h"
#include "commands/trigger.h" #include "commands/trigger.h"
#include "executor/executor.h" #include "executor/executor.h"
...@@ -41,7 +42,7 @@ unique_key_recheck(PG_FUNCTION_ARGS) ...@@ -41,7 +42,7 @@ unique_key_recheck(PG_FUNCTION_ARGS)
{ {
TriggerData *trigdata = castNode(TriggerData, fcinfo->context); TriggerData *trigdata = castNode(TriggerData, fcinfo->context);
const char *funcname = "unique_key_recheck"; const char *funcname = "unique_key_recheck";
HeapTuple new_row; ItemPointerData checktid;
ItemPointerData tmptid; ItemPointerData tmptid;
Relation indexRel; Relation indexRel;
IndexInfo *indexInfo; IndexInfo *indexInfo;
...@@ -73,28 +74,30 @@ unique_key_recheck(PG_FUNCTION_ARGS) ...@@ -73,28 +74,30 @@ unique_key_recheck(PG_FUNCTION_ARGS)
* Get the new data that was inserted/updated. * Get the new data that was inserted/updated.
*/ */
if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event)) if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
new_row = trigdata->tg_trigtuple; checktid = trigdata->tg_trigslot->tts_tid;
else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event)) else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
new_row = trigdata->tg_newtuple; checktid = trigdata->tg_newslot->tts_tid;
else else
{ {
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED), (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
errmsg("function \"%s\" must be fired for INSERT or UPDATE", errmsg("function \"%s\" must be fired for INSERT or UPDATE",
funcname))); funcname)));
new_row = NULL; /* keep compiler quiet */ ItemPointerSetInvalid(&checktid); /* keep compiler quiet */
} }
slot = table_slot_create(trigdata->tg_relation, NULL);
/* /*
* If the new_row is now dead (ie, inserted and then deleted within our * If the row pointed at by checktid is now dead (ie, inserted and then
* transaction), we can skip the check. However, we have to be careful, * deleted within our transaction), we can skip the check. However, we
* because this trigger gets queued only in response to index insertions; * have to be careful, because this trigger gets queued only in response
* which means it does not get queued for HOT updates. The row we are * to index insertions; which means it does not get queued e.g. for HOT
* called for might now be dead, but have a live HOT child, in which case * updates. The row we are called for might now be dead, but have a live
* we still need to make the check --- effectively, we're applying the * HOT child, in which case we still need to make the check ---
* check against the live child row, although we can use the values from * effectively, we're applying the check against the live child row,
* this row since by definition all columns of interest to us are the * although we can use the values from this row since by definition all
* same. * columns of interest to us are the same.
* *
* This might look like just an optimization, because the index AM will * This might look like just an optimization, because the index AM will
* make this identical test before throwing an error. But it's actually * make this identical test before throwing an error. But it's actually
...@@ -103,13 +106,23 @@ unique_key_recheck(PG_FUNCTION_ARGS) ...@@ -103,13 +106,23 @@ unique_key_recheck(PG_FUNCTION_ARGS)
* it's possible the index entry has also been marked dead, and even * it's possible the index entry has also been marked dead, and even
* removed. * removed.
*/ */
tmptid = new_row->t_self; tmptid = checktid;
if (!heap_hot_search(&tmptid, trigdata->tg_relation, SnapshotSelf, NULL))
{ {
/* IndexFetchTableData *scan = table_index_fetch_begin(trigdata->tg_relation);
* All rows in the HOT chain are dead, so skip the check. bool call_again = false;
*/
return PointerGetDatum(NULL); if (!table_index_fetch_tuple(scan, &tmptid, SnapshotSelf, slot,
&call_again, NULL))
{
/*
* All rows referenced by the index entry are dead, so skip the
* check.
*/
ExecDropSingleTupleTableSlot(slot);
table_index_fetch_end(scan);
return PointerGetDatum(NULL);
}
table_index_fetch_end(scan);
} }
/* /*
...@@ -121,14 +134,6 @@ unique_key_recheck(PG_FUNCTION_ARGS) ...@@ -121,14 +134,6 @@ unique_key_recheck(PG_FUNCTION_ARGS)
RowExclusiveLock); RowExclusiveLock);
indexInfo = BuildIndexInfo(indexRel); indexInfo = BuildIndexInfo(indexRel);
/*
* The heap tuple must be put into a slot for FormIndexDatum.
*/
slot = MakeSingleTupleTableSlot(RelationGetDescr(trigdata->tg_relation),
&TTSOpsHeapTuple);
ExecStoreHeapTuple(new_row, slot, false);
/* /*
* Typically the index won't have expressions, but if it does we need an * Typically the index won't have expressions, but if it does we need an
* EState to evaluate them. We need it for exclusion constraints too, * EState to evaluate them. We need it for exclusion constraints too,
...@@ -163,11 +168,12 @@ unique_key_recheck(PG_FUNCTION_ARGS) ...@@ -163,11 +168,12 @@ unique_key_recheck(PG_FUNCTION_ARGS)
{ {
/* /*
* Note: this is not a real insert; it is a check that the index entry * Note: this is not a real insert; it is a check that the index entry
* that has already been inserted is unique. Passing t_self is * that has already been inserted is unique. Passing the tuple's tid
* correct even if t_self is now dead, because that is the TID the * (i.e. unmodified by table_index_fetch_tuple()) is correct even if
* index will know about. * the row is now dead, because that is the TID the index will know
* about.
*/ */
index_insert(indexRel, values, isnull, &(new_row->t_self), index_insert(indexRel, values, isnull, &checktid,
trigdata->tg_relation, UNIQUE_CHECK_EXISTING, trigdata->tg_relation, UNIQUE_CHECK_EXISTING,
indexInfo); indexInfo);
} }
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "access/heapam.h" #include "access/heapam.h"
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/sysattr.h" #include "access/sysattr.h"
#include "access/tableam.h"
#include "access/xact.h" #include "access/xact.h"
#include "access/xlog.h" #include "access/xlog.h"
#include "catalog/dependency.h" #include "catalog/dependency.h"
...@@ -2073,13 +2074,13 @@ CopyTo(CopyState cstate) ...@@ -2073,13 +2074,13 @@ CopyTo(CopyState cstate)
{ {
Datum *values; Datum *values;
bool *nulls; bool *nulls;
HeapScanDesc scandesc; TableScanDesc scandesc;
HeapTuple tuple; HeapTuple tuple;
values = (Datum *) palloc(num_phys_attrs * sizeof(Datum)); values = (Datum *) palloc(num_phys_attrs * sizeof(Datum));
nulls = (bool *) palloc(num_phys_attrs * sizeof(bool)); nulls = (bool *) palloc(num_phys_attrs * sizeof(bool));
scandesc = heap_beginscan(cstate->rel, GetActiveSnapshot(), 0, NULL); scandesc = table_beginscan(cstate->rel, GetActiveSnapshot(), 0, NULL);
processed = 0; processed = 0;
while ((tuple = heap_getnext(scandesc, ForwardScanDirection)) != NULL) while ((tuple = heap_getnext(scandesc, ForwardScanDirection)) != NULL)
...@@ -2094,7 +2095,7 @@ CopyTo(CopyState cstate) ...@@ -2094,7 +2095,7 @@ CopyTo(CopyState cstate)
processed++; processed++;
} }
heap_endscan(scandesc); table_endscan(scandesc);
pfree(values); pfree(values);
pfree(nulls); pfree(nulls);
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "access/genam.h" #include "access/genam.h"
#include "access/heapam.h" #include "access/heapam.h"
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/tableam.h"
#include "access/xact.h" #include "access/xact.h"
#include "access/xloginsert.h" #include "access/xloginsert.h"
#include "access/xlogutils.h" #include "access/xlogutils.h"
...@@ -97,7 +98,7 @@ static int errdetail_busy_db(int notherbackends, int npreparedxacts); ...@@ -97,7 +98,7 @@ static int errdetail_busy_db(int notherbackends, int npreparedxacts);
Oid Oid
createdb(ParseState *pstate, const CreatedbStmt *stmt) createdb(ParseState *pstate, const CreatedbStmt *stmt)
{ {
HeapScanDesc scan; TableScanDesc scan;
Relation rel; Relation rel;
Oid src_dboid; Oid src_dboid;
Oid src_owner; Oid src_owner;
...@@ -589,7 +590,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt) ...@@ -589,7 +590,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
* each one to the new database. * each one to the new database.
*/ */
rel = table_open(TableSpaceRelationId, AccessShareLock); rel = table_open(TableSpaceRelationId, AccessShareLock);
scan = heap_beginscan_catalog(rel, 0, NULL); scan = table_beginscan_catalog(rel, 0, NULL);
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
{ {
Form_pg_tablespace spaceform = (Form_pg_tablespace) GETSTRUCT(tuple); Form_pg_tablespace spaceform = (Form_pg_tablespace) GETSTRUCT(tuple);
...@@ -643,7 +644,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt) ...@@ -643,7 +644,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
XLOG_DBASE_CREATE | XLR_SPECIAL_REL_UPDATE); XLOG_DBASE_CREATE | XLR_SPECIAL_REL_UPDATE);
} }
} }
heap_endscan(scan); table_endscan(scan);
table_close(rel, AccessShareLock); table_close(rel, AccessShareLock);
/* /*
...@@ -1870,11 +1871,11 @@ static void ...@@ -1870,11 +1871,11 @@ static void
remove_dbtablespaces(Oid db_id) remove_dbtablespaces(Oid db_id)
{ {
Relation rel; Relation rel;
HeapScanDesc scan; TableScanDesc scan;
HeapTuple tuple; HeapTuple tuple;
rel = table_open(TableSpaceRelationId, AccessShareLock); rel = table_open(TableSpaceRelationId, AccessShareLock);
scan = heap_beginscan_catalog(rel, 0, NULL); scan = table_beginscan_catalog(rel, 0, NULL);
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
{ {
Form_pg_tablespace spcform = (Form_pg_tablespace) GETSTRUCT(tuple); Form_pg_tablespace spcform = (Form_pg_tablespace) GETSTRUCT(tuple);
...@@ -1917,7 +1918,7 @@ remove_dbtablespaces(Oid db_id) ...@@ -1917,7 +1918,7 @@ remove_dbtablespaces(Oid db_id)
pfree(dstpath); pfree(dstpath);
} }
heap_endscan(scan); table_endscan(scan);
table_close(rel, AccessShareLock); table_close(rel, AccessShareLock);
} }
...@@ -1938,11 +1939,11 @@ check_db_file_conflict(Oid db_id) ...@@ -1938,11 +1939,11 @@ check_db_file_conflict(Oid db_id)
{ {
bool result = false; bool result = false;
Relation rel; Relation rel;
HeapScanDesc scan; TableScanDesc scan;
HeapTuple tuple; HeapTuple tuple;
rel = table_open(TableSpaceRelationId, AccessShareLock); rel = table_open(TableSpaceRelationId, AccessShareLock);
scan = heap_beginscan_catalog(rel, 0, NULL); scan = table_beginscan_catalog(rel, 0, NULL);
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
{ {
Form_pg_tablespace spcform = (Form_pg_tablespace) GETSTRUCT(tuple); Form_pg_tablespace spcform = (Form_pg_tablespace) GETSTRUCT(tuple);
...@@ -1967,7 +1968,7 @@ check_db_file_conflict(Oid db_id) ...@@ -1967,7 +1968,7 @@ check_db_file_conflict(Oid db_id)
pfree(dstpath); pfree(dstpath);
} }
heap_endscan(scan); table_endscan(scan);
table_close(rel, AccessShareLock); table_close(rel, AccessShareLock);
return result; return result;
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/reloptions.h" #include "access/reloptions.h"
#include "access/sysattr.h" #include "access/sysattr.h"
#include "access/tableam.h"
#include "access/xact.h" #include "access/xact.h"
#include "catalog/catalog.h" #include "catalog/catalog.h"
#include "catalog/index.h" #include "catalog/index.h"
...@@ -2336,7 +2337,7 @@ ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind, ...@@ -2336,7 +2337,7 @@ ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind,
{ {
Oid objectOid; Oid objectOid;
Relation relationRelation; Relation relationRelation;
HeapScanDesc scan; TableScanDesc scan;
ScanKeyData scan_keys[1]; ScanKeyData scan_keys[1];
HeapTuple tuple; HeapTuple tuple;
MemoryContext private_context; MemoryContext private_context;
...@@ -2410,7 +2411,7 @@ ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind, ...@@ -2410,7 +2411,7 @@ ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind,
* rels will be processed indirectly by reindex_relation). * rels will be processed indirectly by reindex_relation).
*/ */
relationRelation = table_open(RelationRelationId, AccessShareLock); relationRelation = table_open(RelationRelationId, AccessShareLock);
scan = heap_beginscan_catalog(relationRelation, num_keys, scan_keys); scan = table_beginscan_catalog(relationRelation, num_keys, scan_keys);
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
{ {
Form_pg_class classtuple = (Form_pg_class) GETSTRUCT(tuple); Form_pg_class classtuple = (Form_pg_class) GETSTRUCT(tuple);
...@@ -2469,7 +2470,7 @@ ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind, ...@@ -2469,7 +2470,7 @@ ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind,
MemoryContextSwitchTo(old); MemoryContextSwitchTo(old);
} }
heap_endscan(scan); table_endscan(scan);
table_close(relationRelation, AccessShareLock); table_close(relationRelation, AccessShareLock);
/* Now reindex each rel in a separate transaction */ /* Now reindex each rel in a separate transaction */
......
This diff is collapsed.
...@@ -54,6 +54,7 @@ ...@@ -54,6 +54,7 @@
#include "access/reloptions.h" #include "access/reloptions.h"
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/sysattr.h" #include "access/sysattr.h"
#include "access/tableam.h"
#include "access/xact.h" #include "access/xact.h"
#include "access/xlog.h" #include "access/xlog.h"
#include "access/xloginsert.h" #include "access/xloginsert.h"
...@@ -405,7 +406,7 @@ DropTableSpace(DropTableSpaceStmt *stmt) ...@@ -405,7 +406,7 @@ DropTableSpace(DropTableSpaceStmt *stmt)
{ {
#ifdef HAVE_SYMLINK #ifdef HAVE_SYMLINK
char *tablespacename = stmt->tablespacename; char *tablespacename = stmt->tablespacename;
HeapScanDesc scandesc; TableScanDesc scandesc;
Relation rel; Relation rel;
HeapTuple tuple; HeapTuple tuple;
Form_pg_tablespace spcform; Form_pg_tablespace spcform;
...@@ -421,7 +422,7 @@ DropTableSpace(DropTableSpaceStmt *stmt) ...@@ -421,7 +422,7 @@ DropTableSpace(DropTableSpaceStmt *stmt)
Anum_pg_tablespace_spcname, Anum_pg_tablespace_spcname,
BTEqualStrategyNumber, F_NAMEEQ, BTEqualStrategyNumber, F_NAMEEQ,
CStringGetDatum(tablespacename)); CStringGetDatum(tablespacename));
scandesc = heap_beginscan_catalog(rel, 1, entry); scandesc = table_beginscan_catalog(rel, 1, entry);
tuple = heap_getnext(scandesc, ForwardScanDirection); tuple = heap_getnext(scandesc, ForwardScanDirection);
if (!HeapTupleIsValid(tuple)) if (!HeapTupleIsValid(tuple))
...@@ -439,7 +440,7 @@ DropTableSpace(DropTableSpaceStmt *stmt) ...@@ -439,7 +440,7 @@ DropTableSpace(DropTableSpaceStmt *stmt)
(errmsg("tablespace \"%s\" does not exist, skipping", (errmsg("tablespace \"%s\" does not exist, skipping",
tablespacename))); tablespacename)));
/* XXX I assume I need one or both of these next two calls */ /* XXX I assume I need one or both of these next two calls */
heap_endscan(scandesc); table_endscan(scandesc);
table_close(rel, NoLock); table_close(rel, NoLock);
} }
return; return;
...@@ -467,7 +468,7 @@ DropTableSpace(DropTableSpaceStmt *stmt) ...@@ -467,7 +468,7 @@ DropTableSpace(DropTableSpaceStmt *stmt)
*/ */
CatalogTupleDelete(rel, &tuple->t_self); CatalogTupleDelete(rel, &tuple->t_self);
heap_endscan(scandesc); table_endscan(scandesc);
/* /*
* Remove any comments or security labels on this tablespace. * Remove any comments or security labels on this tablespace.
...@@ -918,7 +919,7 @@ RenameTableSpace(const char *oldname, const char *newname) ...@@ -918,7 +919,7 @@ RenameTableSpace(const char *oldname, const char *newname)
Oid tspId; Oid tspId;
Relation rel; Relation rel;
ScanKeyData entry[1]; ScanKeyData entry[1];
HeapScanDesc scan; TableScanDesc scan;
HeapTuple tup; HeapTuple tup;
HeapTuple newtuple; HeapTuple newtuple;
Form_pg_tablespace newform; Form_pg_tablespace newform;
...@@ -931,7 +932,7 @@ RenameTableSpace(const char *oldname, const char *newname) ...@@ -931,7 +932,7 @@ RenameTableSpace(const char *oldname, const char *newname)
Anum_pg_tablespace_spcname, Anum_pg_tablespace_spcname,
BTEqualStrategyNumber, F_NAMEEQ, BTEqualStrategyNumber, F_NAMEEQ,
CStringGetDatum(oldname)); CStringGetDatum(oldname));
scan = heap_beginscan_catalog(rel, 1, entry); scan = table_beginscan_catalog(rel, 1, entry);
tup = heap_getnext(scan, ForwardScanDirection); tup = heap_getnext(scan, ForwardScanDirection);
if (!HeapTupleIsValid(tup)) if (!HeapTupleIsValid(tup))
ereport(ERROR, ereport(ERROR,
...@@ -943,7 +944,7 @@ RenameTableSpace(const char *oldname, const char *newname) ...@@ -943,7 +944,7 @@ RenameTableSpace(const char *oldname, const char *newname)
newform = (Form_pg_tablespace) GETSTRUCT(newtuple); newform = (Form_pg_tablespace) GETSTRUCT(newtuple);
tspId = newform->oid; tspId = newform->oid;
heap_endscan(scan); table_endscan(scan);
/* Must be owner */ /* Must be owner */
if (!pg_tablespace_ownercheck(tspId, GetUserId())) if (!pg_tablespace_ownercheck(tspId, GetUserId()))
...@@ -961,7 +962,7 @@ RenameTableSpace(const char *oldname, const char *newname) ...@@ -961,7 +962,7 @@ RenameTableSpace(const char *oldname, const char *newname)
Anum_pg_tablespace_spcname, Anum_pg_tablespace_spcname,
BTEqualStrategyNumber, F_NAMEEQ, BTEqualStrategyNumber, F_NAMEEQ,
CStringGetDatum(newname)); CStringGetDatum(newname));
scan = heap_beginscan_catalog(rel, 1, entry); scan = table_beginscan_catalog(rel, 1, entry);
tup = heap_getnext(scan, ForwardScanDirection); tup = heap_getnext(scan, ForwardScanDirection);
if (HeapTupleIsValid(tup)) if (HeapTupleIsValid(tup))
ereport(ERROR, ereport(ERROR,
...@@ -969,7 +970,7 @@ RenameTableSpace(const char *oldname, const char *newname) ...@@ -969,7 +970,7 @@ RenameTableSpace(const char *oldname, const char *newname)
errmsg("tablespace \"%s\" already exists", errmsg("tablespace \"%s\" already exists",
newname))); newname)));
heap_endscan(scan); table_endscan(scan);
/* OK, update the entry */ /* OK, update the entry */
namestrcpy(&(newform->spcname), newname); namestrcpy(&(newform->spcname), newname);
...@@ -993,7 +994,7 @@ AlterTableSpaceOptions(AlterTableSpaceOptionsStmt *stmt) ...@@ -993,7 +994,7 @@ AlterTableSpaceOptions(AlterTableSpaceOptionsStmt *stmt)
{ {
Relation rel; Relation rel;
ScanKeyData entry[1]; ScanKeyData entry[1];
HeapScanDesc scandesc; TableScanDesc scandesc;
HeapTuple tup; HeapTuple tup;
Oid tablespaceoid; Oid tablespaceoid;
Datum datum; Datum datum;
...@@ -1011,7 +1012,7 @@ AlterTableSpaceOptions(AlterTableSpaceOptionsStmt *stmt) ...@@ -1011,7 +1012,7 @@ AlterTableSpaceOptions(AlterTableSpaceOptionsStmt *stmt)
Anum_pg_tablespace_spcname, Anum_pg_tablespace_spcname,
BTEqualStrategyNumber, F_NAMEEQ, BTEqualStrategyNumber, F_NAMEEQ,
CStringGetDatum(stmt->tablespacename)); CStringGetDatum(stmt->tablespacename));
scandesc = heap_beginscan_catalog(rel, 1, entry); scandesc = table_beginscan_catalog(rel, 1, entry);
tup = heap_getnext(scandesc, ForwardScanDirection); tup = heap_getnext(scandesc, ForwardScanDirection);
if (!HeapTupleIsValid(tup)) if (!HeapTupleIsValid(tup))
ereport(ERROR, ereport(ERROR,
...@@ -1053,7 +1054,7 @@ AlterTableSpaceOptions(AlterTableSpaceOptionsStmt *stmt) ...@@ -1053,7 +1054,7 @@ AlterTableSpaceOptions(AlterTableSpaceOptionsStmt *stmt)
heap_freetuple(newtuple); heap_freetuple(newtuple);
/* Conclude heap scan. */ /* Conclude heap scan. */
heap_endscan(scandesc); table_endscan(scandesc);
table_close(rel, NoLock); table_close(rel, NoLock);
return tablespaceoid; return tablespaceoid;
...@@ -1387,7 +1388,7 @@ get_tablespace_oid(const char *tablespacename, bool missing_ok) ...@@ -1387,7 +1388,7 @@ get_tablespace_oid(const char *tablespacename, bool missing_ok)
{ {
Oid result; Oid result;
Relation rel; Relation rel;
HeapScanDesc scandesc; TableScanDesc scandesc;
HeapTuple tuple; HeapTuple tuple;
ScanKeyData entry[1]; ScanKeyData entry[1];
...@@ -1402,7 +1403,7 @@ get_tablespace_oid(const char *tablespacename, bool missing_ok) ...@@ -1402,7 +1403,7 @@ get_tablespace_oid(const char *tablespacename, bool missing_ok)
Anum_pg_tablespace_spcname, Anum_pg_tablespace_spcname,
BTEqualStrategyNumber, F_NAMEEQ, BTEqualStrategyNumber, F_NAMEEQ,
CStringGetDatum(tablespacename)); CStringGetDatum(tablespacename));
scandesc = heap_beginscan_catalog(rel, 1, entry); scandesc = table_beginscan_catalog(rel, 1, entry);
tuple = heap_getnext(scandesc, ForwardScanDirection); tuple = heap_getnext(scandesc, ForwardScanDirection);
/* We assume that there can be at most one matching tuple */ /* We assume that there can be at most one matching tuple */
...@@ -1411,7 +1412,7 @@ get_tablespace_oid(const char *tablespacename, bool missing_ok) ...@@ -1411,7 +1412,7 @@ get_tablespace_oid(const char *tablespacename, bool missing_ok)
else else
result = InvalidOid; result = InvalidOid;
heap_endscan(scandesc); table_endscan(scandesc);
table_close(rel, AccessShareLock); table_close(rel, AccessShareLock);
if (!OidIsValid(result) && !missing_ok) if (!OidIsValid(result) && !missing_ok)
...@@ -1433,7 +1434,7 @@ get_tablespace_name(Oid spc_oid) ...@@ -1433,7 +1434,7 @@ get_tablespace_name(Oid spc_oid)
{ {
char *result; char *result;
Relation rel; Relation rel;
HeapScanDesc scandesc; TableScanDesc scandesc;
HeapTuple tuple; HeapTuple tuple;
ScanKeyData entry[1]; ScanKeyData entry[1];
...@@ -1448,7 +1449,7 @@ get_tablespace_name(Oid spc_oid) ...@@ -1448,7 +1449,7 @@ get_tablespace_name(Oid spc_oid)
Anum_pg_tablespace_oid, Anum_pg_tablespace_oid,
BTEqualStrategyNumber, F_OIDEQ, BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(spc_oid)); ObjectIdGetDatum(spc_oid));
scandesc = heap_beginscan_catalog(rel, 1, entry); scandesc = table_beginscan_catalog(rel, 1, entry);
tuple = heap_getnext(scandesc, ForwardScanDirection); tuple = heap_getnext(scandesc, ForwardScanDirection);
/* We assume that there can be at most one matching tuple */ /* We assume that there can be at most one matching tuple */
...@@ -1457,7 +1458,7 @@ get_tablespace_name(Oid spc_oid) ...@@ -1457,7 +1458,7 @@ get_tablespace_name(Oid spc_oid)
else else
result = NULL; result = NULL;
heap_endscan(scandesc); table_endscan(scandesc);
table_close(rel, AccessShareLock); table_close(rel, AccessShareLock);
return result; return result;
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "access/genam.h" #include "access/genam.h"
#include "access/heapam.h" #include "access/heapam.h"
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/tableam.h"
#include "access/xact.h" #include "access/xact.h"
#include "catalog/binary_upgrade.h" #include "catalog/binary_upgrade.h"
#include "catalog/catalog.h" #include "catalog/catalog.h"
...@@ -2362,14 +2363,15 @@ AlterDomainNotNull(List *names, bool notNull) ...@@ -2362,14 +2363,15 @@ AlterDomainNotNull(List *names, bool notNull)
RelToCheck *rtc = (RelToCheck *) lfirst(rt); RelToCheck *rtc = (RelToCheck *) lfirst(rt);
Relation testrel = rtc->rel; Relation testrel = rtc->rel;
TupleDesc tupdesc = RelationGetDescr(testrel); TupleDesc tupdesc = RelationGetDescr(testrel);
HeapScanDesc scan; TupleTableSlot *slot;
HeapTuple tuple; TableScanDesc scan;
Snapshot snapshot; Snapshot snapshot;
/* Scan all tuples in this relation */ /* Scan all tuples in this relation */
snapshot = RegisterSnapshot(GetLatestSnapshot()); snapshot = RegisterSnapshot(GetLatestSnapshot());
scan = heap_beginscan(testrel, snapshot, 0, NULL); scan = table_beginscan(testrel, snapshot, 0, NULL);
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) slot = table_slot_create(testrel, NULL);
while (table_scan_getnextslot(scan, ForwardScanDirection, slot))
{ {
int i; int i;
...@@ -2379,7 +2381,7 @@ AlterDomainNotNull(List *names, bool notNull) ...@@ -2379,7 +2381,7 @@ AlterDomainNotNull(List *names, bool notNull)
int attnum = rtc->atts[i]; int attnum = rtc->atts[i];
Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1); Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1);
if (heap_attisnull(tuple, attnum, tupdesc)) if (slot_attisnull(slot, attnum))
{ {
/* /*
* In principle the auxiliary information for this * In principle the auxiliary information for this
...@@ -2398,7 +2400,8 @@ AlterDomainNotNull(List *names, bool notNull) ...@@ -2398,7 +2400,8 @@ AlterDomainNotNull(List *names, bool notNull)
} }
} }
} }
heap_endscan(scan); ExecDropSingleTupleTableSlot(slot);
table_endscan(scan);
UnregisterSnapshot(snapshot); UnregisterSnapshot(snapshot);
/* Close each rel after processing, but keep lock */ /* Close each rel after processing, but keep lock */
...@@ -2776,14 +2779,15 @@ validateDomainConstraint(Oid domainoid, char *ccbin) ...@@ -2776,14 +2779,15 @@ validateDomainConstraint(Oid domainoid, char *ccbin)
RelToCheck *rtc = (RelToCheck *) lfirst(rt); RelToCheck *rtc = (RelToCheck *) lfirst(rt);
Relation testrel = rtc->rel; Relation testrel = rtc->rel;
TupleDesc tupdesc = RelationGetDescr(testrel); TupleDesc tupdesc = RelationGetDescr(testrel);
HeapScanDesc scan; TupleTableSlot *slot;
HeapTuple tuple; TableScanDesc scan;
Snapshot snapshot; Snapshot snapshot;
/* Scan all tuples in this relation */ /* Scan all tuples in this relation */
snapshot = RegisterSnapshot(GetLatestSnapshot()); snapshot = RegisterSnapshot(GetLatestSnapshot());
scan = heap_beginscan(testrel, snapshot, 0, NULL); scan = table_beginscan(testrel, snapshot, 0, NULL);
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) slot = table_slot_create(testrel, NULL);
while (table_scan_getnextslot(scan, ForwardScanDirection, slot))
{ {
int i; int i;
...@@ -2796,7 +2800,7 @@ validateDomainConstraint(Oid domainoid, char *ccbin) ...@@ -2796,7 +2800,7 @@ validateDomainConstraint(Oid domainoid, char *ccbin)
Datum conResult; Datum conResult;
Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1); Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1);
d = heap_getattr(tuple, attnum, tupdesc, &isNull); d = slot_getattr(slot, attnum, &isNull);
econtext->domainValue_datum = d; econtext->domainValue_datum = d;
econtext->domainValue_isNull = isNull; econtext->domainValue_isNull = isNull;
...@@ -2826,7 +2830,8 @@ validateDomainConstraint(Oid domainoid, char *ccbin) ...@@ -2826,7 +2830,8 @@ validateDomainConstraint(Oid domainoid, char *ccbin)
ResetExprContext(econtext); ResetExprContext(econtext);
} }
heap_endscan(scan); ExecDropSingleTupleTableSlot(slot);
table_endscan(scan);
UnregisterSnapshot(snapshot); UnregisterSnapshot(snapshot);
/* Hold relation lock till commit (XXX bad for concurrency) */ /* Hold relation lock till commit (XXX bad for concurrency) */
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include "access/heapam.h" #include "access/heapam.h"
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/multixact.h" #include "access/multixact.h"
#include "access/tableam.h"
#include "access/transam.h" #include "access/transam.h"
#include "access/xact.h" #include "access/xact.h"
#include "catalog/namespace.h" #include "catalog/namespace.h"
...@@ -745,12 +746,12 @@ get_all_vacuum_rels(int options) ...@@ -745,12 +746,12 @@ get_all_vacuum_rels(int options)
{ {
List *vacrels = NIL; List *vacrels = NIL;
Relation pgclass; Relation pgclass;
HeapScanDesc scan; TableScanDesc scan;
HeapTuple tuple; HeapTuple tuple;
pgclass = table_open(RelationRelationId, AccessShareLock); pgclass = table_open(RelationRelationId, AccessShareLock);
scan = heap_beginscan_catalog(pgclass, 0, NULL); scan = table_beginscan_catalog(pgclass, 0, NULL);
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
{ {
...@@ -784,7 +785,7 @@ get_all_vacuum_rels(int options) ...@@ -784,7 +785,7 @@ get_all_vacuum_rels(int options)
MemoryContextSwitchTo(oldcontext); MemoryContextSwitchTo(oldcontext);
} }
heap_endscan(scan); table_endscan(scan);
table_close(pgclass, AccessShareLock); table_close(pgclass, AccessShareLock);
return vacrels; return vacrels;
...@@ -1381,7 +1382,7 @@ vac_truncate_clog(TransactionId frozenXID, ...@@ -1381,7 +1382,7 @@ vac_truncate_clog(TransactionId frozenXID,
{ {
TransactionId nextXID = ReadNewTransactionId(); TransactionId nextXID = ReadNewTransactionId();
Relation relation; Relation relation;
HeapScanDesc scan; TableScanDesc scan;
HeapTuple tuple; HeapTuple tuple;
Oid oldestxid_datoid; Oid oldestxid_datoid;
Oid minmulti_datoid; Oid minmulti_datoid;
...@@ -1412,7 +1413,7 @@ vac_truncate_clog(TransactionId frozenXID, ...@@ -1412,7 +1413,7 @@ vac_truncate_clog(TransactionId frozenXID,
*/ */
relation = table_open(DatabaseRelationId, AccessShareLock); relation = table_open(DatabaseRelationId, AccessShareLock);
scan = heap_beginscan_catalog(relation, 0, NULL); scan = table_beginscan_catalog(relation, 0, NULL);
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
{ {
...@@ -1451,7 +1452,7 @@ vac_truncate_clog(TransactionId frozenXID, ...@@ -1451,7 +1452,7 @@ vac_truncate_clog(TransactionId frozenXID,
} }
} }
heap_endscan(scan); table_endscan(scan);
table_close(relation, AccessShareLock); table_close(relation, AccessShareLock);
......
...@@ -204,7 +204,7 @@ execCurrentOf(CurrentOfExpr *cexpr, ...@@ -204,7 +204,7 @@ execCurrentOf(CurrentOfExpr *cexpr,
*/ */
IndexScanDesc scan = ((IndexOnlyScanState *) scanstate)->ioss_ScanDesc; IndexScanDesc scan = ((IndexOnlyScanState *) scanstate)->ioss_ScanDesc;
*current_tid = scan->xs_ctup.t_self; *current_tid = scan->xs_heaptid;
} }
else else
{ {
......
...@@ -108,6 +108,7 @@ ...@@ -108,6 +108,7 @@
#include "access/genam.h" #include "access/genam.h"
#include "access/relscan.h" #include "access/relscan.h"
#include "access/tableam.h"
#include "access/xact.h" #include "access/xact.h"
#include "catalog/index.h" #include "catalog/index.h"
#include "executor/executor.h" #include "executor/executor.h"
...@@ -651,7 +652,6 @@ check_exclusion_or_unique_constraint(Relation heap, Relation index, ...@@ -651,7 +652,6 @@ check_exclusion_or_unique_constraint(Relation heap, Relation index,
Oid *index_collations = index->rd_indcollation; Oid *index_collations = index->rd_indcollation;
int indnkeyatts = IndexRelationGetNumberOfKeyAttributes(index); int indnkeyatts = IndexRelationGetNumberOfKeyAttributes(index);
IndexScanDesc index_scan; IndexScanDesc index_scan;
HeapTuple tup;
ScanKeyData scankeys[INDEX_MAX_KEYS]; ScanKeyData scankeys[INDEX_MAX_KEYS];
SnapshotData DirtySnapshot; SnapshotData DirtySnapshot;
int i; int i;
...@@ -707,8 +707,7 @@ check_exclusion_or_unique_constraint(Relation heap, Relation index, ...@@ -707,8 +707,7 @@ check_exclusion_or_unique_constraint(Relation heap, Relation index,
* to this slot. Be sure to save and restore caller's value for * to this slot. Be sure to save and restore caller's value for
* scantuple. * scantuple.
*/ */
existing_slot = MakeSingleTupleTableSlot(RelationGetDescr(heap), existing_slot = table_slot_create(heap, NULL);
&TTSOpsHeapTuple);
econtext = GetPerTupleExprContext(estate); econtext = GetPerTupleExprContext(estate);
save_scantuple = econtext->ecxt_scantuple; save_scantuple = econtext->ecxt_scantuple;
...@@ -724,11 +723,9 @@ retry: ...@@ -724,11 +723,9 @@ retry:
index_scan = index_beginscan(heap, index, &DirtySnapshot, indnkeyatts, 0); index_scan = index_beginscan(heap, index, &DirtySnapshot, indnkeyatts, 0);
index_rescan(index_scan, scankeys, indnkeyatts, NULL, 0); index_rescan(index_scan, scankeys, indnkeyatts, NULL, 0);
while ((tup = index_getnext(index_scan, while (index_getnext_slot(index_scan, ForwardScanDirection, existing_slot))
ForwardScanDirection)) != NULL)
{ {
TransactionId xwait; TransactionId xwait;
ItemPointerData ctid_wait;
XLTW_Oper reason_wait; XLTW_Oper reason_wait;
Datum existing_values[INDEX_MAX_KEYS]; Datum existing_values[INDEX_MAX_KEYS];
bool existing_isnull[INDEX_MAX_KEYS]; bool existing_isnull[INDEX_MAX_KEYS];
...@@ -739,7 +736,7 @@ retry: ...@@ -739,7 +736,7 @@ retry:
* Ignore the entry for the tuple we're trying to check. * Ignore the entry for the tuple we're trying to check.
*/ */
if (ItemPointerIsValid(tupleid) && if (ItemPointerIsValid(tupleid) &&
ItemPointerEquals(tupleid, &tup->t_self)) ItemPointerEquals(tupleid, &existing_slot->tts_tid))
{ {
if (found_self) /* should not happen */ if (found_self) /* should not happen */
elog(ERROR, "found self tuple multiple times in index \"%s\"", elog(ERROR, "found self tuple multiple times in index \"%s\"",
...@@ -752,7 +749,6 @@ retry: ...@@ -752,7 +749,6 @@ retry:
* Extract the index column values and isnull flags from the existing * Extract the index column values and isnull flags from the existing
* tuple. * tuple.
*/ */
ExecStoreHeapTuple(tup, existing_slot, false);
FormIndexDatum(indexInfo, existing_slot, estate, FormIndexDatum(indexInfo, existing_slot, estate,
existing_values, existing_isnull); existing_values, existing_isnull);
...@@ -787,7 +783,6 @@ retry: ...@@ -787,7 +783,6 @@ retry:
DirtySnapshot.speculativeToken && DirtySnapshot.speculativeToken &&
TransactionIdPrecedes(GetCurrentTransactionId(), xwait)))) TransactionIdPrecedes(GetCurrentTransactionId(), xwait))))
{ {
ctid_wait = tup->t_data->t_ctid;
reason_wait = indexInfo->ii_ExclusionOps ? reason_wait = indexInfo->ii_ExclusionOps ?
XLTW_RecheckExclusionConstr : XLTW_InsertIndex; XLTW_RecheckExclusionConstr : XLTW_InsertIndex;
index_endscan(index_scan); index_endscan(index_scan);
...@@ -795,7 +790,8 @@ retry: ...@@ -795,7 +790,8 @@ retry:
SpeculativeInsertionWait(DirtySnapshot.xmin, SpeculativeInsertionWait(DirtySnapshot.xmin,
DirtySnapshot.speculativeToken); DirtySnapshot.speculativeToken);
else else
XactLockTableWait(xwait, heap, &ctid_wait, reason_wait); XactLockTableWait(xwait, heap,
&existing_slot->tts_tid, reason_wait);
goto retry; goto retry;
} }
...@@ -807,7 +803,7 @@ retry: ...@@ -807,7 +803,7 @@ retry:
{ {
conflict = true; conflict = true;
if (conflictTid) if (conflictTid)
*conflictTid = tup->t_self; *conflictTid = existing_slot->tts_tid;
break; break;
} }
......
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
#include "access/heapam.h" #include "access/heapam.h"
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/sysattr.h" #include "access/sysattr.h"
#include "access/tableam.h"
#include "access/transam.h" #include "access/transam.h"
#include "access/xact.h" #include "access/xact.h"
#include "catalog/namespace.h" #include "catalog/namespace.h"
...@@ -2802,9 +2803,8 @@ EvalPlanQualSlot(EPQState *epqstate, ...@@ -2802,9 +2803,8 @@ EvalPlanQualSlot(EPQState *epqstate,
oldcontext = MemoryContextSwitchTo(epqstate->estate->es_query_cxt); oldcontext = MemoryContextSwitchTo(epqstate->estate->es_query_cxt);
if (relation) if (relation)
*slot = ExecAllocTableSlot(&epqstate->estate->es_tupleTable, *slot = table_slot_create(relation,
RelationGetDescr(relation), &epqstate->estate->es_tupleTable);
&TTSOpsBufferHeapTuple);
else else
*slot = ExecAllocTableSlot(&epqstate->estate->es_tupleTable, *slot = ExecAllocTableSlot(&epqstate->estate->es_tupleTable,
epqstate->origslot->tts_tupleDescriptor, epqstate->origslot->tts_tupleDescriptor,
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "postgres.h" #include "postgres.h"
#include "access/table.h" #include "access/table.h"
#include "access/tableam.h"
#include "catalog/partition.h" #include "catalog/partition.h"
#include "catalog/pg_inherits.h" #include "catalog/pg_inherits.h"
#include "catalog/pg_type.h" #include "catalog/pg_type.h"
...@@ -727,10 +728,8 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate, ...@@ -727,10 +728,8 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate,
if (node->onConflictAction == ONCONFLICT_UPDATE) if (node->onConflictAction == ONCONFLICT_UPDATE)
{ {
TupleConversionMap *map; TupleConversionMap *map;
TupleDesc leaf_desc;
map = leaf_part_rri->ri_PartitionInfo->pi_RootToPartitionMap; map = leaf_part_rri->ri_PartitionInfo->pi_RootToPartitionMap;
leaf_desc = RelationGetDescr(leaf_part_rri->ri_RelationDesc);
Assert(node->onConflictSet != NIL); Assert(node->onConflictSet != NIL);
Assert(rootResultRelInfo->ri_onConflict != NULL); Assert(rootResultRelInfo->ri_onConflict != NULL);
...@@ -743,9 +742,8 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate, ...@@ -743,9 +742,8 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate,
* descriptors match. * descriptors match.
*/ */
leaf_part_rri->ri_onConflict->oc_Existing = leaf_part_rri->ri_onConflict->oc_Existing =
ExecInitExtraTupleSlot(mtstate->ps.state, table_slot_create(leaf_part_rri->ri_RelationDesc,
leaf_desc, &mtstate->ps.state->es_tupleTable);
&TTSOpsBufferHeapTuple);
/* /*
* If the partition's tuple descriptor matches exactly the root * If the partition's tuple descriptor matches exactly the root
...@@ -920,8 +918,7 @@ ExecInitRoutingInfo(ModifyTableState *mtstate, ...@@ -920,8 +918,7 @@ ExecInitRoutingInfo(ModifyTableState *mtstate,
* end of the command. * end of the command.
*/ */
partrouteinfo->pi_PartitionTupleSlot = partrouteinfo->pi_PartitionTupleSlot =
ExecInitExtraTupleSlot(estate, RelationGetDescr(partrel), table_slot_create(partrel, &estate->es_tupleTable);
&TTSOpsHeapTuple);
} }
else else
partrouteinfo->pi_PartitionTupleSlot = NULL; partrouteinfo->pi_PartitionTupleSlot = NULL;
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "access/genam.h" #include "access/genam.h"
#include "access/heapam.h" #include "access/heapam.h"
#include "access/relscan.h" #include "access/relscan.h"
#include "access/tableam.h"
#include "access/transam.h" #include "access/transam.h"
#include "access/xact.h" #include "access/xact.h"
#include "commands/trigger.h" #include "commands/trigger.h"
...@@ -118,7 +119,6 @@ RelationFindReplTupleByIndex(Relation rel, Oid idxoid, ...@@ -118,7 +119,6 @@ RelationFindReplTupleByIndex(Relation rel, Oid idxoid,
TupleTableSlot *searchslot, TupleTableSlot *searchslot,
TupleTableSlot *outslot) TupleTableSlot *outslot)
{ {
HeapTuple scantuple;
ScanKeyData skey[INDEX_MAX_KEYS]; ScanKeyData skey[INDEX_MAX_KEYS];
IndexScanDesc scan; IndexScanDesc scan;
SnapshotData snap; SnapshotData snap;
...@@ -144,10 +144,9 @@ retry: ...@@ -144,10 +144,9 @@ retry:
index_rescan(scan, skey, IndexRelationGetNumberOfKeyAttributes(idxrel), NULL, 0); index_rescan(scan, skey, IndexRelationGetNumberOfKeyAttributes(idxrel), NULL, 0);
/* Try to find the tuple */ /* Try to find the tuple */
if ((scantuple = index_getnext(scan, ForwardScanDirection)) != NULL) if (index_getnext_slot(scan, ForwardScanDirection, outslot))
{ {
found = true; found = true;
ExecStoreHeapTuple(scantuple, outslot, false);
ExecMaterializeSlot(outslot); ExecMaterializeSlot(outslot);
xwait = TransactionIdIsValid(snap.xmin) ? xwait = TransactionIdIsValid(snap.xmin) ?
...@@ -222,19 +221,21 @@ retry: ...@@ -222,19 +221,21 @@ retry:
} }
/* /*
* Compare the tuple and slot and check if they have equal values. * Compare the tuples in the slots by checking if they have equal values.
*/ */
static bool static bool
tuple_equals_slot(TupleDesc desc, HeapTuple tup, TupleTableSlot *slot) tuples_equal(TupleTableSlot *slot1, TupleTableSlot *slot2)
{ {
Datum values[MaxTupleAttributeNumber]; int attrnum;
bool isnull[MaxTupleAttributeNumber];
int attrnum;
heap_deform_tuple(tup, desc, values, isnull); Assert(slot1->tts_tupleDescriptor->natts ==
slot2->tts_tupleDescriptor->natts);
slot_getallattrs(slot1);
slot_getallattrs(slot2);
/* Check equality of the attributes. */ /* Check equality of the attributes. */
for (attrnum = 0; attrnum < desc->natts; attrnum++) for (attrnum = 0; attrnum < slot1->tts_tupleDescriptor->natts; attrnum++)
{ {
Form_pg_attribute att; Form_pg_attribute att;
TypeCacheEntry *typentry; TypeCacheEntry *typentry;
...@@ -243,16 +244,16 @@ tuple_equals_slot(TupleDesc desc, HeapTuple tup, TupleTableSlot *slot) ...@@ -243,16 +244,16 @@ tuple_equals_slot(TupleDesc desc, HeapTuple tup, TupleTableSlot *slot)
* If one value is NULL and other is not, then they are certainly not * If one value is NULL and other is not, then they are certainly not
* equal * equal
*/ */
if (isnull[attrnum] != slot->tts_isnull[attrnum]) if (slot1->tts_isnull[attrnum] != slot2->tts_isnull[attrnum])
return false; return false;
/* /*
* If both are NULL, they can be considered equal. * If both are NULL, they can be considered equal.
*/ */
if (isnull[attrnum]) if (slot1->tts_isnull[attrnum] || slot2->tts_isnull[attrnum])
continue; continue;
att = TupleDescAttr(desc, attrnum); att = TupleDescAttr(slot1->tts_tupleDescriptor, attrnum);
typentry = lookup_type_cache(att->atttypid, TYPECACHE_EQ_OPR_FINFO); typentry = lookup_type_cache(att->atttypid, TYPECACHE_EQ_OPR_FINFO);
if (!OidIsValid(typentry->eq_opr_finfo.fn_oid)) if (!OidIsValid(typentry->eq_opr_finfo.fn_oid))
...@@ -262,8 +263,8 @@ tuple_equals_slot(TupleDesc desc, HeapTuple tup, TupleTableSlot *slot) ...@@ -262,8 +263,8 @@ tuple_equals_slot(TupleDesc desc, HeapTuple tup, TupleTableSlot *slot)
format_type_be(att->atttypid)))); format_type_be(att->atttypid))));
if (!DatumGetBool(FunctionCall2(&typentry->eq_opr_finfo, if (!DatumGetBool(FunctionCall2(&typentry->eq_opr_finfo,
values[attrnum], slot1->tts_values[attrnum],
slot->tts_values[attrnum]))) slot2->tts_values[attrnum])))
return false; return false;
} }
...@@ -284,33 +285,33 @@ bool ...@@ -284,33 +285,33 @@ bool
RelationFindReplTupleSeq(Relation rel, LockTupleMode lockmode, RelationFindReplTupleSeq(Relation rel, LockTupleMode lockmode,
TupleTableSlot *searchslot, TupleTableSlot *outslot) TupleTableSlot *searchslot, TupleTableSlot *outslot)
{ {
HeapTuple scantuple; TupleTableSlot *scanslot;
HeapScanDesc scan; TableScanDesc scan;
SnapshotData snap; SnapshotData snap;
TransactionId xwait; TransactionId xwait;
bool found; bool found;
TupleDesc desc = RelationGetDescr(rel); TupleDesc desc PG_USED_FOR_ASSERTS_ONLY = RelationGetDescr(rel);
Assert(equalTupleDescs(desc, outslot->tts_tupleDescriptor)); Assert(equalTupleDescs(desc, outslot->tts_tupleDescriptor));
/* Start a heap scan. */ /* Start a heap scan. */
InitDirtySnapshot(snap); InitDirtySnapshot(snap);
scan = heap_beginscan(rel, &snap, 0, NULL); scan = table_beginscan(rel, &snap, 0, NULL);
scanslot = table_slot_create(rel, NULL);
retry: retry:
found = false; found = false;
heap_rescan(scan, NULL); table_rescan(scan, NULL);
/* Try to find the tuple */ /* Try to find the tuple */
while ((scantuple = heap_getnext(scan, ForwardScanDirection)) != NULL) while (table_scan_getnextslot(scan, ForwardScanDirection, scanslot))
{ {
if (!tuple_equals_slot(desc, scantuple, searchslot)) if (!tuples_equal(scanslot, searchslot))
continue; continue;
found = true; found = true;
ExecStoreHeapTuple(scantuple, outslot, false); ExecCopySlot(outslot, scanslot);
ExecMaterializeSlot(outslot);
xwait = TransactionIdIsValid(snap.xmin) ? xwait = TransactionIdIsValid(snap.xmin) ?
snap.xmin : snap.xmax; snap.xmin : snap.xmax;
...@@ -375,7 +376,8 @@ retry: ...@@ -375,7 +376,8 @@ retry:
} }
} }
heap_endscan(scan); table_endscan(scan);
ExecDropSingleTupleTableSlot(scanslot);
return found; return found;
} }
...@@ -458,11 +460,9 @@ ExecSimpleRelationUpdate(EState *estate, EPQState *epqstate, ...@@ -458,11 +460,9 @@ ExecSimpleRelationUpdate(EState *estate, EPQState *epqstate,
ResultRelInfo *resultRelInfo = estate->es_result_relation_info; ResultRelInfo *resultRelInfo = estate->es_result_relation_info;
Relation rel = resultRelInfo->ri_RelationDesc; Relation rel = resultRelInfo->ri_RelationDesc;
HeapTupleTableSlot *hsearchslot = (HeapTupleTableSlot *)searchslot; HeapTupleTableSlot *hsearchslot = (HeapTupleTableSlot *)searchslot;
HeapTupleTableSlot *hslot = (HeapTupleTableSlot *)slot;
/* We expect both searchslot and the slot to contain a heap tuple. */ /* We expect the searchslot to contain a heap tuple. */
Assert(TTS_IS_HEAPTUPLE(searchslot) || TTS_IS_BUFFERTUPLE(searchslot)); Assert(TTS_IS_HEAPTUPLE(searchslot) || TTS_IS_BUFFERTUPLE(searchslot));
Assert(TTS_IS_HEAPTUPLE(slot) || TTS_IS_BUFFERTUPLE(slot));
/* For now we support only tables. */ /* For now we support only tables. */
Assert(rel->rd_rel->relkind == RELKIND_RELATION); Assert(rel->rd_rel->relkind == RELKIND_RELATION);
...@@ -493,11 +493,11 @@ ExecSimpleRelationUpdate(EState *estate, EPQState *epqstate, ...@@ -493,11 +493,11 @@ ExecSimpleRelationUpdate(EState *estate, EPQState *epqstate,
tuple = ExecFetchSlotHeapTuple(slot, true, NULL); tuple = ExecFetchSlotHeapTuple(slot, true, NULL);
/* OK, update the tuple and index entries for it */ /* OK, update the tuple and index entries for it */
simple_heap_update(rel, &hsearchslot->tuple->t_self, hslot->tuple); simple_heap_update(rel, &hsearchslot->tuple->t_self, tuple);
ItemPointerCopy(&hslot->tuple->t_self, &slot->tts_tid); ItemPointerCopy(&tuple->t_self, &slot->tts_tid);
if (resultRelInfo->ri_NumIndices > 0 && if (resultRelInfo->ri_NumIndices > 0 &&
!HeapTupleIsHeapOnly(hslot->tuple)) !HeapTupleIsHeapOnly(tuple))
recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self), recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self),
estate, false, NULL, estate, false, NULL,
NIL); NIL);
......
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
#include "access/parallel.h" #include "access/parallel.h"
#include "access/relscan.h" #include "access/relscan.h"
#include "access/table.h" #include "access/table.h"
#include "access/tableam.h"
#include "access/transam.h" #include "access/transam.h"
#include "executor/executor.h" #include "executor/executor.h"
#include "jit/jit.h" #include "jit/jit.h"
...@@ -1121,7 +1122,7 @@ ExecGetTriggerOldSlot(EState *estate, ResultRelInfo *relInfo) ...@@ -1121,7 +1122,7 @@ ExecGetTriggerOldSlot(EState *estate, ResultRelInfo *relInfo)
relInfo->ri_TrigOldSlot = relInfo->ri_TrigOldSlot =
ExecInitExtraTupleSlot(estate, ExecInitExtraTupleSlot(estate,
RelationGetDescr(rel), RelationGetDescr(rel),
&TTSOpsBufferHeapTuple); table_slot_callbacks(rel));
MemoryContextSwitchTo(oldcontext); MemoryContextSwitchTo(oldcontext);
} }
...@@ -1143,7 +1144,7 @@ ExecGetTriggerNewSlot(EState *estate, ResultRelInfo *relInfo) ...@@ -1143,7 +1144,7 @@ ExecGetTriggerNewSlot(EState *estate, ResultRelInfo *relInfo)
relInfo->ri_TrigNewSlot = relInfo->ri_TrigNewSlot =
ExecInitExtraTupleSlot(estate, ExecInitExtraTupleSlot(estate,
RelationGetDescr(rel), RelationGetDescr(rel),
&TTSOpsBufferHeapTuple); table_slot_callbacks(rel));
MemoryContextSwitchTo(oldcontext); MemoryContextSwitchTo(oldcontext);
} }
...@@ -1165,7 +1166,7 @@ ExecGetReturningSlot(EState *estate, ResultRelInfo *relInfo) ...@@ -1165,7 +1166,7 @@ ExecGetReturningSlot(EState *estate, ResultRelInfo *relInfo)
relInfo->ri_ReturningSlot = relInfo->ri_ReturningSlot =
ExecInitExtraTupleSlot(estate, ExecInitExtraTupleSlot(estate,
RelationGetDescr(rel), RelationGetDescr(rel),
&TTSOpsBufferHeapTuple); table_slot_callbacks(rel));
MemoryContextSwitchTo(oldcontext); MemoryContextSwitchTo(oldcontext);
} }
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include "access/heapam.h" #include "access/heapam.h"
#include "access/relscan.h" #include "access/relscan.h"
#include "access/tableam.h"
#include "access/transam.h" #include "access/transam.h"
#include "access/visibilitymap.h" #include "access/visibilitymap.h"
#include "executor/execdebug.h" #include "executor/execdebug.h"
...@@ -61,7 +62,7 @@ static inline void BitmapAdjustPrefetchIterator(BitmapHeapScanState *node, ...@@ -61,7 +62,7 @@ static inline void BitmapAdjustPrefetchIterator(BitmapHeapScanState *node,
TBMIterateResult *tbmres); TBMIterateResult *tbmres);
static inline void BitmapAdjustPrefetchTarget(BitmapHeapScanState *node); static inline void BitmapAdjustPrefetchTarget(BitmapHeapScanState *node);
static inline void BitmapPrefetch(BitmapHeapScanState *node, static inline void BitmapPrefetch(BitmapHeapScanState *node,
HeapScanDesc scan); TableScanDesc scan);
static bool BitmapShouldInitializeSharedState( static bool BitmapShouldInitializeSharedState(
ParallelBitmapHeapState *pstate); ParallelBitmapHeapState *pstate);
...@@ -76,7 +77,8 @@ static TupleTableSlot * ...@@ -76,7 +77,8 @@ static TupleTableSlot *
BitmapHeapNext(BitmapHeapScanState *node) BitmapHeapNext(BitmapHeapScanState *node)
{ {
ExprContext *econtext; ExprContext *econtext;
HeapScanDesc scan; TableScanDesc scan;
HeapScanDesc hscan;
TIDBitmap *tbm; TIDBitmap *tbm;
TBMIterator *tbmiterator = NULL; TBMIterator *tbmiterator = NULL;
TBMSharedIterator *shared_tbmiterator = NULL; TBMSharedIterator *shared_tbmiterator = NULL;
...@@ -92,6 +94,7 @@ BitmapHeapNext(BitmapHeapScanState *node) ...@@ -92,6 +94,7 @@ BitmapHeapNext(BitmapHeapScanState *node)
econtext = node->ss.ps.ps_ExprContext; econtext = node->ss.ps.ps_ExprContext;
slot = node->ss.ss_ScanTupleSlot; slot = node->ss.ss_ScanTupleSlot;
scan = node->ss.ss_currentScanDesc; scan = node->ss.ss_currentScanDesc;
hscan = (HeapScanDesc) scan;
tbm = node->tbm; tbm = node->tbm;
if (pstate == NULL) if (pstate == NULL)
tbmiterator = node->tbmiterator; tbmiterator = node->tbmiterator;
...@@ -219,7 +222,7 @@ BitmapHeapNext(BitmapHeapScanState *node) ...@@ -219,7 +222,7 @@ BitmapHeapNext(BitmapHeapScanState *node)
* least AccessShareLock on the table before performing any of the * least AccessShareLock on the table before performing any of the
* indexscans, but let's be safe.) * indexscans, but let's be safe.)
*/ */
if (tbmres->blockno >= scan->rs_nblocks) if (tbmres->blockno >= hscan->rs_nblocks)
{ {
node->tbmres = tbmres = NULL; node->tbmres = tbmres = NULL;
continue; continue;
...@@ -242,14 +245,14 @@ BitmapHeapNext(BitmapHeapScanState *node) ...@@ -242,14 +245,14 @@ BitmapHeapNext(BitmapHeapScanState *node)
* The number of tuples on this page is put into * The number of tuples on this page is put into
* scan->rs_ntuples; note we don't fill scan->rs_vistuples. * scan->rs_ntuples; note we don't fill scan->rs_vistuples.
*/ */
scan->rs_ntuples = tbmres->ntuples; hscan->rs_ntuples = tbmres->ntuples;
} }
else else
{ {
/* /*
* Fetch the current heap page and identify candidate tuples. * Fetch the current heap page and identify candidate tuples.
*/ */
bitgetpage(scan, tbmres); bitgetpage(hscan, tbmres);
} }
if (tbmres->ntuples >= 0) if (tbmres->ntuples >= 0)
...@@ -260,7 +263,7 @@ BitmapHeapNext(BitmapHeapScanState *node) ...@@ -260,7 +263,7 @@ BitmapHeapNext(BitmapHeapScanState *node)
/* /*
* Set rs_cindex to first slot to examine * Set rs_cindex to first slot to examine
*/ */
scan->rs_cindex = 0; hscan->rs_cindex = 0;
/* Adjust the prefetch target */ /* Adjust the prefetch target */
BitmapAdjustPrefetchTarget(node); BitmapAdjustPrefetchTarget(node);
...@@ -270,7 +273,7 @@ BitmapHeapNext(BitmapHeapScanState *node) ...@@ -270,7 +273,7 @@ BitmapHeapNext(BitmapHeapScanState *node)
/* /*
* Continuing in previously obtained page; advance rs_cindex * Continuing in previously obtained page; advance rs_cindex
*/ */
scan->rs_cindex++; hscan->rs_cindex++;
#ifdef USE_PREFETCH #ifdef USE_PREFETCH
...@@ -297,7 +300,7 @@ BitmapHeapNext(BitmapHeapScanState *node) ...@@ -297,7 +300,7 @@ BitmapHeapNext(BitmapHeapScanState *node)
/* /*
* Out of range? If so, nothing more to look at on this page * Out of range? If so, nothing more to look at on this page
*/ */
if (scan->rs_cindex < 0 || scan->rs_cindex >= scan->rs_ntuples) if (hscan->rs_cindex < 0 || hscan->rs_cindex >= hscan->rs_ntuples)
{ {
node->tbmres = tbmres = NULL; node->tbmres = tbmres = NULL;
continue; continue;
...@@ -324,15 +327,15 @@ BitmapHeapNext(BitmapHeapScanState *node) ...@@ -324,15 +327,15 @@ BitmapHeapNext(BitmapHeapScanState *node)
/* /*
* Okay to fetch the tuple. * Okay to fetch the tuple.
*/ */
targoffset = scan->rs_vistuples[scan->rs_cindex]; targoffset = hscan->rs_vistuples[hscan->rs_cindex];
dp = (Page) BufferGetPage(scan->rs_cbuf); dp = (Page) BufferGetPage(hscan->rs_cbuf);
lp = PageGetItemId(dp, targoffset); lp = PageGetItemId(dp, targoffset);
Assert(ItemIdIsNormal(lp)); Assert(ItemIdIsNormal(lp));
scan->rs_ctup.t_data = (HeapTupleHeader) PageGetItem((Page) dp, lp); hscan->rs_ctup.t_data = (HeapTupleHeader) PageGetItem((Page) dp, lp);
scan->rs_ctup.t_len = ItemIdGetLength(lp); hscan->rs_ctup.t_len = ItemIdGetLength(lp);
scan->rs_ctup.t_tableOid = scan->rs_rd->rd_id; hscan->rs_ctup.t_tableOid = scan->rs_rd->rd_id;
ItemPointerSet(&scan->rs_ctup.t_self, tbmres->blockno, targoffset); ItemPointerSet(&hscan->rs_ctup.t_self, tbmres->blockno, targoffset);
pgstat_count_heap_fetch(scan->rs_rd); pgstat_count_heap_fetch(scan->rs_rd);
...@@ -340,9 +343,9 @@ BitmapHeapNext(BitmapHeapScanState *node) ...@@ -340,9 +343,9 @@ BitmapHeapNext(BitmapHeapScanState *node)
* Set up the result slot to point to this tuple. Note that the * Set up the result slot to point to this tuple. Note that the
* slot acquires a pin on the buffer. * slot acquires a pin on the buffer.
*/ */
ExecStoreBufferHeapTuple(&scan->rs_ctup, ExecStoreBufferHeapTuple(&hscan->rs_ctup,
slot, slot,
scan->rs_cbuf); hscan->rs_cbuf);
/* /*
* If we are using lossy info, we have to recheck the qual * If we are using lossy info, we have to recheck the qual
...@@ -392,17 +395,17 @@ bitgetpage(HeapScanDesc scan, TBMIterateResult *tbmres) ...@@ -392,17 +395,17 @@ bitgetpage(HeapScanDesc scan, TBMIterateResult *tbmres)
Assert(page < scan->rs_nblocks); Assert(page < scan->rs_nblocks);
scan->rs_cbuf = ReleaseAndReadBuffer(scan->rs_cbuf, scan->rs_cbuf = ReleaseAndReadBuffer(scan->rs_cbuf,
scan->rs_rd, scan->rs_base.rs_rd,
page); page);
buffer = scan->rs_cbuf; buffer = scan->rs_cbuf;
snapshot = scan->rs_snapshot; snapshot = scan->rs_base.rs_snapshot;
ntup = 0; ntup = 0;
/* /*
* Prune and repair fragmentation for the whole page, if possible. * Prune and repair fragmentation for the whole page, if possible.
*/ */
heap_page_prune_opt(scan->rs_rd, buffer); heap_page_prune_opt(scan->rs_base.rs_rd, buffer);
/* /*
* We must hold share lock on the buffer content while examining tuple * We must hold share lock on the buffer content while examining tuple
...@@ -430,8 +433,8 @@ bitgetpage(HeapScanDesc scan, TBMIterateResult *tbmres) ...@@ -430,8 +433,8 @@ bitgetpage(HeapScanDesc scan, TBMIterateResult *tbmres)
HeapTupleData heapTuple; HeapTupleData heapTuple;
ItemPointerSet(&tid, page, offnum); ItemPointerSet(&tid, page, offnum);
if (heap_hot_search_buffer(&tid, scan->rs_rd, buffer, snapshot, if (heap_hot_search_buffer(&tid, scan->rs_base.rs_rd, buffer,
&heapTuple, NULL, true)) snapshot, &heapTuple, NULL, true))
scan->rs_vistuples[ntup++] = ItemPointerGetOffsetNumber(&tid); scan->rs_vistuples[ntup++] = ItemPointerGetOffsetNumber(&tid);
} }
} }
...@@ -456,16 +459,16 @@ bitgetpage(HeapScanDesc scan, TBMIterateResult *tbmres) ...@@ -456,16 +459,16 @@ bitgetpage(HeapScanDesc scan, TBMIterateResult *tbmres)
continue; continue;
loctup.t_data = (HeapTupleHeader) PageGetItem((Page) dp, lp); loctup.t_data = (HeapTupleHeader) PageGetItem((Page) dp, lp);
loctup.t_len = ItemIdGetLength(lp); loctup.t_len = ItemIdGetLength(lp);
loctup.t_tableOid = scan->rs_rd->rd_id; loctup.t_tableOid = scan->rs_base.rs_rd->rd_id;
ItemPointerSet(&loctup.t_self, page, offnum); ItemPointerSet(&loctup.t_self, page, offnum);
valid = HeapTupleSatisfiesVisibility(&loctup, snapshot, buffer); valid = HeapTupleSatisfiesVisibility(&loctup, snapshot, buffer);
if (valid) if (valid)
{ {
scan->rs_vistuples[ntup++] = offnum; scan->rs_vistuples[ntup++] = offnum;
PredicateLockTuple(scan->rs_rd, &loctup, snapshot); PredicateLockTuple(scan->rs_base.rs_rd, &loctup, snapshot);
} }
CheckForSerializableConflictOut(valid, scan->rs_rd, &loctup, CheckForSerializableConflictOut(valid, scan->rs_base.rs_rd,
buffer, snapshot); &loctup, buffer, snapshot);
} }
} }
...@@ -598,7 +601,7 @@ BitmapAdjustPrefetchTarget(BitmapHeapScanState *node) ...@@ -598,7 +601,7 @@ BitmapAdjustPrefetchTarget(BitmapHeapScanState *node)
* BitmapPrefetch - Prefetch, if prefetch_pages are behind prefetch_target * BitmapPrefetch - Prefetch, if prefetch_pages are behind prefetch_target
*/ */
static inline void static inline void
BitmapPrefetch(BitmapHeapScanState *node, HeapScanDesc scan) BitmapPrefetch(BitmapHeapScanState *node, TableScanDesc scan)
{ {
#ifdef USE_PREFETCH #ifdef USE_PREFETCH
ParallelBitmapHeapState *pstate = node->pstate; ParallelBitmapHeapState *pstate = node->pstate;
...@@ -741,7 +744,7 @@ ExecReScanBitmapHeapScan(BitmapHeapScanState *node) ...@@ -741,7 +744,7 @@ ExecReScanBitmapHeapScan(BitmapHeapScanState *node)
PlanState *outerPlan = outerPlanState(node); PlanState *outerPlan = outerPlanState(node);
/* rescan to release any page pin */ /* rescan to release any page pin */
heap_rescan(node->ss.ss_currentScanDesc, NULL); table_rescan(node->ss.ss_currentScanDesc, NULL);
/* release bitmaps and buffers if any */ /* release bitmaps and buffers if any */
if (node->tbmiterator) if (node->tbmiterator)
...@@ -785,7 +788,7 @@ ExecReScanBitmapHeapScan(BitmapHeapScanState *node) ...@@ -785,7 +788,7 @@ ExecReScanBitmapHeapScan(BitmapHeapScanState *node)
void void
ExecEndBitmapHeapScan(BitmapHeapScanState *node) ExecEndBitmapHeapScan(BitmapHeapScanState *node)
{ {
HeapScanDesc scanDesc; TableScanDesc scanDesc;
/* /*
* extract information from the node * extract information from the node
...@@ -830,7 +833,7 @@ ExecEndBitmapHeapScan(BitmapHeapScanState *node) ...@@ -830,7 +833,7 @@ ExecEndBitmapHeapScan(BitmapHeapScanState *node)
/* /*
* close heap scan * close heap scan
*/ */
heap_endscan(scanDesc); table_endscan(scanDesc);
} }
/* ---------------------------------------------------------------- /* ----------------------------------------------------------------
...@@ -914,8 +917,7 @@ ExecInitBitmapHeapScan(BitmapHeapScan *node, EState *estate, int eflags) ...@@ -914,8 +917,7 @@ ExecInitBitmapHeapScan(BitmapHeapScan *node, EState *estate, int eflags)
*/ */
ExecInitScanTupleSlot(estate, &scanstate->ss, ExecInitScanTupleSlot(estate, &scanstate->ss,
RelationGetDescr(currentRelation), RelationGetDescr(currentRelation),
&TTSOpsBufferHeapTuple); table_slot_callbacks(currentRelation));
/* /*
* Initialize result type and projection. * Initialize result type and projection.
...@@ -953,10 +955,10 @@ ExecInitBitmapHeapScan(BitmapHeapScan *node, EState *estate, int eflags) ...@@ -953,10 +955,10 @@ ExecInitBitmapHeapScan(BitmapHeapScan *node, EState *estate, int eflags)
* Even though we aren't going to do a conventional seqscan, it is useful * Even though we aren't going to do a conventional seqscan, it is useful
* to create a HeapScanDesc --- most of the fields in it are usable. * to create a HeapScanDesc --- most of the fields in it are usable.
*/ */
scanstate->ss.ss_currentScanDesc = heap_beginscan_bm(currentRelation, scanstate->ss.ss_currentScanDesc = table_beginscan_bm(currentRelation,
estate->es_snapshot, estate->es_snapshot,
0, 0,
NULL); NULL);
/* /*
* all done. * all done.
...@@ -1104,5 +1106,5 @@ ExecBitmapHeapInitializeWorker(BitmapHeapScanState *node, ...@@ -1104,5 +1106,5 @@ ExecBitmapHeapInitializeWorker(BitmapHeapScanState *node,
node->pstate = pstate; node->pstate = pstate;
snapshot = RestoreSnapshot(pstate->phs_snapshot_data); snapshot = RestoreSnapshot(pstate->phs_snapshot_data);
heap_update_snapshot(node->ss.ss_currentScanDesc, snapshot); table_scan_update_snapshot(node->ss.ss_currentScanDesc, snapshot);
} }
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include "access/genam.h" #include "access/genam.h"
#include "access/relscan.h" #include "access/relscan.h"
#include "access/tableam.h"
#include "access/tupdesc.h" #include "access/tupdesc.h"
#include "access/visibilitymap.h" #include "access/visibilitymap.h"
#include "executor/execdebug.h" #include "executor/execdebug.h"
...@@ -119,7 +120,7 @@ IndexOnlyNext(IndexOnlyScanState *node) ...@@ -119,7 +120,7 @@ IndexOnlyNext(IndexOnlyScanState *node)
*/ */
while ((tid = index_getnext_tid(scandesc, direction)) != NULL) while ((tid = index_getnext_tid(scandesc, direction)) != NULL)
{ {
HeapTuple tuple = NULL; bool tuple_from_heap = false;
CHECK_FOR_INTERRUPTS(); CHECK_FOR_INTERRUPTS();
...@@ -165,17 +166,18 @@ IndexOnlyNext(IndexOnlyScanState *node) ...@@ -165,17 +166,18 @@ IndexOnlyNext(IndexOnlyScanState *node)
* Rats, we have to visit the heap to check visibility. * Rats, we have to visit the heap to check visibility.
*/ */
InstrCountTuples2(node, 1); InstrCountTuples2(node, 1);
tuple = index_fetch_heap(scandesc); if (!index_fetch_heap(scandesc, slot))
if (tuple == NULL)
continue; /* no visible tuple, try next index entry */ continue; /* no visible tuple, try next index entry */
ExecClearTuple(slot);
/* /*
* Only MVCC snapshots are supported here, so there should be no * Only MVCC snapshots are supported here, so there should be no
* need to keep following the HOT chain once a visible entry has * need to keep following the HOT chain once a visible entry has
* been found. If we did want to allow that, we'd need to keep * been found. If we did want to allow that, we'd need to keep
* more state to remember not to call index_getnext_tid next time. * more state to remember not to call index_getnext_tid next time.
*/ */
if (scandesc->xs_continue_hot) if (scandesc->xs_heap_continue)
elog(ERROR, "non-MVCC snapshots are not supported in index-only scans"); elog(ERROR, "non-MVCC snapshots are not supported in index-only scans");
/* /*
...@@ -184,13 +186,15 @@ IndexOnlyNext(IndexOnlyScanState *node) ...@@ -184,13 +186,15 @@ IndexOnlyNext(IndexOnlyScanState *node)
* but it's not clear whether it's a win to do so. The next index * but it's not clear whether it's a win to do so. The next index
* entry might require a visit to the same heap page. * entry might require a visit to the same heap page.
*/ */
tuple_from_heap = true;
} }
/* /*
* Fill the scan tuple slot with data from the index. This might be * Fill the scan tuple slot with data from the index. This might be
* provided in either HeapTuple or IndexTuple format. Conceivably an * provided in either HeapTuple or IndexTuple format. Conceivably
* index AM might fill both fields, in which case we prefer the heap * an index AM might fill both fields, in which case we prefer the
* format, since it's probably a bit cheaper to fill a slot from. * heap format, since it's probably a bit cheaper to fill a slot from.
*/ */
if (scandesc->xs_hitup) if (scandesc->xs_hitup)
{ {
...@@ -201,7 +205,7 @@ IndexOnlyNext(IndexOnlyScanState *node) ...@@ -201,7 +205,7 @@ IndexOnlyNext(IndexOnlyScanState *node)
*/ */
Assert(slot->tts_tupleDescriptor->natts == Assert(slot->tts_tupleDescriptor->natts ==
scandesc->xs_hitupdesc->natts); scandesc->xs_hitupdesc->natts);
ExecStoreHeapTuple(scandesc->xs_hitup, slot, false); ExecForceStoreHeapTuple(scandesc->xs_hitup, slot);
} }
else if (scandesc->xs_itup) else if (scandesc->xs_itup)
StoreIndexTuple(slot, scandesc->xs_itup, scandesc->xs_itupdesc); StoreIndexTuple(slot, scandesc->xs_itup, scandesc->xs_itupdesc);
...@@ -244,7 +248,7 @@ IndexOnlyNext(IndexOnlyScanState *node) ...@@ -244,7 +248,7 @@ IndexOnlyNext(IndexOnlyScanState *node)
* anyway, then we already have the tuple-level lock and can skip the * anyway, then we already have the tuple-level lock and can skip the
* page lock. * page lock.
*/ */
if (tuple == NULL) if (!tuple_from_heap)
PredicateLockPage(scandesc->heapRelation, PredicateLockPage(scandesc->heapRelation,
ItemPointerGetBlockNumber(tid), ItemPointerGetBlockNumber(tid),
estate->es_snapshot); estate->es_snapshot);
...@@ -523,7 +527,8 @@ ExecInitIndexOnlyScan(IndexOnlyScan *node, EState *estate, int eflags) ...@@ -523,7 +527,8 @@ ExecInitIndexOnlyScan(IndexOnlyScan *node, EState *estate, int eflags)
* suitable data anyway.) * suitable data anyway.)
*/ */
tupDesc = ExecTypeFromTL(node->indextlist); tupDesc = ExecTypeFromTL(node->indextlist);
ExecInitScanTupleSlot(estate, &indexstate->ss, tupDesc, &TTSOpsHeapTuple); ExecInitScanTupleSlot(estate, &indexstate->ss, tupDesc,
table_slot_callbacks(currentRelation));
/* /*
* Initialize result type and projection info. The node's targetlist will * Initialize result type and projection info. The node's targetlist will
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "access/nbtree.h" #include "access/nbtree.h"
#include "access/relscan.h" #include "access/relscan.h"
#include "access/tableam.h"
#include "catalog/pg_am.h" #include "catalog/pg_am.h"
#include "executor/execdebug.h" #include "executor/execdebug.h"
#include "executor/nodeIndexscan.h" #include "executor/nodeIndexscan.h"
...@@ -64,7 +65,7 @@ static int cmp_orderbyvals(const Datum *adist, const bool *anulls, ...@@ -64,7 +65,7 @@ static int cmp_orderbyvals(const Datum *adist, const bool *anulls,
IndexScanState *node); IndexScanState *node);
static int reorderqueue_cmp(const pairingheap_node *a, static int reorderqueue_cmp(const pairingheap_node *a,
const pairingheap_node *b, void *arg); const pairingheap_node *b, void *arg);
static void reorderqueue_push(IndexScanState *node, HeapTuple tuple, static void reorderqueue_push(IndexScanState *node, TupleTableSlot *slot,
Datum *orderbyvals, bool *orderbynulls); Datum *orderbyvals, bool *orderbynulls);
static HeapTuple reorderqueue_pop(IndexScanState *node); static HeapTuple reorderqueue_pop(IndexScanState *node);
...@@ -83,7 +84,6 @@ IndexNext(IndexScanState *node) ...@@ -83,7 +84,6 @@ IndexNext(IndexScanState *node)
ExprContext *econtext; ExprContext *econtext;
ScanDirection direction; ScanDirection direction;
IndexScanDesc scandesc; IndexScanDesc scandesc;
HeapTuple tuple;
TupleTableSlot *slot; TupleTableSlot *slot;
/* /*
...@@ -130,20 +130,10 @@ IndexNext(IndexScanState *node) ...@@ -130,20 +130,10 @@ IndexNext(IndexScanState *node)
/* /*
* ok, now that we have what we need, fetch the next tuple. * ok, now that we have what we need, fetch the next tuple.
*/ */
while ((tuple = index_getnext(scandesc, direction)) != NULL) while (index_getnext_slot(scandesc, direction, slot))
{ {
CHECK_FOR_INTERRUPTS(); CHECK_FOR_INTERRUPTS();
/*
* Store the scanned tuple in the scan tuple slot of the scan state.
* Note: we pass 'false' because tuples returned by amgetnext are
* pointers onto disk pages and must not be pfree()'d.
*/
ExecStoreBufferHeapTuple(tuple, /* tuple to store */
slot, /* slot to store in */
scandesc->xs_cbuf); /* buffer containing
* tuple */
/* /*
* If the index was lossy, we have to recheck the index quals using * If the index was lossy, we have to recheck the index quals using
* the fetched tuple. * the fetched tuple.
...@@ -183,7 +173,6 @@ IndexNextWithReorder(IndexScanState *node) ...@@ -183,7 +173,6 @@ IndexNextWithReorder(IndexScanState *node)
EState *estate; EState *estate;
ExprContext *econtext; ExprContext *econtext;
IndexScanDesc scandesc; IndexScanDesc scandesc;
HeapTuple tuple;
TupleTableSlot *slot; TupleTableSlot *slot;
ReorderTuple *topmost = NULL; ReorderTuple *topmost = NULL;
bool was_exact; bool was_exact;
...@@ -252,6 +241,8 @@ IndexNextWithReorder(IndexScanState *node) ...@@ -252,6 +241,8 @@ IndexNextWithReorder(IndexScanState *node)
scandesc->xs_orderbynulls, scandesc->xs_orderbynulls,
node) <= 0) node) <= 0)
{ {
HeapTuple tuple;
tuple = reorderqueue_pop(node); tuple = reorderqueue_pop(node);
/* Pass 'true', as the tuple in the queue is a palloc'd copy */ /* Pass 'true', as the tuple in the queue is a palloc'd copy */
...@@ -271,8 +262,7 @@ IndexNextWithReorder(IndexScanState *node) ...@@ -271,8 +262,7 @@ IndexNextWithReorder(IndexScanState *node)
*/ */
next_indextuple: next_indextuple:
slot = node->ss.ss_ScanTupleSlot; slot = node->ss.ss_ScanTupleSlot;
tuple = index_getnext(scandesc, ForwardScanDirection); if (!index_getnext_slot(scandesc, ForwardScanDirection, slot))
if (!tuple)
{ {
/* /*
* No more tuples from the index. But we still need to drain any * No more tuples from the index. But we still need to drain any
...@@ -282,14 +272,6 @@ next_indextuple: ...@@ -282,14 +272,6 @@ next_indextuple:
continue; continue;
} }
/*
* Store the scanned tuple in the scan tuple slot of the scan state.
*/
ExecStoreBufferHeapTuple(tuple, /* tuple to store */
slot, /* slot to store in */
scandesc->xs_cbuf); /* buffer containing
* tuple */
/* /*
* If the index was lossy, we have to recheck the index quals and * If the index was lossy, we have to recheck the index quals and
* ORDER BY expressions using the fetched tuple. * ORDER BY expressions using the fetched tuple.
...@@ -358,7 +340,7 @@ next_indextuple: ...@@ -358,7 +340,7 @@ next_indextuple:
node) > 0)) node) > 0))
{ {
/* Put this tuple to the queue */ /* Put this tuple to the queue */
reorderqueue_push(node, tuple, lastfetched_vals, lastfetched_nulls); reorderqueue_push(node, slot, lastfetched_vals, lastfetched_nulls);
continue; continue;
} }
else else
...@@ -478,7 +460,7 @@ reorderqueue_cmp(const pairingheap_node *a, const pairingheap_node *b, ...@@ -478,7 +460,7 @@ reorderqueue_cmp(const pairingheap_node *a, const pairingheap_node *b,
* Helper function to push a tuple to the reorder queue. * Helper function to push a tuple to the reorder queue.
*/ */
static void static void
reorderqueue_push(IndexScanState *node, HeapTuple tuple, reorderqueue_push(IndexScanState *node, TupleTableSlot *slot,
Datum *orderbyvals, bool *orderbynulls) Datum *orderbyvals, bool *orderbynulls)
{ {
IndexScanDesc scandesc = node->iss_ScanDesc; IndexScanDesc scandesc = node->iss_ScanDesc;
...@@ -488,7 +470,7 @@ reorderqueue_push(IndexScanState *node, HeapTuple tuple, ...@@ -488,7 +470,7 @@ reorderqueue_push(IndexScanState *node, HeapTuple tuple,
int i; int i;
rt = (ReorderTuple *) palloc(sizeof(ReorderTuple)); rt = (ReorderTuple *) palloc(sizeof(ReorderTuple));
rt->htup = heap_copytuple(tuple); rt->htup = ExecCopySlotHeapTuple(slot);
rt->orderbyvals = rt->orderbyvals =
(Datum *) palloc(sizeof(Datum) * scandesc->numberOfOrderBys); (Datum *) palloc(sizeof(Datum) * scandesc->numberOfOrderBys);
rt->orderbynulls = rt->orderbynulls =
...@@ -949,7 +931,7 @@ ExecInitIndexScan(IndexScan *node, EState *estate, int eflags) ...@@ -949,7 +931,7 @@ ExecInitIndexScan(IndexScan *node, EState *estate, int eflags)
*/ */
ExecInitScanTupleSlot(estate, &indexstate->ss, ExecInitScanTupleSlot(estate, &indexstate->ss,
RelationGetDescr(currentRelation), RelationGetDescr(currentRelation),
&TTSOpsBufferHeapTuple); table_slot_callbacks(currentRelation));
/* /*
* Initialize result type and projection. * Initialize result type and projection.
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include "access/heapam.h" #include "access/heapam.h"
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/tableam.h"
#include "access/xact.h" #include "access/xact.h"
#include "catalog/catalog.h" #include "catalog/catalog.h"
#include "commands/trigger.h" #include "commands/trigger.h"
...@@ -2147,7 +2148,7 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags) ...@@ -2147,7 +2148,7 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
mtstate->mt_plans[i] = ExecInitNode(subplan, estate, eflags); mtstate->mt_plans[i] = ExecInitNode(subplan, estate, eflags);
mtstate->mt_scans[i] = mtstate->mt_scans[i] =
ExecInitExtraTupleSlot(mtstate->ps.state, ExecGetResultType(mtstate->mt_plans[i]), ExecInitExtraTupleSlot(mtstate->ps.state, ExecGetResultType(mtstate->mt_plans[i]),
&TTSOpsHeapTuple); table_slot_callbacks(resultRelInfo->ri_RelationDesc));
/* Also let FDWs init themselves for foreign-table result rels */ /* Also let FDWs init themselves for foreign-table result rels */
if (!resultRelInfo->ri_usesFdwDirectModify && if (!resultRelInfo->ri_usesFdwDirectModify &&
...@@ -2207,8 +2208,7 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags) ...@@ -2207,8 +2208,7 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
if (update_tuple_routing_needed) if (update_tuple_routing_needed)
{ {
ExecSetupChildParentMapForSubplan(mtstate); ExecSetupChildParentMapForSubplan(mtstate);
mtstate->mt_root_tuple_slot = MakeTupleTableSlot(RelationGetDescr(rel), mtstate->mt_root_tuple_slot = table_slot_create(rel, NULL);
&TTSOpsHeapTuple);
} }
/* /*
...@@ -2320,8 +2320,8 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags) ...@@ -2320,8 +2320,8 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
/* initialize slot for the existing tuple */ /* initialize slot for the existing tuple */
resultRelInfo->ri_onConflict->oc_Existing = resultRelInfo->ri_onConflict->oc_Existing =
ExecInitExtraTupleSlot(mtstate->ps.state, relationDesc, table_slot_create(resultRelInfo->ri_RelationDesc,
&TTSOpsBufferHeapTuple); &mtstate->ps.state->es_tupleTable);
/* create the tuple slot for the UPDATE SET projection */ /* create the tuple slot for the UPDATE SET projection */
tupDesc = ExecTypeFromTL((List *) node->onConflictSet); tupDesc = ExecTypeFromTL((List *) node->onConflictSet);
...@@ -2430,15 +2430,18 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags) ...@@ -2430,15 +2430,18 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
for (i = 0; i < nplans; i++) for (i = 0; i < nplans; i++)
{ {
JunkFilter *j; JunkFilter *j;
TupleTableSlot *junkresslot;
subplan = mtstate->mt_plans[i]->plan; subplan = mtstate->mt_plans[i]->plan;
if (operation == CMD_INSERT || operation == CMD_UPDATE) if (operation == CMD_INSERT || operation == CMD_UPDATE)
ExecCheckPlanOutput(resultRelInfo->ri_RelationDesc, ExecCheckPlanOutput(resultRelInfo->ri_RelationDesc,
subplan->targetlist); subplan->targetlist);
junkresslot =
ExecInitExtraTupleSlot(estate, NULL,
table_slot_callbacks(resultRelInfo->ri_RelationDesc));
j = ExecInitJunkFilter(subplan->targetlist, j = ExecInitJunkFilter(subplan->targetlist,
ExecInitExtraTupleSlot(estate, NULL, junkresslot);
&TTSOpsHeapTuple));
if (operation == CMD_UPDATE || operation == CMD_DELETE) if (operation == CMD_UPDATE || operation == CMD_DELETE)
{ {
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "access/heapam.h" #include "access/heapam.h"
#include "access/relscan.h" #include "access/relscan.h"
#include "access/tableam.h"
#include "access/tsmapi.h" #include "access/tsmapi.h"
#include "executor/executor.h" #include "executor/executor.h"
#include "executor/nodeSamplescan.h" #include "executor/nodeSamplescan.h"
...@@ -48,6 +49,7 @@ SampleNext(SampleScanState *node) ...@@ -48,6 +49,7 @@ SampleNext(SampleScanState *node)
{ {
HeapTuple tuple; HeapTuple tuple;
TupleTableSlot *slot; TupleTableSlot *slot;
HeapScanDesc hscan;
/* /*
* if this is first call within a scan, initialize * if this is first call within a scan, initialize
...@@ -61,11 +63,12 @@ SampleNext(SampleScanState *node) ...@@ -61,11 +63,12 @@ SampleNext(SampleScanState *node)
tuple = tablesample_getnext(node); tuple = tablesample_getnext(node);
slot = node->ss.ss_ScanTupleSlot; slot = node->ss.ss_ScanTupleSlot;
hscan = (HeapScanDesc) node->ss.ss_currentScanDesc;
if (tuple) if (tuple)
ExecStoreBufferHeapTuple(tuple, /* tuple to store */ ExecStoreBufferHeapTuple(tuple, /* tuple to store */
slot, /* slot to store in */ slot, /* slot to store in */
node->ss.ss_currentScanDesc->rs_cbuf); /* tuple's buffer */ hscan->rs_cbuf); /* tuple's buffer */
else else
ExecClearTuple(slot); ExecClearTuple(slot);
...@@ -147,7 +150,7 @@ ExecInitSampleScan(SampleScan *node, EState *estate, int eflags) ...@@ -147,7 +150,7 @@ ExecInitSampleScan(SampleScan *node, EState *estate, int eflags)
/* and create slot with appropriate rowtype */ /* and create slot with appropriate rowtype */
ExecInitScanTupleSlot(estate, &scanstate->ss, ExecInitScanTupleSlot(estate, &scanstate->ss,
RelationGetDescr(scanstate->ss.ss_currentRelation), RelationGetDescr(scanstate->ss.ss_currentRelation),
&TTSOpsBufferHeapTuple); table_slot_callbacks(scanstate->ss.ss_currentRelation));
/* /*
* Initialize result type and projection. * Initialize result type and projection.
...@@ -219,7 +222,7 @@ ExecEndSampleScan(SampleScanState *node) ...@@ -219,7 +222,7 @@ ExecEndSampleScan(SampleScanState *node)
* close heap scan * close heap scan
*/ */
if (node->ss.ss_currentScanDesc) if (node->ss.ss_currentScanDesc)
heap_endscan(node->ss.ss_currentScanDesc); table_endscan(node->ss.ss_currentScanDesc);
} }
/* ---------------------------------------------------------------- /* ----------------------------------------------------------------
...@@ -319,19 +322,19 @@ tablesample_init(SampleScanState *scanstate) ...@@ -319,19 +322,19 @@ tablesample_init(SampleScanState *scanstate)
if (scanstate->ss.ss_currentScanDesc == NULL) if (scanstate->ss.ss_currentScanDesc == NULL)
{ {
scanstate->ss.ss_currentScanDesc = scanstate->ss.ss_currentScanDesc =
heap_beginscan_sampling(scanstate->ss.ss_currentRelation, table_beginscan_sampling(scanstate->ss.ss_currentRelation,
scanstate->ss.ps.state->es_snapshot, scanstate->ss.ps.state->es_snapshot,
0, NULL, 0, NULL,
scanstate->use_bulkread, scanstate->use_bulkread,
allow_sync, allow_sync,
scanstate->use_pagemode); scanstate->use_pagemode);
} }
else else
{ {
heap_rescan_set_params(scanstate->ss.ss_currentScanDesc, NULL, table_rescan_set_params(scanstate->ss.ss_currentScanDesc, NULL,
scanstate->use_bulkread, scanstate->use_bulkread,
allow_sync, allow_sync,
scanstate->use_pagemode); scanstate->use_pagemode);
} }
pfree(params); pfree(params);
...@@ -350,8 +353,9 @@ static HeapTuple ...@@ -350,8 +353,9 @@ static HeapTuple
tablesample_getnext(SampleScanState *scanstate) tablesample_getnext(SampleScanState *scanstate)
{ {
TsmRoutine *tsm = scanstate->tsmroutine; TsmRoutine *tsm = scanstate->tsmroutine;
HeapScanDesc scan = scanstate->ss.ss_currentScanDesc; TableScanDesc scan = scanstate->ss.ss_currentScanDesc;
HeapTuple tuple = &(scan->rs_ctup); HeapScanDesc hscan = (HeapScanDesc) scan;
HeapTuple tuple = &(hscan->rs_ctup);
Snapshot snapshot = scan->rs_snapshot; Snapshot snapshot = scan->rs_snapshot;
bool pagemode = scan->rs_pageatatime; bool pagemode = scan->rs_pageatatime;
BlockNumber blockno; BlockNumber blockno;
...@@ -359,14 +363,14 @@ tablesample_getnext(SampleScanState *scanstate) ...@@ -359,14 +363,14 @@ tablesample_getnext(SampleScanState *scanstate)
bool all_visible; bool all_visible;
OffsetNumber maxoffset; OffsetNumber maxoffset;
if (!scan->rs_inited) if (!hscan->rs_inited)
{ {
/* /*
* return null immediately if relation is empty * return null immediately if relation is empty
*/ */
if (scan->rs_nblocks == 0) if (hscan->rs_nblocks == 0)
{ {
Assert(!BufferIsValid(scan->rs_cbuf)); Assert(!BufferIsValid(hscan->rs_cbuf));
tuple->t_data = NULL; tuple->t_data = NULL;
return NULL; return NULL;
} }
...@@ -380,15 +384,15 @@ tablesample_getnext(SampleScanState *scanstate) ...@@ -380,15 +384,15 @@ tablesample_getnext(SampleScanState *scanstate)
} }
} }
else else
blockno = scan->rs_startblock; blockno = hscan->rs_startblock;
Assert(blockno < scan->rs_nblocks); Assert(blockno < hscan->rs_nblocks);
heapgetpage(scan, blockno); heapgetpage(scan, blockno);
scan->rs_inited = true; hscan->rs_inited = true;
} }
else else
{ {
/* continue from previously returned page/tuple */ /* continue from previously returned page/tuple */
blockno = scan->rs_cblock; /* current page */ blockno = hscan->rs_cblock; /* current page */
} }
/* /*
...@@ -396,9 +400,9 @@ tablesample_getnext(SampleScanState *scanstate) ...@@ -396,9 +400,9 @@ tablesample_getnext(SampleScanState *scanstate)
* visibility checks. * visibility checks.
*/ */
if (!pagemode) if (!pagemode)
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE); LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
page = (Page) BufferGetPage(scan->rs_cbuf); page = (Page) BufferGetPage(hscan->rs_cbuf);
all_visible = PageIsAllVisible(page) && !snapshot->takenDuringRecovery; all_visible = PageIsAllVisible(page) && !snapshot->takenDuringRecovery;
maxoffset = PageGetMaxOffsetNumber(page); maxoffset = PageGetMaxOffsetNumber(page);
...@@ -431,18 +435,18 @@ tablesample_getnext(SampleScanState *scanstate) ...@@ -431,18 +435,18 @@ tablesample_getnext(SampleScanState *scanstate)
if (all_visible) if (all_visible)
visible = true; visible = true;
else else
visible = SampleTupleVisible(tuple, tupoffset, scan); visible = SampleTupleVisible(tuple, tupoffset, hscan);
/* in pagemode, heapgetpage did this for us */ /* in pagemode, heapgetpage did this for us */
if (!pagemode) if (!pagemode)
CheckForSerializableConflictOut(visible, scan->rs_rd, tuple, CheckForSerializableConflictOut(visible, scan->rs_rd, tuple,
scan->rs_cbuf, snapshot); hscan->rs_cbuf, snapshot);
if (visible) if (visible)
{ {
/* Found visible tuple, return it. */ /* Found visible tuple, return it. */
if (!pagemode) if (!pagemode)
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK); LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
break; break;
} }
else else
...@@ -457,7 +461,7 @@ tablesample_getnext(SampleScanState *scanstate) ...@@ -457,7 +461,7 @@ tablesample_getnext(SampleScanState *scanstate)
* it's time to move to the next. * it's time to move to the next.
*/ */
if (!pagemode) if (!pagemode)
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK); LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
if (tsm->NextSampleBlock) if (tsm->NextSampleBlock)
{ {
...@@ -469,7 +473,7 @@ tablesample_getnext(SampleScanState *scanstate) ...@@ -469,7 +473,7 @@ tablesample_getnext(SampleScanState *scanstate)
{ {
/* Without NextSampleBlock, just do a plain forward seqscan. */ /* Without NextSampleBlock, just do a plain forward seqscan. */
blockno++; blockno++;
if (blockno >= scan->rs_nblocks) if (blockno >= hscan->rs_nblocks)
blockno = 0; blockno = 0;
/* /*
...@@ -485,7 +489,7 @@ tablesample_getnext(SampleScanState *scanstate) ...@@ -485,7 +489,7 @@ tablesample_getnext(SampleScanState *scanstate)
if (scan->rs_syncscan) if (scan->rs_syncscan)
ss_report_location(scan->rs_rd, blockno); ss_report_location(scan->rs_rd, blockno);
finished = (blockno == scan->rs_startblock); finished = (blockno == hscan->rs_startblock);
} }
/* /*
...@@ -493,23 +497,23 @@ tablesample_getnext(SampleScanState *scanstate) ...@@ -493,23 +497,23 @@ tablesample_getnext(SampleScanState *scanstate)
*/ */
if (finished) if (finished)
{ {
if (BufferIsValid(scan->rs_cbuf)) if (BufferIsValid(hscan->rs_cbuf))
ReleaseBuffer(scan->rs_cbuf); ReleaseBuffer(hscan->rs_cbuf);
scan->rs_cbuf = InvalidBuffer; hscan->rs_cbuf = InvalidBuffer;
scan->rs_cblock = InvalidBlockNumber; hscan->rs_cblock = InvalidBlockNumber;
tuple->t_data = NULL; tuple->t_data = NULL;
scan->rs_inited = false; hscan->rs_inited = false;
return NULL; return NULL;
} }
Assert(blockno < scan->rs_nblocks); Assert(blockno < hscan->rs_nblocks);
heapgetpage(scan, blockno); heapgetpage(scan, blockno);
/* Re-establish state for new page */ /* Re-establish state for new page */
if (!pagemode) if (!pagemode)
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE); LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
page = (Page) BufferGetPage(scan->rs_cbuf); page = (Page) BufferGetPage(hscan->rs_cbuf);
all_visible = PageIsAllVisible(page) && !snapshot->takenDuringRecovery; all_visible = PageIsAllVisible(page) && !snapshot->takenDuringRecovery;
maxoffset = PageGetMaxOffsetNumber(page); maxoffset = PageGetMaxOffsetNumber(page);
} }
...@@ -517,7 +521,7 @@ tablesample_getnext(SampleScanState *scanstate) ...@@ -517,7 +521,7 @@ tablesample_getnext(SampleScanState *scanstate)
/* Count successfully-fetched tuples as heap fetches */ /* Count successfully-fetched tuples as heap fetches */
pgstat_count_heap_getnext(scan->rs_rd); pgstat_count_heap_getnext(scan->rs_rd);
return &(scan->rs_ctup); return &(hscan->rs_ctup);
} }
/* /*
...@@ -526,7 +530,7 @@ tablesample_getnext(SampleScanState *scanstate) ...@@ -526,7 +530,7 @@ tablesample_getnext(SampleScanState *scanstate)
static bool static bool
SampleTupleVisible(HeapTuple tuple, OffsetNumber tupoffset, HeapScanDesc scan) SampleTupleVisible(HeapTuple tuple, OffsetNumber tupoffset, HeapScanDesc scan)
{ {
if (scan->rs_pageatatime) if (scan->rs_base.rs_pageatatime)
{ {
/* /*
* In pageatatime mode, heapgetpage() already did visibility checks, * In pageatatime mode, heapgetpage() already did visibility checks,
...@@ -559,7 +563,7 @@ SampleTupleVisible(HeapTuple tuple, OffsetNumber tupoffset, HeapScanDesc scan) ...@@ -559,7 +563,7 @@ SampleTupleVisible(HeapTuple tuple, OffsetNumber tupoffset, HeapScanDesc scan)
{ {
/* Otherwise, we have to check the tuple individually. */ /* Otherwise, we have to check the tuple individually. */
return HeapTupleSatisfiesVisibility(tuple, return HeapTupleSatisfiesVisibility(tuple,
scan->rs_snapshot, scan->rs_base.rs_snapshot,
scan->rs_cbuf); scan->rs_cbuf);
} }
} }
This diff is collapsed.
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "access/heapam.h" #include "access/heapam.h"
#include "access/sysattr.h" #include "access/sysattr.h"
#include "access/tableam.h"
#include "catalog/pg_type.h" #include "catalog/pg_type.h"
#include "executor/execdebug.h" #include "executor/execdebug.h"
#include "executor/nodeTidscan.h" #include "executor/nodeTidscan.h"
...@@ -538,7 +539,7 @@ ExecInitTidScan(TidScan *node, EState *estate, int eflags) ...@@ -538,7 +539,7 @@ ExecInitTidScan(TidScan *node, EState *estate, int eflags)
*/ */
ExecInitScanTupleSlot(estate, &tidstate->ss, ExecInitScanTupleSlot(estate, &tidstate->ss,
RelationGetDescr(currentRelation), RelationGetDescr(currentRelation),
&TTSOpsBufferHeapTuple); table_slot_callbacks(currentRelation));
/* /*
* Initialize result type and projection. * Initialize result type and projection.
......
This diff is collapsed.
This diff is collapsed.
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include "access/heapam.h" #include "access/heapam.h"
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/tableam.h"
#include "access/transam.h" #include "access/transam.h"
#include "access/twophase_rmgr.h" #include "access/twophase_rmgr.h"
#include "access/xact.h" #include "access/xact.h"
...@@ -1206,7 +1207,7 @@ pgstat_collect_oids(Oid catalogid, AttrNumber anum_oid) ...@@ -1206,7 +1207,7 @@ pgstat_collect_oids(Oid catalogid, AttrNumber anum_oid)
HTAB *htab; HTAB *htab;
HASHCTL hash_ctl; HASHCTL hash_ctl;
Relation rel; Relation rel;
HeapScanDesc scan; TableScanDesc scan;
HeapTuple tup; HeapTuple tup;
Snapshot snapshot; Snapshot snapshot;
...@@ -1221,7 +1222,7 @@ pgstat_collect_oids(Oid catalogid, AttrNumber anum_oid) ...@@ -1221,7 +1222,7 @@ pgstat_collect_oids(Oid catalogid, AttrNumber anum_oid)
rel = table_open(catalogid, AccessShareLock); rel = table_open(catalogid, AccessShareLock);
snapshot = RegisterSnapshot(GetLatestSnapshot()); snapshot = RegisterSnapshot(GetLatestSnapshot());
scan = heap_beginscan(rel, snapshot, 0, NULL); scan = table_beginscan(rel, snapshot, 0, NULL);
while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
{ {
Oid thisoid; Oid thisoid;
...@@ -1234,7 +1235,7 @@ pgstat_collect_oids(Oid catalogid, AttrNumber anum_oid) ...@@ -1234,7 +1235,7 @@ pgstat_collect_oids(Oid catalogid, AttrNumber anum_oid)
(void) hash_search(htab, (void *) &thisoid, HASH_ENTER, NULL); (void) hash_search(htab, (void *) &thisoid, HASH_ENTER, NULL);
} }
heap_endscan(scan); table_endscan(scan);
UnregisterSnapshot(snapshot); UnregisterSnapshot(snapshot);
table_close(rel, AccessShareLock); table_close(rel, AccessShareLock);
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment