pgsql: Allow REPLICA IDENTITY to be set on an index that's not (yet) va - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Allow REPLICA IDENTITY to be set on an index that's not (yet) va
Date
Msg-id E1pJIKG-004oqz-Le@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Allow REPLICA IDENTITY to be set on an index that's not (yet) valid.

The motivation for this change is that when pg_dump dumps a
partitioned index that's marked REPLICA IDENTITY, it generates a
command sequence that applies REPLICA IDENTITY before the partitioned
index has been marked valid, causing restore to fail.  We could
perhaps change pg_dump to not do it like that, but that would be
difficult and would not fix existing dump files with the problem.
There seems to be very little reason for the backend to disallow
this anyway --- the code ignores indisreplident when the index
isn't valid --- so instead let's fix it by allowing the case.

Commit 9511fb37a previously expressed a concern that allowing
indisreplident to be set on invalid indexes might allow us to
wind up in a situation where a table could have indisreplident
set on multiple indexes.  I'm not sure I follow that concern
exactly, but in any case the only way that could happen is because
relation_mark_replica_identity is too trusting about the existing set
of markings being valid.  Let's just rip out its early-exit code path
(which sure looks like premature optimization anyway; what are we
doing expending code to make redundant ALTER TABLE ... REPLICA
IDENTITY commands marginally faster and not-redundant ones marginally
slower?) and fix it to positively guarantee that no more than one
index is marked indisreplident.

The pg_dump failure can be demonstrated in all supported branches,
so back-patch all the way.  I chose to back-patch 9511fb37a as well,
just to keep indisreplident handling the same in all branches.

Per bug #17756 from Sergey Belyashov.

Discussion: https://postgr.es/m/17756-dd50e8e0c8dd4a40@postgresql.org

Branch
------
REL_11_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/6c122eddecc1c2e702aa05d2868a3e6a0d75bda1

Modified Files
--------------
src/backend/catalog/index.c                    |  6 ++-
src/backend/commands/tablecmds.c               | 64 +++++++++-----------------
src/test/regress/expected/replica_identity.out | 37 +++++++++++++++
src/test/regress/sql/replica_identity.sql      | 20 ++++++++
4 files changed, 84 insertions(+), 43 deletions(-)


pgsql-committers by date:

Previous
From: Noah Misch
Date:
Subject: pgsql: Reject CancelRequestPacket having unexpected length.
Next
From: Tom Lane
Date:
Subject: pgsql: Track logrep apply workers' last start times to avoid useless wa