pgsql: Fix PageAddItem BRIN bug - Mailing list pgsql-committers

From Alvaro Herrera
Subject pgsql: Fix PageAddItem BRIN bug
Date
Msg-id E1b7SF2-0006Ce-96@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Fix PageAddItem BRIN bug

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

Branch
------
REL9_5_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/2973d7d02085ffad5697770ae5cfdc20d5b1aae7

Modified Files
--------------
src/backend/access/brin/brin_pageops.c |  4 +--
src/backend/access/brin/brin_revmap.c  |  4 +++
src/backend/access/brin/brin_xlog.c    |  3 +-
src/backend/storage/page/bufpage.c     | 62 ++++++++++++++++++++++++++--------
src/include/storage/bufpage.h          |  5 +++
5 files changed, 60 insertions(+), 18 deletions(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: pgsql: Fix missing abort checks in pg_backup_directory.c.
Next
From: Alvaro Herrera
Date:
Subject: pgsql: Fix PageAddItem BRIN bug