From 09b3cb5368b2fd3448384c8cf8860801d7328479 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas.vondra@postgresql.org>
Date: Fri, 11 Mar 2022 02:23:25 +0100
Subject: [PATCH 2/3] fixup: row-filter publications

When initializing the row filter, consider only publications that
actually include the relation (publish_as_relid). The publications may
include different ancestors, in which case the function would get
confused and conclude there's no row filter.
---
 src/backend/replication/pgoutput/pgoutput.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/backend/replication/pgoutput/pgoutput.c b/src/backend/replication/pgoutput/pgoutput.c
index dbac2690b7f..faf7555f913 100644
--- a/src/backend/replication/pgoutput/pgoutput.c
+++ b/src/backend/replication/pgoutput/pgoutput.c
@@ -1875,8 +1875,6 @@ get_rel_sync_entry(PGOutputData *data, Relation relation)
 				entry->pubactions.pubdelete |= pub->pubactions.pubdelete;
 				entry->pubactions.pubtruncate |= pub->pubactions.pubtruncate;
 
-				rel_publications = lappend(rel_publications, pub);
-
 				/*
 				 * We want to publish the changes as the top-most ancestor
 				 * across all publications. So we fetch all ancestors of the
@@ -1894,8 +1892,18 @@ get_rel_sync_entry(PGOutputData *data, Relation relation)
 				if (list_member_oid(ancestors, publish_as_relid))
 					continue;
 
-				/* The new value is an ancestor, so let's keep it. */
-				publish_as_relid = pub_relid;
+				/*
+				 * If the new value is an ancestor, discard the list of
+				 * publications through which we replicate it.
+				 */
+				if (publish_as_relid != pub_relid)
+				{
+					rel_publications = NIL;
+					publish_as_relid = pub_relid;
+				}
+
+				/* Track the publications. */
+				rel_publications = lappend(rel_publications, pub);
 			}
 		}
 
-- 
2.34.1

