Add planner support function for starts_with()

Started by Tom Laneover 4 years ago3 messageshackers
Jump to latest
#1Tom Lane
tgl@sss.pgh.pa.us

When starts_with() and the equivalent ^@ operator were added, they
were plugged into the planner in only a rather half-baked way.
Selectivity estimation got taught about the operator, but the
other infrastructure associated with LIKE/regex matching wasn't
updated. This causes these operators to be planned more stupidly
than a functionally-equivalent LIKE/regex pattern [1]/messages/by-id/CADT4RqB13KQHOJqqQ+WXmYtJrukS2UiFdtfTvT-XA3qYLyB6Cw@mail.gmail.com.

With the (admittedly later) introduction of planner support functions,
it's really quite easy to do better. The attached patch adds a planner
support function for starts_with(), with these benefits:

* A condition such as "textcol ^@ constant" can now use a regular
btree index, not only an SP-GiST index, so long as the index's
collation is C. (This works just like "textcol LIKE 'foo%'".)

* "starts_with(textcol, constant)" can be optimized the same as
"textcol ^@ constant".

I also rejiggered match_pattern_prefix() a bit, with the effect
that fixed-prefix LIKE and regex patterns are now more like
starts_with() in another way: if you apply one to an SPGiST-indexed
column, you'll get an index condition using ^@ rather than two
index conditions with >= and <. That should be more efficient
at runtime, though I didn't try to do any performance testing.

regards, tom lane

[1]: /messages/by-id/CADT4RqB13KQHOJqqQ+WXmYtJrukS2UiFdtfTvT-XA3qYLyB6Cw@mail.gmail.com

Attachments:

planner-support-for-starts-with-1.patchtext/x-diff; charset=us-ascii; name=planner-support-for-starts-with-1.patchDownload+94-22
#2Nathan Bossart
nathandbossart@gmail.com
In reply to: Tom Lane (#1)
Re: Add planner support function for starts_with()

On 10/9/21, 10:24 AM, "Tom Lane" <tgl@sss.pgh.pa.us> wrote:

With the (admittedly later) introduction of planner support functions,
it's really quite easy to do better. The attached patch adds a planner
support function for starts_with(), with these benefits:

The patch looks reasonable to me.

Nathan

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Nathan Bossart (#2)
Re: Add planner support function for starts_with()

"Bossart, Nathan" <bossartn@amazon.com> writes:

On 10/9/21, 10:24 AM, "Tom Lane" <tgl@sss.pgh.pa.us> wrote:

With the (admittedly later) introduction of planner support functions,
it's really quite easy to do better. The attached patch adds a planner
support function for starts_with(), with these benefits:

The patch looks reasonable to me.

Pushed, thanks for reviewing!

regards, tom lane