2002-08-22 06:51:06 +02:00
|
|
|
-- Test basic TRUNCATE functionality.
|
|
|
|
CREATE TABLE truncate_a (col1 integer primary key);
|
|
|
|
INSERT INTO truncate_a VALUES (1);
|
|
|
|
INSERT INTO truncate_a VALUES (2);
|
|
|
|
SELECT * FROM truncate_a;
|
|
|
|
col1
|
|
|
|
------
|
|
|
|
1
|
|
|
|
2
|
|
|
|
(2 rows)
|
|
|
|
|
2002-11-23 05:05:52 +01:00
|
|
|
-- Roll truncate back
|
|
|
|
BEGIN;
|
2002-08-22 06:51:06 +02:00
|
|
|
TRUNCATE truncate_a;
|
2002-11-23 05:05:52 +01:00
|
|
|
ROLLBACK;
|
|
|
|
SELECT * FROM truncate_a;
|
|
|
|
col1
|
|
|
|
------
|
|
|
|
1
|
|
|
|
2
|
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
-- Commit the truncate this time
|
|
|
|
BEGIN;
|
|
|
|
TRUNCATE truncate_a;
|
|
|
|
COMMIT;
|
2002-08-22 06:51:06 +02:00
|
|
|
SELECT * FROM truncate_a;
|
|
|
|
col1
|
|
|
|
------
|
|
|
|
(0 rows)
|
|
|
|
|
2005-01-27 04:19:37 +01:00
|
|
|
-- Test foreign-key checks
|
|
|
|
CREATE TABLE trunc_b (a int REFERENCES truncate_a);
|
|
|
|
CREATE TABLE trunc_c (a serial PRIMARY KEY);
|
|
|
|
CREATE TABLE trunc_d (a int REFERENCES trunc_c);
|
|
|
|
CREATE TABLE trunc_e (a int REFERENCES truncate_a, b int REFERENCES trunc_c);
|
|
|
|
TRUNCATE TABLE truncate_a; -- fail
|
|
|
|
ERROR: cannot truncate a table referenced in a foreign key constraint
|
2006-06-29 18:07:29 +02:00
|
|
|
DETAIL: Table "trunc_b" references "truncate_a".
|
2006-03-03 04:30:54 +01:00
|
|
|
HINT: Truncate table "trunc_b" at the same time, or use TRUNCATE ... CASCADE.
|
2005-01-27 04:19:37 +01:00
|
|
|
TRUNCATE TABLE truncate_a,trunc_b; -- fail
|
|
|
|
ERROR: cannot truncate a table referenced in a foreign key constraint
|
2006-06-29 18:07:29 +02:00
|
|
|
DETAIL: Table "trunc_e" references "truncate_a".
|
2006-03-03 04:30:54 +01:00
|
|
|
HINT: Truncate table "trunc_e" at the same time, or use TRUNCATE ... CASCADE.
|
2005-01-27 04:19:37 +01:00
|
|
|
TRUNCATE TABLE truncate_a,trunc_b,trunc_e; -- ok
|
|
|
|
TRUNCATE TABLE truncate_a,trunc_e; -- fail
|
|
|
|
ERROR: cannot truncate a table referenced in a foreign key constraint
|
2006-06-29 18:07:29 +02:00
|
|
|
DETAIL: Table "trunc_b" references "truncate_a".
|
2006-03-03 04:30:54 +01:00
|
|
|
HINT: Truncate table "trunc_b" at the same time, or use TRUNCATE ... CASCADE.
|
2005-01-27 04:19:37 +01:00
|
|
|
TRUNCATE TABLE trunc_c; -- fail
|
|
|
|
ERROR: cannot truncate a table referenced in a foreign key constraint
|
2006-06-29 18:07:29 +02:00
|
|
|
DETAIL: Table "trunc_d" references "trunc_c".
|
2006-03-03 04:30:54 +01:00
|
|
|
HINT: Truncate table "trunc_d" at the same time, or use TRUNCATE ... CASCADE.
|
2005-01-27 04:19:37 +01:00
|
|
|
TRUNCATE TABLE trunc_c,trunc_d; -- fail
|
|
|
|
ERROR: cannot truncate a table referenced in a foreign key constraint
|
2006-06-29 18:07:29 +02:00
|
|
|
DETAIL: Table "trunc_e" references "trunc_c".
|
2006-03-03 04:30:54 +01:00
|
|
|
HINT: Truncate table "trunc_e" at the same time, or use TRUNCATE ... CASCADE.
|
2005-01-27 04:19:37 +01:00
|
|
|
TRUNCATE TABLE trunc_c,trunc_d,trunc_e; -- ok
|
|
|
|
TRUNCATE TABLE trunc_c,trunc_d,trunc_e,truncate_a; -- fail
|
|
|
|
ERROR: cannot truncate a table referenced in a foreign key constraint
|
2006-06-29 18:07:29 +02:00
|
|
|
DETAIL: Table "trunc_b" references "truncate_a".
|
2006-03-03 04:30:54 +01:00
|
|
|
HINT: Truncate table "trunc_b" at the same time, or use TRUNCATE ... CASCADE.
|
2005-01-27 04:19:37 +01:00
|
|
|
TRUNCATE TABLE trunc_c,trunc_d,trunc_e,truncate_a,trunc_b; -- ok
|
2006-03-03 04:30:54 +01:00
|
|
|
TRUNCATE TABLE truncate_a RESTRICT; -- fail
|
|
|
|
ERROR: cannot truncate a table referenced in a foreign key constraint
|
2006-06-29 18:07:29 +02:00
|
|
|
DETAIL: Table "trunc_b" references "truncate_a".
|
2006-03-03 04:30:54 +01:00
|
|
|
HINT: Truncate table "trunc_b" at the same time, or use TRUNCATE ... CASCADE.
|
|
|
|
TRUNCATE TABLE truncate_a CASCADE; -- ok
|
|
|
|
NOTICE: truncate cascades to table "trunc_b"
|
|
|
|
NOTICE: truncate cascades to table "trunc_e"
|
2005-01-27 04:19:37 +01:00
|
|
|
-- circular references
|
|
|
|
ALTER TABLE truncate_a ADD FOREIGN KEY (col1) REFERENCES trunc_c;
|
|
|
|
-- Add some data to verify that truncating actually works ...
|
|
|
|
INSERT INTO trunc_c VALUES (1);
|
2002-08-22 06:51:06 +02:00
|
|
|
INSERT INTO truncate_a VALUES (1);
|
2005-01-27 04:19:37 +01:00
|
|
|
INSERT INTO trunc_b VALUES (1);
|
|
|
|
INSERT INTO trunc_d VALUES (1);
|
|
|
|
INSERT INTO trunc_e VALUES (1,1);
|
|
|
|
TRUNCATE TABLE trunc_c;
|
2003-07-20 23:56:35 +02:00
|
|
|
ERROR: cannot truncate a table referenced in a foreign key constraint
|
2006-06-29 18:07:29 +02:00
|
|
|
DETAIL: Table "truncate_a" references "trunc_c".
|
|
|
|
HINT: Truncate table "truncate_a" at the same time, or use TRUNCATE ... CASCADE.
|
|
|
|
TRUNCATE TABLE trunc_c,truncate_a;
|
|
|
|
ERROR: cannot truncate a table referenced in a foreign key constraint
|
|
|
|
DETAIL: Table "trunc_d" references "trunc_c".
|
2006-03-03 04:30:54 +01:00
|
|
|
HINT: Truncate table "trunc_d" at the same time, or use TRUNCATE ... CASCADE.
|
2006-06-29 18:07:29 +02:00
|
|
|
TRUNCATE TABLE trunc_c,truncate_a,trunc_d;
|
2005-01-27 04:19:37 +01:00
|
|
|
ERROR: cannot truncate a table referenced in a foreign key constraint
|
2006-06-29 18:07:29 +02:00
|
|
|
DETAIL: Table "trunc_e" references "trunc_c".
|
2006-03-03 04:30:54 +01:00
|
|
|
HINT: Truncate table "trunc_e" at the same time, or use TRUNCATE ... CASCADE.
|
2006-06-29 18:07:29 +02:00
|
|
|
TRUNCATE TABLE trunc_c,truncate_a,trunc_d,trunc_e;
|
2005-01-27 04:19:37 +01:00
|
|
|
ERROR: cannot truncate a table referenced in a foreign key constraint
|
2006-06-29 18:07:29 +02:00
|
|
|
DETAIL: Table "trunc_b" references "truncate_a".
|
2006-03-03 04:30:54 +01:00
|
|
|
HINT: Truncate table "trunc_b" at the same time, or use TRUNCATE ... CASCADE.
|
2006-06-29 18:07:29 +02:00
|
|
|
TRUNCATE TABLE trunc_c,truncate_a,trunc_d,trunc_e,trunc_b;
|
2005-01-27 04:19:37 +01:00
|
|
|
-- Verify that truncating did actually work
|
|
|
|
SELECT * FROM truncate_a
|
|
|
|
UNION ALL
|
|
|
|
SELECT * FROM trunc_c
|
|
|
|
UNION ALL
|
|
|
|
SELECT * FROM trunc_b
|
|
|
|
UNION ALL
|
|
|
|
SELECT * FROM trunc_d;
|
2002-08-22 06:51:06 +02:00
|
|
|
col1
|
|
|
|
------
|
2005-01-27 04:19:37 +01:00
|
|
|
(0 rows)
|
|
|
|
|
|
|
|
SELECT * FROM trunc_e;
|
|
|
|
a | b
|
|
|
|
---+---
|
|
|
|
(0 rows)
|
2002-08-22 06:51:06 +02:00
|
|
|
|
2006-03-03 04:30:54 +01:00
|
|
|
-- Add data again to test TRUNCATE ... CASCADE
|
|
|
|
INSERT INTO trunc_c VALUES (1);
|
|
|
|
INSERT INTO truncate_a VALUES (1);
|
|
|
|
INSERT INTO trunc_b VALUES (1);
|
|
|
|
INSERT INTO trunc_d VALUES (1);
|
|
|
|
INSERT INTO trunc_e VALUES (1,1);
|
|
|
|
TRUNCATE TABLE trunc_c CASCADE; -- ok
|
2006-06-29 18:07:29 +02:00
|
|
|
NOTICE: truncate cascades to table "truncate_a"
|
2006-03-03 04:30:54 +01:00
|
|
|
NOTICE: truncate cascades to table "trunc_d"
|
|
|
|
NOTICE: truncate cascades to table "trunc_e"
|
|
|
|
NOTICE: truncate cascades to table "trunc_b"
|
|
|
|
SELECT * FROM truncate_a
|
|
|
|
UNION ALL
|
|
|
|
SELECT * FROM trunc_c
|
|
|
|
UNION ALL
|
|
|
|
SELECT * FROM trunc_b
|
|
|
|
UNION ALL
|
|
|
|
SELECT * FROM trunc_d;
|
|
|
|
col1
|
|
|
|
------
|
|
|
|
(0 rows)
|
|
|
|
|
|
|
|
SELECT * FROM trunc_e;
|
|
|
|
a | b
|
|
|
|
---+---
|
|
|
|
(0 rows)
|
|
|
|
|
2005-01-27 04:19:37 +01:00
|
|
|
DROP TABLE truncate_a,trunc_c,trunc_b,trunc_d,trunc_e CASCADE;
|
2009-01-12 09:54:27 +01:00
|
|
|
-- Test TRUNCATE with inheritance
|
|
|
|
CREATE TABLE trunc_f (col1 integer primary key);
|
|
|
|
INSERT INTO trunc_f VALUES (1);
|
|
|
|
INSERT INTO trunc_f VALUES (2);
|
|
|
|
CREATE TABLE trunc_fa (col2a text) INHERITS (trunc_f);
|
|
|
|
INSERT INTO trunc_fa VALUES (3, 'three');
|
|
|
|
CREATE TABLE trunc_fb (col2b int) INHERITS (trunc_f);
|
|
|
|
INSERT INTO trunc_fb VALUES (4, 444);
|
|
|
|
CREATE TABLE trunc_faa (col3 text) INHERITS (trunc_fa);
|
|
|
|
INSERT INTO trunc_faa VALUES (5, 'five', 'FIVE');
|
|
|
|
BEGIN;
|
|
|
|
SELECT * FROM trunc_f;
|
|
|
|
col1
|
|
|
|
------
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
(5 rows)
|
|
|
|
|
|
|
|
TRUNCATE trunc_f;
|
|
|
|
SELECT * FROM trunc_f;
|
|
|
|
col1
|
|
|
|
------
|
|
|
|
(0 rows)
|
|
|
|
|
|
|
|
ROLLBACK;
|
|
|
|
BEGIN;
|
|
|
|
SELECT * FROM trunc_f;
|
|
|
|
col1
|
|
|
|
------
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
(5 rows)
|
|
|
|
|
|
|
|
TRUNCATE ONLY trunc_f;
|
|
|
|
SELECT * FROM trunc_f;
|
|
|
|
col1
|
|
|
|
------
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
(3 rows)
|
|
|
|
|
|
|
|
ROLLBACK;
|
|
|
|
BEGIN;
|
|
|
|
SELECT * FROM trunc_f;
|
|
|
|
col1
|
|
|
|
------
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
(5 rows)
|
|
|
|
|
|
|
|
SELECT * FROM trunc_fa;
|
|
|
|
col1 | col2a
|
|
|
|
------+-------
|
|
|
|
3 | three
|
|
|
|
5 | five
|
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
SELECT * FROM trunc_faa;
|
|
|
|
col1 | col2a | col3
|
|
|
|
------+-------+------
|
|
|
|
5 | five | FIVE
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
TRUNCATE ONLY trunc_fb, ONLY trunc_fa;
|
|
|
|
SELECT * FROM trunc_f;
|
|
|
|
col1
|
|
|
|
------
|
|
|
|
1
|
|
|
|
2
|
|
|
|
5
|
|
|
|
(3 rows)
|
|
|
|
|
|
|
|
SELECT * FROM trunc_fa;
|
|
|
|
col1 | col2a
|
|
|
|
------+-------
|
|
|
|
5 | five
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT * FROM trunc_faa;
|
|
|
|
col1 | col2a | col3
|
|
|
|
------+-------+------
|
|
|
|
5 | five | FIVE
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
ROLLBACK;
|
|
|
|
BEGIN;
|
|
|
|
SELECT * FROM trunc_f;
|
|
|
|
col1
|
|
|
|
------
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
(5 rows)
|
|
|
|
|
|
|
|
SELECT * FROM trunc_fa;
|
|
|
|
col1 | col2a
|
|
|
|
------+-------
|
|
|
|
3 | three
|
|
|
|
5 | five
|
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
SELECT * FROM trunc_faa;
|
|
|
|
col1 | col2a | col3
|
|
|
|
------+-------+------
|
|
|
|
5 | five | FIVE
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
TRUNCATE ONLY trunc_fb, trunc_fa;
|
|
|
|
SELECT * FROM trunc_f;
|
|
|
|
col1
|
|
|
|
------
|
|
|
|
1
|
|
|
|
2
|
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
SELECT * FROM trunc_fa;
|
|
|
|
col1 | col2a
|
|
|
|
------+-------
|
|
|
|
(0 rows)
|
|
|
|
|
|
|
|
SELECT * FROM trunc_faa;
|
|
|
|
col1 | col2a | col3
|
|
|
|
------+-------+------
|
|
|
|
(0 rows)
|
|
|
|
|
|
|
|
ROLLBACK;
|
|
|
|
DROP TABLE trunc_f CASCADE;
|
|
|
|
NOTICE: drop cascades to 3 other objects
|
|
|
|
DETAIL: drop cascades to table trunc_fa
|
|
|
|
drop cascades to table trunc_faa
|
|
|
|
drop cascades to table trunc_fb
|
2008-03-28 01:21:56 +01:00
|
|
|
-- Test ON TRUNCATE triggers
|
|
|
|
CREATE TABLE trunc_trigger_test (f1 int, f2 text, f3 text);
|
|
|
|
CREATE TABLE trunc_trigger_log (tgop text, tglevel text, tgwhen text,
|
|
|
|
tgargv text, tgtable name, rowcount bigint);
|
|
|
|
CREATE FUNCTION trunctrigger() RETURNS trigger as $$
|
|
|
|
declare c bigint;
|
|
|
|
begin
|
|
|
|
execute 'select count(*) from ' || quote_ident(tg_table_name) into c;
|
|
|
|
insert into trunc_trigger_log values
|
|
|
|
(TG_OP, TG_LEVEL, TG_WHEN, TG_ARGV[0], tg_table_name, c);
|
|
|
|
return null;
|
|
|
|
end;
|
|
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
-- basic before trigger
|
|
|
|
INSERT INTO trunc_trigger_test VALUES(1, 'foo', 'bar'), (2, 'baz', 'quux');
|
|
|
|
CREATE TRIGGER t
|
|
|
|
BEFORE TRUNCATE ON trunc_trigger_test
|
2010-11-23 21:27:50 +01:00
|
|
|
FOR EACH STATEMENT
|
2008-03-28 01:21:56 +01:00
|
|
|
EXECUTE PROCEDURE trunctrigger('before trigger truncate');
|
|
|
|
SELECT count(*) as "Row count in test table" FROM trunc_trigger_test;
|
|
|
|
Row count in test table
|
|
|
|
-------------------------
|
|
|
|
2
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT * FROM trunc_trigger_log;
|
|
|
|
tgop | tglevel | tgwhen | tgargv | tgtable | rowcount
|
|
|
|
------+---------+--------+--------+---------+----------
|
|
|
|
(0 rows)
|
|
|
|
|
|
|
|
TRUNCATE trunc_trigger_test;
|
|
|
|
SELECT count(*) as "Row count in test table" FROM trunc_trigger_test;
|
|
|
|
Row count in test table
|
|
|
|
-------------------------
|
|
|
|
0
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT * FROM trunc_trigger_log;
|
|
|
|
tgop | tglevel | tgwhen | tgargv | tgtable | rowcount
|
|
|
|
----------+-----------+--------+-------------------------+--------------------+----------
|
|
|
|
TRUNCATE | STATEMENT | BEFORE | before trigger truncate | trunc_trigger_test | 2
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
DROP TRIGGER t ON trunc_trigger_test;
|
|
|
|
truncate trunc_trigger_log;
|
|
|
|
-- same test with an after trigger
|
|
|
|
INSERT INTO trunc_trigger_test VALUES(1, 'foo', 'bar'), (2, 'baz', 'quux');
|
|
|
|
CREATE TRIGGER tt
|
|
|
|
AFTER TRUNCATE ON trunc_trigger_test
|
2010-11-23 21:27:50 +01:00
|
|
|
FOR EACH STATEMENT
|
2008-03-28 01:21:56 +01:00
|
|
|
EXECUTE PROCEDURE trunctrigger('after trigger truncate');
|
|
|
|
SELECT count(*) as "Row count in test table" FROM trunc_trigger_test;
|
|
|
|
Row count in test table
|
|
|
|
-------------------------
|
|
|
|
2
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT * FROM trunc_trigger_log;
|
|
|
|
tgop | tglevel | tgwhen | tgargv | tgtable | rowcount
|
|
|
|
------+---------+--------+--------+---------+----------
|
|
|
|
(0 rows)
|
|
|
|
|
|
|
|
TRUNCATE trunc_trigger_test;
|
|
|
|
SELECT count(*) as "Row count in test table" FROM trunc_trigger_test;
|
|
|
|
Row count in test table
|
|
|
|
-------------------------
|
|
|
|
0
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT * FROM trunc_trigger_log;
|
|
|
|
tgop | tglevel | tgwhen | tgargv | tgtable | rowcount
|
|
|
|
----------+-----------+--------+------------------------+--------------------+----------
|
|
|
|
TRUNCATE | STATEMENT | AFTER | after trigger truncate | trunc_trigger_test | 0
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
DROP TABLE trunc_trigger_test;
|
|
|
|
DROP TABLE trunc_trigger_log;
|
|
|
|
DROP FUNCTION trunctrigger();
|
2008-05-17 01:36:05 +02:00
|
|
|
-- test TRUNCATE ... RESTART IDENTITY
|
|
|
|
CREATE SEQUENCE truncate_a_id1 START WITH 33;
|
|
|
|
CREATE TABLE truncate_a (id serial,
|
|
|
|
id1 integer default nextval('truncate_a_id1'));
|
|
|
|
ALTER SEQUENCE truncate_a_id1 OWNED BY truncate_a.id1;
|
|
|
|
INSERT INTO truncate_a DEFAULT VALUES;
|
|
|
|
INSERT INTO truncate_a DEFAULT VALUES;
|
|
|
|
SELECT * FROM truncate_a;
|
|
|
|
id | id1
|
|
|
|
----+-----
|
|
|
|
1 | 33
|
|
|
|
2 | 34
|
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
TRUNCATE truncate_a;
|
|
|
|
INSERT INTO truncate_a DEFAULT VALUES;
|
|
|
|
INSERT INTO truncate_a DEFAULT VALUES;
|
|
|
|
SELECT * FROM truncate_a;
|
|
|
|
id | id1
|
|
|
|
----+-----
|
|
|
|
3 | 35
|
|
|
|
4 | 36
|
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
TRUNCATE truncate_a RESTART IDENTITY;
|
|
|
|
INSERT INTO truncate_a DEFAULT VALUES;
|
|
|
|
INSERT INTO truncate_a DEFAULT VALUES;
|
|
|
|
SELECT * FROM truncate_a;
|
|
|
|
id | id1
|
|
|
|
----+-----
|
|
|
|
1 | 33
|
|
|
|
2 | 34
|
|
|
|
(2 rows)
|
|
|
|
|
2017-04-06 14:33:16 +02:00
|
|
|
CREATE TABLE truncate_b (id int GENERATED ALWAYS AS IDENTITY (START WITH 44));
|
|
|
|
INSERT INTO truncate_b DEFAULT VALUES;
|
|
|
|
INSERT INTO truncate_b DEFAULT VALUES;
|
|
|
|
SELECT * FROM truncate_b;
|
|
|
|
id
|
|
|
|
----
|
|
|
|
44
|
|
|
|
45
|
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
TRUNCATE truncate_b;
|
|
|
|
INSERT INTO truncate_b DEFAULT VALUES;
|
|
|
|
INSERT INTO truncate_b DEFAULT VALUES;
|
|
|
|
SELECT * FROM truncate_b;
|
|
|
|
id
|
|
|
|
----
|
|
|
|
46
|
|
|
|
47
|
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
TRUNCATE truncate_b RESTART IDENTITY;
|
|
|
|
INSERT INTO truncate_b DEFAULT VALUES;
|
|
|
|
INSERT INTO truncate_b DEFAULT VALUES;
|
|
|
|
SELECT * FROM truncate_b;
|
|
|
|
id
|
|
|
|
----
|
|
|
|
44
|
|
|
|
45
|
|
|
|
(2 rows)
|
|
|
|
|
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 22:42:18 +01:00
|
|
|
-- check rollback of a RESTART IDENTITY operation
|
|
|
|
BEGIN;
|
|
|
|
TRUNCATE truncate_a RESTART IDENTITY;
|
|
|
|
INSERT INTO truncate_a DEFAULT VALUES;
|
|
|
|
SELECT * FROM truncate_a;
|
|
|
|
id | id1
|
|
|
|
----+-----
|
|
|
|
1 | 33
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
ROLLBACK;
|
|
|
|
INSERT INTO truncate_a DEFAULT VALUES;
|
|
|
|
INSERT INTO truncate_a DEFAULT VALUES;
|
|
|
|
SELECT * FROM truncate_a;
|
|
|
|
id | id1
|
|
|
|
----+-----
|
|
|
|
1 | 33
|
|
|
|
2 | 34
|
|
|
|
3 | 35
|
|
|
|
4 | 36
|
|
|
|
(4 rows)
|
|
|
|
|
2008-05-17 01:36:05 +02:00
|
|
|
DROP TABLE truncate_a;
|
|
|
|
SELECT nextval('truncate_a_id1'); -- fail, seq should have been dropped
|
|
|
|
ERROR: relation "truncate_a_id1" does not exist
|
2008-09-01 22:42:46 +02:00
|
|
|
LINE 1: SELECT nextval('truncate_a_id1');
|
|
|
|
^
|
2017-03-07 17:24:51 +01:00
|
|
|
-- partitioned table
|
|
|
|
CREATE TABLE truncparted (a int, b char) PARTITION BY LIST (a);
|
2017-04-25 22:57:43 +02:00
|
|
|
-- error, can't truncate a partitioned table
|
|
|
|
TRUNCATE ONLY truncparted;
|
|
|
|
ERROR: cannot truncate only a partitioned table
|
2018-02-20 23:58:27 +01:00
|
|
|
HINT: Do not specify the ONLY keyword, or use TRUNCATE ONLY on the partitions directly.
|
2017-03-07 17:24:51 +01:00
|
|
|
CREATE TABLE truncparted1 PARTITION OF truncparted FOR VALUES IN (1);
|
|
|
|
INSERT INTO truncparted VALUES (1, 'a');
|
2017-04-25 22:57:43 +02:00
|
|
|
-- error, must truncate partitions
|
|
|
|
TRUNCATE ONLY truncparted;
|
|
|
|
ERROR: cannot truncate only a partitioned table
|
2018-02-20 23:58:27 +01:00
|
|
|
HINT: Do not specify the ONLY keyword, or use TRUNCATE ONLY on the partitions directly.
|
2017-03-07 17:24:51 +01:00
|
|
|
TRUNCATE truncparted;
|
|
|
|
DROP TABLE truncparted;
|
2018-07-12 18:09:08 +02:00
|
|
|
-- foreign key on partitioned table: partition key is referencing column.
|
|
|
|
-- Make sure truncate did execute on all tables
|
|
|
|
CREATE FUNCTION tp_ins_data() RETURNS void LANGUAGE plpgsql AS $$
|
|
|
|
BEGIN
|
|
|
|
INSERT INTO truncprim VALUES (1), (100), (150);
|
|
|
|
INSERT INTO truncpart VALUES (1), (100), (150);
|
|
|
|
END
|
|
|
|
$$;
|
|
|
|
CREATE FUNCTION tp_chk_data(OUT pktb regclass, OUT pkval int, OUT fktb regclass, OUT fkval int)
|
|
|
|
RETURNS SETOF record LANGUAGE plpgsql AS $$
|
|
|
|
BEGIN
|
|
|
|
RETURN QUERY SELECT
|
|
|
|
pk.tableoid::regclass, pk.a, fk.tableoid::regclass, fk.a
|
|
|
|
FROM truncprim pk FULL JOIN truncpart fk USING (a)
|
|
|
|
ORDER BY 2, 4;
|
|
|
|
END
|
|
|
|
$$;
|
|
|
|
CREATE TABLE truncprim (a int PRIMARY KEY);
|
|
|
|
CREATE TABLE truncpart (a int REFERENCES truncprim)
|
|
|
|
PARTITION BY RANGE (a);
|
|
|
|
CREATE TABLE truncpart_1 PARTITION OF truncpart FOR VALUES FROM (0) TO (100);
|
|
|
|
CREATE TABLE truncpart_2 PARTITION OF truncpart FOR VALUES FROM (100) TO (200)
|
|
|
|
PARTITION BY RANGE (a);
|
|
|
|
CREATE TABLE truncpart_2_1 PARTITION OF truncpart_2 FOR VALUES FROM (100) TO (150);
|
|
|
|
CREATE TABLE truncpart_2_d PARTITION OF truncpart_2 DEFAULT;
|
|
|
|
TRUNCATE TABLE truncprim; -- should fail
|
|
|
|
ERROR: cannot truncate a table referenced in a foreign key constraint
|
|
|
|
DETAIL: Table "truncpart" references "truncprim".
|
|
|
|
HINT: Truncate table "truncpart" at the same time, or use TRUNCATE ... CASCADE.
|
|
|
|
select tp_ins_data();
|
|
|
|
tp_ins_data
|
|
|
|
-------------
|
|
|
|
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
-- should truncate everything
|
|
|
|
TRUNCATE TABLE truncprim, truncpart;
|
|
|
|
select * from tp_chk_data();
|
|
|
|
pktb | pkval | fktb | fkval
|
|
|
|
------+-------+------+-------
|
|
|
|
(0 rows)
|
|
|
|
|
|
|
|
select tp_ins_data();
|
|
|
|
tp_ins_data
|
|
|
|
-------------
|
|
|
|
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
-- should truncate everything
|
|
|
|
SET client_min_messages TO WARNING; -- suppress cascading notices
|
|
|
|
TRUNCATE TABLE truncprim CASCADE;
|
|
|
|
RESET client_min_messages;
|
|
|
|
SELECT * FROM tp_chk_data();
|
|
|
|
pktb | pkval | fktb | fkval
|
|
|
|
------+-------+------+-------
|
|
|
|
(0 rows)
|
|
|
|
|
|
|
|
SELECT tp_ins_data();
|
|
|
|
tp_ins_data
|
|
|
|
-------------
|
|
|
|
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
-- should truncate all partitions
|
|
|
|
TRUNCATE TABLE truncpart;
|
|
|
|
SELECT * FROM tp_chk_data();
|
|
|
|
pktb | pkval | fktb | fkval
|
|
|
|
-----------+-------+------+-------
|
|
|
|
truncprim | 1 | |
|
|
|
|
truncprim | 100 | |
|
|
|
|
truncprim | 150 | |
|
|
|
|
(3 rows)
|
|
|
|
|
|
|
|
DROP TABLE truncprim, truncpart;
|
|
|
|
DROP FUNCTION tp_ins_data(), tp_chk_data();
|