-- test/sql/class_rep_dnf.sql -- -- A character class UNDER a repetition that the extractor inlines fewer -- times than a match may hold it must NOT take the exact-pieces (DNF) -- emission. PIECES enumerates the run as a product of independent -- single-character positions; for 'a[bc]{2,5}d' that product is -- abbd|abcd|acbd|accd, but a real match like 'abcbd' equals none of them -- (its trigrams come from different concrete strings), so the index -- dropped the row before the recheck could see it. The sound CNF path -- requires each trigram window on its own and keeps the row. -- -- Regression: 4.6.0 (d0d0300, "tighter k=0 trigram extraction") through -- 4.6.5 returned fewer rows through the index than a sequential scan for -- these patterns. CREATE EXTENSION IF NOT EXISTS pg_tre; \set VERBOSITY terse SET client_min_messages = warning; -- The buggy shapes fall back to CNF, not PIECES: a variable-count class -- rep, and a fixed-count class rep inlined fewer than min_rep times. SELECT tre_extract_debug('a[bc]{2,5}d') LIKE '%CNF%' AS var_class_rep_is_cnf; SELECT tre_extract_debug('a[bc]{3}d') LIKE '%CNF%' AS fixed_class_rep_is_cnf; -- A class that occurs once (no repetition) still takes PIECES: the fix -- only touches class positions produced by an inexact rep inline. SELECT tre_extract_debug('aa[bc]c[b-d]') LIKE '%PIECES%' AS single_class_still_pieces; -- A fixed-count LITERAL rep is fully inlined and class-free, so it is -- unaffected (su{3} is the issue-#2 tightening case). SELECT tre_extract_debug('su{3}') LIKE '%PIECES%' OR tre_extract_debug('su{3}') LIKE '%CNF%' AS literal_rep_extracts; -- End-to-end: the index must return exactly the sequential-scan rows. -- Rows mirror the bug report's reproducer. DROP TABLE IF EXISTS crt; CREATE TABLE crt (id int, s text); INSERT INTO crt VALUES (1,'abcbd'), (2,'abbd'), (3,'acbcbd'), (4,'abcbd x'), (5,'accbd'), (6,'xyz'); INSERT INTO crt SELECT g, 'filler'||g FROM generate_series(100,2100) g; CREATE INDEX crt_tre ON crt USING tre (s); ANALYZE crt; CREATE FUNCTION crt_agree(pat text) RETURNS boolean LANGUAGE plpgsql AS $$ DECLARE a int[]; b int[]; c int[]; d int[]; BEGIN -- index (bitmap) vs seq for the TRE operator at k=0 PERFORM set_config('enable_seqscan','off',true); PERFORM set_config('enable_bitmapscan','on',true); PERFORM set_config('enable_indexscan','off',true); EXECUTE 'SELECT array_agg(id ORDER BY id) FROM crt WHERE s %~~ tre_pattern($1,0)' INTO a USING pat; EXECUTE 'SELECT array_agg(id ORDER BY id) FROM crt WHERE s ~ $1' INTO c USING pat; PERFORM set_config('enable_seqscan','on',true); PERFORM set_config('enable_bitmapscan','off',true); PERFORM set_config('enable_indexscan','off',true); EXECUTE 'SELECT array_agg(id ORDER BY id) FROM crt WHERE tre_amatch(s,$1,0)' INTO b USING pat; EXECUTE 'SELECT array_agg(id ORDER BY id) FROM crt WHERE s ~ $1' INTO d USING pat; RETURN a IS NOT DISTINCT FROM b AND c IS NOT DISTINCT FROM d; END $$; SELECT bool_and(crt_agree(p)) AS index_eq_seq FROM (VALUES ('a[bc]{2,5}d'), ('a[bc]{3}d'), ('a[bc]{2}d'), ('[bc]{2,4}'), ('x[0-9]{2,4}z')) v(p); DROP TABLE crt; DROP FUNCTION crt_agree(text);