From 4a2d39891e1f3801ee68647c9b0aa49ad7027b2c Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu, 2 Jul 2026 17:27:31 -0400
Subject: [PATCH v3 3/5] Use the proper comparator in gbt_bit_ssup_cmp.

If we're dealing with leaf entries, the function to call is bitcmp
not byteacmp.  Using byteacmp didn't lead to any obvious failure,
but it did result in sorting the entries in a way not matching the
datatype's actual sort order, so that the constructed index would
be much less efficient than one would expect, and in particular
worse than what you got before this code was added in v18.

We might want to recommend that users reindex btree_gist indexes
on bit/varbit.

Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/AH*AvQCYKhQGVvPWi1GiU4oY.8.1781609375063.Hmail.3020001251@tju.edu.cn
Backpatch-through: 18
---
 contrib/btree_gist/btree_bit.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/contrib/btree_gist/btree_bit.c b/contrib/btree_gist/btree_bit.c
index dcfe6ed17e4..8d8e9454268 100644
--- a/contrib/btree_gist/btree_bit.c
+++ b/contrib/btree_gist/btree_bit.c
@@ -245,8 +245,7 @@ gbt_bit_ssup_cmp(Datum x, Datum y, SortSupport ssup)
 	Datum		result;
 
 	/* for leaf items we expect lower == upper, so only compare lower */
-	/* XXX shouldn't this use bitcmp() ? */
-	result = DirectFunctionCall2(byteacmp,
+	result = DirectFunctionCall2(bitcmp,
 								 PointerGetDatum(arg1.lower),
 								 PointerGetDatum(arg2.lower));
 
-- 
2.52.0

