]> granicus.if.org Git - postgresql/blobdiff - src/backend/access/nbtree/nbtutils.c
CREATE INDEX ... INCLUDING (column[, ...])
[postgresql] / src / backend / access / nbtree / nbtutils.c
index 83c553ca279e2db0ef1590ed38409666b5e9e9e0..8c5509f4a2c036a2f873cd8ffa67f35943aac84d 100644 (file)
@@ -63,17 +63,26 @@ _bt_mkscankey(Relation rel, IndexTuple itup)
 {
        ScanKey         skey;
        TupleDesc       itupdesc;
-       int                     natts;
+       int                     indnatts,
+                               indnkeyatts;
        int16      *indoption;
        int                     i;
 
        itupdesc = RelationGetDescr(rel);
-       natts = RelationGetNumberOfAttributes(rel);
+       indnatts = IndexRelationGetNumberOfAttributes(rel);
+       indnkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
        indoption = rel->rd_indoption;
 
-       skey = (ScanKey) palloc(natts * sizeof(ScanKeyData));
+       Assert(indnkeyatts != 0);
+       Assert(indnkeyatts <= indnatts);
 
-       for (i = 0; i < natts; i++)
+       /*
+        * We'll execute search using ScanKey constructed on key columns.
+        * Non key (included) columns must be omitted.
+        */
+       skey = (ScanKey) palloc(indnkeyatts * sizeof(ScanKeyData));
+
+       for (i = 0; i < indnkeyatts; i++)
        {
                FmgrInfo   *procinfo;
                Datum           arg;
@@ -115,16 +124,16 @@ ScanKey
 _bt_mkscankey_nodata(Relation rel)
 {
        ScanKey         skey;
-       int                     natts;
+       int                     indnkeyatts;
        int16      *indoption;
        int                     i;
 
-       natts = RelationGetNumberOfAttributes(rel);
+       indnkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
        indoption = rel->rd_indoption;
 
-       skey = (ScanKey) palloc(natts * sizeof(ScanKeyData));
+       skey = (ScanKey) palloc(indnkeyatts * sizeof(ScanKeyData));
 
-       for (i = 0; i < natts; i++)
+       for (i = 0; i < indnkeyatts; i++)
        {
                FmgrInfo   *procinfo;
                int                     flags;