@@ -890,9 +890,14 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
890
890
* We support the convention that sk_subtype == InvalidOid means
891
891
* the opclass input type; this is a hack to simplify life for
892
892
* ScanKeyInit().
893
+ *
894
+ * We do not require that included columns have opclass.
895
+ * So do not try to find the comparison function for these
896
+ * attributes.
893
897
*/
894
- if (cur -> sk_subtype == rel -> rd_opcintype [i ] ||
895
- cur -> sk_subtype == InvalidOid )
898
+ if (i < IndexRelationGetNumberOfKeyAttributes (rel ) &&
899
+ (cur -> sk_subtype == rel -> rd_opcintype [i ] ||
900
+ cur -> sk_subtype == InvalidOid ))
896
901
{
897
902
FmgrInfo * procinfo ;
898
903
@@ -910,14 +915,27 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
910
915
{
911
916
RegProcedure cmp_proc ;
912
917
913
- cmp_proc = get_opfamily_proc (rel -> rd_opfamily [i ],
914
- rel -> rd_opcintype [i ],
915
- cur -> sk_subtype ,
916
- BTORDER_PROC );
917
- if (!RegProcedureIsValid (cmp_proc ))
918
- elog (ERROR , "missing support function %d(%u,%u) for attribute %d of index \"%s\"" ,
919
- BTORDER_PROC , rel -> rd_opcintype [i ], cur -> sk_subtype ,
920
- cur -> sk_attno , RelationGetRelationName (rel ));
918
+ if (i < IndexRelationGetNumberOfKeyAttributes (rel ))
919
+ {
920
+ cmp_proc = get_opfamily_proc (rel -> rd_opfamily [i ],
921
+ rel -> rd_opcintype [i ],
922
+ cur -> sk_subtype ,
923
+ BTORDER_PROC );
924
+ if (!RegProcedureIsValid (cmp_proc ))
925
+ elog (ERROR , "missing support function %d(%u,%u) for attribute %d of index \"%s\"" ,
926
+ BTORDER_PROC , rel -> rd_opcintype [i ], cur -> sk_subtype ,
927
+ cur -> sk_attno , RelationGetRelationName (rel ));
928
+ }
929
+ else
930
+ {
931
+ /*
932
+ * We do not require that included columns have opclass.
933
+ * So for included attributes scankey here can only
934
+ * be SK_SEARCHNULL or SK_SEARCHNOTNULL
935
+ * so it's ok to init scan key entry with invalid cmp_proc.
936
+ */
937
+ cmp_proc = InvalidOid ;
938
+ }
921
939
ScanKeyEntryInitialize (scankeys + i ,
922
940
cur -> sk_flags ,
923
941
cur -> sk_attno ,
0 commit comments