agora inbox for pgsql-bugs@postgresql.org  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: mrdrivingduck@gmail.com
Subject: BUG #19664: nbtree: Assertion failure when a custom index AM reuses bthandler
Date: Mon, 07 Sep 2026 11:51:29 +0000
Message-ID: <19664-35af2d7fa85785d6@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      19664
Logged by:          Jingtang Zhang
Email address:      mrdrivingduck@gmail.com
PostgreSQL version: 19beta3
Operating system:   Linux
Description:        

## Repro

Build with assertions enabled, for example:

./configure --enable-debug --enable-cassert
make

---

Run SQL:

CREATE ACCESS METHOD hx_orph_ix TYPE INDEX HANDLER bthandler;

CREATE TABLE hx_orph_tab(a int, b text);
INSERT INTO hx_orph_tab
SELECT g, 'x' || g FROM generate_series(1, 100) AS g;

CREATE OPERATOR CLASS hx_orph_ix_int4_ops
DEFAULT FOR TYPE integer USING hx_orph_ix AS
  OPERATOR 1 <,
  OPERATOR 2 <=,
  OPERATOR 3 =,
  OPERATOR 4 >=,
  OPERATOR 5 >,
  FUNCTION 1 btint4cmp(integer, integer);

CREATE INDEX hx_orph_idx ON hx_orph_tab USING hx_orph_ix(a);

---

Get:

TRAP: failed Assert("wstate->index->rd_rel->relkind == RELKIND_INDEX &&
wstate->index->rd_rel->relam == BTREE_AM_OID")
File: "nbtsort.c"

---

## Thoughts

hx_orph_ix has a newly allocated AM OID, while its handler is bthandler. The
btree build path is therefore used, but BTGetFillFactor() and
BTGetDeduplicateItems() require the AM OID to be the built-in BTREE_AM_OID.

The OID check is not the relevant safety condition. These macros interpret
rd_options as BTOptions, so they should verify that the relation options
were parsed by btoptions. bthandler returns an IndexAmRoutine with
.amoptions = btoptions, making the layout compatible. Reusing built-in index
handlers is also an established pattern, e.g. the existing gist2 regression
test reuses gisthandler.

Proposed fix:

#define BTGetFillFactor(relation) \
    (AssertMacro(relation->rd_rel->relkind == RELKIND_INDEX && \
                 relation->rd_indam->amoptions == btoptions), \
     ...)

#define BTGetDeduplicateItems(relation) \
    (AssertMacro(relation->rd_rel->relkind == RELKIND_INDEX && \
                 relation->rd_indam->amoptions == btoptions), \
     ...)








view thread (3+ messages)  latest in thread

Message-ID: <19664-35af2d7fa85785d6@postgresql.org>
Permalink:  ../19664-35af2d7fa85785d6@postgresql.org/
Also on:    postgresql.org/message-id/19664-35af2d7fa85785d6@postgresql.org

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, pgsql-bugs@lists.postgresql.org, mrdrivingduck@gmail.com
  Subject: Re: BUG #19664: nbtree: Assertion failure when a custom index AM reuses bthandler
  In-Reply-To: <19664-35af2d7fa85785d6@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox