Re: pg_dump misses comments on NOT NULL constraints - Mailing list pgsql-hackers

From jian he
Subject Re: pg_dump misses comments on NOT NULL constraints
Date
Msg-id CACJufxG2Maz1zz3minzzCbfDxcRgMFcye3bUv4-nX5aQau8V=Q@mail.gmail.com
Whole thread Raw
In response to Re: pg_dump misses comments on NOT NULL constraints  (Álvaro Herrera <alvherre@kurilemu.de>)
List pgsql-hackers
On Wed, Jun 18, 2025 at 11:05 PM Álvaro Herrera <alvherre@kurilemu.de> wrote:
>
> I agree that this is roughly the right approach, but I think you're
> doing it harder than it needs to be -- it might be easier to add a JOIN
> to pg_description to the big query in getTableAttrs(), and add a pointer
> to the returned string in tbinfo->notnull_comments[j] (for versions
> earlier than 18, don't add the join and have it return constant NULL).
> Then in dumpTableSchema, in the place where you added the new query,
> just scan that array and print COMMENT ON commands for each valid
> constraint where that's not a null pointer.
>

Previously I was worried about print_notnull, shouldPrintColumn.
if there is a not-null constraint that is not dumped separately, it has comments
then we should dump these comments, then no need to worry about print_notnull.

using notnull_comments saves us one more query.
however, in determineNotNullFlags we have:

                char       *default_name;
                /* XXX should match ChooseConstraintName better */
                default_name = psprintf("%s_%s_not_null", tbinfo->dobj.name,
                                        tbinfo->attnames[j]);
                if (strcmp(default_name,
                           PQgetvalue(res, r, i_notnull_name)) == 0)
                    tbinfo->notnull_constrs[j] = "";

then we can not blindly use tbinfo->notnull_constrs as the not-null
constraint name.
if tbinfo->notnull_constrs is an empty string, we need to use the above
"%s_%s_not_null" trick to get the default no-null constraint name.

Attachment

pgsql-hackers by date:

Previous
From: Dmitry Koval
Date:
Subject: Re: Add SPLIT PARTITION/MERGE PARTITIONS commands
Next
From: Thom Brown
Date:
Subject: Re: Add CASEFOLD() function.