CREATE UNLOGGED TABLE unlogged1 (a intprimarykey); -- OK CREATE TEMPORARY TABLE unlogged2 (a intprimarykey); -- OK SELECT relname, relkind, relpersistence FROM pg_class WHERE relname ~ '^unlogged\d'ORDERBY relname;
REINDEX INDEX unlogged1_pkey;
REINDEX INDEX unlogged2_pkey; SELECT relname, relkind, relpersistence FROM pg_class WHERE relname ~ '^unlogged\d'ORDERBY relname; DROPTABLE unlogged2; INSERTINTO unlogged1 VALUES (42); CREATE UNLOGGED TABLE public.unlogged2 (a intprimarykey); -- also OK CREATE UNLOGGED TABLE pg_temp.unlogged3 (a intprimarykey); -- not OK CREATETABLE pg_temp.implicitly_temp (a intprimarykey); -- OK CREATE TEMP TABLE explicitly_temp (a intprimarykey); -- also OK CREATE TEMP TABLE pg_temp.doubly_temp (a intprimarykey); -- also OK CREATE TEMP TABLE public.temp_to_perm (a intprimarykey); -- not OK DROPTABLE unlogged1, public.unlogged2;
CREATE UNLOGGED TABLE unlogged1 (a int) PARTITION BY RANGE (a); -- fail CREATETABLE unlogged1 (a int) PARTITION BY RANGE (a); -- ok ALTERTABLE unlogged1 SET LOGGED; -- fails ALTERTABLE unlogged1 SET UNLOGGED; -- fails DROPTABLE unlogged1;
CREATETABLE as_select1 ASSELECT * FROM pg_class WHERE relkind = 'r'; CREATETABLE as_select1 ASSELECT * FROM pg_class WHERE relkind = 'r'; CREATETABLEIFNOTEXISTS as_select1 ASSELECT * FROM pg_class WHERE relkind = 'r'; DROPTABLE as_select1;
PREPARE select1 ASSELECT1as a; CREATETABLE as_select1 AS EXECUTE select1; CREATETABLE as_select1 AS EXECUTE select1; SELECT * FROM as_select1; CREATETABLEIFNOTEXISTS as_select1 AS EXECUTE select1; DROPTABLE as_select1;
DEALLOCATE select1;
-- create an extra wide table to test for issues related to that -- (temporarily hide query, to avoid the long CREATE TABLE stmt)
\set ECHO none SELECT'CREATE TABLE extra_wide_table(firstc text, '|| array_to_string(array_agg('c'||i||' bool'),',')||', lastc text);' FROM generate_series(1, 1100) g(i)
\gexec
\set ECHO all INSERTINTO extra_wide_table(firstc, lastc) VALUES('first col', 'last col'); SELECT firstc, lastc FROM extra_wide_table;
-- check that tables with oids cannot be created anymore CREATETABLE withoid() WITH OIDS; CREATETABLE withoid() WITH (oids); CREATETABLE withoid() WITH (oids = true);
-- but explicitly not adding oids is still supported CREATE TEMP TABLE withoutoid() WITHOUT OIDS; DROPTABLE withoutoid; CREATE TEMP TABLE withoutoid() WITH (oids = false); DROPTABLE withoutoid;
-- temporary tables are ignored by pg_filenode_relation(). CREATE TEMP TABLE relation_filenode_check(c1 int); SELECT relpersistence,
pg_filenode_relation (reltablespace, pg_relation_filenode(oid)) FROM pg_class WHERE relname = 'relation_filenode_check'; DROPTABLE relation_filenode_check;
-- check restriction with default expressions -- invalid use of column reference in default expressions CREATETABLE default_expr_column (id intDEFAULT (id)); CREATETABLE default_expr_column (id intDEFAULT (bar.id)); CREATETABLE default_expr_agg_column (id intDEFAULT (avg(id))); -- invalid column definition CREATETABLE default_expr_non_column (a intDEFAULT (avg(non_existent))); -- invalid use of aggregate CREATETABLE default_expr_agg (a intDEFAULT (avg(1))); -- invalid use of subquery CREATETABLE default_expr_agg (a intDEFAULT (select1)); -- invalid use of set-returning function CREATETABLE default_expr_agg (a intDEFAULT (generate_series(1,3)));
-- Verify that subtransaction rollback restores rd_firstRelfilenodeSubid. CREATETABLE remember_node_subid (c int);
BEGIN; ALTERTABLE remember_node_subid ALTER c TYPE bigint;
SAVEPOINT q; DROPTABLE remember_node_subid; ROLLBACK TO q; COMMIT; DROPTABLE remember_node_subid;
-- generated NOT NULL constraint names must not collide with explicitly named constraints CREATETABLE two_not_null_constraints (
col integerNOTNULL, CONSTRAINT two_not_null_constraints_col_not_null CHECK (col ISNOTNULL)
); DROPTABLE two_not_null_constraints;
-- -- Partitioned tables --
-- cannot combine INHERITS and PARTITION BY (although grammar allows) CREATETABLE partitioned (
a int
) INHERITS (some_table) PARTITION BY LIST (a);
-- cannot use more than 1 column as partition key for list partitioned table CREATETABLE partitioned (
a1 int,
a2 int
) PARTITION BY LIST (a1, a2); -- fail
-- prevent using prohibited expressions in the key CREATE FUNCTION retset (a int) RETURNS SETOF intAS $$ SELECT1; $$ LANGUAGE SQL IMMUTABLE; CREATETABLE partitioned (
a int
) PARTITION BY RANGE (retset(a)); DROP FUNCTION retset(int);
CREATETABLE partitioned (
a int
) PARTITION BY RANGE ((avg(a)));
CREATETABLE partitioned (
a int,
b int
) PARTITION BY RANGE ((avg(a) OVER (PARTITION BY b)));
CREATETABLE partitioned (
a int
) PARTITION BY LIST ((a LIKE (SELECT1)));
CREATETABLE partitioned (
a int
) PARTITION BY RANGE ((42));
CREATE FUNCTION const_func () RETURNS intAS $$ SELECT1; $$ LANGUAGE SQL IMMUTABLE; CREATETABLE partitioned (
a int
) PARTITION BY RANGE (const_func()); DROP FUNCTION const_func();
-- only accept valid partitioning strategy CREATETABLE partitioned (
a int
) PARTITION BY MAGIC (a);
-- specified column must be present in the table CREATETABLE partitioned (
a int
) PARTITION BY RANGE (b);
-- cannot use system columns in partition key CREATETABLE partitioned (
a int
) PARTITION BY RANGE (xmin);
-- cannot use pseudotypes CREATETABLE partitioned (
a int,
b int
) PARTITION BY RANGE (((a, b))); CREATETABLE partitioned (
a int,
b int
) PARTITION BY RANGE (a, ('unknown'));
-- functions in key must be immutable CREATE FUNCTION immut_func (a int) RETURNS intAS $$ SELECT a + random()::int; $$ LANGUAGE SQL; CREATETABLE partitioned (
a int
) PARTITION BY RANGE (immut_func(a)); DROP FUNCTION immut_func(int);
-- prevent using columns of unsupported types in key (type must have a btree operator class) CREATETABLE partitioned (
a point
) PARTITION BY LIST (a); CREATETABLE partitioned (
a point
) PARTITION BY LIST (a point_ops); CREATETABLE partitioned (
a point
) PARTITION BY RANGE (a); CREATETABLE partitioned (
a point
) PARTITION BY RANGE (a point_ops);
-- cannot add NO INHERIT constraints to partitioned tables CREATETABLE partitioned (
a int, CONSTRAINT check_a CHECK (a > 0) NO INHERIT
) PARTITION BY RANGE (a);
-- some checks after successful creation of a partitioned table CREATE FUNCTION plusone(a int) RETURNS INTAS $$ SELECT a+1; $$ LANGUAGE SQL;
CREATETABLE partitioned (
a int,
b int,
c text,
d text
) PARTITION BY RANGE (a oid_ops, plusone(b), c collate"default", d collate"C");
-- check relkind SELECT relkind FROM pg_class WHERE relname = 'partitioned';
-- prevent a function referenced in partition key from being dropped DROP FUNCTION plusone(int);
-- partitioned table cannot participate in regular inheritance CREATETABLE partitioned2 (
a int,
b text
) PARTITION BY RANGE ((a+1), substr(b, 1, 5)); CREATETABLE fail () INHERITS (partitioned2);
-- Partition key in describe output
\d partitioned
\d+ partitioned2
INSERTINTO partitioned2 VALUES (1, 'hello'); CREATETABLE part2_1 PARTITION OF partitioned2 FORVALUESFROM (-1, 'aaaaa') TO (100, 'ccccc');
\d+ part2_1
DROPTABLE partitioned, partitioned2;
-- check reference to partitioned table's rowtype in partition descriptor createtable partitioned (a int, b int)
partition by list ((row(a, b)::partitioned)); createtable partitioned1
partition of partitioned forvaluesin ('(1,2)'::partitioned); createtable partitioned2
partition of partitioned forvaluesin ('(2,4)'::partitioned); explain (costs off) select * from partitioned where row(a,b)::partitioned = '(1,2)'::partitioned; droptable partitioned;
-- whole-row Var in partition key works too createtable partitioned (a int, b int)
partition by list ((partitioned)); createtable partitioned1
partition of partitioned forvaluesin ('(1,2)'); createtable partitioned2
partition of partitioned forvaluesin ('(2,4)'); explain (costs off) select * from partitioned where partitioned = '(1,2)'::partitioned;
\d+ partitioned1 droptable partitioned;
-- check that dependencies of partition columns are handled correctly create domain intdom1 asint;
createtable partitioned (
a intdom1,
b text
) partition by range (a);
altertable partitioned dropcolumn a; -- fail
drop domain intdom1; -- fail, requires cascade
drop domain intdom1 cascade;
table partitioned; -- gone
-- likewise for columns used in partition expressions create domain intdom1 asint;
createtable partitioned (
a intdom1,
b text
) partition by range (plusone(a));
altertable partitioned dropcolumn a; -- fail
drop domain intdom1; -- fail, requires cascade
drop domain intdom1 cascade;
table partitioned; -- gone
-- -- Partitions --
-- check partition bound syntax
CREATETABLE list_parted (
a int
) PARTITION BY LIST (a); CREATETABLE part_p1 PARTITION OF list_parted FORVALUESIN ('1'); CREATETABLE part_p2 PARTITION OF list_parted FORVALUESIN (2); CREATETABLE part_p3 PARTITION OF list_parted FORVALUESIN ((2+1)); CREATETABLE part_null PARTITION OF list_parted FORVALUESIN (null);
\d+ list_parted
-- forbidden expressions for partition bound with list partitioned table CREATETABLE part_bogus_expr_fail PARTITION OF list_parted FORVALUESIN (somename); CREATETABLE part_bogus_expr_fail PARTITION OF list_parted FORVALUESIN (somename.somename); CREATETABLE part_bogus_expr_fail PARTITION OF list_parted FORVALUESIN (a); CREATETABLE part_bogus_expr_fail PARTITION OF list_parted FORVALUESIN (sum(a)); CREATETABLE part_bogus_expr_fail PARTITION OF list_parted FORVALUESIN (sum(somename)); CREATETABLE part_bogus_expr_fail PARTITION OF list_parted FORVALUESIN (sum(1)); CREATETABLE part_bogus_expr_fail PARTITION OF list_parted FORVALUESIN ((select1)); CREATETABLE part_bogus_expr_fail PARTITION OF list_parted FORVALUESIN (generate_series(4, 6)); CREATETABLE part_bogus_expr_fail PARTITION OF list_parted FORVALUESIN ((1+1) collate"POSIX");
-- syntax does not allow empty list of values for list partitions CREATETABLE fail_part PARTITION OF list_parted FORVALUESIN (); -- trying to specify range for list partitioned table CREATETABLE fail_part PARTITION OF list_parted FORVALUESFROM (1) TO (2); -- trying to specify modulus and remainder for list partitioned table CREATETABLE fail_part PARTITION OF list_parted FORVALUESWITH (MODULUS 10, REMAINDER 1);
-- check default partition cannot be created more than once CREATETABLE part_default PARTITION OF list_parted DEFAULT; CREATETABLE fail_default_part PARTITION OF list_parted DEFAULT;
-- specified literal can't be cast to the partition column data type CREATETABLE bools (
a bool
) PARTITION BY LIST (a); CREATETABLE bools_true PARTITION OF bools FORVALUESIN (1); DROPTABLE bools;
-- specified literal can be cast, and the cast might not be immutable CREATETABLE moneyp (
a money
) PARTITION BY LIST (a); CREATETABLE moneyp_10 PARTITION OF moneyp FORVALUESIN (10); CREATETABLE moneyp_11 PARTITION OF moneyp FORVALUESIN ('11'); CREATETABLE moneyp_12 PARTITION OF moneyp FORVALUESIN (to_char(12, '99')::int); DROPTABLE moneyp;
-- cast is immutable CREATETABLE bigintp (
a bigint
) PARTITION BY LIST (a); CREATETABLE bigintp_10 PARTITION OF bigintp FORVALUESIN (10); -- fails due to overlap: CREATETABLE bigintp_10_2 PARTITION OF bigintp FORVALUESIN ('10'); DROPTABLE bigintp;
CREATETABLE range_parted (
a date
) PARTITION BY RANGE (a);
-- forbidden expressions for partition bounds with range partitioned table CREATETABLE part_bogus_expr_fail PARTITION OF range_parted FORVALUESFROM (somename) TO ('2019-01-01'); CREATETABLE part_bogus_expr_fail PARTITION OF range_parted FORVALUESFROM (somename.somename) TO ('2019-01-01'); CREATETABLE part_bogus_expr_fail PARTITION OF range_parted FORVALUESFROM (a) TO ('2019-01-01'); CREATETABLE part_bogus_expr_fail PARTITION OF range_parted FORVALUESFROM (max(a)) TO ('2019-01-01'); CREATETABLE part_bogus_expr_fail PARTITION OF range_parted FORVALUESFROM (max(somename)) TO ('2019-01-01'); CREATETABLE part_bogus_expr_fail PARTITION OF range_parted FORVALUESFROM (max('2019-02-01'::date)) TO ('2019-01-01'); CREATETABLE part_bogus_expr_fail PARTITION OF range_parted FORVALUESFROM ((select1)) TO ('2019-01-01'); CREATETABLE part_bogus_expr_fail PARTITION OF range_parted FORVALUESFROM (generate_series(1, 3)) TO ('2019-01-01');
-- trying to specify list for range partitioned table CREATETABLE fail_part PARTITION OF range_parted FORVALUESIN ('a'); -- trying to specify modulus and remainder for range partitioned table CREATETABLE fail_part PARTITION OF range_parted FORVALUESWITH (MODULUS 10, REMAINDER 1); -- each of start and end bounds must have same number of values as the -- length of the partition key CREATETABLE fail_part PARTITION OF range_parted FORVALUESFROM ('a', 1) TO ('z'); CREATETABLE fail_part PARTITION OF range_parted FORVALUESFROM ('a') TO ('z', 1);
-- cannot specify null values in range bounds CREATETABLE fail_part PARTITION OF range_parted FORVALUESFROM (null) TO (maxvalue);
-- trying to specify modulus and remainder for range partitioned table CREATETABLE fail_part PARTITION OF range_parted FORVALUESWITH (MODULUS 10, REMAINDER 1);
-- check partition bound syntax for the hash partition CREATETABLE hash_parted (
a int
) PARTITION BY HASH (a); CREATETABLE hpart_1 PARTITION OF hash_parted FORVALUESWITH (MODULUS 10, REMAINDER 0); CREATETABLE hpart_2 PARTITION OF hash_parted FORVALUESWITH (MODULUS 50, REMAINDER 1); CREATETABLE hpart_3 PARTITION OF hash_parted FORVALUESWITH (MODULUS 200, REMAINDER 2); CREATETABLE hpart_4 PARTITION OF hash_parted FORVALUESWITH (MODULUS 10, REMAINDER 3); -- modulus 25 is factor of modulus of 50 but 10 is not a factor of 25. CREATETABLE fail_part PARTITION OF hash_parted FORVALUESWITH (MODULUS 25, REMAINDER 3); -- previous modulus 50 is factor of 150 but this modulus is not a factor of next modulus 200. CREATETABLE fail_part PARTITION OF hash_parted FORVALUESWITH (MODULUS 150, REMAINDER 3); -- overlapping remainders CREATETABLE fail_part PARTITION OF hash_parted FORVALUESWITH (MODULUS 100, REMAINDER 3); -- trying to specify range for the hash partitioned table CREATETABLE fail_part PARTITION OF hash_parted FORVALUESFROM ('a', 1) TO ('z'); -- trying to specify list value for the hash partitioned table CREATETABLE fail_part PARTITION OF hash_parted FORVALUESIN (1000);
-- trying to create default partition for the hash partitioned table CREATETABLE fail_default_part PARTITION OF hash_parted DEFAULT;
-- check if compatible with the specified parent
-- cannot create as partition of a non-partitioned table CREATETABLE unparted (
a int
); CREATETABLE fail_part PARTITION OF unparted FORVALUESIN ('a'); CREATETABLE fail_part PARTITION OF unparted FORVALUESWITH (MODULUS 2, REMAINDER 1); DROPTABLE unparted;
-- cannot create a permanent rel as partition of a temp rel CREATE TEMP TABLE temp_parted (
a int
) PARTITION BY LIST (a); CREATETABLE fail_part PARTITION OF temp_parted FORVALUESIN ('a'); DROPTABLE temp_parted;
-- check for partition bound overlap and other invalid specifications
CREATETABLE list_parted2 (
a varchar
) PARTITION BY LIST (a); CREATETABLE part_null_z PARTITION OF list_parted2 FORVALUESIN (null, 'z'); CREATETABLE part_ab PARTITION OF list_parted2 FORVALUESIN ('a', 'b'); CREATETABLE list_parted2_def PARTITION OF list_parted2 DEFAULT;
CREATETABLE range_parted2 (
a int
) PARTITION BY RANGE (a);
-- trying to create range partition with empty range CREATETABLE fail_part PARTITION OF range_parted2 FORVALUESFROM (1) TO (0); -- note that the range '[1, 1)' has no elements CREATETABLE fail_part PARTITION OF range_parted2 FORVALUESFROM (1) TO (1);
CREATETABLE part0 PARTITION OF range_parted2 FORVALUESFROM (minvalue) TO (1); CREATETABLE fail_part PARTITION OF range_parted2 FORVALUESFROM (minvalue) TO (2); CREATETABLE part1 PARTITION OF range_parted2 FORVALUESFROM (1) TO (10); CREATETABLE fail_part PARTITION OF range_parted2 FORVALUESFROM (-1) TO (1); CREATETABLE fail_part PARTITION OF range_parted2 FORVALUESFROM (9) TO (maxvalue); CREATETABLE part2 PARTITION OF range_parted2 FORVALUESFROM (20) TO (30); CREATETABLE part3 PARTITION OF range_parted2 FORVALUESFROM (30) TO (40); CREATETABLE fail_part PARTITION OF range_parted2 FORVALUESFROM (10) TO (30); CREATETABLE fail_part PARTITION OF range_parted2 FORVALUESFROM (10) TO (50);
-- Create a default partition for range partitioned table CREATETABLE range2_default PARTITION OF range_parted2 DEFAULT;
-- More than one default partition is not allowed, so this should give error CREATETABLE fail_default_part PARTITION OF range_parted2 DEFAULT;
-- Check if the range for default partitions overlap INSERTINTO range_parted2 VALUES (85); CREATETABLE fail_part PARTITION OF range_parted2 FORVALUESFROM (80) TO (90); CREATETABLE part4 PARTITION OF range_parted2 FORVALUESFROM (90) TO (100);
-- now check for multi-column range partition key CREATETABLE range_parted3 (
a int,
b int
) PARTITION BY RANGE (a, (b+1));
CREATETABLE part00 PARTITION OF range_parted3 FORVALUESFROM (0, minvalue) TO (0, maxvalue); CREATETABLE fail_part PARTITION OF range_parted3 FORVALUESFROM (0, minvalue) TO (0, 1);
CREATETABLE part10 PARTITION OF range_parted3 FORVALUESFROM (1, minvalue) TO (1, 1); CREATETABLE part11 PARTITION OF range_parted3 FORVALUESFROM (1, 1) TO (1, 10); CREATETABLE part12 PARTITION OF range_parted3 FORVALUESFROM (1, 10) TO (1, maxvalue); CREATETABLE fail_part PARTITION OF range_parted3 FORVALUESFROM (1, 10) TO (1, 20); CREATETABLE range3_default PARTITION OF range_parted3 DEFAULT;
-- cannot create a partition that says column b is allowed to range -- from -infinity to +infinity, while there exist partitions that have -- more specific ranges CREATETABLE fail_part PARTITION OF range_parted3 FORVALUESFROM (1, minvalue) TO (1, maxvalue);
-- check for partition bound overlap and other invalid specifications for the hash partition CREATETABLE hash_parted2 (
a varchar
) PARTITION BY HASH (a); CREATETABLE h2part_1 PARTITION OF hash_parted2 FORVALUESWITH (MODULUS 4, REMAINDER 2); CREATETABLE h2part_2 PARTITION OF hash_parted2 FORVALUESWITH (MODULUS 8, REMAINDER 0); CREATETABLE h2part_3 PARTITION OF hash_parted2 FORVALUESWITH (MODULUS 8, REMAINDER 4); CREATETABLE h2part_4 PARTITION OF hash_parted2 FORVALUESWITH (MODULUS 8, REMAINDER 5); -- overlap with part_4 CREATETABLE fail_part PARTITION OF hash_parted2 FORVALUESWITH (MODULUS 2, REMAINDER 1); -- modulus must be greater than zero CREATETABLE fail_part PARTITION OF hash_parted2 FORVALUESWITH (MODULUS 0, REMAINDER 1); -- remainder must be greater than or equal to zero and less than modulus CREATETABLE fail_part PARTITION OF hash_parted2 FORVALUESWITH (MODULUS 8, REMAINDER 8);
-- check schema propagation from parent
CREATETABLE parted (
a text,
b intNOTNULLDEFAULT0, CONSTRAINT check_a CHECK (length(a) > 0)
) PARTITION BY LIST (a);
CREATETABLE part_a PARTITION OF parted FORVALUESIN ('a');
-- only inherited attributes (never local ones) SELECT attname, attislocal, attinhcount FROM pg_attribute WHERE attrelid = 'part_a'::regclass and attnum > 0 ORDERBY attnum;
-- able to specify column default, column constraint, and table constraint
-- first check the "column specified more than once" error CREATETABLE part_b PARTITION OF parted (
b NOTNULL,
b DEFAULT1,
b CHECK (b >= 0), CONSTRAINT check_a CHECK (length(a) > 0)
) FORVALUESIN ('b');
CREATETABLE part_b PARTITION OF parted (
b NOTNULLDEFAULT1, CONSTRAINT check_a CHECK (length(a) > 0), CONSTRAINT check_b CHECK (b >= 0)
) FORVALUESIN ('b'); -- conislocal should be false for any merged constraints, true otherwise SELECT conname, conislocal, coninhcount FROM pg_constraint WHERE conrelid = 'part_b'::regclass ORDERBY coninhcount DESC, conname;
-- Once check_b is added to the parent, it should be made non-local for part_b ALTERTABLE parted ADDCONSTRAINT check_b CHECK (b >= 0); SELECT conname, conislocal, coninhcount FROM pg_constraint WHERE conrelid = 'part_b'::regclass ORDERBY coninhcount DESC, conname;
-- Neither check_a nor check_b are droppable from part_b ALTERTABLE part_b DROPCONSTRAINT check_a; ALTERTABLE part_b DROPCONSTRAINT check_b;
-- And dropping it from parted should leave no trace of them on part_b, unlike -- traditional inheritance where they will be left behind, because they would -- be local constraints. ALTERTABLE parted DROPCONSTRAINT check_a, DROPCONSTRAINT check_b; SELECT conname, conislocal, coninhcount FROM pg_constraint WHERE conrelid = 'part_b'::regclass ORDERBY coninhcount DESC, conname;
-- specify PARTITION BY for a partition CREATETABLE fail_part_col_not_found PARTITION OF parted FORVALUESIN ('c') PARTITION BY RANGE (c); CREATETABLE part_c PARTITION OF parted (b WITH OPTIONS NOTNULLDEFAULT0) FORVALUESIN ('c') PARTITION BY RANGE ((b));
-- create a level-2 partition CREATETABLE part_c_1_10 PARTITION OF part_c FORVALUESFROM (1) TO (10);
-- check that NOT NULL and default value are inherited correctly createtable parted_notnull_inh_test (a intdefault1, b intnotnulldefault0) partition by list (a); createtable parted_notnull_inh_test1 partition of parted_notnull_inh_test (a notnull, b default1) forvaluesin (1); insertinto parted_notnull_inh_test (b) values (null); -- note that while b's default is overridden, a's default is preserved
\d parted_notnull_inh_test1 droptable parted_notnull_inh_test;
-- check that collations are assigned in partition bound expressions createtable parted_boolean_col (a bool, b text) partition by list(a); createtable parted_boolean_less partition of parted_boolean_col forvaluesin ('foo' < 'bar'); createtable parted_boolean_greater partition of parted_boolean_col forvaluesin ('foo' > 'bar'); droptable parted_boolean_col;
-- check for a conflicting COLLATE clause createtable parted_collate_must_match (a text collate"C", b text collate"C")
partition by range (a); -- on the partition key createtable parted_collate_must_match1 partition of parted_collate_must_match
(a collate"POSIX") forvaluesfrom ('a') to ('m'); -- on another column createtable parted_collate_must_match2 partition of parted_collate_must_match
(b collate"POSIX") forvaluesfrom ('m') to ('z'); droptable parted_collate_must_match;
-- check that non-matching collations for partition bound -- expressions are coerced to the right collation
createtable test_part_coll_posix (a text) partition by range (a collate"POSIX"); -- ok, collation is implicitly coerced createtable test_part_coll partition of test_part_coll_posix forvaluesfrom ('a'collate"C") to ('g'); -- ok createtable test_part_coll2 partition of test_part_coll_posix forvaluesfrom ('g') to ('m'); -- ok, collation is implicitly coerced createtable test_part_coll_cast partition of test_part_coll_posix forvaluesfrom (name 'm'collate"C") to ('s'); -- ok; partition collation silently overrides the default collation of type 'name' createtable test_part_coll_cast2 partition of test_part_coll_posix forvaluesfrom (name 's') to ('z');
droptable test_part_coll_posix;
-- Partition bound in describe output
\d+ part_b
-- Both partition bound and partition key in describe output
\d+ part_c
-- a level-2 partition's constraint will include the parent's expressions
\d+ part_c_1_10
-- Show partition count in the parent's describe output -- Tempted to include \d+ output listing partitions with bound info but -- output could vary depending on the order in which partition oids are -- returned.
\d parted
\d hash_parted
-- check that we get the expected partition constraints CREATETABLE range_parted4 (a int, b int, c int) PARTITION BY RANGE (abs(a), abs(b), c); CREATETABLE unbounded_range_part PARTITION OF range_parted4 FORVALUESFROM (MINVALUE, MINVALUE, MINVALUE) TO (MAXVALUE, MAXVALUE, MAXVALUE);
\d+ unbounded_range_part DROPTABLE unbounded_range_part; CREATETABLE range_parted4_1 PARTITION OF range_parted4 FORVALUESFROM (MINVALUE, MINVALUE, MINVALUE) TO (1, MAXVALUE, MAXVALUE);
\d+ range_parted4_1 CREATETABLE range_parted4_2 PARTITION OF range_parted4 FORVALUESFROM (3, 4, 5) TO (6, 7, MAXVALUE);
\d+ range_parted4_2 CREATETABLE range_parted4_3 PARTITION OF range_parted4 FORVALUESFROM (6, 8, MINVALUE) TO (9, MAXVALUE, MAXVALUE);
\d+ range_parted4_3 DROPTABLE range_parted4;
-- user-defined operator class in partition key CREATE FUNCTION my_int4_sort(int4,int4) RETURNS int LANGUAGE sql AS $$ SELECTCASEWHEN $1 = $2THEN0WHEN $1 > $2THEN1ELSE -1 END; $$; CREATE OPERATOR CLASS test_int4_ops FOR TYPE int4USING btree AS
OPERATOR 1 < (int4,int4), OPERATOR 2 <= (int4,int4),
OPERATOR 3 = (int4,int4), OPERATOR 4 >= (int4,int4),
OPERATOR 5 > (int4,int4), FUNCTION 1 my_int4_sort(int4,int4); CREATETABLE partkey_t (a int4) PARTITION BY RANGE (a test_int4_ops); CREATETABLE partkey_t_1 PARTITION OF partkey_t FORVALUESFROM (0) TO (1000); INSERTINTO partkey_t VALUES (100); INSERTINTO partkey_t VALUES (200);
-- cleanup DROPTABLE parted, list_parted, range_parted, list_parted2, range_parted2, range_parted3; DROPTABLE partkey_t, hash_parted, hash_parted2; DROP OPERATOR CLASS test_int4_ops USING btree; DROP FUNCTION my_int4_sort(int4,int4);
-- comments on partitioned tables columns CREATETABLE parted_col_comment (a int, b text) PARTITION BY LIST (a);
COMMENT ONTABLE parted_col_comment IS'Am partitioned table';
COMMENT ONCOLUMN parted_col_comment.a IS'Partition key'; SELECT obj_description('parted_col_comment'::regclass);
\d+ parted_col_comment DROPTABLE parted_col_comment;
-- specifying storage parameters for partitioned tables is not supported CREATETABLE parted_col_comment (a int, b text) PARTITION BY LIST (a) WITH (fillfactor=100);
-- list partitioning on array type column CREATETABLE arrlp (a int[]) PARTITION BY LIST (a); CREATETABLE arrlp12 PARTITION OF arrlp FORVALUESIN ('{1}', '{2}');
\d+ arrlp12 DROPTABLE arrlp;
-- partition on boolean column createtable boolspart (a bool) partition by list (a); createtable boolspart_t partition of boolspart forvaluesin (true); createtable boolspart_f partition of boolspart forvaluesin (false);
\d+ boolspart droptable boolspart;
-- partitions mixing temporary and permanent relations createtable perm_parted (a int) partition by list (a); create temporary table temp_parted (a int) partition by list (a); createtable perm_part partition of temp_parted default; -- error create temp table temp_part partition of perm_parted default; -- error create temp table temp_part partition of temp_parted default; -- ok droptable perm_parted cascade; droptable temp_parted cascade;
-- check that adding partitions to a table while it is being used is prevented createtable tab_part_create (a int) partition by list (a); createorreplace function func_part_create() returns trigger
language plpgsql as $$
begin
execute 'create table tab_part_create_1 partition of tab_part_create for values in (1)'; returnnull;
end $$; createtrigger trig_part_create beforeinserton tab_part_create foreach statement execute procedure func_part_create(); insertinto tab_part_create values (1); droptable tab_part_create; drop function func_part_create();
-- test using a volatile expression as partition bound createtable volatile_partbound_test (partkey timestamp) partition by range (partkey); createtable volatile_partbound_test1 partition of volatile_partbound_test forvaluesfrom (minvalue) to (current_timestamp); createtable volatile_partbound_test2 partition of volatile_partbound_test forvaluesfrom (current_timestamp) to (maxvalue); -- this should go into the partition volatile_partbound_test2 insertinto volatile_partbound_test values (current_timestamp); select tableoid::regclass from volatile_partbound_test; droptable volatile_partbound_test;
-- test the case where a check constraint on default partition allows -- to avoid scanning it when adding a new partition createtable defcheck (a int, b int) partition by list (b); createtable defcheck_def (a int, c int, b int); altertable defcheck_def drop c; altertable defcheck attach partition defcheck_def default; altertable defcheck_def addcheck (b <= 0and b isnotnull); createtable defcheck_1 partition of defcheck forvaluesin (1, null);
-- test that complex default partition constraints are enforced correctly insertinto defcheck_def values (0, 0); createtable defcheck_0 partition of defcheck forvaluesin (0); droptable defcheck;
-- tests of column drop with partition tables and indexes using -- predicates and expressions. createtable part_column_drop (
useless_1 int,
id int,
useless_2 int,
d int,
b int,
useless_3 int
) partition by range (id); altertable part_column_drop dropcolumn useless_1; altertable part_column_drop dropcolumn useless_2; altertable part_column_drop dropcolumn useless_3; createindex part_column_drop_b_pred on part_column_drop(b) where b = 1; createindex part_column_drop_b_expr on part_column_drop((b = 1)); createindex part_column_drop_d_pred on part_column_drop(d) where d = 2; createindex part_column_drop_d_expr on part_column_drop((d = 2)); createtable part_column_drop_1_10 partition of
part_column_drop forvaluesfrom (1) to (10);
\d part_column_drop
\d part_column_drop_1_10 droptable part_column_drop;
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.21 Sekunden
(vorverarbeitet am 2026-08-08)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.