1999-11-23 21:07:06 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* nodeTidscan.c
|
|
|
|
* Routines to support direct tid scans of relations
|
|
|
|
*
|
2011-01-01 19:18:15 +01:00
|
|
|
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1999-11-23 21:07:06 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/executor/nodeTidscan.c
|
1999-11-23 21:07:06 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* INTERFACE ROUTINES
|
|
|
|
*
|
2002-11-30 06:21:03 +01:00
|
|
|
* ExecTidScan scans a relation using tids
|
1999-11-23 21:07:06 +01:00
|
|
|
* ExecInitTidScan creates and initializes state info.
|
2010-07-12 19:01:06 +02:00
|
|
|
* ExecReScanTidScan rescans the tid relation.
|
1999-11-23 21:07:06 +01:00
|
|
|
* ExecEndTidScan releases all storage.
|
|
|
|
* ExecTidMarkPos marks scan position.
|
2002-11-30 06:21:03 +01:00
|
|
|
* ExecTidRestrPos restores scan position.
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2005-11-26 23:14:57 +01:00
|
|
|
#include "access/heapam.h"
|
2008-05-12 02:00:54 +02:00
|
|
|
#include "access/sysattr.h"
|
2005-11-26 23:14:57 +01:00
|
|
|
#include "catalog/pg_type.h"
|
1999-11-23 21:07:06 +01:00
|
|
|
#include "executor/execdebug.h"
|
|
|
|
#include "executor/nodeTidscan.h"
|
2005-11-26 23:14:57 +01:00
|
|
|
#include "optimizer/clauses.h"
|
2008-05-12 02:00:54 +02:00
|
|
|
#include "storage/bufmgr.h"
|
2005-11-26 23:14:57 +01:00
|
|
|
#include "utils/array.h"
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2003-09-26 03:17:01 +02:00
|
|
|
|
2005-11-26 23:14:57 +01:00
|
|
|
#define IsCTIDVar(node) \
|
|
|
|
((node) != NULL && \
|
|
|
|
IsA((node), Var) && \
|
|
|
|
((Var *) (node))->varattno == SelfItemPointerAttributeNumber && \
|
|
|
|
((Var *) (node))->varlevelsup == 0)
|
|
|
|
|
2003-09-26 03:17:01 +02:00
|
|
|
static void TidListCreate(TidScanState *tidstate);
|
2005-11-26 23:14:57 +01:00
|
|
|
static int itemptr_comparator(const void *a, const void *b);
|
2002-12-05 16:50:39 +01:00
|
|
|
static TupleTableSlot *TidNext(TidScanState *node);
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2003-09-26 03:17:01 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the list of TIDs to be visited, by evaluating the expressions
|
|
|
|
* for them.
|
2005-11-26 23:14:57 +01:00
|
|
|
*
|
|
|
|
* (The result is actually an array, not a list.)
|
2003-09-26 03:17:01 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
TidListCreate(TidScanState *tidstate)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2005-11-26 23:14:57 +01:00
|
|
|
List *evalList = tidstate->tss_tidquals;
|
2003-09-26 03:17:01 +02:00
|
|
|
ExprContext *econtext = tidstate->ss.ps.ps_ExprContext;
|
2008-05-01 01:28:32 +02:00
|
|
|
BlockNumber nblocks;
|
2003-09-26 03:17:01 +02:00
|
|
|
ItemPointerData *tidList;
|
2005-11-26 23:14:57 +01:00
|
|
|
int numAllocTids;
|
|
|
|
int numTids;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *l;
|
2003-09-26 03:17:01 +02:00
|
|
|
|
2008-05-01 01:28:32 +02:00
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* We silently discard any TIDs that are out of range at the time of scan
|
|
|
|
* start. (Since we hold at least AccessShareLock on the table, it won't
|
|
|
|
* be possible for someone to truncate away the blocks we intend to
|
|
|
|
* visit.)
|
2008-05-01 01:28:32 +02:00
|
|
|
*/
|
|
|
|
nblocks = RelationGetNumberOfBlocks(tidstate->ss.ss_currentRelation);
|
|
|
|
|
2005-11-26 23:14:57 +01:00
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* We initialize the array with enough slots for the case that all quals
|
2007-06-11 03:16:30 +02:00
|
|
|
* are simple OpExprs or CurrentOfExprs. If there are any
|
|
|
|
* ScalarArrayOpExprs, we may have to enlarge the array.
|
2005-11-26 23:14:57 +01:00
|
|
|
*/
|
|
|
|
numAllocTids = list_length(evalList);
|
2003-09-26 03:17:01 +02:00
|
|
|
tidList = (ItemPointerData *)
|
2005-11-26 23:14:57 +01:00
|
|
|
palloc(numAllocTids * sizeof(ItemPointerData));
|
|
|
|
numTids = 0;
|
2007-10-24 20:37:09 +02:00
|
|
|
tidstate->tss_isCurrentOf = false;
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2004-05-26 06:41:50 +02:00
|
|
|
foreach(l, evalList)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2005-11-26 23:14:57 +01:00
|
|
|
ExprState *exstate = (ExprState *) lfirst(l);
|
|
|
|
Expr *expr = exstate->expr;
|
2003-09-26 03:17:01 +02:00
|
|
|
ItemPointer itemptr;
|
|
|
|
bool isNull;
|
|
|
|
|
2005-11-26 23:14:57 +01:00
|
|
|
if (is_opclause(expr))
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
FuncExprState *fexstate = (FuncExprState *) exstate;
|
|
|
|
Node *arg1;
|
|
|
|
Node *arg2;
|
2005-11-26 23:14:57 +01:00
|
|
|
|
|
|
|
arg1 = get_leftop(expr);
|
|
|
|
arg2 = get_rightop(expr);
|
|
|
|
if (IsCTIDVar(arg1))
|
|
|
|
exstate = (ExprState *) lsecond(fexstate->args);
|
|
|
|
else if (IsCTIDVar(arg2))
|
|
|
|
exstate = (ExprState *) linitial(fexstate->args);
|
|
|
|
else
|
|
|
|
elog(ERROR, "could not identify CTID variable");
|
|
|
|
|
|
|
|
itemptr = (ItemPointer)
|
|
|
|
DatumGetPointer(ExecEvalExprSwitchContext(exstate,
|
|
|
|
econtext,
|
|
|
|
&isNull,
|
|
|
|
NULL));
|
2008-05-01 01:28:32 +02:00
|
|
|
if (!isNull &&
|
|
|
|
ItemPointerIsValid(itemptr) &&
|
|
|
|
ItemPointerGetBlockNumber(itemptr) < nblocks)
|
2005-11-26 23:14:57 +01:00
|
|
|
{
|
|
|
|
if (numTids >= numAllocTids)
|
|
|
|
{
|
|
|
|
numAllocTids *= 2;
|
|
|
|
tidList = (ItemPointerData *)
|
|
|
|
repalloc(tidList,
|
|
|
|
numAllocTids * sizeof(ItemPointerData));
|
|
|
|
}
|
|
|
|
tidList[numTids++] = *itemptr;
|
|
|
|
}
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
2005-11-26 23:14:57 +01:00
|
|
|
else if (expr && IsA(expr, ScalarArrayOpExpr))
|
|
|
|
{
|
|
|
|
ScalarArrayOpExprState *saexstate = (ScalarArrayOpExprState *) exstate;
|
|
|
|
Datum arraydatum;
|
|
|
|
ArrayType *itemarray;
|
|
|
|
Datum *ipdatums;
|
|
|
|
bool *ipnulls;
|
|
|
|
int ndatums;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
exstate = (ExprState *) lsecond(saexstate->fxprstate.args);
|
|
|
|
arraydatum = ExecEvalExprSwitchContext(exstate,
|
|
|
|
econtext,
|
|
|
|
&isNull,
|
|
|
|
NULL);
|
|
|
|
if (isNull)
|
|
|
|
continue;
|
|
|
|
itemarray = DatumGetArrayTypeP(arraydatum);
|
|
|
|
deconstruct_array(itemarray,
|
|
|
|
TIDOID, SizeOfIptrData, false, 's',
|
|
|
|
&ipdatums, &ipnulls, &ndatums);
|
|
|
|
if (numTids + ndatums > numAllocTids)
|
|
|
|
{
|
|
|
|
numAllocTids = numTids + ndatums;
|
|
|
|
tidList = (ItemPointerData *)
|
|
|
|
repalloc(tidList,
|
|
|
|
numAllocTids * sizeof(ItemPointerData));
|
|
|
|
}
|
|
|
|
for (i = 0; i < ndatums; i++)
|
|
|
|
{
|
|
|
|
if (!ipnulls[i])
|
|
|
|
{
|
|
|
|
itemptr = (ItemPointer) DatumGetPointer(ipdatums[i]);
|
2008-05-01 01:28:32 +02:00
|
|
|
if (ItemPointerIsValid(itemptr) &&
|
|
|
|
ItemPointerGetBlockNumber(itemptr) < nblocks)
|
2005-11-26 23:14:57 +01:00
|
|
|
tidList[numTids++] = *itemptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pfree(ipdatums);
|
|
|
|
pfree(ipnulls);
|
|
|
|
}
|
2007-06-11 03:16:30 +02:00
|
|
|
else if (expr && IsA(expr, CurrentOfExpr))
|
|
|
|
{
|
|
|
|
CurrentOfExpr *cexpr = (CurrentOfExpr *) expr;
|
|
|
|
ItemPointerData cursor_tid;
|
|
|
|
|
2007-06-12 00:22:42 +02:00
|
|
|
if (execCurrentOf(cexpr, econtext,
|
2007-11-15 22:14:46 +01:00
|
|
|
RelationGetRelid(tidstate->ss.ss_currentRelation),
|
2007-06-11 03:16:30 +02:00
|
|
|
&cursor_tid))
|
|
|
|
{
|
|
|
|
if (numTids >= numAllocTids)
|
|
|
|
{
|
|
|
|
numAllocTids *= 2;
|
|
|
|
tidList = (ItemPointerData *)
|
|
|
|
repalloc(tidList,
|
|
|
|
numAllocTids * sizeof(ItemPointerData));
|
|
|
|
}
|
|
|
|
tidList[numTids++] = cursor_tid;
|
2007-10-24 20:37:09 +02:00
|
|
|
tidstate->tss_isCurrentOf = true;
|
2007-06-11 03:16:30 +02:00
|
|
|
}
|
|
|
|
}
|
2005-11-26 23:14:57 +01:00
|
|
|
else
|
|
|
|
elog(ERROR, "could not identify CTID expression");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sort the array of TIDs into order, and eliminate duplicates.
|
2006-10-04 02:30:14 +02:00
|
|
|
* Eliminating duplicates is necessary since we want OR semantics across
|
|
|
|
* the list. Sorting makes it easier to detect duplicates, and as a bonus
|
|
|
|
* ensures that we will visit the heap in the most efficient way.
|
2005-11-26 23:14:57 +01:00
|
|
|
*/
|
|
|
|
if (numTids > 1)
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
int lastTid;
|
|
|
|
int i;
|
2005-11-26 23:14:57 +01:00
|
|
|
|
2007-10-24 20:37:09 +02:00
|
|
|
/* CurrentOfExpr could never appear OR'd with something else */
|
|
|
|
Assert(!tidstate->tss_isCurrentOf);
|
|
|
|
|
2005-11-26 23:14:57 +01:00
|
|
|
qsort((void *) tidList, numTids, sizeof(ItemPointerData),
|
|
|
|
itemptr_comparator);
|
|
|
|
lastTid = 0;
|
|
|
|
for (i = 1; i < numTids; i++)
|
|
|
|
{
|
|
|
|
if (!ItemPointerEquals(&tidList[lastTid], &tidList[i]))
|
|
|
|
tidList[++lastTid] = tidList[i];
|
|
|
|
}
|
|
|
|
numTids = lastTid + 1;
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
2003-09-26 03:17:01 +02:00
|
|
|
|
|
|
|
tidstate->tss_TidList = tidList;
|
|
|
|
tidstate->tss_NumTids = numTids;
|
|
|
|
tidstate->tss_TidPtr = -1;
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
|
|
|
|
2005-11-26 23:14:57 +01:00
|
|
|
/*
|
|
|
|
* qsort comparator for ItemPointerData items
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
itemptr_comparator(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const ItemPointerData *ipa = (const ItemPointerData *) a;
|
|
|
|
const ItemPointerData *ipb = (const ItemPointerData *) b;
|
2006-10-04 02:30:14 +02:00
|
|
|
BlockNumber ba = ItemPointerGetBlockNumber(ipa);
|
|
|
|
BlockNumber bb = ItemPointerGetBlockNumber(ipb);
|
2005-11-26 23:14:57 +01:00
|
|
|
OffsetNumber oa = ItemPointerGetOffsetNumber(ipa);
|
|
|
|
OffsetNumber ob = ItemPointerGetOffsetNumber(ipb);
|
|
|
|
|
|
|
|
if (ba < bb)
|
|
|
|
return -1;
|
|
|
|
if (ba > bb)
|
|
|
|
return 1;
|
|
|
|
if (oa < ob)
|
|
|
|
return -1;
|
|
|
|
if (oa > ob)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-11-23 21:07:06 +01:00
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* TidNext
|
|
|
|
*
|
|
|
|
* Retrieve a tuple from the TidScan node's currentRelation
|
|
|
|
* using the tids in the TidScanState information.
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static TupleTableSlot *
|
2002-12-05 16:50:39 +01:00
|
|
|
TidNext(TidScanState *node)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
EState *estate;
|
|
|
|
ScanDirection direction;
|
1999-11-23 21:07:06 +01:00
|
|
|
Snapshot snapshot;
|
|
|
|
Relation heapRelation;
|
|
|
|
HeapTuple tuple;
|
2000-04-12 19:17:23 +02:00
|
|
|
TupleTableSlot *slot;
|
1999-11-23 21:07:06 +01:00
|
|
|
Buffer buffer = InvalidBuffer;
|
2003-09-26 03:17:01 +02:00
|
|
|
ItemPointerData *tidList;
|
2000-04-12 19:17:23 +02:00
|
|
|
int numTids;
|
1999-11-23 21:07:06 +01:00
|
|
|
bool bBackward;
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* extract necessary information from tid scan node
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
estate = node->ss.ps.state;
|
1999-11-23 21:07:06 +01:00
|
|
|
direction = estate->es_direction;
|
|
|
|
snapshot = estate->es_snapshot;
|
2002-12-05 16:50:39 +01:00
|
|
|
heapRelation = node->ss.ss_currentRelation;
|
|
|
|
slot = node->ss.ss_ScanTupleSlot;
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2003-09-26 03:17:01 +02:00
|
|
|
/*
|
|
|
|
* First time through, compute the list of TIDs to be visited
|
|
|
|
*/
|
|
|
|
if (node->tss_TidList == NULL)
|
|
|
|
TidListCreate(node);
|
|
|
|
|
|
|
|
tidList = node->tss_TidList;
|
|
|
|
numTids = node->tss_NumTids;
|
|
|
|
|
2002-12-05 16:50:39 +01:00
|
|
|
tuple = &(node->tss_htup);
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-11-26 23:14:57 +01:00
|
|
|
* Initialize or advance scan position, depending on direction.
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
|
|
|
bBackward = ScanDirectionIsBackward(direction);
|
|
|
|
if (bBackward)
|
|
|
|
{
|
2005-11-26 23:14:57 +01:00
|
|
|
if (node->tss_TidPtr < 0)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2005-11-26 23:14:57 +01:00
|
|
|
/* initialize for backward scan */
|
2002-12-05 16:50:39 +01:00
|
|
|
node->tss_TidPtr = numTids - 1;
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
2005-11-26 23:14:57 +01:00
|
|
|
else
|
|
|
|
node->tss_TidPtr--;
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-11-26 23:14:57 +01:00
|
|
|
if (node->tss_TidPtr < 0)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2005-11-26 23:14:57 +01:00
|
|
|
/* initialize for forward scan */
|
2002-12-05 16:50:39 +01:00
|
|
|
node->tss_TidPtr = 0;
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
2005-11-26 23:14:57 +01:00
|
|
|
else
|
|
|
|
node->tss_TidPtr++;
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
|
|
|
|
2005-11-26 23:14:57 +01:00
|
|
|
while (node->tss_TidPtr >= 0 && node->tss_TidPtr < numTids)
|
|
|
|
{
|
2002-12-05 16:50:39 +01:00
|
|
|
tuple->t_self = tidList[node->tss_TidPtr];
|
2007-10-24 20:37:09 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For WHERE CURRENT OF, the tuple retrieved from the cursor might
|
2007-11-15 22:14:46 +01:00
|
|
|
* since have been updated; if so, we should fetch the version that is
|
|
|
|
* current according to our snapshot.
|
2007-10-24 20:37:09 +02:00
|
|
|
*/
|
|
|
|
if (node->tss_isCurrentOf)
|
|
|
|
heap_get_latest_tid(heapRelation, snapshot, &tuple->t_self);
|
|
|
|
|
2002-05-24 20:57:57 +02:00
|
|
|
if (heap_fetch(heapRelation, snapshot, tuple, &buffer, false, NULL))
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* store the scanned tuple in the scan tuple slot of the scan
|
2005-10-15 04:49:52 +02:00
|
|
|
* state. Eventually we will only do this and not return a tuple.
|
|
|
|
* Note: we pass 'false' because tuples returned by amgetnext are
|
|
|
|
* pointers onto disk pages and were not created with palloc() and
|
|
|
|
* so should not be pfree()'d.
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
2000-04-12 19:17:23 +02:00
|
|
|
ExecStoreTuple(tuple, /* tuple to store */
|
2001-10-28 07:26:15 +01:00
|
|
|
slot, /* slot to store in */
|
2000-04-12 19:17:23 +02:00
|
|
|
buffer, /* buffer associated with tuple */
|
|
|
|
false); /* don't pfree */
|
1999-11-23 21:07:06 +01:00
|
|
|
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* At this point we have an extra pin on the buffer, because
|
2005-10-15 04:49:52 +02:00
|
|
|
* ExecStoreTuple incremented the pin count. Drop our local pin.
|
2000-04-12 19:17:23 +02:00
|
|
|
*/
|
|
|
|
ReleaseBuffer(buffer);
|
|
|
|
|
2005-11-26 23:14:57 +01:00
|
|
|
return slot;
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
2005-11-26 23:14:57 +01:00
|
|
|
/* Bad TID or failed snapshot qual; try next */
|
1999-11-23 21:07:06 +01:00
|
|
|
if (bBackward)
|
2002-12-05 16:50:39 +01:00
|
|
|
node->tss_TidPtr--;
|
1999-11-23 21:07:06 +01:00
|
|
|
else
|
2002-12-05 16:50:39 +01:00
|
|
|
node->tss_TidPtr++;
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
2001-03-22 07:16:21 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* if we get here it means the tid scan failed so we are at the end of the
|
|
|
|
* scan..
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
|
|
|
return ExecClearTuple(slot);
|
|
|
|
}
|
|
|
|
|
Re-implement EvalPlanQual processing to improve its performance and eliminate
a lot of strange behaviors that occurred in join cases. We now identify the
"current" row for every joined relation in UPDATE, DELETE, and SELECT FOR
UPDATE/SHARE queries. If an EvalPlanQual recheck is necessary, we jam the
appropriate row into each scan node in the rechecking plan, forcing it to emit
only that one row. The former behavior could rescan the whole of each joined
relation for each recheck, which was terrible for performance, and what's much
worse could result in duplicated output tuples.
Also, the original implementation of EvalPlanQual could not re-use the recheck
execution tree --- it had to go through a full executor init and shutdown for
every row to be tested. To avoid this overhead, I've associated a special
runtime Param with each LockRows or ModifyTable plan node, and arranged to
make every scan node below such a node depend on that Param. Thus, by
signaling a change in that Param, the EPQ machinery can just rescan the
already-built test plan.
This patch also adds a prohibition on set-returning functions in the
targetlist of SELECT FOR UPDATE/SHARE. This is needed to avoid the
duplicate-output-tuple problem. It seems fairly reasonable since the
other restrictions on SELECT FOR UPDATE are meant to ensure that there
is a unique correspondence between source tuples and result tuples,
which an output SRF destroys as much as anything else does.
2009-10-26 03:26:45 +01:00
|
|
|
/*
|
|
|
|
* TidRecheck -- access method routine to recheck a tuple in EvalPlanQual
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
TidRecheck(TidScanState *node, TupleTableSlot *slot)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* XXX shouldn't we check here to make sure tuple matches TID list? In
|
|
|
|
* runtime-key case this is not certain, is it? However, in the WHERE
|
|
|
|
* CURRENT OF case it might not match anyway ...
|
|
|
|
*/
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-23 21:07:06 +01:00
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecTidScan(node)
|
|
|
|
*
|
|
|
|
* Scans the relation using tids and returns
|
|
|
|
* the next qualifying tuple in the direction specified.
|
Re-implement EvalPlanQual processing to improve its performance and eliminate
a lot of strange behaviors that occurred in join cases. We now identify the
"current" row for every joined relation in UPDATE, DELETE, and SELECT FOR
UPDATE/SHARE queries. If an EvalPlanQual recheck is necessary, we jam the
appropriate row into each scan node in the rechecking plan, forcing it to emit
only that one row. The former behavior could rescan the whole of each joined
relation for each recheck, which was terrible for performance, and what's much
worse could result in duplicated output tuples.
Also, the original implementation of EvalPlanQual could not re-use the recheck
execution tree --- it had to go through a full executor init and shutdown for
every row to be tested. To avoid this overhead, I've associated a special
runtime Param with each LockRows or ModifyTable plan node, and arranged to
make every scan node below such a node depend on that Param. Thus, by
signaling a change in that Param, the EPQ machinery can just rescan the
already-built test plan.
This patch also adds a prohibition on set-returning functions in the
targetlist of SELECT FOR UPDATE/SHARE. This is needed to avoid the
duplicate-output-tuple problem. It seems fairly reasonable since the
other restrictions on SELECT FOR UPDATE are meant to ensure that there
is a unique correspondence between source tuples and result tuples,
which an output SRF destroys as much as anything else does.
2009-10-26 03:26:45 +01:00
|
|
|
* We call the ExecScan() routine and pass it the appropriate
|
|
|
|
* access method functions.
|
1999-11-23 21:07:06 +01:00
|
|
|
*
|
|
|
|
* Conditions:
|
|
|
|
* -- the "cursor" maintained by the AMI is positioned at the tuple
|
|
|
|
* returned previously.
|
|
|
|
*
|
|
|
|
* Initial States:
|
|
|
|
* -- the relation indicated is opened for scanning so that the
|
|
|
|
* "cursor" is positioned before the first qualifying tuple.
|
2005-11-26 23:14:57 +01:00
|
|
|
* -- tidPtr is -1.
|
1999-11-23 21:07:06 +01:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
TupleTableSlot *
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecTidScan(TidScanState *node)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
Re-implement EvalPlanQual processing to improve its performance and eliminate
a lot of strange behaviors that occurred in join cases. We now identify the
"current" row for every joined relation in UPDATE, DELETE, and SELECT FOR
UPDATE/SHARE queries. If an EvalPlanQual recheck is necessary, we jam the
appropriate row into each scan node in the rechecking plan, forcing it to emit
only that one row. The former behavior could rescan the whole of each joined
relation for each recheck, which was terrible for performance, and what's much
worse could result in duplicated output tuples.
Also, the original implementation of EvalPlanQual could not re-use the recheck
execution tree --- it had to go through a full executor init and shutdown for
every row to be tested. To avoid this overhead, I've associated a special
runtime Param with each LockRows or ModifyTable plan node, and arranged to
make every scan node below such a node depend on that Param. Thus, by
signaling a change in that Param, the EPQ machinery can just rescan the
already-built test plan.
This patch also adds a prohibition on set-returning functions in the
targetlist of SELECT FOR UPDATE/SHARE. This is needed to avoid the
duplicate-output-tuple problem. It seems fairly reasonable since the
other restrictions on SELECT FOR UPDATE are meant to ensure that there
is a unique correspondence between source tuples and result tuples,
which an output SRF destroys as much as anything else does.
2009-10-26 03:26:45 +01:00
|
|
|
return ExecScan(&node->ss,
|
|
|
|
(ExecScanAccessMtd) TidNext,
|
|
|
|
(ExecScanRecheckMtd) TidRecheck);
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
2010-07-12 19:01:06 +02:00
|
|
|
* ExecReScanTidScan(node)
|
1999-11-23 21:07:06 +01:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2010-07-12 19:01:06 +02:00
|
|
|
ExecReScanTidScan(TidScanState *node)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2003-09-26 03:17:01 +02:00
|
|
|
if (node->tss_TidList)
|
|
|
|
pfree(node->tss_TidList);
|
|
|
|
node->tss_TidList = NULL;
|
|
|
|
node->tss_NumTids = 0;
|
2002-12-05 16:50:39 +01:00
|
|
|
node->tss_TidPtr = -1;
|
Re-implement EvalPlanQual processing to improve its performance and eliminate
a lot of strange behaviors that occurred in join cases. We now identify the
"current" row for every joined relation in UPDATE, DELETE, and SELECT FOR
UPDATE/SHARE queries. If an EvalPlanQual recheck is necessary, we jam the
appropriate row into each scan node in the rechecking plan, forcing it to emit
only that one row. The former behavior could rescan the whole of each joined
relation for each recheck, which was terrible for performance, and what's much
worse could result in duplicated output tuples.
Also, the original implementation of EvalPlanQual could not re-use the recheck
execution tree --- it had to go through a full executor init and shutdown for
every row to be tested. To avoid this overhead, I've associated a special
runtime Param with each LockRows or ModifyTable plan node, and arranged to
make every scan node below such a node depend on that Param. Thus, by
signaling a change in that Param, the EPQ machinery can just rescan the
already-built test plan.
This patch also adds a prohibition on set-returning functions in the
targetlist of SELECT FOR UPDATE/SHARE. This is needed to avoid the
duplicate-output-tuple problem. It seems fairly reasonable since the
other restrictions on SELECT FOR UPDATE are meant to ensure that there
is a unique correspondence between source tuples and result tuples,
which an output SRF destroys as much as anything else does.
2009-10-26 03:26:45 +01:00
|
|
|
|
|
|
|
ExecScanReScan(&node->ss);
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecEndTidScan
|
|
|
|
*
|
|
|
|
* Releases any storage allocated through C routines.
|
|
|
|
* Returns nothing.
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecEndTidScan(TidScanState *node)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2002-02-19 21:11:20 +01:00
|
|
|
/*
|
2002-12-15 17:17:59 +01:00
|
|
|
* Free the exprcontext
|
2002-02-19 21:11:20 +01:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecFreeExprContext(&node->ss.ps);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* clear out tuple table slots
|
|
|
|
*/
|
|
|
|
ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
|
|
|
|
ExecClearTuple(node->ss.ss_ScanTupleSlot);
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2002-02-19 21:11:20 +01:00
|
|
|
* close the heap relation.
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
2005-12-02 21:03:42 +01:00
|
|
|
ExecCloseScanRelation(node->ss.ss_currentRelation);
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecTidMarkPos
|
|
|
|
*
|
|
|
|
* Marks scan position by marking the current tid.
|
|
|
|
* Returns nothing.
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecTidMarkPos(TidScanState *node)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2002-12-05 16:50:39 +01:00
|
|
|
node->tss_MarkTidPtr = node->tss_TidPtr;
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecTidRestrPos
|
|
|
|
*
|
|
|
|
* Restores scan position by restoring the current tid.
|
|
|
|
* Returns nothing.
|
|
|
|
*
|
|
|
|
* XXX Assumes previously marked scan position belongs to current tid
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecTidRestrPos(TidScanState *node)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2002-12-05 16:50:39 +01:00
|
|
|
node->tss_TidPtr = node->tss_MarkTidPtr;
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecInitTidScan
|
|
|
|
*
|
|
|
|
* Initializes the tid scan's state information, creates
|
|
|
|
* scan keys, and opens the base and tid relations.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* node: TidNode node produced by the planner.
|
|
|
|
* estate: the execution state initialized in InitPlan.
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
TidScanState *
|
2006-02-28 05:10:28 +01:00
|
|
|
ExecInitTidScan(TidScan *node, EState *estate, int eflags)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
TidScanState *tidstate;
|
1999-11-23 21:07:06 +01:00
|
|
|
Relation currentRelation;
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2002-12-05 16:50:39 +01:00
|
|
|
* create state structure
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
tidstate = makeNode(TidScanState);
|
|
|
|
tidstate->ss.ps.plan = (Plan *) node;
|
|
|
|
tidstate->ss.ps.state = estate;
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2002-12-05 16:50:39 +01:00
|
|
|
* Miscellaneous initialization
|
1999-11-23 21:07:06 +01:00
|
|
|
*
|
2002-12-05 16:50:39 +01:00
|
|
|
* create expression context for node
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecAssignExprContext(estate, &tidstate->ss.ps);
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2006-12-26 20:26:46 +01:00
|
|
|
tidstate->ss.ps.ps_TupFromTlist = false;
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2002-12-05 16:50:39 +01:00
|
|
|
* initialize child expressions
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
tidstate->ss.ps.targetlist = (List *)
|
2002-12-13 20:46:01 +01:00
|
|
|
ExecInitExpr((Expr *) node->scan.plan.targetlist,
|
2002-12-05 16:50:39 +01:00
|
|
|
(PlanState *) tidstate);
|
|
|
|
tidstate->ss.ps.qual = (List *)
|
2002-12-13 20:46:01 +01:00
|
|
|
ExecInitExpr((Expr *) node->scan.plan.qual,
|
2002-12-05 16:50:39 +01:00
|
|
|
(PlanState *) tidstate);
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2005-11-26 23:14:57 +01:00
|
|
|
tidstate->tss_tidquals = (List *)
|
|
|
|
ExecInitExpr((Expr *) node->tidquals,
|
2003-09-26 03:17:01 +02:00
|
|
|
(PlanState *) tidstate);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* tuple table initialization
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecInitResultTupleSlot(estate, &tidstate->ss.ps);
|
|
|
|
ExecInitScanTupleSlot(estate, &tidstate->ss);
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2003-09-26 03:17:01 +02:00
|
|
|
* mark tid list as not computed yet
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
2003-09-26 03:17:01 +02:00
|
|
|
tidstate->tss_TidList = NULL;
|
|
|
|
tidstate->tss_NumTids = 0;
|
|
|
|
tidstate->tss_TidPtr = -1;
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-12-02 21:03:42 +01:00
|
|
|
* open the base relation and acquire appropriate lock on it.
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
2005-12-02 21:03:42 +01:00
|
|
|
currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid);
|
2002-02-19 21:11:20 +01:00
|
|
|
|
2002-12-05 16:50:39 +01:00
|
|
|
tidstate->ss.ss_currentRelation = currentRelation;
|
|
|
|
tidstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* get the scan type from the relation descriptor.
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
2006-06-16 20:42:24 +02:00
|
|
|
ExecAssignScanType(&tidstate->ss, RelationGetDescr(currentRelation));
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2003-02-03 16:07:08 +01:00
|
|
|
/*
|
|
|
|
* Initialize result tuple type and projection info.
|
|
|
|
*/
|
|
|
|
ExecAssignResultTypeFromTL(&tidstate->ss.ps);
|
|
|
|
ExecAssignScanProjectionInfo(&tidstate->ss);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* all done.
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
return tidstate;
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|