From 889b24547a2a13f2759f33c120286cd2fa3bb6ac Mon Sep 17 00:00:00 2001
From: Hou Zhijie <houzj.fnst@cn.fujitsu.com>
Date: Fri, 12 May 2023 17:40:08 +0800
Subject: [PATCH] Fix possible logical replication table sync crash

Commit e7e7da2f8d57bbe6c8fb463a9eec51486000ba2e added a new
password_required option but forgot that you need database access
to check whether an arbitrary role ID is a superuser.

e7e7da2f8d57bbe6c8fb463a9eec51486000ba2e fixed a similar bug in apply
worker, and this patch is to fix the similar bug in table sync worker.

---
 src/backend/replication/logical/tablesync.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index 0c71ae9ba7..5945f19af0 100644
--- a/src/backend/replication/logical/tablesync.c
+++ b/src/backend/replication/logical/tablesync.c
@@ -1285,10 +1285,15 @@ LogicalRepSyncTableStart(XLogRecPtr *origin_startpos)
 									slotname,
 									NAMEDATALEN);
 
+	StartTransactionCommand();
+
 	/* Is the use of a password mandatory? */
 	must_use_password = MySubscription->passwordrequired &&
 		!superuser_arg(MySubscription->owner);
 
+	/* Note that the superuser_arg call can access the DB */
+	CommitTransactionCommand();
+
 	/*
 	 * Here we use the slot name instead of the subscription name as the
 	 * application_name, so that it is different from the leader apply worker,
-- 
2.30.0.windows.2

