*** ./src/bin/psql/common.c.orig	2004-09-19 16:37:04.000000000 +0200
--- ./src/bin/psql/common.c	2004-09-19 18:03:21.000000000 +0200
***************
*** 865,870 ****
--- 865,871 ----
  	TimevalStruct before,
  				after;
  	bool		OK;
+ 	bool		implicit_savepoint = false;
  
  	if (!pset.db)
  	{
***************
*** 908,913 ****
--- 909,931 ----
  		PQclear(results);
  	}
  
+ 	/*
+ 	 * Implicit savepoints make it possible to only rollback the last statement
+ 	 * on errors (interactive mode only, backend version >= 8.0)
+ 	 */
+ 	if (PQtransactionStatus(pset.db) == PQTRANS_INTRANS &&
+ 		PQserverVersion(pset.db) >= 80000 &&
+ 		GetVariableBool(pset.vars, "IMPLICIT_SAVEPOINTS") &&
+ 		!pset.notty)
+ 	{
+ 		/* use PSQLexec instead of PQexec here, so -E prints this query */
+ 		results = PSQLexec("SAVEPOINT pg_internal_psql", false);
+ 		if (!results)		
+ 			return false;
+ 		PQclear(results);
+ 		implicit_savepoint = true;
+ 	}
+ 
  	if (pset.timing)
  		GETTIMEOFDAY(&before);
  
***************
*** 943,948 ****
--- 961,976 ----
  
  	PrintNotifications();
  
+ 	/* if query failed, try to rollback to savepoint set before statement */
+ 	if (! OK
+ 		&& implicit_savepoint
+ 		&& PQtransactionStatus(pset.db) == PQTRANS_INERROR)
+ 	{
+ 		results = PSQLexec("ROLLBACK TO pg_internal_psql", false);
+ 		if (results)
+ 			PQclear(results);
+ 	}
+ 
  	return OK;
  }
  
*** ./src/bin/psql/startup.c.orig	2004-09-19 16:34:51.000000000 +0200
--- ./src/bin/psql/startup.c	2004-09-19 16:36:04.000000000 +0200
***************
*** 143,148 ****
--- 143,149 ----
  
  	/* Default values for variables that are used in noninteractive cases */
  	SetVariableBool(pset.vars, "AUTOCOMMIT");
+ 	SetVariable(pset.vars, "IMPLICIT_SAVEPOINTS", "off");
  	SetVariable(pset.vars, "VERBOSITY", "default");
  	pset.verbosity = PQERRORS_DEFAULT;
  
