-- 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; psql:test/sql/class_rep_dnf.sql:16: NOTICE: extension "pg_tre" already exists, skipping CREATE EXTENSION \set VERBOSITY terse SET client_min_messages = warning; SET -- 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; var_class_rep_is_cnf ---------------------- t (1 row) SELECT tre_extract_debug('a[bc]{3}d') LIKE '%CNF%' AS fixed_class_rep_is_cnf; fixed_class_rep_is_cnf ------------------------ t (1 row) -- 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; single_class_still_pieces --------------------------- t (1 row) -- 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; literal_rep_extracts ---------------------- t (1 row) -- End-to-end: the index must return exactly the sequential-scan rows. -- Rows mirror the bug report's reproducer. DROP TABLE IF EXISTS crt; DROP TABLE CREATE TABLE crt (id int, s text); CREATE TABLE INSERT INTO crt VALUES (1,'abcbd'), (2,'abbd'), (3,'acbcbd'), (4,'abcbd x'), (5,'accbd'), (6,'xyz'); INSERT 0 6 INSERT INTO crt SELECT g, 'filler'||g FROM generate_series(100,2100) g; INSERT 0 2001 CREATE INDEX crt_tre ON crt USING tre (s); CREATE INDEX ANALYZE crt; ANALYZE 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 $$; CREATE FUNCTION 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); index_eq_seq -------------- t (1 row) DROP TABLE crt; DROP TABLE DROP FUNCTION crt_agree(text); DROP FUNCTION