BUG #19936: Planning is slower on 19.0 with many partial indexes on a SQL function

Started by PG Bug reporting form4 days ago1 messagesbugs

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 19936
Logged by: Jordan Dimov
Email address: jdimov@a115.bg
PostgreSQL version: 19.0
Operating system: Ubuntu 26.04, x86_64 (PGDG packages)
Description:

Hello,

I think I have found a planning slowdown in PostgreSQL 19.0. I may be
missing something, so please tell me if this is expected.

My table has many partial indexes. Each one indexes an expression that calls
a small SQL function. The function is marked IMMUTABLE, but it calls STABLE
functions, so the planner can never inline it. On 19.0, a simple query on
this table takes about four times longer to plan than on 18.7.

This script shows it. Run it in an empty database:

create function f(j jsonb) returns bytea language sql immutable strict
return sha256(convert_to(j::text, 'UTF8'));
create table t(p text, a jsonb);
insert into t select 'p' || (i % 550), jsonb_build_array(i)
from generate_series(1, 55000) i;
select format('create index on t (f(a -> 0)) where p = %L', 'p' || k)
from generate_series(0, 549) k \gexec
analyze t;
explain (summary) select * from t where p = 'p7' limit 1;
explain (summary) select * from t where p = 'p7' limit 1;
explain (summary) select * from t where p = 'p7' limit 1;

The first EXPLAIN warms the cache. I planned the query five more times on
each version, on the same machine. Planning time, in ms:

18.7 19.0
indexes on f(a -> 0) 0.70 - 0.91 2.65 - 3.19
indexes on (a -> 0), no function 0.64 - 0.92 0.81 - 0.90
f with SET search_path added 0.68 - 0.80 0.74 - 0.89

So the function seems to be the cause. When I add a SET clause to it, 19.0
is as fast as 18.7 again. As I understand it, a SET clause stops inlining
early, before the function body is read.

My guess, which I have not checked in the source: 19.0 tries to inline the
function again for every index, each time it plans a query. 18.7 seems to do
this only once. Commit c925ad30b0 ("Fix
const-simplification for index expressions and predicate") may be related,
but I have not bisected it.

This matters to us because our application has about 550 such indexes on one
table. On 19.0, its slowest 5% of requests took about twice as long. The SET
clause is not a good workaround for us, because it makes every call to the
function slower.

Versions: 18.7 (Ubuntu 18.7-1.pgdg26.04+5) and 19.0 (Ubuntu
19.0-2.pgdg26.04+1), on x86_64 Linux. Both use shared_buffers = 2GB and jit
= off. The 18.7 server also has a few I/O and parallel settings raised. I
don't think they affect planning.

Thank you for looking at this.

Jordan Dimov