Fix incorrect handling of lookahead constraints in pg_regprefix().
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 19 Oct 2015 20:54:53 +0000 (13:54 -0700)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 19 Oct 2015 20:54:53 +0000 (13:54 -0700)
commit52f21c5882ebad18d4cfcd67d99d31b90397ce29
tree2b3bb4e43ceb09e22ff030e76f8abbae74bdd3c0
parenta850d7136fd0e1220be32df8646117f7017d67d6
Fix incorrect handling of lookahead constraints in pg_regprefix().

pg_regprefix was doing nothing with lookahead constraints, which would
be fine if it were the right kind of nothing, but it isn't: we have to
terminate our search for a fixed prefix, not just pretend the LACON arc
isn't there.  Otherwise, if the current state has both a LACON outarc and a
single plain-color outarc, we'd falsely conclude that the color represents
an addition to the fixed prefix, and generate an extracted index condition
that restricts the indexscan too much.  (See added regression test case.)

Terminating the search is conservative: we could traverse the LACON arc
(thus assuming that the constraint can be satisfied at runtime) and then
examine the outarcs of the linked-to state.  But that would be a lot more
work than it seems worth, because writing a LACON followed by a single
plain character is a pretty silly thing to do.

This makes a difference only in rather contrived cases, but it's a bug,
so back-patch to all supported branches.
src/backend/regex/regprefix.c
src/test/regress/expected/regex.out
src/test/regress/sql/regex.sql