1 -- Test basic TRUNCATE functionality.
2 CREATE TABLE truncate_a (col1 integer primary key);
3 NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "truncate_a_pkey" for table "truncate_a"
4 INSERT INTO truncate_a VALUES (1);
5 INSERT INTO truncate_a VALUES (2);
6 SELECT * FROM truncate_a;
17 SELECT * FROM truncate_a;
24 -- Commit the truncate this time
28 SELECT * FROM truncate_a;
33 -- Test foreign-key checks
34 CREATE TABLE trunc_b (a int REFERENCES truncate_a);
35 CREATE TABLE trunc_c (a serial PRIMARY KEY);
36 NOTICE: CREATE TABLE will create implicit sequence "trunc_c_a_seq" for serial column "trunc_c.a"
37 NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "trunc_c_pkey" for table "trunc_c"
38 CREATE TABLE trunc_d (a int REFERENCES trunc_c);
39 CREATE TABLE trunc_e (a int REFERENCES truncate_a, b int REFERENCES trunc_c);
40 TRUNCATE TABLE truncate_a; -- fail
41 ERROR: cannot truncate a table referenced in a foreign key constraint
42 DETAIL: Table "trunc_b" references "truncate_a".
43 HINT: Truncate table "trunc_b" at the same time, or use TRUNCATE ... CASCADE.
44 TRUNCATE TABLE truncate_a,trunc_b; -- fail
45 ERROR: cannot truncate a table referenced in a foreign key constraint
46 DETAIL: Table "trunc_e" references "truncate_a".
47 HINT: Truncate table "trunc_e" at the same time, or use TRUNCATE ... CASCADE.
48 TRUNCATE TABLE truncate_a,trunc_b,trunc_e; -- ok
49 TRUNCATE TABLE truncate_a,trunc_e; -- fail
50 ERROR: cannot truncate a table referenced in a foreign key constraint
51 DETAIL: Table "trunc_b" references "truncate_a".
52 HINT: Truncate table "trunc_b" at the same time, or use TRUNCATE ... CASCADE.
53 TRUNCATE TABLE trunc_c; -- fail
54 ERROR: cannot truncate a table referenced in a foreign key constraint
55 DETAIL: Table "trunc_d" references "trunc_c".
56 HINT: Truncate table "trunc_d" at the same time, or use TRUNCATE ... CASCADE.
57 TRUNCATE TABLE trunc_c,trunc_d; -- fail
58 ERROR: cannot truncate a table referenced in a foreign key constraint
59 DETAIL: Table "trunc_e" references "trunc_c".
60 HINT: Truncate table "trunc_e" at the same time, or use TRUNCATE ... CASCADE.
61 TRUNCATE TABLE trunc_c,trunc_d,trunc_e; -- ok
62 TRUNCATE TABLE trunc_c,trunc_d,trunc_e,truncate_a; -- fail
63 ERROR: cannot truncate a table referenced in a foreign key constraint
64 DETAIL: Table "trunc_b" references "truncate_a".
65 HINT: Truncate table "trunc_b" at the same time, or use TRUNCATE ... CASCADE.
66 TRUNCATE TABLE trunc_c,trunc_d,trunc_e,truncate_a,trunc_b; -- ok
67 TRUNCATE TABLE truncate_a RESTRICT; -- fail
68 ERROR: cannot truncate a table referenced in a foreign key constraint
69 DETAIL: Table "trunc_b" references "truncate_a".
70 HINT: Truncate table "trunc_b" at the same time, or use TRUNCATE ... CASCADE.
71 TRUNCATE TABLE truncate_a CASCADE; -- ok
72 NOTICE: truncate cascades to table "trunc_b"
73 NOTICE: truncate cascades to table "trunc_e"
74 -- circular references
75 ALTER TABLE truncate_a ADD FOREIGN KEY (col1) REFERENCES trunc_c;
76 -- Add some data to verify that truncating actually works ...
77 INSERT INTO trunc_c VALUES (1);
78 INSERT INTO truncate_a VALUES (1);
79 INSERT INTO trunc_b VALUES (1);
80 INSERT INTO trunc_d VALUES (1);
81 INSERT INTO trunc_e VALUES (1,1);
82 TRUNCATE TABLE trunc_c;
83 ERROR: cannot truncate a table referenced in a foreign key constraint
84 DETAIL: Table "truncate_a" references "trunc_c".
85 HINT: Truncate table "truncate_a" at the same time, or use TRUNCATE ... CASCADE.
86 TRUNCATE TABLE trunc_c,truncate_a;
87 ERROR: cannot truncate a table referenced in a foreign key constraint
88 DETAIL: Table "trunc_d" references "trunc_c".
89 HINT: Truncate table "trunc_d" at the same time, or use TRUNCATE ... CASCADE.
90 TRUNCATE TABLE trunc_c,truncate_a,trunc_d;
91 ERROR: cannot truncate a table referenced in a foreign key constraint
92 DETAIL: Table "trunc_e" references "trunc_c".
93 HINT: Truncate table "trunc_e" at the same time, or use TRUNCATE ... CASCADE.
94 TRUNCATE TABLE trunc_c,truncate_a,trunc_d,trunc_e;
95 ERROR: cannot truncate a table referenced in a foreign key constraint
96 DETAIL: Table "trunc_b" references "truncate_a".
97 HINT: Truncate table "trunc_b" at the same time, or use TRUNCATE ... CASCADE.
98 TRUNCATE TABLE trunc_c,truncate_a,trunc_d,trunc_e,trunc_b;
99 -- Verify that truncating did actually work
100 SELECT * FROM truncate_a
102 SELECT * FROM trunc_c
104 SELECT * FROM trunc_b
106 SELECT * FROM trunc_d;
111 SELECT * FROM trunc_e;
116 -- Add data again to test TRUNCATE ... CASCADE
117 INSERT INTO trunc_c VALUES (1);
118 INSERT INTO truncate_a VALUES (1);
119 INSERT INTO trunc_b VALUES (1);
120 INSERT INTO trunc_d VALUES (1);
121 INSERT INTO trunc_e VALUES (1,1);
122 TRUNCATE TABLE trunc_c CASCADE; -- ok
123 NOTICE: truncate cascades to table "truncate_a"
124 NOTICE: truncate cascades to table "trunc_d"
125 NOTICE: truncate cascades to table "trunc_e"
126 NOTICE: truncate cascades to table "trunc_b"
127 SELECT * FROM truncate_a
129 SELECT * FROM trunc_c
131 SELECT * FROM trunc_b
133 SELECT * FROM trunc_d;
138 SELECT * FROM trunc_e;
143 DROP TABLE truncate_a,trunc_c,trunc_b,trunc_d,trunc_e CASCADE;
144 -- Test TRUNCATE with inheritance
145 CREATE TABLE trunc_f (col1 integer primary key);
146 NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "trunc_f_pkey" for table "trunc_f"
147 INSERT INTO trunc_f VALUES (1);
148 INSERT INTO trunc_f VALUES (2);
149 CREATE TABLE trunc_fa (col2a text) INHERITS (trunc_f);
150 INSERT INTO trunc_fa VALUES (3, 'three');
151 CREATE TABLE trunc_fb (col2b int) INHERITS (trunc_f);
152 INSERT INTO trunc_fb VALUES (4, 444);
153 CREATE TABLE trunc_faa (col3 text) INHERITS (trunc_fa);
154 INSERT INTO trunc_faa VALUES (5, 'five', 'FIVE');
156 SELECT * FROM trunc_f;
167 SELECT * FROM trunc_f;
174 SELECT * FROM trunc_f;
184 TRUNCATE ONLY trunc_f;
185 SELECT * FROM trunc_f;
195 SELECT * FROM trunc_f;
205 SELECT * FROM trunc_fa;
212 SELECT * FROM trunc_faa;
214 ------+-------+------
218 TRUNCATE ONLY trunc_fb, ONLY trunc_fa;
219 SELECT * FROM trunc_f;
227 SELECT * FROM trunc_fa;
233 SELECT * FROM trunc_faa;
235 ------+-------+------
241 SELECT * FROM trunc_f;
251 SELECT * FROM trunc_fa;
258 SELECT * FROM trunc_faa;
260 ------+-------+------
264 TRUNCATE ONLY trunc_fb, trunc_fa;
265 SELECT * FROM trunc_f;
272 SELECT * FROM trunc_fa;
277 SELECT * FROM trunc_faa;
279 ------+-------+------
283 DROP TABLE trunc_f CASCADE;
284 NOTICE: drop cascades to 3 other objects
285 DETAIL: drop cascades to table trunc_fa
286 drop cascades to table trunc_faa
287 drop cascades to table trunc_fb
288 -- Test ON TRUNCATE triggers
289 CREATE TABLE trunc_trigger_test (f1 int, f2 text, f3 text);
290 CREATE TABLE trunc_trigger_log (tgop text, tglevel text, tgwhen text,
291 tgargv text, tgtable name, rowcount bigint);
292 CREATE FUNCTION trunctrigger() RETURNS trigger as $$
295 execute 'select count(*) from ' || quote_ident(tg_table_name) into c;
296 insert into trunc_trigger_log values
297 (TG_OP, TG_LEVEL, TG_WHEN, TG_ARGV[0], tg_table_name, c);
301 -- basic before trigger
302 INSERT INTO trunc_trigger_test VALUES(1, 'foo', 'bar'), (2, 'baz', 'quux');
304 BEFORE TRUNCATE ON trunc_trigger_test
306 EXECUTE PROCEDURE trunctrigger('before trigger truncate');
307 SELECT count(*) as "Row count in test table" FROM trunc_trigger_test;
308 Row count in test table
309 -------------------------
313 SELECT * FROM trunc_trigger_log;
314 tgop | tglevel | tgwhen | tgargv | tgtable | rowcount
315 ------+---------+--------+--------+---------+----------
318 TRUNCATE trunc_trigger_test;
319 SELECT count(*) as "Row count in test table" FROM trunc_trigger_test;
320 Row count in test table
321 -------------------------
325 SELECT * FROM trunc_trigger_log;
326 tgop | tglevel | tgwhen | tgargv | tgtable | rowcount
327 ----------+-----------+--------+-------------------------+--------------------+----------
328 TRUNCATE | STATEMENT | BEFORE | before trigger truncate | trunc_trigger_test | 2
331 DROP TRIGGER t ON trunc_trigger_test;
332 truncate trunc_trigger_log;
333 -- same test with an after trigger
334 INSERT INTO trunc_trigger_test VALUES(1, 'foo', 'bar'), (2, 'baz', 'quux');
336 AFTER TRUNCATE ON trunc_trigger_test
338 EXECUTE PROCEDURE trunctrigger('after trigger truncate');
339 SELECT count(*) as "Row count in test table" FROM trunc_trigger_test;
340 Row count in test table
341 -------------------------
345 SELECT * FROM trunc_trigger_log;
346 tgop | tglevel | tgwhen | tgargv | tgtable | rowcount
347 ------+---------+--------+--------+---------+----------
350 TRUNCATE trunc_trigger_test;
351 SELECT count(*) as "Row count in test table" FROM trunc_trigger_test;
352 Row count in test table
353 -------------------------
357 SELECT * FROM trunc_trigger_log;
358 tgop | tglevel | tgwhen | tgargv | tgtable | rowcount
359 ----------+-----------+--------+------------------------+--------------------+----------
360 TRUNCATE | STATEMENT | AFTER | after trigger truncate | trunc_trigger_test | 0
363 DROP TABLE trunc_trigger_test;
364 DROP TABLE trunc_trigger_log;
365 DROP FUNCTION trunctrigger();
366 -- test TRUNCATE ... RESTART IDENTITY
367 CREATE SEQUENCE truncate_a_id1 START WITH 33;
368 CREATE TABLE truncate_a (id serial,
369 id1 integer default nextval('truncate_a_id1'));
370 NOTICE: CREATE TABLE will create implicit sequence "truncate_a_id_seq" for serial column "truncate_a.id"
371 ALTER SEQUENCE truncate_a_id1 OWNED BY truncate_a.id1;
372 INSERT INTO truncate_a DEFAULT VALUES;
373 INSERT INTO truncate_a DEFAULT VALUES;
374 SELECT * FROM truncate_a;
382 INSERT INTO truncate_a DEFAULT VALUES;
383 INSERT INTO truncate_a DEFAULT VALUES;
384 SELECT * FROM truncate_a;
391 TRUNCATE truncate_a RESTART IDENTITY;
392 INSERT INTO truncate_a DEFAULT VALUES;
393 INSERT INTO truncate_a DEFAULT VALUES;
394 SELECT * FROM truncate_a;
401 DROP TABLE truncate_a;
402 SELECT nextval('truncate_a_id1'); -- fail, seq should have been dropped
403 ERROR: relation "truncate_a_id1" does not exist
404 LINE 1: SELECT nextval('truncate_a_id1');