diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2010-11-17 16:42:18 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2010-11-17 16:42:18 -0500 |
commit | 511e902b51c2a1c0d012426ceb6486b1202120f3 (patch) | |
tree | 56c99ffc969908dbfaf8ec34b844a13c46a61945 /src/backend/commands/tablecmds.c | |
parent | cfad144f894b306fc300f5d03ea52a32d4624db0 (diff) | |
download | postgresql-511e902b51c2a1c0d012426ceb6486b1202120f3.tar.gz postgresql-511e902b51c2a1c0d012426ceb6486b1202120f3.zip |
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.
Diffstat (limited to 'src/backend/commands/tablecmds.c')
-rw-r--r-- | src/backend/commands/tablecmds.c | 38 |
1 files changed, 14 insertions, 24 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 6ec8a854100..b22bcf0d663 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -916,10 +916,9 @@ ExecuteTruncate(TruncateStmt *stmt) /* * If we are asked to restart sequences, find all the sequences, lock them - * (we only need AccessShareLock because that's all that ALTER SEQUENCE - * takes), and check permissions. We want to do this early since it's - * pointless to do all the truncation work only to fail on sequence - * permissions. + * (we need AccessExclusiveLock for ResetSequence), and check permissions. + * We want to do this early since it's pointless to do all the truncation + * work only to fail on sequence permissions. */ if (stmt->restart_seqs) { @@ -934,7 +933,7 @@ ExecuteTruncate(TruncateStmt *stmt) Oid seq_relid = lfirst_oid(seqcell); Relation seq_rel; - seq_rel = relation_open(seq_relid, AccessShareLock); + seq_rel = relation_open(seq_relid, AccessExclusiveLock); /* This check must match AlterSequence! */ if (!pg_class_ownercheck(seq_relid, GetUserId())) @@ -1044,6 +1043,16 @@ ExecuteTruncate(TruncateStmt *stmt) } /* + * Restart owned sequences if we were asked to. + */ + foreach(cell, seq_relids) + { + Oid seq_relid = lfirst_oid(cell); + + ResetSequence(seq_relid); + } + + /* * Process all AFTER STATEMENT TRUNCATE triggers. */ resultRelInfo = resultRelInfos; @@ -1067,25 +1076,6 @@ ExecuteTruncate(TruncateStmt *stmt) heap_close(rel, NoLock); } - - /* - * Lastly, restart any owned sequences if we were asked to. This is done - * last because it's nontransactional: restarts will not roll back if we - * abort later. Hence it's important to postpone them as long as - * possible. (This is also a big reason why we locked and - * permission-checked the sequences beforehand.) - */ - if (stmt->restart_seqs) - { - List *options = list_make1(makeDefElem("restart", NULL)); - - foreach(cell, seq_relids) - { - Oid seq_relid = lfirst_oid(cell); - - AlterSequenceInternal(seq_relid, options); - } - } } /* |