From e215c3c0b49c65067b4de2288f97e7cbab4b90e9 Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Wed, 2 Feb 2022 09:48:37 -0500
Subject: [PATCH] Fix server crash bug in 'server' backup target.

When this code executed as superuser it appeared to work because no
system catalog lookups happened, but otherwise it crashes because there
is no transaction environment. Fix that.
---
 src/backend/replication/basebackup_server.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/backend/replication/basebackup_server.c b/src/backend/replication/basebackup_server.c
index 18b0e11d90..a878629668 100644
--- a/src/backend/replication/basebackup_server.c
+++ b/src/backend/replication/basebackup_server.c
@@ -10,6 +10,7 @@
  */
 #include "postgres.h"
 
+#include "access/xact.h"
 #include "catalog/pg_authid.h"
 #include "miscadmin.h"
 #include "replication/basebackup.h"
@@ -67,10 +68,12 @@ bbsink_server_new(bbsink *next, char *pathname)
 	sink->base.bbs_next = next;
 
 	/* Replication permission is not sufficient in this case. */
+	StartTransactionCommand();
 	if (!is_member_of_role(GetUserId(), ROLE_PG_WRITE_SERVER_FILES))
 		ereport(ERROR,
 				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
 				 errmsg("must be superuser or a member of the pg_write_server_files role to create server backup")));
+	CommitTransactionCommand();
 
 	/*
 	 * It's not a good idea to store your backups in the same directory that
-- 
2.24.3 (Apple Git-128)

