| From: | David Rowley <dgrowleyml(at)gmail(dot)com> |
|---|---|
| To: | Daniel Gustafsson <daniel(at)yesql(dot)se> |
| Cc: | Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Re: pg_upgrade's get_rel_infos_query() query can sometimes be slow |
| Date: | 2026-11-27 12:42:02 |
| Message-ID: | CAApHDvppDL4rbSggawxfNTJrS-W2+zavzXB2yP0rTDp0SKcRAA@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On Fri, 27 Nov 2026 at 23:29, Daniel Gustafsson <daniel(at)yesql(dot)se> wrote:
>
> > On 27 Nov 2026, at 10:59, Heikki Linnakangas <hlinnaka(at)iki(dot)fi> wrote:
>
> > It seems bonkers to optimize for the case that the stats are bad like that, reltuples=1 and relpages=1 is a completely unrealistic case.
It would be good to have more information. I did vent some
frustrations on that topic in my 2nd paragraph. I can try and push for
more, but I suspect I'll struggle to get the EXPLAIN ANALYZE we'd both
ideally want. These come from upgrading large customers, and it's all
very locked down in terms of what is shared. I think we might need to
apply some of our knowledge that underestimations can lead to nested
loops, which when compounded with other joins can cause $badthings.
I suspect the most likely cause of the issue is pg_namespace, as
that's the only table there that might have dozens of rows, but the
actual relation size is probably unlikely to push into multiple pages.
That would mean the density correction in
table_block_relation_estimate_size() won't have any effect as relpages
would match curpages, and if the planner's selectivity estimate to
pg_namespace, accounting for the filters that are applied to remove
the ones we don't want, might lead to a 1-row estimate.
Digging a little deeper, I found some additional documentation which
says: "the regex and NOT IN filter in regular_heap cause the planner
to estimate 1 row. "The planner then picks nested-loop index scans
over pg_class and pg_index for every schema."
I tried running the original query with just pg_namespace's reltuples
and relpages set to 1 with the 100k table schema restored. I get poor
performance just from that. See the attached
orig_1-row_pg_namespace.out. The following is where the slowness comes
from:
-> Nested Loop (cost=0.41..51.78 rows=1 width=148) (actual
time=946.284..44504.748 rows=400004.00 loops=1)
Join Filter: (c.relnamespace = n.oid)
Rows Removed by Join Filter: 21600216 <- ouch!
Buffers: shared hit=123209833, temp read=51812 written=1324
-> Seq Scan on pg_namespace n (cost=0.00..1.01 rows=1
width=68) (actual time=0.007..0.069 rows=55.00 loops=1)
Buffers: shared hit=1
-> Nested Loop (cost=0.41..50.72 rows=4 width=88)
(actual time=1.204..797.741 rows=400004.00 loops=55) <-- this nested
run 55 times due to the 1 row estimate.
I think the safest thing to do here is just get rid of the
pg_namespace join. We can use relnamespace::regnamespace::text and
use something like
replace(regexp_replace(relnamespace::regnamespace::text, '^"(.*)"$',
'\1'), '""', '"') to get rid of the leading/trailing quoting and any
escaped quotes from quotes in the name.
I've attached two more reworked SQLs.
$ pgbench -n -f david3.sql -T 30 postgres | grep latency
latency average = 1242.003 ms
$ pgbench -n -f david4.sql -T 30 postgres | grep latency
latency average = 1188.389 ms
$ psql postgres
psql (20devel)
Type "help" for help.
postgres=# SET allow_system_table_mods = on;
UPDATE pg_class
SET relpages = 1, reltuples = 1
WHERE oid IN ('pg_class'::regclass, 'pg_index'::regclass,
'pg_namespace'::regclass);
SET
UPDATE 3
postgres=# \q
$ pgbench -n -f david3.sql -T 30 postgres | grep latency
latency average = 2093.776 ms
$ pgbench -n -f david4.sql -T 30 postgres | grep latency
latency average = 1247.155 ms
I think david4.sql would be fine to go with.
David
| Attachment | Content-Type | Size |
|---|---|---|
| david3.sql | application/octet-stream | 1.8 KB |
| david4.sql | application/octet-stream | 2.0 KB |
| orig_1-row_pg_namespace.out | application/octet-stream | 9.1 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Amit Kapila | 2026-11-27 12:42:55 | Re: unidirectional logical replication logging issue |
| Previous Message | Álvaro Herrera | 2026-11-27 12:27:04 | Re: Set 1s WaitLatch timeout if standby limit has expired in ResolveRecoveryConflictWithBufferPin |