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-19 11:53:25
Message-ID: f683c773-6035-4e57-b494-60a2ebb5b0b5@oss.nttdata.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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.

Regards,

--
Fujii Masao
NTT DATA Japan Corporation

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2025-06-19 12:10:41 Re: pg_dump/pg_dumpall help synopses and terminology
Previous Message Tatsuo Ishii 2025-06-19 11:50:44 Re: BackendKeyData is mandatory?