amcheck: detect duplicate PostingItem keys in GIN posting trees

Started by Stepan Neretin5 months ago3 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1Stepan Neretin
slpmcf@gmail.com

Hi,

gin_index_check() compares adjacent separator keys on posting-tree
internal pages with ItemPointerCompare(...) < 0, so it reports
out-of-order keys but misses duplicates.

Equal separators should not occur: each PostingItem key is the child
page's right bound (dataPrepareDownlink), and leaf TIDs are strictly
increasing, so siblings must be strictly ascending. dataLocateItem
also treats equal keys as an exact match to that downlink. Entry-tree
checks already reject equals (>= 0); posting trees should do the same.

The attached patch changes the comparison to <= 0 and adds a TAP test
that corrupts two adjacent keys to be equal.

Best regards, Stepan Neretin.

Attachments:

0001-amcheck-Detect-duplicate-PostingItem-keys-in-GIN-pos.patchtext/x-patch; charset=US-ASCII; name=0001-amcheck-Detect-duplicate-PostingItem-keys-in-GIN-pos.patchDownload+32-2
#2Sofia Kopikova
s.kopikova@postgrespro.ru
In reply to: Stepan Neretin (#1)
Re: amcheck: detect duplicate PostingItem keys in GIN posting trees

On 7/15/26 15:00, Stepan Neretin wrote:

Hi,

  gin_index_check() compares adjacent separator keys on posting-tree
  internal pages with ItemPointerCompare(...) < 0, so it reports
  out-of-order keys but misses duplicates.

  Equal separators should not occur: each PostingItem key is the child
  page's right bound (dataPrepareDownlink), and leaf TIDs are strictly
  increasing, so siblings must be strictly ascending. dataLocateItem
  also treats equal keys as an exact match to that downlink. Entry-tree
  checks already reject equals (>= 0); posting trees should do the same.

  The attached patch changes the comparison to <= 0 and adds a TAP test
  that corrupts two adjacent keys to be equal.

  Best regards, Stepan Neretin.

Thanks for your patch, Stepan!

The fix itself looks good to me, but why don't you make your test in the
same style as other tests? I mean, make 'test' relation name and '2'
block number perl variables, like it's done in the tests above.

--
regards,
Sofia Kopikova
Postgres Professional

#3Stepan Neretin
slpmcf@gmail.com
In reply to: Sofia Kopikova (#2)
Re: amcheck: detect duplicate PostingItem keys in GIN posting trees

On Thu, Jul 16, 2026 at 11:25 PM Sofia Kopikova <s.kopikova@postgrespro.ru>
wrote:

On 7/15/26 15:00, Stepan Neretin wrote:

Hi,

gin_index_check() compares adjacent separator keys on posting-tree
internal pages with ItemPointerCompare(...) < 0, so it reports
out-of-order keys but misses duplicates.

Equal separators should not occur: each PostingItem key is the child
page's right bound (dataPrepareDownlink), and leaf TIDs are strictly
increasing, so siblings must be strictly ascending. dataLocateItem
also treats equal keys as an exact match to that downlink. Entry-tree
checks already reject equals (>= 0); posting trees should do the same.

The attached patch changes the comparison to <= 0 and adds a TAP test
that corrupts two adjacent keys to be equal.

Best regards, Stepan Neretin.

Thanks for your patch, Stepan!

The fix itself looks good to me, but why don't you make your test in the
same style as other tests? I mean, make 'test' relation name and '2'
block number perl variables, like it's done in the tests above.

--
regards,
Sofia Kopikova
Postgres Professional

Hi Sofia,

Thanks for the review!

I've updated the TAP test to use $relname / $blkno like the other tests
in the file. Attached is v2.

Best regards,
Stepan Neretin

Attachments:

t253084_3
v2-0001-amcheck-Detect-duplicate-PostingItem-keys-in-GIN-pos.patchtext/x-patch; charset=US-ASCII; name=v2-0001-amcheck-Detect-duplicate-PostingItem-keys-in-GIN-pos.patchDownload+33-2