pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: kehan5800@gmail.com
Subject: BUG #19749: bpchar_ops declares equalimage although bpchar equality ignores trailing spaces
Date: Sun, 04 Oct 2026 05:20:01 +0000
Message-ID: <19749-d46adf7c7d910b07@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      19749
Logged by:          Ke
Email address:      kehan5800@gmail.com
PostgreSQL version: 18.6
Operating system:   Ubuntu 22.04.2 x86_64
Description:        

bpchar_ops registers btvarstrequalimage as btree support function 4
(src/include/catalog/pg_amproc.dat:33-35). btvarstrequalimage
(src/backend/utils/adt/varlena.c:2311) returns true for any deterministic
collation. But bpchar equality ignores trailing spaces, so for bpchar
"equal" does not imply "identical image". A bpchar column without typmod
(CREATE TABLE t (c bpchar)) stores values at their own length, so such
pairs can coexist in one index:

    SELECT 'a'::bpchar = 'a  '::bpchar,                      -- t
           octet_length('a'::bpchar), octet_length('a  '::bpchar);   -- 1, 3

On an assert-enabled build, a plain CREATE INDEX hits the check in
_bt_keep_natts() (src/backend/access/nbtree/nbtutils.c:882-883):

    CREATE TABLE t (c bpchar);
    INSERT INTO t SELECT x FROM generate_series(1, 500),
                       (VALUES ('a'::bpchar), ('a  '::bpchar)) v(x);
    CREATE INDEX ti ON t (c);

    TRAP: failed Assert("!itup_key->allequalimage || keepnatts ==
      _bt_keep_natts_fast(rel, lastleft, firstright)"),
      File: "nbtutils.c", Line: 882
    LOG:  client backend (PID ...) was terminated by signal 6: Aborted

The assertion is only reached on a leaf page split, so small tables do not
show it: 365 rows (730 index tuples) build fine, 370 rows abort.

This is the same assertion that interval_ops tripped in 2023
(https://www.postgresql.org/message-id/20231011013317.22.nmisch@google.com),
fixed by removing interval_ops' equalimage support function. In that
message's audit, bpchar_ops is listed under btvarstrequalimage and was
judged okay. Of the opclasses using btvarstrequalimage (bpchar_ops,
text_ops for text and name, varchar_ops via text), only bpchar has an
equality that ignores part of the value.

Impact on a non-assert build (checked on an 18.6 build without assertions):
I could not demonstrate wrong results.
amcheck bt_index_parent_check(heapallindexed, rootdescend) passes, index
and sequential scans agree, index-only scans return the heap's bytes, and
deduplication does not merge 'a' and 'a  ' (_bt_dedup_pass compares images,
which differ). The demonstrated problem is a reachable Assert (developer
and buildfarm builds) plus an optimisation whose stated precondition
(equal implies image-equal) does not hold for this opclass.

Expected: bpchar_ops does not claim equalimage, or CREATE INDEX on bpchar
does not violate the nbtree invariant.

Actual: allequalimage is set for bpchar indexes, and an assert-enabled
server aborts on CREATE INDEX with equal-but-differently-padded values.

Suggested fix (the same as for interval_ops): drop the amprocnum 4 entry
for btree/bpchar_ops from pg_amproc.dat (catversion bump). As with
interval_ops, existing indexes keep the allequalimage flag in their
metapage, so a REINDEX note would be needed if back-patched. A
typmod-dependent answer is not possible, since equalimage is called per
opclass and collation, not per column.








view thread (2+ messages)  latest in thread

Message-ID: <19749-d46adf7c7d910b07@postgresql.org>
Permalink:  ../19749-d46adf7c7d910b07@postgresql.org/
Also on:    postgresql.org/message-id/19749-d46adf7c7d910b07@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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, kehan5800@gmail.com
  Subject: Re: BUG #19749: bpchar_ops declares equalimage although bpchar equality ignores trailing spaces
  In-Reply-To: <19749-d46adf7c7d910b07@postgresql.org>
  References: <19749-d46adf7c7d910b07@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 DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox