truncate.out 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. -- Test basic TRUNCATE functionality.
  2. CREATE TABLE truncate_a (col1 integer primary key);
  3. INSERT INTO truncate_a VALUES (1);
  4. INSERT INTO truncate_a VALUES (2);
  5. SELECT * FROM truncate_a;
  6. col1
  7. ------
  8. 1
  9. 2
  10. (2 rows)
  11. -- Roll truncate back
  12. BEGIN;
  13. TRUNCATE truncate_a;
  14. ROLLBACK;
  15. SELECT * FROM truncate_a;
  16. col1
  17. ------
  18. 1
  19. 2
  20. (2 rows)
  21. -- Commit the truncate this time
  22. BEGIN;
  23. TRUNCATE truncate_a;
  24. COMMIT;
  25. SELECT * FROM truncate_a;
  26. col1
  27. ------
  28. (0 rows)
  29. -- Test foreign-key checks
  30. CREATE TABLE trunc_b (a int REFERENCES truncate_a);
  31. CREATE TABLE trunc_c (a serial PRIMARY KEY);
  32. CREATE TABLE trunc_d (a int REFERENCES trunc_c);
  33. CREATE TABLE trunc_e (a int REFERENCES truncate_a, b int REFERENCES trunc_c);
  34. TRUNCATE TABLE truncate_a; -- fail
  35. ERROR: cannot truncate a table referenced in a foreign key constraint
  36. DETAIL: Table "trunc_b" references "truncate_a".
  37. HINT: Truncate table "trunc_b" at the same time, or use TRUNCATE ... CASCADE.
  38. TRUNCATE TABLE truncate_a,trunc_b; -- fail
  39. ERROR: cannot truncate a table referenced in a foreign key constraint
  40. DETAIL: Table "trunc_e" references "truncate_a".
  41. HINT: Truncate table "trunc_e" at the same time, or use TRUNCATE ... CASCADE.
  42. TRUNCATE TABLE truncate_a,trunc_b,trunc_e; -- ok
  43. TRUNCATE TABLE truncate_a,trunc_e; -- fail
  44. ERROR: cannot truncate a table referenced in a foreign key constraint
  45. DETAIL: Table "trunc_b" references "truncate_a".
  46. HINT: Truncate table "trunc_b" at the same time, or use TRUNCATE ... CASCADE.
  47. TRUNCATE TABLE trunc_c; -- fail
  48. ERROR: cannot truncate a table referenced in a foreign key constraint
  49. DETAIL: Table "trunc_d" references "trunc_c".
  50. HINT: Truncate table "trunc_d" at the same time, or use TRUNCATE ... CASCADE.
  51. TRUNCATE TABLE trunc_c,trunc_d; -- fail
  52. ERROR: cannot truncate a table referenced in a foreign key constraint
  53. DETAIL: Table "trunc_e" references "trunc_c".
  54. HINT: Truncate table "trunc_e" at the same time, or use TRUNCATE ... CASCADE.
  55. TRUNCATE TABLE trunc_c,trunc_d,trunc_e; -- ok
  56. TRUNCATE TABLE trunc_c,trunc_d,trunc_e,truncate_a; -- fail
  57. ERROR: cannot truncate a table referenced in a foreign key constraint
  58. DETAIL: Table "trunc_b" references "truncate_a".
  59. HINT: Truncate table "trunc_b" at the same time, or use TRUNCATE ... CASCADE.
  60. TRUNCATE TABLE trunc_c,trunc_d,trunc_e,truncate_a,trunc_b; -- ok
  61. TRUNCATE TABLE truncate_a RESTRICT; -- fail
  62. ERROR: cannot truncate a table referenced in a foreign key constraint
  63. DETAIL: Table "trunc_b" references "truncate_a".
  64. HINT: Truncate table "trunc_b" at the same time, or use TRUNCATE ... CASCADE.
  65. TRUNCATE TABLE truncate_a CASCADE; -- ok
  66. NOTICE: truncate cascades to table "trunc_b"
  67. NOTICE: truncate cascades to table "trunc_e"
  68. -- circular references
  69. ALTER TABLE truncate_a ADD FOREIGN KEY (col1) REFERENCES trunc_c;
  70. -- Add some data to verify that truncating actually works ...
  71. INSERT INTO trunc_c VALUES (1);
  72. INSERT INTO truncate_a VALUES (1);
  73. INSERT INTO trunc_b VALUES (1);
  74. INSERT INTO trunc_d VALUES (1);
  75. INSERT INTO trunc_e VALUES (1,1);
  76. TRUNCATE TABLE trunc_c;
  77. ERROR: cannot truncate a table referenced in a foreign key constraint
  78. DETAIL: Table "truncate_a" references "trunc_c".
  79. HINT: Truncate table "truncate_a" at the same time, or use TRUNCATE ... CASCADE.
  80. TRUNCATE TABLE trunc_c,truncate_a;
  81. ERROR: cannot truncate a table referenced in a foreign key constraint
  82. DETAIL: Table "trunc_d" references "trunc_c".
  83. HINT: Truncate table "trunc_d" at the same time, or use TRUNCATE ... CASCADE.
  84. TRUNCATE TABLE trunc_c,truncate_a,trunc_d;
  85. ERROR: cannot truncate a table referenced in a foreign key constraint
  86. DETAIL: Table "trunc_e" references "trunc_c".
  87. HINT: Truncate table "trunc_e" at the same time, or use TRUNCATE ... CASCADE.
  88. TRUNCATE TABLE trunc_c,truncate_a,trunc_d,trunc_e;
  89. ERROR: cannot truncate a table referenced in a foreign key constraint
  90. DETAIL: Table "trunc_b" references "truncate_a".
  91. HINT: Truncate table "trunc_b" at the same time, or use TRUNCATE ... CASCADE.
  92. TRUNCATE TABLE trunc_c,truncate_a,trunc_d,trunc_e,trunc_b;
  93. -- Verify that truncating did actually work
  94. SELECT * FROM truncate_a
  95. UNION ALL
  96. SELECT * FROM trunc_c
  97. UNION ALL
  98. SELECT * FROM trunc_b
  99. UNION ALL
  100. SELECT * FROM trunc_d;
  101. col1
  102. ------
  103. (0 rows)
  104. SELECT * FROM trunc_e;
  105. a | b
  106. ---+---
  107. (0 rows)
  108. -- Add data again to test TRUNCATE ... CASCADE
  109. INSERT INTO trunc_c VALUES (1);
  110. INSERT INTO truncate_a VALUES (1);
  111. INSERT INTO trunc_b VALUES (1);
  112. INSERT INTO trunc_d VALUES (1);
  113. INSERT INTO trunc_e VALUES (1,1);
  114. TRUNCATE TABLE trunc_c CASCADE; -- ok
  115. NOTICE: truncate cascades to table "truncate_a"
  116. NOTICE: truncate cascades to table "trunc_d"
  117. NOTICE: truncate cascades to table "trunc_e"
  118. NOTICE: truncate cascades to table "trunc_b"
  119. SELECT * FROM truncate_a
  120. UNION ALL
  121. SELECT * FROM trunc_c
  122. UNION ALL
  123. SELECT * FROM trunc_b
  124. UNION ALL
  125. SELECT * FROM trunc_d;
  126. col1
  127. ------
  128. (0 rows)
  129. SELECT * FROM trunc_e;
  130. a | b
  131. ---+---
  132. (0 rows)
  133. DROP TABLE truncate_a,trunc_c,trunc_b,trunc_d,trunc_e CASCADE;
  134. -- Test TRUNCATE with inheritance
  135. CREATE TABLE trunc_f (col1 integer primary key);
  136. INSERT INTO trunc_f VALUES (1);
  137. INSERT INTO trunc_f VALUES (2);
  138. CREATE TABLE trunc_fa (col2a text) INHERITS (trunc_f);
  139. INSERT INTO trunc_fa VALUES (3, 'three');
  140. CREATE TABLE trunc_fb (col2b int) INHERITS (trunc_f);
  141. INSERT INTO trunc_fb VALUES (4, 444);
  142. CREATE TABLE trunc_faa (col3 text) INHERITS (trunc_fa);
  143. INSERT INTO trunc_faa VALUES (5, 'five', 'FIVE');
  144. BEGIN;
  145. SELECT * FROM trunc_f;
  146. col1
  147. ------
  148. 1
  149. 2
  150. 3
  151. 4
  152. 5
  153. (5 rows)
  154. TRUNCATE trunc_f;
  155. SELECT * FROM trunc_f;
  156. col1
  157. ------
  158. (0 rows)
  159. ROLLBACK;
  160. BEGIN;
  161. SELECT * FROM trunc_f;
  162. col1
  163. ------
  164. 1
  165. 2
  166. 3
  167. 4
  168. 5
  169. (5 rows)
  170. TRUNCATE ONLY trunc_f;
  171. SELECT * FROM trunc_f;
  172. col1
  173. ------
  174. 3
  175. 4
  176. 5
  177. (3 rows)
  178. ROLLBACK;
  179. BEGIN;
  180. SELECT * FROM trunc_f;
  181. col1
  182. ------
  183. 1
  184. 2
  185. 3
  186. 4
  187. 5
  188. (5 rows)
  189. SELECT * FROM trunc_fa;
  190. col1 | col2a
  191. ------+-------
  192. 3 | three
  193. 5 | five
  194. (2 rows)
  195. SELECT * FROM trunc_faa;
  196. col1 | col2a | col3
  197. ------+-------+------
  198. 5 | five | FIVE
  199. (1 row)
  200. TRUNCATE ONLY trunc_fb, ONLY trunc_fa;
  201. SELECT * FROM trunc_f;
  202. col1
  203. ------
  204. 1
  205. 2
  206. 5
  207. (3 rows)
  208. SELECT * FROM trunc_fa;
  209. col1 | col2a
  210. ------+-------
  211. 5 | five
  212. (1 row)
  213. SELECT * FROM trunc_faa;
  214. col1 | col2a | col3
  215. ------+-------+------
  216. 5 | five | FIVE
  217. (1 row)
  218. ROLLBACK;
  219. BEGIN;
  220. SELECT * FROM trunc_f;
  221. col1
  222. ------
  223. 1
  224. 2
  225. 3
  226. 4
  227. 5
  228. (5 rows)
  229. SELECT * FROM trunc_fa;
  230. col1 | col2a
  231. ------+-------
  232. 3 | three
  233. 5 | five
  234. (2 rows)
  235. SELECT * FROM trunc_faa;
  236. col1 | col2a | col3
  237. ------+-------+------
  238. 5 | five | FIVE
  239. (1 row)
  240. TRUNCATE ONLY trunc_fb, trunc_fa;
  241. SELECT * FROM trunc_f;
  242. col1
  243. ------
  244. 1
  245. 2
  246. (2 rows)
  247. SELECT * FROM trunc_fa;
  248. col1 | col2a
  249. ------+-------
  250. (0 rows)
  251. SELECT * FROM trunc_faa;
  252. col1 | col2a | col3
  253. ------+-------+------
  254. (0 rows)
  255. ROLLBACK;
  256. DROP TABLE trunc_f CASCADE;
  257. NOTICE: drop cascades to 3 other objects
  258. DETAIL: drop cascades to table trunc_fa
  259. drop cascades to table trunc_faa
  260. drop cascades to table trunc_fb
  261. -- Test ON TRUNCATE triggers
  262. CREATE TABLE trunc_trigger_test (f1 int, f2 text, f3 text);
  263. CREATE TABLE trunc_trigger_log (tgop text, tglevel text, tgwhen text,
  264. tgargv text, tgtable name, rowcount bigint);
  265. CREATE FUNCTION trunctrigger() RETURNS trigger as $$
  266. declare c bigint;
  267. begin
  268. execute 'select count(*) from ' || quote_ident(tg_table_name) into c;
  269. insert into trunc_trigger_log values
  270. (TG_OP, TG_LEVEL, TG_WHEN, TG_ARGV[0], tg_table_name, c);
  271. return null;
  272. end;
  273. $$ LANGUAGE plpgsql;
  274. -- basic before trigger
  275. INSERT INTO trunc_trigger_test VALUES(1, 'foo', 'bar'), (2, 'baz', 'quux');
  276. CREATE TRIGGER t
  277. BEFORE TRUNCATE ON trunc_trigger_test
  278. FOR EACH STATEMENT
  279. EXECUTE PROCEDURE trunctrigger('before trigger truncate');
  280. SELECT count(*) as "Row count in test table" FROM trunc_trigger_test;
  281. Row count in test table
  282. -------------------------
  283. 2
  284. (1 row)
  285. SELECT * FROM trunc_trigger_log;
  286. tgop | tglevel | tgwhen | tgargv | tgtable | rowcount
  287. ------+---------+--------+--------+---------+----------
  288. (0 rows)
  289. TRUNCATE trunc_trigger_test;
  290. SELECT count(*) as "Row count in test table" FROM trunc_trigger_test;
  291. Row count in test table
  292. -------------------------
  293. 0
  294. (1 row)
  295. SELECT * FROM trunc_trigger_log;
  296. tgop | tglevel | tgwhen | tgargv | tgtable | rowcount
  297. ----------+-----------+--------+-------------------------+--------------------+----------
  298. TRUNCATE | STATEMENT | BEFORE | before trigger truncate | trunc_trigger_test | 2
  299. (1 row)
  300. DROP TRIGGER t ON trunc_trigger_test;
  301. truncate trunc_trigger_log;
  302. -- same test with an after trigger
  303. INSERT INTO trunc_trigger_test VALUES(1, 'foo', 'bar'), (2, 'baz', 'quux');
  304. CREATE TRIGGER tt
  305. AFTER TRUNCATE ON trunc_trigger_test
  306. FOR EACH STATEMENT
  307. EXECUTE PROCEDURE trunctrigger('after trigger truncate');
  308. SELECT count(*) as "Row count in test table" FROM trunc_trigger_test;
  309. Row count in test table
  310. -------------------------
  311. 2
  312. (1 row)
  313. SELECT * FROM trunc_trigger_log;
  314. tgop | tglevel | tgwhen | tgargv | tgtable | rowcount
  315. ------+---------+--------+--------+---------+----------
  316. (0 rows)
  317. TRUNCATE trunc_trigger_test;
  318. SELECT count(*) as "Row count in test table" FROM trunc_trigger_test;
  319. Row count in test table
  320. -------------------------
  321. 0
  322. (1 row)
  323. SELECT * FROM trunc_trigger_log;
  324. tgop | tglevel | tgwhen | tgargv | tgtable | rowcount
  325. ----------+-----------+--------+------------------------+--------------------+----------
  326. TRUNCATE | STATEMENT | AFTER | after trigger truncate | trunc_trigger_test | 0
  327. (1 row)
  328. DROP TABLE trunc_trigger_test;
  329. DROP TABLE trunc_trigger_log;
  330. DROP FUNCTION trunctrigger();
  331. -- test TRUNCATE ... RESTART IDENTITY
  332. CREATE SEQUENCE truncate_a_id1 START WITH 33;
  333. CREATE TABLE truncate_a (id serial,
  334. id1 integer default nextval('truncate_a_id1'));
  335. ALTER SEQUENCE truncate_a_id1 OWNED BY truncate_a.id1;
  336. INSERT INTO truncate_a DEFAULT VALUES;
  337. INSERT INTO truncate_a DEFAULT VALUES;
  338. SELECT * FROM truncate_a;
  339. id | id1
  340. ----+-----
  341. 1 | 33
  342. 2 | 34
  343. (2 rows)
  344. TRUNCATE truncate_a;
  345. INSERT INTO truncate_a DEFAULT VALUES;
  346. INSERT INTO truncate_a DEFAULT VALUES;
  347. SELECT * FROM truncate_a;
  348. id | id1
  349. ----+-----
  350. 3 | 35
  351. 4 | 36
  352. (2 rows)
  353. TRUNCATE truncate_a RESTART IDENTITY;
  354. INSERT INTO truncate_a DEFAULT VALUES;
  355. INSERT INTO truncate_a DEFAULT VALUES;
  356. SELECT * FROM truncate_a;
  357. id | id1
  358. ----+-----
  359. 1 | 33
  360. 2 | 34
  361. (2 rows)
  362. CREATE TABLE truncate_b (id int GENERATED ALWAYS AS IDENTITY (START WITH 44));
  363. INSERT INTO truncate_b DEFAULT VALUES;
  364. INSERT INTO truncate_b DEFAULT VALUES;
  365. SELECT * FROM truncate_b;
  366. id
  367. ----
  368. 44
  369. 45
  370. (2 rows)
  371. TRUNCATE truncate_b;
  372. INSERT INTO truncate_b DEFAULT VALUES;
  373. INSERT INTO truncate_b DEFAULT VALUES;
  374. SELECT * FROM truncate_b;
  375. id
  376. ----
  377. 46
  378. 47
  379. (2 rows)
  380. TRUNCATE truncate_b RESTART IDENTITY;
  381. INSERT INTO truncate_b DEFAULT VALUES;
  382. INSERT INTO truncate_b DEFAULT VALUES;
  383. SELECT * FROM truncate_b;
  384. id
  385. ----
  386. 44
  387. 45
  388. (2 rows)
  389. -- check rollback of a RESTART IDENTITY operation
  390. BEGIN;
  391. TRUNCATE truncate_a RESTART IDENTITY;
  392. INSERT INTO truncate_a DEFAULT VALUES;
  393. SELECT * FROM truncate_a;
  394. id | id1
  395. ----+-----
  396. 1 | 33
  397. (1 row)
  398. ROLLBACK;
  399. INSERT INTO truncate_a DEFAULT VALUES;
  400. INSERT INTO truncate_a DEFAULT VALUES;
  401. SELECT * FROM truncate_a;
  402. id | id1
  403. ----+-----
  404. 1 | 33
  405. 2 | 34
  406. 3 | 35
  407. 4 | 36
  408. (4 rows)
  409. DROP TABLE truncate_a;
  410. SELECT nextval('truncate_a_id1'); -- fail, seq should have been dropped
  411. ERROR: relation "truncate_a_id1" does not exist
  412. LINE 1: SELECT nextval('truncate_a_id1');
  413. ^
  414. -- partitioned table
  415. CREATE TABLE truncparted (a int, b char) PARTITION BY LIST (a);
  416. -- error, can't truncate a partitioned table
  417. TRUNCATE ONLY truncparted;
  418. ERROR: cannot truncate only a partitioned table
  419. HINT: Do not specify the ONLY keyword, or use TRUNCATE ONLY on the partitions directly.
  420. CREATE TABLE truncparted1 PARTITION OF truncparted FOR VALUES IN (1);
  421. INSERT INTO truncparted VALUES (1, 'a');
  422. -- error, must truncate partitions
  423. TRUNCATE ONLY truncparted;
  424. ERROR: cannot truncate only a partitioned table
  425. HINT: Do not specify the ONLY keyword, or use TRUNCATE ONLY on the partitions directly.
  426. TRUNCATE truncparted;
  427. DROP TABLE truncparted;
  428. -- foreign key on partitioned table: partition key is referencing column.
  429. -- Make sure truncate did execute on all tables
  430. CREATE FUNCTION tp_ins_data() RETURNS void LANGUAGE plpgsql AS $$
  431. BEGIN
  432. INSERT INTO truncprim VALUES (1), (100), (150);
  433. INSERT INTO truncpart VALUES (1), (100), (150);
  434. END
  435. $$;
  436. CREATE FUNCTION tp_chk_data(OUT pktb regclass, OUT pkval int, OUT fktb regclass, OUT fkval int)
  437. RETURNS SETOF record LANGUAGE plpgsql AS $$
  438. BEGIN
  439. RETURN QUERY SELECT
  440. pk.tableoid::regclass, pk.a, fk.tableoid::regclass, fk.a
  441. FROM truncprim pk FULL JOIN truncpart fk USING (a)
  442. ORDER BY 2, 4;
  443. END
  444. $$;
  445. CREATE TABLE truncprim (a int PRIMARY KEY);
  446. CREATE TABLE truncpart (a int REFERENCES truncprim)
  447. PARTITION BY RANGE (a);
  448. CREATE TABLE truncpart_1 PARTITION OF truncpart FOR VALUES FROM (0) TO (100);
  449. CREATE TABLE truncpart_2 PARTITION OF truncpart FOR VALUES FROM (100) TO (200)
  450. PARTITION BY RANGE (a);
  451. CREATE TABLE truncpart_2_1 PARTITION OF truncpart_2 FOR VALUES FROM (100) TO (150);
  452. CREATE TABLE truncpart_2_d PARTITION OF truncpart_2 DEFAULT;
  453. TRUNCATE TABLE truncprim; -- should fail
  454. ERROR: cannot truncate a table referenced in a foreign key constraint
  455. DETAIL: Table "truncpart" references "truncprim".
  456. HINT: Truncate table "truncpart" at the same time, or use TRUNCATE ... CASCADE.
  457. select tp_ins_data();
  458. tp_ins_data
  459. -------------
  460. (1 row)
  461. -- should truncate everything
  462. TRUNCATE TABLE truncprim, truncpart;
  463. select * from tp_chk_data();
  464. pktb | pkval | fktb | fkval
  465. ------+-------+------+-------
  466. (0 rows)
  467. select tp_ins_data();
  468. tp_ins_data
  469. -------------
  470. (1 row)
  471. -- should truncate everything
  472. TRUNCATE TABLE truncprim CASCADE;
  473. NOTICE: truncate cascades to table "truncpart"
  474. NOTICE: truncate cascades to table "truncpart_1"
  475. NOTICE: truncate cascades to table "truncpart_2"
  476. NOTICE: truncate cascades to table "truncpart_2_1"
  477. NOTICE: truncate cascades to table "truncpart_2_d"
  478. SELECT * FROM tp_chk_data();
  479. pktb | pkval | fktb | fkval
  480. ------+-------+------+-------
  481. (0 rows)
  482. SELECT tp_ins_data();
  483. tp_ins_data
  484. -------------
  485. (1 row)
  486. -- should truncate all partitions
  487. TRUNCATE TABLE truncpart;
  488. SELECT * FROM tp_chk_data();
  489. pktb | pkval | fktb | fkval
  490. -----------+-------+------+-------
  491. truncprim | 1 | |
  492. truncprim | 100 | |
  493. truncprim | 150 | |
  494. (3 rows)
  495. DROP TABLE truncprim, truncpart;
  496. DROP FUNCTION tp_ins_data(), tp_chk_data();
  497. -- test cascade when referencing a partitioned table
  498. CREATE TABLE trunc_a (a INT PRIMARY KEY) PARTITION BY RANGE (a);
  499. CREATE TABLE trunc_a1 PARTITION OF trunc_a FOR VALUES FROM (0) TO (10);
  500. CREATE TABLE trunc_a2 PARTITION OF trunc_a FOR VALUES FROM (10) TO (20)
  501. PARTITION BY RANGE (a);
  502. CREATE TABLE trunc_a21 PARTITION OF trunc_a2 FOR VALUES FROM (10) TO (12);
  503. CREATE TABLE trunc_a22 PARTITION OF trunc_a2 FOR VALUES FROM (12) TO (16);
  504. CREATE TABLE trunc_a2d PARTITION OF trunc_a2 DEFAULT;
  505. CREATE TABLE trunc_a3 PARTITION OF trunc_a FOR VALUES FROM (20) TO (30);
  506. INSERT INTO trunc_a VALUES (0), (5), (10), (15), (20), (25);
  507. -- truncate a partition cascading to a table
  508. CREATE TABLE ref_b (
  509. b INT PRIMARY KEY,
  510. a INT REFERENCES trunc_a(a) ON DELETE CASCADE
  511. );
  512. INSERT INTO ref_b VALUES (10, 0), (50, 5), (100, 10), (150, 15);
  513. TRUNCATE TABLE trunc_a1 CASCADE;
  514. NOTICE: truncate cascades to table "ref_b"
  515. SELECT a FROM ref_b;
  516. a
  517. ---
  518. (0 rows)
  519. DROP TABLE ref_b;
  520. -- truncate a partition cascading to a partitioned table
  521. CREATE TABLE ref_c (
  522. c INT PRIMARY KEY,
  523. a INT REFERENCES trunc_a(a) ON DELETE CASCADE
  524. ) PARTITION BY RANGE (c);
  525. CREATE TABLE ref_c1 PARTITION OF ref_c FOR VALUES FROM (100) TO (200);
  526. CREATE TABLE ref_c2 PARTITION OF ref_c FOR VALUES FROM (200) TO (300);
  527. INSERT INTO ref_c VALUES (100, 10), (150, 15), (200, 20), (250, 25);
  528. TRUNCATE TABLE trunc_a21 CASCADE;
  529. NOTICE: truncate cascades to table "ref_c"
  530. NOTICE: truncate cascades to table "ref_c1"
  531. NOTICE: truncate cascades to table "ref_c2"
  532. SELECT a as "from table ref_c" FROM ref_c;
  533. from table ref_c
  534. ------------------
  535. (0 rows)
  536. SELECT a as "from table trunc_a" FROM trunc_a ORDER BY a;
  537. from table trunc_a
  538. --------------------
  539. 15
  540. 20
  541. 25
  542. (3 rows)
  543. DROP TABLE trunc_a, ref_c;