6 create domain domaindroptest int4;
7 comment on domain domaindroptest is 'About to drop this..';
9 create domain dependenttypetest domaindroptest;
11 -- fail because of dependent type
12 drop domain domaindroptest;
14 drop domain domaindroptest cascade;
16 -- this should fail because already gone
17 drop domain domaindroptest cascade;
22 -- Note: the point of checking both INSERT and COPY FROM is that INSERT
23 -- exercises CoerceToDomain while COPY exercises domain_in.
25 create domain domainvarchar varchar(5);
26 create domain domainnumeric numeric(8,2);
27 create domain domainint4 int4;
28 create domain domaintext text;
30 -- Test explicit coercions --- these should succeed (and truncate)
31 SELECT cast('123456' as domainvarchar);
32 SELECT cast('12345' as domainvarchar);
34 -- Test tables using domains
35 create table basictest
38 , testvarchar domainvarchar
39 , testnumeric domainnumeric
42 INSERT INTO basictest values ('88', 'haha', 'short', '123.12'); -- Good
43 INSERT INTO basictest values ('88', 'haha', 'short text', '123.12'); -- Bad varchar
44 INSERT INTO basictest values ('88', 'haha', 'short', '123.1212'); -- Truncate numeric
47 COPY basictest (testvarchar) FROM stdin; -- fail
51 COPY basictest (testvarchar) FROM stdin;
55 select * from basictest;
57 -- check that domains inherit operations from base types
58 select testtext || testvarchar as concat, testnumeric + 42 as sum
61 -- check that union/case/coalesce type resolution handles domains properly
62 select coalesce(4::domainint4, 7) is of (int4) as t;
63 select coalesce(4::domainint4, 7) is of (domainint4) as f;
64 select coalesce(4::domainint4, 7::domainint4) is of (domainint4) as t;
67 drop domain domainvarchar restrict;
68 drop domain domainnumeric restrict;
69 drop domain domainint4 restrict;
70 drop domain domaintext;
73 -- Test domains over array types
75 create domain domainint4arr int4[1];
76 create domain domainchar4arr varchar(4)[2][3];
78 create table domarrtest
79 ( testint4arr domainint4arr
80 , testchar4arr domainchar4arr
82 INSERT INTO domarrtest values ('{2,2}', '{{"a","b"},{"c","d"}}');
83 INSERT INTO domarrtest values ('{{2,2},{2,2}}', '{{"a","b"}}');
84 INSERT INTO domarrtest values ('{2,2}', '{{"a","b"},{"c","d"},{"e","f"}}');
85 INSERT INTO domarrtest values ('{2,2}', '{{"a"},{"c"}}');
86 INSERT INTO domarrtest values (NULL, '{{"a","b","c"},{"d","e","f"}}');
87 INSERT INTO domarrtest values (NULL, '{{"toolong","b","c"},{"d","e","f"}}');
88 select * from domarrtest;
89 select testint4arr[1], testchar4arr[2:2] from domarrtest;
91 COPY domarrtest FROM stdin;
96 COPY domarrtest FROM stdin; -- fail
100 select * from domarrtest;
102 drop table domarrtest;
103 drop domain domainint4arr restrict;
104 drop domain domainchar4arr restrict;
107 create domain dnotnull varchar(15) NOT NULL;
108 create domain dnull varchar(15);
109 create domain dcheck varchar(15) NOT NULL CHECK (VALUE = 'a' OR VALUE = 'c' OR VALUE = 'd');
111 create table nulltest
113 , col2 dnotnull NULL -- NOT NULL in the domain cannot be overridden
114 , col3 dnull NOT NULL
116 , col5 dcheck CHECK (col5 IN ('c', 'd'))
118 INSERT INTO nulltest DEFAULT VALUES;
119 INSERT INTO nulltest values ('a', 'b', 'c', 'd', 'c'); -- Good
120 insert into nulltest values ('a', 'b', 'c', 'd', NULL);
121 insert into nulltest values ('a', 'b', 'c', 'd', 'a');
122 INSERT INTO nulltest values (NULL, 'b', 'c', 'd', 'd');
123 INSERT INTO nulltest values ('a', NULL, 'c', 'd', 'c');
124 INSERT INTO nulltest values ('a', 'b', NULL, 'd', 'c');
125 INSERT INTO nulltest values ('a', 'b', 'c', NULL, 'd'); -- Good
128 COPY nulltest FROM stdin; --fail
132 COPY nulltest FROM stdin; --fail
137 COPY nulltest FROM stdin;
143 select * from nulltest;
145 -- Test out coerced (casted) constraints
146 SELECT cast('1' as dnotnull);
147 SELECT cast(NULL as dnotnull); -- fail
148 SELECT cast(cast(NULL as dnull) as dnotnull); -- fail
149 SELECT cast(col4 as dnotnull) from nulltest; -- fail
153 drop domain dnotnull restrict;
154 drop domain dnull restrict;
155 drop domain dcheck restrict;
158 create domain ddef1 int4 DEFAULT 3;
159 create domain ddef2 oid DEFAULT '12';
160 -- Type mixing, function returns int8
161 create domain ddef3 text DEFAULT 5;
162 create sequence ddef4_seq;
163 create domain ddef4 int4 DEFAULT nextval('ddef4_seq');
164 create domain ddef5 numeric(8,2) NOT NULL DEFAULT '12.12';
166 create table defaulttest
170 , col4 ddef4 PRIMARY KEY
171 , col5 ddef1 NOT NULL DEFAULT NULL
172 , col6 ddef2 DEFAULT '88'
173 , col7 ddef4 DEFAULT 8000
176 insert into defaulttest(col4) values(0); -- fails, col5 defaults to null
177 alter table defaulttest alter column col5 drop default;
178 insert into defaulttest default values; -- succeeds, inserts domain default
179 -- We used to treat SET DEFAULT NULL as equivalent to DROP DEFAULT; wrong
180 alter table defaulttest alter column col5 set default null;
181 insert into defaulttest(col4) values(0); -- fails
182 alter table defaulttest alter column col5 drop default;
183 insert into defaulttest default values;
184 insert into defaulttest default values;
186 -- Test defaults with copy
187 COPY defaulttest(col5) FROM stdin;
191 select * from defaulttest;
193 drop table defaulttest cascade;
195 -- Test ALTER DOMAIN .. NOT NULL
196 create domain dnotnulltest integer;
197 create table domnotnull
202 insert into domnotnull default values;
203 alter domain dnotnulltest set not null; -- fails
205 update domnotnull set col1 = 5;
206 alter domain dnotnulltest set not null; -- fails
208 update domnotnull set col2 = 6;
210 alter domain dnotnulltest set not null;
212 update domnotnull set col1 = null; -- fails
214 alter domain dnotnulltest drop not null;
216 update domnotnull set col1 = null;
218 drop domain dnotnulltest cascade;
220 -- Test ALTER DOMAIN .. DEFAULT ..
221 create table domdeftest (col1 ddef1);
223 insert into domdeftest default values;
224 select * from domdeftest;
226 alter domain ddef1 set default '42';
227 insert into domdeftest default values;
228 select * from domdeftest;
230 alter domain ddef1 drop default;
231 insert into domdeftest default values;
232 select * from domdeftest;
234 drop table domdeftest;
236 -- Test ALTER DOMAIN .. CONSTRAINT ..
237 create domain con as integer;
238 create table domcontest (col1 con);
240 insert into domcontest values (1);
241 insert into domcontest values (2);
242 alter domain con add constraint t check (VALUE < 1); -- fails
244 alter domain con add constraint t check (VALUE < 34);
245 alter domain con add check (VALUE > 0);
247 insert into domcontest values (-5); -- fails
248 insert into domcontest values (42); -- fails
249 insert into domcontest values (5);
251 alter domain con drop constraint t;
252 insert into domcontest values (-5); --fails
253 insert into domcontest values (42);
255 -- Confirm ALTER DOMAIN with RULES.
256 create table domtab (col1 integer);
257 create domain dom as integer;
258 create view domview as select cast(col1 as dom) from domtab;
259 insert into domtab (col1) values (null);
260 insert into domtab (col1) values (5);
261 select * from domview;
263 alter domain dom set not null;
264 select * from domview; -- fail
266 alter domain dom drop not null;
267 select * from domview;
269 alter domain dom add constraint domchkgt6 check(value > 6);
270 select * from domview; --fail
272 alter domain dom drop constraint domchkgt6 restrict;
273 select * from domview;
276 drop domain ddef1 restrict;
277 drop domain ddef2 restrict;
278 drop domain ddef3 restrict;
279 drop domain ddef4 restrict;
280 drop domain ddef5 restrict;
281 drop sequence ddef4_seq;
283 -- Test domains over domains
284 create domain vchar4 varchar(4);
285 create domain dinter vchar4 check (substring(VALUE, 1, 1) = 'x');
286 create domain dtop dinter check (substring(VALUE, 2, 1) = '1');
289 select 'x1234'::dtop; -- explicit coercion should truncate
290 select 'y1234'::dtop; -- fail
291 select 'y123'::dtop; -- fail
292 select 'yz23'::dtop; -- fail
293 select 'xz23'::dtop; -- fail
295 create temp table dtest(f1 dtop);
297 insert into dtest values('x123');
298 insert into dtest values('x1234'); -- fail, implicit coercion
299 insert into dtest values('y1234'); -- fail, implicit coercion
300 insert into dtest values('y123'); -- fail
301 insert into dtest values('yz23'); -- fail
302 insert into dtest values('xz23'); -- fail
305 drop domain vchar4 cascade;
307 -- Make sure that constraints of newly-added domain columns are
308 -- enforced correctly, even if there's no default value for the new
309 -- column. Per bug #1433
310 create domain str_domain as text not null;
312 create table domain_test (a int, b int);
314 insert into domain_test values (1, 2);
315 insert into domain_test values (1, 2);
318 alter table domain_test add column c str_domain;
320 create domain str_domain2 as text check (value <> 'foo') default 'foo';
323 alter table domain_test add column d str_domain2;
325 -- Check that domain constraints on prepared statement parameters of
326 -- unknown type are enforced correctly.
327 create domain pos_int as int4 check (value > 0) not null;
328 prepare s1 as select $1::pos_int = 10 as "is_ten";
331 execute s1(0); -- should fail
332 execute s1(NULL); -- should fail
334 -- Check that domain constraints on plpgsql function parameters, results,
335 -- and local variables are enforced correctly.
337 create function doubledecrement(p1 pos_int) returns pos_int as $$
341 end$$ language plpgsql;
343 select doubledecrement(3); -- fail because of implicit null assignment
345 create or replace function doubledecrement(p1 pos_int) returns pos_int as $$
346 declare v pos_int := 0;
349 end$$ language plpgsql;
351 select doubledecrement(3); -- fail at initialization assignment
353 create or replace function doubledecrement(p1 pos_int) returns pos_int as $$
354 declare v pos_int := 1;
358 end$$ language plpgsql;
360 select doubledecrement(null); -- fail before call
361 select doubledecrement(0); -- fail before call
362 select doubledecrement(1); -- fail at assignment to v
363 select doubledecrement(2); -- fail at return
364 select doubledecrement(3); -- good
366 -- Check that ALTER DOMAIN tests columns of derived types
368 create domain posint as int4;
370 -- Currently, this doesn't work for composite types, but verify it complains
371 create type ddtest1 as (f1 posint);
372 create table ddtest2(f1 ddtest1);
373 insert into ddtest2 values(row(-1));
374 alter domain posint add constraint c1 check(value >= 0);
377 create table ddtest2(f1 ddtest1[]);
378 insert into ddtest2 values('{(-1)}');
379 alter domain posint add constraint c1 check(value >= 0);
382 alter domain posint add constraint c1 check(value >= 0);
384 create domain posint2 as posint check (value % 2 = 0);
385 create table ddtest2(f1 posint2);
386 insert into ddtest2 values(11); -- fail
387 insert into ddtest2 values(-2); -- fail
388 insert into ddtest2 values(2);
390 alter domain posint add constraint c2 check(value >= 10); -- fail
391 alter domain posint add constraint c2 check(value > 0); -- OK
395 drop domain posint cascade;