Constraint's NO INHERIT option is ignored in CREATE TABLE LIKE statement

Поиск
Список
Период
Сортировка
Искать

Constraint's NO INHERIT option is ignored in CREATE TABLE LIKE statement

От:
Ildar Musin <ildar@adjust.com>
Дата:
Вложения:
Hi hackers,

My colleague Chris Travers discovered something that looks like a bug.
Let's say we have a table with a constraint that is declared as NO INHERIT.

CREATE TABLE test (
    x INT CHECK (x > 0) NO INHERIT
);
\d test
                Table "public.test"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 x      | integer |           |          |
Check constraints:
    "test_x_check1" CHECK (x > 0) NO INHERIT

Now when we want to make a copy of the table structure into a new table
the `NO INHERIT` option is ignored.

CREATE TABLE test2 (LIKE test INCLUDING CONSTRAINTS);
\d test2
               Table "public.test2"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 x      | integer |           |          |
Check constraints:
    "test_x_check1" CHECK (x > 0)

Is this a bug or expected behaviour? Just in case I've attached a patch
that fixes this.

Regards,
Ildar

Re: Constraint's NO INHERIT option is ignored in CREATE TABLE LIKE statement

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Ildar Musin  writes:
> My colleague Chris Travers discovered something that looks like a bug.
> Let's say we have a table with a constraint that is declared as NO INHERIT.
> ...
> Now when we want to make a copy of the table structure into a new table
> the `NO INHERIT` option is ignored.

Hm, I agree that's a bug, since the otherwise-pretty-detailed CREATE TABLE
LIKE documentation makes no mention of such a difference between original
and cloned constraint.

However, I'd be disinclined to back-patch, since it's barely possible
somebody out there is depending on the existing behavior.

			regards, tom lane


Re: Constraint's NO INHERIT option is ignored in CREATE TABLE LIKE statement

От:
"David G. Johnston" <david.g.johnston@gmail.com>
Дата:
On Wed, Feb 19, 2020 at 4:02 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Ildar Musin <ildar@adjust.com> writes:
> My colleague Chris Travers discovered something that looks like a bug.
> Let's say we have a table with a constraint that is declared as NO INHERIT.
> ...
> Now when we want to make a copy of the table structure into a new table
> the `NO INHERIT` option is ignored.

Hm, I agree that's a bug, since the otherwise-pretty-detailed CREATE TABLE
LIKE documentation makes no mention of such a difference between original
and cloned constraint.

However, I'd be disinclined to back-patch, since it's barely possible
somebody out there is depending on the existing behavior.

Not sure I agree with the premise that it is not supposed to be copied; is there some other object type the allows NO INHERIT that isn't copied when CREATE TABLE LIKE is used and check constraints are the odd ones out?

Inheritance is what NO INHERIT is about and CREATE TABLE LIKE pointedly doesn't setup an inheritance structure.  The documentation seems ok since saying that NO INHERIT is ignored when inheritance is not being used seems self-evident.  Sure, maybe some clarity here could be had, but its not like this comes up with any regularity.

David J.

Re: Constraint's NO INHERIT option is ignored in CREATE TABLE LIKE statement

От:
Amit Langote <amitlangote09@gmail.com>
Дата:
Вложения:
On Thu, Feb 20, 2020 at 8:02 AM Tom Lane  wrote:
> Ildar Musin  writes:
> > My colleague Chris Travers discovered something that looks like a bug.
> > Let's say we have a table with a constraint that is declared as NO INHERIT.
> > ...
> > Now when we want to make a copy of the table structure into a new table
> > the `NO INHERIT` option is ignored.
>
> Hm, I agree that's a bug, since the otherwise-pretty-detailed CREATE TABLE
> LIKE documentation makes no mention of such a difference between original
> and cloned constraint.

By the way, partitioned tables to not allow constraints that are
marked NO INHERIT.  For example,

create table b (a int check (a > 0) no inherit) partition by list (a);
ERROR:  cannot add NO INHERIT constraint to partitioned table "b"

We must ensure that partitioned tables don't accidentally end up with
one via CREATE TABLE LIKE path.  I tested Ildar's patch and things
seem fine, but it might be better to add a test.  Attached updated
patch with that taken care of.

Thanks,
Amit

Re: Constraint's NO INHERIT option is ignored in CREATE TABLE LIKE statement

От:
Amit Langote <amitlangote09@gmail.com>
Дата:
On Thu, Feb 20, 2020 at 8:20 AM David G. Johnston
 wrote:
> On Wed, Feb 19, 2020 at 4:02 PM Tom Lane  wrote:
>> Ildar Musin  writes:
>> > My colleague Chris Travers discovered something that looks like a bug.
>> > Let's say we have a table with a constraint that is declared as NO INHERIT.
>> > ...
>> > Now when we want to make a copy of the table structure into a new table
>> > the `NO INHERIT` option is ignored.
>>
>> Hm, I agree that's a bug, since the otherwise-pretty-detailed CREATE TABLE
>> LIKE documentation makes no mention of such a difference between original
>> and cloned constraint.
>>
>> However, I'd be disinclined to back-patch, since it's barely possible
>> somebody out there is depending on the existing behavior.
>
> Not sure I agree with the premise that it is not supposed to be copied; is there some other object type the allows NO INHERIT that isn't copied when CREATE TABLE LIKE is used and check constraints are the odd ones out?

Syntax currently allows only CHECK constraints to be marked NO INHERIT.

Thanks,
Amit


Re: Constraint's NO INHERIT option is ignored in CREATE TABLE LIKE statement

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Amit Langote  writes:
> On Thu, Feb 20, 2020 at 8:20 AM David G. Johnston
>  wrote:
>> Not sure I agree with the premise that it is not supposed to be copied; is there some other object type the allows NO INHERIT that isn't copied when CREATE TABLE LIKE is used and check constraints are the odd ones out?

> Syntax currently allows only CHECK constraints to be marked NO INHERIT.

Hearing no further comments, pushed, with a bit of cosmetic polishing.

			regards, tom lane


FAQ