From d31e7b80bf0368f42e94767a0dbf3570af72c88b Mon Sep 17 00:00:00 2001
From: Jim Jones <jim.jones@uni-muenster.de>
Date: Thu, 15 Oct 2026 20:35:03 +0200
Subject: [PATCH v1] Reject COPY TO of unlogged relations during recovery

---
 src/backend/commands/copyto.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/src/backend/commands/copyto.c b/src/backend/commands/copyto.c
index a187d8ec284..18a87efe2e9 100644
--- a/src/backend/commands/copyto.c
+++ b/src/backend/commands/copyto.c
@@ -21,6 +21,7 @@
 #include "access/table.h"
 #include "access/tableam.h"
 #include "access/tupconvert.h"
+#include "access/xlog.h"
 #include "catalog/pg_inherits.h"
 #include "commands/copyapi.h"
 #include "commands/progress.h"
@@ -889,6 +890,25 @@ BeginCopyTo(ParseState *pstate,
 							RelationGetRelationName(rel))));
 	}
 
+	/* Temporary and unlogged relations are inaccessible during recovery. */
+	if (rel != NULL && RecoveryInProgress())
+	{
+		bool		accessible = RelationIsPermanent(rel);
+
+		foreach_oid(child, children)
+		{
+			if (get_rel_persistence(child) != RELPERSISTENCE_PERMANENT)
+			{
+				accessible = false;
+				break;
+			}
+		}
+
+		if (!accessible)
+			ereport(ERROR,
+					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+					 errmsg("cannot access temporary or unlogged relations during recovery")));
+	}
 
 	/* Allocate workspace and zero all fields */
 	cstate = palloc0_object(CopyToStateData);
-- 
2.56.0

