Apply worker can pick an invalid index for REPLICA IDENTITY FULL lookups

From: Mihail Nikalayeu <mihailnikalayeu(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>, onderkalaci(at)gmail(dot)com
Subject: Apply worker can pick an invalid index for REPLICA IDENTITY FULL lookups
Date: 2026-08-22 19:56:00
Message-ID: CADzfLwWuubcbJBDRZ_J1SSqHDNjNmUYSAgf5y=17LxmP401xbw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello,

When the remote relation has REPLICA IDENTITY FULL and the local one
has no primary key or replica identity, the apply worker looks for any
index it can search by.
FindUsableIndexForReplicaIdentityFull() walks RelationGetIndexList()
and takes the first index of a suitable shape without checking if it
is valid.
That list omits only indexes that are not indislive, so the leftover
from a failed CREATE INDEX CONCURRENTLY is eligible -- and such an
index need not contain every row.

A failed CIC validation leaves an index that scans cleanly but lacks
exactly the rows validation would have added. Consequently, updates
and deletes for those rows are dropped as update_missing conflicts and
the subscriber quietly diverges.

A failed build leaves an index that is empty down to the metapage,
which parks the subscription in a permanent error:
ERROR: could not read blocks 0..0 in file "base/5/16433": read only 0
of 8192 bytes

The fix and a reproducer are in the attachments.

Affects 16 and up, from 89e46da5e51.

Regards,
Mikhail Nikalayeu

Attachment Content-Type Size
v1-0001-Don-t-choose-an-invalid-index-for-REPLICA-IDENTIT.patch application/octet-stream 5.0 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Jonathan S. Katz 2026-08-22 19:56:40 Re: add list of major features to the v19 release notes
Previous Message Andres Freund 2026-08-22 19:43:45 Re: PGQ catalog representation and pg_dump support