• Peter Geoghegan's avatar
    Fix nbtsort.c's page space accounting. · 6dd86c26
    Peter Geoghegan authored
    Commit dd299df8, which made heap TID a tiebreaker nbtree index
    column, introduced new rules on page space management to make suffix
    truncation safe.  In general, suffix truncation needs to have a small
    amount of extra space available on the new left page when splitting a
    leaf page.  This is needed in case it turns out that truncation cannot
    even "truncate away the heap TID column", resulting in a
    larger-than-firstright leaf high key with an explicit heap TID
    representation.
    
    Despite all this, CREATE INDEX/nbtsort.c did not account for the
    possible need for extra heap TID space on leaf pages when deciding
    whether or not a new item could fit on current page.  This could lead to
    "failed to add item to the index page" errors when CREATE
    INDEX/nbtsort.c tried to finish off a leaf page that lacked space for a
    larger-than-firstright leaf high key (it only had space for firstright
    tuple, which was just short of what was needed following "truncation").
    
    Several conditions needed to be met all at once for CREATE INDEX to
    fail.  The problem was in the hard limit on what will fit on a page,
    which tends to be masked by the soft fillfactor-wise limit.  The easiest
    way to recreate the problem seems to be a CREATE INDEX on a low
    cardinality text column, with tuples that are of non-uniform width,
    using a fillfactor of 100.
    
    To fix, bring nbtsort.c in line with nbtsplitloc.c, which already
    pessimistically assumes that all leaf page splits will have high keys
    that have a heap TID appended.
    
    Reported-By: Andreas Joseph Krogh
    Discussion: https://postgr.es/m/VisenaEmail.c5.3ee7fe277d514162.16a6d785bea@tc7-visena
    6dd86c26
nbtsort.c 57 KB