From 0a80ba3090f3d291973f13b16fbb948d0b4d0ca1 Mon Sep 17 00:00:00 2001 From: Julien Rouhaud Date: Sat, 17 Sep 2022 22:05:24 +0800 Subject: [PATCH] Fix a few allocation size and array assignation mistakes. Some part of the code was allocating array entries to account for included columns while it's only needed for key columns, and some other parts were trying to store data for included columns on info that's only relevant for key columns (and thus allocated in consequence). While at it, reorder a few lines to keep the order consistent between and hypo_newIndex() and hypo_injectHypotheticalIndex(). Per initial report from Zhihong Yu. --- hypopg_index.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/hypopg_index.c b/hypopg_index.c index bf7ca38..d03d554 100644 --- a/hypopg_index.c +++ b/hypopg_index.c @@ -1013,21 +1013,17 @@ hypo_injectHypotheticalIndex(PlannerInfo *root, #endif index->indexkeys = (int *) palloc(sizeof(int) * ncolumns); - index->indexcollations = (Oid *) palloc(sizeof(int) * ncolumns); - index->opfamily = (Oid *) palloc(sizeof(int) * ncolumns); - index->opcintype = (Oid *) palloc(sizeof(int) * ncolumns); - -#if PG_VERSION_NUM >= 90500 - index->canreturn = (bool *) palloc(sizeof(bool) * ncolumns); -#endif + index->indexcollations = (Oid *) palloc(sizeof(int) * nkeycolumns); + index->opfamily = (Oid *) palloc(sizeof(int) * nkeycolumns); + index->opcintype = (Oid *) palloc(sizeof(int) * nkeycolumns); if ((index->relam == BTREE_AM_OID) || entry->amcanorder) { if (index->relam != BTREE_AM_OID) - index->sortopfamily = palloc0(sizeof(Oid) * ncolumns); + index->sortopfamily = palloc0(sizeof(Oid) * nkeycolumns); - index->reverse_sort = (bool *) palloc(sizeof(bool) * ncolumns); - index->nulls_first = (bool *) palloc(sizeof(bool) * ncolumns); + index->reverse_sort = (bool *) palloc(sizeof(bool) * nkeycolumns); + index->nulls_first = (bool *) palloc(sizeof(bool) * nkeycolumns); } else { @@ -1036,6 +1032,10 @@ hypo_injectHypotheticalIndex(PlannerInfo *root, index->nulls_first = NULL; } +#if PG_VERSION_NUM >= 90500 + index->canreturn = (bool *) palloc(sizeof(bool) * ncolumns); +#endif + for (i = 0; i < ncolumns; i++) { index->indexkeys[i] = entry->indexkeys[i]; @@ -1046,9 +1046,9 @@ hypo_injectHypotheticalIndex(PlannerInfo *root, for (i = 0; i < nkeycolumns; i++) { + index->indexcollations[i] = entry->indexcollations[i]; index->opfamily[i] = entry->opfamily[i]; index->opcintype[i] = entry->opcintype[i]; - index->indexcollations[i] = entry->indexcollations[i]; } /* @@ -1064,7 +1064,7 @@ hypo_injectHypotheticalIndex(PlannerInfo *root, */ index->sortopfamily = index->opfamily; - for (i = 0; i < ncolumns; i++) + for (i = 0; i < nkeycolumns; i++) { index->reverse_sort[i] = entry->reverse_sort[i]; index->nulls_first[i] = entry->nulls_first[i]; @@ -1074,7 +1074,7 @@ hypo_injectHypotheticalIndex(PlannerInfo *root, { if (entry->sortopfamily) { - for (i = 0; i < ncolumns; i++) + for (i = 0; i < nkeycolumns; i++) { index->sortopfamily[i] = entry->sortopfamily[i]; index->reverse_sort[i] = entry->reverse_sort[i];