-- ranked_phrase_mem (1.18.2, reported by txfs19260817): a ranked boolean query -- that CONTAINS a phrase must decode the phrase operand's position-bearing -- posting list through the huge-alloc path, and must return correct results. -- -- Through 1.18.1 the phrase/position path in the scan allocated a PosPosting -- buffer with a plain palloc sized to the term's document frequency (np): -- bm25_decode_term already routes its own df-scaled buffers through -- MemoryContextAllocHuge, but three callers (bm25_lookup_term's TID buffer, the -- k-way merge output, and the position-decode PosPosting buffer) then re-copied -- np entries with a bare palloc. PosPosting is wider than ItemPointerData, so a -- common phrase term at corpus scale overran MaxAllocSize ("invalid memory -- alloc request size", 575/3762 of the field trace, one client). The fix -- routes all three through FTS_ALLOC_MAYBE_HUGE; ci/check-alloc.sh now covers -- the np/total drivers so the class cannot regress. -- -- wide_filter_mem guards the bare @@@ FILTER collect path; this guards the -- RANKED (ORDER BY <=> LIMIT) path with a phrase operand, which uses the -- position decode. A small corpus cannot reach MaxAllocSize, so this asserts -- the behaviour the fix preserves: the phrase/position path returns the correct -- ranked rows, matching the heap recheck, with tombstones scattered across the -- decode. SET client_min_messages = warning; CREATE EXTENSION IF NOT EXISTS pg_fts; LOAD 'pg_fts'; -- positions = on so the phrase operand is answered from the postings (the -- position-decode path), not an exact heap recheck. CREATE TABLE rpm (id int PRIMARY KEY, body text); -- 'quick' is common (~1/4 of rows), the phrase 'brown fox' appears where both -- words are adjacent (~1/12), so 'quick | (brown <-> fox)' is a wide-ish ranked -- boolean query containing a phrase, like Zishuo's "'firebase' | ... | 'don' <-> -- 't' | ...". Document lengths vary. INSERT INTO rpm SELECT g, (SELECT string_agg('w' || (1 + (g * 7919 + i * 104729) % 149), ' ') FROM generate_series(1, 5 + (g * 29) % 50) i) || CASE WHEN g % 4 = 0 THEN ' quick' ELSE '' END || CASE WHEN g % 12 = 5 THEN ' brown fox' ELSE '' END FROM generate_series(1, 6000) g; CREATE INDEX rpm_idx ON rpm USING fts (to_ftsdoc('simple', body)) WITH (positions = on); VACUUM ANALYZE rpm; SET max_parallel_workers_per_gather = 0; -- Scatter deletes so the phrase decode crosses tombstones, then VACUUM. DELETE FROM rpm WHERE id % 9 = 0; VACUUM rpm; -- The ranked phrase-bearing boolean query (the collect/position path) must -- return the SAME rows as the heap recheck of the same predicate, both as a set -- and in BM25 order. Captured in strict row order via row_number() over the -- ordered subquery. CREATE TEMP TABLE ranked AS SELECT id, row_number() OVER () n FROM (SELECT id FROM rpm WHERE to_ftsdoc('simple', body) @@@ to_ftsquery('simple', 'quick | (brown <-> fox)') ORDER BY to_ftsdoc('simple', body) <=> to_ftsquery('simple', 'quick | (brown <-> fox)') LIMIT 100000) s; -- heap truth: every live row matching the predicate (set only, order unchecked). CREATE TEMP TABLE truth AS SELECT id FROM rpm WHERE fts_match(to_ftsdoc('simple', body), to_ftsquery('simple', 'quick | (brown <-> fox)')); -- Same live row set, and no deleted row leaked into the ranked result. SELECT (SELECT array_agg(id ORDER BY id) FROM ranked) = (SELECT array_agg(id ORDER BY id) FROM truth) AS ranked_matches_heap, (SELECT count(*) FROM ranked WHERE id % 9 = 0) AS deleted_leaked; -- The ranked result is in BM25 order: equals the forced ordering-index-scan -- order (the position-decode path is the same either way under the fix). SET enable_bitmapscan = off; SET enable_seqscan = off; CREATE TEMP TABLE ranked_forced AS SELECT id, row_number() OVER () n FROM (SELECT id FROM rpm WHERE to_ftsdoc('simple', body) @@@ to_ftsquery('simple', 'quick | (brown <-> fox)') ORDER BY to_ftsdoc('simple', body) <=> to_ftsquery('simple', 'quick | (brown <-> fox)') LIMIT 100000) s; RESET enable_bitmapscan; RESET enable_seqscan; SELECT (SELECT array_agg(id ORDER BY n) FROM ranked) = (SELECT array_agg(id ORDER BY n) FROM ranked_forced) AS ranked_order_matches_forced;