Skip to content

Commit ea4e1c0

Browse files
committed
Additional fixes of memory alignment in pg_mcv_list code
Commit d85e0f3 tried to fix memory alignment issues in serialization and deserialization of pg_mcv_list values, but it was a few bricks shy. The arrays of uint16 indexes in serialized items was not aligned, and the both the values and isnull flags were using the same pointer. Per investigation by Tom Lane on gaur.
1 parent 7ad6498 commit ea4e1c0

File tree

1 file changed

+9
-8
lines changed
  • src/backend/statistics

1 file changed

+9
-8
lines changed

src/backend/statistics/mcv.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
* ndim * (sizeof(uint16) + sizeof(bool)) + 2 * sizeof(double)
5252
*/
5353
#define ITEM_SIZE(ndims) \
54-
((ndims) * (sizeof(uint16) + sizeof(bool)) + 2 * sizeof(double))
54+
MAXALIGN((ndims) * (sizeof(uint16) + sizeof(bool)) + 2 * sizeof(double))
5555

5656
/*
5757
* Macros for convenient access to parts of a serialized MCV item.
@@ -929,8 +929,8 @@ statext_mcv_deserialize(bytea *data)
929929
mcvlen = MAXALIGN(offsetof(MCVList, items) + (sizeof(MCVItem) * nitems));
930930

931931
/* arrays of values and isnull flags for all MCV items */
932-
mcvlen += MAXALIGN(sizeof(Datum) * ndims * nitems);
933-
mcvlen += MAXALIGN(sizeof(bool) * ndims * nitems);
932+
mcvlen += nitems * MAXALIGN(sizeof(Datum) * ndims);
933+
mcvlen += nitems * MAXALIGN(sizeof(bool) * ndims);
934934

935935
/* we don't quite need to align this, but it makes some assers easier */
936936
mcvlen += MAXALIGN(datalen);
@@ -942,9 +942,9 @@ statext_mcv_deserialize(bytea *data)
942942
valuesptr = (char *) mcvlist
943943
+ MAXALIGN(offsetof(MCVList, items) + (sizeof(MCVItem) * nitems));
944944

945-
isnullptr = valuesptr + (MAXALIGN(sizeof(Datum) * ndims * nitems));
945+
isnullptr = valuesptr + (nitems * MAXALIGN(sizeof(Datum) * ndims));
946946

947-
dataptr = isnullptr + (MAXALIGN(sizeof(bool) * ndims * nitems));
947+
dataptr = isnullptr + (nitems * MAXALIGN(sizeof(bool) * ndims));
948948

949949
/*
950950
* Build mapping (index => value) for translating the serialized data into
@@ -1043,10 +1043,11 @@ statext_mcv_deserialize(bytea *data)
10431043
MCVItem *item = &mcvlist->items[i];
10441044

10451045
item->values = (Datum *) valuesptr;
1046-
valuesptr += (sizeof(Datum) * ndims);
1046+
valuesptr += MAXALIGN(sizeof(Datum) * ndims);
1047+
1048+
item->isnull = (bool *) isnullptr;
1049+
isnullptr += MAXALIGN(sizeof(bool) * ndims);
10471050

1048-
item->isnull = (bool *) valuesptr;
1049-
valuesptr += (sizeof(bool) * ndims);
10501051

10511052
/* just point to the right place */
10521053
indexes = ITEM_INDEXES(ptr);

0 commit comments

Comments
 (0)