pg_dump crash on identity sequence with not loaded attributes

Artur Zakirov <zaartur@gmail.com>

From: Artur Zakirov <zaartur@gmail.com>
To: PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
Date: 2024-12-09T15:54:47Z
Lists: pgsql-bugs

Attachments

Hi all,

pg_dump crashes when a table is added into an extension and its
identity sequence isn't. It can be reproduced by pgmq by the following
way:

    git clone git@github.com:tembo-io/pgmq.git
    cd pgmq && git checkout v1.4.5
    make clean && make install
    psql -U postgres -c "create extension if not exists pgmq; SELECT
pgmq.create('my_queue'); select pgmq.send('my_queue', '\"asdf\"');"
    pg_dump -d postgres -U postgres -Fc -f backup

pg_dump will crash. The backtrace:

    * thread #1, queue = 'com.apple.main-thread', stop reason =
EXC_BAD_ACCESS (code=1, address=0x0)
        frame #0: 0x000000010001ce24 pg_dump`dumpTable [inlined]
dumpSequence(fout=0x00000001438045a0, tbinfo=0x00000001280205b0) at
pg_dump.c:17832:15 [opt]
    17829
fmtQualifiedDumpable(owning_tab));
    17830                        appendPQExpBuffer(query,
    17831
"ALTER COLUMN %s ADD GENERATED ",
    -> 17832
fmtId(owning_tab->attnames[tbinfo->owning_col - 1]));
    17833                        if
(owning_tab->attidentity[tbinfo->owning_col - 1] ==
ATTRIBUTE_IDENTITY_ALWAYS)
    17834                                appendPQExpBufferStr(query, "ALWAYS");
    17835                        else if
(owning_tab->attidentity[tbinfo->owning_col - 1] ==
ATTRIBUTE_IDENTITY_BY_DEFAULT)
    warning: pg_dump was compiled with optimization - stepping may
behave oddly; variables may not be available.
    (lldb) bt
    * thread #1, queue = 'com.apple.main-thread', stop reason =
EXC_BAD_ACCESS (code=1, address=0x0)
    * frame #0: 0x000000010001ce24 pg_dump`dumpTable [inlined]
dumpSequence(fout=0x00000001438045a0, tbinfo=0x00000001280205b0) at
pg_dump.c:17832:15 [opt]
        frame #1: 0x000000010001cc94
pg_dump`dumpTable(fout=0x00000001438045a0, tbinfo=0x00000001280205b0)
at pg_dump.c:15720:4 [opt]
        frame #2: 0x0000000100004f60 pg_dump`main [inlined]
dumpDumpableObject(fout=0x00000001438045a0, dobj=0x00000001280205b0)
at pg_dump.c:10616:4 [opt]
        frame #3: 0x0000000100004eac pg_dump`main(argc=<unavailable>,
argv=<unavailable>) at pg_dump.c:1085:3 [opt]
        frame #4: 0x000000019429c274 dyld`start + 2840

I created a PR for pgmq to fix the issue on their side:
https://github.com/tembo-io/pgmq/pull/352

While the client should probably include the sequence together with
the table and the issue is partially on their side, I think pg_dump
shouldn't crash in such cases. I could reproduce it at least since
Postgres 15 to master.

The attached patch fixes the issue on pg_dump side by ignoring
identity sequences if their table attributes are not loaded. I'm not
sure if this is the best fix. It might be better to raise an error
instead to let a user know that they should add the sequence to the
extension too.

-- 
Kind regards,
Artur

Commits

  1. Fix possible crash in pg_dump with identity sequences.

  2. Do not dump identity sequences with excluded parent table