postgresql/src/backend/utils
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
..
adt Add new buffers_backend_fsync field to pg_stat_bgwriter. 2010-11-15 12:42:59 -05:00
cache Make TRUNCATE ... RESTART IDENTITY restart sequences transactionally. 2010-11-17 16:42:18 -05:00
error Use appendStringInfoString() where appropriate in elog.c. 2010-11-04 15:28:35 -04:00
fmgr Fix comparisons of pointers with zero to compare with NULL instead. 2010-10-29 15:51:52 -04:00
hash Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
init Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
mb Improved parallel make support 2010-11-12 22:15:16 +02:00
misc Make get_stack_depth_rlimit() handle RLIM_INFINITY more sanely. 2010-11-06 16:50:18 -04:00
mmgr Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
resowner Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
sort Eliminate some repetitive coding in tuplesort.c. 2010-10-07 20:32:21 -04:00
time 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
Gen_dummy_probes.sed Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
Gen_fmgrtab.pl Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
Makefile Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
probes.d Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00