Actually, discovered bug is not related to patch except new test faces with it, problem is: CompareIndexInfo() checks rd_opfamily for equality for all attributes, not only for key attribute.

Patch attached. But it seems to me, field's names of
IndexInfo structure are a bit confused now:
    int         ii_NumIndexAttrs;   /* total number of columns in index */
int ii_NumIndexKeyAttrs; /* number of key columns in index */
    AttrNumber  ii_KeyAttrNumbers[INDEX_MAX_KEYS];

ii_KeyAttrNumbers contains all columns, i.e. it contains ii_NumIndexAttrs number of columns, not a ii_NumIndexKeyAttrs number as easy to think.

I suggest rename ii_KeyAttrNumbers to ii_AttrNumbers or ii_IndexAttrNumbers. Opinions?

     for (i = 0; i < info1->ii_NumIndexAttrs; i++)
         if (maplen < info2->ii_KeyAttrNumbers[i])

Teodor Sigaev                      E-mail:
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 5d73e92901..0002816fcc 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -1831,6 +1831,10 @@ CompareIndexInfo(IndexInfo *info1, IndexInfo *info2,
 	if (info1->ii_NumIndexAttrs != info2->ii_NumIndexAttrs)
 		return false;
+	/* and same number of key attributes */
+	if (info1->ii_NumIndexKeyAttrs != info2->ii_NumIndexKeyAttrs)
+		return false;
 	 * and columns match through the attribute map (actual attribute numbers
 	 * might differ!)  Note that this implies that index columns that are
@@ -1850,7 +1854,9 @@ CompareIndexInfo(IndexInfo *info1, IndexInfo *info2,
 		if (collations1[i] != collations2[i])
 			return false;
-		if (opfamilies1[i] != opfamilies2[i])
+		/* opfamily is valid on for key attributes */
+		if (i < info2->ii_NumIndexKeyAttrs && opfamilies1[i] != opfamilies2[i])
 			return false;

Reply via email to