Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x4DzU-0070zt-27 for pgsql-bugs@arkaria.postgresql.org; Wed, 09 Sep 2026 08:49:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x4DzT-00DLsf-2E for pgsql-bugs@arkaria.postgresql.org; Wed, 09 Sep 2026 08:49:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x3XtO-001NXn-1T for pgsql-bugs@lists.postgresql.org; Mon, 07 Sep 2026 11:52:06 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x3XtM-00000004QML-3sht for pgsql-bugs@lists.postgresql.org; Mon, 07 Sep 2026 11:52:05 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=BqZnonsqRQUkPsaLvXjr6rGVNuAp92k6giJpMbVrZXY=; b=YFE+bIpbq8ThNJNn/76emDPaE2 SWdzSxeACEE/5sJYwpD6NNpx0U4DF3B8XhnJYv5CMP3qNO9AA4G3Z4b88X8M0gV1hBl1bRjiW3dCD IrIqoloMjW/JFmKsvfSmB2qprVWihzw7gloDXFPQO3zuj1T55d+FAbqxH7OMZbQmVplGn5n+16F3G bBFvdCygTgTLnDwcZZHl3czgebQJlyigI8mOfBKDt2RDRoey5/byQmgvBMBWQWhPZRSEShgOTIlqg 8JfcPtTr9OgpF8qRHxy6sYLTEub8GbU7srxXMNmEJ1dqsLFhdalWaXzhiIN504bhEEBRYoRYsB64K ALee5bUQ==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x3XtM-00DQV3-2R for pgsql-bugs@lists.postgresql.org; Mon, 07 Sep 2026 11:52:04 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1x3XtL-00000000aXm-17xb for pgsql-bugs@lists.postgresql.org; Mon, 07 Sep 2026 11:52:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19664: nbtree: Assertion failure when a custom index AM reuses bthandler To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: mrdrivingduck@gmail.com Reply-To: mrdrivingduck@gmail.com, pgsql-bugs@lists.postgresql.org Date: Mon, 07 Sep 2026 11:51:29 +0000 Message-ID: <19664-35af2d7fa85785d6@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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: =20 ## 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 <=3D, OPERATOR 3 =3D, OPERATOR 4 >=3D, 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 =3D=3D RELKIND_INDEX && wstate->index->rd_rel->relam =3D=3D 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 =3D btoptions, making the layout compatible. Reusing built-in in= dex 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 =3D=3D RELKIND_INDEX && \ relation->rd_indam->amoptions =3D=3D btoptions), \ ...) #define BTGetDeduplicateItems(relation) \ (AssertMacro(relation->rd_rel->relkind =3D=3D RELKIND_INDEX && \ relation->rd_indam->amoptions =3D=3D btoptions), \ ...)