]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/truncate.out
Teach the system how to use hashing for UNION. (INTERSECT/EXCEPT will follow,
[postgresql] / src / test / regress / expected / truncate.out
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;
7  col1 
8 ------
9     1
10     2
11 (2 rows)
12
13 -- Roll truncate back
14 BEGIN;
15 TRUNCATE truncate_a;
16 ROLLBACK;
17 SELECT * FROM truncate_a;
18  col1 
19 ------
20     1
21     2
22 (2 rows)
23
24 -- Commit the truncate this time
25 BEGIN;
26 TRUNCATE truncate_a;
27 COMMIT;
28 SELECT * FROM truncate_a;
29  col1 
30 ------
31 (0 rows)
32
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
101    UNION ALL
102  SELECT * FROM trunc_c
103    UNION ALL
104  SELECT * FROM trunc_b
105    UNION ALL
106  SELECT * FROM trunc_d;
107  col1 
108 ------
109 (0 rows)
110
111 SELECT * FROM trunc_e;
112  a | b 
113 ---+---
114 (0 rows)
115
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
128    UNION ALL
129  SELECT * FROM trunc_c
130    UNION ALL
131  SELECT * FROM trunc_b
132    UNION ALL
133  SELECT * FROM trunc_d;
134  col1 
135 ------
136 (0 rows)
137
138 SELECT * FROM trunc_e;
139  a | b 
140 ---+---
141 (0 rows)
142
143 DROP TABLE truncate_a,trunc_c,trunc_b,trunc_d,trunc_e CASCADE;
144 -- Test ON TRUNCATE triggers
145 CREATE TABLE trunc_trigger_test (f1 int, f2 text, f3 text);
146 CREATE TABLE trunc_trigger_log (tgop text, tglevel text, tgwhen text,
147         tgargv text, tgtable name, rowcount bigint);
148 CREATE FUNCTION trunctrigger() RETURNS trigger as $$
149 declare c bigint;
150 begin
151     execute 'select count(*) from ' || quote_ident(tg_table_name) into c;
152     insert into trunc_trigger_log values
153       (TG_OP, TG_LEVEL, TG_WHEN, TG_ARGV[0], tg_table_name, c);
154     return null;
155 end;
156 $$ LANGUAGE plpgsql;
157 -- basic before trigger
158 INSERT INTO trunc_trigger_test VALUES(1, 'foo', 'bar'), (2, 'baz', 'quux');
159 CREATE TRIGGER t
160 BEFORE TRUNCATE ON trunc_trigger_test
161 FOR EACH STATEMENT 
162 EXECUTE PROCEDURE trunctrigger('before trigger truncate');
163 SELECT count(*) as "Row count in test table" FROM trunc_trigger_test;
164  Row count in test table 
165 -------------------------
166                        2
167 (1 row)
168
169 SELECT * FROM trunc_trigger_log;
170  tgop | tglevel | tgwhen | tgargv | tgtable | rowcount 
171 ------+---------+--------+--------+---------+----------
172 (0 rows)
173
174 TRUNCATE trunc_trigger_test;
175 SELECT count(*) as "Row count in test table" FROM trunc_trigger_test;
176  Row count in test table 
177 -------------------------
178                        0
179 (1 row)
180
181 SELECT * FROM trunc_trigger_log;
182    tgop   |  tglevel  | tgwhen |         tgargv          |      tgtable       | rowcount 
183 ----------+-----------+--------+-------------------------+--------------------+----------
184  TRUNCATE | STATEMENT | BEFORE | before trigger truncate | trunc_trigger_test |        2
185 (1 row)
186
187 DROP TRIGGER t ON trunc_trigger_test;
188 truncate trunc_trigger_log;
189 -- same test with an after trigger
190 INSERT INTO trunc_trigger_test VALUES(1, 'foo', 'bar'), (2, 'baz', 'quux');
191 CREATE TRIGGER tt
192 AFTER TRUNCATE ON trunc_trigger_test
193 FOR EACH STATEMENT 
194 EXECUTE PROCEDURE trunctrigger('after trigger truncate');
195 SELECT count(*) as "Row count in test table" FROM trunc_trigger_test;
196  Row count in test table 
197 -------------------------
198                        2
199 (1 row)
200
201 SELECT * FROM trunc_trigger_log;
202  tgop | tglevel | tgwhen | tgargv | tgtable | rowcount 
203 ------+---------+--------+--------+---------+----------
204 (0 rows)
205
206 TRUNCATE trunc_trigger_test;
207 SELECT count(*) as "Row count in test table" FROM trunc_trigger_test;
208  Row count in test table 
209 -------------------------
210                        0
211 (1 row)
212
213 SELECT * FROM trunc_trigger_log;
214    tgop   |  tglevel  | tgwhen |         tgargv         |      tgtable       | rowcount 
215 ----------+-----------+--------+------------------------+--------------------+----------
216  TRUNCATE | STATEMENT | AFTER  | after trigger truncate | trunc_trigger_test |        0
217 (1 row)
218
219 DROP TABLE trunc_trigger_test;
220 DROP TABLE trunc_trigger_log;
221 DROP FUNCTION trunctrigger();
222 -- test TRUNCATE ... RESTART IDENTITY
223 CREATE SEQUENCE truncate_a_id1 START WITH 33;
224 CREATE TABLE truncate_a (id serial,
225                          id1 integer default nextval('truncate_a_id1'));
226 NOTICE:  CREATE TABLE will create implicit sequence "truncate_a_id_seq" for serial column "truncate_a.id"
227 ALTER SEQUENCE truncate_a_id1 OWNED BY truncate_a.id1;
228 INSERT INTO truncate_a DEFAULT VALUES;
229 INSERT INTO truncate_a DEFAULT VALUES;
230 SELECT * FROM truncate_a;
231  id | id1 
232 ----+-----
233   1 |  33
234   2 |  34
235 (2 rows)
236
237 TRUNCATE truncate_a;
238 INSERT INTO truncate_a DEFAULT VALUES;
239 INSERT INTO truncate_a DEFAULT VALUES;
240 SELECT * FROM truncate_a;
241  id | id1 
242 ----+-----
243   3 |  35
244   4 |  36
245 (2 rows)
246
247 TRUNCATE truncate_a RESTART IDENTITY;
248 INSERT INTO truncate_a DEFAULT VALUES;
249 INSERT INTO truncate_a DEFAULT VALUES;
250 SELECT * FROM truncate_a;
251  id | id1 
252 ----+-----
253   1 |  33
254   2 |  34
255 (2 rows)
256
257 DROP TABLE truncate_a;
258 SELECT nextval('truncate_a_id1'); -- fail, seq should have been dropped
259 ERROR:  relation "truncate_a_id1" does not exist