From 35f72956482c84fa592c49f629933694e10fad1e Mon Sep 17 00:00:00 2001
From: Hou Zhijie <houzj.fnst@cn.fujitsu.com>
Date: Mon, 13 Mar 2023 17:36:32 +0800
Subject: [PATCH 1/2] Fix column order problem

---
 src/backend/replication/logical/relation.c | 23 ++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/src/backend/replication/logical/relation.c b/src/backend/replication/logical/relation.c
index 7364bc3f07..ddabab0e01 100644
--- a/src/backend/replication/logical/relation.c
+++ b/src/backend/replication/logical/relation.c
@@ -53,7 +53,8 @@ typedef struct LogicalRepPartMapEntry
 } LogicalRepPartMapEntry;
 
 static Oid	FindLogicalRepLocalIndex(Relation localrel,
-									 LogicalRepRelation *remoterel);
+									 LogicalRepRelation *remoterel,
+									 AttrMap *attrmap);
 
 /*
  * Relcache invalidation callback for our relation map cache.
@@ -450,7 +451,9 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
 		 * of the relation cache entry (such as ANALYZE or CREATE/DROP index
 		 * on the relation).
 		 */
-		entry->localindexoid = FindLogicalRepLocalIndex(entry->localrel, remoterel);
+		entry->localindexoid = FindLogicalRepLocalIndex(entry->localrel,
+														remoterel,
+														entry->attrmap);
 
 		entry->localrelvalid = true;
 	}
@@ -722,7 +725,7 @@ logicalrep_partition_open(LogicalRepRelMapEntry *root,
 	 * We also prefer to run this code on the oldctx so that we do not leak
 	 * anything in the LogicalRepPartMapContext (hence CacheMemoryContext).
 	 */
-	entry->localindexoid = FindLogicalRepLocalIndex(partrel, remoterel);
+	entry->localindexoid = FindLogicalRepLocalIndex(partrel, remoterel, entry->attrmap);
 
 	entry->localrelvalid = true;
 
@@ -758,7 +761,7 @@ IsIndexOnlyOnExpression(IndexInfo *indexInfo)
  */
 static bool
 RemoteRelContainsLeftMostColumnOnIdx(IndexInfo  *indexInfo,
-									 LogicalRepRelation  *remoterel)
+									 AttrMap *attrmap)
 {
 	AttrNumber			keycol;
 
@@ -769,7 +772,7 @@ RemoteRelContainsLeftMostColumnOnIdx(IndexInfo  *indexInfo,
 	if (!AttributeNumberIsValid(keycol))
 		return false;
 
-	return bms_is_member(keycol-1, remoterel->attkeys);
+	return attrmap->attnums[AttrNumberGetAttrOffset(keycol)] >= 0;
 }
 
 /*
@@ -801,8 +804,7 @@ RemoteRelContainsLeftMostColumnOnIdx(IndexInfo  *indexInfo,
  * If no suitable index is found, returns InvalidOid.
  */
 static Oid
-FindUsableIndexForReplicaIdentityFull(Relation localrel,
-									  LogicalRepRelation *remoterel)
+FindUsableIndexForReplicaIdentityFull(Relation localrel, AttrMap *attrmap)
 {
 	List	   *idxlist = RelationGetIndexList(localrel);
 	ListCell   *lc;
@@ -819,7 +821,7 @@ FindUsableIndexForReplicaIdentityFull(Relation localrel,
 		idxInfo = BuildIndexInfo(idxRel);
 		isUsableIdx = IsIndexUsableForReplicaIdentityFull(idxInfo);
 		containsLeftMostCol =
-			RemoteRelContainsLeftMostColumnOnIdx(idxInfo, remoterel);
+			RemoteRelContainsLeftMostColumnOnIdx(idxInfo, attrmap);
 		index_close(idxRel, AccessShareLock);
 
 		/* Return the first eligible index found */
@@ -881,7 +883,8 @@ IsIdxSafeToSkipDuplicates(Relation rel, Oid idxoid)
  * returns InvalidOid.
  */
 static Oid
-FindLogicalRepLocalIndex(Relation localrel, LogicalRepRelation *remoterel)
+FindLogicalRepLocalIndex(Relation localrel, LogicalRepRelation *remoterel,
+						 AttrMap *attrmap)
 {
 	Oid			idxoid;
 
@@ -916,7 +919,7 @@ FindLogicalRepLocalIndex(Relation localrel, LogicalRepRelation *remoterel)
 		 * long run or use the full-fledged planner which could cause
 		 * overhead.
 		 */
-		return FindUsableIndexForReplicaIdentityFull(localrel, remoterel);
+		return FindUsableIndexForReplicaIdentityFull(localrel, attrmap);
 	}
 
 	return InvalidOid;
-- 
2.30.0.windows.2

