[PATCH] Fix stale relation close in sequence synchronization

Ayush Tiwari <ayushtiwari.slg01@gmail.com>

From: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Cc: Amit Kapila <amit.kapila16@gmail.com>
Date: 2026-04-28T11:24:24Z
Lists: pgsql-hackers

Attachments

Hi,

I found a crash in the logical replication sequence synchronization worker
when the publisher returns NULL sequence data for a sequence after at least
one sequence in the same sync batch has already been processed.

One way to reproduce this is to use a subscription that connects to the
publisher as a replication role that can read one published sequence but
cannot read another one. pg_get_sequence_data() returns NULLs for the
inaccessible sequence. In get_and_validate_seq_info(), that path returns
COPYSEQ_SKIPPED before assigning a new value to the Relation output
argument.
copy_sequences() then still sees the Relation pointer left from the previous
row and calls table_close() on it again. On a cassert build, this trips:

    TRAP: failed Assert("rel->rd_refcnt > 0");  "relcache.c" file

The attached patch clears the output Relation pointer at the start of
get_and_validate_seq_info() and clears the local pointer in copy_sequences()
after closing it. That keeps early returns from reusing a relation from a
previous row.

The patch also adds a TAP test to 036_sequences.pl.

Thoughts?

Regards,
Ayush

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Fix BF failure introduced in commit 2bf6c9ff71.

  2. Fix double table_close of sequence_rel in copy_sequences().

  3. Fix BF failure introduced by commit b3f6b14cf4.