8 CREATE TABLE hobbies_r (
13 CREATE TABLE equipment_r (
86 ) INHERITS (person) WITH OIDS;
89 CREATE TABLE student (
94 CREATE TABLE stud_emp (
96 ) INHERITS (emp, student);
110 CREATE TABLE slow_emp4000 (
114 CREATE TABLE fast_emp4000 (
123 CREATE TABLE ihighway () INHERITS (road);
125 CREATE TABLE shighway (
129 CREATE TABLE real_city (
136 -- test the "star" operators a bit more thoroughly -- this time,
137 -- throw in lots of NULL fields...
139 -- a is the type root
140 -- b and c inherit from a (one-level single inheritance)
141 -- d inherits from b and c (two-level multiple inheritance)
142 -- e inherits from c (two-level single inheritance)
143 -- f inherits from e (three-level single inheritance)
145 CREATE TABLE a_star (
150 CREATE TABLE b_star (
154 CREATE TABLE c_star (
158 CREATE TABLE d_star (
160 ) INHERITS (b_star, c_star);
162 CREATE TABLE e_star (
166 CREATE TABLE f_star (
170 CREATE TABLE aggtest (
175 CREATE TABLE hash_i4_heap (
180 CREATE TABLE hash_name_heap (
185 CREATE TABLE hash_txt_heap (
190 CREATE TABLE hash_f8_heap (
195 -- don't include the hash_ovfl_heap stuff in the distribution
196 -- the data set is too large for what it's worth
198 -- CREATE TABLE hash_ovfl_heap (
203 CREATE TABLE bt_i4_heap (
208 CREATE TABLE bt_name_heap (
213 CREATE TABLE bt_txt_heap (
218 CREATE TABLE bt_f8_heap (
223 CREATE TABLE array_op_test (
229 CREATE TABLE array_index_op_test (
235 CREATE TABLE testjsonb (
239 CREATE TABLE unknowntab (
243 CREATE TYPE unknown_comptype AS (
247 CREATE TABLE IF NOT EXISTS test_tsvector(
252 CREATE TABLE IF NOT EXISTS test_tsvector(
256 CREATE UNLOGGED TABLE unlogged1 (a int primary key); -- OK
257 CREATE TEMPORARY TABLE unlogged2 (a int primary key); -- OK
258 SELECT relname, relkind, relpersistence FROM pg_class WHERE relname ~ '^unlogged\d' ORDER BY relname;
259 REINDEX INDEX unlogged1_pkey;
260 REINDEX INDEX unlogged2_pkey;
261 SELECT relname, relkind, relpersistence FROM pg_class WHERE relname ~ '^unlogged\d' ORDER BY relname;
262 DROP TABLE unlogged2;
263 INSERT INTO unlogged1 VALUES (42);
264 CREATE UNLOGGED TABLE public.unlogged2 (a int primary key); -- also OK
265 CREATE UNLOGGED TABLE pg_temp.unlogged3 (a int primary key); -- not OK
266 CREATE TABLE pg_temp.implicitly_temp (a int primary key); -- OK
267 CREATE TEMP TABLE explicitly_temp (a int primary key); -- also OK
268 CREATE TEMP TABLE pg_temp.doubly_temp (a int primary key); -- also OK
269 CREATE TEMP TABLE public.temp_to_perm (a int primary key); -- not OK
270 DROP TABLE unlogged1, public.unlogged2;
272 CREATE TABLE as_select1 AS SELECT * FROM pg_class WHERE relkind = 'r';
273 CREATE TABLE as_select1 AS SELECT * FROM pg_class WHERE relkind = 'r';
274 CREATE TABLE IF NOT EXISTS as_select1 AS SELECT * FROM pg_class WHERE relkind = 'r';
275 DROP TABLE as_select1;
277 -- check that the oid column is added before the primary key is checked
278 CREATE TABLE oid_pk (f1 INT, PRIMARY KEY(oid)) WITH OIDS;
282 -- Partitioned tables
285 -- cannot combine INHERITS and PARTITION BY (although grammar allows)
286 CREATE TABLE partitioned (
288 ) INHERITS (some_table) PARTITION BY LIST (a);
290 -- cannot use more than 1 column as partition key for list partitioned table
291 CREATE TABLE partitioned (
294 ) PARTITION BY LIST (a1, a2); -- fail
296 -- unsupported constraint type for partitioned tables
297 CREATE TABLE partitioned (
299 ) PARTITION BY RANGE (a);
304 CREATE TABLE partitioned (
305 a int REFERENCES pkrel(a)
306 ) PARTITION BY RANGE (a);
309 CREATE TABLE partitioned (
311 ) PARTITION BY RANGE (a);
313 CREATE TABLE partitioned (
315 EXCLUDE USING gist (a WITH &&)
316 ) PARTITION BY RANGE (a);
318 -- prevent column from being used twice in the partition key
319 CREATE TABLE partitioned (
321 ) PARTITION BY RANGE (a, a);
323 -- prevent using prohibited expressions in the key
324 CREATE FUNCTION retset (a int) RETURNS SETOF int AS $$ SELECT 1; $$ LANGUAGE SQL IMMUTABLE;
325 CREATE TABLE partitioned (
327 ) PARTITION BY RANGE (retset(a));
328 DROP FUNCTION retset(int);
330 CREATE TABLE partitioned (
332 ) PARTITION BY RANGE ((avg(a)));
334 CREATE TABLE partitioned (
337 ) PARTITION BY RANGE ((avg(a) OVER (PARTITION BY b)));
339 CREATE TABLE partitioned (
341 ) PARTITION BY LIST ((a LIKE (SELECT 1)));
343 CREATE TABLE partitioned (
345 ) PARTITION BY RANGE (('a'));
347 CREATE FUNCTION const_func () RETURNS int AS $$ SELECT 1; $$ LANGUAGE SQL IMMUTABLE;
348 CREATE TABLE partitioned (
350 ) PARTITION BY RANGE (const_func());
351 DROP FUNCTION const_func();
353 -- only accept "list" and "range" as partitioning strategy
354 CREATE TABLE partitioned (
356 ) PARTITION BY HASH (a);
358 -- specified column must be present in the table
359 CREATE TABLE partitioned (
361 ) PARTITION BY RANGE (b);
363 -- cannot use system columns in partition key
364 CREATE TABLE partitioned (
366 ) PARTITION BY RANGE (xmin);
368 -- functions in key must be immutable
369 CREATE FUNCTION immut_func (a int) RETURNS int AS $$ SELECT a + random()::int; $$ LANGUAGE SQL;
370 CREATE TABLE partitioned (
372 ) PARTITION BY RANGE (immut_func(a));
373 DROP FUNCTION immut_func(int);
375 -- cannot contain whole-row references
376 CREATE TABLE partitioned (
378 ) PARTITION BY RANGE ((partitioned));
380 -- prevent using columns of unsupported types in key (type must have a btree operator class)
381 CREATE TABLE partitioned (
383 ) PARTITION BY LIST (a);
384 CREATE TABLE partitioned (
386 ) PARTITION BY LIST (a point_ops);
387 CREATE TABLE partitioned (
389 ) PARTITION BY RANGE (a);
390 CREATE TABLE partitioned (
392 ) PARTITION BY RANGE (a point_ops);
394 -- cannot add NO INHERIT constraints to partitioned tables
395 CREATE TABLE partitioned (
397 CONSTRAINT check_a CHECK (a > 0) NO INHERIT
398 ) PARTITION BY RANGE (a);
400 -- some checks after successful creation of a partitioned table
401 CREATE FUNCTION plusone(a int) RETURNS INT AS $$ SELECT a+1; $$ LANGUAGE SQL;
403 CREATE TABLE partitioned (
408 ) PARTITION BY RANGE (a oid_ops, plusone(b), c collate "default", d collate "C");
411 SELECT relkind FROM pg_class WHERE relname = 'partitioned';
413 -- check that range partition key columns are marked NOT NULL
414 SELECT attname, attnotnull FROM pg_attribute
415 WHERE attrelid = 'partitioned'::regclass AND attnum > 0
418 -- prevent a function referenced in partition key from being dropped
419 DROP FUNCTION plusone(int);
421 -- partitioned table cannot participate in regular inheritance
422 CREATE TABLE partitioned2 (
424 ) PARTITION BY LIST ((a+1));
425 CREATE TABLE fail () INHERITS (partitioned2);
427 -- Partition key in describe output
431 DROP TABLE partitioned, partitioned2;
437 -- check partition bound syntax
439 CREATE TABLE list_parted (
441 ) PARTITION BY LIST (a);
442 -- syntax allows only string literal, numeric literal and null to be
443 -- specified for a partition bound value
444 CREATE TABLE part_1 PARTITION OF list_parted FOR VALUES IN ('1');
445 CREATE TABLE part_2 PARTITION OF list_parted FOR VALUES IN (2);
446 CREATE TABLE part_null PARTITION OF list_parted FOR VALUES IN (null);
447 CREATE TABLE fail_part PARTITION OF list_parted FOR VALUES IN (int '1');
448 CREATE TABLE fail_part PARTITION OF list_parted FOR VALUES IN ('1'::int);
450 -- syntax does not allow empty list of values for list partitions
451 CREATE TABLE fail_part PARTITION OF list_parted FOR VALUES IN ();
452 -- trying to specify range for list partitioned table
453 CREATE TABLE fail_part PARTITION OF list_parted FOR VALUES FROM (1) TO (2);
455 -- specified literal can't be cast to the partition column data type
458 ) PARTITION BY LIST (a);
459 CREATE TABLE bools_true PARTITION OF bools FOR VALUES IN (1);
462 CREATE TABLE range_parted (
464 ) PARTITION BY RANGE (a);
466 -- trying to specify list for range partitioned table
467 CREATE TABLE fail_part PARTITION OF range_parted FOR VALUES IN ('a');
468 -- each of start and end bounds must have same number of values as the
469 -- length of the partition key
470 CREATE TABLE fail_part PARTITION OF range_parted FOR VALUES FROM ('a', 1) TO ('z');
471 CREATE TABLE fail_part PARTITION OF range_parted FOR VALUES FROM ('a') TO ('z', 1);
473 -- cannot specify null values in range bounds
474 CREATE TABLE fail_part PARTITION OF range_parted FOR VALUES FROM (null) TO (unbounded);
476 -- check if compatible with the specified parent
478 -- cannot create as partition of a non-partitioned table
479 CREATE TABLE unparted (
482 CREATE TABLE fail_part PARTITION OF unparted FOR VALUES IN ('a');
485 -- cannot create a permanent rel as partition of a temp rel
486 CREATE TEMP TABLE temp_parted (
488 ) PARTITION BY LIST (a);
489 CREATE TABLE fail_part PARTITION OF temp_parted FOR VALUES IN ('a');
490 DROP TABLE temp_parted;
492 -- cannot create a table with oids as partition of table without oids
493 CREATE TABLE no_oids_parted (
495 ) PARTITION BY RANGE (a) WITHOUT OIDS;
496 CREATE TABLE fail_part PARTITION OF no_oids_parted FOR VALUES FROM (1) TO (10) WITH OIDS;
497 DROP TABLE no_oids_parted;
499 -- If the partitioned table has oids, then the partition must have them.
500 -- If the WITHOUT OIDS option is specified for partition, it is overridden.
501 CREATE TABLE oids_parted (
503 ) PARTITION BY RANGE (a) WITH OIDS;
504 CREATE TABLE part_forced_oids PARTITION OF oids_parted FOR VALUES FROM (1) TO (10) WITHOUT OIDS;
506 DROP TABLE oids_parted, part_forced_oids;
508 -- check for partition bound overlap and other invalid specifications
510 CREATE TABLE list_parted2 (
512 ) PARTITION BY LIST (a);
513 CREATE TABLE part_null_z PARTITION OF list_parted2 FOR VALUES IN (null, 'z');
514 CREATE TABLE part_ab PARTITION OF list_parted2 FOR VALUES IN ('a', 'b');
516 CREATE TABLE fail_part PARTITION OF list_parted2 FOR VALUES IN (null);
517 CREATE TABLE fail_part PARTITION OF list_parted2 FOR VALUES IN ('b', 'c');
519 CREATE TABLE range_parted2 (
521 ) PARTITION BY RANGE (a);
523 -- trying to create range partition with empty range
524 CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (1) TO (0);
525 -- note that the range '[1, 1)' has no elements
526 CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (1) TO (1);
528 CREATE TABLE part0 PARTITION OF range_parted2 FOR VALUES FROM (unbounded) TO (1);
529 CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (unbounded) TO (2);
530 CREATE TABLE part1 PARTITION OF range_parted2 FOR VALUES FROM (1) TO (10);
531 CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (9) TO (unbounded);
532 CREATE TABLE part2 PARTITION OF range_parted2 FOR VALUES FROM (20) TO (30);
533 CREATE TABLE part3 PARTITION OF range_parted2 FOR VALUES FROM (30) TO (40);
534 CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (10) TO (30);
535 CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (10) TO (50);
537 -- now check for multi-column range partition key
538 CREATE TABLE range_parted3 (
541 ) PARTITION BY RANGE (a, (b+1));
543 CREATE TABLE part00 PARTITION OF range_parted3 FOR VALUES FROM (0, unbounded) TO (0, unbounded);
544 CREATE TABLE fail_part PARTITION OF range_parted3 FOR VALUES FROM (0, unbounded) TO (0, 1);
546 CREATE TABLE part10 PARTITION OF range_parted3 FOR VALUES FROM (1, unbounded) TO (1, 1);
547 CREATE TABLE part11 PARTITION OF range_parted3 FOR VALUES FROM (1, 1) TO (1, 10);
548 CREATE TABLE part12 PARTITION OF range_parted3 FOR VALUES FROM (1, 10) TO (1, unbounded);
549 CREATE TABLE fail_part PARTITION OF range_parted3 FOR VALUES FROM (1, 10) TO (1, 20);
551 -- cannot create a partition that says column b is allowed to range
552 -- from -infinity to +infinity, while there exist partitions that have
553 -- more specific ranges
554 CREATE TABLE fail_part PARTITION OF range_parted3 FOR VALUES FROM (1, unbounded) TO (1, unbounded);
556 -- check schema propagation from parent
558 CREATE TABLE parted (
560 b int NOT NULL DEFAULT 0,
561 CONSTRAINT check_a CHECK (length(a) > 0)
562 ) PARTITION BY LIST (a);
564 CREATE TABLE part_a PARTITION OF parted FOR VALUES IN ('a');
566 -- only inherited attributes (never local ones)
567 SELECT attname, attislocal, attinhcount FROM pg_attribute
568 WHERE attrelid = 'part_a'::regclass and attnum > 0
571 -- able to specify column default, column constraint, and table constraint
572 CREATE TABLE part_b PARTITION OF parted (
573 b NOT NULL DEFAULT 1 CHECK (b >= 0),
574 CONSTRAINT check_a CHECK (length(a) > 0)
575 ) FOR VALUES IN ('b');
576 -- conislocal should be false for any merged constraints
577 SELECT conislocal, coninhcount FROM pg_constraint WHERE conrelid = 'part_b'::regclass AND conname = 'check_a';
579 -- specify PARTITION BY for a partition
580 CREATE TABLE fail_part_col_not_found PARTITION OF parted FOR VALUES IN ('c') PARTITION BY RANGE (c);
581 CREATE TABLE part_c PARTITION OF parted FOR VALUES IN ('c') PARTITION BY RANGE ((b));
583 -- create a level-2 partition
584 CREATE TABLE part_c_1_10 PARTITION OF part_c FOR VALUES FROM (1) TO (10);
586 -- Partition bound in describe output
589 -- Both partition bound and partition key in describe output
592 -- Show partition count in the parent's describe output
593 -- Tempted to include \d+ output listing partitions with bound info but
594 -- output could vary depending on the order in which partition oids are
599 DROP TABLE parted, list_parted, range_parted, list_parted2, range_parted2, range_parted3;