aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/tablecmds.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/commands/tablecmds.c')
-rw-r--r--src/backend/commands/tablecmds.c38
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);
- }
- }
}
/*