From e8ae01966115a35d3815e0445da5f78878f6dd14 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Sat, 2 Feb 2013 12:56:14 -0500
Subject: [PATCH] Adjust COPY FREEZE error message to be more accurate and
 consistent.

Per suggestions from Noah and Tom.
---
 src/backend/commands/copy.c         | 50 +++++++++++++----------------
 src/test/regress/expected/copy2.out |  6 ++--
 2 files changed, 26 insertions(+), 30 deletions(-)

diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index 49cc8dd88f4..523c1e03315 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -1996,36 +1996,32 @@ CopyFrom(CopyState cstate)
 		hi_options |= HEAP_INSERT_SKIP_FSM;
 		if (!XLogIsNeeded())
 			hi_options |= HEAP_INSERT_SKIP_WAL;
+	}
 
-		/*
-		 * Optimize if new relfilenode was created in this subxact or
-		 * one of its committed children and we won't see those rows later
-		 * as part of an earlier scan or command. This ensures that if this
-		 * subtransaction aborts then the frozen rows won't be visible
-		 * after xact cleanup. Note that the stronger test of exactly
-		 * which subtransaction created it is crucial for correctness
-		 * of this optimisation.
-		 */
-		if (cstate->freeze)
-		{
-			if (!ThereAreNoPriorRegisteredSnapshots() || !ThereAreNoReadyPortals())
-				ereport(ERROR,
-						(ERRCODE_INVALID_TRANSACTION_STATE,
-						errmsg("cannot perform FREEZE because of prior transaction activity")));
+	/*
+	 * Optimize if new relfilenode was created in this subxact or
+	 * one of its committed children and we won't see those rows later
+	 * as part of an earlier scan or command. This ensures that if this
+	 * subtransaction aborts then the frozen rows won't be visible
+	 * after xact cleanup. Note that the stronger test of exactly
+	 * which subtransaction created it is crucial for correctness
+	 * of this optimisation.
+	 */
+	if (cstate->freeze)
+	{
+		if (!ThereAreNoPriorRegisteredSnapshots() || !ThereAreNoReadyPortals())
+			ereport(ERROR,
+					(ERRCODE_INVALID_TRANSACTION_STATE,
+					errmsg("cannot perform FREEZE because of prior transaction activity")));
 
-			if (cstate->rel->rd_createSubid == GetCurrentSubTransactionId() ||
-				cstate->rel->rd_newRelfilenodeSubid == GetCurrentSubTransactionId())
-				hi_options |= HEAP_INSERT_FROZEN;
-			else
-				ereport(ERROR,
-						(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE,
-						errmsg("cannot perform FREEZE because of transaction activity after table creation or truncation")));
-		}
+		if (cstate->rel->rd_createSubid != GetCurrentSubTransactionId() &&
+			cstate->rel->rd_newRelfilenodeSubid != GetCurrentSubTransactionId())
+			ereport(ERROR,
+					(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE,
+					 errmsg("cannot perform FREEZE because the table was not created or truncated in the current subtransaction")));
+
+		hi_options |= HEAP_INSERT_FROZEN;
 	}
-	else if (cstate->freeze)
-		ereport(ERROR,
-				(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE,
-				 errmsg("cannot perform FREEZE because the table was not created or truncated in the current transaction")));
 
 	/*
 	 * We need a ResultRelInfo so we can use the regular executor's
diff --git a/src/test/regress/expected/copy2.out b/src/test/regress/expected/copy2.out
index 5777b242747..34fa131c52b 100644
--- a/src/test/regress/expected/copy2.out
+++ b/src/test/regress/expected/copy2.out
@@ -334,12 +334,12 @@ SELECT * FROM vistest;
 COMMIT;
 TRUNCATE vistest;
 COPY vistest FROM stdin CSV FREEZE;
-ERROR:  cannot perform FREEZE because the table was not created or truncated in the current transaction
+ERROR:  cannot perform FREEZE because the table was not created or truncated in the current subtransaction
 BEGIN;
 TRUNCATE vistest;
 SAVEPOINT s1;
 COPY vistest FROM stdin CSV FREEZE;
-ERROR:  cannot perform FREEZE because of transaction activity after table creation or truncation
+ERROR:  cannot perform FREEZE because the table was not created or truncated in the current subtransaction
 COMMIT;
 BEGIN;
 INSERT INTO vistest VALUES ('z');
@@ -347,7 +347,7 @@ SAVEPOINT s1;
 TRUNCATE vistest;
 ROLLBACK TO SAVEPOINT s1;
 COPY vistest FROM stdin CSV FREEZE;
-ERROR:  cannot perform FREEZE because the table was not created or truncated in the current transaction
+ERROR:  cannot perform FREEZE because the table was not created or truncated in the current subtransaction
 COMMIT;
 CREATE FUNCTION truncate_in_subxact() RETURNS VOID AS
 $$
-- 
GitLab