Re: Logical replication can lose an update after concurrent index invalidation

Поиск
Список
Период
Сортировка
Искать

Re: Logical replication can lose an update after concurrent index invalidation

От:
vignesh C <vignesh21@gmail.com>
Дата:
On Fri, 28 Aug 2026 at 03:05, Mihail Nikalayeu
 wrote:
>
> Attached are patches for master (which also applies to v19 as-is),
> v18, v17 and v16. The back-branch patches contain the code change
> only. The test requires the injection point added by bc32a12e0db; v18
> and v17 don't have it, and it can't be used on v16.

RelationFindReplTupleByIndex() is an extern function declared in a
core executor header (executor.h). Adding a new parameter
(isIdxSafeToSkipDuplicates) changes its calling convention. Any
third-party extension or out-of-tree apply worker compiled against an
earlier minor version like PG16 branch that calls this function will
pass incorrect arguments on the stack/registers, leading to stack
corruption or crashes when running on a patched minor release.
--- a/src/include/executor/executor.h
+++ b/src/include/executor/executor.h
@@ -652,6 +652,7 @@ extern void check_exclusion_constraint(Relation
heap, Relation index,
  * prototypes from functions in execReplication.c
  */
 extern bool RelationFindReplTupleByIndex(Relation rel, Oid idxoid,
+ bool isIdxSafeToSkipDuplicates,
  LockTupleMode lockmode,
  TupleTableSlot *searchslot,
  TupleTableSlot *outslot);

Similarly I'm not sure of adding isidentity to LogicalRepRelMapEntry
in back branches as it will change its size and memory layout.
External extensions that embed, allocate, or iterate over arrays of
LogicalRepRelMapEntry across minor releases will experience struct
size mismatches and potential memory misalignment.
diff --git a/src/include/replication/logicalrelation.h
b/src/include/replication/logicalrelation.h
index 3f4d906d741..4f98b1ea789 100644
--- a/src/include/replication/logicalrelation.h
+++ b/src/include/replication/logicalrelation.h
@@ -33,6 +33,11 @@ typedef struct LogicalRepRelMapEntry
  AttrMap    *attrmap; /* map of local attributes to remote ones */
  bool updatable; /* Can apply updates/deletes? */
  Oid localindexoid; /* which index to use, or InvalidOid if none */
+ bool isidentity; /* localindexoid was chosen as the
+ * relation's replica identity or
+ * primary key, rather than as one
+ * usable for a REPLICA IDENTITY
+ * FULL remote relation */

Regards,
Vignesh


Re: Logical replication can lose an update after concurrent index invalidation

От:
vignesh C <vignesh21@gmail.com>
Дата:
On Thu, 3 Sept 2026 at 16:15, Mihail Nikalayeu
 wrote:
>
> Zhijie, Amit, thanks for the reviews!
>
> > we shall mention in the comments atop the old function that it should
> > not be used in new code anymore
>
> Done.
>

Couple of minor comments:
1) I was able to compile without this header inclusion:
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -249,6 +249,7 @@

 #include "access/genam.h"
 #include "access/commit_ts.h"
+#include "access/htup_details.h"
 #include "access/table.h"

2) pgindent needs to be run for the modified changes:
a) pgindent shows this needs adjustment:
+                    * A replica identity or primary key index
identifies the row on
+                        * its own.  The catalogs might no longer call
it the identity,
+                        * as DROP INDEX CONCURRENTLY or REINDEX
CONCURRENTLY can commit

b) similarly here too:
+       bool            idxisreplident; /* whether localindexoid is
the relation's
+                                                                *
replica identity or primary key, rather
+                                                                *
than an index usable for a REPLICA
+                                                                *
IDENTITY FULL remote relation.  Placed
+                                                                *
here to fit in existing padding, which
+                                                                *
leaves the struct layout unchanged in
+                                                                * the
back branches. */

Regards,
Vignesh


