diff --git a/src/backend/access/gin/gininsert.c b/src/backend/access/gin/gininsert.c index a575afacab3..57a49be06b8 100644 --- a/src/backend/access/gin/gininsert.c +++ b/src/backend/access/gin/gininsert.c @@ -1454,7 +1454,7 @@ GinBufferStoreTuple(GinBuffer *buffer, GinTuple *tup) if (GinBufferIsEmpty(buffer)) { buffer->category = tup->category; - buffer->keylen = tup->keylen; + buffer->keylen = tup->u.keylen; buffer->attnum = tup->attrnum; buffer->typlen = tup->typlen; @@ -2238,7 +2238,7 @@ _gin_build_tuple(OffsetNumber attrnum, unsigned char category, char *ptr; Size tuplen; - int keylen; + Size keylen; dlist_mutable_iter iter; dlist_head segments; @@ -2314,7 +2314,7 @@ _gin_build_tuple(OffsetNumber attrnum, unsigned char category, tuple->tuplen = tuplen; tuple->attrnum = attrnum; tuple->category = category; - tuple->keylen = keylen; + tuple->u.keylen = keylen; tuple->nitems = nitems; /* key type info */ @@ -2387,7 +2387,7 @@ _gin_parse_tuple_key(GinTuple *a) if (a->typbyval) { - memcpy(&key, a->data, a->keylen); + memcpy(&key, a->data, a->u.keylen); return key; } @@ -2406,8 +2406,8 @@ _gin_parse_tuple_items(GinTuple *a) int ndecoded; ItemPointer items; - len = a->tuplen - SHORTALIGN(offsetof(GinTuple, data) + a->keylen); - ptr = (char *) a + SHORTALIGN(offsetof(GinTuple, data) + a->keylen); + len = a->tuplen - SHORTALIGN(offsetof(GinTuple, data) + a->u.keylen); + ptr = (char *) a + SHORTALIGN(offsetof(GinTuple, data) + a->u.keylen); items = ginPostingListDecodeAllSegments((GinPostingList *) ptr, len, &ndecoded); diff --git a/src/include/access/gin_tuple.h b/src/include/access/gin_tuple.h index 702f7d12889..3c3345ca0ef 100644 --- a/src/include/access/gin_tuple.h +++ b/src/include/access/gin_tuple.h @@ -21,7 +21,25 @@ typedef struct GinTuple { int tuplen; /* length of the whole tuple */ OffsetNumber attrnum; /* attnum of index key */ - uint16 keylen; /* bytes in data for key value */ + + /* + * The key value is accessed in place, so data (below) must be aligned + * well enough for any key type. We include both "double" and "int64" in + * the union to ensure that the compiler knows it must be MAXALIGN'ed (cf. + * configure's computation of MAXIMUM_ALIGNOF); this puts data at a + * MAXALIGN'ed offset, which the static assertion below verifies. On + * 64-bit platforms, this already happens anyway; this trick is only + * needed on some 32-bit platforms. Note that putting a flexible array + * member into a union is not valid C, so we have to pick some other + * member to move the alignment around. This one just happens to be a + * space-efficient one. + */ + union + { + Size keylen; /* bytes in data for key value */ + double force_align_d; + int64 force_align_i64; + } u; int16 typlen; /* typlen for key */ bool typbyval; /* typbyval for key */ signed char category; /* category: normal or NULL? */ @@ -29,12 +47,15 @@ typedef struct GinTuple char data[FLEXIBLE_ARRAY_MEMBER]; } GinTuple; +StaticAssertDecl(offsetof(GinTuple, data) % MAXIMUM_ALIGNOF == 0, + "GinTuple.data must be MAXALIGN'ed"); + static inline ItemPointer GinTupleGetFirst(GinTuple *tup) { GinPostingList *list; - list = (GinPostingList *) SHORTALIGN(tup->data + tup->keylen); + list = (GinPostingList *) SHORTALIGN(tup->data + tup->u.keylen); return &list->first; }