{
/*
* A pre-9.4 format uncompressed page is represented by a single
- * segment, with an array of items.
+ * segment, with an array of items. The corner case is uncompressed
+ * page containing no items, which is represented as no segments.
*/
ItemPointer uncompressed;
int nuncompressed;
uncompressed = dataLeafPageGetUncompressed(page, &nuncompressed);
- seginfo = palloc(sizeof(leafSegmentInfo));
+ if (nuncompressed > 0)
+ {
+ seginfo = palloc(sizeof(leafSegmentInfo));
- seginfo->action = GIN_SEGMENT_REPLACE;
- seginfo->seg = NULL;
- seginfo->items = palloc(nuncompressed * sizeof(ItemPointerData));
- memcpy(seginfo->items, uncompressed, nuncompressed * sizeof(ItemPointerData));
- seginfo->nitems = nuncompressed;
+ seginfo->action = GIN_SEGMENT_REPLACE;
+ seginfo->seg = NULL;
+ seginfo->items = palloc(nuncompressed * sizeof(ItemPointerData));
+ memcpy(seginfo->items, uncompressed, nuncompressed * sizeof(ItemPointerData));
+ seginfo->nitems = nuncompressed;
- dlist_push_tail(&leaf->segments, &seginfo->node);
+ dlist_push_tail(&leaf->segments, &seginfo->node);
+ }
leaf->oldformat = true;
}
ItemPointer uncompressed = (ItemPointer) GinDataPageGetData(page);
int nuncompressed = GinPageGetOpaque(page)->maxoff;
int npacked;
- GinPostingList *plist;
- plist = ginCompressPostingList(uncompressed, nuncompressed,
- BLCKSZ, &npacked);
- Assert(npacked == nuncompressed);
+ /*
+ * Empty leaf pages are deleted as part of vacuum, but leftmost and
+ * rightmost pages are never deleted. So, pg_upgrade'd from pre-9.4
+ * instances might contain empty leaf pages, and we need to handle
+ * them correctly.
+ */
+ if (nuncompressed > 0)
+ {
+ GinPostingList *plist;
+
+ plist = ginCompressPostingList(uncompressed, nuncompressed,
+ BLCKSZ, &npacked);
+ totalsize = SizeOfGinPostingList(plist);
+
+ Assert(npacked == nuncompressed);
- totalsize = SizeOfGinPostingList(plist);
+ memcpy(GinDataLeafPageGetPostingList(page), plist, totalsize);
+ }
+ else
+ {
+ totalsize = 0;
+ }
- memcpy(GinDataLeafPageGetPostingList(page), plist, totalsize);
GinDataPageSetDataSize(page, totalsize);
GinPageSetCompressed(page);
GinPageGetOpaque(page)->maxoff = InvalidOffsetNumber;