Re: Logical replication can lose an update after concurrent index invalidation

От:
Amit Kapila <amit.kapila16@gmail.com>
Дата:
Вложения:
On Mon, Sep 7, 2026 at 9:04 PM vignesh C  wrote:
>
> On Thu, 3 Sept 2026 at 16:15, Mihail Nikalayeu
>  wrote:
> >
> > Zhijie, Amit, thanks for the reviews!
> >
> > > we shall mention in the comments atop the old function that it should
> > > not be used in new code anymore
> >
> > Done.
> >
>
> Couple of minor comments:
> 1) I was able to compile without this header inclusion:
> --- a/src/backend/replication/logical/worker.c
> +++ b/src/backend/replication/logical/worker.c
> @@ -249,6 +249,7 @@
>
>  #include "access/genam.h"
>  #include "access/commit_ts.h"
> +#include "access/htup_details.h"
>  #include "access/table.h"
>

Fixed in the attached. Apart from this I changed multiple comments to
make those clear. One notable change is, I moved the newly added
boolean after localindexoid as it reads better there because then we
don't need to forward reference the fields. For back-branches, if it
needs to be moved to an earlier location then we can do that in those
versions but for HEAD and 19, the new location seems better.

Also, shall we keep just one test, say Drop Index Concurrently instead
of two as both tests do the same thing in a slightly different way? I
have not done that but if you agree please update the patch
accordingly.

-- 
With Regards,
Amit Kapila.

Re: Logical replication can lose an update after concurrent index invalidation

От:
Zhijie Hou <houzhijie22@gmail.com>
Дата:
Hi,

On Fri, Sep 18, 2026 at 8:55 PM Amit Kapila  wrote:
> Fixed in the attached. Apart from this I changed multiple comments to
> make those clear. One notable change is, I moved the newly added
> boolean after localindexoid as it reads better there because then we
> don't need to forward reference the fields. For back-branches, if it
> needs to be moved to an earlier location then we can do that in those
> versions but for HEAD and 19, the new location seems better.
>
> Also, shall we keep just one test, say Drop Index Concurrently instead
> of two as both tests do the same thing in a slightly different way? I
> have not done that but if you agree please update the patch
> accordingly.

I also think one DROP INDEX CONCURRENTLY test is sufficient to cover this bug.

The patch looks mostly good to me, I just have a few minor comments:

