HypoPG / hypopg

Hypothetical Indexes for PostgreSQL
https://hypopg.readthedocs.io
Other
1.39k stars 59 forks source link

Correctly allocate hypoIndex entries #75

Closed tedyu closed 1 year ago

tedyu commented 2 years ago

ncolumns field is calculated as:

      entry->ncolumns = nkeycolumns + ninccolumns;

In hypo_injectHypotheticalIndex:

            for (i = 0; i < ncolumns; i++)
            {
                index->sortopfamily[i] = entry->sortopfamily[i];
                index->reverse_sort[i] = entry->reverse_sort[i];
                index->nulls_first[i] = entry->nulls_first[i];

However, when these fields are initialized, only nkeycolumns elements are allocated.

This PR changes the element count in allocations so that enough memory is allocated.

tedyu commented 2 years ago

cc @rjuju

rjuju commented 2 years ago

Hi,

Thanks for the report! However I think your proposed fix is backward: those info are only needed for key columns, so the loop should be changed instead. I found a few other similar mistakes, and while at it reordered the code a bit to make instructions order similar in both place, see https://github.com/HypoPG/hypopg/commit/0a80ba3090f3d291973f13b16fbb948d0b4d0ca1