• Alvaro Herrera's avatar
    Fix PageAddItem BRIN bug · 975ad4e6
    Alvaro Herrera authored
    BRIN was relying on the ability to remove a tuple from an index page,
    then putting another tuple in the same line pointer.  But PageAddItem
    refuses to add a tuple beyond the first free item past the last used
    item, and in particular, it rejects an attempt to add an item to an
    empty page anywhere other than the first line pointer.  PageAddItem
    issues a WARNING and indicates to the caller that it failed, which in
    turn causes the BRIN calling code to issue a PANIC, so the whole
    sequence looks like this:
    	WARNING:  specified item offset is too large
    	PANIC:  failed to add BRIN tuple
    
    To fix, create a new function PageAddItemExtended which is like
    PageAddItem except that the two boolean arguments become a flags bitmap;
    the "overwrite" and "is_heap" boolean flags in PageAddItem become
    PAI_OVERWITE and PAI_IS_HEAP flags in the new function, and a new flag
    PAI_ALLOW_FAR_OFFSET enables the behavior required by BRIN.
    PageAddItem() retains its original signature, for compatibility with
    third-party modules (other callers in core code are not modified,
    either).
    
    Also, in the belt-and-suspenders spirit, I added a new sanity check in
    brinGetTupleForHeapBlock to raise an error if an TID found in the revmap
    is not marked as live by the page header.  This causes it to react with
    "ERROR: corrupted BRIN index" to the bug at hand, rather than a hard
    crash.
    
    Backpatch to 9.5.
    
    Bug reported by Andreas Seltenreich as detected by his handy sqlsmith
    fuzzer.
    Discussion: https://www.postgresql.org/message-id/87mvni77jh.fsf@elite.ansel.ydns.eu
    975ad4e6
bufpage.h 14.9 KB