postgresql/src
Tom Lane 511e902b51 Make TRUNCATE ... RESTART IDENTITY restart sequences transactionally.
In the previous coding, we simply issued ALTER SEQUENCE RESTART commands,
which do not roll back on error.  This meant that an error between
truncating and committing left the sequences out of sync with the table
contents, with potentially bad consequences as were noted in a Warning on
the TRUNCATE man page.

To fix, create a new storage file (relfilenode) for a sequence that is to
be reset due to RESTART IDENTITY.  If the transaction aborts, we'll
automatically revert to the old storage file.  This acts just like a
rewriting ALTER TABLE operation.  A penalty is that we have to take
exclusive lock on the sequence, but since we've already got exclusive lock
on its owning table, that seems unlikely to be much of a problem.

The interaction of this with usual nontransactional behaviors of sequence
operations is a bit weird, but it's hard to see what would be completely
consistent.  Our choice is to discard cached-but-unissued sequence values
both when the RESTART is executed, and at rollback if any; but to not touch
the currval() state either time.

In passing, move the sequence reset operations to happen before not after
any AFTER TRUNCATE triggers are fired.  The previous ordering was not
logically sensible, but was forced by the need to minimize inconsistency
if the triggers caused an error.  Transactional rollback is a much better
solution to that.

Patch by Steve Singer, rather heavily adjusted by me.
2010-11-17 16:42:18 -05:00
..
backend Make TRUNCATE ... RESTART IDENTITY restart sequences transactionally. 2010-11-17 16:42:18 -05:00
bin Cleanup various comparisons with the constant "true". 2010-11-14 21:03:48 -05:00
include Make TRUNCATE ... RESTART IDENTITY restart sequences transactionally. 2010-11-17 16:42:18 -05:00
interfaces Additional fixes for parallel make 2010-11-17 08:08:41 +02:00
makefiles Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
pl Fix aboriginal mistake in plpython's set-returning-function support. 2010-11-15 14:26:55 -05:00
port Fix comparisons of pointers with zero to compare with NULL instead. 2010-10-29 15:51:52 -04:00
template Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
test Make TRUNCATE ... RESTART IDENTITY restart sequences transactionally. 2010-11-17 16:42:18 -05:00
timezone Improved parallel make support 2010-11-12 22:15:16 +02:00
tools Attempt to fix MSVC builds broken by parallel make changes. 2010-11-12 22:55:15 -05:00
tutorial Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
.gitignore Convert cvsignore to gitignore, and add .gitignore for build targets. 2010-09-22 12:57:04 +02:00
DEVELOPERS Replace a couple of references to files that no longer exist in the source 2009-05-04 08:08:47 +00:00
Makefile Additional fixes for parallel make 2010-11-17 08:08:41 +02:00
Makefile.global.in Adjust comments about what's needed to avoid make 3.80 bug. 2010-11-15 01:00:48 -05:00
Makefile.shlib Improved parallel make support 2010-11-12 22:15:16 +02:00
bcc32.mak Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
nls-global.mk Fix makefile logic to not break the build when xgettext is missing 2010-10-14 23:16:46 +03:00
win32.mak Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00