@@ -98,7 +98,8 @@ static List *build_index_paths(PlannerInfo *root, RelOptInfo *rel,
98
98
IndexOptInfo * index , IndexClauseSet * clauses ,
99
99
bool useful_predicate ,
100
100
ScanTypeControl scantype ,
101
- bool * skip_nonnative_saop );
101
+ bool * skip_nonnative_saop ,
102
+ bool * skip_lower_saop );
102
103
static List * build_paths_for_OR (PlannerInfo * root , RelOptInfo * rel ,
103
104
List * clauses , List * other_clauses );
104
105
static List * generate_bitmap_or_paths (PlannerInfo * root , RelOptInfo * rel ,
@@ -702,6 +703,7 @@ get_index_paths(PlannerInfo *root, RelOptInfo *rel,
702
703
{
703
704
List * indexpaths ;
704
705
bool skip_nonnative_saop = false;
706
+ bool skip_lower_saop = false;
705
707
ListCell * lc ;
706
708
707
709
/*
@@ -712,8 +714,24 @@ get_index_paths(PlannerInfo *root, RelOptInfo *rel,
712
714
index , clauses ,
713
715
index -> predOK ,
714
716
ST_ANYSCAN ,
715
- & skip_nonnative_saop );
716
-
717
+ & skip_nonnative_saop ,
718
+ & skip_lower_saop );
719
+
720
+ /*
721
+ * If we skipped any lower-order ScalarArrayOpExprs on an index with an AM
722
+ * that supports them, then try again including those clauses. This will
723
+ * produce paths with more selectivity but no ordering.
724
+ */
725
+ if (skip_lower_saop )
726
+ {
727
+ indexpaths = list_concat (indexpaths ,
728
+ build_index_paths (root , rel ,
729
+ index , clauses ,
730
+ index -> predOK ,
731
+ ST_ANYSCAN ,
732
+ & skip_nonnative_saop ,
733
+ NULL ));
734
+ }
717
735
/*
718
736
* Submit all the ones that can form plain IndexScan plans to add_path. (A
719
737
* plain IndexPath can represent either a plain IndexScan or an
@@ -750,6 +768,7 @@ get_index_paths(PlannerInfo *root, RelOptInfo *rel,
750
768
index , clauses ,
751
769
false,
752
770
ST_BITMAPSCAN ,
771
+ NULL ,
753
772
NULL );
754
773
* bitindexpaths = list_concat (* bitindexpaths , indexpaths );
755
774
}
@@ -794,7 +813,8 @@ build_index_paths(PlannerInfo *root, RelOptInfo *rel,
794
813
IndexOptInfo * index , IndexClauseSet * clauses ,
795
814
bool useful_predicate ,
796
815
ScanTypeControl scantype ,
797
- bool * skip_nonnative_saop )
816
+ bool * skip_nonnative_saop ,
817
+ bool * skip_lower_saop )
798
818
{
799
819
List * result = NIL ;
800
820
IndexPath * ipath ;
@@ -805,6 +825,7 @@ build_index_paths(PlannerInfo *root, RelOptInfo *rel,
805
825
List * orderbyclausecols ;
806
826
List * index_pathkeys ;
807
827
List * useful_pathkeys ;
828
+ bool found_lower_saop_clause ;
808
829
bool pathkeys_possibly_useful ;
809
830
bool index_is_ordered ;
810
831
bool index_only_scan ;
@@ -843,6 +864,7 @@ build_index_paths(PlannerInfo *root, RelOptInfo *rel,
843
864
* otherwise accounted for.
844
865
*/
845
866
index_clauses = NIL ;
867
+ found_lower_saop_clause = false;
846
868
outer_relids = bms_copy (rel -> lateral_relids );
847
869
for (indexcol = 0 ; indexcol < index -> nkeycolumns ; indexcol ++ )
848
870
{
@@ -866,6 +888,16 @@ build_index_paths(PlannerInfo *root, RelOptInfo *rel,
866
888
* skip_nonnative_saop = true;
867
889
continue ;
868
890
}
891
+ if (skip_nonnative_saop && IsA (rinfo -> clause , ScalarArrayOpExpr ) && indexcol > 0 )
892
+ {
893
+ if (skip_lower_saop )
894
+ {
895
+ /* Caller doesn't want to lose index ordering */
896
+ * skip_lower_saop = true;
897
+ continue ;
898
+ }
899
+ found_lower_saop_clause = true;
900
+ }
869
901
870
902
/* OK to include this clause */
871
903
index_clauses = lappend (index_clauses , iclause );
@@ -897,6 +929,7 @@ build_index_paths(PlannerInfo *root, RelOptInfo *rel,
897
929
* if we are only trying to build bitmap indexscans.
898
930
*/
899
931
pathkeys_possibly_useful = (scantype != ST_BITMAPSCAN &&
932
+ !found_lower_saop_clause &&
900
933
has_useful_pathkeys (root , rel ));
901
934
index_is_ordered = (index -> sortopfamily != NULL );
902
935
if (index_is_ordered && pathkeys_possibly_useful )
@@ -1148,6 +1181,7 @@ build_paths_for_OR(PlannerInfo *root, RelOptInfo *rel,
1148
1181
index , & clauseset ,
1149
1182
useful_predicate ,
1150
1183
ST_BITMAPSCAN ,
1184
+ NULL ,
1151
1185
NULL );
1152
1186
result = list_concat (result , indexpaths );
1153
1187
}
0 commit comments