Skip to content

Commit 5b8e442

Browse files
committed
Make plan_cluster_use_sort cope with no IndexOptInfo for the target index.
The original coding assumed that such a case represents caller error, but actually get_relation_info will omit generating an IndexOptInfo for any index it thinks is unsafe to use. Therefore, handle this case by returning "true" to indicate that a seqscan-and-sort is the preferred way to implement the CLUSTER operation. New bug in 9.1, no backpatch needed. Per bug #5985 from Daniel Grace.
1 parent 395fcac commit 5b8e442

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/backend/optimizer/plan/planner.c

+18-11
Original file line numberDiff line numberDiff line change
@@ -3093,15 +3093,6 @@ plan_cluster_use_sort(Oid tableOid, Oid indexOid)
30933093
/* Build RelOptInfo */
30943094
rel = build_simple_rel(root, 1, RELOPT_BASEREL);
30953095

3096-
/*
3097-
* Rather than doing all the pushups that would be needed to use
3098-
* set_baserel_size_estimates, just do a quick hack for rows and width.
3099-
*/
3100-
rel->rows = rel->tuples;
3101-
rel->width = get_relation_data_width(tableOid, NULL);
3102-
3103-
root->total_table_pages = rel->pages;
3104-
31053096
/* Locate IndexOptInfo for the target index */
31063097
indexInfo = NULL;
31073098
foreach(lc, rel->indexlist)
@@ -3110,9 +3101,25 @@ plan_cluster_use_sort(Oid tableOid, Oid indexOid)
31103101
if (indexInfo->indexoid == indexOid)
31113102
break;
31123103
}
3104+
3105+
/*
3106+
* It's possible that get_relation_info did not generate an IndexOptInfo
3107+
* for the desired index; this could happen if it's not yet reached its
3108+
* indcheckxmin usability horizon, or if it's a system index and we're
3109+
* ignoring system indexes. In such cases we should tell CLUSTER to not
3110+
* trust the index contents but use seqscan-and-sort.
3111+
*/
31133112
if (lc == NULL) /* not in the list? */
3114-
elog(ERROR, "index %u does not belong to table %u",
3115-
indexOid, tableOid);
3113+
return true; /* use sort */
3114+
3115+
/*
3116+
* Rather than doing all the pushups that would be needed to use
3117+
* set_baserel_size_estimates, just do a quick hack for rows and width.
3118+
*/
3119+
rel->rows = rel->tuples;
3120+
rel->width = get_relation_data_width(tableOid, NULL);
3121+
3122+
root->total_table_pages = rel->pages;
31163123

31173124
/*
31183125
* Determine eval cost of the index expressions, if any. We need to

0 commit comments

Comments
 (0)