Index: common.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/bin/psql/common.c,v
retrieving revision 1.110.2.1
diff -u -u -r1.110.2.1 common.c
--- common.c	22 Nov 2005 18:23:27 -0000	1.110.2.1
+++ common.c	30 Jun 2006 13:42:29 -0000
@@ -1067,8 +1067,6 @@
 	if (OK)
 		OK = PrintQueryResults(results);
 
-	PQclear(results);
-
 	/* If we made a temporary savepoint, possibly release/rollback */
 	if (on_error_rollback_savepoint)
 	{
@@ -1076,23 +1074,35 @@
 
 		/* We always rollback on an error */
 		if (transaction_status == PQTRANS_INERROR)
+		{
+			PQclear(results);
 			results = PQexec(pset.db, "ROLLBACK TO pg_psql_temporary_savepoint");
+		}
 		/* If they are no longer in a transaction, then do nothing */
 		else if (transaction_status != PQTRANS_INTRANS)
+		{
+			PQclear(results);
 			results = NULL;
+		}
 		else
 		{
 			/*
-			 * Do nothing if they are messing with savepoints themselves: If
+			 * Do nothing if they are messing with savepoints themselves: if
 			 * the user did RELEASE or ROLLBACK, our savepoint is gone. If
 			 * they issued a SAVEPOINT, releasing ours would remove theirs.
 			 */
 			if (strcmp(PQcmdStatus(results), "SAVEPOINT") == 0 ||
 				strcmp(PQcmdStatus(results), "RELEASE") == 0 ||
 				strcmp(PQcmdStatus(results), "ROLLBACK") == 0)
+			{
+				PQclear(results);
 				results = NULL;
+			}
 			else
+			{
+				PQclear(results);
 				results = PQexec(pset.db, "RELEASE pg_psql_temporary_savepoint");
+			}
 		}
 		if (PQresultStatus(results) != PGRES_COMMAND_OK)
 		{
@@ -1101,8 +1111,8 @@
 			ResetCancelConn();
 			return false;
 		}
-		PQclear(results);
 	}
+	PQclear(results);
 
 	/* Possible microtiming output */
 	if (OK && pset.timing)
