Re: pg_dump misses comments on NOT NULL constraints

From: Fujii Masao <masao(dot)fujii(at)oss(dot)nttdata(dot)com>
To: jian he <jian(dot)universality(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org, Álvaro Herrera <alvherre(at)kurilemu(dot)de>
Subject: Re: pg_dump misses comments on NOT NULL constraints
Date: 2025-06-25 10:02:29
Message-ID: 92157594-3cd3-4402-be43-a9adeaa1c0f3@oss.nttdata.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 2025/06/19 20:53, Fujii Masao wrote:
>
>
> On 2025/06/19 14:42, jian he wrote:
>> On Wed, Jun 18, 2025 at 10:21 AM Fujii Masao
>> <masao(dot)fujii(at)oss(dot)nttdata(dot)com> wrote:
>>>
>>> I ran into another issue related to comments on NOT NULL constraints.
>>> When using CREATE TABLE ... (LIKE ... INCLUDING ALL), the NOT NULL constraints
>>> are copied, but their comments are not. For example:
>>>
>>> -----------------------------------------------------
>>> =# CREATE TABLE t (i int);
>>> =# ALTER TABLE t ADD CONSTRAINT my_not_null_i NOT NULL i;
>>> =# ALTER TABLE t ADD CONSTRAINT my_check_i CHECK (i > 0);
>>> =# COMMENT ON CONSTRAINT my_not_null_i ON t IS 'my not null for i';
>>> =# COMMENT ON CONSTRAINT my_check_i ON t IS 'my check for i';
>>>
>>> =# CREATE TABLE t_copied (LIKE t INCLUDING ALL);
>>>
>>> As shown, the comment on my_not_null_i is not copied to the new table,
>>> even though the constraint itself is. Could this be another oversight
>>> in commit 14e87ffa5c5?
>>>
>>
>> hi.
>> in transformTableLikeClausem, let cxt(CreateStmtContext) to add
>> CommentStmt should just work.
>> Please check attached, tests also added.
>
> Thanks for the patch! LGTM.
>
> Just one minor suggestion:
>
> +        /* Copy comments on not-null constraints */
> +        if (table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS)
> +        {
>
> It might be clearer to move this block after the line:
>
>     cxt->nnconstraints = list_concat(cxt->nnconstraints, lst);
>
> That would make the code a bit more readable.

I've applied this cosmetic change to the patch.
Barring objections, I'll commit the patch.

Regards,

--
Fujii Masao
NTT DATA Japan Corporation

Attachment Content-Type Size
v2-0001-Make-CREATE-TABLE-LIKE-copy-comments-on-NOT-NULL-.patch text/plain 5.2 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Ashutosh Bapat 2025-06-25 10:32:41 Re: Logrep launcher race conditions leading to slow tests
Previous Message Fujii Masao 2025-06-25 09:57:03 Re: pg_dump misses comments on NOT NULL constraints