Commit e6dd664d authored by Tom Lane's avatar Tom Lane

Docs: create some user-facing documentation about index-only scans.

We didn't have any real user documentation about how index-only scans
work or how to design indexes to exploit them.  Remedy that.
Per gripe from David Johnston.
parent 6f69b963
...@@ -3406,9 +3406,6 @@ include_dir 'conf.d' ...@@ -3406,9 +3406,6 @@ include_dir 'conf.d'
<varlistentry id="guc-enable-indexonlyscan" xreflabel="enable_indexonlyscan"> <varlistentry id="guc-enable-indexonlyscan" xreflabel="enable_indexonlyscan">
<term><varname>enable_indexonlyscan</varname> (<type>boolean</type>) <term><varname>enable_indexonlyscan</varname> (<type>boolean</type>)
<indexterm>
<primary>index-only scan</primary>
</indexterm>
<indexterm> <indexterm>
<primary><varname>enable_indexonlyscan</> configuration parameter</primary> <primary><varname>enable_indexonlyscan</> configuration parameter</primary>
</indexterm> </indexterm>
...@@ -3416,7 +3413,8 @@ include_dir 'conf.d' ...@@ -3416,7 +3413,8 @@ include_dir 'conf.d'
<listitem> <listitem>
<para> <para>
Enables or disables the query planner's use of index-only-scan plan Enables or disables the query planner's use of index-only-scan plan
types. The default is <literal>on</>. types (see <xref linkend="indexes-index-only-scans">).
The default is <literal>on</>.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
......
...@@ -354,10 +354,11 @@ amvacuumcleanup (IndexVacuumInfo *info, ...@@ -354,10 +354,11 @@ amvacuumcleanup (IndexVacuumInfo *info,
bool bool
amcanreturn (Relation indexRelation, int attno); amcanreturn (Relation indexRelation, int attno);
</programlisting> </programlisting>
Check whether the index can support <firstterm>index-only scans</> on the Check whether the index can support <link
given column, by returning the indexed column values for an index entry in linkend="indexes-index-only-scans"><firstterm>index-only scans</></link> on
the form of an <structname>IndexTuple</structname>. The attribute number the given column, by returning the indexed column values for an index entry
is 1-based, i.e. the first columns attno is 1. Returns TRUE if supported, in the form of an <structname>IndexTuple</structname>. The attribute number
is 1-based, i.e. the first column's attno is 1. Returns TRUE if supported,
else FALSE. If the access method does not support index-only scans at all, else FALSE. If the access method does not support index-only scans at all,
the <structfield>amcanreturn</> field in its <structname>IndexAmRoutine</> the <structfield>amcanreturn</> field in its <structname>IndexAmRoutine</>
struct can be set to NULL. struct can be set to NULL.
...@@ -489,8 +490,8 @@ amgettuple (IndexScanDesc scan, ...@@ -489,8 +490,8 @@ amgettuple (IndexScanDesc scan,
</para> </para>
<para> <para>
If the index supports index-only scans (i.e., If the index supports <link linkend="indexes-index-only-scans">index-only
<function>amcanreturn</function> returns TRUE for it), scans</link> (i.e., <function>amcanreturn</function> returns TRUE for it),
then on success the AM must also check then on success the AM must also check
<literal>scan-&gt;xs_want_itup</>, and if that is true it must return <literal>scan-&gt;xs_want_itup</>, and if that is true it must return
the original indexed data for the index entry, in the form of an the original indexed data for the index entry, in the form of an
...@@ -700,9 +701,10 @@ amrestrpos (IndexScanDesc scan); ...@@ -700,9 +701,10 @@ amrestrpos (IndexScanDesc scan);
<para> <para>
If the index stores the original indexed data values (and not some lossy 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 representation of them), it is useful to
support <link linkend="indexes-index-only-scans">index-only scans</link>, in
which the index returns the actual data not just the TID of the heap tuple. 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 This will only avoid I/O if the visibility map shows that the TID is on an
all-visible page; else the heap tuple must be visited anyway to check 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. MVCC visibility. But that is no concern of the access method's.
</para> </para>
......
This diff is collapsed.
...@@ -102,8 +102,9 @@ ...@@ -102,8 +102,9 @@
</listitem> </listitem>
<listitem> <listitem>
<simpara>To update the visibility map, which speeds up index-only <simpara>To update the visibility map, which speeds
scans.</simpara> up <link linkend="indexes-index-only-scans">index-only
scans</link>.</simpara>
</listitem> </listitem>
<listitem> <listitem>
...@@ -363,9 +364,11 @@ ...@@ -363,9 +364,11 @@
Since <productname>PostgreSQL</productname> indexes don't contain tuple Since <productname>PostgreSQL</productname> indexes don't contain tuple
visibility information, a normal index scan fetches the heap tuple for each visibility information, a normal index scan fetches the heap tuple for each
matching index entry, to check whether it should be seen by the current matching index entry, to check whether it should be seen by the current
transaction. An <firstterm>index-only scan</>, on the other hand, checks transaction.
the visibility map first. If it's known that all tuples on the page are An <link linkend="indexes-index-only-scans"><firstterm>index-only
visible, the heap fetch can be skipped. This is most noticeable on scan</></link>, on the other hand, checks the visibility map first.
If it's known that all tuples on the page are
visible, the heap fetch can be skipped. This is most useful on
large data sets where the visibility map can prevent disk accesses. large data sets where the visibility map can prevent disk accesses.
The visibility map is vastly smaller than the heap, so it can easily be The visibility map is vastly smaller than the heap, so it can easily be
cached even when the heap is very large. cached even when the heap is very large.
......
...@@ -636,9 +636,11 @@ Note that indexes do not have VMs. ...@@ -636,9 +636,11 @@ Note that indexes do not have VMs.
The visibility map stores two bits per heap page. The first bit, if set, The visibility map stores two bits per heap page. The first bit, if set,
indicates that the page is all-visible, or in other words that the page does indicates that the page is all-visible, or in other words that the page does
not contain any tuples that need to be vacuumed. not contain any tuples that need to be vacuumed.
This information can also be used by <firstterm>index-only scans</> to answer This information can also be used
queries using only the index tuple. by <link linkend="indexes-index-only-scans"><firstterm>index-only
scans</></link> to answer queries using only the index tuple.
The second bit, if set, means that all tuples on the page have been frozen. The second bit, if set, means that all tuples on the page have been frozen.
That means that even an anti-wraparound vacuum need not revisit the page.
</para> </para>
<para> <para>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment