postgresql/contrib/btree_gist/expected/char_1.out
Tom Lane 54b1cb7eb7 Fix results of index-only scans on btree_gist char(N) indexes.
If contrib/btree_gist is used to make a GIST index on a char(N)
(bpchar) column, and that column is retrieved via an index-only
scan, what came out had all trailing spaces removed.  Since
that doesn't happen in any other kind of table scan, this is
clearly a bug.  The cause is that gbt_bpchar_compress() strips
trailing spaces (using rtrim1) before a new index entry is made.
That was probably a good idea when this code was first written,
but since we invented index-only scans, it's not so good.

One answer could be to mark this opclass as incapable of index-only
scans.  But to do so, we'd need an extension module version bump,
followed by manual action by DBAs to install the updated version
of btree_gist.  And it's not really a desirable place to end up,
anyway.

Instead, let's fix the code by removing the unwanted space-stripping
action and adjusting the opclass's comparison logic to ignore
trailing spaces as bpchar normally does.  This will not hinder
cases that work today, since index searches with this logic will
act the same whether trailing spaces are stored or not.  It will
not by itself fix the problem of getting space-stripped results
from index-only scans, of course.  Users who care about that can
REINDEX affected indexes after installing this update, to immediately
replace all improperly-truncated index entries.  Otherwise, it can
be expected that the index's behavior will change incrementally as
old entries are replaced by new ones.

Per report from Alexander Lakhin.  Back-patch to all supported branches.

Discussion: https://postgr.es/m/696c995b-b37f-5526-f45d-04abe713179f@gmail.com
2022-01-08 14:54:39 -05:00

83 lines
1.6 KiB
Plaintext

-- char check
CREATE TABLE chartmp (a char(32));
\copy chartmp from 'data/char.data'
SET enable_seqscan=on;
SELECT count(*) FROM chartmp WHERE a < '31b0'::char(32);
count
-------
773
(1 row)
SELECT count(*) FROM chartmp WHERE a <= '31b0'::char(32);
count
-------
774
(1 row)
SELECT count(*) FROM chartmp WHERE a = '31b0'::char(32);
count
-------
1
(1 row)
SELECT count(*) FROM chartmp WHERE a >= '31b0'::char(32);
count
-------
215
(1 row)
SELECT count(*) FROM chartmp WHERE a > '31b0'::char(32);
count
-------
214
(1 row)
CREATE INDEX charidx ON chartmp USING GIST ( a );
SET enable_seqscan=off;
SELECT count(*) FROM chartmp WHERE a < '31b0'::char(32);
count
-------
773
(1 row)
SELECT count(*) FROM chartmp WHERE a <= '31b0'::char(32);
count
-------
774
(1 row)
SELECT count(*) FROM chartmp WHERE a = '31b0'::char(32);
count
-------
1
(1 row)
SELECT count(*) FROM chartmp WHERE a >= '31b0'::char(32);
count
-------
215
(1 row)
SELECT count(*) FROM chartmp WHERE a > '31b0'::char(32);
count
-------
214
(1 row)
-- Test index-only scans
SET enable_bitmapscan=off;
EXPLAIN (COSTS OFF)
SELECT * FROM chartmp WHERE a BETWEEN '31a' AND '31c';
QUERY PLAN
---------------------------------------------------------------
Index Only Scan using charidx on chartmp
Index Cond: ((a >= '31a'::bpchar) AND (a <= '31c'::bpchar))
(2 rows)
SELECT * FROM chartmp WHERE a BETWEEN '31a' AND '31c';
a
----------------------------------
31b0
(1 row)