Re: BUG #19688: pg_dump --schema scans all sequences in PostgreSQL 18, causing severe performance regression

From: Nathan Bossart <nathandbossart(at)gmail(dot)com>
To: Diego <mrstephenamell(at)gmail(dot)com>
Cc: Andrew Krylosov <krylosov(dot)andrew(at)gmail(dot)com>, pgsql-bugs(at)lists(dot)postgresql(dot)org, cesarg9(at)gmail(dot)com, surya poondla <suryapoondla4(at)gmail(dot)com>, Christopher Bell <bellchristopherryan(at)gmail(dot)com>
Subject: Re: BUG #19688: pg_dump --schema scans all sequences in PostgreSQL 18, causing severe performance regression
Date: 2026-11-30 17:05:45
Message-ID: aw2s6S0SA-upqojC@nathan
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Thu, Nov 26, 2026 at 08:18:43AM -0800, Diego wrote:
> Surya Poondla, Christopher Bell and I reviewed this patch together, as
> part of the November patch review workshop.
>
> Short version: the patch does what it says, we could not find any change
> in what pg_dump outputs or in how it fails, and it looks ready to us.
> There is one comment suggestion and one question about tests at the end.

Thanks for the patch and for the review.

> On 2026-09-26, Andrew Krylosov wrote:
>> I can reproduce it on HEAD with 20000 sequences in one schema and 4 in
>> another. "pg_dump -n small" takes about 1.3 s, of which the
>> collectSequences() query takes about 1 s; with the attached patch it
>> takes 0.33 s and the query 25 ms.
>
> Confirmed on both branches with that setup. On master the query goes
> from about 2 s to 12 ms.

I'll admit I'm a tad surprised at the level of the regression. I suppose
it's fairly obvious that the gather-all-sequences approach should take
longer than the query-per-sequence one at some point, but I guess I was
expecting that threshold to be much higher than it actually is. It's
possible that when I was working on this I failed to appropriately test
non-cached scenarios.

> The full dump is not quite free. With the patch it came out 1-3% slower
> in every set of runs we did, 0.04 to 0.13 s out of 4 s. The new query
> makes the same 142,243 calls, plus a sort and a merge join that the old
> one did not need, and with the default work_mem the sort spills to disk
> (external merge, 3.7 MB). Timed alone, the two queries take 2637 ms and
> 2654 ms (median of 15), which is within the noise. We do not think it
> matters, but it is not zero, so the numbers are here.

I think a 1-3% hit is worth it to help use-cases like the reporter's.

> Keeping one row per sequence with the LEFT JOIN means the array has the
> same shape as before, so neither bsearch() caller had to change. And
> building the list as unnest('{...}'::pg_catalog.oid[]) AS src(...) is
> what pg_dump already does for its per-table queries (getTableAttrs(),
> getIndexes(), getTriggers() and others), down to the "do we have more
> than the '{'?" test.

I was a bit concerned about dumps with many sequences, but the list-of-OIDs
approach seems to work up to something like 50M sequences, if my LLM is to
be believed. If we needed to, we could fall back to the current non-list
query for dumps with more sequences than that, but I'm tempted to suggest
that 50 million sequences ought to be enough for anyone. Does anyone have
experience with workloads that use anywhere near that many sequences? I do
not.

I also briefly explored whether there was something we could do on the
server-side to improve this query's performance, but short of some kind of
elaborate pre-fetching mechanism to load the sequence tuples into shared
buffers, I don't think there's much there.

> 1. A comment at the call site.
>
> [...]
>
> /*
> * Collect sequence information. This must come after all
> * TableDataInfo objects have been created, since we fetch sequence
> * data only for sequences that have one.
> */

A comment along these lines seems reasonable.

> 2. Tests.
>
> The patch adds none. The speedup is hard to test, but the lock behaviour
> is deterministic: hold an uncommitted DROP SEQUENCE on a sequence
> outside the dumped schema in a background session, run pg_dump -n, and
> it has to finish. Would a small TAP test for that be welcome?

Eh, I don't think it's worth it. The existing tests already fail if we
skip a sequence that needs to be dumped, which is the main thing we need to
verify.

> That is all we have. With or without the comment, this looks ready for a
> committer to us.

I'll plan on committing this soon. Thanks again!

--
nathan

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Dmitry Dolgov 2026-11-30 17:06:40 Re: BUG #19957: jsonpath `last` returns the wrong index after a filter containing `exists($x[0])`
Previous Message Nitin Motiani 2026-11-30 16:39:43 Re: BUG #19936: Planning is slower on 19.0 with many partial indexes on a SQL function