Re: pg_dump does not dump domain not-null constraint's comments

Noah Misch <noah@leadboat.com>

From: Noah Misch <noah@leadboat.com>
To: Álvaro Herrera <alvherre@kurilemu.de>
Cc: jian he <jian.universality@gmail.com>, PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2025-09-13T02:02:33Z
Lists: pgsql-hackers

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Fix pg_dump COMMENT dependency for separate domain constraints.

  2. pg_dump: include comments on not-null constraints on domains, too

  3. Fix dumping of comments on invalid constraints on domains

  4. Catalog domain not-null constraints

  5. In pg_dump, don't dump a stats object unless dumping underlying table.

  6. Enable CHECK constraints to be declared NOT VALID

On Tue, Jul 15, 2025 at 03:40:38PM +0200, Álvaro Herrera wrote:
> On 2025-Jul-15, jian he wrote:
> > we should let:
> > dumpConstraint handles dumping separate "NOT VALID" domain constraints along
> > with their comments.
> > dumpDomain: handles dumping "inlined" valid (not separate) domain constraints
> > together with their comments.

This became commit 0858f0f.

> @@ -18487,8 +18493,25 @@ dumpConstraint(Archive *fout, const ConstraintInfo *coninfo)
>  										  .description = "CHECK CONSTRAINT",
>  										  .section = SECTION_POST_DATA,
>  										  .createStmt = q->data,
>  										  .dropStmt = delq->data));
> +
> +			if (coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
> +			{
> +				char	   *qtypname;
> +
> +				PQExpBuffer conprefix = createPQExpBuffer();
> +				qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
> +
> +				appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN",
> +								  fmtId(coninfo->dobj.name));
> +
> +				dumpComment(fout, conprefix->data, qtypname,
> +							tyinfo->dobj.namespace->dobj.name,
> +							tyinfo->rolname,
> +							coninfo->dobj.catId, 0, tyinfo->dobj.dumpId);

The last argument gives the dump object on which the comment has a dependency.
Since this is the case of a separately-dumped constraint, the comment needs to
depend on that constraint (coninfo), not on the domain (tyinfo):

-							coninfo->dobj.catId, 0, tyinfo->dobj.dumpId);
+							coninfo->dobj.catId, 0, coninfo->dobj.dumpId);

I didn't encounter a failure from this, but sufficient restore parallelism
might reach a failure.  A failure would look like a "does not exist" error in
the COMMENT command, due to the constraint not yet existing.
dumpTableConstraintComment() is an older case that optimally handles the last
dumpComment() arg.

In the absence of objections, I'll make it so.