COPY domarrtest FROM stdin;
{3,4} {q,w,e}
\N \N
\.
COPY domarrtest FROM stdin; -- fail
{3,4} {qwerty,w,e}
\.
select * from domarrtest;
update domarrtest set
testint4arr[1] = testint4arr[1] + 1,
testint4arr[3] = testint4arr[3] - 1 where testchar4arr isnull;
select * from domarrtest where testchar4arr isnull;
droptable domarrtest; drop domain domainint4arr restrict; drop domain domainchar4arr restrict;
create domain dia asint[]; select'{1,2,3}'::dia; select array_dims('{1,2,3}'::dia); select pg_typeof('{1,2,3}'::dia); select pg_typeof('{1,2,3}'::dia || 42); -- should be int[] not dia drop domain dia;
-- Test domains over composites
create type comptype as (r float8, i float8); create domain dcomptype as comptype; createtable dcomptable (d1 dcomptype unique);
create function makedcomp(r float8, i float8) returns dcomptype as'select row(r, i)' language sql;
select makedcomp(1,2); select makedcomp(2,1); -- fail select * from makedcomp(1,2) m; select m, m isnotnullfrom makedcomp(1,2) m;
drop function makedcomp(float8, float8); droptable dcomptable; drop type comptype cascade;
-- check altering and dropping columns used by domain constraints create type comptype as (r float8, i float8); create domain dcomptype as comptype; alter domain dcomptype addconstraint c1 check ((value).r > 0);
comment onconstraint c1 on domain dcomptype is'random commentary';
select row(0,1)::dcomptype; -- fail
alter type comptype alter attribute r type varchar; -- fail alter type comptype alter attribute r type bigint;
alter type comptype drop attribute r; -- fail alter type comptype drop attribute i;
select conname, obj_description(oid, 'pg_constraint') from pg_constraint where contypid = 'dcomptype'::regtype; -- check comment is still there
drop type comptype cascade;
-- Test domains over arrays of composite
create type comptype as (r float8, i float8); create domain dcomptypea as comptype[]; createtable dcomptable (d1 dcomptypea unique);
create domain vc4 asvarchar(4); createtable vc4table (f1 vc4[]); insertinto vc4table values(array['too long']); -- fail insertinto vc4table values(array['too long']::vc4[]); -- cast truncates select * from vc4table; droptable vc4table; drop type vc4;
-- You can sort of fake arrays-of-arrays by putting a domain in between create domain dposinta as posint[]; createtable dposintatable (f1 dposinta[]); insertinto dposintatable values(array[array[42]]); -- fail insertinto dposintatable values(array[array[42]::posint[]]); -- still fail insertinto dposintatable values(array[array[42]::dposinta]); -- but this works select f1, f1[1], (f1[1])[1] from dposintatable; select pg_typeof(f1) from dposintatable; select pg_typeof(f1[1]) from dposintatable; select pg_typeof(f1[1][1]) from dposintatable; select pg_typeof((f1[1])[1]) from dposintatable; update dposintatable set f1[2] = array[99]; select f1, f1[1], (f1[2])[1] from dposintatable; -- it'd be nice if you could do something like this, but for now you can't: update dposintatable set f1[2][1] = array[97]; -- maybe someday we can make this syntax work: update dposintatable set (f1[2])[1] = array[98];
droptable dposintatable; drop domain posint cascade;
-- Test arrays over domains of composite
create type comptype as (cf1 int, cf2 int); create domain dcomptype as comptype check ((value).cf1 > 0);
createtable dcomptable (f1 dcomptype[]); insertinto dcomptable values (null); update dcomptable set f1[1].cf2 = 5; table dcomptable; update dcomptable set f1[1].cf1 = -1; -- fail update dcomptable set f1[1].cf1 = 1; table dcomptable; -- if there's no constraints, a different code path is taken: alter domain dcomptype dropconstraint dcomptype_check; update dcomptable set f1[1].cf1 = -1; -- now ok table dcomptable;
-- Test defaults with copy
COPY defaulttest(col5) FROM stdin; 42
\.
select * from defaulttest;
droptable defaulttest cascade;
-- Test ALTER DOMAIN .. NOT NULL create domain dnotnulltest integer; createtable domnotnull
( col1 dnotnulltest
, col2 dnotnulltest
);
insertinto domnotnull defaultvalues; alter domain dnotnulltest setnotnull; -- fails
update domnotnull set col1 = 5; alter domain dnotnulltest setnotnull; -- fails
update domnotnull set col2 = 6;
alter domain dnotnulltest setnotnull;
update domnotnull set col1 = null; -- fails
alter domain dnotnulltest dropnotnull;
update domnotnull set col1 = null;
update domnotnull set col1 = 5;
-- these constraints can also be added and removed by name alter domain dnotnulltest addconstraint dnotnulltest_notnull notnull; update domnotnull set col1 = null; -- fails select conname, pg_get_constraintdef(oid) from pg_constraint where contypid = 'dnotnulltest'::regtype;
alter domain dnotnulltest dropconstraint dnotnulltest_notnull; update domnotnull set col1 = null;
drop domain dnotnulltest cascade;
-- Test ALTER DOMAIN .. DEFAULT .. createtable domdeftest (col1 ddef1);
insertinto domdeftest defaultvalues; select * from domdeftest;
alter domain ddef1 setdefault'42'; insertinto domdeftest defaultvalues; select * from domdeftest;
alter domain ddef1 dropdefault; insertinto domdeftest defaultvalues; select * from domdeftest;
droptable domdeftest;
-- Test ALTER DOMAIN .. CONSTRAINT .. create domain con asinteger; createtable domcontest (col1 con);
insertinto domcontest values (1); insertinto domcontest values (2); alter domain con addconstraint t check (VALUE < 1); -- fails
alter domain con addconstraint t check (VALUE < 34); alter domain con addcheck (VALUE > 0);
insertinto domconnotnulltest defaultvalues; alter domain connotnull addnotnull; -- fails
update domconnotnulltest set col1 = 5; alter domain connotnull addnotnull; -- fails
update domconnotnulltest set col2 = 6;
alter domain connotnull addconstraint constr1 notnull; select count(*) from pg_constraint where contypid = 'connotnull'::regtype and contype = 'n'; alter domain connotnull addconstraint constr1bis notnull; -- redundant select count(*) from pg_constraint where contypid = 'connotnull'::regtype and contype = 'n';
\dD connotnull
update domconnotnulltest set col1 = null; -- fails
alter domain connotnull dropconstraint constr1;
update domconnotnulltest set col1 = null;
drop domain connotnull cascade; droptable domconnotnulltest;
-- Test ALTER DOMAIN .. CONSTRAINT .. NOT VALID create domain things ASINT; CREATETABLE thethings (stuff things); INSERTINTO thethings (stuff) VALUES (55); ALTER DOMAIN things ADDCONSTRAINT meow CHECK (VALUE < 11); ALTER DOMAIN things ADDCONSTRAINT meow CHECK (VALUE < 11) NOT VALID; ALTER DOMAIN things VALIDATE CONSTRAINT meow; UPDATE thethings SET stuff = 10; ALTER DOMAIN things VALIDATE CONSTRAINT meow;
-- Confirm ALTER DOMAIN with RULES. createtable domtab (col1 integer); create domain dom asinteger; create view domview asselect cast(col1 as dom) from domtab; insertinto domtab (col1) values (null); insertinto domtab (col1) values (5); select * from domview;
alter domain dom setnotnull; select * from domview; -- fail
alter domain dom dropnotnull; select * from domview;
alter domain dom addconstraint domchkgt6 check(value > 6); select * from domview; --fail
alter domain dom dropconstraint domchkgt6 restrict; select * from domview;
-- cleanup drop domain ddef1 restrict; drop domain ddef2 restrict; drop domain ddef3 restrict; drop domain ddef4 restrict; drop domain ddef5 restrict; drop sequence ddef4_seq;
-- Make sure that constraints of newly-added domain columns are -- enforced correctly, even if there's no default value for the new -- column. Per bug #1433 create domain str_domain as text notnull;
create domain str_domain2 as text check (value <> 'foo') default'foo';
-- should fail altertable domain_test addcolumn d str_domain2;
-- Check that domain constraints on prepared statement parameters of -- unknown type are enforced correctly. create domain pos_int asint4check (value > 0) notnull;
prepare s1 asselect $1::pos_int = 10as"is_ten";
execute s1(10);
execute s1(0); -- should fail
execute s1(NULL); -- should fail
-- Check that domain constraints on plpgsql function parameters, results, -- and local variables are enforced correctly.
create function doubledecrement(p1 pos_int) returns pos_int as $$ declare v pos_int;
begin return p1;
end$$ language plpgsql;
select doubledecrement(3); -- fail because of implicit null assignment
createorreplace function doubledecrement(p1 pos_int) returns pos_int as $$ declare v pos_int := 0;
begin return p1;
end$$ language plpgsql;
select doubledecrement(3); -- fail at initialization assignment
createorreplace function doubledecrement(p1 pos_int) returns pos_int as $$ declare v pos_int := 1;
begin
v := p1 - 1; return v - 1;
end$$ language plpgsql;
select doubledecrement(null); -- fail before call select doubledecrement(0); -- fail before call select doubledecrement(1); -- fail at assignment to v select doubledecrement(2); -- fail at return select doubledecrement(3); -- good
-- Check that ALTER DOMAIN tests columns of derived types
create domain posint asint4;
-- Currently, this doesn't work for composite types, but verify it complains create type ddtest1 as (f1 posint); createtable ddtest2(f1 ddtest1); insertinto ddtest2 values(row(-1)); alter domain posint addconstraint c1 check(value >= 0); droptable ddtest2;
-- Likewise for domains within arrays of composite createtable ddtest2(f1 ddtest1[]); insertinto ddtest2 values('{(-1)}'); alter domain posint addconstraint c1 check(value >= 0); droptable ddtest2;
-- Likewise for domains within domains over composite create domain ddtest1d as ddtest1; createtable ddtest2(f1 ddtest1d); insertinto ddtest2 values('(-1)'); alter domain posint addconstraint c1 check(value >= 0); droptable ddtest2; drop domain ddtest1d;
-- Likewise for domains within domains over array of composite create domain ddtest1d as ddtest1[]; createtable ddtest2(f1 ddtest1d); insertinto ddtest2 values('{(-1)}'); alter domain posint addconstraint c1 check(value >= 0); droptable ddtest2; drop domain ddtest1d;
-- Doesn't work for ranges, either create type rposint as range (subtype = posint); createtable ddtest2(f1 rposint); insertinto ddtest2 values('(-1,3]'); alter domain posint addconstraint c1 check(value >= 0); droptable ddtest2; drop type rposint;
alter domain posint addconstraint c1 check(value >= 0);
alter domain posint addconstraint c2 check(value >= 10); -- fail alter domain posint addconstraint c2 check(value > 0); -- OK
droptable ddtest2; drop type ddtest1; drop domain posint cascade;
-- -- Check enforcement of domain-related typmod in plpgsql (bug #5717) --
createorreplace function array_elem_check(numeric) returns numericas $$ declare
x numeric(4,2)[1];
begin
x[1] := $1; return x[1];
end$$ language plpgsql;
create temp table op (f1 orderedpair); insertinto op values (array[1,2]); insertinto op values (array[2,1]); -- fail
update op set f1[2] = 3; update op set f1[2] = 0; -- fail select * from op;
createorreplace function array_elem_check(int) returns intas $$ declare
x orderedpair := '{1,2}';
begin
x[2] := $1; return x[2];
end$$ language plpgsql;
droptable dom_table; drop domain inotnull; drop function sql_is_distinct_from(anyelement, anyelement);
-- -- Renaming --
create domain testdomain1 asint; alter domain testdomain1 renameto testdomain2; alter type testdomain2 renameto testdomain3; -- alter type also works drop domain testdomain3;
-- -- Renaming domain constraints --
create domain testdomain1 asintconstraintunsignedcheck (value > 0); alter domain testdomain1 renameconstraintunsignedto unsigned_foo; alter domain testdomain1 dropconstraint unsigned_foo; drop domain testdomain1;
-- -- Get the base type of a domain -- create domain mytext as text; create domain mytext_child_1 as mytext;
SELECT * FROM information_schema.column_domain_usage WHERE domain_name IN ('con', 'dom', 'pos_int', 'things') ORDERBY domain_name;
SELECT * FROM information_schema.domain_constraints WHERE domain_name IN ('con', 'dom', 'pos_int', 'things') ORDERBY constraint_name;
SELECT * FROM information_schema.domains WHERE domain_name IN ('con', 'dom', 'pos_int', 'things') ORDERBY domain_name;
SELECT * FROM information_schema.check_constraints WHERE (constraint_schema, constraint_name) IN (SELECT constraint_schema, constraint_name FROM information_schema.domain_constraints WHERE domain_name IN ('con', 'dom', 'pos_int', 'things')) ORDERBY constraint_name;
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.17 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.