Skip to content

Commit 83d892d

Browse files
committed
Revert pre 5bf748b SAOP behavior in PG17
Commit 5bf748b allows generation of unsafe SAOP path keys on a multicolumn index that were disabled previously by 807a40c.
1 parent 20cea79 commit 83d892d

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

src/backend/optimizer/path/indxpath.c

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ static List *build_index_paths(PlannerInfo *root, RelOptInfo *rel,
9898
IndexOptInfo *index, IndexClauseSet *clauses,
9999
bool useful_predicate,
100100
ScanTypeControl scantype,
101-
bool *skip_nonnative_saop);
101+
bool *skip_nonnative_saop,
102+
bool *skip_lower_saop);
102103
static List *build_paths_for_OR(PlannerInfo *root, RelOptInfo *rel,
103104
List *clauses, List *other_clauses);
104105
static List *generate_bitmap_or_paths(PlannerInfo *root, RelOptInfo *rel,
@@ -702,6 +703,7 @@ get_index_paths(PlannerInfo *root, RelOptInfo *rel,
702703
{
703704
List *indexpaths;
704705
bool skip_nonnative_saop = false;
706+
bool skip_lower_saop = false;
705707
ListCell *lc;
706708

707709
/*
@@ -712,8 +714,24 @@ get_index_paths(PlannerInfo *root, RelOptInfo *rel,
712714
index, clauses,
713715
index->predOK,
714716
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+
}
717735
/*
718736
* Submit all the ones that can form plain IndexScan plans to add_path. (A
719737
* plain IndexPath can represent either a plain IndexScan or an
@@ -750,6 +768,7 @@ get_index_paths(PlannerInfo *root, RelOptInfo *rel,
750768
index, clauses,
751769
false,
752770
ST_BITMAPSCAN,
771+
NULL,
753772
NULL);
754773
*bitindexpaths = list_concat(*bitindexpaths, indexpaths);
755774
}
@@ -794,7 +813,8 @@ build_index_paths(PlannerInfo *root, RelOptInfo *rel,
794813
IndexOptInfo *index, IndexClauseSet *clauses,
795814
bool useful_predicate,
796815
ScanTypeControl scantype,
797-
bool *skip_nonnative_saop)
816+
bool *skip_nonnative_saop,
817+
bool *skip_lower_saop)
798818
{
799819
List *result = NIL;
800820
IndexPath *ipath;
@@ -805,6 +825,7 @@ build_index_paths(PlannerInfo *root, RelOptInfo *rel,
805825
List *orderbyclausecols;
806826
List *index_pathkeys;
807827
List *useful_pathkeys;
828+
bool found_lower_saop_clause;
808829
bool pathkeys_possibly_useful;
809830
bool index_is_ordered;
810831
bool index_only_scan;
@@ -843,6 +864,7 @@ build_index_paths(PlannerInfo *root, RelOptInfo *rel,
843864
* otherwise accounted for.
844865
*/
845866
index_clauses = NIL;
867+
found_lower_saop_clause = false;
846868
outer_relids = bms_copy(rel->lateral_relids);
847869
for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
848870
{
@@ -866,6 +888,16 @@ build_index_paths(PlannerInfo *root, RelOptInfo *rel,
866888
*skip_nonnative_saop = true;
867889
continue;
868890
}
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+
}
869901

870902
/* OK to include this clause */
871903
index_clauses = lappend(index_clauses, iclause);
@@ -897,6 +929,7 @@ build_index_paths(PlannerInfo *root, RelOptInfo *rel,
897929
* if we are only trying to build bitmap indexscans.
898930
*/
899931
pathkeys_possibly_useful = (scantype != ST_BITMAPSCAN &&
932+
!found_lower_saop_clause &&
900933
has_useful_pathkeys(root, rel));
901934
index_is_ordered = (index->sortopfamily != NULL);
902935
if (index_is_ordered && pathkeys_possibly_useful)
@@ -1148,6 +1181,7 @@ build_paths_for_OR(PlannerInfo *root, RelOptInfo *rel,
11481181
index, &clauseset,
11491182
useful_predicate,
11501183
ST_BITMAPSCAN,
1184+
NULL,
11511185
NULL);
11521186
result = list_concat(result, indexpaths);
11531187
}

src/test/regress/expected/create_index.out

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,11 +1943,13 @@ explain (costs off)
19431943
SELECT thousand, tenthous FROM tenk1
19441944
WHERE thousand < 2 AND tenthous IN (1001,3000)
19451945
ORDER BY thousand;
1946-
QUERY PLAN
1947-
--------------------------------------------------------------------------------
1948-
Index Only Scan using tenk1_thous_tenthous on tenk1
1949-
Index Cond: ((thousand < 2) AND (tenthous = ANY ('{1001,3000}'::integer[])))
1950-
(2 rows)
1946+
QUERY PLAN
1947+
--------------------------------------------------------------------------------------
1948+
Sort
1949+
Sort Key: thousand
1950+
-> Index Only Scan using tenk1_thous_tenthous on tenk1
1951+
Index Cond: ((thousand < 2) AND (tenthous = ANY ('{1001,3000}'::integer[])))
1952+
(4 rows)
19511953

19521954
SELECT thousand, tenthous FROM tenk1
19531955
WHERE thousand < 2 AND tenthous IN (1001,3000)
@@ -1963,11 +1965,13 @@ explain (costs off)
19631965
SELECT thousand, tenthous FROM tenk1
19641966
WHERE thousand < 2 AND tenthous IN (1001,3000)
19651967
ORDER BY thousand DESC, tenthous DESC;
1966-
QUERY PLAN
1967-
--------------------------------------------------------------------------------
1968-
Index Only Scan Backward using tenk1_thous_tenthous on tenk1
1969-
Index Cond: ((thousand < 2) AND (tenthous = ANY ('{1001,3000}'::integer[])))
1970-
(2 rows)
1968+
QUERY PLAN
1969+
--------------------------------------------------------------------------------------
1970+
Sort
1971+
Sort Key: thousand DESC, tenthous DESC
1972+
-> Index Only Scan using tenk1_thous_tenthous on tenk1
1973+
Index Cond: ((thousand < 2) AND (tenthous = ANY ('{1001,3000}'::integer[])))
1974+
(4 rows)
19711975

19721976
SELECT thousand, tenthous FROM tenk1
19731977
WHERE thousand < 2 AND tenthous IN (1001,3000)

0 commit comments

Comments
 (0)