2010-09-20 22:08:53 +02:00
|
|
|
<!-- doc/src/sgml/indexam.sgml -->
|
2005-02-13 04:04:15 +01:00
|
|
|
|
|
|
|
<chapter id="indexam">
|
|
|
|
<title>Index Access Method Interface Definition</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
This chapter defines the interface between the core
|
|
|
|
<productname>PostgreSQL</productname> system and <firstterm>index access
|
|
|
|
methods</>, which manage individual index types. The core system
|
|
|
|
knows nothing about indexes beyond what is specified here, so it is
|
|
|
|
possible to develop entirely new index types by writing add-on code.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
All indexes in <productname>PostgreSQL</productname> are what are known
|
|
|
|
technically as <firstterm>secondary indexes</>; that is, the index is
|
|
|
|
physically separate from the table file that it describes. Each index
|
|
|
|
is stored as its own physical <firstterm>relation</> and so is described
|
|
|
|
by an entry in the <structname>pg_class</> catalog. The contents of an
|
|
|
|
index are entirely under the control of its index access method. In
|
|
|
|
practice, all index access methods divide indexes into standard-size
|
|
|
|
pages so that they can use the regular storage manager and buffer manager
|
|
|
|
to access the index contents. (All the existing index access methods
|
|
|
|
furthermore use the standard page layout described in <xref
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
linkend="storage-page-layout">, and most use the same format for index
|
2005-02-13 04:04:15 +01:00
|
|
|
tuple headers; but these decisions are not forced on an access method.)
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
An index is effectively a mapping from some data key values to
|
|
|
|
<firstterm>tuple identifiers</>, or <acronym>TIDs</>, of row versions
|
|
|
|
(tuples) in the index's parent table. A TID consists of a
|
|
|
|
block number and an item number within that block (see <xref
|
|
|
|
linkend="storage-page-layout">). This is sufficient
|
|
|
|
information to fetch a particular row version from the table.
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
Indexes are not directly aware that under MVCC, there might be multiple
|
2005-02-13 04:04:15 +01:00
|
|
|
extant versions of the same logical row; to an index, each tuple is
|
|
|
|
an independent object that needs its own index entry. Thus, an
|
|
|
|
update of a row always creates all-new index entries for the row, even if
|
2009-03-06 00:06:45 +01:00
|
|
|
the key values did not change. (HOT tuples are an exception to this
|
|
|
|
statement; but indexes do not deal with those, either.) Index entries for
|
|
|
|
dead tuples are reclaimed (by vacuuming) when the dead tuples themselves
|
|
|
|
are reclaimed.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
<sect1 id="index-api">
|
|
|
|
<title>Basic API Structure for Indexes</title>
|
2005-02-13 04:04:15 +01:00
|
|
|
|
|
|
|
<para>
|
|
|
|
Each index access method is described by a row in the
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
<link linkend="catalog-pg-am"><structname>pg_am</structname></link>
|
|
|
|
system catalog. The <structname>pg_am</structname> entry
|
|
|
|
specifies a name and a <firstterm>handler function</> for the access
|
|
|
|
method. There is not currently any special support
|
2005-02-13 04:04:15 +01:00
|
|
|
for creating or deleting <structname>pg_am</structname> entries;
|
|
|
|
anyone able to write a new access method is expected to be competent
|
|
|
|
to insert an appropriate row for themselves.
|
|
|
|
</para>
|
|
|
|
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
<para>
|
|
|
|
An index access method handler function must be declared to accept a
|
|
|
|
single argument of type <type>internal</> and to return the
|
|
|
|
pseudo-type <type>index_am_handler</>. The argument is a dummy value that
|
|
|
|
simply serves to prevent handler functions from being called directly from
|
|
|
|
SQL commands. The result of the function must be a palloc'd struct of
|
|
|
|
type <structname>IndexAmRoutine</structname>, which contains everything
|
|
|
|
that the core code needs to know to make use of the index access method.
|
|
|
|
The <structname>IndexAmRoutine</structname> struct, also called the access
|
|
|
|
method's <firstterm>API struct</>, includes fields specifying assorted
|
|
|
|
fixed properties of the access method, such as whether it can support
|
|
|
|
multicolumn indexes. More importantly, it contains pointers to support
|
|
|
|
functions for the access method, which do all of the real work to access
|
|
|
|
indexes. These support functions are plain C functions and are not
|
|
|
|
visible or callable at the SQL level. The support functions are described
|
|
|
|
in <xref linkend="index-functions">.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The structure <structname>IndexAmRoutine</structname> is defined thus:
|
|
|
|
<programlisting>
|
|
|
|
typedef struct IndexAmRoutine
|
|
|
|
{
|
|
|
|
NodeTag type;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Total number of strategies (operators) by which we can traverse/search
|
|
|
|
* this AM. Zero if AM does not have a fixed set of strategy assignments.
|
|
|
|
*/
|
|
|
|
uint16 amstrategies;
|
|
|
|
/* total number of support functions that this AM uses */
|
|
|
|
uint16 amsupport;
|
|
|
|
/* does AM support ORDER BY indexed column's value? */
|
|
|
|
bool amcanorder;
|
|
|
|
/* does AM support ORDER BY result of an operator on indexed column? */
|
|
|
|
bool amcanorderbyop;
|
|
|
|
/* does AM support backward scanning? */
|
|
|
|
bool amcanbackward;
|
|
|
|
/* does AM support UNIQUE indexes? */
|
|
|
|
bool amcanunique;
|
|
|
|
/* does AM support multi-column indexes? */
|
|
|
|
bool amcanmulticol;
|
|
|
|
/* does AM require scans to have a constraint on the first index column? */
|
|
|
|
bool amoptionalkey;
|
|
|
|
/* does AM handle ScalarArrayOpExpr quals? */
|
|
|
|
bool amsearcharray;
|
|
|
|
/* does AM handle IS NULL/IS NOT NULL quals? */
|
|
|
|
bool amsearchnulls;
|
|
|
|
/* can index storage data type differ from column data type? */
|
|
|
|
bool amstorage;
|
|
|
|
/* can an index of this type be clustered on? */
|
|
|
|
bool amclusterable;
|
|
|
|
/* does AM handle predicate locks? */
|
|
|
|
bool ampredlocks;
|
|
|
|
/* type of data stored in index, or InvalidOid if variable */
|
|
|
|
Oid amkeytype;
|
|
|
|
|
|
|
|
/* interface functions */
|
|
|
|
ambuild_function ambuild;
|
|
|
|
ambuildempty_function ambuildempty;
|
|
|
|
aminsert_function aminsert;
|
|
|
|
ambulkdelete_function ambulkdelete;
|
|
|
|
amvacuumcleanup_function amvacuumcleanup;
|
|
|
|
amcanreturn_function amcanreturn; /* can be NULL */
|
|
|
|
amcostestimate_function amcostestimate;
|
|
|
|
amoptions_function amoptions;
|
|
|
|
amvalidate_function amvalidate;
|
|
|
|
ambeginscan_function ambeginscan;
|
|
|
|
amrescan_function amrescan;
|
|
|
|
amgettuple_function amgettuple; /* can be NULL */
|
|
|
|
amgetbitmap_function amgetbitmap; /* can be NULL */
|
|
|
|
amendscan_function amendscan;
|
|
|
|
ammarkpos_function ammarkpos; /* can be NULL */
|
|
|
|
amrestrpos_function amrestrpos; /* can be NULL */
|
|
|
|
} IndexAmRoutine;
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
2005-02-13 04:04:15 +01:00
|
|
|
<para>
|
|
|
|
To be useful, an index access method must also have one or more
|
2006-12-23 01:43:13 +01:00
|
|
|
<firstterm>operator families</> and
|
2005-02-13 04:04:15 +01:00
|
|
|
<firstterm>operator classes</> defined in
|
2006-12-23 01:43:13 +01:00
|
|
|
<link linkend="catalog-pg-opfamily"><structname>pg_opfamily</structname></link>,
|
2005-02-13 04:04:15 +01:00
|
|
|
<link linkend="catalog-pg-opclass"><structname>pg_opclass</structname></link>,
|
|
|
|
<link linkend="catalog-pg-amop"><structname>pg_amop</structname></link>, and
|
|
|
|
<link linkend="catalog-pg-amproc"><structname>pg_amproc</structname></link>.
|
|
|
|
These entries allow the planner
|
|
|
|
to determine what kinds of query qualifications can be used with
|
2006-12-23 01:43:13 +01:00
|
|
|
indexes of this access method. Operator families and classes are described
|
2005-02-13 04:04:15 +01:00
|
|
|
in <xref linkend="xindex">, which is prerequisite material for reading
|
|
|
|
this chapter.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2009-03-24 21:17:18 +01:00
|
|
|
An individual index is defined by a
|
2005-02-13 04:04:15 +01:00
|
|
|
<link linkend="catalog-pg-class"><structname>pg_class</structname></link>
|
|
|
|
entry that describes it as a physical relation, plus a
|
|
|
|
<link linkend="catalog-pg-index"><structname>pg_index</structname></link>
|
|
|
|
entry that shows the logical content of the index — that is, the set
|
|
|
|
of index columns it has and the semantics of those columns, as captured by
|
|
|
|
the associated operator classes. The index columns (key values) can be
|
|
|
|
either simple columns of the underlying table or expressions over the table
|
|
|
|
rows. The index access method normally has no interest in where the index
|
|
|
|
key values come from (it is always handed precomputed key values) but it
|
|
|
|
will be very interested in the operator class information in
|
|
|
|
<structname>pg_index</structname>. Both of these catalog entries can be
|
|
|
|
accessed as part of the <structname>Relation</> data structure that is
|
|
|
|
passed to all operations on the index.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
Some of the flag fields of <structname>IndexAmRoutine</> have nonobvious
|
2005-02-13 04:04:15 +01:00
|
|
|
implications. The requirements of <structfield>amcanunique</structfield>
|
2006-07-31 22:09:10 +02:00
|
|
|
are discussed in <xref linkend="index-unique-checks">.
|
2005-02-13 04:04:15 +01:00
|
|
|
The <structfield>amcanmulticol</structfield> flag asserts that the
|
2005-11-05 00:14:02 +01:00
|
|
|
access method supports multicolumn indexes, while
|
2005-06-14 01:14:49 +02:00
|
|
|
<structfield>amoptionalkey</structfield> asserts that it allows scans
|
|
|
|
where no indexable restriction clause is given for the first index column.
|
|
|
|
When <structfield>amcanmulticol</structfield> is false,
|
|
|
|
<structfield>amoptionalkey</structfield> essentially says whether the
|
2011-01-08 22:08:05 +01:00
|
|
|
access method supports full-index scans without any restriction clause.
|
2005-06-14 01:14:49 +02:00
|
|
|
Access methods that support multiple index columns <emphasis>must</>
|
|
|
|
support scans that omit restrictions on any or all of the columns after
|
|
|
|
the first; however they are permitted to require some restriction to
|
|
|
|
appear for the first index column, and this is signaled by setting
|
|
|
|
<structfield>amoptionalkey</structfield> false.
|
2011-01-08 22:08:05 +01:00
|
|
|
One reason that an index AM might set
|
|
|
|
<structfield>amoptionalkey</structfield> false is if it doesn't index
|
2013-05-21 03:13:13 +02:00
|
|
|
null values. Since most indexable operators are
|
|
|
|
strict and hence cannot return true for null inputs,
|
2005-11-05 00:14:02 +01:00
|
|
|
it is at first sight attractive to not store index entries for null values:
|
2005-02-13 04:04:15 +01:00
|
|
|
they could never be returned by an index scan anyway. However, this
|
2005-06-14 01:14:49 +02:00
|
|
|
argument fails when an index scan has no restriction clause for a given
|
|
|
|
index column. In practice this means that
|
|
|
|
indexes that have <structfield>amoptionalkey</structfield> true must
|
|
|
|
index nulls, since the planner might decide to use such an index
|
|
|
|
with no scan keys at all. A related restriction is that an index
|
2005-02-13 04:04:15 +01:00
|
|
|
access method that supports multiple index columns <emphasis>must</>
|
|
|
|
support indexing null values in columns after the first, because the planner
|
2005-06-14 01:14:49 +02:00
|
|
|
will assume the index can be used for queries that do not restrict
|
|
|
|
these columns. For example, consider an index on (a,b) and a query with
|
2005-02-13 04:04:15 +01:00
|
|
|
<literal>WHERE a = 4</literal>. The system will assume the index can be
|
|
|
|
used to scan for rows with <literal>a = 4</literal>, which is wrong if the
|
|
|
|
index omits rows where <literal>b</> is null.
|
|
|
|
It is, however, OK to omit rows where the first indexed column is null.
|
2011-01-08 22:08:05 +01:00
|
|
|
An index access method that does index nulls may also set
|
2007-04-07 00:33:43 +02:00
|
|
|
<structfield>amsearchnulls</structfield>, indicating that it supports
|
2010-01-01 22:53:49 +01:00
|
|
|
<literal>IS NULL</> and <literal>IS NOT NULL</> clauses as search
|
|
|
|
conditions.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<sect1 id="index-functions">
|
|
|
|
<title>Index Access Method Functions</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The index construction and maintenance functions that an index access
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
method must provide in <structname>IndexAmRoutine</structname> are:
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<programlisting>
|
2006-05-11 01:18:39 +02:00
|
|
|
IndexBuildResult *
|
2005-02-13 04:04:15 +01:00
|
|
|
ambuild (Relation heapRelation,
|
|
|
|
Relation indexRelation,
|
|
|
|
IndexInfo *indexInfo);
|
|
|
|
</programlisting>
|
|
|
|
Build a new index. The index relation has been physically created,
|
|
|
|
but is empty. It must be filled in with whatever fixed data the
|
|
|
|
access method requires, plus entries for all tuples already existing
|
|
|
|
in the table. Ordinarily the <function>ambuild</> function will call
|
|
|
|
<function>IndexBuildHeapScan()</> to scan the table for existing tuples
|
|
|
|
and compute the keys that need to be inserted into the index.
|
2006-05-11 01:18:39 +02:00
|
|
|
The function must return a palloc'd struct containing statistics about
|
|
|
|
the new index.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<programlisting>
|
2010-12-29 12:48:53 +01:00
|
|
|
void
|
|
|
|
ambuildempty (Relation indexRelation);
|
|
|
|
</programlisting>
|
2014-07-17 04:20:15 +02:00
|
|
|
Build an empty index, and write it to the initialization fork (<symbol>INIT_FORKNUM</symbol>)
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
of the given relation. This method is called only for unlogged indexes; the
|
2010-12-29 12:48:53 +01:00
|
|
|
empty index written to the initialization fork will be copied over the main
|
|
|
|
relation fork on each server restart.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<programlisting>
|
2005-03-21 02:24:04 +01:00
|
|
|
bool
|
2005-02-13 04:04:15 +01:00
|
|
|
aminsert (Relation indexRelation,
|
2005-03-21 02:24:04 +01:00
|
|
|
Datum *values,
|
|
|
|
bool *isnull,
|
2005-02-13 04:04:15 +01:00
|
|
|
ItemPointer heap_tid,
|
|
|
|
Relation heapRelation,
|
2009-07-29 22:56:21 +02:00
|
|
|
IndexUniqueCheck checkUnique);
|
2005-02-13 04:04:15 +01:00
|
|
|
</programlisting>
|
2005-03-21 02:24:04 +01:00
|
|
|
Insert a new tuple into an existing index. The <literal>values</> and
|
|
|
|
<literal>isnull</> arrays give the key values to be indexed, and
|
2005-02-13 04:04:15 +01:00
|
|
|
<literal>heap_tid</> is the TID to be indexed.
|
|
|
|
If the access method supports unique indexes (its
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
<structfield>amcanunique</> flag is true) then
|
2009-07-29 22:56:21 +02:00
|
|
|
<literal>checkUnique</> indicates the type of uniqueness check to
|
|
|
|
perform. This varies depending on whether the unique constraint is
|
|
|
|
deferrable; see <xref linkend="index-unique-checks"> for details.
|
|
|
|
Normally the access method only needs the <literal>heapRelation</>
|
|
|
|
parameter when performing uniqueness checking (since then it will have to
|
|
|
|
look into the heap to verify tuple liveness).
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2010-08-17 06:37:21 +02:00
|
|
|
The function's Boolean result value is significant only when
|
2009-07-29 22:56:21 +02:00
|
|
|
<literal>checkUnique</> is <literal>UNIQUE_CHECK_PARTIAL</>.
|
|
|
|
In this case a TRUE result means the new entry is known unique, whereas
|
|
|
|
FALSE means it might be non-unique (and a deferred uniqueness check must
|
|
|
|
be scheduled). For other cases a constant FALSE result is recommended.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Some indexes might not index all tuples. If the tuple is not to be
|
|
|
|
indexed, <function>aminsert</> should just return without doing anything.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<programlisting>
|
|
|
|
IndexBulkDeleteResult *
|
2006-05-03 00:25:10 +02:00
|
|
|
ambulkdelete (IndexVacuumInfo *info,
|
|
|
|
IndexBulkDeleteResult *stats,
|
2005-02-13 04:04:15 +01:00
|
|
|
IndexBulkDeleteCallback callback,
|
|
|
|
void *callback_state);
|
|
|
|
</programlisting>
|
|
|
|
Delete tuple(s) from the index. This is a <quote>bulk delete</> operation
|
|
|
|
that is intended to be implemented by scanning the whole index and checking
|
|
|
|
each entry to see if it should be deleted.
|
2006-05-03 00:25:10 +02:00
|
|
|
The passed-in <literal>callback</> function must be called, in the style
|
2005-02-13 04:04:15 +01:00
|
|
|
<literal>callback(<replaceable>TID</>, callback_state) returns bool</literal>,
|
|
|
|
to determine whether any particular index entry, as identified by its
|
|
|
|
referenced TID, is to be deleted. Must return either NULL or a palloc'd
|
|
|
|
struct containing statistics about the effects of the deletion operation.
|
2006-05-03 00:25:10 +02:00
|
|
|
It is OK to return NULL if no information needs to be passed on to
|
|
|
|
<function>amvacuumcleanup</>.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
2006-02-12 00:31:34 +01:00
|
|
|
<para>
|
2006-05-03 00:25:10 +02:00
|
|
|
Because of limited <varname>maintenance_work_mem</>,
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
<function>ambulkdelete</> might need to be called more than once when many
|
2006-05-03 00:25:10 +02:00
|
|
|
tuples are to be deleted. The <literal>stats</> argument is the result
|
|
|
|
of the previous call for this index (it is NULL for the first call within a
|
|
|
|
<command>VACUUM</> operation). This allows the AM to accumulate statistics
|
|
|
|
across the whole operation. Typically, <function>ambulkdelete</> will
|
|
|
|
modify and return the same struct if the passed <literal>stats</> is not
|
|
|
|
null.
|
2006-02-12 00:31:34 +01:00
|
|
|
</para>
|
|
|
|
|
2005-02-13 04:04:15 +01:00
|
|
|
<para>
|
|
|
|
<programlisting>
|
|
|
|
IndexBulkDeleteResult *
|
2006-05-03 00:25:10 +02:00
|
|
|
amvacuumcleanup (IndexVacuumInfo *info,
|
2005-02-13 04:04:15 +01:00
|
|
|
IndexBulkDeleteResult *stats);
|
|
|
|
</programlisting>
|
2006-05-03 00:25:10 +02:00
|
|
|
Clean up after a <command>VACUUM</command> operation (zero or more
|
|
|
|
<function>ambulkdelete</> calls). This does not have to do anything
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
beyond returning index statistics, but it might perform bulk cleanup
|
2006-05-03 00:25:10 +02:00
|
|
|
such as reclaiming empty index pages. <literal>stats</> is whatever the
|
|
|
|
last <function>ambulkdelete</> call returned, or NULL if
|
|
|
|
<function>ambulkdelete</> was not called because no tuples needed to be
|
|
|
|
deleted. If the result is not NULL it must be a palloc'd struct.
|
|
|
|
The statistics it contains will be used to update <structname>pg_class</>,
|
|
|
|
and will be reported by <command>VACUUM</> if <literal>VERBOSE</> is given.
|
|
|
|
It is OK to return NULL if the index was not changed at all during the
|
|
|
|
<command>VACUUM</command> operation, but otherwise correct stats should
|
|
|
|
be returned.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
2009-03-24 21:17:18 +01:00
|
|
|
<para>
|
|
|
|
As of <productname>PostgreSQL</productname> 8.4,
|
|
|
|
<function>amvacuumcleanup</> will also be called at completion of an
|
|
|
|
<command>ANALYZE</> operation. In this case <literal>stats</> is always
|
|
|
|
NULL and any return value will be ignored. This case can be distinguished
|
|
|
|
by checking <literal>info->analyze_only</literal>. It is recommended
|
|
|
|
that the access method do nothing except post-insert cleanup in such a
|
|
|
|
call, and that only in an autovacuum worker process.
|
|
|
|
</para>
|
|
|
|
|
2006-07-04 00:45:41 +02:00
|
|
|
<para>
|
|
|
|
<programlisting>
|
2011-12-18 21:49:00 +01:00
|
|
|
bool
|
2015-03-26 18:12:00 +01:00
|
|
|
amcanreturn (Relation indexRelation, int attno);
|
2011-12-18 21:49:00 +01:00
|
|
|
</programlisting>
|
2015-03-26 18:12:00 +01:00
|
|
|
Check whether the index can support <firstterm>index-only scans</> on the
|
|
|
|
given column, by returning the indexed column values for an index entry in
|
|
|
|
the form of an <structname>IndexTuple</structname>. The attribute number
|
|
|
|
is 1-based, i.e. the first columns attno is 1. Returns TRUE if supported,
|
|
|
|
else FALSE. If the access method does not support index-only scans at all,
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
the <structfield>amcanreturn</> field in its <structname>IndexAmRoutine</>
|
|
|
|
struct can be set to NULL.
|
2011-12-18 21:49:00 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<programlisting>
|
2006-07-04 00:45:41 +02:00
|
|
|
void
|
|
|
|
amcostestimate (PlannerInfo *root,
|
2011-12-25 01:03:21 +01:00
|
|
|
IndexPath *path,
|
2012-01-28 01:26:38 +01:00
|
|
|
double loop_count,
|
2006-07-04 00:45:41 +02:00
|
|
|
Cost *indexStartupCost,
|
|
|
|
Cost *indexTotalCost,
|
|
|
|
Selectivity *indexSelectivity,
|
|
|
|
double *indexCorrelation);
|
|
|
|
</programlisting>
|
|
|
|
Estimate the costs of an index scan. This function is described fully
|
|
|
|
in <xref linkend="index-cost-estimation">, below.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<programlisting>
|
|
|
|
bytea *
|
|
|
|
amoptions (ArrayType *reloptions,
|
|
|
|
bool validate);
|
|
|
|
</programlisting>
|
|
|
|
Parse and validate the reloptions array for an index. This is called only
|
|
|
|
when a non-null reloptions array exists for the index.
|
|
|
|
<parameter>reloptions</> is a <type>text</> array containing entries of the
|
|
|
|
form <replaceable>name</><literal>=</><replaceable>value</>.
|
|
|
|
The function should construct a <type>bytea</> value, which will be copied
|
|
|
|
into the <structfield>rd_options</> field of the index's relcache entry.
|
|
|
|
The data contents of the <type>bytea</> value are open for the access
|
2009-03-06 00:06:45 +01:00
|
|
|
method to define; most of the standard access methods use struct
|
2006-07-04 00:45:41 +02:00
|
|
|
<structname>StdRdOptions</>.
|
|
|
|
When <parameter>validate</> is true, the function should report a suitable
|
|
|
|
error message if any of the options are unrecognized or have invalid
|
|
|
|
values; when <parameter>validate</> is false, invalid entries should be
|
|
|
|
silently ignored. (<parameter>validate</> is false when loading options
|
|
|
|
already stored in <structname>pg_catalog</>; an invalid entry could only
|
|
|
|
be found if the access method has changed its rules for options, and in
|
|
|
|
that case ignoring obsolete entries is appropriate.)
|
|
|
|
It is OK to return NULL if default behavior is wanted.
|
|
|
|
</para>
|
|
|
|
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
<para>
|
|
|
|
<programlisting>
|
|
|
|
bool
|
|
|
|
amvalidate (Oid opclassoid);
|
|
|
|
</programlisting>
|
|
|
|
Validate the catalog entries for the specified operator class, so far as
|
|
|
|
the access method can reasonably do that. For example, this might include
|
|
|
|
testing that all required support functions are provided.
|
|
|
|
The <function>amvalidate</> function must return false if the opclass is
|
|
|
|
invalid. Problems should be reported with <function>ereport</> messages.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
2005-02-13 04:04:15 +01:00
|
|
|
<para>
|
|
|
|
The purpose of an index, of course, is to support scans for tuples matching
|
|
|
|
an indexable <literal>WHERE</> condition, often called a
|
|
|
|
<firstterm>qualifier</> or <firstterm>scan key</>. The semantics of
|
|
|
|
index scanning are described more fully in <xref linkend="index-scanning">,
|
2009-03-06 00:06:45 +01:00
|
|
|
below. An index access method can support <quote>plain</> index scans,
|
|
|
|
<quote>bitmap</> index scans, or both. The scan-related functions that an
|
|
|
|
index access method must or may provide are:
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<programlisting>
|
|
|
|
IndexScanDesc
|
|
|
|
ambeginscan (Relation indexRelation,
|
|
|
|
int nkeys,
|
2010-12-03 02:50:48 +01:00
|
|
|
int norderbys);
|
2005-02-13 04:04:15 +01:00
|
|
|
</programlisting>
|
2010-12-03 02:50:48 +01:00
|
|
|
Prepare for an index scan. The <literal>nkeys</> and <literal>norderbys</>
|
|
|
|
parameters indicate the number of quals and ordering operators that will be
|
|
|
|
used in the scan; these may be useful for space allocation purposes.
|
|
|
|
Note that the actual values of the scan keys aren't provided yet.
|
|
|
|
The result must be a palloc'd struct.
|
|
|
|
For implementation reasons the index access method
|
2005-02-13 04:04:15 +01:00
|
|
|
<emphasis>must</> create this struct by calling
|
|
|
|
<function>RelationGetIndexScan()</>. In most cases
|
2010-12-03 02:50:48 +01:00
|
|
|
<function>ambeginscan</> does little beyond making that call and perhaps
|
|
|
|
acquiring locks;
|
2005-11-05 00:14:02 +01:00
|
|
|
the interesting parts of index-scan startup are in <function>amrescan</>.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<programlisting>
|
2010-12-03 02:50:48 +01:00
|
|
|
void
|
|
|
|
amrescan (IndexScanDesc scan,
|
|
|
|
ScanKey keys,
|
|
|
|
int nkeys,
|
|
|
|
ScanKey orderbys,
|
|
|
|
int norderbys);
|
|
|
|
</programlisting>
|
2012-06-07 23:06:20 +02:00
|
|
|
Start or restart an index scan, possibly with new scan keys. (To restart
|
2010-12-03 02:50:48 +01:00
|
|
|
using previously-passed keys, NULL is passed for <literal>keys</> and/or
|
|
|
|
<literal>orderbys</>.) Note that it is not allowed for
|
|
|
|
the number of keys or order-by operators to be larger than
|
|
|
|
what was passed to <function>ambeginscan</>. In practice the restart
|
|
|
|
feature is used when a new outer tuple is selected by a nested-loop join
|
|
|
|
and so a new key comparison value is needed, but the scan key structure
|
|
|
|
remains the same.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<programlisting>
|
2005-02-13 04:04:15 +01:00
|
|
|
boolean
|
|
|
|
amgettuple (IndexScanDesc scan,
|
|
|
|
ScanDirection direction);
|
|
|
|
</programlisting>
|
|
|
|
Fetch the next tuple in the given scan, moving in the given
|
|
|
|
direction (forward or backward in the index). Returns TRUE if a tuple was
|
|
|
|
obtained, FALSE if no matching tuples remain. In the TRUE case the tuple
|
|
|
|
TID is stored into the <literal>scan</> structure. Note that
|
|
|
|
<quote>success</> means only that the index contains an entry that matches
|
|
|
|
the scan keys, not that the tuple necessarily still exists in the heap or
|
2008-04-13 21:18:14 +02:00
|
|
|
will pass the caller's snapshot test. On success, <function>amgettuple</>
|
|
|
|
must also set <literal>scan->xs_recheck</> to TRUE or FALSE.
|
|
|
|
FALSE means it is certain that the index entry matches the scan keys.
|
|
|
|
TRUE means this is not certain, and the conditions represented by the
|
|
|
|
scan keys must be rechecked against the heap tuple after fetching it.
|
|
|
|
This provision supports <quote>lossy</> index operators.
|
|
|
|
Note that rechecking will extend only to the scan conditions; a partial
|
|
|
|
index predicate (if any) is never rechecked by <function>amgettuple</>
|
|
|
|
callers.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
2011-10-08 02:13:02 +02:00
|
|
|
<para>
|
2011-12-18 21:49:00 +01:00
|
|
|
If the index supports index-only scans (i.e.,
|
|
|
|
<function>amcanreturn</function> returns TRUE for it),
|
|
|
|
then on success the AM must also check
|
2011-10-09 06:21:08 +02:00
|
|
|
<literal>scan->xs_want_itup</>, and if that is true it must return
|
2011-10-08 02:13:02 +02:00
|
|
|
the original indexed data for the index entry, in the form of an
|
2011-10-17 01:15:04 +02:00
|
|
|
<structname>IndexTuple</> pointer stored at <literal>scan->xs_itup</>,
|
|
|
|
with tuple descriptor <literal>scan->xs_itupdesc</>.
|
2011-10-09 06:21:08 +02:00
|
|
|
(Management of the data referenced by the pointer is the access method's
|
|
|
|
responsibility. The data must remain good at least until the next
|
|
|
|
<function>amgettuple</>, <function>amrescan</>, or <function>amendscan</>
|
|
|
|
call for the scan.)
|
2011-10-08 02:13:02 +02:00
|
|
|
</para>
|
|
|
|
|
2009-03-06 00:06:45 +01:00
|
|
|
<para>
|
|
|
|
The <function>amgettuple</> function need only be provided if the access
|
|
|
|
method supports <quote>plain</> index scans. If it doesn't, the
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
<structfield>amgettuple</> field in its <structname>IndexAmRoutine</>
|
|
|
|
struct must be set to NULL.
|
2009-03-06 00:06:45 +01:00
|
|
|
</para>
|
|
|
|
|
2005-02-13 04:04:15 +01:00
|
|
|
<para>
|
|
|
|
<programlisting>
|
2008-04-11 00:25:26 +02:00
|
|
|
int64
|
|
|
|
amgetbitmap (IndexScanDesc scan,
|
|
|
|
TIDBitmap *tbm);
|
2005-03-28 01:53:05 +02:00
|
|
|
</programlisting>
|
2008-04-11 00:25:26 +02:00
|
|
|
Fetch all tuples in the given scan and add them to the caller-supplied
|
2010-08-17 06:37:21 +02:00
|
|
|
<type>TIDBitmap</type> (that is, OR the set of tuple IDs into whatever set is already
|
2009-03-24 21:17:18 +01:00
|
|
|
in the bitmap). The number of tuples fetched is returned (this might be
|
|
|
|
just an approximate count, for instance some AMs do not detect duplicates).
|
2008-04-13 21:18:14 +02:00
|
|
|
While inserting tuple IDs into the bitmap, <function>amgetbitmap</> can
|
|
|
|
indicate that rechecking of the scan conditions is required for specific
|
|
|
|
tuple IDs. This is analogous to the <literal>xs_recheck</> output parameter
|
|
|
|
of <function>amgettuple</>. Note: in the current implementation, support
|
|
|
|
for this feature is conflated with support for lossy storage of the bitmap
|
|
|
|
itself, and therefore callers recheck both the scan conditions and the
|
|
|
|
partial index predicate (if any) for recheckable tuples. That might not
|
|
|
|
always be true, however.
|
2008-04-11 00:25:26 +02:00
|
|
|
<function>amgetbitmap</> and
|
2005-03-28 01:53:05 +02:00
|
|
|
<function>amgettuple</> cannot be used in the same index scan; there
|
2008-04-11 00:25:26 +02:00
|
|
|
are other restrictions too when using <function>amgetbitmap</>, as explained
|
2005-03-28 01:53:05 +02:00
|
|
|
in <xref linkend="index-scanning">.
|
|
|
|
</para>
|
|
|
|
|
2009-03-06 00:06:45 +01:00
|
|
|
<para>
|
|
|
|
The <function>amgetbitmap</> function need only be provided if the access
|
|
|
|
method supports <quote>bitmap</> index scans. If it doesn't, the
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
<structfield>amgetbitmap</> field in its <structname>IndexAmRoutine</>
|
|
|
|
struct must be set to NULL.
|
2009-03-06 00:06:45 +01:00
|
|
|
</para>
|
|
|
|
|
2005-03-28 01:53:05 +02:00
|
|
|
<para>
|
|
|
|
<programlisting>
|
2005-02-13 04:04:15 +01:00
|
|
|
void
|
|
|
|
amendscan (IndexScanDesc scan);
|
|
|
|
</programlisting>
|
|
|
|
End a scan and release resources. The <literal>scan</> struct itself
|
|
|
|
should not be freed, but any locks or pins taken internally by the
|
|
|
|
access method must be released.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<programlisting>
|
|
|
|
void
|
|
|
|
ammarkpos (IndexScanDesc scan);
|
|
|
|
</programlisting>
|
|
|
|
Mark current scan position. The access method need only support one
|
|
|
|
remembered scan position per scan.
|
|
|
|
</para>
|
|
|
|
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
<para>
|
|
|
|
The <function>ammarkpos</> function need only be provided if the access
|
|
|
|
method supports ordered scans. If it doesn't,
|
|
|
|
the <structfield>ammarkpos</> field in its <structname>IndexAmRoutine</>
|
|
|
|
struct may be set to NULL.
|
|
|
|
</para>
|
|
|
|
|
2005-02-13 04:04:15 +01:00
|
|
|
<para>
|
|
|
|
<programlisting>
|
|
|
|
void
|
|
|
|
amrestrpos (IndexScanDesc scan);
|
|
|
|
</programlisting>
|
|
|
|
Restore the scan to the most recently marked position.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
The <function>amrestrpos</> function need only be provided if the access
|
|
|
|
method supports ordered scans. If it doesn't,
|
|
|
|
the <structfield>amrestrpos</> field in its <structname>IndexAmRoutine</>
|
|
|
|
struct may be set to NULL.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<sect1 id="index-scanning">
|
|
|
|
<title>Index Scanning</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
In an index scan, the index access method is responsible for regurgitating
|
|
|
|
the TIDs of all the tuples it has been told about that match the
|
|
|
|
<firstterm>scan keys</>. The access method is <emphasis>not</> involved in
|
|
|
|
actually fetching those tuples from the index's parent table, nor in
|
|
|
|
determining whether they pass the scan's time qualification test or other
|
|
|
|
conditions.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
A scan key is the internal representation of a <literal>WHERE</> clause of
|
|
|
|
the form <replaceable>index_key</> <replaceable>operator</>
|
|
|
|
<replaceable>constant</>, where the index key is one of the columns of the
|
2006-12-23 01:43:13 +01:00
|
|
|
index and the operator is one of the members of the operator family
|
2005-02-13 04:04:15 +01:00
|
|
|
associated with that index column. An index scan has zero or more scan
|
|
|
|
keys, which are implicitly ANDed — the returned tuples are expected
|
|
|
|
to satisfy all the indicated conditions.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2008-04-14 19:05:34 +02:00
|
|
|
The access method can report that the index is <firstterm>lossy</>, or
|
|
|
|
requires rechecks, for a particular query. This implies that the index
|
|
|
|
scan will return all the entries that pass the scan key, plus possibly
|
|
|
|
additional entries that do not. The core system's index-scan machinery
|
|
|
|
will then apply the index conditions again to the heap tuple to verify
|
|
|
|
whether or not it really should be selected. If the recheck option is not
|
|
|
|
specified, the index scan must return exactly the set of matching entries.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Note that it is entirely up to the access method to ensure that it
|
|
|
|
correctly finds all and only the entries passing all the given scan keys.
|
|
|
|
Also, the core system will simply hand off all the <literal>WHERE</>
|
2006-12-23 01:43:13 +01:00
|
|
|
clauses that match the index keys and operator families, without any
|
2005-02-13 04:04:15 +01:00
|
|
|
semantic analysis to determine whether they are redundant or
|
|
|
|
contradictory. As an example, given
|
|
|
|
<literal>WHERE x > 4 AND x > 14</> where <literal>x</> is a b-tree
|
|
|
|
indexed column, it is left to the b-tree <function>amrescan</> function
|
|
|
|
to realize that the first scan key is redundant and can be discarded.
|
|
|
|
The extent of preprocessing needed during <function>amrescan</> will
|
|
|
|
depend on the extent to which the index access method needs to reduce
|
|
|
|
the scan keys to a <quote>normalized</> form.
|
|
|
|
</para>
|
|
|
|
|
2007-01-21 00:13:01 +01:00
|
|
|
<para>
|
|
|
|
Some access methods return index entries in a well-defined order, others
|
2010-12-04 05:49:06 +01:00
|
|
|
do not. There are actually two different ways that an access method can
|
|
|
|
support sorted output:
|
|
|
|
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Access methods that always return entries in the natural ordering
|
|
|
|
of their data (such as btree) should set
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
<structfield>amcanorder</> to true.
|
2010-12-04 05:49:06 +01:00
|
|
|
Currently, such access methods must use btree-compatible strategy
|
|
|
|
numbers for their equality and ordering operators.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Access methods that support ordering operators should set
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
<structfield>amcanorderbyop</> to true.
|
2010-12-04 05:49:06 +01:00
|
|
|
This indicates that the index is capable of returning entries in
|
|
|
|
an order satisfying <literal>ORDER BY</> <replaceable>index_key</>
|
|
|
|
<replaceable>operator</> <replaceable>constant</>. Scan modifiers
|
|
|
|
of that form can be passed to <function>amrescan</> as described
|
|
|
|
previously.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
2007-01-21 00:13:01 +01:00
|
|
|
</para>
|
|
|
|
|
2005-02-13 04:04:15 +01:00
|
|
|
<para>
|
|
|
|
The <function>amgettuple</> function has a <literal>direction</> argument,
|
|
|
|
which can be either <literal>ForwardScanDirection</> (the normal case)
|
|
|
|
or <literal>BackwardScanDirection</>. If the first call after
|
|
|
|
<function>amrescan</> specifies <literal>BackwardScanDirection</>, then the
|
|
|
|
set of matching index entries is to be scanned back-to-front rather than in
|
|
|
|
the normal front-to-back direction, so <function>amgettuple</> must return
|
|
|
|
the last matching tuple in the index, rather than the first one as it
|
|
|
|
normally would. (This will only occur for access
|
2010-12-04 05:49:06 +01:00
|
|
|
methods that set <structfield>amcanorder</> to true.) After the
|
2005-02-13 04:04:15 +01:00
|
|
|
first call, <function>amgettuple</> must be prepared to advance the scan in
|
2008-10-18 00:10:30 +02:00
|
|
|
either direction from the most recently returned entry. (But if
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
<structfield>amcanbackward</> is false, all subsequent
|
2008-10-18 00:10:30 +02:00
|
|
|
calls will have the same direction as the first one.)
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2008-10-18 00:10:30 +02:00
|
|
|
Access methods that support ordered scans must support <quote>marking</> a
|
|
|
|
position in a scan and later returning to the marked position. The same
|
|
|
|
position might be restored multiple times. However, only one position need
|
|
|
|
be remembered per scan; a new <function>ammarkpos</> call overrides the
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
previously marked position. An access method that does not support ordered
|
|
|
|
scans need not provide <function>ammarkpos</> and <function>amrestrpos</>
|
|
|
|
functions in <structname>IndexAmRoutine</>; set those pointers to NULL
|
|
|
|
instead.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Both the scan position and the mark position (if any) must be maintained
|
|
|
|
consistently in the face of concurrent insertions or deletions in the
|
|
|
|
index. It is OK if a freshly-inserted entry is not returned by a scan that
|
|
|
|
would have found the entry if it had existed when the scan started, or for
|
|
|
|
the scan to return such an entry upon rescanning or backing
|
|
|
|
up even though it had not been returned the first time through. Similarly,
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
a concurrent delete might or might not be reflected in the results of a scan.
|
2005-02-13 04:04:15 +01:00
|
|
|
What is important is that insertions or deletions not cause the scan to
|
|
|
|
miss or multiply return entries that were not themselves being inserted or
|
2006-07-31 22:09:10 +02:00
|
|
|
deleted.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
2011-10-08 02:13:02 +02:00
|
|
|
<para>
|
|
|
|
If the index stores the original indexed data values (and not some lossy
|
|
|
|
representation of them), it is useful to support index-only scans, in
|
|
|
|
which the index returns the actual data not just the TID of the heap tuple.
|
|
|
|
This will only work if the visibility map shows that the TID is on an
|
|
|
|
all-visible page; else the heap tuple must be visited anyway to check
|
|
|
|
MVCC visibility. But that is no concern of the access method's.
|
|
|
|
</para>
|
|
|
|
|
2005-03-28 01:53:05 +02:00
|
|
|
<para>
|
2009-03-24 21:17:18 +01:00
|
|
|
Instead of using <function>amgettuple</>, an index scan can be done with
|
2008-04-11 00:25:26 +02:00
|
|
|
<function>amgetbitmap</> to fetch all tuples in one call. This can be
|
2005-03-28 01:53:05 +02:00
|
|
|
noticeably more efficient than <function>amgettuple</> because it allows
|
|
|
|
avoiding lock/unlock cycles within the access method. In principle
|
2008-04-11 00:25:26 +02:00
|
|
|
<function>amgetbitmap</> should have the same effects as repeated
|
2005-03-28 01:53:05 +02:00
|
|
|
<function>amgettuple</> calls, but we impose several restrictions to
|
2009-03-24 21:17:18 +01:00
|
|
|
simplify matters. First of all, <function>amgetbitmap</> returns all
|
|
|
|
tuples at once and marking or restoring scan positions isn't
|
2008-04-11 00:25:26 +02:00
|
|
|
supported. Secondly, the tuples are returned in a bitmap which doesn't
|
|
|
|
have any specific ordering, which is why <function>amgetbitmap</> doesn't
|
2010-12-04 05:49:06 +01:00
|
|
|
take a <literal>direction</> argument. (Ordering operators will never be
|
2011-10-08 02:13:02 +02:00
|
|
|
supplied for such a scan, either.)
|
|
|
|
Also, there is no provision for index-only scans with
|
|
|
|
<function>amgetbitmap</>, since there is no way to return the contents of
|
|
|
|
index tuples.
|
|
|
|
Finally, <function>amgetbitmap</>
|
2008-04-11 00:25:26 +02:00
|
|
|
does not guarantee any locking of the returned tuples, with implications
|
2005-03-28 01:53:05 +02:00
|
|
|
spelled out in <xref linkend="index-locking">.
|
|
|
|
</para>
|
|
|
|
|
2009-03-06 00:06:45 +01:00
|
|
|
<para>
|
|
|
|
Note that it is permitted for an access method to implement only
|
|
|
|
<function>amgetbitmap</> and not <function>amgettuple</>, or vice versa,
|
|
|
|
if its internal implementation is unsuited to one API or the other.
|
|
|
|
</para>
|
|
|
|
|
2005-02-13 04:04:15 +01:00
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<sect1 id="index-locking">
|
|
|
|
<title>Index Locking Considerations</title>
|
|
|
|
|
|
|
|
<para>
|
2006-07-31 22:09:10 +02:00
|
|
|
Index access methods must handle concurrent updates
|
|
|
|
of the index by multiple processes.
|
|
|
|
The core <productname>PostgreSQL</productname> system obtains
|
2005-02-13 04:04:15 +01:00
|
|
|
<literal>AccessShareLock</> on the index during an index scan, and
|
2006-07-31 22:09:10 +02:00
|
|
|
<literal>RowExclusiveLock</> when updating the index (including plain
|
2010-02-08 05:33:55 +01:00
|
|
|
<command>VACUUM</>). Since these lock types do not conflict, the access
|
|
|
|
method is responsible for handling any fine-grained locking it might need.
|
|
|
|
An exclusive lock on the index as a whole will be taken only during index
|
|
|
|
creation, destruction, or <command>REINDEX</>.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Building an index type that supports concurrent updates usually requires
|
|
|
|
extensive and subtle analysis of the required behavior. For the b-tree
|
|
|
|
and hash index types, you can read about the design decisions involved in
|
|
|
|
<filename>src/backend/access/nbtree/README</> and
|
|
|
|
<filename>src/backend/access/hash/README</>.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Aside from the index's own internal consistency requirements, concurrent
|
|
|
|
updates create issues about consistency between the parent table (the
|
|
|
|
<firstterm>heap</>) and the index. Because
|
2009-03-24 21:17:18 +01:00
|
|
|
<productname>PostgreSQL</productname> separates accesses
|
2005-02-13 04:04:15 +01:00
|
|
|
and updates of the heap from those of the index, there are windows in
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
which the index might be inconsistent with the heap. We handle this problem
|
2005-02-13 04:04:15 +01:00
|
|
|
with the following rules:
|
|
|
|
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
A new heap entry is made before making its index entries. (Therefore
|
|
|
|
a concurrent index scan is likely to fail to see the heap entry.
|
|
|
|
This is okay because the index reader would be uninterested in an
|
|
|
|
uncommitted row anyway. But see <xref linkend="index-unique-checks">.)
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
When a heap entry is to be deleted (by <command>VACUUM</>), all its
|
|
|
|
index entries must be removed first.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2006-07-31 22:09:10 +02:00
|
|
|
An index scan must maintain a pin
|
2005-02-13 04:04:15 +01:00
|
|
|
on the index page holding the item last returned by
|
|
|
|
<function>amgettuple</>, and <function>ambulkdelete</> cannot delete
|
|
|
|
entries from pages that are pinned by other backends. The need
|
|
|
|
for this rule is explained below.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
|
2006-07-31 22:09:10 +02:00
|
|
|
Without the third rule, it is possible for an index reader to
|
2005-02-13 04:04:15 +01:00
|
|
|
see an index entry just before it is removed by <command>VACUUM</>, and
|
|
|
|
then to arrive at the corresponding heap entry after that was removed by
|
2006-07-31 22:09:10 +02:00
|
|
|
<command>VACUUM</>.
|
2005-02-13 04:04:15 +01:00
|
|
|
This creates no serious problems if that item
|
|
|
|
number is still unused when the reader reaches it, since an empty
|
|
|
|
item slot will be ignored by <function>heap_fetch()</>. But what if a
|
|
|
|
third backend has already re-used the item slot for something else?
|
|
|
|
When using an MVCC-compliant snapshot, there is no problem because
|
|
|
|
the new occupant of the slot is certain to be too new to pass the
|
|
|
|
snapshot test. However, with a non-MVCC-compliant snapshot (such as
|
Use an MVCC snapshot, rather than SnapshotNow, for catalog scans.
SnapshotNow scans have the undesirable property that, in the face of
concurrent updates, the scan can fail to see either the old or the new
versions of the row. In many cases, we work around this by requiring
DDL operations to hold AccessExclusiveLock on the object being
modified; in some cases, the existing locking is inadequate and random
failures occur as a result. This commit doesn't change anything
related to locking, but will hopefully pave the way to allowing lock
strength reductions in the future.
The major issue has held us back from making this change in the past
is that taking an MVCC snapshot is significantly more expensive than
using a static special snapshot such as SnapshotNow. However, testing
of various worst-case scenarios reveals that this problem is not
severe except under fairly extreme workloads. To mitigate those
problems, we avoid retaking the MVCC snapshot for each new scan;
instead, we take a new snapshot only when invalidation messages have
been processed. The catcache machinery already requires that
invalidation messages be sent before releasing the related heavyweight
lock; else other backends might rely on locally-cached data rather
than scanning the catalog at all. Thus, making snapshot reuse
dependent on the same guarantees shouldn't break anything that wasn't
already subtly broken.
Patch by me. Review by Michael Paquier and Andres Freund.
2013-07-02 15:47:01 +02:00
|
|
|
<literal>SnapshotAny</>), it would be possible to accept and return
|
2005-02-13 04:04:15 +01:00
|
|
|
a row that does not in fact match the scan keys. We could defend
|
|
|
|
against this scenario by requiring the scan keys to be rechecked
|
|
|
|
against the heap row in all cases, but that is too expensive. Instead,
|
|
|
|
we use a pin on an index page as a proxy to indicate that the reader
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
might still be <quote>in flight</> from the index entry to the matching
|
2005-02-13 04:04:15 +01:00
|
|
|
heap entry. Making <function>ambulkdelete</> block on such a pin ensures
|
|
|
|
that <command>VACUUM</> cannot delete the heap entry before the reader
|
2005-11-05 00:14:02 +01:00
|
|
|
is done with it. This solution costs little in run time, and adds blocking
|
2005-02-13 04:04:15 +01:00
|
|
|
overhead only in the rare cases where there actually is a conflict.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
This solution requires that index scans be <quote>synchronous</>: we have
|
|
|
|
to fetch each heap tuple immediately after scanning the corresponding index
|
|
|
|
entry. This is expensive for a number of reasons. An
|
|
|
|
<quote>asynchronous</> scan in which we collect many TIDs from the index,
|
|
|
|
and only visit the heap tuples sometime later, requires much less index
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
locking overhead and can allow a more efficient heap access pattern.
|
2005-02-13 04:04:15 +01:00
|
|
|
Per the above analysis, we must use the synchronous approach for
|
2005-03-28 01:53:05 +02:00
|
|
|
non-MVCC-compliant snapshots, but an asynchronous scan is workable
|
|
|
|
for a query using an MVCC snapshot.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2008-04-11 00:25:26 +02:00
|
|
|
In an <function>amgetbitmap</> index scan, the access method does not
|
|
|
|
keep an index pin on any of the returned tuples. Therefore
|
2005-03-28 01:53:05 +02:00
|
|
|
it is only safe to use such scans with MVCC-compliant snapshots.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
Implement genuine serializable isolation level.
Until now, our Serializable mode has in fact been what's called Snapshot
Isolation, which allows some anomalies that could not occur in any
serialized ordering of the transactions. This patch fixes that using a
method called Serializable Snapshot Isolation, based on research papers by
Michael J. Cahill (see README-SSI for full references). In Serializable
Snapshot Isolation, transactions run like they do in Snapshot Isolation,
but a predicate lock manager observes the reads and writes performed and
aborts transactions if it detects that an anomaly might occur. This method
produces some false positives, ie. it sometimes aborts transactions even
though there is no anomaly.
To track reads we implement predicate locking, see storage/lmgr/predicate.c.
Whenever a tuple is read, a predicate lock is acquired on the tuple. Shared
memory is finite, so when a transaction takes many tuple-level locks on a
page, the locks are promoted to a single page-level lock, and further to a
single relation level lock if necessary. To lock key values with no matching
tuple, a sequential scan always takes a relation-level lock, and an index
scan acquires a page-level lock that covers the search key, whether or not
there are any matching keys at the moment.
A predicate lock doesn't conflict with any regular locks or with another
predicate locks in the normal sense. They're only used by the predicate lock
manager to detect the danger of anomalies. Only serializable transactions
participate in predicate locking, so there should be no extra overhead for
for other transactions.
Predicate locks can't be released at commit, but must be remembered until
all the transactions that overlapped with it have completed. That means that
we need to remember an unbounded amount of predicate locks, so we apply a
lossy but conservative method of tracking locks for committed transactions.
If we run short of shared memory, we overflow to a new "pg_serial" SLRU
pool.
We don't currently allow Serializable transactions in Hot Standby mode.
That would be hard, because even read-only transactions can cause anomalies
that wouldn't otherwise occur.
Serializable isolation mode now means the new fully serializable level.
Repeatable Read gives you the old Snapshot Isolation level that we have
always had.
Kevin Grittner and Dan Ports, reviewed by Jeff Davis, Heikki Linnakangas and
Anssi Kääriäinen
2011-02-07 22:46:51 +01:00
|
|
|
<para>
|
|
|
|
When the <structfield>ampredlocks</> flag is not set, any scan using that
|
|
|
|
index access method within a serializable transaction will acquire a
|
2013-04-19 05:35:19 +02:00
|
|
|
nonblocking predicate lock on the full index. This will generate a
|
Implement genuine serializable isolation level.
Until now, our Serializable mode has in fact been what's called Snapshot
Isolation, which allows some anomalies that could not occur in any
serialized ordering of the transactions. This patch fixes that using a
method called Serializable Snapshot Isolation, based on research papers by
Michael J. Cahill (see README-SSI for full references). In Serializable
Snapshot Isolation, transactions run like they do in Snapshot Isolation,
but a predicate lock manager observes the reads and writes performed and
aborts transactions if it detects that an anomaly might occur. This method
produces some false positives, ie. it sometimes aborts transactions even
though there is no anomaly.
To track reads we implement predicate locking, see storage/lmgr/predicate.c.
Whenever a tuple is read, a predicate lock is acquired on the tuple. Shared
memory is finite, so when a transaction takes many tuple-level locks on a
page, the locks are promoted to a single page-level lock, and further to a
single relation level lock if necessary. To lock key values with no matching
tuple, a sequential scan always takes a relation-level lock, and an index
scan acquires a page-level lock that covers the search key, whether or not
there are any matching keys at the moment.
A predicate lock doesn't conflict with any regular locks or with another
predicate locks in the normal sense. They're only used by the predicate lock
manager to detect the danger of anomalies. Only serializable transactions
participate in predicate locking, so there should be no extra overhead for
for other transactions.
Predicate locks can't be released at commit, but must be remembered until
all the transactions that overlapped with it have completed. That means that
we need to remember an unbounded amount of predicate locks, so we apply a
lossy but conservative method of tracking locks for committed transactions.
If we run short of shared memory, we overflow to a new "pg_serial" SLRU
pool.
We don't currently allow Serializable transactions in Hot Standby mode.
That would be hard, because even read-only transactions can cause anomalies
that wouldn't otherwise occur.
Serializable isolation mode now means the new fully serializable level.
Repeatable Read gives you the old Snapshot Isolation level that we have
always had.
Kevin Grittner and Dan Ports, reviewed by Jeff Davis, Heikki Linnakangas and
Anssi Kääriäinen
2011-02-07 22:46:51 +01:00
|
|
|
read-write conflict with the insert of any tuple into that index by a
|
|
|
|
concurrent serializable transaction. If certain patterns of read-write
|
|
|
|
conflicts are detected among a set of concurrent serializable
|
2011-06-29 08:26:14 +02:00
|
|
|
transactions, one of those transactions may be canceled to protect data
|
Implement genuine serializable isolation level.
Until now, our Serializable mode has in fact been what's called Snapshot
Isolation, which allows some anomalies that could not occur in any
serialized ordering of the transactions. This patch fixes that using a
method called Serializable Snapshot Isolation, based on research papers by
Michael J. Cahill (see README-SSI for full references). In Serializable
Snapshot Isolation, transactions run like they do in Snapshot Isolation,
but a predicate lock manager observes the reads and writes performed and
aborts transactions if it detects that an anomaly might occur. This method
produces some false positives, ie. it sometimes aborts transactions even
though there is no anomaly.
To track reads we implement predicate locking, see storage/lmgr/predicate.c.
Whenever a tuple is read, a predicate lock is acquired on the tuple. Shared
memory is finite, so when a transaction takes many tuple-level locks on a
page, the locks are promoted to a single page-level lock, and further to a
single relation level lock if necessary. To lock key values with no matching
tuple, a sequential scan always takes a relation-level lock, and an index
scan acquires a page-level lock that covers the search key, whether or not
there are any matching keys at the moment.
A predicate lock doesn't conflict with any regular locks or with another
predicate locks in the normal sense. They're only used by the predicate lock
manager to detect the danger of anomalies. Only serializable transactions
participate in predicate locking, so there should be no extra overhead for
for other transactions.
Predicate locks can't be released at commit, but must be remembered until
all the transactions that overlapped with it have completed. That means that
we need to remember an unbounded amount of predicate locks, so we apply a
lossy but conservative method of tracking locks for committed transactions.
If we run short of shared memory, we overflow to a new "pg_serial" SLRU
pool.
We don't currently allow Serializable transactions in Hot Standby mode.
That would be hard, because even read-only transactions can cause anomalies
that wouldn't otherwise occur.
Serializable isolation mode now means the new fully serializable level.
Repeatable Read gives you the old Snapshot Isolation level that we have
always had.
Kevin Grittner and Dan Ports, reviewed by Jeff Davis, Heikki Linnakangas and
Anssi Kääriäinen
2011-02-07 22:46:51 +01:00
|
|
|
integrity. When the flag is set, it indicates that the index access
|
|
|
|
method implements finer-grained predicate locking, which will tend to
|
|
|
|
reduce the frequency of such transaction cancellations.
|
|
|
|
</para>
|
|
|
|
|
2005-02-13 04:04:15 +01:00
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<sect1 id="index-unique-checks">
|
|
|
|
<title>Index Uniqueness Checks</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<productname>PostgreSQL</productname> enforces SQL uniqueness constraints
|
|
|
|
using <firstterm>unique indexes</>, which are indexes that disallow
|
|
|
|
multiple entries with identical keys. An access method that supports this
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
feature sets <structfield>amcanunique</> true.
|
2005-02-13 04:04:15 +01:00
|
|
|
(At present, only b-tree supports it.)
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Because of MVCC, it is always necessary to allow duplicate entries to
|
|
|
|
exist physically in an index: the entries might refer to successive
|
|
|
|
versions of a single logical row. The behavior we actually want to
|
|
|
|
enforce is that no MVCC snapshot could include two rows with equal
|
|
|
|
index keys. This breaks down into the following cases that must be
|
|
|
|
checked when inserting a new row into a unique index:
|
|
|
|
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
If a conflicting valid row has been deleted by the current transaction,
|
|
|
|
it's okay. (In particular, since an UPDATE always deletes the old row
|
|
|
|
version before inserting the new version, this will allow an UPDATE on
|
|
|
|
a row without changing the key.)
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
If a conflicting row has been inserted by an as-yet-uncommitted
|
|
|
|
transaction, the would-be inserter must wait to see if that transaction
|
|
|
|
commits. If it rolls back then there is no conflict. If it commits
|
|
|
|
without deleting the conflicting row again, there is a uniqueness
|
|
|
|
violation. (In practice we just wait for the other transaction to
|
|
|
|
end and then redo the visibility check in toto.)
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Similarly, if a conflicting valid row has been deleted by an
|
|
|
|
as-yet-uncommitted transaction, the would-be inserter must wait
|
|
|
|
for that transaction to commit or abort, and then repeat the test.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
</para>
|
|
|
|
|
2006-08-25 06:06:58 +02:00
|
|
|
<para>
|
2009-07-29 22:56:21 +02:00
|
|
|
Furthermore, immediately before reporting a uniqueness violation
|
2006-08-25 06:06:58 +02:00
|
|
|
according to the above rules, the access method must recheck the
|
|
|
|
liveness of the row being inserted. If it is committed dead then
|
2009-07-29 22:56:21 +02:00
|
|
|
no violation should be reported. (This case cannot occur during the
|
2006-08-25 06:06:58 +02:00
|
|
|
ordinary scenario of inserting a row that's just been created by
|
|
|
|
the current transaction. It can happen during
|
2009-03-24 21:17:18 +01:00
|
|
|
<command>CREATE UNIQUE INDEX CONCURRENTLY</>, however.)
|
2006-08-25 06:06:58 +02:00
|
|
|
</para>
|
|
|
|
|
2005-02-13 04:04:15 +01:00
|
|
|
<para>
|
|
|
|
We require the index access method to apply these tests itself, which
|
|
|
|
means that it must reach into the heap to check the commit status of
|
|
|
|
any row that is shown to have a duplicate key according to the index
|
|
|
|
contents. This is without a doubt ugly and non-modular, but it saves
|
|
|
|
redundant work: if we did a separate probe then the index lookup for
|
|
|
|
a conflicting row would be essentially repeated while finding the place to
|
|
|
|
insert the new row's index entry. What's more, there is no obvious way
|
|
|
|
to avoid race conditions unless the conflict check is an integral part
|
|
|
|
of insertion of the new index entry.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2009-07-29 22:56:21 +02:00
|
|
|
If the unique constraint is deferrable, there is additional complexity:
|
|
|
|
we need to be able to insert an index entry for a new row, but defer any
|
|
|
|
uniqueness-violation error until end of statement or even later. To
|
|
|
|
avoid unnecessary repeat searches of the index, the index access method
|
|
|
|
should do a preliminary uniqueness check during the initial insertion.
|
|
|
|
If this shows that there is definitely no conflicting live tuple, we
|
|
|
|
are done. Otherwise, we schedule a recheck to occur when it is time to
|
|
|
|
enforce the constraint. If, at the time of the recheck, both the inserted
|
|
|
|
tuple and some other tuple with the same key are live, then the error
|
|
|
|
must be reported. (Note that for this purpose, <quote>live</> actually
|
|
|
|
means <quote>any tuple in the index entry's HOT chain is live</>.)
|
|
|
|
To implement this, the <function>aminsert</> function is passed a
|
|
|
|
<literal>checkUnique</> parameter having one of the following values:
|
|
|
|
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<literal>UNIQUE_CHECK_NO</> indicates that no uniqueness checking
|
|
|
|
should be done (this is not a unique index).
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<literal>UNIQUE_CHECK_YES</> indicates that this is a non-deferrable
|
|
|
|
unique index, and the uniqueness check must be done immediately, as
|
|
|
|
described above.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<literal>UNIQUE_CHECK_PARTIAL</> indicates that the unique
|
|
|
|
constraint is deferrable. <productname>PostgreSQL</productname>
|
|
|
|
will use this mode to insert each row's index entry. The access
|
|
|
|
method must allow duplicate entries into the index, and report any
|
|
|
|
potential duplicates by returning FALSE from <function>aminsert</>.
|
|
|
|
For each row for which FALSE is returned, a deferred recheck will
|
|
|
|
be scheduled.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The access method must identify any rows which might violate the
|
|
|
|
unique constraint, but it is not an error for it to report false
|
|
|
|
positives. This allows the check to be done without waiting for other
|
|
|
|
transactions to finish; conflicts reported here are not treated as
|
|
|
|
errors and will be rechecked later, by which time they may no longer
|
|
|
|
be conflicts.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<literal>UNIQUE_CHECK_EXISTING</> indicates that this is a deferred
|
|
|
|
recheck of a row that was reported as a potential uniqueness violation.
|
|
|
|
Although this is implemented by calling <function>aminsert</>, the
|
|
|
|
access method must <emphasis>not</> insert a new index entry in this
|
|
|
|
case. The index entry is already present. Rather, the access method
|
|
|
|
must check to see if there is another live index entry. If so, and
|
|
|
|
if the target row is also still live, report error.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
It is recommended that in a <literal>UNIQUE_CHECK_EXISTING</> call,
|
|
|
|
the access method further verify that the target row actually does
|
|
|
|
have an existing entry in the index, and report error if not. This
|
|
|
|
is a good idea because the index tuple values passed to
|
|
|
|
<function>aminsert</> will have been recomputed. If the index
|
|
|
|
definition involves functions that are not really immutable, we
|
|
|
|
might be checking the wrong area of the index. Checking that the
|
|
|
|
target row is found in the recheck verifies that we are scanning
|
|
|
|
for the same tuple values as were used in the original insertion.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<sect1 id="index-cost-estimation">
|
|
|
|
<title>Index Cost Estimation Functions</title>
|
|
|
|
|
|
|
|
<para>
|
2010-12-03 02:50:48 +01:00
|
|
|
The <function>amcostestimate</> function is given information describing
|
|
|
|
a possible index scan, including lists of WHERE and ORDER BY clauses that
|
|
|
|
have been determined to be usable with the index. It must return estimates
|
2005-02-13 04:04:15 +01:00
|
|
|
of the cost of accessing the index and the selectivity of the WHERE
|
|
|
|
clauses (that is, the fraction of parent-table rows that will be
|
|
|
|
retrieved during the index scan). For simple cases, nearly all the
|
|
|
|
work of the cost estimator can be done by calling standard routines
|
2010-08-17 06:37:21 +02:00
|
|
|
in the optimizer; the point of having an <function>amcostestimate</> function is
|
2005-02-13 04:04:15 +01:00
|
|
|
to allow index access methods to provide index-type-specific knowledge,
|
|
|
|
in case it is possible to improve on the standard estimates.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2010-08-17 06:37:21 +02:00
|
|
|
Each <function>amcostestimate</> function must have the signature:
|
2005-02-13 04:04:15 +01:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
void
|
2005-06-06 00:32:58 +02:00
|
|
|
amcostestimate (PlannerInfo *root,
|
2011-12-25 01:03:21 +01:00
|
|
|
IndexPath *path,
|
2012-01-28 01:26:38 +01:00
|
|
|
double loop_count,
|
2005-02-13 04:04:15 +01:00
|
|
|
Cost *indexStartupCost,
|
|
|
|
Cost *indexTotalCost,
|
|
|
|
Selectivity *indexSelectivity,
|
|
|
|
double *indexCorrelation);
|
|
|
|
</programlisting>
|
|
|
|
|
2011-12-25 01:03:21 +01:00
|
|
|
The first three parameters are inputs:
|
2005-02-13 04:04:15 +01:00
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
2010-08-17 06:37:21 +02:00
|
|
|
<term><parameter>root</></term>
|
2005-02-13 04:04:15 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2005-06-06 00:32:58 +02:00
|
|
|
The planner's information about the query being processed.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2011-12-25 01:03:21 +01:00
|
|
|
<term><parameter>path</></term>
|
2005-02-13 04:04:15 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2011-12-25 01:03:21 +01:00
|
|
|
The index access path being considered. All fields except cost and
|
|
|
|
selectivity values are valid.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2006-06-06 19:59:58 +02:00
|
|
|
|
|
|
|
<varlistentry>
|
2012-01-28 01:26:38 +01:00
|
|
|
<term><parameter>loop_count</></term>
|
2006-06-06 19:59:58 +02:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2012-01-28 01:26:38 +01:00
|
|
|
The number of repetitions of the index scan that should be factored
|
|
|
|
into the cost estimates. This will typically be greater than one when
|
|
|
|
considering a parameterized scan for use in the inside of a nestloop
|
|
|
|
join. Note that the cost estimates should still be for just one scan;
|
|
|
|
a larger <parameter>loop_count</> means that it may be appropriate
|
|
|
|
to allow for some caching effects across multiple scans.
|
2006-06-06 19:59:58 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2005-02-13 04:04:15 +01:00
|
|
|
</variablelist>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The last four parameters are pass-by-reference outputs:
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
2010-08-17 06:37:21 +02:00
|
|
|
<term><parameter>*indexStartupCost</></term>
|
2005-02-13 04:04:15 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Set to cost of index start-up processing
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2010-08-17 06:37:21 +02:00
|
|
|
<term><parameter>*indexTotalCost</></term>
|
2005-02-13 04:04:15 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Set to total cost of index processing
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2010-08-17 06:37:21 +02:00
|
|
|
<term><parameter>*indexSelectivity</></term>
|
2005-02-13 04:04:15 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Set to index selectivity
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2010-08-17 06:37:21 +02:00
|
|
|
<term><parameter>*indexCorrelation</></term>
|
2005-02-13 04:04:15 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Set to correlation coefficient between index scan order and
|
|
|
|
underlying table's order
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Note that cost estimate functions must be written in C, not in SQL or
|
|
|
|
any available procedural language, because they must access internal
|
|
|
|
data structures of the planner/optimizer.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2006-06-05 04:49:58 +02:00
|
|
|
The index access costs should be computed using the parameters used by
|
2005-03-28 01:53:05 +02:00
|
|
|
<filename>src/backend/optimizer/path/costsize.c</filename>: a sequential
|
2006-06-05 04:49:58 +02:00
|
|
|
disk block fetch has cost <varname>seq_page_cost</>, a nonsequential fetch
|
|
|
|
has cost <varname>random_page_cost</>, and the cost of processing one index
|
|
|
|
row should usually be taken as <varname>cpu_index_tuple_cost</>. In
|
|
|
|
addition, an appropriate multiple of <varname>cpu_operator_cost</> should
|
|
|
|
be charged for any comparison operators invoked during index processing
|
2011-12-25 01:03:21 +01:00
|
|
|
(especially evaluation of the indexquals themselves).
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The access costs should include all disk and CPU costs associated with
|
2005-03-28 01:53:05 +02:00
|
|
|
scanning the index itself, but <emphasis>not</> the costs of retrieving or
|
|
|
|
processing the parent-table rows that are identified by the index.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2006-06-05 04:49:58 +02:00
|
|
|
The <quote>start-up cost</quote> is the part of the total scan cost that
|
|
|
|
must be expended before we can begin to fetch the first row. For most
|
|
|
|
indexes this can be taken as zero, but an index type with a high start-up
|
|
|
|
cost might want to set it nonzero.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2010-08-17 06:37:21 +02:00
|
|
|
The <parameter>indexSelectivity</> should be set to the estimated fraction of the parent
|
2005-02-13 04:04:15 +01:00
|
|
|
table rows that will be retrieved during the index scan. In the case
|
2008-04-14 19:05:34 +02:00
|
|
|
of a lossy query, this will typically be higher than the fraction of
|
2005-02-13 04:04:15 +01:00
|
|
|
rows that actually pass the given qual conditions.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2010-08-17 06:37:21 +02:00
|
|
|
The <parameter>indexCorrelation</> should be set to the correlation (ranging between
|
2005-02-13 04:04:15 +01:00
|
|
|
-1.0 and 1.0) between the index order and the table order. This is used
|
|
|
|
to adjust the estimate for the cost of fetching rows from the parent
|
|
|
|
table.
|
|
|
|
</para>
|
|
|
|
|
2006-06-06 19:59:58 +02:00
|
|
|
<para>
|
2012-01-28 01:26:38 +01:00
|
|
|
When <parameter>loop_count</> is greater than one, the returned numbers
|
|
|
|
should be averages expected for any one scan of the index.
|
2006-06-06 19:59:58 +02:00
|
|
|
</para>
|
|
|
|
|
2005-02-13 04:04:15 +01:00
|
|
|
<procedure>
|
|
|
|
<title>Cost Estimation</title>
|
|
|
|
<para>
|
|
|
|
A typical cost estimator will proceed as follows:
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<step>
|
|
|
|
<para>
|
|
|
|
Estimate and return the fraction of parent-table rows that will be visited
|
|
|
|
based on the given qual conditions. In the absence of any index-type-specific
|
|
|
|
knowledge, use the standard optimizer function <function>clauselist_selectivity()</function>:
|
|
|
|
|
|
|
|
<programlisting>
|
2011-12-25 01:03:21 +01:00
|
|
|
*indexSelectivity = clauselist_selectivity(root, path->indexquals,
|
|
|
|
path->indexinfo->rel->relid,
|
2008-08-14 20:48:00 +02:00
|
|
|
JOIN_INNER, NULL);
|
2005-02-13 04:04:15 +01:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</step>
|
|
|
|
|
|
|
|
<step>
|
|
|
|
<para>
|
|
|
|
Estimate the number of index rows that will be visited during the
|
2010-08-17 06:37:21 +02:00
|
|
|
scan. For many index types this is the same as <parameter>indexSelectivity</> times
|
2005-02-13 04:04:15 +01:00
|
|
|
the number of rows in the index, but it might be more. (Note that the
|
2011-12-25 01:03:21 +01:00
|
|
|
index's size in pages and rows is available from the
|
|
|
|
<literal>path->indexinfo</> struct.)
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
</step>
|
|
|
|
|
|
|
|
<step>
|
|
|
|
<para>
|
|
|
|
Estimate the number of index pages that will be retrieved during the scan.
|
2010-08-17 06:37:21 +02:00
|
|
|
This might be just <parameter>indexSelectivity</> times the index's size in pages.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
</step>
|
|
|
|
|
|
|
|
<step>
|
|
|
|
<para>
|
|
|
|
Compute the index access cost. A generic estimator might do this:
|
|
|
|
|
|
|
|
<programlisting>
|
2010-07-29 21:34:41 +02:00
|
|
|
/*
|
|
|
|
* Our generic assumption is that the index pages will be read
|
|
|
|
* sequentially, so they cost seq_page_cost each, not random_page_cost.
|
|
|
|
* Also, we charge for evaluation of the indexquals at each index row.
|
|
|
|
* All the costs are assumed to be paid incrementally during the scan.
|
|
|
|
*/
|
2011-12-25 01:03:21 +01:00
|
|
|
cost_qual_eval(&index_qual_cost, path->indexquals, root);
|
2010-07-29 21:34:41 +02:00
|
|
|
*indexStartupCost = index_qual_cost.startup;
|
|
|
|
*indexTotalCost = seq_page_cost * numIndexPages +
|
|
|
|
(cpu_index_tuple_cost + index_qual_cost.per_tuple) * numIndexTuples;
|
2005-02-13 04:04:15 +01:00
|
|
|
</programlisting>
|
2006-06-06 19:59:58 +02:00
|
|
|
|
|
|
|
However, the above does not account for amortization of index reads
|
2012-01-28 01:26:38 +01:00
|
|
|
across repeated index scans.
|
2005-02-13 04:04:15 +01:00
|
|
|
</para>
|
|
|
|
</step>
|
|
|
|
|
|
|
|
<step>
|
|
|
|
<para>
|
|
|
|
Estimate the index correlation. For a simple ordered index on a single
|
|
|
|
field, this can be retrieved from pg_statistic. If the correlation
|
|
|
|
is not known, the conservative estimate is zero (no correlation).
|
|
|
|
</para>
|
|
|
|
</step>
|
|
|
|
</procedure>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Examples of cost estimator functions can be found in
|
|
|
|
<filename>src/backend/utils/adt/selfuncs.c</filename>.
|
|
|
|
</para>
|
|
|
|
</sect1>
|
|
|
|
</chapter>
|