1.

 extern bool RelationFindReplTupleByIndex(Relation rel, Oid idxoid,
+ bool isIdxSafeToSkipDuplicates,

Shall we use a shorter parameter name, like skipduplicates, to stay consistent
with the other parameters of this function?

2.

+ * XXX This can disagree with the index the apply worker searched by,
+ * see FindReplTupleInLocalRel(). It may not even be one that
+ * ExecOpenIndices() locked.
  */
  Oid replica_index = GetRelationIdentityOrPK(localrel);

Just in case we want to fix this in the current patch, I think one option that
avoids adding a new parameter to multiple public functions would be to cache the
replica identity index OID in remote_ctx and expose a function to retrieve it.
We could then call that function here instead of GetRelationIdentityOrPK().

Otherwise, we would need to add a new index OID parameter to
ExecSimpleRelationUpdate, even though the identity index isn't otherwise used
there - and ReportApplyConflict would need a new parameter as well.

Best Regards,
Zhijie Hou


Re: Logical replication can lose an update after concurrent index invalidation

От:
vignesh C <vignesh21@gmail.com>
Дата:
On Fri, 18 Sept 2026 at 18:25, Amit Kapila  wrote:
>
> Also, shall we keep just one test, say Drop Index Concurrently instead
> of two as both tests do the same thing in a slightly different way? I
> have not done that but if you agree please update the patch
> accordingly.

I also felt one test should be fine.

The patch mostly looks good. I have one small comment: the tests use a
different ordering for wakeup and detach. If we retain the second
test, we could keep the ordering consistent.
+       # Release the worker.  The index it holds is no longer the identity.
+       $node_subscriber->safe_psql(
+               'postgres',
+               "SELECT
injection_points_wakeup('apply-update-before-open-indices');
+                SELECT
injection_points_detach('apply-update-before-open-indices');"
+       );

+       $node_subscriber->safe_psql(
+               'postgres',
+               "SELECT
injection_points_detach('apply-update-before-open-indices');
+                SELECT
injection_points_wakeup('apply-update-before-open-indices');"
+       );

Regards,
Vignesh


Re: Logical replication can lose an update after concurrent index invalidation

От:
Amit Kapila <amit.kapila16@gmail.com>
Дата:
On Mon, Sep 21, 2026 at 2:03 PM Zhijie Hou  wrote:
>
> On Fri, Sep 18, 2026 at 8:55 PM Amit Kapila  wrote:
> > Fixed in the attached. Apart from this I changed multiple comments to
> > make those clear. One notable change is, I moved the newly added
> > boolean after localindexoid as it reads better there because then we
> > don't need to forward reference the fields. For back-branches, if it
> > needs to be moved to an earlier location then we can do that in those
> > versions but for HEAD and 19, the new location seems better.
> >
> > Also, shall we keep just one test, say Drop Index Concurrently instead
> > of two as both tests do the same thing in a slightly different way? I
> > have not done that but if you agree please update the patch
> > accordingly.
>
> I also think one DROP INDEX CONCURRENTLY test is sufficient to cover this bug.
>
> The patch looks mostly good to me, I just have a few minor comments:
>
> 1.
>
>  extern bool RelationFindReplTupleByIndex(Relation rel, Oid idxoid,
> + bool isIdxSafeToSkipDuplicates,
>
> Shall we use a shorter parameter name, like skipduplicates, to stay consistent
> with the other parameters of this function?
>

LGTM.

> 2.
>
> + * XXX This can disagree with the index the apply worker searched by,
> + * see FindReplTupleInLocalRel(). It may not even be one that
> + * ExecOpenIndices() locked.
>   */
>   Oid replica_index = GetRelationIdentityOrPK(localrel);
>
> Just in case we want to fix this in the current patch, I think one option that
> avoids adding a new parameter to multiple public functions would be to cache the
> replica identity index OID in remote_ctx and expose a function to retrieve it.
> We could then call that function here instead of GetRelationIdentityOrPK().
>
> Otherwise, we would need to add a new index OID parameter to
> ExecSimpleRelationUpdate, even though the identity index isn't otherwise used
> there - and ReportApplyConflict would need a new parameter as well.
>

I think this is a lower priority case. Let's do this separately.

-- 
With Regards,
Amit Kapila.


Re: Logical replication can lose an update after concurrent index invalidation

От:
vignesh C <vignesh21@gmail.com>
Дата:
Вложения:
On Fri, 18 Sept 2026 at 18:25, Amit Kapila  wrote:
>
> On Mon, Sep 7, 2026 at 9:04 PM vignesh C  wrote:
> >
> > On Thu, 3 Sept 2026 at 16:15, Mihail Nikalayeu
> >  wrote:
> > >
> > > Zhijie, Amit, thanks for the reviews!
> > >
> > > > we shall mention in the comments atop the old function that it should
> > > > not be used in new code anymore
> > >
> > > Done.
> > >
> >
> > Couple of minor comments:
> > 1) I was able to compile without this header inclusion:
> > --- a/src/backend/replication/logical/worker.c
> > +++ b/src/backend/replication/logical/worker.c
> > @@ -249,6 +249,7 @@
> >
> >  #include "access/genam.h"
> >  #include "access/commit_ts.h"
> > +#include "access/htup_details.h"
> >  #include "access/table.h"
> >
>
> Fixed in the attached. Apart from this I changed multiple comments to
> make those clear. One notable change is, I moved the newly added
> boolean after localindexoid as it reads better there because then we
> don't need to forward reference the fields. For back-branches, if it
> needs to be moved to an earlier location then we can do that in those
> versions but for HEAD and 19, the new location seems better.

I felt we will have to use the earlier location in case of
back-branches to avoid offset changing.

> Also, shall we keep just one test, say Drop Index Concurrently instead
> of two as both tests do the same thing in a slightly different way? I
> have not done that but if you agree please update the patch
> accordingly.

Here is an updated version including the back-branches version.
This version also addresses Hou-san's agreed 1st comment from [1].

[1] - https://www.postgresql.org/message-id/CAFvd2n-NBbpLM5yhfxLdmpLy5vshpBNqbJ_vX4cj6Z3Wx5BwFw%40mail.gmail.com

Regards,
Vignesh

Re: Logical replication can lose an update after concurrent index invalidation

От:
Amit Kapila <amit.kapila16@gmail.com>
Дата:
On Mon, Sep 21, 2026 at 6:01 PM vignesh C  wrote:
>
> Here is an updated version including the back-branches version.
> This version also addresses Hou-san's agreed 1st comment from [1].
>

We should include the test till the version where required Injection
Point functionality is present.

-- 
With Regards,
Amit Kapila.


Re: Logical replication can lose an update after concurrent index invalidation

От:
vignesh C <vignesh21@gmail.com>
Дата:
Вложения:
On Tue, 22 Sept 2026 at 15:33, Amit Kapila  wrote:
>
> On Mon, Sep 21, 2026 at 6:01 PM vignesh C  wrote:
> >
> > Here is an updated version including the back-branches version.
> > This version also addresses Hou-san's agreed 1st comment from [1].
> >
>
> We should include the test till the version where required Injection
> Point functionality is present.

Here is an updated version with the injection point and its test added
up to the PG17 version, where injection points were introduced.
I have also retained the DROP INDEX CONCURRENTLY test instead of the
REINDEX CONCURRENTLY test, as the former requires only one injection
point (apply-update-before-open-indices), whereas the latter requires
two injection points: reindex-relation-concurrently-before-swap and
apply-update-before-open-indices.

Regards,
Vignesh

Re: Logical replication can lose an update after concurrent index invalidation

От:
vignesh C <vignesh21@gmail.com>
Дата:
Вложения:
On Wed, 23 Sept 2026 at 15:26, vignesh C  wrote:
>
> On Tue, 22 Sept 2026 at 15:33, Amit Kapila  wrote:

The ABI compliance check in buildfarm failed at [1], [2], and [3]
after the patch was committed because the LogicalRepRelMapEntry
structure added the idxisreplident field. However, this does not cause
an ABI break, as the new bool uses existing alignment padding, so the
offsets of existing fields and sizeof(LogicalRepRelMapEntry) remain
unchanged. Here is a patch to document this in
.abi-compliance-history.

[1] - https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=akepa&dt=2026-09-28%2011%3A27%3A04
[2] - https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=akepa&dt=2026-09-28%2011%3A26%3A06
[3] - https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=crake&dt=2026-09-28%2011%3A47%3A02

Regards,
Vignesh

Re: Logical replication can lose an update after concurrent index invalidation

От:
Amit Kapila <amit.kapila16@gmail.com>
Дата:
On Mon, Sep 28, 2026 at 8:20 AM vignesh C  wrote:
>
> The ABI compliance check in buildfarm failed at [1], [2], and [3]
> after the patch was committed because the LogicalRepRelMapEntry
> structure added the idxisreplident field. However, this does not cause
> an ABI break, as the new bool uses existing alignment padding, so the
> offsets of existing fields and sizeof(LogicalRepRelMapEntry) remain
> unchanged. Here is a patch to document this in
> .abi-compliance-history.
>

Thanks, I've pushed the patches.

-- 
With Regards,
Amit Kapila.


FAQ