@@ -1143,9 +1143,14 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
1143
1143
* We support the convention that sk_subtype == InvalidOid means
1144
1144
* the opclass input type; this is a hack to simplify life for
1145
1145
* ScanKeyInit().
1146
+ *
1147
+ * We do not require that included columns have opclass.
1148
+ * So do not try to find the comparison function for these
1149
+ * attributes.
1146
1150
*/
1147
- if (cur -> sk_subtype == rel -> rd_opcintype [i ] ||
1148
- cur -> sk_subtype == InvalidOid )
1151
+ if (i < IndexRelationGetNumberOfKeyAttributes (rel ) &&
1152
+ (cur -> sk_subtype == rel -> rd_opcintype [i ] ||
1153
+ cur -> sk_subtype == InvalidOid ))
1149
1154
{
1150
1155
FmgrInfo * procinfo ;
1151
1156
@@ -1163,14 +1168,27 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
1163
1168
{
1164
1169
RegProcedure cmp_proc ;
1165
1170
1166
- cmp_proc = get_opfamily_proc (rel -> rd_opfamily [i ],
1167
- rel -> rd_opcintype [i ],
1168
- cur -> sk_subtype ,
1169
- BTORDER_PROC );
1170
- if (!RegProcedureIsValid (cmp_proc ))
1171
- elog (ERROR , "missing support function %d(%u,%u) for attribute %d of index \"%s\"" ,
1172
- BTORDER_PROC , rel -> rd_opcintype [i ], cur -> sk_subtype ,
1173
- cur -> sk_attno , RelationGetRelationName (rel ));
1171
+ if (i < IndexRelationGetNumberOfKeyAttributes (rel ))
1172
+ {
1173
+ cmp_proc = get_opfamily_proc (rel -> rd_opfamily [i ],
1174
+ rel -> rd_opcintype [i ],
1175
+ cur -> sk_subtype ,
1176
+ BTORDER_PROC );
1177
+ if (!RegProcedureIsValid (cmp_proc ))
1178
+ elog (ERROR , "missing support function %d(%u,%u) for attribute %d of index \"%s\"" ,
1179
+ BTORDER_PROC , rel -> rd_opcintype [i ], cur -> sk_subtype ,
1180
+ cur -> sk_attno , RelationGetRelationName (rel ));
1181
+ }
1182
+ else
1183
+ {
1184
+ /*
1185
+ * We do not require that included columns have opclass.
1186
+ * So for included attributes scankey here can only
1187
+ * be SK_SEARCHNULL or SK_SEARCHNOTNULL
1188
+ * so it's ok to init scan key entry with invalid cmp_proc.
1189
+ */
1190
+ cmp_proc = InvalidOid ;
1191
+ }
1174
1192
ScanKeyEntryInitialize (scankeys + i ,
1175
1193
cur -> sk_flags ,
1176
1194
cur -> sk_attno ,
0 commit comments