create_table.sql 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. --
  2. -- CREATE_TABLE
  3. --
  4. --
  5. -- CLASS DEFINITIONS
  6. --
  7. CREATE TABLE hobbies_r (
  8. name text,
  9. person text
  10. );
  11. CREATE TABLE equipment_r (
  12. name text,
  13. hobby text
  14. );
  15. CREATE TABLE onek (
  16. unique1 int4,
  17. unique2 int4,
  18. two int4,
  19. four int4,
  20. ten int4,
  21. twenty int4,
  22. hundred int4,
  23. thousand int4,
  24. twothousand int4,
  25. fivethous int4,
  26. tenthous int4,
  27. odd int4,
  28. even int4,
  29. stringu1 name,
  30. stringu2 name,
  31. string4 name
  32. );
  33. CREATE TABLE tenk1 (
  34. unique1 int4,
  35. unique2 int4,
  36. two int4,
  37. four int4,
  38. ten int4,
  39. twenty int4,
  40. hundred int4,
  41. thousand int4,
  42. twothousand int4,
  43. fivethous int4,
  44. tenthous int4,
  45. odd int4,
  46. even int4,
  47. stringu1 name,
  48. stringu2 name,
  49. string4 name
  50. );
  51. CREATE TABLE tenk2 (
  52. unique1 int4,
  53. unique2 int4,
  54. two int4,
  55. four int4,
  56. ten int4,
  57. twenty int4,
  58. hundred int4,
  59. thousand int4,
  60. twothousand int4,
  61. fivethous int4,
  62. tenthous int4,
  63. odd int4,
  64. even int4,
  65. stringu1 name,
  66. stringu2 name,
  67. string4 name
  68. );
  69. CREATE TABLE person (
  70. name text,
  71. age int4,
  72. location point
  73. );
  74. CREATE TABLE emp (
  75. salary int4,
  76. manager name
  77. ) INHERITS (person);
  78. CREATE TABLE student (
  79. gpa float8
  80. ) INHERITS (person);
  81. CREATE TABLE stud_emp (
  82. percent int4
  83. ) INHERITS (emp, student);
  84. CREATE TABLE city (
  85. name name,
  86. location box,
  87. budget city_budget
  88. );
  89. CREATE TABLE dept (
  90. dname name,
  91. mgrname text
  92. );
  93. CREATE TABLE slow_emp4000 (
  94. home_base box
  95. );
  96. CREATE TABLE fast_emp4000 (
  97. home_base box
  98. );
  99. CREATE TABLE road (
  100. name text,
  101. thepath path
  102. );
  103. CREATE TABLE ihighway () INHERITS (road);
  104. CREATE TABLE shighway (
  105. surface text
  106. ) INHERITS (road);
  107. CREATE TABLE real_city (
  108. pop int4,
  109. cname text,
  110. outline path
  111. );
  112. --
  113. -- test the "star" operators a bit more thoroughly -- this time,
  114. -- throw in lots of NULL fields...
  115. --
  116. -- a is the type root
  117. -- b and c inherit from a (one-level single inheritance)
  118. -- d inherits from b and c (two-level multiple inheritance)
  119. -- e inherits from c (two-level single inheritance)
  120. -- f inherits from e (three-level single inheritance)
  121. --
  122. CREATE TABLE a_star (
  123. class char,
  124. a int4
  125. );
  126. CREATE TABLE b_star (
  127. b text
  128. ) INHERITS (a_star);
  129. CREATE TABLE c_star (
  130. c name
  131. ) INHERITS (a_star);
  132. CREATE TABLE d_star (
  133. d float8
  134. ) INHERITS (b_star, c_star);
  135. CREATE TABLE e_star (
  136. e int2
  137. ) INHERITS (c_star);
  138. CREATE TABLE f_star (
  139. f polygon
  140. ) INHERITS (e_star);
  141. CREATE TABLE aggtest (
  142. a int2,
  143. b float4
  144. );
  145. CREATE TABLE hash_i4_heap (
  146. seqno int4,
  147. random int4
  148. );
  149. CREATE TABLE hash_name_heap (
  150. seqno int4,
  151. random name
  152. );
  153. CREATE TABLE hash_txt_heap (
  154. seqno int4,
  155. random text
  156. );
  157. CREATE TABLE hash_f8_heap (
  158. seqno int4,
  159. random float8
  160. );
  161. -- don't include the hash_ovfl_heap stuff in the distribution
  162. -- the data set is too large for what it's worth
  163. --
  164. -- CREATE TABLE hash_ovfl_heap (
  165. -- x int4,
  166. -- y int4
  167. -- );
  168. CREATE TABLE bt_i4_heap (
  169. seqno int4,
  170. random int4
  171. );
  172. CREATE TABLE bt_name_heap (
  173. seqno name,
  174. random int4
  175. );
  176. CREATE TABLE bt_txt_heap (
  177. seqno text,
  178. random int4
  179. );
  180. CREATE TABLE bt_f8_heap (
  181. seqno float8,
  182. random int4
  183. );
  184. CREATE TABLE array_op_test (
  185. seqno int4,
  186. i int4[],
  187. t text[]
  188. );
  189. CREATE TABLE array_index_op_test (
  190. seqno int4,
  191. i int4[],
  192. t text[]
  193. );
  194. CREATE TABLE testjsonb (
  195. j jsonb
  196. );
  197. CREATE TABLE unknowntab (
  198. u unknown -- fail
  199. );
  200. CREATE TYPE unknown_comptype AS (
  201. u unknown -- fail
  202. );
  203. CREATE TABLE IF NOT EXISTS test_tsvector(
  204. t text,
  205. a tsvector
  206. );
  207. CREATE TABLE IF NOT EXISTS test_tsvector(
  208. t text
  209. );
  210. -- invalid: non-lowercase quoted reloptions identifiers
  211. CREATE TABLE tas_case WITH ("Fillfactor" = 10) AS SELECT 1 a;
  212. CREATE UNLOGGED TABLE unlogged1 (a int primary key); -- OK
  213. CREATE TEMPORARY TABLE unlogged2 (a int primary key); -- OK
  214. SELECT relname, relkind, relpersistence FROM pg_class WHERE relname ~ '^unlogged\d' ORDER BY relname;
  215. REINDEX INDEX unlogged1_pkey;
  216. REINDEX INDEX unlogged2_pkey;
  217. SELECT relname, relkind, relpersistence FROM pg_class WHERE relname ~ '^unlogged\d' ORDER BY relname;
  218. DROP TABLE unlogged2;
  219. INSERT INTO unlogged1 VALUES (42);
  220. CREATE UNLOGGED TABLE public.unlogged2 (a int primary key); -- also OK
  221. CREATE UNLOGGED TABLE pg_temp.unlogged3 (a int primary key); -- not OK
  222. CREATE TABLE pg_temp.implicitly_temp (a int primary key); -- OK
  223. CREATE TEMP TABLE explicitly_temp (a int primary key); -- also OK
  224. CREATE TEMP TABLE pg_temp.doubly_temp (a int primary key); -- also OK
  225. CREATE TEMP TABLE public.temp_to_perm (a int primary key); -- not OK
  226. DROP TABLE unlogged1, public.unlogged2;
  227. CREATE TABLE as_select1 AS SELECT * FROM pg_class WHERE relkind = 'r';
  228. CREATE TABLE as_select1 AS SELECT * FROM pg_class WHERE relkind = 'r';
  229. CREATE TABLE IF NOT EXISTS as_select1 AS SELECT * FROM pg_class WHERE relkind = 'r';
  230. DROP TABLE as_select1;
  231. PREPARE select1 AS SELECT 1 as a;
  232. CREATE TABLE as_select1 AS EXECUTE select1;
  233. CREATE TABLE as_select1 AS EXECUTE select1;
  234. SELECT * FROM as_select1;
  235. CREATE TABLE IF NOT EXISTS as_select1 AS EXECUTE select1;
  236. DROP TABLE as_select1;
  237. DEALLOCATE select1;
  238. -- create an extra wide table to test for issues related to that
  239. -- (temporarily hide query, to avoid the long CREATE TABLE stmt)
  240. \set ECHO none
  241. SELECT 'CREATE TABLE extra_wide_table(firstc text, '|| array_to_string(array_agg('c'||i||' bool'),',')||', lastc text);'
  242. FROM generate_series(1, 1100) g(i)
  243. \gexec
  244. \set ECHO all
  245. INSERT INTO extra_wide_table(firstc, lastc) VALUES('first col', 'last col');
  246. SELECT firstc, lastc FROM extra_wide_table;
  247. -- check that tables with oids cannot be created anymore
  248. CREATE TABLE withoid() WITH OIDS;
  249. CREATE TABLE withoid() WITH (oids);
  250. CREATE TABLE withoid() WITH (oids = true);
  251. -- but explicitly not adding oids is still supported
  252. CREATE TEMP TABLE withoutoid() WITHOUT OIDS; DROP TABLE withoutoid;
  253. CREATE TEMP TABLE withoutoid() WITH (oids = false); DROP TABLE withoutoid;
  254. -- check restriction with default expressions
  255. -- invalid use of column reference in default expressions
  256. CREATE TABLE default_expr_column (id int DEFAULT (id));
  257. CREATE TABLE default_expr_column (id int DEFAULT (bar.id));
  258. CREATE TABLE default_expr_agg_column (id int DEFAULT (avg(id)));
  259. -- invalid column definition
  260. CREATE TABLE default_expr_non_column (a int DEFAULT (avg(non_existent)));
  261. -- invalid use of aggregate
  262. CREATE TABLE default_expr_agg (a int DEFAULT (avg(1)));
  263. -- invalid use of subquery
  264. CREATE TABLE default_expr_agg (a int DEFAULT (select 1));
  265. -- invalid use of set-returning function
  266. CREATE TABLE default_expr_agg (a int DEFAULT (generate_series(1,3)));
  267. -- Verify that subtransaction rollback restores rd_createSubid.
  268. BEGIN;
  269. CREATE TABLE remember_create_subid (c int);
  270. SAVEPOINT q; DROP TABLE remember_create_subid; ROLLBACK TO q;
  271. COMMIT;
  272. DROP TABLE remember_create_subid;
  273. -- Verify that subtransaction rollback restores rd_firstRelfilenodeSubid.
  274. CREATE TABLE remember_node_subid (c int);
  275. BEGIN;
  276. ALTER TABLE remember_node_subid ALTER c TYPE bigint;
  277. SAVEPOINT q; DROP TABLE remember_node_subid; ROLLBACK TO q;
  278. COMMIT;
  279. DROP TABLE remember_node_subid;
  280. --
  281. -- Partitioned tables
  282. --
  283. -- cannot combine INHERITS and PARTITION BY (although grammar allows)
  284. CREATE TABLE partitioned (
  285. a int
  286. ) INHERITS (some_table) PARTITION BY LIST (a);
  287. -- cannot use more than 1 column as partition key for list partitioned table
  288. CREATE TABLE partitioned (
  289. a1 int,
  290. a2 int
  291. ) PARTITION BY LIST (a1, a2); -- fail
  292. -- unsupported constraint type for partitioned tables
  293. CREATE TABLE partitioned (
  294. a int,
  295. EXCLUDE USING gist (a WITH &&)
  296. ) PARTITION BY RANGE (a);
  297. -- prevent using prohibited expressions in the key
  298. CREATE FUNCTION retset (a int) RETURNS SETOF int AS $$ SELECT 1; $$ LANGUAGE SQL IMMUTABLE;
  299. CREATE TABLE partitioned (
  300. a int
  301. ) PARTITION BY RANGE (retset(a));
  302. DROP FUNCTION retset(int);
  303. CREATE TABLE partitioned (
  304. a int
  305. ) PARTITION BY RANGE ((avg(a)));
  306. CREATE TABLE partitioned (
  307. a int,
  308. b int
  309. ) PARTITION BY RANGE ((avg(a) OVER (PARTITION BY b)));
  310. CREATE TABLE partitioned (
  311. a int
  312. ) PARTITION BY LIST ((a LIKE (SELECT 1)));
  313. CREATE TABLE partitioned (
  314. a int
  315. ) PARTITION BY RANGE ((42));
  316. CREATE FUNCTION const_func () RETURNS int AS $$ SELECT 1; $$ LANGUAGE SQL IMMUTABLE;
  317. CREATE TABLE partitioned (
  318. a int
  319. ) PARTITION BY RANGE (const_func());
  320. DROP FUNCTION const_func();
  321. -- only accept valid partitioning strategy
  322. CREATE TABLE partitioned (
  323. a int
  324. ) PARTITION BY MAGIC (a);
  325. -- specified column must be present in the table
  326. CREATE TABLE partitioned (
  327. a int
  328. ) PARTITION BY RANGE (b);
  329. -- cannot use system columns in partition key
  330. CREATE TABLE partitioned (
  331. a int
  332. ) PARTITION BY RANGE (xmin);
  333. -- cannot use pseudotypes
  334. CREATE TABLE partitioned (
  335. a int,
  336. b int
  337. ) PARTITION BY RANGE (((a, b)));
  338. CREATE TABLE partitioned (
  339. a int,
  340. b int
  341. ) PARTITION BY RANGE (a, ('unknown'));
  342. -- functions in key must be immutable
  343. CREATE FUNCTION immut_func (a int) RETURNS int AS $$ SELECT a + random()::int; $$ LANGUAGE SQL;
  344. CREATE TABLE partitioned (
  345. a int
  346. ) PARTITION BY RANGE (immut_func(a));
  347. DROP FUNCTION immut_func(int);
  348. -- prevent using columns of unsupported types in key (type must have a btree operator class)
  349. CREATE TABLE partitioned (
  350. a point
  351. ) PARTITION BY LIST (a);
  352. CREATE TABLE partitioned (
  353. a point
  354. ) PARTITION BY LIST (a point_ops);
  355. CREATE TABLE partitioned (
  356. a point
  357. ) PARTITION BY RANGE (a);
  358. CREATE TABLE partitioned (
  359. a point
  360. ) PARTITION BY RANGE (a point_ops);
  361. -- cannot add NO INHERIT constraints to partitioned tables
  362. CREATE TABLE partitioned (
  363. a int,
  364. CONSTRAINT check_a CHECK (a > 0) NO INHERIT
  365. ) PARTITION BY RANGE (a);
  366. -- some checks after successful creation of a partitioned table
  367. CREATE FUNCTION plusone(a int) RETURNS INT AS $$ SELECT a+1; $$ LANGUAGE SQL;
  368. CREATE TABLE partitioned (
  369. a int,
  370. b int,
  371. c text,
  372. d text
  373. ) PARTITION BY RANGE (a oid_ops, plusone(b), c collate "default", d collate "C");
  374. -- check relkind
  375. SELECT relkind FROM pg_class WHERE relname = 'partitioned';
  376. -- prevent a function referenced in partition key from being dropped
  377. DROP FUNCTION plusone(int);
  378. -- partitioned table cannot participate in regular inheritance
  379. CREATE TABLE partitioned2 (
  380. a int,
  381. b text
  382. ) PARTITION BY RANGE ((a+1), substr(b, 1, 5));
  383. CREATE TABLE fail () INHERITS (partitioned2);
  384. -- Partition key in describe output
  385. \d partitioned
  386. \d+ partitioned2
  387. INSERT INTO partitioned2 VALUES (1, 'hello');
  388. CREATE TABLE part2_1 PARTITION OF partitioned2 FOR VALUES FROM (-1, 'aaaaa') TO (100, 'ccccc');
  389. \d+ part2_1
  390. DROP TABLE partitioned, partitioned2;
  391. -- check reference to partitioned table's rowtype in partition descriptor
  392. create table partitioned (a int, b int)
  393. partition by list ((row(a, b)::partitioned));
  394. create table partitioned1
  395. partition of partitioned for values in ('(1,2)'::partitioned);
  396. create table partitioned2
  397. partition of partitioned for values in ('(2,4)'::partitioned);
  398. explain (costs off)
  399. select * from partitioned where row(a,b)::partitioned = '(1,2)'::partitioned;
  400. drop table partitioned;
  401. -- whole-row Var in partition key works too
  402. create table partitioned (a int, b int)
  403. partition by list ((partitioned));
  404. create table partitioned1
  405. partition of partitioned for values in ('(1,2)');
  406. create table partitioned2
  407. partition of partitioned for values in ('(2,4)');
  408. explain (costs off)
  409. select * from partitioned where partitioned = '(1,2)'::partitioned;
  410. \d+ partitioned1
  411. drop table partitioned;
  412. -- check that dependencies of partition columns are handled correctly
  413. create domain intdom1 as int;
  414. create table partitioned (
  415. a intdom1,
  416. b text
  417. ) partition by range (a);
  418. alter table partitioned drop column a; -- fail
  419. drop domain intdom1; -- fail, requires cascade
  420. drop domain intdom1 cascade;
  421. table partitioned; -- gone
  422. -- likewise for columns used in partition expressions
  423. create domain intdom1 as int;
  424. create table partitioned (
  425. a intdom1,
  426. b text
  427. ) partition by range (plusone(a));
  428. alter table partitioned drop column a; -- fail
  429. drop domain intdom1; -- fail, requires cascade
  430. drop domain intdom1 cascade;
  431. table partitioned; -- gone
  432. --
  433. -- Partitions
  434. --
  435. -- check partition bound syntax
  436. CREATE TABLE list_parted (
  437. a int
  438. ) PARTITION BY LIST (a);
  439. CREATE TABLE part_p1 PARTITION OF list_parted FOR VALUES IN ('1');
  440. CREATE TABLE part_p2 PARTITION OF list_parted FOR VALUES IN (2);
  441. CREATE TABLE part_p3 PARTITION OF list_parted FOR VALUES IN ((2+1));
  442. CREATE TABLE part_null PARTITION OF list_parted FOR VALUES IN (null);
  443. \d+ list_parted
  444. -- forbidden expressions for partition bound with list partitioned table
  445. CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (somename);
  446. CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (somename.somename);
  447. CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (a);
  448. CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (sum(a));
  449. CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (sum(somename));
  450. CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (sum(1));
  451. CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN ((select 1));
  452. CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN (generate_series(4, 6));
  453. CREATE TABLE part_bogus_expr_fail PARTITION OF list_parted FOR VALUES IN ((1+1) collate "POSIX");
  454. -- syntax does not allow empty list of values for list partitions
  455. CREATE TABLE fail_part PARTITION OF list_parted FOR VALUES IN ();
  456. -- trying to specify range for list partitioned table
  457. CREATE TABLE fail_part PARTITION OF list_parted FOR VALUES FROM (1) TO (2);
  458. -- trying to specify modulus and remainder for list partitioned table
  459. CREATE TABLE fail_part PARTITION OF list_parted FOR VALUES WITH (MODULUS 10, REMAINDER 1);
  460. -- check default partition cannot be created more than once
  461. CREATE TABLE part_default PARTITION OF list_parted DEFAULT;
  462. CREATE TABLE fail_default_part PARTITION OF list_parted DEFAULT;
  463. -- specified literal can't be cast to the partition column data type
  464. CREATE TABLE bools (
  465. a bool
  466. ) PARTITION BY LIST (a);
  467. CREATE TABLE bools_true PARTITION OF bools FOR VALUES IN (1);
  468. DROP TABLE bools;
  469. -- specified literal can be cast, and the cast might not be immutable
  470. CREATE TABLE moneyp (
  471. a money
  472. ) PARTITION BY LIST (a);
  473. CREATE TABLE moneyp_10 PARTITION OF moneyp FOR VALUES IN (10);
  474. CREATE TABLE moneyp_11 PARTITION OF moneyp FOR VALUES IN ('11');
  475. CREATE TABLE moneyp_12 PARTITION OF moneyp FOR VALUES IN (to_char(12, '99')::int);
  476. DROP TABLE moneyp;
  477. -- cast is immutable
  478. CREATE TABLE bigintp (
  479. a bigint
  480. ) PARTITION BY LIST (a);
  481. CREATE TABLE bigintp_10 PARTITION OF bigintp FOR VALUES IN (10);
  482. -- fails due to overlap:
  483. CREATE TABLE bigintp_10_2 PARTITION OF bigintp FOR VALUES IN ('10');
  484. DROP TABLE bigintp;
  485. CREATE TABLE range_parted (
  486. a date
  487. ) PARTITION BY RANGE (a);
  488. -- forbidden expressions for partition bounds with range partitioned table
  489. CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted
  490. FOR VALUES FROM (somename) TO ('2019-01-01');
  491. CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted
  492. FOR VALUES FROM (somename.somename) TO ('2019-01-01');
  493. CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted
  494. FOR VALUES FROM (a) TO ('2019-01-01');
  495. CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted
  496. FOR VALUES FROM (max(a)) TO ('2019-01-01');
  497. CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted
  498. FOR VALUES FROM (max(somename)) TO ('2019-01-01');
  499. CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted
  500. FOR VALUES FROM (max('2019-02-01'::date)) TO ('2019-01-01');
  501. CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted
  502. FOR VALUES FROM ((select 1)) TO ('2019-01-01');
  503. CREATE TABLE part_bogus_expr_fail PARTITION OF range_parted
  504. FOR VALUES FROM (generate_series(1, 3)) TO ('2019-01-01');
  505. -- trying to specify list for range partitioned table
  506. CREATE TABLE fail_part PARTITION OF range_parted FOR VALUES IN ('a');
  507. -- trying to specify modulus and remainder for range partitioned table
  508. CREATE TABLE fail_part PARTITION OF range_parted FOR VALUES WITH (MODULUS 10, REMAINDER 1);
  509. -- each of start and end bounds must have same number of values as the
  510. -- length of the partition key
  511. CREATE TABLE fail_part PARTITION OF range_parted FOR VALUES FROM ('a', 1) TO ('z');
  512. CREATE TABLE fail_part PARTITION OF range_parted FOR VALUES FROM ('a') TO ('z', 1);
  513. -- cannot specify null values in range bounds
  514. CREATE TABLE fail_part PARTITION OF range_parted FOR VALUES FROM (null) TO (maxvalue);
  515. -- trying to specify modulus and remainder for range partitioned table
  516. CREATE TABLE fail_part PARTITION OF range_parted FOR VALUES WITH (MODULUS 10, REMAINDER 1);
  517. -- check partition bound syntax for the hash partition
  518. CREATE TABLE hash_parted (
  519. a int
  520. ) PARTITION BY HASH (a);
  521. CREATE TABLE hpart_1 PARTITION OF hash_parted FOR VALUES WITH (MODULUS 10, REMAINDER 0);
  522. CREATE TABLE hpart_2 PARTITION OF hash_parted FOR VALUES WITH (MODULUS 50, REMAINDER 1);
  523. CREATE TABLE hpart_3 PARTITION OF hash_parted FOR VALUES WITH (MODULUS 200, REMAINDER 2);
  524. CREATE TABLE hpart_4 PARTITION OF hash_parted FOR VALUES WITH (MODULUS 10, REMAINDER 3);
  525. -- modulus 25 is factor of modulus of 50 but 10 is not a factor of 25.
  526. CREATE TABLE fail_part PARTITION OF hash_parted FOR VALUES WITH (MODULUS 25, REMAINDER 3);
  527. -- previous modulus 50 is factor of 150 but this modulus is not a factor of next modulus 200.
  528. CREATE TABLE fail_part PARTITION OF hash_parted FOR VALUES WITH (MODULUS 150, REMAINDER 3);
  529. -- overlapping remainders
  530. CREATE TABLE fail_part PARTITION OF hash_parted FOR VALUES WITH (MODULUS 100, REMAINDER 3);
  531. -- trying to specify range for the hash partitioned table
  532. CREATE TABLE fail_part PARTITION OF hash_parted FOR VALUES FROM ('a', 1) TO ('z');
  533. -- trying to specify list value for the hash partitioned table
  534. CREATE TABLE fail_part PARTITION OF hash_parted FOR VALUES IN (1000);
  535. -- trying to create default partition for the hash partitioned table
  536. CREATE TABLE fail_default_part PARTITION OF hash_parted DEFAULT;
  537. -- check if compatible with the specified parent
  538. -- cannot create as partition of a non-partitioned table
  539. CREATE TABLE unparted (
  540. a int
  541. );
  542. CREATE TABLE fail_part PARTITION OF unparted FOR VALUES IN ('a');
  543. CREATE TABLE fail_part PARTITION OF unparted FOR VALUES WITH (MODULUS 2, REMAINDER 1);
  544. DROP TABLE unparted;
  545. -- cannot create a permanent rel as partition of a temp rel
  546. CREATE TEMP TABLE temp_parted (
  547. a int
  548. ) PARTITION BY LIST (a);
  549. CREATE TABLE fail_part PARTITION OF temp_parted FOR VALUES IN ('a');
  550. DROP TABLE temp_parted;
  551. -- check for partition bound overlap and other invalid specifications
  552. CREATE TABLE list_parted2 (
  553. a varchar
  554. ) PARTITION BY LIST (a);
  555. CREATE TABLE part_null_z PARTITION OF list_parted2 FOR VALUES IN (null, 'z');
  556. CREATE TABLE part_ab PARTITION OF list_parted2 FOR VALUES IN ('a', 'b');
  557. CREATE TABLE list_parted2_def PARTITION OF list_parted2 DEFAULT;
  558. CREATE TABLE fail_part PARTITION OF list_parted2 FOR VALUES IN (null);
  559. CREATE TABLE fail_part PARTITION OF list_parted2 FOR VALUES IN ('b', 'c');
  560. -- check default partition overlap
  561. INSERT INTO list_parted2 VALUES('X');
  562. CREATE TABLE fail_part PARTITION OF list_parted2 FOR VALUES IN ('W', 'X', 'Y');
  563. CREATE TABLE range_parted2 (
  564. a int
  565. ) PARTITION BY RANGE (a);
  566. -- trying to create range partition with empty range
  567. CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (1) TO (0);
  568. -- note that the range '[1, 1)' has no elements
  569. CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (1) TO (1);
  570. CREATE TABLE part0 PARTITION OF range_parted2 FOR VALUES FROM (minvalue) TO (1);
  571. CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (minvalue) TO (2);
  572. CREATE TABLE part1 PARTITION OF range_parted2 FOR VALUES FROM (1) TO (10);
  573. CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (-1) TO (1);
  574. CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (9) TO (maxvalue);
  575. CREATE TABLE part2 PARTITION OF range_parted2 FOR VALUES FROM (20) TO (30);
  576. CREATE TABLE part3 PARTITION OF range_parted2 FOR VALUES FROM (30) TO (40);
  577. CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (10) TO (30);
  578. CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (10) TO (50);
  579. -- Create a default partition for range partitioned table
  580. CREATE TABLE range2_default PARTITION OF range_parted2 DEFAULT;
  581. -- More than one default partition is not allowed, so this should give error
  582. CREATE TABLE fail_default_part PARTITION OF range_parted2 DEFAULT;
  583. -- Check if the range for default partitions overlap
  584. INSERT INTO range_parted2 VALUES (85);
  585. CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (80) TO (90);
  586. CREATE TABLE part4 PARTITION OF range_parted2 FOR VALUES FROM (90) TO (100);
  587. -- now check for multi-column range partition key
  588. CREATE TABLE range_parted3 (
  589. a int,
  590. b int
  591. ) PARTITION BY RANGE (a, (b+1));
  592. CREATE TABLE part00 PARTITION OF range_parted3 FOR VALUES FROM (0, minvalue) TO (0, maxvalue);
  593. CREATE TABLE fail_part PARTITION OF range_parted3 FOR VALUES FROM (0, minvalue) TO (0, 1);
  594. CREATE TABLE part10 PARTITION OF range_parted3 FOR VALUES FROM (1, minvalue) TO (1, 1);
  595. CREATE TABLE part11 PARTITION OF range_parted3 FOR VALUES FROM (1, 1) TO (1, 10);
  596. CREATE TABLE part12 PARTITION OF range_parted3 FOR VALUES FROM (1, 10) TO (1, maxvalue);
  597. CREATE TABLE fail_part PARTITION OF range_parted3 FOR VALUES FROM (1, 10) TO (1, 20);
  598. CREATE TABLE range3_default PARTITION OF range_parted3 DEFAULT;
  599. -- cannot create a partition that says column b is allowed to range
  600. -- from -infinity to +infinity, while there exist partitions that have
  601. -- more specific ranges
  602. CREATE TABLE fail_part PARTITION OF range_parted3 FOR VALUES FROM (1, minvalue) TO (1, maxvalue);
  603. -- check for partition bound overlap and other invalid specifications for the hash partition
  604. CREATE TABLE hash_parted2 (
  605. a varchar
  606. ) PARTITION BY HASH (a);
  607. CREATE TABLE h2part_1 PARTITION OF hash_parted2 FOR VALUES WITH (MODULUS 4, REMAINDER 2);
  608. CREATE TABLE h2part_2 PARTITION OF hash_parted2 FOR VALUES WITH (MODULUS 8, REMAINDER 0);
  609. CREATE TABLE h2part_3 PARTITION OF hash_parted2 FOR VALUES WITH (MODULUS 8, REMAINDER 4);
  610. CREATE TABLE h2part_4 PARTITION OF hash_parted2 FOR VALUES WITH (MODULUS 8, REMAINDER 5);
  611. -- overlap with part_4
  612. CREATE TABLE fail_part PARTITION OF hash_parted2 FOR VALUES WITH (MODULUS 2, REMAINDER 1);
  613. -- modulus must be greater than zero
  614. CREATE TABLE fail_part PARTITION OF hash_parted2 FOR VALUES WITH (MODULUS 0, REMAINDER 1);
  615. -- remainder must be greater than or equal to zero and less than modulus
  616. CREATE TABLE fail_part PARTITION OF hash_parted2 FOR VALUES WITH (MODULUS 8, REMAINDER 8);
  617. -- check schema propagation from parent
  618. CREATE TABLE parted (
  619. a text,
  620. b int NOT NULL DEFAULT 0,
  621. CONSTRAINT check_a CHECK (length(a) > 0)
  622. ) PARTITION BY LIST (a);
  623. CREATE TABLE part_a PARTITION OF parted FOR VALUES IN ('a');
  624. -- only inherited attributes (never local ones)
  625. SELECT attname, attislocal, attinhcount FROM pg_attribute
  626. WHERE attrelid = 'part_a'::regclass and attnum > 0
  627. ORDER BY attnum;
  628. -- able to specify column default, column constraint, and table constraint
  629. -- first check the "column specified more than once" error
  630. CREATE TABLE part_b PARTITION OF parted (
  631. b NOT NULL,
  632. b DEFAULT 1,
  633. b CHECK (b >= 0),
  634. CONSTRAINT check_a CHECK (length(a) > 0)
  635. ) FOR VALUES IN ('b');
  636. CREATE TABLE part_b PARTITION OF parted (
  637. b NOT NULL DEFAULT 1,
  638. CONSTRAINT check_a CHECK (length(a) > 0),
  639. CONSTRAINT check_b CHECK (b >= 0)
  640. ) FOR VALUES IN ('b');
  641. -- conislocal should be false for any merged constraints, true otherwise
  642. SELECT conislocal, coninhcount FROM pg_constraint WHERE conrelid = 'part_b'::regclass ORDER BY conislocal, coninhcount;
  643. -- Once check_b is added to the parent, it should be made non-local for part_b
  644. ALTER TABLE parted ADD CONSTRAINT check_b CHECK (b >= 0);
  645. SELECT conislocal, coninhcount FROM pg_constraint WHERE conrelid = 'part_b'::regclass;
  646. -- Neither check_a nor check_b are droppable from part_b
  647. ALTER TABLE part_b DROP CONSTRAINT check_a;
  648. ALTER TABLE part_b DROP CONSTRAINT check_b;
  649. -- And dropping it from parted should leave no trace of them on part_b, unlike
  650. -- traditional inheritance where they will be left behind, because they would
  651. -- be local constraints.
  652. ALTER TABLE parted DROP CONSTRAINT check_a, DROP CONSTRAINT check_b;
  653. SELECT conislocal, coninhcount FROM pg_constraint WHERE conrelid = 'part_b'::regclass;
  654. -- specify PARTITION BY for a partition
  655. CREATE TABLE fail_part_col_not_found PARTITION OF parted FOR VALUES IN ('c') PARTITION BY RANGE (c);
  656. CREATE TABLE part_c PARTITION OF parted (b WITH OPTIONS NOT NULL DEFAULT 0) FOR VALUES IN ('c') PARTITION BY RANGE ((b));
  657. -- create a level-2 partition
  658. CREATE TABLE part_c_1_10 PARTITION OF part_c FOR VALUES FROM (1) TO (10);
  659. -- check that NOT NULL and default value are inherited correctly
  660. create table parted_notnull_inh_test (a int default 1, b int not null default 0) partition by list (a);
  661. create table parted_notnull_inh_test1 partition of parted_notnull_inh_test (a not null, b default 1) for values in (1);
  662. insert into parted_notnull_inh_test (b) values (null);
  663. -- note that while b's default is overriden, a's default is preserved
  664. \d parted_notnull_inh_test1
  665. drop table parted_notnull_inh_test;
  666. -- check that collations are assigned in partition bound expressions
  667. create table parted_boolean_col (a bool, b text) partition by list(a);
  668. create table parted_boolean_less partition of parted_boolean_col
  669. for values in ('foo' < 'bar');
  670. create table parted_boolean_greater partition of parted_boolean_col
  671. for values in ('foo' > 'bar');
  672. drop table parted_boolean_col;
  673. -- check for a conflicting COLLATE clause
  674. create table parted_collate_must_match (a text collate "C", b text collate "C")
  675. partition by range (a);
  676. -- on the partition key
  677. create table parted_collate_must_match1 partition of parted_collate_must_match
  678. (a collate "POSIX") for values from ('a') to ('m');
  679. -- on another column
  680. create table parted_collate_must_match2 partition of parted_collate_must_match
  681. (b collate "POSIX") for values from ('m') to ('z');
  682. drop table parted_collate_must_match;
  683. -- check that non-matching collations for partition bound
  684. -- expressions are coerced to the right collation
  685. create table test_part_coll_posix (a text) partition by range (a collate "POSIX");
  686. -- ok, collation is implicitly coerced
  687. create table test_part_coll partition of test_part_coll_posix for values from ('a' collate "C") to ('g');
  688. -- ok
  689. create table test_part_coll2 partition of test_part_coll_posix for values from ('g') to ('m');
  690. -- ok, collation is implicitly coerced
  691. create table test_part_coll_cast partition of test_part_coll_posix for values from (name 'm' collate "C") to ('s');
  692. -- ok; partition collation silently overrides the default collation of type 'name'
  693. create table test_part_coll_cast2 partition of test_part_coll_posix for values from (name 's') to ('z');
  694. drop table test_part_coll_posix;
  695. -- Partition bound in describe output
  696. \d+ part_b
  697. -- Both partition bound and partition key in describe output
  698. \d+ part_c
  699. -- a level-2 partition's constraint will include the parent's expressions
  700. \d+ part_c_1_10
  701. -- Show partition count in the parent's describe output
  702. -- Tempted to include \d+ output listing partitions with bound info but
  703. -- output could vary depending on the order in which partition oids are
  704. -- returned.
  705. \d parted
  706. \d hash_parted
  707. -- check that we get the expected partition constraints
  708. CREATE TABLE range_parted4 (a int, b int, c int) PARTITION BY RANGE (abs(a), abs(b), c);
  709. CREATE TABLE unbounded_range_part PARTITION OF range_parted4 FOR VALUES FROM (MINVALUE, MINVALUE, MINVALUE) TO (MAXVALUE, MAXVALUE, MAXVALUE);
  710. \d+ unbounded_range_part
  711. DROP TABLE unbounded_range_part;
  712. CREATE TABLE range_parted4_1 PARTITION OF range_parted4 FOR VALUES FROM (MINVALUE, MINVALUE, MINVALUE) TO (1, MAXVALUE, MAXVALUE);
  713. \d+ range_parted4_1
  714. CREATE TABLE range_parted4_2 PARTITION OF range_parted4 FOR VALUES FROM (3, 4, 5) TO (6, 7, MAXVALUE);
  715. \d+ range_parted4_2
  716. CREATE TABLE range_parted4_3 PARTITION OF range_parted4 FOR VALUES FROM (6, 8, MINVALUE) TO (9, MAXVALUE, MAXVALUE);
  717. \d+ range_parted4_3
  718. DROP TABLE range_parted4;
  719. -- user-defined operator class in partition key
  720. CREATE FUNCTION my_int4_sort(int4,int4) RETURNS int LANGUAGE sql
  721. AS $$ SELECT CASE WHEN $1 = $2 THEN 0 WHEN $1 > $2 THEN 1 ELSE -1 END; $$;
  722. CREATE OPERATOR CLASS test_int4_ops FOR TYPE int4 USING btree AS
  723. OPERATOR 1 < (int4,int4), OPERATOR 2 <= (int4,int4),
  724. OPERATOR 3 = (int4,int4), OPERATOR 4 >= (int4,int4),
  725. OPERATOR 5 > (int4,int4), FUNCTION 1 my_int4_sort(int4,int4);
  726. CREATE TABLE partkey_t (a int4) PARTITION BY RANGE (a test_int4_ops);
  727. CREATE TABLE partkey_t_1 PARTITION OF partkey_t FOR VALUES FROM (0) TO (1000);
  728. INSERT INTO partkey_t VALUES (100);
  729. INSERT INTO partkey_t VALUES (200);
  730. -- cleanup
  731. DROP TABLE parted, list_parted, range_parted, list_parted2, range_parted2, range_parted3;
  732. DROP TABLE partkey_t, hash_parted, hash_parted2;
  733. DROP OPERATOR CLASS test_int4_ops USING btree;
  734. DROP FUNCTION my_int4_sort(int4,int4);
  735. -- comments on partitioned tables columns
  736. CREATE TABLE parted_col_comment (a int, b text) PARTITION BY LIST (a);
  737. COMMENT ON TABLE parted_col_comment IS 'Am partitioned table';
  738. COMMENT ON COLUMN parted_col_comment.a IS 'Partition key';
  739. SELECT obj_description('parted_col_comment'::regclass);
  740. \d+ parted_col_comment
  741. DROP TABLE parted_col_comment;
  742. -- list partitioning on array type column
  743. CREATE TABLE arrlp (a int[]) PARTITION BY LIST (a);
  744. CREATE TABLE arrlp12 PARTITION OF arrlp FOR VALUES IN ('{1}', '{2}');
  745. \d+ arrlp12
  746. DROP TABLE arrlp;
  747. -- partition on boolean column
  748. create table boolspart (a bool) partition by list (a);
  749. create table boolspart_t partition of boolspart for values in (true);
  750. create table boolspart_f partition of boolspart for values in (false);
  751. \d+ boolspart
  752. drop table boolspart;
  753. -- partitions mixing temporary and permanent relations
  754. create table perm_parted (a int) partition by list (a);
  755. create temporary table temp_parted (a int) partition by list (a);
  756. create table perm_part partition of temp_parted default; -- error
  757. create temp table temp_part partition of perm_parted default; -- error
  758. create temp table temp_part partition of temp_parted default; -- ok
  759. drop table perm_parted cascade;
  760. drop table temp_parted cascade;
  761. -- check that adding partitions to a table while it is being used is prevented
  762. create table tab_part_create (a int) partition by list (a);
  763. create or replace function func_part_create() returns trigger
  764. language plpgsql as $$
  765. begin
  766. execute 'create table tab_part_create_1 partition of tab_part_create for values in (1)';
  767. return null;
  768. end $$;
  769. create trigger trig_part_create before insert on tab_part_create
  770. for each statement execute procedure func_part_create();
  771. insert into tab_part_create values (1);
  772. drop table tab_part_create;
  773. drop function func_part_create();
  774. -- test using a volatile expression as partition bound
  775. create table volatile_partbound_test (partkey timestamp) partition by range (partkey);
  776. create table volatile_partbound_test1 partition of volatile_partbound_test for values from (minvalue) to (current_timestamp);
  777. create table volatile_partbound_test2 partition of volatile_partbound_test for values from (current_timestamp) to (maxvalue);
  778. -- this should go into the partition volatile_partbound_test2
  779. insert into volatile_partbound_test values (current_timestamp);
  780. select tableoid::regclass from volatile_partbound_test;
  781. drop table volatile_partbound_test;
  782. -- test the case where a check constraint on default partition allows
  783. -- to avoid scanning it when adding a new partition
  784. create table defcheck (a int, b int) partition by list (b);
  785. create table defcheck_def (a int, c int, b int);
  786. alter table defcheck_def drop c;
  787. alter table defcheck attach partition defcheck_def default;
  788. alter table defcheck_def add check (b <= 0 and b is not null);
  789. create table defcheck_1 partition of defcheck for values in (1, null);
  790. -- test that complex default partition constraints are enforced correctly
  791. insert into defcheck_def values (0, 0);
  792. create table defcheck_0 partition of defcheck for values in (0);
  793. drop table defcheck;
  794. -- tests of column drop with partition tables and indexes using
  795. -- predicates and expressions.
  796. create table part_column_drop (
  797. useless_1 int,
  798. id int,
  799. useless_2 int,
  800. d int,
  801. b int,
  802. useless_3 int
  803. ) partition by range (id);
  804. alter table part_column_drop drop column useless_1;
  805. alter table part_column_drop drop column useless_2;
  806. alter table part_column_drop drop column useless_3;
  807. create index part_column_drop_b_pred on part_column_drop(b) where b = 1;
  808. create index part_column_drop_b_expr on part_column_drop((b = 1));
  809. create index part_column_drop_d_pred on part_column_drop(d) where d = 2;
  810. create index part_column_drop_d_expr on part_column_drop((d = 2));
  811. create table part_column_drop_1_10 partition of
  812. part_column_drop for values from (1) to (10);
  813. \d part_column_drop
  814. \d part_column_drop_1_10
  815. drop table part_column_drop;