1999-11-23 21:07:06 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* nodeTidscan.c
|
|
|
|
* Routines to support direct tid scans of relations
|
|
|
|
*
|
2021-01-02 19:06:25 +01:00
|
|
|
* Portions Copyright (c) 1996-2021, 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.
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2008-05-12 02:00:54 +02:00
|
|
|
#include "access/sysattr.h"
|
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
2019-03-11 20:46:41 +01:00
|
|
|
#include "access/tableam.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"
|
2019-11-07 04:51:04 +01:00
|
|
|
#include "lib/qunique.h"
|
2017-07-26 02:37:17 +02:00
|
|
|
#include "miscadmin.h"
|
2019-01-29 21:26:44 +01:00
|
|
|
#include "nodes/nodeFuncs.h"
|
2008-05-12 02:00:54 +02:00
|
|
|
#include "storage/bufmgr.h"
|
2011-09-04 07:13:16 +02:00
|
|
|
#include "utils/array.h"
|
2011-02-23 18:18:09 +01:00
|
|
|
#include "utils/rel.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)
|
|
|
|
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 23:45:36 +01:00
|
|
|
/* one element in tss_tidexprs */
|
|
|
|
typedef struct TidExpr
|
|
|
|
{
|
|
|
|
ExprState *exprstate; /* ExprState for a TID-yielding subexpr */
|
|
|
|
bool isarray; /* if true, it yields tid[] not just tid */
|
|
|
|
CurrentOfExpr *cexpr; /* alternatively, we can have CURRENT OF */
|
|
|
|
} TidExpr;
|
|
|
|
|
|
|
|
static void TidExprListCreate(TidScanState *tidstate);
|
|
|
|
static void TidListEval(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
|
|
|
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 23:45:36 +01:00
|
|
|
/*
|
|
|
|
* Extract the qual subexpressions that yield TIDs to search for,
|
|
|
|
* and compile them into ExprStates if they're ordinary expressions.
|
|
|
|
*
|
|
|
|
* CURRENT OF is a special case that we can't compile usefully;
|
|
|
|
* just drop it into the TidExpr list as-is.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
TidExprListCreate(TidScanState *tidstate)
|
|
|
|
{
|
|
|
|
TidScan *node = (TidScan *) tidstate->ss.ps.plan;
|
|
|
|
ListCell *l;
|
|
|
|
|
|
|
|
tidstate->tss_tidexprs = NIL;
|
|
|
|
tidstate->tss_isCurrentOf = false;
|
|
|
|
|
|
|
|
foreach(l, node->tidquals)
|
|
|
|
{
|
|
|
|
Expr *expr = (Expr *) lfirst(l);
|
|
|
|
TidExpr *tidexpr = (TidExpr *) palloc0(sizeof(TidExpr));
|
|
|
|
|
|
|
|
if (is_opclause(expr))
|
|
|
|
{
|
|
|
|
Node *arg1;
|
|
|
|
Node *arg2;
|
|
|
|
|
|
|
|
arg1 = get_leftop(expr);
|
|
|
|
arg2 = get_rightop(expr);
|
|
|
|
if (IsCTIDVar(arg1))
|
|
|
|
tidexpr->exprstate = ExecInitExpr((Expr *) arg2,
|
|
|
|
&tidstate->ss.ps);
|
|
|
|
else if (IsCTIDVar(arg2))
|
|
|
|
tidexpr->exprstate = ExecInitExpr((Expr *) arg1,
|
|
|
|
&tidstate->ss.ps);
|
|
|
|
else
|
|
|
|
elog(ERROR, "could not identify CTID variable");
|
|
|
|
tidexpr->isarray = false;
|
|
|
|
}
|
|
|
|
else if (expr && IsA(expr, ScalarArrayOpExpr))
|
|
|
|
{
|
|
|
|
ScalarArrayOpExpr *saex = (ScalarArrayOpExpr *) expr;
|
|
|
|
|
|
|
|
Assert(IsCTIDVar(linitial(saex->args)));
|
|
|
|
tidexpr->exprstate = ExecInitExpr(lsecond(saex->args),
|
|
|
|
&tidstate->ss.ps);
|
|
|
|
tidexpr->isarray = true;
|
|
|
|
}
|
|
|
|
else if (expr && IsA(expr, CurrentOfExpr))
|
|
|
|
{
|
|
|
|
CurrentOfExpr *cexpr = (CurrentOfExpr *) expr;
|
|
|
|
|
|
|
|
tidexpr->cexpr = cexpr;
|
|
|
|
tidstate->tss_isCurrentOf = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
elog(ERROR, "could not identify CTID expression");
|
|
|
|
|
|
|
|
tidstate->tss_tidexprs = lappend(tidstate->tss_tidexprs, tidexpr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CurrentOfExpr could never appear OR'd with something else */
|
|
|
|
Assert(list_length(tidstate->tss_tidexprs) == 1 ||
|
|
|
|
!tidstate->tss_isCurrentOf);
|
|
|
|
}
|
|
|
|
|
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
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 23:45:36 +01:00
|
|
|
TidListEval(TidScanState *tidstate)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2003-09-26 03:17:01 +02:00
|
|
|
ExprContext *econtext = tidstate->ss.ps.ps_ExprContext;
|
2019-05-18 03:52:01 +02:00
|
|
|
TableScanDesc scan;
|
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
|
|
|
/*
|
2019-05-18 03:52:01 +02:00
|
|
|
* Start scan on-demand - initializing a scan isn't free (e.g. heap stats
|
|
|
|
* the size of the table), so it makes sense to delay that until needed -
|
|
|
|
* the node might never get executed.
|
2008-05-01 01:28:32 +02:00
|
|
|
*/
|
2019-05-18 03:52:01 +02:00
|
|
|
if (tidstate->ss.ss_currentScanDesc == NULL)
|
|
|
|
tidstate->ss.ss_currentScanDesc =
|
2020-02-07 14:00:21 +01:00
|
|
|
table_beginscan_tid(tidstate->ss.ss_currentRelation,
|
|
|
|
tidstate->ss.ps.state->es_snapshot);
|
2019-05-18 03:52:01 +02:00
|
|
|
scan = tidstate->ss.ss_currentScanDesc;
|
2008-05-01 01:28:32 +02:00
|
|
|
|
2005-11-26 23:14:57 +01: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
|
|
|
*/
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 23:45:36 +01:00
|
|
|
numAllocTids = list_length(tidstate->tss_tidexprs);
|
2003-09-26 03:17:01 +02:00
|
|
|
tidList = (ItemPointerData *)
|
2005-11-26 23:14:57 +01:00
|
|
|
palloc(numAllocTids * sizeof(ItemPointerData));
|
|
|
|
numTids = 0;
|
1999-11-23 21:07:06 +01:00
|
|
|
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 23:45:36 +01:00
|
|
|
foreach(l, tidstate->tss_tidexprs)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 23:45:36 +01:00
|
|
|
TidExpr *tidexpr = (TidExpr *) lfirst(l);
|
2003-09-26 03:17:01 +02:00
|
|
|
ItemPointer itemptr;
|
|
|
|
bool isNull;
|
|
|
|
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 23:45:36 +01:00
|
|
|
if (tidexpr->exprstate && !tidexpr->isarray)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2005-11-26 23:14:57 +01:00
|
|
|
itemptr = (ItemPointer)
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 23:45:36 +01:00
|
|
|
DatumGetPointer(ExecEvalExprSwitchContext(tidexpr->exprstate,
|
2005-11-26 23:14:57 +01:00
|
|
|
econtext,
|
2017-01-19 23:12:38 +01:00
|
|
|
&isNull));
|
2019-05-18 03:52:01 +02:00
|
|
|
if (isNull)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We silently discard any TIDs that the AM considers invalid
|
|
|
|
* (E.g. for heap, they could be 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.).
|
|
|
|
*/
|
|
|
|
if (!table_tuple_tid_valid(scan, itemptr))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (numTids >= numAllocTids)
|
2005-11-26 23:14:57 +01:00
|
|
|
{
|
2019-05-18 03:52:01 +02:00
|
|
|
numAllocTids *= 2;
|
|
|
|
tidList = (ItemPointerData *)
|
|
|
|
repalloc(tidList,
|
|
|
|
numAllocTids * sizeof(ItemPointerData));
|
2005-11-26 23:14:57 +01:00
|
|
|
}
|
2019-05-18 03:52:01 +02:00
|
|
|
tidList[numTids++] = *itemptr;
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 23:45:36 +01:00
|
|
|
else if (tidexpr->exprstate && tidexpr->isarray)
|
2005-11-26 23:14:57 +01:00
|
|
|
{
|
|
|
|
Datum arraydatum;
|
|
|
|
ArrayType *itemarray;
|
|
|
|
Datum *ipdatums;
|
|
|
|
bool *ipnulls;
|
|
|
|
int ndatums;
|
|
|
|
int i;
|
|
|
|
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 23:45:36 +01:00
|
|
|
arraydatum = ExecEvalExprSwitchContext(tidexpr->exprstate,
|
2005-11-26 23:14:57 +01:00
|
|
|
econtext,
|
2017-01-19 23:12:38 +01:00
|
|
|
&isNull);
|
2005-11-26 23:14:57 +01:00
|
|
|
if (isNull)
|
|
|
|
continue;
|
|
|
|
itemarray = DatumGetArrayTypeP(arraydatum);
|
|
|
|
deconstruct_array(itemarray,
|
2020-03-04 16:34:25 +01:00
|
|
|
TIDOID, sizeof(ItemPointerData), false, TYPALIGN_SHORT,
|
2005-11-26 23:14:57 +01:00
|
|
|
&ipdatums, &ipnulls, &ndatums);
|
|
|
|
if (numTids + ndatums > numAllocTids)
|
|
|
|
{
|
|
|
|
numAllocTids = numTids + ndatums;
|
|
|
|
tidList = (ItemPointerData *)
|
|
|
|
repalloc(tidList,
|
|
|
|
numAllocTids * sizeof(ItemPointerData));
|
|
|
|
}
|
|
|
|
for (i = 0; i < ndatums; i++)
|
|
|
|
{
|
2019-05-18 03:52:01 +02:00
|
|
|
if (ipnulls[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
itemptr = (ItemPointer) DatumGetPointer(ipdatums[i]);
|
|
|
|
|
|
|
|
if (!table_tuple_tid_valid(scan, itemptr))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tidList[numTids++] = *itemptr;
|
2005-11-26 23:14:57 +01:00
|
|
|
}
|
|
|
|
pfree(ipdatums);
|
|
|
|
pfree(ipnulls);
|
|
|
|
}
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 23:45:36 +01:00
|
|
|
else
|
2007-06-11 03:16:30 +02:00
|
|
|
{
|
|
|
|
ItemPointerData cursor_tid;
|
|
|
|
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 23:45:36 +01:00
|
|
|
Assert(tidexpr->cexpr);
|
|
|
|
if (execCurrentOf(tidexpr->cexpr, econtext,
|
2007-06-11 03:16:30 +02:00
|
|
|
RelationGetRelid(tidstate->ss.ss_currentRelation),
|
|
|
|
&cursor_tid))
|
|
|
|
{
|
|
|
|
if (numTids >= numAllocTids)
|
|
|
|
{
|
|
|
|
numAllocTids *= 2;
|
|
|
|
tidList = (ItemPointerData *)
|
|
|
|
repalloc(tidList,
|
|
|
|
numAllocTids * sizeof(ItemPointerData));
|
|
|
|
}
|
|
|
|
tidList[numTids++] = cursor_tid;
|
|
|
|
}
|
|
|
|
}
|
2005-11-26 23:14:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sort the array of TIDs into order, and eliminate duplicates.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
if (numTids > 1)
|
|
|
|
{
|
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);
|
2019-11-07 04:51:04 +01:00
|
|
|
numTids = qunique(tidList, numTids, sizeof(ItemPointerData),
|
|
|
|
itemptr_comparator);
|
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;
|
|
|
|
BlockNumber ba = ItemPointerGetBlockNumber(ipa);
|
|
|
|
BlockNumber bb = ItemPointerGetBlockNumber(ipb);
|
|
|
|
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
|
|
|
{
|
|
|
|
EState *estate;
|
|
|
|
ScanDirection direction;
|
|
|
|
Snapshot snapshot;
|
2019-05-18 03:52:01 +02:00
|
|
|
TableScanDesc scan;
|
1999-11-23 21:07:06 +01:00
|
|
|
Relation heapRelation;
|
|
|
|
TupleTableSlot *slot;
|
2003-09-26 03:17:01 +02:00
|
|
|
ItemPointerData *tidList;
|
1999-11-23 21:07:06 +01:00
|
|
|
int numTids;
|
|
|
|
bool bBackward;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* extract necessary information from tid scan node
|
|
|
|
*/
|
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)
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 23:45:36 +01:00
|
|
|
TidListEval(node);
|
2003-09-26 03:17:01 +02:00
|
|
|
|
2019-05-18 03:52:01 +02:00
|
|
|
scan = node->ss.ss_currentScanDesc;
|
2003-09-26 03:17:01 +02:00
|
|
|
tidList = node->tss_TidList;
|
|
|
|
numTids = node->tss_NumTids;
|
|
|
|
|
1999-11-23 21:07:06 +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)
|
|
|
|
{
|
2019-03-26 01:14:48 +01:00
|
|
|
ItemPointerData tid = tidList[node->tss_TidPtr];
|
2007-10-24 20:37:09 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For WHERE CURRENT OF, the tuple retrieved from the cursor might
|
|
|
|
* since have been updated; if so, we should fetch the version that is
|
|
|
|
* current according to our snapshot.
|
|
|
|
*/
|
|
|
|
if (node->tss_isCurrentOf)
|
2019-05-24 01:25:48 +02:00
|
|
|
table_tuple_get_latest_tid(scan, &tid);
|
2007-10-24 20:37:09 +02:00
|
|
|
|
2019-05-24 01:25:48 +02:00
|
|
|
if (table_tuple_fetch_row_version(heapRelation, &tid, snapshot, slot))
|
2005-11-26 23:14:57 +01:00
|
|
|
return slot;
|
2019-03-25 08:13:42 +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++;
|
2017-07-26 02:37:17 +02:00
|
|
|
|
|
|
|
CHECK_FOR_INTERRUPTS();
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
2001-03-22 07:16:21 +01:00
|
|
|
|
1999-11-23 21:07:06 +01:00
|
|
|
/*
|
|
|
|
* if we get here it means the tid scan failed so we are at the end of the
|
|
|
|
* scan..
|
|
|
|
*/
|
|
|
|
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.
|
2019-08-19 09:21:39 +02:00
|
|
|
* -- tss_TidPtr is -1.
|
1999-11-23 21:07:06 +01:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
2017-07-17 09:33:49 +02:00
|
|
|
static TupleTableSlot *
|
|
|
|
ExecTidScan(PlanState *pstate)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2017-07-17 09:33:49 +02:00
|
|
|
TidScanState *node = castNode(TidScanState, pstate);
|
|
|
|
|
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
|
|
|
|
2019-05-18 03:52:01 +02:00
|
|
|
/* not really necessary, but seems good form */
|
|
|
|
if (node->ss.ss_currentScanDesc)
|
|
|
|
table_rescan(node->ss.ss_currentScanDesc, NULL);
|
|
|
|
|
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
|
|
|
{
|
2019-05-18 03:52:01 +02:00
|
|
|
if (node->ss.ss_currentScanDesc)
|
|
|
|
table_endscan(node->ss.ss_currentScanDesc);
|
|
|
|
|
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
|
|
|
|
*/
|
Don't require return slots for nodes without projection.
In a lot of nodes the return slot is not required. That can either be
because the node doesn't do any projection (say an Append node), or
because the node does perform projections but the projection is
optimized away because the projection would yield an identical row.
Slots aren't that small, especially for wide rows, so it's worthwhile
to avoid creating them. It's not possible to just skip creating the
slot - it's currently used to determine the tuple descriptor returned
by ExecGetResultType(). So separate the determination of the result
type from the slot creation. The work previously done internally
ExecInitResultTupleSlotTL() can now also be done separately with
ExecInitResultTypeTL() and ExecInitResultSlot(). That way nodes that
aren't guaranteed to need a result slot, can use
ExecInitResultTypeTL() to determine the result type of the node, and
ExecAssignScanProjectionInfo() (via
ExecConditionalAssignProjectionInfo()) determines that a result slot
is needed, it is created with ExecInitResultSlot().
Besides the advantage of avoiding to create slots that then are
unused, this is necessary preparation for later patches around tuple
table slot abstraction. In particular separating the return descriptor
and slot is a prerequisite to allow JITing of tuple deforming with
knowledge of the underlying tuple format, and to avoid unnecessarily
creating JITed tuple deforming for virtual slots.
This commit removes a redundant argument from
ExecInitResultTupleSlotTL(). While this commit touches a lot of the
relevant lines anyway, it'd normally still not worthwhile to cause
breakage, except that aforementioned later commits will touch *all*
ExecInitResultTupleSlotTL() callers anyway (but fits worse
thematically).
Author: Andres Freund
Discussion: https://postgr.es/m/20181105210039.hh4vvi4vwoq5ba2q@alap3.anarazel.de
2018-11-10 02:19:39 +01:00
|
|
|
if (node->ss.ps.ps_ResultTupleSlot)
|
|
|
|
ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecClearTuple(node->ss.ss_ScanTupleSlot);
|
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:
|
2019-08-19 09:21:39 +02:00
|
|
|
* node: TidScan node produced by the planner.
|
1999-11-23 21:07:06 +01:00
|
|
|
* 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
|
|
|
{
|
|
|
|
TidScanState *tidstate;
|
|
|
|
Relation currentRelation;
|
|
|
|
|
|
|
|
/*
|
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;
|
2017-07-17 09:33:49 +02:00
|
|
|
tidstate->ss.ps.ExecProcNode = ExecTidScan;
|
1999-11-23 21:07:06 +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
|
|
|
|
|
|
|
/*
|
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
|
|
|
|
|
|
|
/*
|
2018-10-06 21:49:37 +02:00
|
|
|
* open the scan relation
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
2013-04-27 23:48:57 +02:00
|
|
|
currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
* get the scan type from the relation descriptor.
|
|
|
|
*/
|
2018-02-17 06:17:38 +01:00
|
|
|
ExecInitScanTupleSlot(estate, &tidstate->ss,
|
Introduce notion of different types of slots (without implementing them).
Upcoming work intends to allow pluggable ways to introduce new ways of
storing table data. Accessing those table access methods from the
executor requires TupleTableSlots to be carry tuples in the native
format of such storage methods; otherwise there'll be a significant
conversion overhead.
Different access methods will require different data to store tuples
efficiently (just like virtual, minimal, heap already require fields
in TupleTableSlot). To allow that without requiring additional pointer
indirections, we want to have different structs (embedding
TupleTableSlot) for different types of slots. Thus different types of
slots are needed, which requires adapting creators of slots.
The slot that most efficiently can represent a type of tuple in an
executor node will often depend on the type of slot a child node
uses. Therefore we need to track the type of slot is returned by
nodes, so parent slots can create slots based on that.
Relatedly, JIT compilation of tuple deforming needs to know which type
of slot a certain expression refers to, so it can create an
appropriate deforming function for the type of tuple in the slot.
But not all nodes will only return one type of slot, e.g. an append
node will potentially return different types of slots for each of its
subplans.
Therefore add function that allows to query the type of a node's
result slot, and whether it'll always be the same type (whether it's
fixed). This can be queried using ExecGetResultSlotOps().
The scan, result, inner, outer type of slots are automatically
inferred from ExecInitScanTupleSlot(), ExecInitResultSlot(),
left/right subtrees respectively. If that's not correct for a node,
that can be overwritten using new fields in PlanState.
This commit does not introduce the actually abstracted implementation
of different kind of TupleTableSlots, that will be left for a followup
commit. The different types of slots introduced will, for now, still
use the same backing implementation.
While this already partially invalidates the big comment in
tuptable.h, it seems to make more sense to update it later, when the
different TupleTableSlot implementations actually exist.
Author: Ashutosh Bapat and Andres Freund, with changes by Amit Khandekar
Discussion: https://postgr.es/m/20181105210039.hh4vvi4vwoq5ba2q@alap3.anarazel.de
2018-11-16 07:00:30 +01:00
|
|
|
RelationGetDescr(currentRelation),
|
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
2019-03-11 20:46:41 +01:00
|
|
|
table_slot_callbacks(currentRelation));
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2003-02-03 16:07:08 +01:00
|
|
|
/*
|
Don't require return slots for nodes without projection.
In a lot of nodes the return slot is not required. That can either be
because the node doesn't do any projection (say an Append node), or
because the node does perform projections but the projection is
optimized away because the projection would yield an identical row.
Slots aren't that small, especially for wide rows, so it's worthwhile
to avoid creating them. It's not possible to just skip creating the
slot - it's currently used to determine the tuple descriptor returned
by ExecGetResultType(). So separate the determination of the result
type from the slot creation. The work previously done internally
ExecInitResultTupleSlotTL() can now also be done separately with
ExecInitResultTypeTL() and ExecInitResultSlot(). That way nodes that
aren't guaranteed to need a result slot, can use
ExecInitResultTypeTL() to determine the result type of the node, and
ExecAssignScanProjectionInfo() (via
ExecConditionalAssignProjectionInfo()) determines that a result slot
is needed, it is created with ExecInitResultSlot().
Besides the advantage of avoiding to create slots that then are
unused, this is necessary preparation for later patches around tuple
table slot abstraction. In particular separating the return descriptor
and slot is a prerequisite to allow JITing of tuple deforming with
knowledge of the underlying tuple format, and to avoid unnecessarily
creating JITed tuple deforming for virtual slots.
This commit removes a redundant argument from
ExecInitResultTupleSlotTL(). While this commit touches a lot of the
relevant lines anyway, it'd normally still not worthwhile to cause
breakage, except that aforementioned later commits will touch *all*
ExecInitResultTupleSlotTL() callers anyway (but fits worse
thematically).
Author: Andres Freund
Discussion: https://postgr.es/m/20181105210039.hh4vvi4vwoq5ba2q@alap3.anarazel.de
2018-11-10 02:19:39 +01:00
|
|
|
* Initialize result type and projection.
|
2003-02-03 16:07:08 +01:00
|
|
|
*/
|
Don't require return slots for nodes without projection.
In a lot of nodes the return slot is not required. That can either be
because the node doesn't do any projection (say an Append node), or
because the node does perform projections but the projection is
optimized away because the projection would yield an identical row.
Slots aren't that small, especially for wide rows, so it's worthwhile
to avoid creating them. It's not possible to just skip creating the
slot - it's currently used to determine the tuple descriptor returned
by ExecGetResultType(). So separate the determination of the result
type from the slot creation. The work previously done internally
ExecInitResultTupleSlotTL() can now also be done separately with
ExecInitResultTypeTL() and ExecInitResultSlot(). That way nodes that
aren't guaranteed to need a result slot, can use
ExecInitResultTypeTL() to determine the result type of the node, and
ExecAssignScanProjectionInfo() (via
ExecConditionalAssignProjectionInfo()) determines that a result slot
is needed, it is created with ExecInitResultSlot().
Besides the advantage of avoiding to create slots that then are
unused, this is necessary preparation for later patches around tuple
table slot abstraction. In particular separating the return descriptor
and slot is a prerequisite to allow JITing of tuple deforming with
knowledge of the underlying tuple format, and to avoid unnecessarily
creating JITed tuple deforming for virtual slots.
This commit removes a redundant argument from
ExecInitResultTupleSlotTL(). While this commit touches a lot of the
relevant lines anyway, it'd normally still not worthwhile to cause
breakage, except that aforementioned later commits will touch *all*
ExecInitResultTupleSlotTL() callers anyway (but fits worse
thematically).
Author: Andres Freund
Discussion: https://postgr.es/m/20181105210039.hh4vvi4vwoq5ba2q@alap3.anarazel.de
2018-11-10 02:19:39 +01:00
|
|
|
ExecInitResultTypeTL(&tidstate->ss.ps);
|
2003-02-03 16:07:08 +01:00
|
|
|
ExecAssignScanProjectionInfo(&tidstate->ss);
|
|
|
|
|
2018-02-17 06:17:38 +01:00
|
|
|
/*
|
|
|
|
* initialize child expressions
|
|
|
|
*/
|
|
|
|
tidstate->ss.ps.qual =
|
|
|
|
ExecInitQual(node->scan.plan.qual, (PlanState *) tidstate);
|
|
|
|
|
|
|
|
TidExprListCreate(tidstate);
|
|
|
|
|
1999-11-23 21:07:06 +01:00
|
|
|
/*
|
|
|
|
* all done.
|
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
return tidstate;
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|