Improve the performance of LIKE/regex estimation in non-C locales, by making
authorTom Lane <[email protected]>
Wed, 7 Nov 2007 22:37:43 +0000 (22:37 +0000)
committerTom Lane <[email protected]>
Wed, 7 Nov 2007 22:37:43 +0000 (22:37 +0000)
make_greater_string() try harder to generate a string that's actually greater
than its input string.  Before we just assumed that making a string that was
memcmp-greater was enough, but it is easy to generate examples where this is
not so when the locale is not C.  Instead, loop until the relevant comparison
function agrees that the generated string is greater than the input.

Unfortunately this is probably not enough to guarantee that the generated
string is greater than all extensions of the input, so we cannot relax the
restriction to C locale for the LIKE/regex index optimization.  But it should
at least improve the odds of getting a useful selectivity estimate in
prefix_selectivity().  Per example from Guillaume Smet.

Backpatch to 8.1, mainly because that's what the complainant is using...

src/backend/optimizer/path/indxpath.c
src/backend/utils/adt/selfuncs.c
src/include/utils/selfuncs.h

index 2253f81c435fa50281bb0208e96d7fecf3dd36f5..e3fbcbf6c712159c452f5cd42e101617e9775f78 100644 (file)
@@ -2374,6 +2374,7 @@ prefix_quals(Node *leftop, Oid opclass,
        Oid                     datatype;
        Oid                     oproid;
        Expr       *expr;
+       FmgrInfo        ltproc;
        Const      *greaterstr;
 
        Assert(pstatus != Pattern_Prefix_None);
@@ -2470,13 +2471,14 @@ prefix_quals(Node *leftop, Oid opclass,
         * "x < greaterstr".
         *-------
         */
-       greaterstr = make_greater_string(prefix_const);
+       oproid = get_opclass_member(opclass, InvalidOid,
+                                                               BTLessStrategyNumber);
+       if (oproid == InvalidOid)
+               elog(ERROR, "no < operator for opclass %u", opclass);
+       fmgr_info(get_opcode(oproid), &ltproc);
+       greaterstr = make_greater_string(prefix_const, &ltproc);
        if (greaterstr)
        {
-               oproid = get_opclass_member(opclass, InvalidOid,
-                                                                       BTLessStrategyNumber);
-               if (oproid == InvalidOid)
-                       elog(ERROR, "no < operator for opclass %u", opclass);
                expr = make_opclause(oproid, BOOLOID, false,
                                                         (Expr *) leftop, (Expr *) greaterstr);
                result = lappend(result, make_restrictinfo(expr, true, false, NULL));
index c05753fb79895ee79832cf2486df81c0ddf22df4..9bbfc6aa32cf0c17cfce0c73fd61ff98c552acdf 100644 (file)
@@ -3747,6 +3747,7 @@ prefix_selectivity(PlannerInfo *root, Node *variable,
        Selectivity prefixsel;
        Oid                     cmpopr;
        List       *cmpargs;
+       FmgrInfo        ltproc;
        Const      *greaterstrcon;
 
        cmpopr = get_opclass_member(opclass, InvalidOid,
@@ -3766,15 +3767,17 @@ prefix_selectivity(PlannerInfo *root, Node *variable,
         *      "x < greaterstr".
         *-------
         */
-       greaterstrcon = make_greater_string(prefixcon);
+       cmpopr = get_opclass_member(opclass, InvalidOid,
+                                                               BTLessStrategyNumber);
+       if (cmpopr == InvalidOid)
+               elog(ERROR, "no < operator for opclass %u", opclass);
+       fmgr_info(get_opcode(cmpopr), &ltproc);
+
+       greaterstrcon = make_greater_string(prefixcon, &ltproc);
        if (greaterstrcon)
        {
                Selectivity topsel;
 
-               cmpopr = get_opclass_member(opclass, InvalidOid,
-                                                                       BTLessStrategyNumber);
-               if (cmpopr == InvalidOid)
-                       elog(ERROR, "no < operator for opclass %u", opclass);
                cmpargs = list_make2(variable, greaterstrcon);
                /* Assume scalarltsel is appropriate for all supported types */
                topsel = DatumGetFloat8(DirectFunctionCall4(scalarltsel,
@@ -4074,8 +4077,17 @@ pattern_selectivity(Const *patt, Pattern_Type ptype)
  * in the form of a Const pointer; else return NULL.
  *
  * The key requirement here is that given a prefix string, say "foo",
- * we must be able to generate another string "fop" that is greater
- * than all strings "foobar" starting with "foo".
+ * we must be able to generate another string "fop" that is greater than
+ * all strings "foobar" starting with "foo".  We can test that we have
+ * generated a string greater than the prefix string, but in non-C locales
+ * that is not a bulletproof guarantee that an extension of the string might
+ * not sort after it; an example is that "foo " is less than "foo!", but it
+ * is not clear that a "dictionary" sort ordering will consider "foo!" less
+ * than "foo bar".  Therefore, this function should be used only for
+ * estimation purposes when working in a non-C locale.
+ *
+ * The caller must provide the appropriate "less than" comparison function
+ * for testing the strings.
  *
  * If we max out the righthand byte, truncate off the last character
  * and start incrementing the next.  For example, if "z" were the last
@@ -4084,20 +4096,15 @@ pattern_selectivity(Const *patt, Pattern_Type ptype)
  *
  * This could be rather slow in the worst case, but in most cases we
  * won't have to try more than one or two strings before succeeding.
- *
- * NOTE: at present this assumes we are in the C locale, so that simple
- * bytewise comparison applies.  However, we might be in a multibyte
- * encoding such as UTF8, so we do have to watch out for generating
- * invalid encoding sequences.
  */
 Const *
-make_greater_string(const Const *str_const)
+make_greater_string(const Const *str_const, FmgrInfo *ltproc)
 {
        Oid                     datatype = str_const->consttype;
        char       *workstr;
        int                     len;
 
-       /* Get the string and a modifiable copy */
+       /* Get a modifiable copy of the string in C-string format */
        if (datatype == NAMEOID)
        {
                workstr = DatumGetCString(DirectFunctionCall1(nameout,
@@ -4151,8 +4158,18 @@ make_greater_string(const Const *str_const)
                        else
                                workstr_const = string_to_bytea_const(workstr, len);
 
-                       pfree(workstr);
-                       return workstr_const;
+                       if (DatumGetBool(FunctionCall2(ltproc,
+                                                                                  str_const->constvalue,
+                                                                                  workstr_const->constvalue)))
+                       {
+                               /* Successfully made a string larger than the input */
+                               pfree(workstr);
+                               return workstr_const;
+                       }
+
+                       /* No good, release unusable value and try again */
+                       pfree(DatumGetPointer(workstr_const->constvalue));
+                       pfree(workstr_const);
                }
 
                /* restore last byte so we don't confuse pg_mbcliplen */
index 1da8de090fd49daed2fb45b0d86c0c8b5f7391a8..d0a468e8d059781ef5da82a762e04c4e4b0685a5 100644 (file)
@@ -80,7 +80,7 @@ extern Pattern_Prefix_Status pattern_fixed_prefix(Const *patt,
                                         Pattern_Type ptype,
                                         Const **prefix,
                                         Const **rest);
-extern Const *make_greater_string(const Const *str_const);
+extern Const *make_greater_string(const Const *str_const, FmgrInfo *ltproc);
 
 extern Datum eqsel(PG_FUNCTION_ARGS);
 extern Datum neqsel(PG_FUNCTION_ARGS);