Skip to content

Commit 9cebe49

Browse files
committed
Fix fuzzy thinking about amcanmulticol versus amcaninclude.
These flags should be independent: in particular an index AM should be able to say that it supports include columns without necessarily supporting multiple key columns. The included-columns patch got this wrong, possibly aided by the fact that it didn't bother to update the documentation. While here, clarify some text about amcanreturn, which was a little vague about what should happen when amcanreturn reports that only some of the index columns are returnable. Noted while reviewing the SP-GiST included-columns patch, which quite incorrectly (and unsafely) changed SP-GiST to claim amcanmulticol = true as a workaround for this bug. Backpatch to v11 where included columns were introduced.
1 parent a87d780 commit 9cebe49

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

doc/src/sgml/indexam.sgml

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ typedef struct IndexAmRoutine
181181
implications. The requirements of <structfield>amcanunique</structfield>
182182
are discussed in <xref linkend="index-unique-checks"/>.
183183
The <structfield>amcanmulticol</structfield> flag asserts that the
184-
access method supports multicolumn indexes, while
184+
access method supports multi-key-column indexes, while
185185
<structfield>amoptionalkey</structfield> asserts that it allows scans
186186
where no indexable restriction clause is given for the first index column.
187187
When <structfield>amcanmulticol</structfield> is false,
@@ -217,6 +217,19 @@ typedef struct IndexAmRoutine
217217
conditions.
218218
</para>
219219

220+
<para>
221+
The <structfield>amcaninclude</structfield> flag indicates whether the
222+
access method supports <quote>included</quote> columns, that is it can
223+
store (without processing) additional columns beyond the key column(s).
224+
The requirements of the preceding paragraph apply only to the key
225+
columns. In particular, the combination
226+
of <structfield>amcanmulticol</structfield>=<literal>false</literal>
227+
and <structfield>amcaninclude</structfield>=<literal>true</literal> is
228+
sensible: it means that there can only be one key column, but there can
229+
also be included column(s). Also, included columns must be allowed to be
230+
null, independently of <structfield>amoptionalkey</structfield>.
231+
</para>
232+
220233
</sect1>
221234

222235
<sect1 id="index-functions">
@@ -368,10 +381,13 @@ amcanreturn (Relation indexRelation, int attno);
368381
</programlisting>
369382
Check whether the index can support <link
370383
linkend="indexes-index-only-scans"><firstterm>index-only scans</firstterm></link> on
371-
the given column, by returning the indexed column values for an index entry
372-
in the form of an <structname>IndexTuple</structname>. The attribute number
373-
is 1-based, i.e., the first column's attno is 1. Returns true if supported,
374-
else false. If the access method does not support index-only scans at all,
384+
the given column, by returning the column's original indexed value.
385+
The attribute number is 1-based, i.e., the first column's attno is 1.
386+
Returns true if supported, else false.
387+
This function should always return true for included columns
388+
(if those are supported), since there's little point in an included
389+
column that can't be retrieved.
390+
If the access method does not support index-only scans at all,
375391
the <structfield>amcanreturn</structfield> field in its <structname>IndexAmRoutine</structname>
376392
struct can be set to NULL.
377393
</para>
@@ -461,7 +477,7 @@ amproperty (Oid index_oid, int attno,
461477
core code does not know how to do that and will return NULL. It may
462478
also be advantageous to implement <literal>AMPROP_RETURNABLE</literal> testing,
463479
if that can be done more cheaply than by opening the index and calling
464-
<structfield>amcanreturn</structfield>, which is the core code's default behavior.
480+
<function>amcanreturn</function>, which is the core code's default behavior.
465481
The default behavior should be satisfactory for all other standard
466482
properties.
467483
</para>
@@ -553,10 +569,13 @@ amgettuple (IndexScanDesc scan,
553569

554570
<para>
555571
If the index supports <link linkend="indexes-index-only-scans">index-only
556-
scans</link> (i.e., <function>amcanreturn</function> returns true for it),
572+
scans</link> (i.e., <function>amcanreturn</function> returns true for any
573+
of its columns),
557574
then on success the AM must also check <literal>scan-&gt;xs_want_itup</literal>,
558575
and if that is true it must return the originally indexed data for the
559-
index entry. The data can be returned in the form of an
576+
index entry. Columns for which <function>amcanreturn</function> returns
577+
false can be returned as nulls.
578+
The data can be returned in the form of an
560579
<structname>IndexTuple</structname> pointer stored at <literal>scan-&gt;xs_itup</literal>,
561580
with tuple descriptor <literal>scan-&gt;xs_itupdesc</literal>; or in the form of
562581
a <structname>HeapTuple</structname> pointer stored at <literal>scan-&gt;xs_hitup</literal>,

src/backend/commands/indexcmds.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ DefineIndex(Oid relationId,
402402
list_copy(stmt->indexIncludingParams));
403403
numberOfAttributes = list_length(allIndexParams);
404404

405-
if (numberOfAttributes <= 0)
405+
if (numberOfKeyAttributes <= 0)
406406
ereport(ERROR,
407407
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
408408
errmsg("must specify at least one column")));
@@ -618,7 +618,7 @@ DefineIndex(Oid relationId,
618618
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
619619
errmsg("access method \"%s\" does not support included columns",
620620
accessMethodName)));
621-
if (numberOfAttributes > 1 && !amRoutine->amcanmulticol)
621+
if (numberOfKeyAttributes > 1 && !amRoutine->amcanmulticol)
622622
ereport(ERROR,
623623
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
624624
errmsg("access method \"%s\" does not support multicolumn indexes",

0 commit comments

Comments
 (0)