Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d642598

Browse files
committedSep 28, 2023
Fix checking of index expressions in CompareIndexInfo().
This code was sloppy about comparison of index columns that are expressions. It didn't reliably reject cases where one index has an expression where the other has a plain column, and it could index off the start of the attmap array, leading to a Valgrind complaint (though an actual crash seems unlikely). I'm not sure that the expression-vs-column sloppiness leads to any visible problem in practice, because the subsequent comparison of the two expression lists would reject cases where the indexes have different numbers of expressions overall. Maybe we could falsely match indexes having the same expressions in different column positions, but it'd require unlucky contents of the word before the attmap array. It's not too surprising that no problem has been reported from the field. Nonetheless, this code is clearly wrong. Per bug #18135 from Alexander Lakhin. Back-patch to all supported branches. Discussion: https://postgr.es/m/18135-532f4a755e71e4d2@postgresql.org
1 parent abe4237 commit d642598

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed
 

‎src/backend/catalog/index.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,7 +2539,7 @@ CompareIndexInfo(IndexInfo *info1, IndexInfo *info2,
25392539

25402540
/*
25412541
* and columns match through the attribute map (actual attribute numbers
2542-
* might differ!) Note that this implies that index columns that are
2542+
* might differ!) Note that this checks that index columns that are
25432543
* expressions appear in the same positions. We will next compare the
25442544
* expressions themselves.
25452545
*/
@@ -2548,13 +2548,22 @@ CompareIndexInfo(IndexInfo *info1, IndexInfo *info2,
25482548
if (maplen < info2->ii_IndexAttrNumbers[i])
25492549
elog(ERROR, "incorrect attribute map");
25502550

2551-
/* ignore expressions at this stage */
2552-
if ((info1->ii_IndexAttrNumbers[i] != InvalidAttrNumber) &&
2553-
(attmap[info2->ii_IndexAttrNumbers[i] - 1] !=
2554-
info1->ii_IndexAttrNumbers[i]))
2555-
return false;
2551+
/* ignore expressions for now (but check their collation/opfamily) */
2552+
if (!(info1->ii_IndexAttrNumbers[i] == InvalidAttrNumber &&
2553+
info2->ii_IndexAttrNumbers[i] == InvalidAttrNumber))
2554+
{
2555+
/* fail if just one index has an expression in this column */
2556+
if (info1->ii_IndexAttrNumbers[i] == InvalidAttrNumber ||
2557+
info2->ii_IndexAttrNumbers[i] == InvalidAttrNumber)
2558+
return false;
2559+
2560+
/* both are columns, so check for match after mapping */
2561+
if (attmap[info2->ii_IndexAttrNumbers[i] - 1] !=
2562+
info1->ii_IndexAttrNumbers[i])
2563+
return false;
2564+
}
25562565

2557-
/* collation and opfamily is not valid for including columns */
2566+
/* collation and opfamily are not valid for included columns */
25582567
if (i >= info1->ii_NumIndexKeyAttrs)
25592568
continue;
25602569

0 commit comments

Comments
 (0)
Failed to load comments.