From c9b0a910ba9ba3f1857e36ef1e6a44fd9df31097 Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Sat, 21 Nov 2026 12:00:43 -0500 Subject: [PATCH v4 1/2] Use statistics on the call for pattern-matching functions like_regex_support() now leaves the estimate to boolvarsel() when the call has statistics, as in commit 1eccb931507. Reported-by: Fujii Masao Discussion: https://postgr.es/m/CAHGQGwF1nvAh+Zspg24Qwy6ZSXfiHAH30Z8DW36yArrX4VTd8g@mail.gmail.com --- src/backend/utils/adt/like_support.c | 30 +++++++++++++++++++++++++ src/test/regress/expected/stats_ext.out | 8 +++++++ src/test/regress/sql/stats_ext.sql | 3 +++ 3 files changed, 41 insertions(+) diff --git a/src/backend/utils/adt/like_support.c b/src/backend/utils/adt/like_support.c index 588425b3812..3a1bb4c1848 100644 --- a/src/backend/utils/adt/like_support.c +++ b/src/backend/utils/adt/like_support.c @@ -74,6 +74,7 @@ typedef enum (OidIsValid(coll) && !get_collation_isdeterministic(coll)) static Node *like_regex_support(Node *rawreq, Pattern_Type ptype); +static bool funccall_has_stats(SupportRequestSelectivity *req); static List *match_pattern_prefix(Node *leftop, Node *rightop, Pattern_Type ptype, @@ -168,6 +169,15 @@ like_regex_support(Node *rawreq, Pattern_Type ptype) SupportRequestSelectivity *req = (SupportRequestSelectivity *) rawreq; Selectivity s1; + /* + * If there are statistics on the function call itself, which can come + * from an expression index or extended statistics, they are better + * than anything we can derive from the pattern. Decline, so that the + * planner falls back to boolvarsel(), which uses them. + */ + if (!req->is_join && funccall_has_stats(req)) + return NULL; + if (req->is_join) { /* @@ -235,6 +245,26 @@ like_regex_support(Node *rawreq, Pattern_Type ptype) return ret; } +/* + * Are there statistics on the function call described by a selectivity + * request? + */ +static bool +funccall_has_stats(SupportRequestSelectivity *req) +{ + FuncExpr *fexpr; + VariableStatData vardata; + bool result; + + fexpr = makeFuncExpr(req->funcid, BOOLOID, req->args, + InvalidOid, req->inputcollid, COERCE_EXPLICIT_CALL); + examine_variable(req->root, (Node *) fexpr, req->varRelid, &vardata); + result = HeapTupleIsValid(vardata.statsTuple); + ReleaseVariableStats(vardata); + + return result; +} + /* * match_pattern_prefix * Try to generate an indexqual for a LIKE or regex operator. diff --git a/src/test/regress/expected/stats_ext.out b/src/test/regress/expected/stats_ext.out index 42d6c2777ea..75ca51bd57c 100644 --- a/src/test/regress/expected/stats_ext.out +++ b/src/test/regress/expected/stats_ext.out @@ -3724,6 +3724,8 @@ SELECT * FROM check_estimated_rows('SELECT * FROM sb_2 WHERE numeric_lt(y, 1.0)' (1 row) CREATE STATISTICS extstat_sb_2_small ON numeric_lt(y, 1.0) FROM sb_2; +-- These statistics should win over the estimate from the support function. +CREATE STATISTICS extstat_sb_2_prefix ON starts_with(payload, payload) FROM sb_2; ANALYZE sb_2; SELECT * FROM check_estimated_rows('SELECT * FROM sb_2 WHERE numeric_lt(y, 1.0)'); estimated | actual @@ -3731,6 +3733,12 @@ SELECT * FROM check_estimated_rows('SELECT * FROM sb_2 WHERE numeric_lt(y, 1.0)' 196 | 196 (1 row) +SELECT * FROM check_estimated_rows('SELECT * FROM sb_2 WHERE starts_with(payload, payload)'); + estimated | actual +-----------+-------- + 10000 | 10000 +(1 row) + -- Tidy up DROP TABLE sb_1, sb_2 CASCADE; -- Check statistics generated for range type and expressions. diff --git a/src/test/regress/sql/stats_ext.sql b/src/test/regress/sql/stats_ext.sql index 6045cfa51e8..05edbe8f168 100644 --- a/src/test/regress/sql/stats_ext.sql +++ b/src/test/regress/sql/stats_ext.sql @@ -1920,9 +1920,12 @@ RESET enable_mergejoin; SELECT * FROM check_estimated_rows('SELECT * FROM sb_2 WHERE numeric_lt(y, 1.0)'); CREATE STATISTICS extstat_sb_2_small ON numeric_lt(y, 1.0) FROM sb_2; +-- These statistics should win over the estimate from the support function. +CREATE STATISTICS extstat_sb_2_prefix ON starts_with(payload, payload) FROM sb_2; ANALYZE sb_2; SELECT * FROM check_estimated_rows('SELECT * FROM sb_2 WHERE numeric_lt(y, 1.0)'); +SELECT * FROM check_estimated_rows('SELECT * FROM sb_2 WHERE starts_with(payload, payload)'); -- Tidy up DROP TABLE sb_1, sb_2 CASCADE; -- 2.37.1 (Apple Git-137.1)