From 30838d2102d66ee560582cca1d0788e903dd5b41 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=BE=AF=E5=BF=97=E6=9D=B0?=
 <houzhijie@houzhijiedeMacBook-Pro.local>
Date: Thu, 8 Oct 2026 10:31:14 +0800
Subject: [PATCH vTopup] Simplify codes

---
 src/backend/replication/logical/conflict.c | 91 ++++++++--------------
 1 file changed, 33 insertions(+), 58 deletions(-)

diff --git a/src/backend/replication/logical/conflict.c b/src/backend/replication/logical/conflict.c
index 5d6da1c8632..87ba52799e5 100644
--- a/src/backend/replication/logical/conflict.c
+++ b/src/backend/replication/logical/conflict.c
@@ -316,10 +316,11 @@ ReportApplyConflict(EState *estate, ResultRelInfo *relinfo, int elevel,
 					TupleTableSlot *remoteslot, List *conflicttuples)
 {
 	Relation	localrel = relinfo->ri_RelationDesc;
-	ConflictLogDest dest;
+	ConflictLogDest dest = CONFLICT_LOG_DEST_LOG;
 	Relation	conflictlogrel = NULL;
-	bool		log_dest_table = false;
-	bool		log_dest_logfile = false;
+	bool		log_dest_table;
+	bool		log_dest_logfile;
+	StringInfoData err_detail;
 
 	pgstat_report_subscription_conflict(MySubscription->oid, type);
 
@@ -330,29 +331,12 @@ ReportApplyConflict(EState *estate, ResultRelInfo *relinfo, int elevel,
 	 * always reported exclusively to the server log.
 	 */
 	if (elevel < ERROR)
-	{
 		conflictlogrel = get_conflictlog_dest_and_table(&dest);
-		log_dest_table = CONFLICTS_LOGGED_TO_TABLE(dest);
-		log_dest_logfile = CONFLICTS_LOGGED_TO_LOG(dest);
 
-		/*
-		 * If a conflict log table was requested but it has been dropped
-		 * concurrently (e.g. a concurrent ALTER SUBSCRIPTION changed
-		 * conflict_log_destination), get_conflictlog_dest_and_table()
-		 * returned NULL.  Fall back to logging to the server log so that the
-		 * conflict is not lost.
-		 */
-		if (log_dest_table && conflictlogrel == NULL)
-		{
-			log_dest_table = false;
-			log_dest_logfile = true;
-		}
-	}
-	else
-	{
-		/* ERROR-level conflicts always go to the server log with full detail */
-		log_dest_logfile = true;
-	}
+	log_dest_table = CONFLICTS_LOGGED_TO_TABLE(dest);
+	log_dest_logfile = CONFLICTS_LOGGED_TO_LOG(dest);
+
+	initStringInfo(&err_detail);
 
 	/*
 	 * Report the conflict to the server log.  When the server log is one of
@@ -362,10 +346,6 @@ ReportApplyConflict(EState *estate, ResultRelInfo *relinfo, int elevel,
 	 */
 	if (log_dest_logfile)
 	{
-		StringInfoData err_detail;
-
-		initStringInfo(&err_detail);
-
 		/* Form errdetail message by combining conflicting tuples information. */
 		if (conflicttuples != NIL)
 		{
@@ -388,34 +368,23 @@ ReportApplyConflict(EState *estate, ResultRelInfo *relinfo, int elevel,
 									 0,
 									 &err_detail);
 		}
-
-		/* Standard reporting with full internal details. */
-		ereport(elevel,
-				errcode_apply_conflict(type),
-				errmsg("conflict detected on relation \"%s.%s\": conflict=%s",
-					   get_namespace_name(RelationGetNamespace(localrel)),
-					   RelationGetRelationName(localrel),
-					   ConflictTypeNames[type]),
-				errdetail_internal("%s", err_detail.data));
 	}
 	else if (log_dest_table)
 	{
-		/*
-		 * Not logging conflict details to the server log; report the conflict
-		 * but omit raw tuple data since it is captured in the conflict log
-		 * table.
-		 */
-		ereport(elevel,
-				errcode_apply_conflict(type),
-				errmsg("conflict detected on relation \"%s.%s\": conflict=%s",
-					   get_namespace_name(RelationGetNamespace(localrel)),
-					   RelationGetRelationName(localrel),
-					   ConflictTypeNames[type]),
-				errdetail("Conflict details are logged to the conflict log table: %s.%s",
-						  get_namespace_name(RelationGetNamespace(conflictlogrel)),
-						  RelationGetRelationName(conflictlogrel)));
+		appendStringInfo(&err_detail,
+						 _("Conflict details are logged to the conflict log table: %s.%s"),
+						 get_namespace_name(RelationGetNamespace(conflictlogrel)),
+						 RelationGetRelationName(conflictlogrel));
 	}
 
+	ereport(elevel,
+			errcode_apply_conflict(type),
+			errmsg("conflict detected on relation \"%s.%s\": conflict=%s",
+				   get_namespace_name(RelationGetNamespace(localrel)),
+				   RelationGetRelationName(localrel),
+				   ConflictTypeNames[type]),
+			errdetail_internal("%s", err_detail.data));
+
 	/*
 	 * Insert into the conflict log table for LOG-level conflicts if
 	 * requested.
@@ -476,13 +445,13 @@ InitConflictIndexes(ResultRelInfo *relInfo)
  *
  * The table is opened with try_table_open(), so NULL is returned if the
  * conflict log table has been dropped concurrently (e.g. by an ALTER
- * SUBSCRIPTION that changed conflict_log_destination).  Callers must treat a
- * NULL result for a table destination as "table unavailable" and fall back to
- * server-log reporting rather than failing.
+ * SUBSCRIPTION that changed conflict_log_destination). In that case
+ * *log_dest falls back to server-log reporting.
  */
 static Relation
 get_conflictlog_dest_and_table(ConflictLogDest *log_dest)
 {
+	Relation	conflictlogrel;
 	Oid			conflictlogrelid;
 
 	/*
@@ -500,11 +469,17 @@ get_conflictlog_dest_and_table(ConflictLogDest *log_dest)
 	Assert(OidIsValid(conflictlogrelid));
 
 	/*
-	 * Use try_table_open(): the table may have been dropped concurrently by
-	 * an ALTER SUBSCRIPTION that changed conflict_log_destination.  Returning
-	 * NULL lets the caller fall back to the server log instead of failing.
+	 * Use try_table_open(): the table may have been dropped concurrently by an
+	 * ALTER SUBSCRIPTION that changed conflict_log_destination.  Returning
+	 * NULL and tell the caller to fall back to the server log instead of
+	 * failing.
 	 */
-	return try_table_open(conflictlogrelid, RowExclusiveLock);
+	conflictlogrel = try_table_open(conflictlogrelid, RowExclusiveLock);
+
+	if (!conflictlogrel)
+		*log_dest = CONFLICT_LOG_DEST_LOG;
+
+	return conflictlogrel;
 }
 
 /*
-- 
2.50.1 (Apple Git-155)

