Thread: Rowtype column and domain subfield with DEFAULT and NOT NULL constraint

Rowtype column and domain subfield with DEFAULT and NOT NULL constraint

From
Julien Tachoires
Date:
Hi,

A customer has reported us a strange behaviour regarding a rowtype 
column with a domain subfield:

test=# CREATE DOMAIN my_int_not_null_1 AS INTEGER DEFAULT 1 NOT NULL;
CREATE DOMAIN
test=# CREATE TYPE  my_int_rowtype AS (
test(#     f1 INTEGER,
test(#     f2 my_int_not_null_1
test(# );
CREATE TYPE
test=# CREATE TABLE test (id SERIAL, col1 my_int_rowtype);
CREATE TABLE
test=# INSERT INTO test (col1.f1) VALUES (1);
INSERT 0 1
test=# INSERT INTO test (id, col1.f1) VALUES (2, 1);
INSERT 0 1
test=# INSERT INTO test (col1) VALUES ((1,NULL));
ERROR:  domain my_int_not_null_1 does not allow null values
test=# SELECT * FROM test; id | col1
----+------  1 | (1,)  2 | (1,)
(2 rows)

It seems:

- the DEFAULT value (from the domain) is not inserted
- the NOT NULL constraint is no applied excepting if we set explicitly 
the value to NULL, looks like it is the same issue than before, when 
there is no DEFAULT the parser/rewriter should set the column/subfield 
to NULL.

Is build_column_default() the right place to handle that case ?

-- 
Julien Tachoires
http://dalibo.com - http://dalibo.org



Julien Tachoires <julien.tachoires@dalibo.com> writes:
> A customer has reported us a strange behaviour regarding a rowtype 
> column with a domain subfield:

Rowtypes in general do not support defaults for component fields.

> Is build_column_default() the right place to handle that case ?

It's unlikely that this is simple to change.  As an example, should
the default be inserted during a cast to the rowtype?  How about
plpgsql variable initialization?  What are you going to do about
scalar-NULL values of the rowtype (note "forbid them" is likely
to cause all sorts of collateral damage)?

But in any case, none of the examples you showed have anything to
do with build_column_default().  That would only get applied if
the INSERT's targetlist didn't mention col1 at all.
        regards, tom lane



Re: Rowtype column and domain subfield with DEFAULT and NOT NULL constraint

From
Michael Paquier
Date:
On Wed, Mar 12, 2014 at 11:16 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Julien Tachoires <julien.tachoires@dalibo.com> writes:
>> A customer has reported us a strange behaviour regarding a rowtype
>> column with a domain subfield:
>
> Rowtypes in general do not support defaults for component fields.
And what about adding a TODO item?
Support default values for component fields of rowtypes

We could as well for the time being improve the documentation to
mention that with some examples. For example with some more content on
the page of INSERT.
Regards,
-- 
Michael