Test timings are increasing too fast for cfbot
Test timings are increasing too fast for cfbot
От:
Andres Freund <andres@anarazel.de>
Дата:
Hi,
I was working on giving at least committers, and perhaps a few more folks,
access to our custom compute setup for running CI, as I had gotten a few
reports about folks running out of CI cycles during busy periods like right
now.
I was thinking that we had enough compute credits to make that possible. Which
I luckily, prodded by a question from Melanie, did check.
Unfortunately it seems that our usage has increased substantially enough to
make that a problem, even with just cfbot.
Most of the test are quite cheap to run, however windows costs a fair bit in
licensing cost (we spend 2x on windows than we do for compute, ram and storage
for all the tests together). There's a few other things we can improve
(e.g. we spend a fair bit on cross region data transfers, which should largely
be fixable). But we do need to get the increases in windows runtime under
control.
Here's a look at the test timings. Unfortunately the trends are decidedly not
good, particularly on windows. Looking at a few random cross-branch commits
and looking only at the test times (since build times are affected by caching
etc).
CI msvc CI mingw CI linux andres-local
17 11:21 10:38 3:32 1:22
18 16:03 14:22 5:39 1:38
master 18:13 15:00 6:69 1:49
I checked this across a few commits and got similar numbers
everywhere. There's a lot of noise, but the trendline is pretty darn clear.
Due to the number of times we run the main regression tests, they have a
particularly large effect on test resources.
time cycles syscalls
17 37.13 239.2T 1.573M
18 44.27 295.7T 1.715M
master 48.19 323.5T 1.854M
Another interesting aspect is the time spent doing initdb has gone up
noticeably:
assert optimize
17 1.934 819.1
18 2.190 827.9
master 2.274 814.5
Which, given the number of times initdb is executed in the tests (even with
the template mechanism in place), probably is a factor in all this. It's
certainly interesting that somehow assertions play a major role, given that
the non-assert time hasn't regressed.
Some increase in test cycles and time is inevitable, but to me the current
rate of increase is not sustainable / faster than the rate at which hardware
speed increases.
Greetings,
Andres Freund
Re: Test timings are increasing too fast for cfbot
От:
Andres Freund <andres@anarazel.de>
Дата:
Hi,
On 2026-03-24 17:30:46 -0400, Andres Freund wrote:
> Due to the number of times we run the main regression tests, they have a
> particularly large effect on test resources.
>
> time cycles syscalls
> 17 37.13 239.2T 1.573M
> 18 44.27 295.7T 1.715M
> master 48.19 323.5T 1.854M
A surprisingly large source of this is psql internal queries. I first ran the
regression tests with pg_stat_statements activated [1] and noticed that a lot
of the queries, by exec and or plan time, are psql queries.
I patched psql to add "-- psql internal\n" to every internal query and then
used log_min_duration_statement=0 to see how much of the time is spent in psql
- ~13% of the reported duration of all queries.
A lot of that is because describeTbleDetails() ends up with a sequential scan:
regression[1020428][1]=# EXPLAIN ANALYZE SELECT c.oid,
n.nspname,
c.relname
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname OPERATOR(pg_catalog.~) '^(pg_class)$' COLLATE pg_catalog.default
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 2, 3;
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ QUERY PLAN │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Sort (cost=324.79..324.80 rows=1 width=132) (actual time=7.192..7.194 rows=1.00 loops=1) │
│ Sort Key: n.nspname, c.relname │
│ Sort Method: quicksort Memory: 25kB │
│ Buffers: shared hit=234 │
│ -> Nested Loop Left Join (cost=0.00..324.78 rows=1 width=132) (actual time=2.489..7.121 rows=1.00 loops=1) │
│ Join Filter: (n.oid = c.relnamespace) │
│ Buffers: shared hit=231 │
│ -> Seq Scan on pg_class c (cost=0.00..313.79 rows=1 width=72) (actual time=2.473..7.105 rows=1.00 loops=1) │
│ Filter: ((relname ~ '^(pg_class)$'::text) AND pg_table_is_visible(oid)) │
│ Rows Removed by Filter: 2260 │
│ Buffers: shared hit=229 │
│ -> Seq Scan on pg_namespace n (cost=0.00..8.22 rows=222 width=68) (actual time=0.011..0.011 rows=1.00 loops=1) │
│ Buffers: shared hit=2 │
│ Planning: │
│ Buffers: shared hit=6 │
│ Planning Time: 0.520 ms │
│ Execution Time: 7.265 ms │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
(17 rows)
It seems decidedly not optimal that "\d tablename", without any patterns, ends
up doing a seqscan. That's bad enough in the regression database, but there
are many PG instances with many many entries in pg_class.
I don't think this was always the case?
If I remove the COLLATE pg_catalog.default, a sane plan is chosen. That's
obviously not the right fix, but seemed interesting enough to mention.
That alone seems makes a surprising 4.5% improvement in an unoptimized -O0
build.
Another psql query that stands out is \d's query for publications. Not because
of runtime, but because of the planning cost. Nuking that (obviously not
correct), is worth another 3% of test time (and some test failues).
I've not analyzed the psql queries any further, but I'd bet that the rest also
do add up.
The most expensive query in the entire regression tests is:
DO $$
BEGIN
FOR r IN 1..1350 LOOP
DELETE FROM dedup_unique_test_table;
INSERT INTO dedup_unique_test_table SELECT 1;
END LOOP;
END$$;
which makes sense, as that's pretty clearly O(N^2).
There are a surprisingly large number of temporary file creations in the
regression tests. A lot of them due to shared file sets in the context of
parallelism. I wonder whether that is a partial cause for the slowness on
windows, which has very slow metadata operations and slower data caching.
The slowest test is stats_ext.sql - Not surprising, it does sequential scans
of tables with ~1000-10000 rows over and over again. I don't see why it has
to do that with as many rows as it does.
Another thing we spend a decent amount of time, distributed over many places,
is FROM generate_series(x, y), which ends up slow due to nodeFunctionscan.c
always using a tuplestore.
Code wise, the most immediately noticeable things are
1) MEMORY_CONTEXT_CHECKING
2) AssertCheckRanges() (only in the brin test, but there a very large portion
of the runtime)
3) verify_compact_attribute(), pretty spread around
Greetings,
Andres Freund
[1] which annoyingly generates a bunch of test failures due to "Query
Identifier" getting added to tests
Re: Test timings are increasing too fast for cfbot
От:
Julien Rouhaud <rjuju123@gmail.com>
Дата:
Hi, On Tue, Mar 24, 2026 at 11:15:32PM -0400, Andres Freund wrote: > > A surprisingly large source of this is psql internal queries. I first ran the > regression tests with pg_stat_statements activated [1] and noticed that a lot > of the queries, by exec and or plan time, are psql queries. > > [1] which annoyingly generates a bunch of test failures due to "Query > Identifier" getting added to tests FWIW `compute_query_id = regress` is supposed to avoid that problem.
Make \d tablename fast again, regression introduced by 85b7efa1cdd
(forked from: Test timings are increasing too fast for cfbot) On Wed, 25 Mar 2026 at 04:15, Andres Freund wrote: > It seems decidedly not optimal that "\d tablename", without any patterns, ends > up doing a seqscan. That's bad enough in the regression database, but there > are many PG instances with many many entries in pg_class. > > I don't think this was always the case? > > If I remove the COLLATE pg_catalog.default, a sane plan is chosen. That's > obviously not the right fix, but seemed interesting enough to mention. Due to a very similar problem I faced in the past[1], I thought I had a good sense of where roughly the problem was. And I indeed quickly found it. Attached is a patch that addresses this issue and starts using index scans again for \d tablename. This should be backpatched to PG18 where the regression was introduced by 85b7efa1cdd [1]: https://www.postgresql.org/message-id/flat/CAGECzQRqysy0eJMKR5he3gwtLrT87f9u5CQQua6B_XNwMnUtFA%40mail.gmail.com
Re: Test timings are increasing too fast for cfbot
От:
Tomas Vondra <tomas@vondra.me>
Дата:
On 3/25/26 04:15, Andres Freund wrote: > ... > > The slowest test is stats_ext.sql - Not surprising, it does sequential scans > of tables with ~1000-10000 rows over and over again. I don't see why it has > to do that with as many rows as it does. > IIRC we needed to use non-trivial amounts of data to ensure building the right right type of statistics (e.g. no MCV). But I understand it can be annoyingly expensive, so I'll try to make it cheaper. > ... > 2) AssertCheckRanges() (only in the brin test, but there a very large portion > of the runtime) True. It is a very comprehensive validation of the ranges, and it was very useful - particularly during development. But I'll try to make it more targeted at the stuff actually changed / called less often. Both changes will require time (so that we don't lose test coverage), but I assume it's OK if that happens sometime after the feature freeze. regards -- Tomas Vondra
Re: Test timings are increasing too fast for cfbot
От:
Andres Freund <andres@anarazel.de>
Дата:
Hi, On 2026-03-25 15:38:01 +0100, Tomas Vondra wrote: > On 3/25/26 04:15, Andres Freund wrote: > > ... > > > > The slowest test is stats_ext.sql - Not surprising, it does sequential scans > > of tables with ~1000-10000 rows over and over again. I don't see why it has > > to do that with as many rows as it does. > > > > IIRC we needed to use non-trivial amounts of data to ensure building the > right right type of statistics (e.g. no MCV). But I understand it can be > annoyingly expensive, so I'll try to make it cheaper. Great. It might also be worth checking if any of the query conditions can be made cheaper without actually changing what they do test. > > ... > > 2) AssertCheckRanges() (only in the brin test, but there a very large portion > > of the runtime) > > True. It is a very comprehensive validation of the ranges, and it was > very useful - particularly during development. But I'll try to make it > more targeted at the stuff actually changed / called less often. Makes sense. There might also be some changs that make it faster without loosing any coverage. E.g. not using FunctionCall2Coll() - which initializes stuff on every call - but doing InitFunctionCallInfoData() once and then update arguments + FunctionCallInvoke() for each call. > Both changes will require time (so that we don't lose test coverage), > but I assume it's OK if that happens sometime after the feature freeze. Yea. We're not running out of credits tomorrow or something like that. Greetings, Andres Freund
Re: Test timings are increasing too fast for cfbot
От:
David Rowley <dgrowleyml@gmail.com>
Дата:
On Wed, 25 Mar 2026 at 16:15, Andres Freund wrote: > Code wise, the most immediately noticeable things are > 3) verify_compact_attribute(), pretty spread around We do now have TupleDescFinalize(), where those could be checked just once rather than on each call to TupleDescCompactAttr(). That's not quite as watertight a guarantee as someone could change the FormData_pg_attribute after TupleDescFinalize(). Just doing it in TupleDescFinalize() would at least still catch the places where people forget to call populate_compact_attribute() before TupleDescFinalize(). I would have expected this to be a little less overhead now since d8a859d22 removed the calls to TupleDescCompactAttr() in the main deforming routine. Maybe I should just make that change in the other deformers...? Do you have an idea of which callers of verify_compact_attribute() are causing the most overhead? David
Re: Test timings are increasing too fast for cfbot
От:
Andres Freund <andres@anarazel.de>
Дата:
On 2026-03-26 11:09:36 +1300, David Rowley wrote: > On Wed, 25 Mar 2026 at 16:15, Andres Freund wrote: > > Code wise, the most immediately noticeable things are > > > 3) verify_compact_attribute(), pretty spread around > > We do now have TupleDescFinalize(), where those could be checked just > once rather than on each call to TupleDescCompactAttr(). That's not > quite as watertight a guarantee as someone could change the > FormData_pg_attribute after TupleDescFinalize(). Just doing it in > TupleDescFinalize() would at least still catch the places where people > forget to call populate_compact_attribute() before > TupleDescFinalize(). Maybe verify_compact_attribute() could just do an assert comparison between the underlying non-compact attribute and the compact one? Or even just an assert checking if the compact attribute is initialized, with the full checking happening in TupleDescFinalize(), as you suggest? > I would have expected this to be a little less overhead now since > d8a859d22 removed the calls to TupleDescCompactAttr() in the main > deforming routine. Maybe I should just make that change in the other > deformers...? Might be worth it. > Do you have an idea of which callers of verify_compact_attribute() are > causing the most overhead? I'm not sure how much to believe the profile when the costs are as distributed as they are here. But according to the profile it's - heap_form_tuple - heap_form_minimal_tuple - index_getattr - nocachegetattr Greetings, Andres Freund
Re: Test timings are increasing too fast for cfbot
От:
Andres Freund <andres@anarazel.de>
Дата:
Hi, On 2026-03-25 12:36:12 +0800, Julien Rouhaud wrote: > On Tue, Mar 24, 2026 at 11:15:32PM -0400, Andres Freund wrote: > > > > A surprisingly large source of this is psql internal queries. I first ran the > > regression tests with pg_stat_statements activated [1] and noticed that a lot > > of the queries, by exec and or plan time, are psql queries. > > > > [1] which annoyingly generates a bunch of test failures due to "Query > > Identifier" getting added to tests > > FWIW `compute_query_id = regress` is supposed to avoid that problem. Hm. I guess that should work. Thanks! It's pretty annoying that one has to manually specify it to keep the tests passing. I wonder if we should make pg_regress specify it to avoid that. Greetings, Andres Freund
Re: Make \d tablename fast again, regression introduced by 85b7efa1cdd
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
"Jelte Fennema-Nio" writes:
> (forked from: Test timings are increasing too fast for cfbot)
> On Wed, 25 Mar 2026 at 04:15, Andres Freund wrote:
>> It seems decidedly not optimal that "\d tablename", without any patterns, ends
>> up doing a seqscan.
>> ...
>> I don't think this was always the case?
> Due to a very similar problem I faced in the past[1], I thought I had a
> good sense of where roughly the problem was. And I indeed quickly found
> it.
> Attached is a patch that addresses this issue and starts using index
> scans again for \d tablename.
I looked this over, and I think it's basically right, but don't
we need to check that *both* of the collations are deterministic?
Also the commit message had a lot of details wrong. AFAICS this
only affects the exact-match case not prefix-match, and it applies
to regexes as well as LIKE.
See attached v2.
regards, tom lane
From 841b299cfd0910e9c324252953824365a725ecd3 Mon Sep 17 00:00:00 2001
From: Tom Lane
Date: Sat, 4 Jul 2026 16:21:42 -0400
Subject: [PATCH v2] Fix LIKE/regex optimization for indexscan with exact-match
pattern.
Commit 85b7efa1c introduced support for LIKE with non-deterministic
collations. By moving some conditionals around, it accidentally broke
the optimization for converting a LIKE or regex exact-match pattern
to an equality indexqual when the index collation doesn't match the
filter collation. We can't do that conversion if either collation is
nondeterministic, but we can if both are deterministic. This patch
re-introduces the optimization for that common case.
One important beneficiary of this optimization is the "\d tablename"
command in psql. Without this fix that will do a seqscan on pg_class
instead of an index point lookup.
Reported-by: Andres Freund
Author: Jelte Fennema-Nio
Reviewed-by: Tom Lane
Discussion: https://postgr.es/m/DHBQIZX8SZVI.ZX614ZMFL645@jeltef.nl
Backpatch-through: 18
---
src/backend/utils/adt/like_support.c | 15 +++++++++++----
src/test/regress/expected/collate.out | 21 ++++++++++++++++++++-
src/test/regress/sql/collate.sql | 11 +++++++++++
3 files changed, 42 insertions(+), 5 deletions(-)
diff --git a/src/backend/utils/adt/like_support.c b/src/backend/utils/adt/like_support.c
index 01cd6b10730..dea6a147c69 100644
--- a/src/backend/utils/adt/like_support.c
+++ b/src/backend/utils/adt/like_support.c
@@ -69,6 +69,10 @@ typedef enum
Pattern_Prefix_None, Pattern_Prefix_Partial, Pattern_Prefix_Exact,
} Pattern_Prefix_Status;
+/* non-collatable comparisons, eg for bytea, are always deterministic */
+#define NONDETERMINISTIC(coll) \
+ (OidIsValid(coll) && !get_collation_isdeterministic(coll))
+
static Node *like_regex_support(Node *rawreq, Pattern_Type ptype);
static List *match_pattern_prefix(Node *leftop,
Node *rightop,
@@ -381,12 +385,17 @@ match_pattern_prefix(Node *leftop,
* us to not be concerned with specific opclasses (except for the legacy
* "pattern" cases); any index that correctly implements the operators
* will work.
+ *
+ * Also, since all deterministic collations agree on equality, we can use
+ * an index that uses a different collation so long as both collations are
+ * deterministic. Otherwise, fail quietly.
*/
if (pstatus == Pattern_Prefix_Exact)
{
if (!op_in_opfamily(eqopr, opfamily))
return NIL;
- if (indexcollation != expr_coll)
+ if (indexcollation != expr_coll &&
+ (NONDETERMINISTIC(indexcollation) || NONDETERMINISTIC(expr_coll)))
return NIL;
expr = make_opclause(eqopr, BOOLOID, false,
(Expr *) leftop, (Expr *) prefix,
@@ -400,10 +409,8 @@ match_pattern_prefix(Node *leftop,
* expression collation is nondeterministic. The optimized equality or
* prefix tests use bytewise comparisons, which is not consistent with
* nondeterministic collations.
- *
- * expr_coll is not set for a non-collation-aware data type such as bytea.
*/
- if (expr_coll && !get_collation_isdeterministic(expr_coll))
+ if (NONDETERMINISTIC(expr_coll))
return NIL;
/*
diff --git a/src/test/regress/expected/collate.out b/src/test/regress/expected/collate.out
index 25818f09ad2..a6c46347647 100644
--- a/src/test/regress/expected/collate.out
+++ b/src/test/regress/expected/collate.out
@@ -768,13 +768,31 @@ DETAIL: LOCALE cannot be specified together with LC_COLLATE or LC_CTYPE.
CREATE COLLATION coll_dup_chk (FROM = "C", VERSION = "1");
ERROR: conflicting or redundant options
DETAIL: FROM cannot be specified together with any other options.
+-- Regex exact-match optimization should use index even when the expression
+-- has COLLATE "default" and the index has a different (but deterministic)
+-- collation OID, because equality is collation-insensitive for deterministic
+-- collations.
+CREATE TABLE regex_idx_test (x text);
+CREATE INDEX ON regex_idx_test (x COLLATE "C");
+SET enable_seqscan = off;
+EXPLAIN (costs off)
+SELECT * FROM regex_idx_test WHERE x ~ '^(abc)$' COLLATE "default";
+ QUERY PLAN
+-------------------------------------------------
+ Bitmap Heap Scan on regex_idx_test
+ Filter: (x ~ '^(abc)$'::text)
+ -> Bitmap Index Scan on regex_idx_test_x_idx
+ Index Cond: (x = 'abc'::text)
+(4 rows)
+
+RESET enable_seqscan;
--
-- Clean up. Many of these table names will be re-used if the user is
-- trying to run any platform-specific collation tests later, so we
-- must get rid of them.
--
DROP SCHEMA collate_tests CASCADE;
-NOTICE: drop cascades to 20 other objects
+NOTICE: drop cascades to 21 other objects
DETAIL: drop cascades to table collate_test1
drop cascades to table collate_test_like
drop cascades to table collate_test2
@@ -795,3 +813,4 @@ drop cascades to collation builtin_c
drop cascades to collation mycoll2
drop cascades to table collate_test23
drop cascades to view collate_on_int
+drop cascades to table regex_idx_test
diff --git a/src/test/regress/sql/collate.sql b/src/test/regress/sql/collate.sql
index 4b0e4472c3f..78507e5a89b 100644
--- a/src/test/regress/sql/collate.sql
+++ b/src/test/regress/sql/collate.sql
@@ -302,6 +302,17 @@ CREATE COLLATION coll_dup_chk (LC_CTYPE = "POSIX", LOCALE = '');
-- FROM conflicts with any other option
CREATE COLLATION coll_dup_chk (FROM = "C", VERSION = "1");
+-- Regex exact-match optimization should use index even when the expression
+-- has COLLATE "default" and the index has a different (but deterministic)
+-- collation OID, because equality is collation-insensitive for deterministic
+-- collations.
+CREATE TABLE regex_idx_test (x text);
+CREATE INDEX ON regex_idx_test (x COLLATE "C");
+SET enable_seqscan = off;
+EXPLAIN (costs off)
+SELECT * FROM regex_idx_test WHERE x ~ '^(abc)$' COLLATE "default";
+RESET enable_seqscan;
+
--
-- Clean up. Many of these table names will be re-used if the user is
-- trying to run any platform-specific collation tests later, so we
--
2.52.0
Re: Make \d tablename fast again, regression introduced by 85b7efa1cdd
On Sat, 4 Jul 2026 at 22:27, Tom Lane wrote: > I looked this over, and I think it's basically right, but don't > we need to check that *both* of the collations are deterministic? That's not what the code did before 85b7efa1c at least. And I think that's correct because I think it's fine if the index collation is nondeterministic: it'll return a superset of the matches that the expression collation thinks are equal, and then the recheck condition will filter out the conflicting cases (because it's marked as lossy). Attached is v3, which adds a test for this case and expands the comment. I left your commit message improvements as is, I agree that the message is a lot better after your changes (I was indeed sorely mistaken about the general prefix pattern optimization being impacted, the regex addition is nice but seems relatively minor). P.S. I think we could mark the comparisons in certain cases as non-lossy, but after trying that for a bit the details turn out more complicated than I expected. And that's definitely not something to backport.
Re: Make \d tablename fast again, regression introduced by 85b7efa1cdd
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
"Jelte Fennema-Nio" writes:
> On Sat, 4 Jul 2026 at 22:27, Tom Lane wrote:
>> I looked this over, and I think it's basically right, but don't
>> we need to check that *both* of the collations are deterministic?
> That's not what the code did before 85b7efa1c at least. And I think that's
> correct because I think it's fine if the index collation is
> nondeterministic: it'll return a superset of the matches that the
> expression collation thinks are equal, and then the recheck condition will
> filter out the conflicting cases (because it's marked as lossy).
Hmm. Okay, but the comment had better make it explicit that we're
relying on that superset assumption.
> Attached is v3, which adds a test for this case and expands the comment.
I fooled with the comments and test cases some more and pushed it.
(I don't like test cases that create one-off tables, especially not
when there are existing tables that will serve the purpose just as
well. The regression tests are slow enough already.)
> P.S. I think we could mark the comparisons in certain cases as
> non-lossy, but after trying that for a bit the details turn out more
> complicated than I expected. And that's definitely not something to backport.
Yeah, that was my immediate reaction to your message. I agree it's
only material for HEAD, but here's a draft patch to do that.
regards, tom lane
diff --git a/src/backend/utils/adt/like_support.c b/src/backend/utils/adt/like_support.c
index 4c8db9147ee..d12a3e833d2 100644
--- a/src/backend/utils/adt/like_support.c
+++ b/src/backend/utils/adt/like_support.c
@@ -66,7 +66,10 @@ typedef enum
typedef enum
{
- Pattern_Prefix_None, Pattern_Prefix_Partial, Pattern_Prefix_Exact,
+ Pattern_Prefix_None, /* cannot produce an indexqual */
+ Pattern_Prefix_Partial, /* maps to an index range restriction */
+ Pattern_Prefix_Exact, /* maps to a simple equality test */
+ Pattern_Prefix_Exact_Lossy, /* maps to an equality test, but recheck */
} Pattern_Prefix_Status;
/* non-collatable comparisons, eg for bytea, are always deterministic */
@@ -79,7 +82,8 @@ static List *match_pattern_prefix(Node *leftop,
Pattern_Type ptype,
Oid expr_coll,
Oid opfamily,
- Oid indexcollation);
+ Oid indexcollation,
+ bool *lossy);
static double patternsel_common(PlannerInfo *root,
Oid oprid,
Oid opfuncid,
@@ -215,7 +219,8 @@ like_regex_support(Node *rawreq, Pattern_Type ptype)
ptype,
clause->inputcollid,
req->opfamily,
- req->indexcollation);
+ req->indexcollation,
+ &req->lossy);
}
else if (is_funcclause(req->node)) /* be paranoid */
{
@@ -228,7 +233,8 @@ like_regex_support(Node *rawreq, Pattern_Type ptype)
ptype,
clause->inputcollid,
req->opfamily,
- req->indexcollation);
+ req->indexcollation,
+ &req->lossy);
}
}
@@ -238,6 +244,10 @@ like_regex_support(Node *rawreq, Pattern_Type ptype)
/*
* match_pattern_prefix
* Try to generate an indexqual for a LIKE or regex operator.
+ *
+ * We return a list of indexqual expressions on success, or NIL on failure.
+ * On success, *lossy is changed to false (from its initial state of true)
+ * if the indexqual expression(s) can fully replace the operator.
*/
static List *
match_pattern_prefix(Node *leftop,
@@ -245,7 +255,8 @@ match_pattern_prefix(Node *leftop,
Pattern_Type ptype,
Oid expr_coll,
Oid opfamily,
- Oid indexcollation)
+ Oid indexcollation,
+ bool *lossy)
{
List *result;
Const *patt;
@@ -394,9 +405,12 @@ match_pattern_prefix(Node *leftop,
* nondeterministic collation will return a superset of the bitwise-equal
* entries. Since the "=" indexqual is marked as lossy by default, we'll
* apply the LIKE/regex operator as a recheck, and that will filter out
- * any non-matching entries.
+ * any non-matching entries. Furthermore, we can get away without a
+ * recheck if the expression is LIKE (but not regex) and the index
+ * collation is also deterministic or matches the expression's.
*/
- if (pstatus == Pattern_Prefix_Exact)
+ if (pstatus == Pattern_Prefix_Exact ||
+ pstatus == Pattern_Prefix_Exact_Lossy)
{
if (!op_in_opfamily(eqopr, opfamily))
return NIL;
@@ -405,14 +419,17 @@ match_pattern_prefix(Node *leftop,
expr = make_opclause(eqopr, BOOLOID, false,
(Expr *) leftop, (Expr *) prefix,
InvalidOid, indexcollation);
+ if (pstatus == Pattern_Prefix_Exact &&
+ (indexcollation == expr_coll || !NONDETERMINISTIC(indexcollation)))
+ *lossy = false;
result = list_make1(expr);
return result;
}
/*
- * Anything other than Pattern_Prefix_Exact is not supported if the
- * expression collation is nondeterministic. The optimized equality or
- * prefix tests use bytewise comparisons, which is not consistent with
+ * Anything other than Pattern_Prefix_Exact[_Lossy] is not supported if
+ * the expression collation is nondeterministic. The optimized equality
+ * or prefix tests use bytewise comparisons, which is not consistent with
* nondeterministic collations.
*/
if (NONDETERMINISTIC(expr_coll))
@@ -645,7 +662,8 @@ patternsel_common(PlannerInfo *root,
prefix->consttype = rdatatype;
}
- if (pstatus == Pattern_Prefix_Exact)
+ if (pstatus == Pattern_Prefix_Exact ||
+ pstatus == Pattern_Prefix_Exact_Lossy)
{
/*
* Pattern specifies an exact match, so estimate as for '='
@@ -985,7 +1003,7 @@ icnlikejoinsel(PG_FUNCTION_ARGS)
/*
* Extract the fixed prefix, if any, for a pattern.
*
- * *prefix is set to a palloc'd prefix string (in the form of a Const node),
+ * *prefix_const is set to a palloc'd string (in the form of a Const node),
* or to NULL if no fixed prefix exists for the pattern.
* If rest_selec is not NULL, *rest_selec is set to an estimate of the
* selectivity of the remainder of the pattern (without any fixed prefix).
@@ -1005,6 +1023,7 @@ like_fixed_prefix(Const *patt_const, Const **prefix_const,
Oid typeid = patt_const->consttype;
int pos,
match_pos;
+ bool badpattern = false;
/* the right-hand const is type text or bytea */
Assert(typeid == BYTEAOID || typeid == TEXTOID);
@@ -1038,7 +1057,10 @@ like_fixed_prefix(Const *patt_const, Const **prefix_const,
{
pos++;
if (pos >= pattlen)
+ {
+ badpattern = true;
break;
+ }
}
match[match_pos++] = patt[pos];
@@ -1059,7 +1081,16 @@ like_fixed_prefix(Const *patt_const, Const **prefix_const,
/* in LIKE, an empty pattern is an exact match! */
if (pos == pattlen)
- return Pattern_Prefix_Exact; /* reached end of pattern, so exact */
+ {
+ /*
+ * Reached end of pattern, so exact -- unless the operator would throw
+ * an error at runtime. In that case our representation is lossy,
+ * since we don't want to throw that error here.
+ */
+ if (badpattern)
+ return Pattern_Prefix_Exact_Lossy;
+ return Pattern_Prefix_Exact;
+ }
if (match_pos > 0)
return Pattern_Prefix_Partial;
@@ -1086,6 +1117,7 @@ like_fixed_prefix_ci(Const *patt_const, Oid collation, Const **prefix_const,
char *match;
int match_mblen;
pg_locale_t locale = 0;
+ bool badpattern = false;
/* the right-hand const is type text or bytea */
Assert(typeid == BYTEAOID || typeid == TEXTOID);
@@ -1125,7 +1157,10 @@ like_fixed_prefix_ci(Const *patt_const, Oid collation, Const **prefix_const,
{
wpos++;
if (wpos >= wpattlen)
+ {
+ badpattern = true;
break;
+ }
}
/*
@@ -1165,7 +1200,16 @@ like_fixed_prefix_ci(Const *patt_const, Oid collation, Const **prefix_const,
/* in LIKE, an empty pattern is an exact match! */
if (wpos == wpattlen)
- return Pattern_Prefix_Exact; /* reached end of pattern, so exact */
+ {
+ /*
+ * Reached end of pattern, so exact -- unless the operator would throw
+ * an error at runtime. In that case our representation is lossy,
+ * since we don't want to throw that error here.
+ */
+ if (badpattern)
+ return Pattern_Prefix_Exact_Lossy;
+ return Pattern_Prefix_Exact;
+ }
if (wmatch_pos > 0)
return Pattern_Prefix_Partial;
@@ -1235,8 +1279,13 @@ regex_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation,
pfree(prefix);
+ /*
+ * Because regexp_fixed_prefix() doesn't analyze the regexp fully, even an
+ * "exact" match is lossy: we know that a match must be this string, but
+ * it could still fail to match.
+ */
if (exact)
- return Pattern_Prefix_Exact; /* pattern specifies exact match */
+ return Pattern_Prefix_Exact_Lossy;
else
return Pattern_Prefix_Partial;
}
diff --git a/src/test/regress/expected/collate.icu.utf8.out b/src/test/regress/expected/collate.icu.utf8.out
index fb95eee9b7c..1574433d5ef 100644
--- a/src/test/regress/expected/collate.icu.utf8.out
+++ b/src/test/regress/expected/collate.icu.utf8.out
@@ -2098,12 +2098,11 @@ SELECT * FROM test1ci WHERE x ~ '^abc$' COLLATE "C";
EXPLAIN (COSTS OFF)
SELECT * FROM test1ci WHERE x LIKE 'abc' COLLATE case_insensitive;
- QUERY PLAN
--------------------------------------------------------
+ QUERY PLAN
+-------------------------------------------
Index Scan using test1ci_x_idx on test1ci
Index Cond: (x = 'abc'::text)
- Filter: (x ~~ 'abc'::text COLLATE case_insensitive)
-(3 rows)
+(2 rows)
RESET enable_seqscan;
RESET enable_indexonlyscan;
diff --git a/src/test/regress/expected/collate.out b/src/test/regress/expected/collate.out
index b17c5abaddc..da301358ada 100644
--- a/src/test/regress/expected/collate.out
+++ b/src/test/regress/expected/collate.out
@@ -784,12 +784,11 @@ SELECT * FROM pg_class WHERE relname ~ '^pg_class$' COLLATE "POSIX";
EXPLAIN (COSTS OFF)
SELECT * FROM pg_class WHERE relname LIKE 'pg\_class' COLLATE "POSIX";
- QUERY PLAN
-----------------------------------------------------------
+ QUERY PLAN
+---------------------------------------------------------
Index Scan using pg_class_relname_nsp_index on pg_class
Index Cond: (relname = 'pg_class'::text)
- Filter: (relname ~~ 'pg\_class'::text COLLATE "POSIX")
-(3 rows)
+(2 rows)
--
-- Clean up. Many of these table names will be re-used if the user is
Re: Make \d tablename fast again, regression introduced by 85b7efa1cdd
On Mon Jul 6, 2026 at 7:16 PM CEST, Tom Lane wrote: >> P.S. I think we could mark the comparisons in certain cases as >> non-lossy, but after trying that for a bit the details turn out more >> complicated than I expected. And that's definitely not something to backport. > > Yeah, that was my immediate reaction to your message. I agree it's > only material for HEAD, but here's a draft patch to do that. I found two other edge cases worth considering for this. Which is why I didn't pursue it further, because I assumed with those two existing there were probably some more that I missed (and indeed I had missed that regexes should always be lossy, and there might be more still): 1. bpchar columns should be lossy because LIKE and = behave differently for padded values. 2. When the index has a collation, but no collation can be detected for the expression, then we currently throw an error during the recheck. It seems strange to me to throw that error during execution instead of planning, but it seems even weirder that removing the recheck would make the query succeed. Attached a v2 draft patch that adds some tests for those cases and checks to handle them (and some comments by an LLM that I haven't cleaned up much). I think issue 2 might need a different approach altogether. To be clear, I don't plan to pursue this further right now, just wanted to share the details of my investigation in case you want to take it further.