Rollback on Error

Started by Michael Paesoldover 21 years ago18 messages
#1Michael Paesold
mpaesold@gmx.at

I just read this in the MySQL manual:
(http://dev.mysql.com/doc/mysql/en/InnoDB_Error_handling.html)

"Error handling in InnoDB is not always the same as specified in the SQL
standard. According to the standard, any error during an SQL statement
should cause the rollback of that statement. InnoDB sometimes rolls back
only part of the statement, or the whole transaction. The following items
describe how InnoDB performs error handling:"

Ignore InnoDB, but read "According to the standard, any error during an SQL
statement should cause the rollback of that statement"...

I though the postgres behaviour of rolling back the whole transaction was
standard? If that is not the case, I don't understand why core seems to be
against a mode (GUC), where an implicit savepoint is generated before each
statement so that "rollback of the last statement" would be possible.

Is MySQL wrong on the standard, or has postgresql core attitude changed to
support the standard where possible - at least if the user wants?

Best Regards,
Michael Paesold

P.S: again thanks for PostgreSQL!

#2Martijn van Oosterhout
kleptog@svana.org
In reply to: Michael Paesold (#1)
Re: Rollback on Error

On Tue, Sep 14, 2004 at 11:39:32AM +0200, Michael Paesold wrote:

I though the postgres behaviour of rolling back the whole transaction was
standard? If that is not the case, I don't understand why core seems to be
against a mode (GUC), where an implicit savepoint is generated before each
statement so that "rollback of the last statement" would be possible.

Well. If such a mode ever becomes available I'll be looking into how to
make sure it never gets turned on. My mind is currently boggling at the
number of things it would break.

A transaction is either committed as a whole, without errors, or not at
all. If you want to do a savepoint after each statement, go right
ahead, nothing is stopping you. I just don't think making it any kind
of default is a very good idea...
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/

Show quoted text

Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
tool for doing 5% of the work and then sitting around waiting for someone
else to do the other 95% so you can sue them.

#3Peter Eisentraut
peter_e@gmx.net
In reply to: Michael Paesold (#1)
Re: Rollback on Error

Michael Paesold wrote:

I though the postgres behaviour of rolling back the whole transaction
was standard?

No.

If that is not the case, I don't understand why core
seems to be against a mode (GUC), where an implicit savepoint is
generated before each statement so that "rollback of the last
statement" would be possible.

Because it's dangerous in noninteractive mode. When you send a
transaction to the server, you don't want some statements be left out.

--
Peter Eisentraut
http://developer.postgresql.org/~petere/

#4Michael Paesold
mpaesold@gmx.at
In reply to: Michael Paesold (#1)
Re: Rollback on Error

Peter Eisentraut wrote:

Michael Paesold wrote:

I though the postgres behaviour of rolling back the whole transaction
was standard?

No.

Thanks for putting that straigt.

If that is not the case, I don't understand why core
seems to be against a mode (GUC), where an implicit savepoint is
generated before each statement so that "rollback of the last
statement" would be possible.

Because it's dangerous in noninteractive mode. When you send a
transaction to the server, you don't want some statements be left out.

I understand it's dangerous, it would possibly introduce data-inconsistency
in all applications that don't check for errors after each statement...

Still it would be very useful in interactive psql...
of course, it's bad in scripted mode. Perhaps it could be implemented inside
psql? Would such a patch be accepted?

Regards,
Michael Paesold

#5Csaba Nagy
nagy@ecircle-ag.com
In reply to: Peter Eisentraut (#3)
Re: Rollback on Error

On Tue, 2004-09-14 at 11:56, Peter Eisentraut wrote:

Michael Paesold wrote:

I though the postgres behaviour of rolling back the whole transaction
was standard?

No.

If that is not the case, I don't understand why core
seems to be against a mode (GUC), where an implicit savepoint is
generated before each statement so that "rollback of the last
statement" would be possible.

Because it's dangerous in noninteractive mode. When you send a
transaction to the server, you don't want some statements be left out.

But it does not mean the server will commit the transaction for you...
it will throw an exception on the failed query, and it's up to you to
continue the transaction, commit it right away, or to roll it back. Old
code will probably just work fine if it will roll back on any received
error, which it had to do anyway even with the current behavior.
So it is just giving more choice to the developer instead of deciding
for him that the transaction is failed... and yes, there are valid
scenarios where it is quicker to execute a query and based on the fact
that it fails or not do different things, without the need of redoing
the whole transaction executed before.
It's another story that this feature should not be turned on by default
if it has some performance overhead (which I suppose it has, as setting
a savepoint is probably not free).

Just my 2c,
Csaba.

#6Paul Thomas
paul@tmsl.demon.co.uk
In reply to: Michael Paesold (#1)
Re: Rollback on Error

On 14/09/2004 10:39 Michael Paesold wrote:

I just read this in the MySQL manual:
(http://dev.mysql.com/doc/mysql/en/InnoDB_Error_handling.html)

"Error handling in InnoDB is not always the same as specified in the SQL
standard. According to the standard, any error during an SQL statement
should cause the rollback of that statement. InnoDB sometimes rolls back
only part of the statement, or the whole transaction. The following items
describe how InnoDB performs error handling:"

Ignore InnoDB, but read "According to the standard, any error during an
SQL
statement should cause the rollback of that statement"...

I though the postgres behaviour of rolling back the whole transaction was
standard? If that is not the case, I don't understand why core seems to be
against a mode (GUC), where an implicit savepoint is generated before each
statement so that "rollback of the last statement" would be possible.

Is MySQL wrong on the standard, or has postgresql core attitude changed to
support the standard where possible - at least if the user wants?

Well, you could go off and read the standards of course but I'll save you
the time and tell you that PostrgreSQL does it correctly.

But seriously, are you so naive that you would believe _anything_ MySQL AB 
say? 
-- 
Paul Thomas
+------------------------------+-------------------------------------------+
| Thomas Micro Systems Limited | Software Solutions for Business           |
| Computer Consultants         | http://www.thomas-micro-systems-ltd.co.uk |
+------------------------------+-------------------------------------------+
#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Michael Paesold (#1)
Re: Rollback on Error

"Michael Paesold" <mpaesold@gmx.at> writes:

I though the postgres behaviour of rolling back the whole transaction was
standard?

Not really.

If that is not the case, I don't understand why core seems to be
against a mode (GUC), where an implicit savepoint is generated before each
statement so that "rollback of the last statement" would be possible.

Because we learned our lesson with the ill-fated autocommit GUC
variable. You can't have fundamental transactional semantics depending
on the phase of the moon, but from the point of view of application
code, anything that can be flipped as easily as a GUC variable is an
unknown.

If you've been following recent -hackers discussions you will also
realize that a forced savepoint for every statement is untenable
from a performance perspective anyway.

regards, tom lane

#8Michael Paesold
mpaesold@gmx.at
In reply to: Michael Paesold (#1)
Re: Rollback on Error

Tom Lane wrote:

"Michael Paesold" <mpaesold@gmx.at> writes:

...

If that is not the case, I don't understand why core seems to be
against a mode (GUC), where an implicit savepoint is generated before

each

statement so that "rollback of the last statement" would be possible.

Because we learned our lesson with the ill-fated autocommit GUC
variable. You can't have fundamental transactional semantics depending
on the phase of the moon, but from the point of view of application
code, anything that can be flipped as easily as a GUC variable is an
unknown.

If you've been following recent -hackers discussions you will also
realize that a forced savepoint for every statement is untenable
from a performance perspective anyway.

Well, I read the threads "Re: beta1 & beta2 & Windows & heavy load" and
"Cleaning up recovery from subtransaction start failure".

I see, that savepoint/subxacts are rather expensive, at least wrt
shared-memory (lock on xid).

On the other hand, the scenario of a psql option (read: I have given up the
idea of a backend implementation) to rollback only last statement on error
is quite different.

In the mentioned thread, several thousand rows where deleted, each firing a
trigger creating a subxact. But in usual interactive administrative
sessions, you would perhaps have 5 to 20 statements in a usual transaction.
At least according to my personal experience.

Am I right in asserting that psql could recognize if a session is
interactive or scripted (\i, psql < script.sql)? And it already has user
settable options, (\set, e.g. autocommit)...

So a proper patch implementing a corresponding mode in psql that wraps each
statement (of a multi statement transaction) in a subxact, if the user
enables it, would be accepted or even called welcome?

Just thinking about...

BEGIN;
SET TRANSACTION ISOLATION SERIALIZABLE;
SELECT * FROM a WHERE id = 5;
UPDATE a SET .... WHERE id = 5;

-- check everything is ok...
SELEC * FROM a WHERE id = 5;

--> ERROR: syntax error at or near "SELEC" at character 1

-- arghh... typo do it all over again...

Best Regards,
Michael Paesold

#9Tom Lane
tgl@sss.pgh.pa.us
In reply to: Michael Paesold (#8)
Re: Rollback on Error

"Michael Paesold" <mpaesold@gmx.at> writes:

Tom Lane wrote:

"Michael Paesold" <mpaesold@gmx.at> writes:

If that is not the case, I don't understand why core seems to be
against a mode (GUC), where an implicit savepoint is generated before
each statement so that "rollback of the last statement" would be possible.

Because we learned our lesson with the ill-fated autocommit GUC
variable. You can't have fundamental transactional semantics depending
on the phase of the moon, but from the point of view of application
code, anything that can be flipped as easily as a GUC variable is an
unknown.

On the other hand, the scenario of a psql option (read: I have given up the
idea of a backend implementation) to rollback only last statement on error
is quite different.

Sure (and we already have one for autocommit). But I thought you were
asking about a backend implementation.

regards, tom lane

#10Michael Paesold
mpaesold@gmx.at
In reply to: Michael Paesold (#1)
1 attachment(s)
Re: Rollback on Error

Tom Lane wrote:

"Michael Paesold" <mpaesold@gmx.at> writes:

On the other hand, the scenario of a psql option (read: I have
given up the idea of a backend implementation) to rollback only
last statement on error is quite different.

Sure (and we already have one for autocommit). But I thought you were
asking about a backend implementation.

I have implemented what I have suggested for psql. I have attached a first
patch for review here, because I have a few questions. Also I want to make
sure the whole thing is reasonable.

I have named the option "IMPLICIT_SAVEPOINTS", because that's what it is. If
someone has a better name that would describe the purpose of the feature, I
am happy to change it.

The feature is activated, if
* \set IMPLICIT_SAVEPOINTS 'on'
* connection is in "idle in transaction" state
* psql session is interactive

The code executes an implicit "SAVEPOINT pg_internal_psql" in
common.c/SendQuery to which it will try to rollback to, if the executed
query fails.

Open questions:
* Should psql print a notice in the case of that rollback?
Something like "Rollback of last statement successful."?

* What is currently missing, is a detection of \i ... obviously this feature
should not be used for each query in \i. Perhaps only for the whole \i
command?
So what should I do to detect \i?
Add an extra argument to MainLoop, SendQuery and process_file()? (many
changes)
Add a global variable in common.c/h (e.g. bool
deactivate_implicit_savepoints) that can be used in process_file to
temporarily deactivate the code path?
(more local changes, but rather a hack imho)

Please have a look at the patch and comment.

Best Regards,
Michael Paesold

Attachments:

implicit_savepoints.patchapplication/octet-stream; name=implicit_savepoints.patchDownload
*** ./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;
  
#11Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Michael Paesold (#10)
Re: Rollback on Error

I assume this is to be saved for 8.1.

This has been saved for the 8.1 release:

http:/momjian.postgresql.org/cgi-bin/pgpatches2

---------------------------------------------------------------------------

Michael Paesold wrote:

Tom Lane wrote:

"Michael Paesold" <mpaesold@gmx.at> writes:

On the other hand, the scenario of a psql option (read: I have
given up the idea of a backend implementation) to rollback only
last statement on error is quite different.

Sure (and we already have one for autocommit). But I thought you were
asking about a backend implementation.

I have implemented what I have suggested for psql. I have attached a first
patch for review here, because I have a few questions. Also I want to make
sure the whole thing is reasonable.

I have named the option "IMPLICIT_SAVEPOINTS", because that's what it is. If
someone has a better name that would describe the purpose of the feature, I
am happy to change it.

The feature is activated, if
* \set IMPLICIT_SAVEPOINTS 'on'
* connection is in "idle in transaction" state
* psql session is interactive

The code executes an implicit "SAVEPOINT pg_internal_psql" in
common.c/SendQuery to which it will try to rollback to, if the executed
query fails.

Open questions:
* Should psql print a notice in the case of that rollback?
Something like "Rollback of last statement successful."?

* What is currently missing, is a detection of \i ... obviously this feature
should not be used for each query in \i. Perhaps only for the whole \i
command?
So what should I do to detect \i?
Add an extra argument to MainLoop, SendQuery and process_file()? (many
changes)
Add a global variable in common.c/h (e.g. bool
deactivate_implicit_savepoints) that can be used in process_file to
temporarily deactivate the code path?
(more local changes, but rather a hack imho)

Please have a look at the patch and comment.

Best Regards,
Michael Paesold

[ Attachment, skipping... ]

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#12Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#11)
Re: Rollback on Error

Bruce Momjian <pgman@candle.pha.pa.us> writes:

I assume this is to be saved for 8.1.

This has been saved for the 8.1 release:
http:/momjian.postgresql.org/cgi-bin/pgpatches2

It is not remotely ready to apply yet, so please do not put it in the
queue.

regards, tom lane

#13Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Tom Lane (#12)
Re: Rollback on Error

Tom Lane wrote:

Bruce Momjian <pgman@candle.pha.pa.us> writes:

I assume this is to be saved for 8.1.

This has been saved for the 8.1 release:
http:/momjian.postgresql.org/cgi-bin/pgpatches2

It is not remotely ready to apply yet, so please do not put it in the
queue.

That queue isn't for 8.1 ready-to-apply stuff. It just says "saved".

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#14Michael Paesold
mpaesold@gmx.at
In reply to: Bruce Momjian (#13)
Re: Rollback on Error

Bruce Momjian <pgman@candle.pha.pa.us> wrote:

Bruce Momjian <pgman@candle.pha.pa.us> writes:

I assume this is to be saved for 8.1.

I assumed that to, so I did not want to disturb any more now.

This has been saved for the 8.1 release:
http:/momjian.postgresql.org/cgi-bin/pgpatches2

Tom Lane wrote:

It is not remotely ready to apply yet, so please do not put it in the
queue.

I hope you will be willing to comment on the issues when times come. I am
not really satisfied myself, but without further discussion I did not want
to continue to work on it. Anyway, I understand this is not the right time
now (8.0 beta).

Best Regards,
Michael Paesold

#15Alvaro Herrera Munoz
alvherre@dcc.uchile.cl
In reply to: Michael Paesold (#14)
Re: [HACKERS] Rollback on Error

On Fri, Oct 08, 2004 at 08:40:56PM +0200, Michael Paesold wrote:

I hope you will be willing to comment on the issues when times come. I am
not really satisfied myself, but without further discussion I did not want
to continue to work on it. Anyway, I understand this is not the right time
now (8.0 beta).

I think it would be wise to create a function to discover what savepoints
are available, and from that know when to release an automatically-
established savepoint.

Of course, the function would only work when the backend is not in abort
state, but I think that's a reasonable restriction.

--
Alvaro Herrera (<alvherre[@]dcc.uchile.cl>)
"Ni aun el genio muy grande llegar�a muy lejos
si tuviera que sacarlo todo de su propio interior" (Goethe)

#16Michael Paesold
mpaesold@gmx.at
In reply to: Bruce Momjian (#13)
Re: Rollback on Error

Alvaro Herrera Munoz wrote:

I think it would be wise to create a function to discover what savepoints
are available, and from that know when to release an automatically-
established savepoint.

Of course, the function would only work when the backend is not in abort
state, but I think that's a reasonable restriction.

Ok, that would need a set returning function in the backend, right? Could
you help me write it when time comes? (I don't feel backend hacking savvy.)

What about the resources consumed by savepoints
a) that can be freed by a RELEASE (trx state stack, what else?)
versus
b) that cannot be freed (xid locks, anything else?)

Is it worth the effort of extra work (not programmer's but runtime ;-)?

Bruce,
in Revision 1.1355 of the TODO you removed a line from the todo list that
said:
-Use nested transactions to prevent syntax errors from aborting a
transaction

I tought this is what I made a patch for, so shouldn't it be back for 8.1?
Or at least something similar?

Best Regards,
Michael Paesold

#17Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Michael Paesold (#16)
Re: Rollback on Error

Michael Paesold wrote:

Alvaro Herrera Munoz wrote:

I think it would be wise to create a function to discover what savepoints
are available, and from that know when to release an automatically-
established savepoint.

Of course, the function would only work when the backend is not in abort
state, but I think that's a reasonable restriction.

Ok, that would need a set returning function in the backend, right? Could
you help me write it when time comes? (I don't feel backend hacking savvy.)

What about the resources consumed by savepoints
a) that can be freed by a RELEASE (trx state stack, what else?)
versus
b) that cannot be freed (xid locks, anything else?)

Is it worth the effort of extra work (not programmer's but runtime ;-)?

Bruce,
in Revision 1.1355 of the TODO you removed a line from the todo list that
said:
-Use nested transactions to prevent syntax errors from aborting a
transaction

I tought this is what I made a patch for, so shouldn't it be back for 8.1?
Or at least something similar?

The item was adjusted to be more specific:

* Add an option to automatically use savepoints for each statement in a
multi-statement transaction.

When enabled, this would allow errors in multi-statement transactions
to be automatically ignored.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#18Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Michael Paesold (#10)
Re: Rollback on Error

This has already been implemented in CVS as a psql \set variable:

ON_ERROR_ROLLBACK = 'interactive'

and will appear in 8.1.

---------------------------------------------------------------------------

Michael Paesold wrote:

Tom Lane wrote:

"Michael Paesold" <mpaesold@gmx.at> writes:

On the other hand, the scenario of a psql option (read: I have
given up the idea of a backend implementation) to rollback only
last statement on error is quite different.

Sure (and we already have one for autocommit). But I thought you were
asking about a backend implementation.

I have implemented what I have suggested for psql. I have attached a first
patch for review here, because I have a few questions. Also I want to make
sure the whole thing is reasonable.

I have named the option "IMPLICIT_SAVEPOINTS", because that's what it is. If
someone has a better name that would describe the purpose of the feature, I
am happy to change it.

The feature is activated, if
* \set IMPLICIT_SAVEPOINTS 'on'
* connection is in "idle in transaction" state
* psql session is interactive

The code executes an implicit "SAVEPOINT pg_internal_psql" in
common.c/SendQuery to which it will try to rollback to, if the executed
query fails.

Open questions:
* Should psql print a notice in the case of that rollback?
Something like "Rollback of last statement successful."?

* What is currently missing, is a detection of \i ... obviously this feature
should not be used for each query in \i. Perhaps only for the whole \i
command?
So what should I do to detect \i?
Add an extra argument to MainLoop, SendQuery and process_file()? (many
changes)
Add a global variable in common.c/h (e.g. bool
deactivate_implicit_savepoints) that can be used in process_file to
temporarily deactivate the code path?
(more local changes, but rather a hack imho)

Please have a look at the patch and comment.

Best Regards,
Michael Paesold

[ Attachment, skipping... ]

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073