row), then on success it must also check
    <literal>scan->xs_want_itup</>, and if that is true it must return
    the original indexed data for the index entry, in the form of an
-   <structname>IndexTuple</> pointer stored at <literal>scan->xs_itup</>.
+   <structname>IndexTuple</> pointer stored at <literal>scan->xs_itup</>,
+   with tuple descriptor <literal>scan->xs_itupdesc</>.
    (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</>
 
 
    /*
     * We don't know yet whether the scan will be index-only, so we do not
-    * allocate the tuple workspace arrays until btrescan.
+    * allocate the tuple workspace arrays until btrescan.  However, we set up
+    * scan->xs_itupdesc whether we'll need it or not, since that's so cheap.
     */
    so->currTuples = so->markTuples = NULL;
    so->currPos.nextTupleOffset = 0;
    so->markPos.nextTupleOffset = 0;
 
+   scan->xs_itupdesc = RelationGetDescr(rel);
+
    scan->opaque = so;
 
    PG_RETURN_POINTER(scan);
 
 
 static TupleTableSlot *IndexOnlyNext(IndexOnlyScanState *node);
 static void StoreIndexTuple(TupleTableSlot *slot, IndexTuple itup,
-                           Relation indexRel);
+               TupleDesc itupdesc);
 
 
 /* ----------------------------------------------------------------
        /*
         * Fill the scan tuple slot with data from the index.
         */
-       StoreIndexTuple(slot, scandesc->xs_itup, scandesc->indexRelation);
+       StoreIndexTuple(slot, scandesc->xs_itup, scandesc->xs_itupdesc);
 
        /*
         * If the index was lossy, we have to recheck the index quals.
  * right now we don't need it elsewhere.
  */
 static void
-StoreIndexTuple(TupleTableSlot *slot, IndexTuple itup, Relation indexRel)
+StoreIndexTuple(TupleTableSlot *slot, IndexTuple itup, TupleDesc itupdesc)
 {
-   TupleDesc   indexDesc = RelationGetDescr(indexRel);
-   int         nindexatts = indexDesc->natts;
+   int         nindexatts = itupdesc->natts;
    Datum      *values = slot->tts_values;
    bool       *isnull = slot->tts_isnull;
    int         i;
 
    /*
-    * Note: we must use the index relation's tupdesc in index_getattr, not
+    * Note: we must use the tupdesc supplied by the AM in index_getattr, not
     * the slot's tupdesc, in case the latter has different datatypes (this
     * happens for btree name_ops in particular).  They'd better have the same
-    * number of columns though.
+    * number of columns though, as well as being datatype-compatible which
+    * is something we can't so easily check.
     */
    Assert(slot->tts_tupleDescriptor->natts == nindexatts);
 
    ExecClearTuple(slot);
    for (i = 0; i < nindexatts; i++)
-       values[i] = index_getattr(itup, i + 1, indexDesc, &isnull[i]);
+       values[i] = index_getattr(itup, i + 1, itupdesc, &isnull[i]);
    ExecStoreVirtualTuple(slot);
 }
 
 
 #include "access/genam.h"
 #include "access/heapam.h"
 #include "access/itup.h"
+#include "access/tupdesc.h"
 
 
 typedef struct HeapScanDescData
 
    /* in an index-only scan, this is valid after a successful amgettuple */
    IndexTuple  xs_itup;        /* index tuple returned by AM */
+   TupleDesc   xs_itupdesc;    /* rowtype descriptor of xs_itup */
 
    /* xs_ctup/xs_cbuf/xs_recheck are valid after a successful index_getnext */
    HeapTupleData xs_ctup;      /* current heap tuple, if any */