postgres.git / summary / log / commit / refs

commit    21b7a3fd683e2ef1923007fdda93d72d7b611bc3
Author:   Michael Paquier <michael@paquier.xyz>
Date:     Wed Nov 11 00:29:48 2026 +0000
First released in: 19.0

    Fix uninitialized cache use in gtsvector_picksplit()
    
    When a page split involves two entries, the seed selection loop does not
    run and the seeds default to entries 1 and 2.  The cache of the last
    entry was filled only after the initial union keys were built from it,
    hence the right side of the union could be computed from memory that was
    not initialized.  This could cause index scans to miss matching rows.
    
    This issue is fixed by filling the cache of the last entry before
    forming the initial unions, by moving around a fillcache().
    
    GiST indexes based on tsvector may need a REINDEX, if these are based on
    a single column.
    
    Issue spotted originally by valgrind.
    
    Reported-by: Alexander Lakhin <exclusion@gmail.com>
    Author: Shihao Zhong <zhong950419@gmail.com>
    Reviewed-by: Michael Paquier <michael@paquier.xyz>
    Discussion: https://postgr.es/m/19780-93fb5c0e4ec2af18@postgresql.org
    Backpatch-through: 15


src/backend/utils/adt/tsgistidx.c | 6 ++++-- src/test/regress/expected/tsearch.out | 17 +++++++++++++++++ src/test/regress/sql/tsearch.sql | 13 +++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c index 5698ee5502b..0deea2871a4 100644 --- a/src/backend/utils/adt/tsgistidx.c +++ b/src/backend/utils/adt/tsgistidx.c @@ -687,6 +687,10 @@ gtsvector_picksplit(PG_FUNCTION_ARGS) seed_2 = 2; } + /* seed_2 can be the last entry, so fill its cache first */ + maxoff = OffsetNumberNext(maxoff); + fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff), siglen); + /* form initial .. */ datum_l = gtsvector_alloc(SIGNKEY | (cache[seed_1].allistrue ? ALLISTRUE : 0), siglen, cache[seed_1].sign); @@ -694,8 +698,6 @@ gtsvector_picksplit(PG_FUNCTION_ARGS) siglen, cache[seed_2].sign); union_l = GETSIGN(datum_l); union_r = GETSIGN(datum_r); - maxoff = OffsetNumberNext(maxoff); - fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff), siglen); /* sort before ... */ costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff); for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j)) diff --git a/src/test/regress/expected/tsearch.out b/src/test/regress/expected/tsearch.out index 38ddb7aa64e..b150f69527e 100644 --- a/src/test/regress/expected/tsearch.out +++ b/src/test/regress/expected/tsearch.out @@ -870,6 +870,23 @@ RESET enable_seqscan; RESET enable_indexscan; RESET enable_bitmapscan; DROP INDEX wowidx; +-- Check picksplit with only two entries. +CREATE TABLE test_tsvector_wide (a tsvector, b text); +-- The EXTERNAL INCLUDE column means that two index tuples do not fit on +-- one leaf page. The second INSERT splits that page. +ALTER TABLE test_tsvector_wide ALTER COLUMN b SET STORAGE EXTERNAL; +CREATE INDEX ON test_tsvector_wide USING gist (a) INCLUDE (b); +INSERT INTO test_tsvector_wide VALUES ('foo', repeat('x', 5000)); +INSERT INTO test_tsvector_wide SELECT 'bar', b FROM test_tsvector_wide; +SET enable_seqscan = off; +SELECT count(*) FROM test_tsvector_wide WHERE a @@ 'bar'; + count +------- + 1 +(1 row) + +RESET enable_seqscan; +DROP TABLE test_tsvector_wide; CREATE INDEX wowidx ON test_tsvector USING gin (a); SET enable_seqscan=OFF; -- GIN only supports bitmapscan, so no need to test plain indexscan diff --git a/src/test/regress/sql/tsearch.sql b/src/test/regress/sql/tsearch.sql index bcaff947dca..b2d629c3add 100644 --- a/src/test/regress/sql/tsearch.sql +++ b/src/test/regress/sql/tsearch.sql @@ -222,6 +222,19 @@ RESET enable_bitmapscan; DROP INDEX wowidx; +-- Check picksplit with only two entries. +CREATE TABLE test_tsvector_wide (a tsvector, b text); +-- The EXTERNAL INCLUDE column means that two index tuples do not fit on +-- one leaf page. The second INSERT splits that page. +ALTER TABLE test_tsvector_wide ALTER COLUMN b SET STORAGE EXTERNAL; +CREATE INDEX ON test_tsvector_wide USING gist (a) INCLUDE (b); +INSERT INTO test_tsvector_wide VALUES ('foo', repeat('x', 5000)); +INSERT INTO test_tsvector_wide SELECT 'bar', b FROM test_tsvector_wide; +SET enable_seqscan = off; +SELECT count(*) FROM test_tsvector_wide WHERE a @@ 'bar'; +RESET enable_seqscan; +DROP TABLE test_tsvector_wide; + CREATE INDEX wowidx ON test_tsvector USING gin (a); SET enable_seqscan=OFF; [parent: 61afc2ecdbe7]