Skip to content

Commit 5edd21d

Browse files
committed
Refactor pattern_fixed_prefix() to avoid dealing in incomplete patterns.
Previously, pattern_fixed_prefix() was defined to return whatever fixed prefix it could extract from the pattern, plus the "rest" of the pattern. That definition was sensible for LIKE patterns, but not so much for regexes, where reconstituting a valid pattern minus the prefix could be quite tricky (certainly the existing code wasn't doing that correctly). Since the only thing that callers ever did with the "rest" of the pattern was to pass it to like_selectivity() or regex_selectivity(), let's cut out the middle-man and just have pattern_fixed_prefix's subroutines do this directly. Then pattern_fixed_prefix can return a simple selectivity number, and the question of how to cope with partial patterns is removed from its API specification. While at it, adjust the API spec so that callers who don't actually care about the pattern's selectivity (which is a lot of them) can pass NULL for the selectivity pointer to skip doing the work of computing a selectivity estimate. This patch is only an API refactoring that doesn't actually change any processing, other than allowing a little bit of useless work to be skipped. However, it's necessary infrastructure for my upcoming fix to regex prefix extraction, because after that change there won't be any simple way to identify the "rest" of the regex, not even to the low level of fidelity needed by regex_selectivity. We can cope with that if regex_fixed_prefix and regex_selectivity communicate directly, but not if we have to work within the old API. Hence, back-patch to all active branches.
1 parent 490cb05 commit 5edd21d

File tree

3 files changed

+57
-127
lines changed

3 files changed

+57
-127
lines changed

src/backend/optimizer/path/indxpath.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,7 +2219,6 @@ match_special_index_operator(Expr *clause, Oid opfamily,
22192219
Oid expr_op;
22202220
Const *patt;
22212221
Const *prefix = NULL;
2222-
Const *rest = NULL;
22232222
Pattern_Prefix_Status pstatus = Pattern_Prefix_None;
22242223

22252224
/*
@@ -2247,13 +2246,13 @@ match_special_index_operator(Expr *clause, Oid opfamily,
22472246
case OID_NAME_LIKE_OP:
22482247
/* the right-hand const is type text for all of these */
22492248
pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like,
2250-
&prefix, &rest);
2249+
&prefix, NULL);
22512250
isIndexable = (pstatus != Pattern_Prefix_None);
22522251
break;
22532252

22542253
case OID_BYTEA_LIKE_OP:
22552254
pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like,
2256-
&prefix, &rest);
2255+
&prefix, NULL);
22572256
isIndexable = (pstatus != Pattern_Prefix_None);
22582257
break;
22592258

@@ -2262,7 +2261,7 @@ match_special_index_operator(Expr *clause, Oid opfamily,
22622261
case OID_NAME_ICLIKE_OP:
22632262
/* the right-hand const is type text for all of these */
22642263
pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like_IC,
2265-
&prefix, &rest);
2264+
&prefix, NULL);
22662265
isIndexable = (pstatus != Pattern_Prefix_None);
22672266
break;
22682267

@@ -2271,7 +2270,7 @@ match_special_index_operator(Expr *clause, Oid opfamily,
22712270
case OID_NAME_REGEXEQ_OP:
22722271
/* the right-hand const is type text for all of these */
22732272
pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex,
2274-
&prefix, &rest);
2273+
&prefix, NULL);
22752274
isIndexable = (pstatus != Pattern_Prefix_None);
22762275
break;
22772276

@@ -2280,7 +2279,7 @@ match_special_index_operator(Expr *clause, Oid opfamily,
22802279
case OID_NAME_ICREGEXEQ_OP:
22812280
/* the right-hand const is type text for all of these */
22822281
pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex_IC,
2283-
&prefix, &rest);
2282+
&prefix, NULL);
22842283
isIndexable = (pstatus != Pattern_Prefix_None);
22852284
break;
22862285

@@ -2536,7 +2535,6 @@ expand_indexqual_opclause(RestrictInfo *rinfo, Oid opfamily)
25362535
Oid expr_op = ((OpExpr *) clause)->opno;
25372536
Const *patt = (Const *) rightop;
25382537
Const *prefix = NULL;
2539-
Const *rest = NULL;
25402538
Pattern_Prefix_Status pstatus;
25412539

25422540
/*
@@ -2556,7 +2554,7 @@ expand_indexqual_opclause(RestrictInfo *rinfo, Oid opfamily)
25562554
if (!op_in_opfamily(expr_op, opfamily))
25572555
{
25582556
pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like,
2559-
&prefix, &rest);
2557+
&prefix, NULL);
25602558
return prefix_quals(leftop, opfamily, prefix, pstatus);
25612559
}
25622560
break;
@@ -2568,7 +2566,7 @@ expand_indexqual_opclause(RestrictInfo *rinfo, Oid opfamily)
25682566
{
25692567
/* the right-hand const is type text for all of these */
25702568
pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like_IC,
2571-
&prefix, &rest);
2569+
&prefix, NULL);
25722570
return prefix_quals(leftop, opfamily, prefix, pstatus);
25732571
}
25742572
break;
@@ -2580,7 +2578,7 @@ expand_indexqual_opclause(RestrictInfo *rinfo, Oid opfamily)
25802578
{
25812579
/* the right-hand const is type text for all of these */
25822580
pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex,
2583-
&prefix, &rest);
2581+
&prefix, NULL);
25842582
return prefix_quals(leftop, opfamily, prefix, pstatus);
25852583
}
25862584
break;
@@ -2592,7 +2590,7 @@ expand_indexqual_opclause(RestrictInfo *rinfo, Oid opfamily)
25922590
{
25932591
/* the right-hand const is type text for all of these */
25942592
pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex_IC,
2595-
&prefix, &rest);
2593+
&prefix, NULL);
25962594
return prefix_quals(leftop, opfamily, prefix, pstatus);
25972595
}
25982596
break;

0 commit comments

Comments
 (0)