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

From: Diego <mrstephenamell(at)gmail(dot)com>
To: Andrew Krylosov <krylosov(dot)andrew(at)gmail(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org, cesarg9(at)gmail(dot)com, Nathan Bossart <nathandbossart(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-26 16:18:43
Message-ID: 6b085be3.ec2c7a27.78c2c.aaa2@mx.google.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi Andrew,

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.

== What we tested ==

v1_PG18 on REL_18_STABLE at its base commit, 2d748cfe33 (me), and v1 on
master at 1e676702cac (Surya). Both apply cleanly and build without
warnings; on my side that is Debian 12, gcc 12.2, meson. The pg_dump TAP
suite passes on REL_18_STABLE with the patch.

Both still apply, with offsets, on today's REL_18_STABLE (2ea7106) and
master (d5fb939), and v1_PG19 applies on today's REL_19_STABLE
(5fd37fe). On those three, pgindent and "git diff --check" are clean.

We did not test the REL_19_STABLE variant beyond that; its changed lines
are the same as the REL_18_STABLE ones. We also did not run the patched
pg_dump against a pre-18 server; that branch still sends the same query
text as before.

== The regression and the fix ==

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.

We also ran it at the size of the original report: 142,237 sequences in
one schema ("big") and 6 in another ("small"). This is REL_18_STABLE,
-O2 without assertions, everything in cache, median of 5 runs. There is
one server and one database; only the pg_dump binary changes.

unpatched patched
pg_dump -n small -Fc 3.36 s 0.80 s
collectSequences() query 2652 ms 64 ms
pg_get_sequence_data() calls 142,243 6

pg_dump -Fc (whole database) 4.06 s 4.14 s
collectSequences() query 2628 ms 2675 ms
pg_get_sequence_data() calls 142,243 142,243
length of the query text 206 bytes 912 kB

The query times come from the server log, with
log_min_duration_statement = 0. The call counts are the loops of the
Function Scan node in EXPLAIN ANALYZE of that same query text.

Two things about these numbers.

They are CPU only. In the report, the same 142,237 sequences took 298 s
waiting on DataFileRead, so with a cold cache the gain is far bigger
than the 4x shown here.

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.

> The same dump also waits for an AccessExclusiveLock held on an
> unrelated sequence: with an uncommitted DROP SEQUENCE in the other
> schema, it waited until thattransaction ended.

Confirmed. With "BEGIN; DROP SEQUENCE big.s000002;" left open in another
session, the unpatched "pg_dump -n small" sits in wait_event Lock /
relation on the collectSequences() query until we kill it. The patched
one finishes in 0.7 s, the same as without the lock.

== Output and error behaviour ==

Plain-format dumps taken from that same database with the unpatched and
the patched pg_dump are identical, once the random \restrict and
\unrestrict lines are removed, for all of these:

-n small -n small -s
-n small -a -n small -s --sequence-data
-t small.tab -t small.called
-n small --exclude-table-data=small.never_called
-n nosq -N big
-s (no options)

"small" has a sequence advanced with nextval(), one never called, two
set with setval() (is_called true and false), a serial column and an
identity column. "nosq" is a schema with a table and no sequences: the
OID list is '{}' and pg_get_sequence_data() is never executed. The last
one is the full dump, with 142,243 setval() calls in both.

Surya ran the same kind of comparison on master (-n, -t, -a, -N,
-s --sequence-data and a full dump) with the same result.

The failure case is preserved too. Dumping "small" as a role that lacks
SELECT on one of its sequences fails in both with

pg_dump: error: failed to get data for sequence "never_called"; user
may lack SELECT privilege on the sequence or the sequence may have
been concurrently dropped

and a role that does have SELECT on them gets the same dump as a
superuser.

== The approach ==

What has to hold is that dumpSequenceData() never looks up a sequence
whose data was not fetched. We went through it:

- dumpSequenceData() is only reached for DO_SEQUENCE_SET objects. Those
are created only in makeTableDataInfo(), which is also what sets
tbinfo->dataObj. So "has a dataObj" is exactly "will get a
dumpSequenceData() call". Testing dataObj instead of
DUMP_COMPONENT_DATA is the right choice, because makeTableDataInfo()
can decline to create the object (--exclude-table-data,
--no-unlogged-table-data).

- makeTableDataInfo() is called from getTableData(), for both of its
calls in main(), and from processExtensionTables(), which runs inside
getSchemaData(). All of that happens before collectSequences().

- dumpSequence() does look up sequences that are not in the list, but
it only reads the definition fields, and those are still fetched for
every sequence.

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.

== Suggestions ==

1. A comment at the call site.

The comment inside collectSequences() says it relies on the
TableDataInfo objects already existing, but nothing at the call in
main() does, and that call sits some 30 lines below getTableData() with
unrelated calls in between. If someone later created a sequence's
TableDataInfo after collectSequences(), dumpSequenceData() would fail
with the "user may lack SELECT privilege ... or the sequence may have
been concurrently dropped" error, which would send them looking in the
wrong place. Something like:

/*
* Collect sequence information. This must come after all
* TableDataInfo objects have been created, since we fetch sequence
* data only for sequences that have one.
*/

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?

One catch: pg_dump sets statement_timeout and lock_timeout to 0 when it
connects, so without the fix such a test hangs instead of failing,
unless the test itself puts a timeout around pg_dump. We are happy to
write it if you think it is worth having.

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

Thanks for picking this up.
Surya Poondla, Christopher Bell and Diego

In response to

Browse pgsql-bugs by date

  From Date Subject
Previous Message Narayanan Venkateswaran 2026-11-26 15:15:53 Re: BUG #19863: Adding a partition hides a row in a DEFAULT subpartition with reordered columns