2006-04-06 00:11:58 +02:00
|
|
|
--
|
|
|
|
-- Test domains.
|
|
|
|
--
|
2002-03-19 03:18:25 +01:00
|
|
|
-- Test Comment / Drop
|
|
|
|
create domain domaindroptest int4;
|
|
|
|
comment on domain domaindroptest is 'About to drop this..';
|
2006-04-06 00:11:58 +02:00
|
|
|
create domain dependenttypetest domaindroptest;
|
|
|
|
-- fail because of dependent type
|
2002-03-19 03:18:25 +01:00
|
|
|
drop domain domaindroptest;
|
2006-04-06 00:11:58 +02:00
|
|
|
ERROR: cannot drop type domaindroptest because other objects depend on it
|
2008-06-11 23:53:49 +02:00
|
|
|
DETAIL: type dependenttypetest depends on type domaindroptest
|
2006-04-06 00:11:58 +02:00
|
|
|
HINT: Use DROP ... CASCADE to drop the dependent objects too.
|
|
|
|
drop domain domaindroptest cascade;
|
|
|
|
NOTICE: drop cascades to type dependenttypetest
|
2002-07-12 20:43:19 +02:00
|
|
|
-- this should fail because already gone
|
|
|
|
drop domain domaindroptest cascade;
|
2003-07-20 23:56:35 +02:00
|
|
|
ERROR: type "domaindroptest" does not exist
|
2007-03-19 17:30:32 +01:00
|
|
|
-- Test domain input.
|
|
|
|
-- Note: the point of checking both INSERT and COPY FROM is that INSERT
|
|
|
|
-- exercises CoerceToDomain while COPY exercises domain_in.
|
2002-03-19 03:18:25 +01:00
|
|
|
create domain domainvarchar varchar(5);
|
|
|
|
create domain domainnumeric numeric(8,2);
|
|
|
|
create domain domainint4 int4;
|
|
|
|
create domain domaintext text;
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
-- Test explicit coercions --- these should succeed (and truncate)
|
|
|
|
SELECT cast('123456' as domainvarchar);
|
|
|
|
domainvarchar
|
|
|
|
---------------
|
|
|
|
12345
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT cast('12345' as domainvarchar);
|
2002-07-06 22:16:36 +02:00
|
|
|
domainvarchar
|
|
|
|
---------------
|
|
|
|
12345
|
|
|
|
(1 row)
|
|
|
|
|
2002-03-19 03:18:25 +01:00
|
|
|
-- Test tables using domains
|
|
|
|
create table basictest
|
|
|
|
( testint4 domainint4
|
|
|
|
, testtext domaintext
|
|
|
|
, testvarchar domainvarchar
|
|
|
|
, testnumeric domainnumeric
|
|
|
|
);
|
|
|
|
INSERT INTO basictest values ('88', 'haha', 'short', '123.12'); -- Good
|
|
|
|
INSERT INTO basictest values ('88', 'haha', 'short text', '123.12'); -- Bad varchar
|
|
|
|
ERROR: value too long for type character varying(5)
|
|
|
|
INSERT INTO basictest values ('88', 'haha', 'short', '123.1212'); -- Truncate numeric
|
2002-09-20 05:52:50 +02:00
|
|
|
-- Test copy
|
|
|
|
COPY basictest (testvarchar) FROM stdin; -- fail
|
2003-04-24 23:16:45 +02:00
|
|
|
ERROR: value too long for type character varying(5)
|
2006-04-06 00:11:58 +02:00
|
|
|
CONTEXT: COPY basictest, line 1, column testvarchar: "notsoshorttext"
|
2002-09-20 05:52:50 +02:00
|
|
|
COPY basictest (testvarchar) FROM stdin;
|
2002-03-19 03:18:25 +01:00
|
|
|
select * from basictest;
|
|
|
|
testint4 | testtext | testvarchar | testnumeric
|
|
|
|
----------+----------+-------------+-------------
|
2003-05-14 05:26:03 +02:00
|
|
|
88 | haha | short | 123.12
|
|
|
|
88 | haha | short | 123.12
|
|
|
|
| | short |
|
2002-09-20 05:52:50 +02:00
|
|
|
(3 rows)
|
2002-03-19 03:18:25 +01:00
|
|
|
|
2002-03-20 20:45:13 +01:00
|
|
|
-- check that domains inherit operations from base types
|
2002-09-01 00:10:48 +02:00
|
|
|
select testtext || testvarchar as concat, testnumeric + 42 as sum
|
2002-03-20 20:45:13 +01:00
|
|
|
from basictest;
|
|
|
|
concat | sum
|
|
|
|
-----------+--------
|
|
|
|
hahashort | 165.12
|
|
|
|
hahashort | 165.12
|
2002-09-20 05:52:50 +02:00
|
|
|
|
|
|
|
|
(3 rows)
|
2002-03-20 20:45:13 +01:00
|
|
|
|
Fix select_common_type() so that it can select a domain type, if all inputs
to a UNION, CASE, or related construct are of the same domain type. The
main part of this routine smashes domains to their base types, which seems
necessary because the logic involves TypeCategory() and IsPreferredType(),
neither of which work usefully on domains. However, we can add a first
pass that just detects whether all the inputs are exactly the same type,
and if so accept that without question (so long as it's not UNKNOWN).
Per recent gripe from Dean Rasheed.
In passing, remove some tests for InvalidOid, which have clearly been dead
code for quite some time now, because getBaseType() would fail on that input.
Also, clarify the manual's not-very-precise description of the existing
algorithm's behavior.
2007-11-26 17:46:51 +01:00
|
|
|
-- check that union/case/coalesce type resolution handles domains properly
|
|
|
|
select coalesce(4::domainint4, 7) is of (int4) as t;
|
|
|
|
t
|
|
|
|
---
|
|
|
|
t
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
select coalesce(4::domainint4, 7) is of (domainint4) as f;
|
|
|
|
f
|
|
|
|
---
|
|
|
|
f
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
select coalesce(4::domainint4, 7::domainint4) is of (domainint4) as t;
|
|
|
|
t
|
|
|
|
---
|
|
|
|
t
|
|
|
|
(1 row)
|
|
|
|
|
2002-03-19 03:18:25 +01:00
|
|
|
drop table basictest;
|
|
|
|
drop domain domainvarchar restrict;
|
|
|
|
drop domain domainnumeric restrict;
|
|
|
|
drop domain domainint4 restrict;
|
2002-03-20 20:45:13 +01:00
|
|
|
drop domain domaintext;
|
2007-03-19 17:30:32 +01:00
|
|
|
-- Test domains over array types
|
2002-03-19 03:18:25 +01:00
|
|
|
create domain domainint4arr int4[1];
|
2007-03-19 17:30:32 +01:00
|
|
|
create domain domainchar4arr varchar(4)[2][3];
|
2002-03-19 03:18:25 +01:00
|
|
|
create table domarrtest
|
|
|
|
( testint4arr domainint4arr
|
2007-03-19 17:30:32 +01:00
|
|
|
, testchar4arr domainchar4arr
|
2002-03-19 03:18:25 +01:00
|
|
|
);
|
2004-08-05 05:30:44 +02:00
|
|
|
INSERT INTO domarrtest values ('{2,2}', '{{"a","b"},{"c","d"}}');
|
|
|
|
INSERT INTO domarrtest values ('{{2,2},{2,2}}', '{{"a","b"}}');
|
|
|
|
INSERT INTO domarrtest values ('{2,2}', '{{"a","b"},{"c","d"},{"e","f"}}');
|
|
|
|
INSERT INTO domarrtest values ('{2,2}', '{{"a"},{"c"}}');
|
|
|
|
INSERT INTO domarrtest values (NULL, '{{"a","b","c"},{"d","e","f"}}');
|
2007-03-19 17:30:32 +01:00
|
|
|
INSERT INTO domarrtest values (NULL, '{{"toolong","b","c"},{"d","e","f"}}');
|
|
|
|
ERROR: value too long for type character varying(4)
|
2002-03-20 20:45:13 +01:00
|
|
|
select * from domarrtest;
|
2007-03-19 17:30:32 +01:00
|
|
|
testint4arr | testchar4arr
|
2002-03-20 20:45:13 +01:00
|
|
|
---------------+---------------------
|
2004-08-05 05:30:44 +02:00
|
|
|
{2,2} | {{a,b},{c,d}}
|
|
|
|
{{2,2},{2,2}} | {{a,b}}
|
|
|
|
{2,2} | {{a,b},{c,d},{e,f}}
|
|
|
|
{2,2} | {{a},{c}}
|
|
|
|
| {{a,b,c},{d,e,f}}
|
2002-03-20 20:45:13 +01:00
|
|
|
(5 rows)
|
|
|
|
|
2007-03-19 17:30:32 +01:00
|
|
|
select testint4arr[1], testchar4arr[2:2] from domarrtest;
|
|
|
|
testint4arr | testchar4arr
|
|
|
|
-------------+--------------
|
2004-08-05 05:30:44 +02:00
|
|
|
2 | {{c,d}}
|
2005-11-17 23:14:56 +01:00
|
|
|
| {}
|
2004-08-05 05:30:44 +02:00
|
|
|
2 | {{c,d}}
|
2002-03-20 20:45:13 +01:00
|
|
|
2 | {{c}}
|
2004-08-05 05:30:44 +02:00
|
|
|
| {{d,e,f}}
|
2002-03-20 20:45:13 +01:00
|
|
|
(5 rows)
|
|
|
|
|
2011-06-08 18:52:12 +02:00
|
|
|
select array_dims(testint4arr), array_dims(testchar4arr) from domarrtest;
|
|
|
|
array_dims | array_dims
|
|
|
|
------------+------------
|
|
|
|
[1:2] | [1:2][1:2]
|
|
|
|
[1:2][1:2] | [1:1][1:2]
|
|
|
|
[1:2] | [1:3][1:2]
|
|
|
|
[1:2] | [1:2][1:1]
|
|
|
|
| [1:2][1:3]
|
|
|
|
(5 rows)
|
|
|
|
|
2007-03-19 17:30:32 +01:00
|
|
|
COPY domarrtest FROM stdin;
|
|
|
|
COPY domarrtest FROM stdin; -- fail
|
|
|
|
ERROR: value too long for type character varying(4)
|
|
|
|
CONTEXT: COPY domarrtest, line 1, column testchar4arr: "{qwerty,w,e}"
|
|
|
|
select * from domarrtest;
|
|
|
|
testint4arr | testchar4arr
|
|
|
|
---------------+---------------------
|
|
|
|
{2,2} | {{a,b},{c,d}}
|
|
|
|
{{2,2},{2,2}} | {{a,b}}
|
|
|
|
{2,2} | {{a,b},{c,d},{e,f}}
|
|
|
|
{2,2} | {{a},{c}}
|
|
|
|
| {{a,b,c},{d,e,f}}
|
|
|
|
{3,4} | {q,w,e}
|
|
|
|
|
|
|
|
|
(7 rows)
|
|
|
|
|
2002-03-19 03:18:25 +01:00
|
|
|
drop table domarrtest;
|
|
|
|
drop domain domainint4arr restrict;
|
2007-03-19 17:30:32 +01:00
|
|
|
drop domain domainchar4arr restrict;
|
2011-06-08 18:52:12 +02:00
|
|
|
create domain dia as int[];
|
|
|
|
select '{1,2,3}'::dia;
|
|
|
|
dia
|
|
|
|
---------
|
|
|
|
{1,2,3}
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
select array_dims('{1,2,3}'::dia);
|
|
|
|
array_dims
|
|
|
|
------------
|
|
|
|
[1:3]
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
select pg_typeof('{1,2,3}'::dia);
|
|
|
|
pg_typeof
|
|
|
|
-----------
|
|
|
|
dia
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
select pg_typeof('{1,2,3}'::dia || 42); -- should be int[] not dia
|
|
|
|
pg_typeof
|
|
|
|
-----------
|
|
|
|
integer[]
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
drop domain dia;
|
2002-03-19 03:18:25 +01:00
|
|
|
create domain dnotnull varchar(15) NOT NULL;
|
2002-07-06 22:16:36 +02:00
|
|
|
create domain dnull varchar(15);
|
2002-11-15 03:50:21 +01:00
|
|
|
create domain dcheck varchar(15) NOT NULL CHECK (VALUE = 'a' OR VALUE = 'c' OR VALUE = 'd');
|
2002-03-19 03:18:25 +01:00
|
|
|
create table nulltest
|
|
|
|
( col1 dnotnull
|
|
|
|
, col2 dnotnull NULL -- NOT NULL in the domain cannot be overridden
|
|
|
|
, col3 dnull NOT NULL
|
|
|
|
, col4 dnull
|
2002-11-15 03:50:21 +01:00
|
|
|
, col5 dcheck CHECK (col5 IN ('c', 'd'))
|
2002-03-19 03:18:25 +01:00
|
|
|
);
|
|
|
|
INSERT INTO nulltest DEFAULT VALUES;
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: domain dnotnull does not allow null values
|
2002-11-15 03:50:21 +01:00
|
|
|
INSERT INTO nulltest values ('a', 'b', 'c', 'd', 'c'); -- Good
|
|
|
|
insert into nulltest values ('a', 'b', 'c', 'd', NULL);
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: domain dcheck does not allow null values
|
2002-11-15 03:50:21 +01:00
|
|
|
insert into nulltest values ('a', 'b', 'c', 'd', 'a');
|
2004-06-10 19:56:03 +02:00
|
|
|
ERROR: new row for relation "nulltest" violates check constraint "nulltest_col5_check"
|
2011-11-29 21:02:10 +01:00
|
|
|
DETAIL: Failing row contains (a, b, c, d, a).
|
2002-11-15 03:50:21 +01:00
|
|
|
INSERT INTO nulltest values (NULL, 'b', 'c', 'd', 'd');
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: domain dnotnull does not allow null values
|
2002-11-15 03:50:21 +01:00
|
|
|
INSERT INTO nulltest values ('a', NULL, 'c', 'd', 'c');
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: domain dnotnull does not allow null values
|
2002-11-15 03:50:21 +01:00
|
|
|
INSERT INTO nulltest values ('a', 'b', NULL, 'd', 'c');
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: null value in column "col3" violates not-null constraint
|
2011-11-30 00:29:18 +01:00
|
|
|
DETAIL: Failing row contains (a, b, null, d, c).
|
2002-11-15 03:50:21 +01:00
|
|
|
INSERT INTO nulltest values ('a', 'b', 'c', NULL, 'd'); -- Good
|
2002-09-20 05:52:50 +02:00
|
|
|
-- Test copy
|
|
|
|
COPY nulltest FROM stdin; --fail
|
2006-04-06 00:11:58 +02:00
|
|
|
ERROR: null value in column "col3" violates not-null constraint
|
2011-11-30 00:29:18 +01:00
|
|
|
DETAIL: Failing row contains (a, b, null, d, d).
|
2006-04-06 00:11:58 +02:00
|
|
|
CONTEXT: COPY nulltest, line 1: "a b \N d d"
|
|
|
|
COPY nulltest FROM stdin; --fail
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: domain dcheck does not allow null values
|
2006-10-06 19:14:01 +02:00
|
|
|
CONTEXT: COPY nulltest, line 1, column col5: null input
|
2002-11-15 03:50:21 +01:00
|
|
|
-- Last row is bad
|
2002-09-20 05:52:50 +02:00
|
|
|
COPY nulltest FROM stdin;
|
2004-06-10 19:56:03 +02:00
|
|
|
ERROR: new row for relation "nulltest" violates check constraint "nulltest_col5_check"
|
2011-11-29 21:02:10 +01:00
|
|
|
DETAIL: Failing row contains (a, b, c, null, a).
|
2003-09-30 00:06:40 +02:00
|
|
|
CONTEXT: COPY nulltest, line 3: "a b c \N a"
|
2002-03-19 03:18:25 +01:00
|
|
|
select * from nulltest;
|
2002-11-15 03:50:21 +01:00
|
|
|
col1 | col2 | col3 | col4 | col5
|
|
|
|
------+------+------+------+------
|
|
|
|
a | b | c | d | c
|
|
|
|
a | b | c | | d
|
|
|
|
(2 rows)
|
2002-03-19 03:18:25 +01:00
|
|
|
|
2002-07-06 22:16:36 +02:00
|
|
|
-- Test out coerced (casted) constraints
|
|
|
|
SELECT cast('1' as dnotnull);
|
|
|
|
dnotnull
|
|
|
|
----------
|
|
|
|
1
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT cast(NULL as dnotnull); -- fail
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: domain dnotnull does not allow null values
|
2002-07-06 22:16:36 +02:00
|
|
|
SELECT cast(cast(NULL as dnull) as dnotnull); -- fail
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: domain dnotnull does not allow null values
|
2002-07-06 22:16:36 +02:00
|
|
|
SELECT cast(col4 as dnotnull) from nulltest; -- fail
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: domain dnotnull does not allow null values
|
2002-07-06 22:16:36 +02:00
|
|
|
-- cleanup
|
2002-03-19 03:18:25 +01:00
|
|
|
drop table nulltest;
|
|
|
|
drop domain dnotnull restrict;
|
|
|
|
drop domain dnull restrict;
|
2002-12-06 06:00:34 +01:00
|
|
|
drop domain dcheck restrict;
|
2002-03-19 03:18:25 +01:00
|
|
|
create domain ddef1 int4 DEFAULT 3;
|
|
|
|
create domain ddef2 oid DEFAULT '12';
|
|
|
|
-- Type mixing, function returns int8
|
|
|
|
create domain ddef3 text DEFAULT 5;
|
|
|
|
create sequence ddef4_seq;
|
2005-10-03 01:50:16 +02:00
|
|
|
create domain ddef4 int4 DEFAULT nextval('ddef4_seq');
|
2002-03-19 03:18:25 +01:00
|
|
|
create domain ddef5 numeric(8,2) NOT NULL DEFAULT '12.12';
|
|
|
|
create table defaulttest
|
|
|
|
( col1 ddef1
|
|
|
|
, col2 ddef2
|
|
|
|
, col3 ddef3
|
2002-08-20 18:37:03 +02:00
|
|
|
, col4 ddef4 PRIMARY KEY
|
2002-03-19 03:18:25 +01:00
|
|
|
, col5 ddef1 NOT NULL DEFAULT NULL
|
|
|
|
, col6 ddef2 DEFAULT '88'
|
|
|
|
, col7 ddef4 DEFAULT 8000
|
2002-09-01 00:10:48 +02:00
|
|
|
, col8 ddef5
|
2002-03-19 03:18:25 +01:00
|
|
|
);
|
2007-10-29 20:40:40 +01:00
|
|
|
insert into defaulttest(col4) values(0); -- fails, col5 defaults to null
|
|
|
|
ERROR: null value in column "col5" violates not-null constraint
|
2011-11-30 00:29:18 +01:00
|
|
|
DETAIL: Failing row contains (3, 12, 5, 0, null, 88, 8000, 12.12).
|
2007-10-29 20:40:40 +01:00
|
|
|
alter table defaulttest alter column col5 drop default;
|
|
|
|
insert into defaulttest default values; -- succeeds, inserts domain default
|
|
|
|
-- We used to treat SET DEFAULT NULL as equivalent to DROP DEFAULT; wrong
|
|
|
|
alter table defaulttest alter column col5 set default null;
|
|
|
|
insert into defaulttest(col4) values(0); -- fails
|
|
|
|
ERROR: null value in column "col5" violates not-null constraint
|
2011-11-30 00:29:18 +01:00
|
|
|
DETAIL: Failing row contains (3, 12, 5, 0, null, 88, 8000, 12.12).
|
2007-10-29 20:40:40 +01:00
|
|
|
alter table defaulttest alter column col5 drop default;
|
2002-03-19 03:18:25 +01:00
|
|
|
insert into defaulttest default values;
|
|
|
|
insert into defaulttest default values;
|
2002-09-20 05:52:50 +02:00
|
|
|
-- Test defaults with copy
|
|
|
|
COPY defaulttest(col5) FROM stdin;
|
2002-03-19 03:18:25 +01:00
|
|
|
select * from defaulttest;
|
|
|
|
col1 | col2 | col3 | col4 | col5 | col6 | col7 | col8
|
|
|
|
------+------+------+------+------+------+------+-------
|
2003-05-14 05:26:03 +02:00
|
|
|
3 | 12 | 5 | 1 | 3 | 88 | 8000 | 12.12
|
|
|
|
3 | 12 | 5 | 2 | 3 | 88 | 8000 | 12.12
|
|
|
|
3 | 12 | 5 | 3 | 3 | 88 | 8000 | 12.12
|
|
|
|
3 | 12 | 5 | 4 | 42 | 88 | 8000 | 12.12
|
2002-09-20 05:52:50 +02:00
|
|
|
(4 rows)
|
2002-03-19 03:18:25 +01:00
|
|
|
|
2002-12-06 06:00:34 +01:00
|
|
|
drop table defaulttest cascade;
|
|
|
|
-- Test ALTER DOMAIN .. NOT NULL
|
|
|
|
create domain dnotnulltest integer;
|
|
|
|
create table domnotnull
|
|
|
|
( col1 dnotnulltest
|
|
|
|
, col2 dnotnulltest
|
|
|
|
);
|
|
|
|
insert into domnotnull default values;
|
|
|
|
alter domain dnotnulltest set not null; -- fails
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: column "col1" of table "domnotnull" contains null values
|
2002-12-06 06:00:34 +01:00
|
|
|
update domnotnull set col1 = 5;
|
|
|
|
alter domain dnotnulltest set not null; -- fails
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: column "col2" of table "domnotnull" contains null values
|
2002-12-06 06:00:34 +01:00
|
|
|
update domnotnull set col2 = 6;
|
|
|
|
alter domain dnotnulltest set not null;
|
|
|
|
update domnotnull set col1 = null; -- fails
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: domain dnotnulltest does not allow null values
|
2002-12-06 06:00:34 +01:00
|
|
|
alter domain dnotnulltest drop not null;
|
|
|
|
update domnotnull set col1 = null;
|
|
|
|
drop domain dnotnulltest cascade;
|
2008-06-11 23:53:49 +02:00
|
|
|
NOTICE: drop cascades to 2 other objects
|
|
|
|
DETAIL: drop cascades to table domnotnull column col1
|
|
|
|
drop cascades to table domnotnull column col2
|
2002-12-06 06:00:34 +01:00
|
|
|
-- Test ALTER DOMAIN .. DEFAULT ..
|
|
|
|
create table domdeftest (col1 ddef1);
|
|
|
|
insert into domdeftest default values;
|
|
|
|
select * from domdeftest;
|
|
|
|
col1
|
|
|
|
------
|
2003-05-14 05:26:03 +02:00
|
|
|
3
|
2002-12-06 06:00:34 +01:00
|
|
|
(1 row)
|
|
|
|
|
|
|
|
alter domain ddef1 set default '42';
|
|
|
|
insert into domdeftest default values;
|
|
|
|
select * from domdeftest;
|
|
|
|
col1
|
|
|
|
------
|
2003-05-14 05:26:03 +02:00
|
|
|
3
|
|
|
|
42
|
2002-12-06 06:00:34 +01:00
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
alter domain ddef1 drop default;
|
|
|
|
insert into domdeftest default values;
|
|
|
|
select * from domdeftest;
|
|
|
|
col1
|
|
|
|
------
|
2003-05-14 05:26:03 +02:00
|
|
|
3
|
|
|
|
42
|
|
|
|
|
2002-12-06 06:00:34 +01:00
|
|
|
(3 rows)
|
|
|
|
|
|
|
|
drop table domdeftest;
|
|
|
|
-- Test ALTER DOMAIN .. CONSTRAINT ..
|
|
|
|
create domain con as integer;
|
|
|
|
create table domcontest (col1 con);
|
|
|
|
insert into domcontest values (1);
|
|
|
|
insert into domcontest values (2);
|
|
|
|
alter domain con add constraint t check (VALUE < 1); -- fails
|
2003-09-29 02:05:25 +02:00
|
|
|
ERROR: column "col1" of table "domcontest" contains values that violate the new constraint
|
2002-12-06 06:00:34 +01:00
|
|
|
alter domain con add constraint t check (VALUE < 34);
|
|
|
|
alter domain con add check (VALUE > 0);
|
|
|
|
insert into domcontest values (-5); -- fails
|
2004-06-10 19:56:03 +02:00
|
|
|
ERROR: value for domain con violates check constraint "con_check"
|
2002-12-06 06:00:34 +01:00
|
|
|
insert into domcontest values (42); -- fails
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: value for domain con violates check constraint "t"
|
2002-12-06 06:00:34 +01:00
|
|
|
insert into domcontest values (5);
|
|
|
|
alter domain con drop constraint t;
|
|
|
|
insert into domcontest values (-5); --fails
|
2004-06-10 19:56:03 +02:00
|
|
|
ERROR: value for domain con violates check constraint "con_check"
|
2002-12-06 06:00:34 +01:00
|
|
|
insert into domcontest values (42);
|
2012-01-05 18:48:55 +01:00
|
|
|
alter domain con drop constraint nonexistent;
|
|
|
|
ERROR: constraint "nonexistent" of domain "con" does not exist
|
|
|
|
alter domain con drop constraint if exists nonexistent;
|
|
|
|
NOTICE: constraint "nonexistent" of domain "con" does not exist, skipping
|
2011-06-02 00:43:50 +02:00
|
|
|
-- Test ALTER DOMAIN .. CONSTRAINT .. NOT VALID
|
|
|
|
create domain things AS INT;
|
|
|
|
CREATE TABLE thethings (stuff things);
|
|
|
|
INSERT INTO thethings (stuff) VALUES (55);
|
|
|
|
ALTER DOMAIN things ADD CONSTRAINT meow CHECK (VALUE < 11);
|
|
|
|
ERROR: column "stuff" of table "thethings" contains values that violate the new constraint
|
|
|
|
ALTER DOMAIN things ADD CONSTRAINT meow CHECK (VALUE < 11) NOT VALID;
|
|
|
|
ALTER DOMAIN things VALIDATE CONSTRAINT meow;
|
|
|
|
ERROR: column "stuff" of table "thethings" contains values that violate the new constraint
|
|
|
|
UPDATE thethings SET stuff = 10;
|
|
|
|
ALTER DOMAIN things VALIDATE CONSTRAINT meow;
|
2003-02-03 22:15:45 +01:00
|
|
|
-- Confirm ALTER DOMAIN with RULES.
|
|
|
|
create table domtab (col1 integer);
|
|
|
|
create domain dom as integer;
|
|
|
|
create view domview as select cast(col1 as dom) from domtab;
|
|
|
|
insert into domtab (col1) values (null);
|
|
|
|
insert into domtab (col1) values (5);
|
|
|
|
select * from domview;
|
|
|
|
col1
|
|
|
|
------
|
2003-05-14 05:26:03 +02:00
|
|
|
|
|
|
|
5
|
2003-02-03 22:15:45 +01:00
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
alter domain dom set not null;
|
|
|
|
select * from domview; -- fail
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: domain dom does not allow null values
|
2003-02-03 22:15:45 +01:00
|
|
|
alter domain dom drop not null;
|
|
|
|
select * from domview;
|
|
|
|
col1
|
|
|
|
------
|
2003-05-14 05:26:03 +02:00
|
|
|
|
|
|
|
5
|
2003-02-03 22:15:45 +01:00
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
alter domain dom add constraint domchkgt6 check(value > 6);
|
|
|
|
select * from domview; --fail
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: value for domain dom violates check constraint "domchkgt6"
|
2003-02-03 22:15:45 +01:00
|
|
|
alter domain dom drop constraint domchkgt6 restrict;
|
|
|
|
select * from domview;
|
|
|
|
col1
|
|
|
|
------
|
2003-05-14 05:26:03 +02:00
|
|
|
|
|
|
|
5
|
2003-02-03 22:15:45 +01:00
|
|
|
(2 rows)
|
|
|
|
|
2002-12-06 06:00:34 +01:00
|
|
|
-- cleanup
|
2002-03-19 03:18:25 +01:00
|
|
|
drop domain ddef1 restrict;
|
|
|
|
drop domain ddef2 restrict;
|
|
|
|
drop domain ddef3 restrict;
|
|
|
|
drop domain ddef4 restrict;
|
|
|
|
drop domain ddef5 restrict;
|
2005-10-03 01:50:16 +02:00
|
|
|
drop sequence ddef4_seq;
|
2006-04-06 00:11:58 +02:00
|
|
|
-- Test domains over domains
|
|
|
|
create domain vchar4 varchar(4);
|
|
|
|
create domain dinter vchar4 check (substring(VALUE, 1, 1) = 'x');
|
|
|
|
create domain dtop dinter check (substring(VALUE, 2, 1) = '1');
|
|
|
|
select 'x123'::dtop;
|
|
|
|
dtop
|
|
|
|
------
|
|
|
|
x123
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
select 'x1234'::dtop; -- explicit coercion should truncate
|
|
|
|
dtop
|
|
|
|
------
|
|
|
|
x123
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
select 'y1234'::dtop; -- fail
|
|
|
|
ERROR: value for domain dtop violates check constraint "dinter_check"
|
|
|
|
select 'y123'::dtop; -- fail
|
|
|
|
ERROR: value for domain dtop violates check constraint "dinter_check"
|
|
|
|
select 'yz23'::dtop; -- fail
|
|
|
|
ERROR: value for domain dtop violates check constraint "dinter_check"
|
|
|
|
select 'xz23'::dtop; -- fail
|
|
|
|
ERROR: value for domain dtop violates check constraint "dtop_check"
|
|
|
|
create temp table dtest(f1 dtop);
|
|
|
|
insert into dtest values('x123');
|
|
|
|
insert into dtest values('x1234'); -- fail, implicit coercion
|
|
|
|
ERROR: value too long for type character varying(4)
|
|
|
|
insert into dtest values('y1234'); -- fail, implicit coercion
|
|
|
|
ERROR: value too long for type character varying(4)
|
|
|
|
insert into dtest values('y123'); -- fail
|
|
|
|
ERROR: value for domain dtop violates check constraint "dinter_check"
|
|
|
|
insert into dtest values('yz23'); -- fail
|
|
|
|
ERROR: value for domain dtop violates check constraint "dinter_check"
|
|
|
|
insert into dtest values('xz23'); -- fail
|
|
|
|
ERROR: value for domain dtop violates check constraint "dtop_check"
|
|
|
|
drop table dtest;
|
|
|
|
drop domain vchar4 cascade;
|
2008-06-11 23:53:49 +02:00
|
|
|
NOTICE: drop cascades to 2 other objects
|
|
|
|
DETAIL: drop cascades to type dinter
|
|
|
|
drop cascades to type dtop
|
2005-01-25 04:22:19 +01:00
|
|
|
-- 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 not null;
|
|
|
|
create table domain_test (a int, b int);
|
|
|
|
insert into domain_test values (1, 2);
|
|
|
|
insert into domain_test values (1, 2);
|
|
|
|
-- should fail
|
|
|
|
alter table domain_test add column c str_domain;
|
|
|
|
ERROR: domain str_domain does not allow null values
|
|
|
|
create domain str_domain2 as text check (value <> 'foo') default 'foo';
|
|
|
|
-- should fail
|
|
|
|
alter table domain_test add column d str_domain2;
|
|
|
|
ERROR: value for domain str_domain2 violates check constraint "str_domain2_check"
|
2006-01-15 23:34:49 +01:00
|
|
|
-- Check that domain constraints on prepared statement parameters of
|
|
|
|
-- unknown type are enforced correctly.
|
|
|
|
create domain pos_int as int4 check (value > 0) not null;
|
|
|
|
prepare s1 as select $1::pos_int = 10 as "is_ten";
|
|
|
|
execute s1(10);
|
|
|
|
is_ten
|
|
|
|
--------
|
|
|
|
t
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
execute s1(0); -- should fail
|
|
|
|
ERROR: value for domain pos_int violates check constraint "pos_int_check"
|
|
|
|
execute s1(NULL); -- should fail
|
|
|
|
ERROR: domain pos_int does not allow null values
|
2006-04-06 00:11:58 +02:00
|
|
|
-- 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;
|
2007-02-01 20:22:07 +01:00
|
|
|
begin
|
|
|
|
return p1;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
select doubledecrement(3); -- fail because of implicit null assignment
|
|
|
|
ERROR: domain pos_int does not allow null values
|
2012-02-01 08:14:37 +01:00
|
|
|
CONTEXT: PL/pgSQL function doubledecrement(pos_int) line 3 during statement block local variable initialization
|
2007-02-01 20:22:07 +01:00
|
|
|
create or replace 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
|
|
|
|
ERROR: value for domain pos_int violates check constraint "pos_int_check"
|
2012-02-01 08:14:37 +01:00
|
|
|
CONTEXT: PL/pgSQL function doubledecrement(pos_int) line 3 during statement block local variable initialization
|
2007-02-01 20:22:07 +01:00
|
|
|
create or replace function doubledecrement(p1 pos_int) returns pos_int as $$
|
|
|
|
declare v pos_int := 1;
|
2006-04-06 00:11:58 +02:00
|
|
|
begin
|
|
|
|
v := p1 - 1;
|
|
|
|
return v - 1;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
select doubledecrement(null); -- fail before call
|
|
|
|
ERROR: domain pos_int does not allow null values
|
|
|
|
select doubledecrement(0); -- fail before call
|
|
|
|
ERROR: value for domain pos_int violates check constraint "pos_int_check"
|
|
|
|
select doubledecrement(1); -- fail at assignment to v
|
|
|
|
ERROR: value for domain pos_int violates check constraint "pos_int_check"
|
2012-02-01 08:14:37 +01:00
|
|
|
CONTEXT: PL/pgSQL function doubledecrement(pos_int) line 4 at assignment
|
2006-04-06 00:11:58 +02:00
|
|
|
select doubledecrement(2); -- fail at return
|
|
|
|
ERROR: value for domain pos_int violates check constraint "pos_int_check"
|
2012-02-01 08:14:37 +01:00
|
|
|
CONTEXT: PL/pgSQL function doubledecrement(pos_int) while casting return value to function's return type
|
2006-04-06 00:11:58 +02:00
|
|
|
select doubledecrement(3); -- good
|
|
|
|
doubledecrement
|
|
|
|
-----------------
|
|
|
|
1
|
|
|
|
(1 row)
|
|
|
|
|
2007-05-11 22:17:15 +02:00
|
|
|
-- Check that ALTER DOMAIN tests columns of derived types
|
|
|
|
create domain posint as int4;
|
|
|
|
-- Currently, this doesn't work for composite types, but verify it complains
|
|
|
|
create type ddtest1 as (f1 posint);
|
|
|
|
create table ddtest2(f1 ddtest1);
|
|
|
|
insert into ddtest2 values(row(-1));
|
|
|
|
alter domain posint add constraint c1 check(value >= 0);
|
2011-06-21 23:33:20 +02:00
|
|
|
ERROR: cannot alter type "posint" because column "ddtest2.f1" uses it
|
2007-05-11 22:17:15 +02:00
|
|
|
drop table ddtest2;
|
|
|
|
create table ddtest2(f1 ddtest1[]);
|
|
|
|
insert into ddtest2 values('{(-1)}');
|
|
|
|
alter domain posint add constraint c1 check(value >= 0);
|
2011-06-21 23:33:20 +02:00
|
|
|
ERROR: cannot alter type "posint" because column "ddtest2.f1" uses it
|
2007-05-11 22:17:15 +02:00
|
|
|
drop table ddtest2;
|
|
|
|
alter domain posint add constraint c1 check(value >= 0);
|
|
|
|
create domain posint2 as posint check (value % 2 = 0);
|
|
|
|
create table ddtest2(f1 posint2);
|
|
|
|
insert into ddtest2 values(11); -- fail
|
|
|
|
ERROR: value for domain posint2 violates check constraint "posint2_check"
|
|
|
|
insert into ddtest2 values(-2); -- fail
|
|
|
|
ERROR: value for domain posint2 violates check constraint "c1"
|
|
|
|
insert into ddtest2 values(2);
|
|
|
|
alter domain posint add constraint c2 check(value >= 10); -- fail
|
|
|
|
ERROR: column "f1" of table "ddtest2" contains values that violate the new constraint
|
|
|
|
alter domain posint add constraint c2 check(value > 0); -- OK
|
|
|
|
drop table ddtest2;
|
|
|
|
drop type ddtest1;
|
|
|
|
drop domain posint cascade;
|
|
|
|
NOTICE: drop cascades to type posint2
|
Improve handling of domains over arrays.
This patch eliminates various bizarre behaviors caused by sloppy thinking
about the difference between a domain type and its underlying array type.
In particular, the operation of updating one element of such an array
has to be considered as yielding a value of the underlying array type,
*not* a value of the domain, because there's no assurance that the
domain's CHECK constraints are still satisfied. If we're intending to
store the result back into a domain column, we have to re-cast to the
domain type so that constraints are re-checked.
For similar reasons, such a domain can't be blindly matched to an ANYARRAY
polymorphic parameter, because the polymorphic function is likely to apply
array-ish operations that could invalidate the domain constraints. For the
moment, we just forbid such matching. We might later wish to insert an
automatic downcast to the underlying array type, but such a change should
also change matching of domains to ANYELEMENT for consistency.
To ensure that all such logic is rechecked, this patch removes the original
hack of setting a domain's pg_type.typelem field to match its base type;
the typelem will always be zero instead. In those places where it's really
okay to look through the domain type with no other logic changes, use the
newly added get_base_element_type function in place of get_element_type.
catversion bumped due to change in pg_type contents.
Per bug #5717 from Richard Huxton and subsequent discussion.
2010-10-21 22:07:17 +02:00
|
|
|
--
|
|
|
|
-- Check enforcement of domain-related typmod in plpgsql (bug #5717)
|
|
|
|
--
|
|
|
|
create or replace function array_elem_check(numeric) returns numeric as $$
|
|
|
|
declare
|
|
|
|
x numeric(4,2)[1];
|
|
|
|
begin
|
|
|
|
x[1] := $1;
|
|
|
|
return x[1];
|
|
|
|
end$$ language plpgsql;
|
|
|
|
select array_elem_check(121.00);
|
|
|
|
ERROR: numeric field overflow
|
|
|
|
DETAIL: A field with precision 4, scale 2 must round to an absolute value less than 10^2.
|
2012-02-01 08:14:37 +01:00
|
|
|
CONTEXT: PL/pgSQL function array_elem_check(numeric) line 5 at assignment
|
Improve handling of domains over arrays.
This patch eliminates various bizarre behaviors caused by sloppy thinking
about the difference between a domain type and its underlying array type.
In particular, the operation of updating one element of such an array
has to be considered as yielding a value of the underlying array type,
*not* a value of the domain, because there's no assurance that the
domain's CHECK constraints are still satisfied. If we're intending to
store the result back into a domain column, we have to re-cast to the
domain type so that constraints are re-checked.
For similar reasons, such a domain can't be blindly matched to an ANYARRAY
polymorphic parameter, because the polymorphic function is likely to apply
array-ish operations that could invalidate the domain constraints. For the
moment, we just forbid such matching. We might later wish to insert an
automatic downcast to the underlying array type, but such a change should
also change matching of domains to ANYELEMENT for consistency.
To ensure that all such logic is rechecked, this patch removes the original
hack of setting a domain's pg_type.typelem field to match its base type;
the typelem will always be zero instead. In those places where it's really
okay to look through the domain type with no other logic changes, use the
newly added get_base_element_type function in place of get_element_type.
catversion bumped due to change in pg_type contents.
Per bug #5717 from Richard Huxton and subsequent discussion.
2010-10-21 22:07:17 +02:00
|
|
|
select array_elem_check(1.23456);
|
|
|
|
array_elem_check
|
|
|
|
------------------
|
|
|
|
1.23
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
create domain mynums as numeric(4,2)[1];
|
|
|
|
create or replace function array_elem_check(numeric) returns numeric as $$
|
|
|
|
declare
|
|
|
|
x mynums;
|
|
|
|
begin
|
|
|
|
x[1] := $1;
|
|
|
|
return x[1];
|
|
|
|
end$$ language plpgsql;
|
|
|
|
select array_elem_check(121.00);
|
|
|
|
ERROR: numeric field overflow
|
|
|
|
DETAIL: A field with precision 4, scale 2 must round to an absolute value less than 10^2.
|
2012-02-01 08:14:37 +01:00
|
|
|
CONTEXT: PL/pgSQL function array_elem_check(numeric) line 5 at assignment
|
Improve handling of domains over arrays.
This patch eliminates various bizarre behaviors caused by sloppy thinking
about the difference between a domain type and its underlying array type.
In particular, the operation of updating one element of such an array
has to be considered as yielding a value of the underlying array type,
*not* a value of the domain, because there's no assurance that the
domain's CHECK constraints are still satisfied. If we're intending to
store the result back into a domain column, we have to re-cast to the
domain type so that constraints are re-checked.
For similar reasons, such a domain can't be blindly matched to an ANYARRAY
polymorphic parameter, because the polymorphic function is likely to apply
array-ish operations that could invalidate the domain constraints. For the
moment, we just forbid such matching. We might later wish to insert an
automatic downcast to the underlying array type, but such a change should
also change matching of domains to ANYELEMENT for consistency.
To ensure that all such logic is rechecked, this patch removes the original
hack of setting a domain's pg_type.typelem field to match its base type;
the typelem will always be zero instead. In those places where it's really
okay to look through the domain type with no other logic changes, use the
newly added get_base_element_type function in place of get_element_type.
catversion bumped due to change in pg_type contents.
Per bug #5717 from Richard Huxton and subsequent discussion.
2010-10-21 22:07:17 +02:00
|
|
|
select array_elem_check(1.23456);
|
|
|
|
array_elem_check
|
|
|
|
------------------
|
|
|
|
1.23
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
create domain mynums2 as mynums;
|
|
|
|
create or replace function array_elem_check(numeric) returns numeric as $$
|
|
|
|
declare
|
|
|
|
x mynums2;
|
|
|
|
begin
|
|
|
|
x[1] := $1;
|
|
|
|
return x[1];
|
|
|
|
end$$ language plpgsql;
|
|
|
|
select array_elem_check(121.00);
|
|
|
|
ERROR: numeric field overflow
|
|
|
|
DETAIL: A field with precision 4, scale 2 must round to an absolute value less than 10^2.
|
2012-02-01 08:14:37 +01:00
|
|
|
CONTEXT: PL/pgSQL function array_elem_check(numeric) line 5 at assignment
|
Improve handling of domains over arrays.
This patch eliminates various bizarre behaviors caused by sloppy thinking
about the difference between a domain type and its underlying array type.
In particular, the operation of updating one element of such an array
has to be considered as yielding a value of the underlying array type,
*not* a value of the domain, because there's no assurance that the
domain's CHECK constraints are still satisfied. If we're intending to
store the result back into a domain column, we have to re-cast to the
domain type so that constraints are re-checked.
For similar reasons, such a domain can't be blindly matched to an ANYARRAY
polymorphic parameter, because the polymorphic function is likely to apply
array-ish operations that could invalidate the domain constraints. For the
moment, we just forbid such matching. We might later wish to insert an
automatic downcast to the underlying array type, but such a change should
also change matching of domains to ANYELEMENT for consistency.
To ensure that all such logic is rechecked, this patch removes the original
hack of setting a domain's pg_type.typelem field to match its base type;
the typelem will always be zero instead. In those places where it's really
okay to look through the domain type with no other logic changes, use the
newly added get_base_element_type function in place of get_element_type.
catversion bumped due to change in pg_type contents.
Per bug #5717 from Richard Huxton and subsequent discussion.
2010-10-21 22:07:17 +02:00
|
|
|
select array_elem_check(1.23456);
|
|
|
|
array_elem_check
|
|
|
|
------------------
|
|
|
|
1.23
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
drop function array_elem_check(numeric);
|
|
|
|
--
|
|
|
|
-- Check enforcement of array-level domain constraints
|
|
|
|
--
|
|
|
|
create domain orderedpair as int[2] check (value[1] < value[2]);
|
|
|
|
select array[1,2]::orderedpair;
|
|
|
|
array
|
|
|
|
-------
|
|
|
|
{1,2}
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
select array[2,1]::orderedpair; -- fail
|
|
|
|
ERROR: value for domain orderedpair violates check constraint "orderedpair_check"
|
|
|
|
create temp table op (f1 orderedpair);
|
|
|
|
insert into op values (array[1,2]);
|
|
|
|
insert into op values (array[2,1]); -- fail
|
|
|
|
ERROR: value for domain orderedpair violates check constraint "orderedpair_check"
|
|
|
|
update op set f1[2] = 3;
|
|
|
|
update op set f1[2] = 0; -- fail
|
|
|
|
ERROR: value for domain orderedpair violates check constraint "orderedpair_check"
|
|
|
|
select * from op;
|
|
|
|
f1
|
|
|
|
-------
|
|
|
|
{1,3}
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
create or replace function array_elem_check(int) returns int as $$
|
|
|
|
declare
|
|
|
|
x orderedpair := '{1,2}';
|
|
|
|
begin
|
|
|
|
x[2] := $1;
|
|
|
|
return x[2];
|
|
|
|
end$$ language plpgsql;
|
|
|
|
select array_elem_check(3);
|
|
|
|
array_elem_check
|
|
|
|
------------------
|
|
|
|
3
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
select array_elem_check(-1);
|
|
|
|
ERROR: value for domain orderedpair violates check constraint "orderedpair_check"
|
2012-02-01 08:14:37 +01:00
|
|
|
CONTEXT: PL/pgSQL function array_elem_check(integer) line 5 at assignment
|
Improve handling of domains over arrays.
This patch eliminates various bizarre behaviors caused by sloppy thinking
about the difference between a domain type and its underlying array type.
In particular, the operation of updating one element of such an array
has to be considered as yielding a value of the underlying array type,
*not* a value of the domain, because there's no assurance that the
domain's CHECK constraints are still satisfied. If we're intending to
store the result back into a domain column, we have to re-cast to the
domain type so that constraints are re-checked.
For similar reasons, such a domain can't be blindly matched to an ANYARRAY
polymorphic parameter, because the polymorphic function is likely to apply
array-ish operations that could invalidate the domain constraints. For the
moment, we just forbid such matching. We might later wish to insert an
automatic downcast to the underlying array type, but such a change should
also change matching of domains to ANYELEMENT for consistency.
To ensure that all such logic is rechecked, this patch removes the original
hack of setting a domain's pg_type.typelem field to match its base type;
the typelem will always be zero instead. In those places where it's really
okay to look through the domain type with no other logic changes, use the
newly added get_base_element_type function in place of get_element_type.
catversion bumped due to change in pg_type contents.
Per bug #5717 from Richard Huxton and subsequent discussion.
2010-10-21 22:07:17 +02:00
|
|
|
drop function array_elem_check(int);
|
2011-12-22 21:43:56 +01:00
|
|
|
--
|
|
|
|
-- Renaming
|
|
|
|
--
|
|
|
|
create domain testdomain1 as int;
|
|
|
|
alter domain testdomain1 rename to testdomain2;
|
|
|
|
alter type testdomain2 rename to testdomain3; -- alter type also works
|
|
|
|
drop domain testdomain3;
|
2012-04-03 07:11:51 +02:00
|
|
|
--
|
|
|
|
-- Renaming domain constraints
|
|
|
|
--
|
|
|
|
create domain testdomain1 as int constraint unsigned check (value > 0);
|
|
|
|
alter domain testdomain1 rename constraint unsigned to unsigned_foo;
|
|
|
|
alter domain testdomain1 drop constraint unsigned_foo;
|
|
|
|
drop domain testdomain1;
|